includes/lang.php
changeset 668 0631e4de5de6
parent 654 18dbf386d356
child 685 17ebe24cdf85
equal deleted inserted replaced
667:72818d2bf336 668:0631e4de5de6
    66    */
    66    */
    67   
    67   
    68   var $debug = false;
    68   var $debug = false;
    69   
    69   
    70   /**
    70   /**
       
    71    * List of available filters to pass variables through.
       
    72    * @var array
       
    73    * @access private
       
    74    */
       
    75   
       
    76   protected $filters = array();
       
    77   
       
    78   /**
    71    * Constructor.
    79    * Constructor.
    72    * @param int|string Language ID or code to load.
    80    * @param int|string Language ID or code to load.
    73    */
    81    */
    74   
    82   
    75   function __construct($lang)
    83   function __construct($lang)
   111     $row = $db->fetchrow();
   119     $row = $db->fetchrow();
   112     
   120     
   113     $this->lang_id   = intval( $row['lang_id'] );
   121     $this->lang_id   = intval( $row['lang_id'] );
   114     $this->lang_code = $row['lang_code'];
   122     $this->lang_code = $row['lang_code'];
   115     $this->lang_timestamp = $row['last_changed'];
   123     $this->lang_timestamp = $row['last_changed'];
       
   124     
       
   125     $this->register_filter('htmlsafe', 'htmlspecialchars');
       
   126     $this->register_filter('urlencode', 'urlencode');
       
   127     $this->register_filter('rawurlencode', 'rawurlencode');
       
   128     
       
   129     $code = $plugins->setHook('lang_init');
       
   130     foreach ( $code as $cmd )
       
   131     {
       
   132       eval($cmd);
       
   133     }
   116   }
   134   }
   117   
   135   
   118   /**
   136   /**
   119    * Fetches language strings from the database, or a cache file if it's available.
   137    * Fetches language strings from the database, or a cache file if it's available.
   120    * @param bool If true (default), allows the cache to be used.
   138    * @param bool If true (default), allows the cache to be used.
   589     ob_end_clean();
   607     ob_end_clean();
   590     return $contents;
   608     return $contents;
   591   }
   609   }
   592   
   610   
   593   /**
   611   /**
       
   612    * Registers a filter, a function that strings can be passed through to change the string somehow (e.g. htmlspecialchars)
       
   613    * @param string Filter name. Lowercase alphanumeric (htmlsafe)
       
   614    * @param callback Function to call.
       
   615    * @return bool True on success, false if some error occurred
       
   616    */
       
   617   
       
   618   public function register_filter($filter_name, $filter_function)
       
   619   {
       
   620     if ( !is_string($filter_function) && !is_array($filter_function) )
       
   621     {
       
   622       return false;
       
   623     }
       
   624     if ( ( is_string($filter_function) && !function_exists($filter_function) ) || ( is_array($filter_function) && !method_exists(@$filter_function[0], @$filter_function[1]) ) )
       
   625     {
       
   626       return false;
       
   627     }
       
   628     if ( !preg_match('/^[a-z0-9_]+$/', $filter_name) )
       
   629     {
       
   630       return false;
       
   631     }
       
   632     $this->filters[$filter_name] = $filter_function;
       
   633   }
       
   634   
       
   635   /**
   594    * Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
   636    * Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
   595    * in the default language. If even then the string can't be found, this function will return what was passed to it.
   637    * in the default language. If even then the string can't be found, this function will return what was passed to it.
   596    *
   638    *
   597    * This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
   639    * This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
   598    * of 'foo' => 'foo substitute'.
   640    * of 'foo' => 'foo substitute'.
   677    * @return string
   719    * @return string
   678    */
   720    */
   679   
   721   
   680   function substitute($string, $subs)
   722   function substitute($string, $subs)
   681   {
   723   {
   682     preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
   724     preg_match_all('/%this\.([a-z0-9_]+)((?:\|(?:[a-z0-9_]+))*)%/', $string, $matches);
   683     if ( count($matches[0]) > 0 )
   725     if ( count($matches[0]) > 0 )
   684     {
   726     {
   685       foreach ( $matches[1] as $i => $string_id )
   727       foreach ( $matches[1] as $i => $string_id )
   686       {
   728       {
   687         $result = $this->get($string_id);
   729         $result = $this->get($string_id);
   688         $string = str_replace($matches[0][$i], $result, $string);
   730         $string = str_replace($matches[0][$i], $this->process_filters($result, $matches[2][$i]), $string);
   689       }
   731       }
   690     }
   732     }
   691     preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
   733     preg_match_all('/%config\.([a-z0-9_]+)((?:\|(?:[a-z0-9_]+))*)%/', $string, $matches);
   692     if ( count($matches[0]) > 0 )
   734     if ( count($matches[0]) > 0 )
   693     {
   735     {
   694       foreach ( $matches[1] as $i => $string_id )
   736       foreach ( $matches[1] as $i => $string_id )
   695       {
   737       {
   696         $result = getConfig($string_id);
   738         $result = getConfig($string_id, '');
   697         $string = str_replace($matches[0][$i], $result, $string);
   739         $string = str_replace($matches[0][$i], $this->process_filters($result, $matches[2][$i]), $string);
   698       }
   740       }
   699     }
   741     }
   700     foreach ( $subs as $key => $value )
   742     preg_match_all('/%([a-z0-9_]+)((?:\|(?:[a-z0-9_]+))*)%/', $string, $matches);
   701     {
   743     if ( count($matches[0]) > 0 )
   702       $subs[$key] = strval($value);
   744     {
   703       $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
   745       foreach ( $matches[1] as $i => $string_id )
       
   746       {
       
   747         if ( isset($subs[$string_id]) )
       
   748         {
       
   749           $string = str_replace($matches[0][$i], $this->process_filters($subs[$string_id], $matches[2][$i]), $string);
       
   750         }
       
   751       }
   704     }
   752     }
   705     return ( $this->debug ) ? "$string*" : $string;
   753     return ( $this->debug ) ? "$string*" : $string;
       
   754   }
       
   755   
       
   756   /**
       
   757    * Processes filters to a language string.
       
   758    * @param string Unprocessed string
       
   759    * @param string Filter list (format: |filter1|filter2|filter3, initial pipe is important); can also be an array if you so desire
       
   760    * @return string
       
   761    */
       
   762   
       
   763   function process_filters($string, $filters)
       
   764   {
       
   765     if ( !empty($filters) )
       
   766     {
       
   767       $filters = trim($filters, '|');
       
   768       $filters = explode('|', $filters);
       
   769       foreach ( $filters as $filter )
       
   770       {
       
   771         if ( isset($this->filters[$filter]) )
       
   772         {
       
   773           $result = @call_user_func($this->filters[$filter], $string);
       
   774           if ( is_string($result) )
       
   775           {
       
   776             $string = $result;
       
   777           }
       
   778         }
       
   779       }
       
   780     }
       
   781     return $string;
   706   }
   782   }
   707   
   783   
   708 } // class Language
   784 } // class Language
   709 
   785 
   710 ?>
   786 ?>