* @link http://nav.mageia.org/ * * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+ * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License aspublished by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. */ // definition class NCache { function __construct() { } /** * Factory. * * @param string $path where cache file store is located, relative to app path. * @param integer $timeout in seconds * * @return NCache */ public static function build($path, $timeout = 3600) { $path = __DIR__ . '/' . $path; if (!is_dir($path)) return null; if ($timeout < 60) $timeout = 60; $i = new self; $i->_path = $path; $i->_timeout = $timeout; return $i; } /** * Get value for $key. * * @param mixed $key * * @return mixed */ function get($key = null) { if (is_null($key)) return false; $filename = $this->_get_filename($key); if ($this->_is_valid_file($filename, $this->_timeout)) { return unserialize(file_get_contents($filename)); } return null; } /** * Save $value under $key. * * @param mixed $key * @param mixed $value */ function set($key, $value) { if (is_null($key)) return false; $filename = $this->_get_filename($key); file_put_contents($filename, serialize($value)); return true; } /** * Get cache file from key. * * @param mixed $key * * @return string */ private function _get_filename($key) { $key = hash('sha1', serialize($key)); return $this->_path . '/' . $key . '.cache'; } /** * Check that the cache file exists and has not expired. * * @param string $filename * * @return boolean */ private function _is_valid_file($filename, $timeout) { if (!file_exists($filename)) { //error_log(sprintf('Could not find %s', $filename), 0); return false; } if (filemtime($filename) + $timeout < time()) { //error_log(sprintf('%s timestamp expired (timeout was %ds.).', $filename, $timeout)); unlink($filename); return false; } //error_log(sprintf('Found %s', $filename)); return true; } } class l10n { // public static $t; /** * Load langs/$lang.lang into global $_t array. * * @param string $lang * * @return void */ public static function load($lang) { global $_t; $_t = array(); if ($lang == 'en') return; $po_file = __DIR__ . '/langs/' . $lang . '.po'; $cache_file = __DIR__ . '/var/tmp/cache/nav_lang_' . $lang . '.php'; $po_ts = filemtime($po_file); if (file_exists($cache_file)) { include $cache_file; if ($_ts > $po_ts) return; } if (file_exists($po_file)) { $dictionary = phpmo_parse_po_file($po_file); foreach ($dictionary as $key => $value) { if ($key != '') { if ($value['msgstr'][0] != '') { $_t[trim($key)] = trim($value['msgstr'][0]); } else { $_t[trim($key)] = trim($key); } } } $_t_data = var_export($_t, true); $cache = <<
  • %s
  • ', str_replace( array('$L', '$S'), array($lang, $vhost), $i[1] ), $i[0], $i[3], $i[2] ); } if (!is_null($inject)) $s[] = sprintf('
  • %s
  • ', $inject); $s = implode($s); $h = sprintf('', $s); if ($wrap) $h = sprintf('
    %s
    ', $h); if (!is_null($cache)) $cache->set($key, $h); return $h; } /** * Returns CSS definition ready to be inserted into a HTML document. * * @return string */ function _mgnav_style() { if ( defined('ALIGNMENT') && constant('ALIGNMENT') == 'Center' ){ return ''; } else { return ''; } } /** * Get the primary language subtag only.

    */ function _lang_check($s = null) { if (is_null($s)) { return 'en'; } $supported = array( 'ast', 'cs', 'de', 'el', 'en', 'eo', 'es', 'et', 'fi', 'fr', 'id', 'it', 'lv', 'nb', 'nl', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sl', 'sv', 'tr', 'uk', 'ur', 'zh-cn', 'zh-tw' ); if (in_array($s, $supported)) return $s; $sub = explode('-', $s); $sub = strtolower($sub[0]); if (in_array($sub, $supported)) return $sub; return 'en'; }