[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/include/ -> Lang.class.php (source)

   1  <?php
   2  /* 
   3   * phpMyVisites : website statistics and audience measurements
   4   * Copyright (C) 2002 - 2006
   5   * http://www.phpmyvisites.net/ 
   6   * phpMyVisites is free software (license GNU/GPL)
   7   * Authors : phpMyVisites team
   8  */
   9  
  10  // $Id: Lang.class.php 215 2007-05-01 13:03:34Z matthieu_ $
  11  
  12  
  13  
  14  require_once  INCLUDE_PATH . "/core/include/Cookie.class.php";
  15  require_once  INCLUDE_PATH . "/core/include/Request.class.php";
  16  require_once  INCLUDE_PATH . "/core/include/commonDB.php";
  17  require_once  INCLUDE_PATH . "/core/include/Logs.functions.php";
  18  
  19  class Lang
  20  {
  21      // content of the config file
  22      var $langAvailable;
  23      
  24      // lang file 
  25      var $file;
  26      
  27      // config file
  28      var $fileAdress;
  29      
  30  	function Lang( )
  31      {
  32          $c = new Cookie(COOKIE_NAME_VIEW);
  33          
  34          // look if reload lang file
  35          $this->fileAdress = INCLUDE_PATH . "/config/lang_available.php";
  36          if(!file_exists( $this->fileAdress ))
  37          {
  38              $this->reloadLangFile();
  39          }
  40          else
  41          {
  42              require $this->fileAdress;
  43              
  44              if(!isset($langAvailable))
  45              {
  46                  print("There is a problem with the /config/lang_available.php file.");
  47                  $langAvailableFile = INCLUDE_PATH . "/config/lang_available.php";
  48                  if(!unlink($langAvailableFile))
  49                  {
  50                      print("Error when trying to delete $langAvailableFile. You have to delete the file $langAvailableFile manually.");
  51                  }
  52                  print("<br>Please refresh this page");
  53                  exit;
  54              }
  55              
  56              $this->langAvailable = $langAvailable;
  57          }
  58          
  59          $langRequest = Request::getLang();
  60          if(!file_exists( LANGS_PATH . "/". $langRequest))
  61          {
  62              // cookie ?
  63              if(($langRequest = $c->getVar('lang'))
  64                  && file_exists(LANGS_PATH . "/" . $langRequest))
  65              {
  66                  $this->file = $langRequest;
  67              }
  68              else
  69              {
  70                  // default lang?
  71                  if( defined('INTERFACE_DEFAULT_LANG')
  72                      && file_exists(LANGS_PATH . "/" . INTERFACE_DEFAULT_LANG))
  73                  {
  74                      $this->file = INTERFACE_DEFAULT_LANG;
  75                  }
  76              }
  77              
  78          }
  79          else
  80          {
  81              $this->file = $langRequest;
  82          }
  83                  
  84          // if lang not found in REQUEST + COOKIE + not set with INTERFACE_DEFAULT_LANG
  85          // then we try to choose the better lang
  86          if( !isset($this->file)
  87              || !strpos( $this->file, 'utf-8.php')
  88              || strpos( $this->file, '..') !== FALSE
  89              )
  90          {
  91              $this->file = $this->getNearestLang();
  92          }
  93          
  94          $this->setLang();
  95          
  96          // cookie
  97          $c->setVar('lang', $this->file);
  98          $c->save();
  99          
 100          //print($c->toString());
 101          //print($this->file);
 102      }
 103      
 104  	function setLangFile( $newFile )
 105      {
 106          $this->file = $newFile;
 107      }
 108      
 109      /**
 110       * Singleton
 111       */
 112      function &getInstance()
 113      {
 114          static $instance;
 115          
 116          if (!isset($instance)){
 117              $c = __CLASS__;
 118              $instance = new $c;
 119          }
 120          return $instance;
 121      }
 122      
 123  	function setLang()
 124      {
 125          $lang = array();
 126          require LANGS_PATH . "/" . $this->file;
 127  
 128          if(!is_array($lang) 
 129              || sizeof($lang) == 0 )
 130          {
 131              print("<b>Error! </b> The lang file ".LANGS_PATH . "/" . $this->file." may be corrupted (incorrect structure). Please reupload a new version of this file, then refresh this page.");
 132              exit;
 133          }
 134          $GLOBALS['lang'] = $lang;
 135      }
 136      
 137  	function getFileName()
 138      {
 139          return $this->file;
 140      }
 141      
 142  	function setNewLang( $file )
 143      {
 144          $this->setLangFile($file);
 145          $this->setLang();
 146      }
 147      
 148  	function setRandomLang()
 149      {
 150          $keys = array_keys($this->langAvailable);    
 151          $randomKey = rand( 0, sizeof($this->langAvailable) - 2 ); // -1 because of the key default lang at the end
 152          $this->setNewLang( $keys[$randomKey] );
 153      }
 154      
 155      /**
 156       * useless from now on? 2.2RC1
 157       */
 158  	function getNearestLang()
 159      {        
 160          $country = getCountry( 
 161                          getHostnameExt(
 162                                  getHost(
 163                                      getIp()
 164                                          )
 165                                      ), 
 166                          secureVar(@$_SERVER['HTTP_ACCEPT_LANGUAGE'])
 167              );
 168          
 169          foreach($this->langAvailable as $key => $langInfo)
 170          {
 171              if($langInfo[3] == $country)
 172              {
 173                  return $key;
 174              }
 175          }
 176          return $this->langAvailable['default_lang'];
 177      }
 178      
 179      /**
 180       * Returns array key => value
 181       * key = fileName
 182       * value = Lang name in english (or french if french langage is used)
 183       * Note: an element with the key "other" is added, containing the string "Other?" translated.
 184       * 
 185       * @return array 
 186       */
 187  	function getArrayLangs()
 188      {
 189          $return = array();
 190          
 191          // french, print languages in french
 192          if(substr($this->getFileName(), 0, 2) === 'fr')
 193          {
 194              $key = 0;
 195          }
 196          else
 197          {
 198              $key = 1;
 199          }
 200          
 201          foreach($this->langAvailable as $file => $a_info)
 202          {
 203              if(is_array($a_info))
 204              {
 205                  $return[$file] = $a_info[$key];
 206              }
 207          }
 208          ksort($return);
 209          $return['other'] = $GLOBALS['lang']['generique_autrelangure'];
 210          
 211          return $return;
 212      }
 213      
 214  	function getArrayTranslators()
 215      {
 216          $return = array();
 217          
 218          // french, print languages in french
 219          if(substr($this->getFileName(), 0, 2) === 'fr')
 220          {
 221              $key = 0;
 222          }
 223          else
 224          {
 225              $key = 1;
 226          }
 227          
 228          foreach($this->langAvailable as $file => $a_info)
 229          {
 230              if(is_array($a_info))
 231              {
 232                  $return[] = array(
 233                      'lang_name' => $a_info[$key],
 234                      'lang_file' => $file,
 235                      'translator_name' => $a_info[4],
 236                      'translator_email' => $a_info[5]
 237                      );
 238              }
 239          }
 240          
 241          $return[] = array(
 242              'lang_name' => $GLOBALS['lang']['generique_autrelangure'],
 243              'translator_name' => $GLOBALS['lang']['generique_vous'],
 244              'lang_file' => 'other',
 245              'translator_email' => 'matthieu.aubry@gmail.com?subject=I Want To Become A Powerfull Translator'
 246              );
 247          return $return;
 248      }
 249      
 250  	function reloadLangFile()
 251      {        
 252          $handle=opendir(LANGS_PATH );
 253          while ($file = readdir($handle)) 
 254          {
 255              if (strpos($file, '-utf-8.php'))
 256              {
 257                  $lang=array();
 258                  require LANGS_PATH . "/". $file;
 259                  
 260                  if(count($lang) === 0)
 261                  {
 262                      trigger_error( "Problems while loading Lang file. Try to upload the /langs/ directory", E_USER_ERROR );
 263                  }
 264                  
 265                  $langAvailable[$file] = array(
 266                      $lang['lang_libelle_fr'],
 267                      $lang['lang_libelle_en'],
 268                      $lang['charset'],
 269                      $lang['lang_iso'],
 270                      $lang['auteur_nom'],
 271                      $lang['auteur_email']
 272                  );
 273              }
 274          }
 275          closedir($handle);
 276          
 277          // sort by english language name
 278  		function sortLangArray($a,$b)
 279          {
 280              return ($a[1] < $b[1])  ? -1 : 1;;
 281          }
 282          uasort($langAvailable,"sortLangArray");
 283          
 284          // used in getNearestLang but it seems useless since the introduction of
 285          // interface_default_lang
 286          // in 2.2RC1
 287          $langAvailable['default_lang'] = defined('INTERFACE_DEFAULT_LANG') ? INTERFACE_DEFAULT_LANG : 'en-utf-8.php';
 288          
 289          saveConfigFile($this->fileAdress, $langAvailable, 'langAvailable');
 290          
 291          $this->langAvailable = $langAvailable;
 292      }
 293      
 294  	function getFontName()
 295      {
 296          if(isset($GLOBALS['languageFonts'][$GLOBALS['lang']['lang_iso']]))
 297          {
 298              return $GLOBALS['languageFonts'][$GLOBALS['lang']['lang_iso']];
 299          }
 300          else
 301          {
 302              return $GLOBALS['defaultFont'];
 303          }
 304      }
 305  
 306  	function addPluginLangFile ($p_pluginsPathLang, $p_defaultFileName = "en-utf-8.php") 
 307      {
 308          $langInstance =& Lang::getInstance();
 309          
 310          if (($p_pluginsPathLang != "") && (substr($p_pluginsPathLang, -1) != "/")) 
 311          {
 312              $p_pluginsPathLang .= "/";
 313          }
 314          
 315          // Set default laguage
 316          $lang = array();
 317          if (is_file (PLUGINS_PATH.$p_pluginsPathLang .$p_defaultFileName)) 
 318          {
 319              require PLUGINS_PATH.$p_pluginsPathLang.$p_defaultFileName;
 320              $GLOBALS['lang'] = array_merge($GLOBALS['lang'], $lang);
 321          }
 322          
 323          // Try to set the good lang file
 324          $lang = array();
 325          if (is_file (PLUGINS_PATH.$p_pluginsPathLang .$langInstance->getFileName())) 
 326          {
 327              require PLUGINS_PATH.$p_pluginsPathLang .$langInstance->getFileName();
 328              $GLOBALS['lang'] = array_merge($GLOBALS['lang'], $lang);
 329          }
 330          
 331      }
 332  }
 333  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics