Ignore:
Timestamp:
May 30, 2009, 5:21:43 PM (15 years ago)
Author:
vdigital
Message:

+ Language Switch code review (No cookies => session).
+ Optional $confno_flag_languages = Array() to remove flags

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0/plugins/language_switch/language_switch.inc.php

    r3368 r3375  
    2323if (!defined('LANGUAGE_SWITCH_PATH'))
    2424define('LANGUAGE_SWITCH_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
    25 function language_switch()
    26 {
    27   global $user, $template, $conf, $lang;
    28   if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
    29   $same = $user['language'];
    30   if ( isset( $_GET['lang']) )
     25class language_controler {
     26  static public function _switch()
    3127  {
    32     if ( !empty($_GET['lang'] ) and
    33       file_exists( PHPWG_ROOT_PATH.'language/'
    34       . $_GET['lang'].'/common.lang.php') )
    35     {
    36       if (is_a_guest() or is_generic())
    37       {
    38         setcookie( 'pwg_lang_switch', $_GET['lang'],
    39           time()+60*60*24*30, cookie_path() );
     28    global $user;
     29    if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
     30    $same = $user['language'];
     31    if ( isset( $_GET['lang']) ) {
     32      if ( !empty($_GET['lang'] ) and file_exists( PHPWG_ROOT_PATH.'language/'
     33        . $_GET['lang'].'/common.lang.php') ) {
     34        if (is_a_guest() or is_generic()) {
     35          pwg_set_session_var( 'lang_switch', $_GET['lang'] );
     36        } else {
     37          $query = 'UPDATE '.USER_INFOS_TABLE.'
     38          SET language = \''.$_GET['lang'].'\'
     39          WHERE user_id = '.$user['id'].'
     40          ;';
     41          pwg_query($query);
     42        }
     43        $user['language'] = $_GET['lang'];
    4044      }
    41       else
    42       {
    43         $query = 'UPDATE '.USER_INFOS_TABLE.'
    44         SET language = \''.$_GET['lang'].'\'
    45         WHERE user_id = '.$user['id'].'
    46         ;';
    47         pwg_query($query);
     45    }
     46    elseif ((is_a_guest() or is_generic())) {
     47      $user['language'] = pwg_get_session_var( 'lang_switch', $user['language'] );
     48    }
     49  // Reload language only if it isn't the same one
     50    if ( $same !== $user['language']) {
     51      load_language('common.lang', '', array('language'=>$user['language']) );
     52      load_language('local.lang', '', array('language'=>$user['language'], 'no_fallback'=>true) );
     53      if (defined('IN_ADMIN') and IN_ADMIN) { // Never currently
     54        load_language('admin.lang', '', array('language'=>$user['language']) );
    4855      }
    49       $user['language'] = $_GET['lang'];
    5056    }
    5157  }
    52 // Users have $user['language']
    53 // Guest or generic members will use their cookied language !
    54   if ((is_a_guest() or is_generic())
    55     and isset( $_COOKIE['pwg_lang_switch'] ) )
     58  static public function _flags()
    5659  {
    57     $user['language'] = $_COOKIE['pwg_lang_switch'];
    58   }
    59 // Reload language only if it isn't the same one
    60   if ( $same !== $user['language'])
    61   {
    62     load_language('common.lang', '', array('language'=>$user['language']) );
    63     load_language('local.lang', '', array('language'=>$user['language'], 'no_fallback'=>true) );
    64     if (defined('IN_ADMIN') and IN_ADMIN)
    65     {
    66       load_language('admin.lang', '', array('language'=>$user['language']) );
     60    global $user, $template, $conf;
     61    $available_lang = get_languages();
     62    if ( isset($conf['no_flag_languages']) )
     63      $available_lang = array_diff_key($available_lang, array_flip($conf['no_flag_languages']));
     64    $url_starting = $_SERVER['REQUEST_URI'];
     65    if ( isset( $_GET['lang']) ) {
     66      $pos = stripos  ( $url_starting  , '&lang=' );
     67      if (is_numeric($pos) and $pos > 0) $url_starting = substr($url_starting, 0, $pos);
    6768    }
     69    $pos = stripos($url_starting, script_basename());
     70    if (is_numeric($pos)) $url_starting = substr($url_starting, $pos);
     71    foreach ( $available_lang as $code => $displayname ) {
     72      $qlc = array (
     73        'url' => add_url_params( $url_starting, array('lang'=> $code) ),
     74        'alt' => ucwords( $displayname ),
     75        'img' => 'plugins/language_switch/icons/' . $code . '.jpg',
     76        );
     77      if ( $code !== $user['language'] and file_exists(PHPWG_ROOT_PATH.$qlc['img']) )
     78        $lsw['flags'][$code] = $qlc ;
     79      else $lsw['Active'] = $qlc;
     80    }
     81    $template->set_filename('language_flags', dirname(__FILE__) . '/flags.tpl');
     82    $lsw['side'] = ceil(sqrt(count($available_lang)));
     83    $template->assign(array(
     84      'lang_switch'=> $lsw,
     85      'LANGUAGE_SWITCH_PATH' => LANGUAGE_SWITCH_PATH,
     86    ));
     87    $flags = $template->parse('language_flags',true);
     88    $template->clear_assign('lang_switch');
     89    $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
     90    // TODO : Try to cache $flags and $user['language'] in $_SESSION for performance
    6891  }
    6992}
    70 //if ( isset( $_GET['lang']) ) { redirect( make_index_url() ); }
    71 function Lang_flags()
    72 {
    73   global $user, $template;
    74   $available_lang = get_languages();
    75         $lsw = array();
    76   foreach ( $available_lang as $code => $displayname )
    77   {
    78     $qlc_url = add_url_params( make_index_url(), array( 'lang' => $code ) );
    79     $qlc_alt = ucwords( $displayname );
    80     $qlc_img =  'plugins/language_switch/icons/'
    81        . $code . '.jpg';
    82     if ( $code !== $user['language'] and file_exists(PHPWG_ROOT_PATH.$qlc_img) )
    83     {
    84       $lsw['flags'][$code] = Array(
    85                                 'url' => $qlc_url,
    86                                 'alt' => $qlc_alt,
    87                                 'img' => $qlc_img,
    88                         ) ;
    89     } else {
    90                         $lsw['Active'] = Array(
    91                                 'url' => $qlc_url,
    92                                 'alt' => $qlc_alt,
    93                                 'img' => $qlc_img,
    94                         );
    95                 }
    96   }
    97         $template->set_filename('language_flags', dirname(__FILE__) . '/flags.tpl');
    98         $lsw['side'] = ceil(sqrt(count($available_lang)));
    99         $template->assign(array(
    100                 'lang_switch'=> $lsw,
    101                 'LANGUAGE_SWITCH_PATH' => LANGUAGE_SWITCH_PATH,
    102         ));
    103         $flags = $template->parse('language_flags',true);
    104         $template->clear_assign('lang_switch');
    105         $template->concat( 'PLUGIN_INDEX_ACTIONS', $flags);
    106         // In state of making a $flags each time TODO Caching $flags[$user['language']]
     93  /* {html_head} usage function */
     94  /* See flags.tpl for example (due no catenation available) */
     95if (!function_exists('Componant_exists')) {
     96  function Componant_exists($path, $file)
     97    { return file_exists( $path . $file); }
    10798}
    10899
    109 if (!function_exists('Componant_exists')) {
    110         function Componant_exists($path, $file)
    111         {
    112           return file_exists( $path . $file);
    113         }
    114 }
    115 
    116 // Should be deleted later
    117 function Lang_flags_previous_function()
    118 {
    119   global $user, $template;
    120   $available_lang = get_languages();
    121   foreach ( $available_lang as $code => $displayname )
    122   {
    123     $qlc_url = add_url_params( make_index_url(), array( 'lang' => $code ) );
    124     $qlc_alt = ucwords( $displayname );
    125     $qlc_title =  $qlc_alt;
    126     $qlc_img =  'plugins/language_switch/icons/'
    127        . $code . '.gif';
    128 
    129     if ( $code !== $user['language'] and file_exists(PHPWG_ROOT_PATH.$qlc_img) )
    130     {
    131       $template->concat( 'PLUGIN_INDEX_ACTIONS',
    132         '<li><a href="' . $qlc_url . '" ><img src="' . get_root_url().$qlc_img . '" alt="'
    133         . $qlc_alt . '" title="'
    134         . $qlc_title . '" style="border: 1px solid #000000; '
    135         . ' margin: 0px 2px;" /></a></li>');
    136     }
    137   }
    138 }
    139100?>
Note: See TracChangeset for help on using the changeset viewer.