1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
<?php
/**
* mageia.org global nav bar utilities.
*
* PHP version 5.4
*
* @category Mageia
* @package Mageia\Web\nav
* @author rda <rda@mageia.org>
* @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 = <<<P
<?php
/**! Generated. Do not edit. */
// filemtime($po_file)
\$_ts = $po_ts;
// $lang strings
global \$_t;
\$_t = $_t_data;
P;
file_put_contents($cache_file, $cache);
}
}
/**
* Get value for key $s in global array $_t.
*
* @param string $s
*
* @return string
*/
public static function _t($s) {
if (trim($s) == '')
return '';
global $_t;
$s = array_key_exists($s, $_t) ? $_t[$s] : $s;
$s = trim(str_replace(array('{ok}', '{OK}', '{Ok}', '{oK}'), '', $s));
return $s;
}
}
/**
* Produce navigation HTML code.
*
* @param boolean $wrap = false should it be wrapped in a <header id="nav" /> element?
* @param string $lang = 'en'
* @param string $inject = null
* @param string $vhost = 'www.mageia.org'
* @param object $cache
*
* @return string HTML code
*/
function _mgnav_html($wrap = false, $lang = 'en', $inject = null, $vhost = 'www.mageia.org', $cache = null)
{
$key = array($wrap, $lang, $inject, $vhost);
if (!is_null($cache) && ($h = $cache->get($key))) {
apache_note('navCacheHit', 1);
return $h;
}
apache_note('navCacheHit', 0);
$lang = _lang_check($lang);
l10n::load($lang, $cache);
$tn = array(
array('mageia', '//$S/$L/map/', 'Mageia', l10n::_t('Go to mageia.org site map.')),
array('about', '//$S/$L/about/', l10n::_t('About us'), l10n::_t('Learn more about Mageia.')),
array('downloads', '//$S/$L/downloads/', l10n::_t('Downloads'), l10n::_t('Download Mageia ISO and updates.')),
array('support', '//$S/$L/support/', l10n::_t('Support'), l10n::_t('Get support from Mageia community.')),
array('wiki', '//wiki.mageia.org/', l10n::_t('Wiki'), l10n::_t('Wiki of the Mageia Community')),
array('doc', '//$S/$L/doc/', l10n::_t('Docs'), l10n::_t('Documentations of Mageia')),
array('community', '//$S/$L/community/', l10n::_t('Community'), l10n::_t('')),
array('contribute', '//$S/$L/contribute/', l10n::_t('Contribute'), l10n::_t('You too can build Mageia with us!')),
array('donate', '//$S/$L/donate/', l10n::_t('Donate'), l10n::_t('')),
array('you', '//identity.mageia.org/', l10n::_t('You'), l10n::_t('Your Mageia online account.')),
array('contact', '//$S/$L/contact/', l10n::_t('Contact'), l10n::_t('Contact Us'))
// <search>
);
$s = array();
foreach ($tn as $i) {
$s[] = sprintf('<li><a href="%s" class="%s" title="%s">%s</a></li>',
str_replace(
array('$L', '$S'),
array($lang, $vhost),
$i[1]
),
$i[0],
$i[3],
$i[2]
);
}
if (!is_null($inject))
$s[] = sprintf('<li>%s</li>', $inject);
$s = implode($s);
$h = sprintf('<!--googleoff: all--><nav id="mgnav"><ul id="nav">%s</ul></nav><!--googleon: all-->', $s);
if ($wrap)
$h = sprintf('<header id="hmgn">%s</header>', $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 '<style>' . file_get_contents(__DIR__ . '/css/source.css') . '</style><style>' . file_get_contents(__DIR__ . '/css/center.css') . '</style>';
} else {
return '<style>' . file_get_contents(__DIR__ . '/css/source.css') . '</style>';
}
}
/**
* Get the primary language subtag only.<p></p>
*/
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';
}
|