[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/public_html/ -> switchlang.php (source)

   1  <?php
   2  
   3  /* Reminder: always indent with 4 spaces (no tabs). */
   4  // +---------------------------------------------------------------------------+
   5  // | Geeklog 1.4                                                               |
   6  // +---------------------------------------------------------------------------+
   7  // | switchlang.php                                                            |
   8  // |                                                                           |
   9  // | Switch the user's language                                                |
  10  // +---------------------------------------------------------------------------+
  11  // | Copyright (C) 2006 by the following authors:                              |
  12  // |                                                                           |
  13  // | Authors: Dirk Haun         - dirk AT haun-online DOT de                   |
  14  // |          based on earlier works by Euan McKay and LWC                     |
  15  // +---------------------------------------------------------------------------+
  16  // |                                                                           |
  17  // | This program is free software; you can redistribute it and/or             |
  18  // | modify it under the terms of the GNU General Public License               |
  19  // | as published by the Free Software Foundation; either version 2            |
  20  // | of the License, or (at your option) any later version.                    |
  21  // |                                                                           |
  22  // | This program is distributed in the hope that it will be useful,           |
  23  // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
  24  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
  25  // | GNU General Public License for more details.                              |
  26  // |                                                                           |
  27  // | You should have received a copy of the GNU General Public License         |
  28  // | along with this program; if not, write to the Free Software Foundation,   |
  29  // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
  30  // |                                                                           |
  31  // +---------------------------------------------------------------------------+
  32  //
  33  // $Id: switchlang.php,v 1.3 2006/10/03 09:19:45 dhaun Exp $
  34  
  35  require_once  ('lib-common.php');
  36  
  37  /**
  38  * Switch language in a URL.
  39  *
  40  * @param    string  $url        current URL
  41  * @param    string  $newlang    new language to switch to
  42  * @param    string  $oldlang    old, i.e. current language
  43  * @return   string              new URL after the language switch
  44  *
  45  */
  46  function switch_language ($url, $newlang, $oldlang)
  47  {
  48      global $_CONF;
  49  
  50      $retval = '';
  51  
  52      if (empty ($newlang) || empty ($oldlang) ||
  53              (strlen ($newlang) != strlen ($oldlang))) {
  54          return $url;
  55      }
  56  
  57      $lang_len = strlen ($oldlang);
  58      if ((strpos ($url, '&') === false) && (strpos ($url, '?') === false) &&
  59              $_CONF['url_rewrite']) {
  60          // for "rewritten" URLs we assume that the first parameter after
  61          // the script name is the ID, e.g. /article.php/story-id-here_en
  62          $changed = false;
  63          $p = explode ('/', $url);
  64          for ($i = 0; $i < count ($p); $i++) {
  65              if (substr ($p[$i], -4) == '.php') {
  66                  // found the script name - assume next parameter is the ID
  67                  if (isset ($p[$i + 1])) {
  68                      if (substr ($p[$i + 1], -($lang_len + 1)) == '_' . $oldlang) {
  69                          $p[$i + 1] = substr_replace ($p[$i + 1], $newlang,
  70                                                       -$lang_len);
  71                          $changed = true;
  72                      }
  73                  }
  74                  break;
  75              }
  76          }
  77          if ($changed) {
  78              // merge the pieces back together
  79              $url = implode ('/', $p);
  80          }
  81  
  82          $retval = $url;
  83      } else { // URL contains '?' or '&'
  84          $url = explode ('&', $url);
  85          $urlpart = $url[0];
  86          if (count ($url) > 1) {
  87              array_shift ($url);
  88              $extra_vars = '&' . implode ('&', $url);
  89          } else {
  90              $extra_vars = '';
  91          }
  92  
  93          if (substr ($urlpart, -($lang_len + 1)) == '_' . $oldlang) {
  94              $urlpart = substr_replace ($urlpart, $newlang, -$lang_len);
  95          }
  96  
  97          $retval = $urlpart . $extra_vars;
  98      }
  99  
 100      return $retval;
 101  }
 102  
 103  
 104  // MAIN
 105  $ret_url = '';
 106  if (isset ($_SERVER['HTTP_REFERER'])) {
 107      if (strpos ($_SERVER['HTTP_REFERER'], $_CONF['site_url']) !== false) {
 108          $ret_url = $_SERVER['HTTP_REFERER'];
 109      }
 110  }
 111  
 112  // if not allowed, just ignore and return
 113  if ($_CONF['allow_user_language'] == 1) {
 114  
 115      COM_setArgNames (array ('lang'));
 116  
 117      $lang = strtolower (COM_applyFilter (COM_getArgument ('lang')));
 118      $lang = preg_replace( '/[^a-z0-9\-_]/', '', $lang );
 119      $oldlang = COM_getLanguageId ();
 120  
 121      // do we really have a new language to switch to?
 122      if (!empty ($lang) && array_key_exists ($lang, $_CONF['language_files'])) {
 123  
 124          // does such a language file exist?
 125          $langfile = $_CONF['language_files'][$lang];
 126          if (is_file ($_CONF['path_language'] . $langfile . '.php')) {
 127  
 128              // Set the language cookie.
 129              // Mainly used for the anonymous user so the rest of this session
 130              // will remain in their selected language
 131              setcookie ($_CONF['cookie_language'], $langfile, time() + 31536000,
 132                         $_CONF['cookie_path'], $_CONF['cookiedomain'],
 133                         $_CONF['cookiesecure']);
 134  
 135              // if user is not anonymous, store the preference in the database
 136              if (isset ($_USER['uid']) && ($_USER['uid'] > 1)) {
 137                  DB_query ("UPDATE {$_TABLES['users']} SET language = '{$langfile}' WHERE uid = {$_USER['uid']}");
 138              }
 139          }
 140      }
 141  
 142      // Change the language ID if needed
 143      if (!empty ($ret_url) && !empty ($lang) && !empty ($oldlang)) {
 144          $ret_url = switch_language ($ret_url, $lang, $oldlang);
 145      }
 146  }
 147  
 148  // if the user didn't come from our site, send them to our index page
 149  if (empty ($ret_url)) {
 150      $ret_url = $_CONF['site_url'] . '/';
 151  }
 152  
 153  header ("Location: $ret_url");
 154  
 155  ?>


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics