1 |
rda |
1096 |
<?php |
2 |
rda |
2051 |
/** |
3 |
|
|
* mageia.org global nav bar utilities. |
4 |
|
|
* |
5 |
|
|
* PHP version 5.4 |
6 |
|
|
* |
7 |
|
|
* @category Mageia |
8 |
|
|
* @package Mageia\Web\nav |
9 |
|
|
* @author rda <rda@mageia.org> |
10 |
|
|
* @link http://nav.mageia.org/ |
11 |
|
|
* |
12 |
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+ |
13 |
|
|
* |
14 |
|
|
* This program is free software; you can redistribute it and/or modify it |
15 |
|
|
* under the terms of the GNU General Public License aspublished by the |
16 |
|
|
* Free Software Foundation; either version 2 of the License, or (at your |
17 |
|
|
* option) any later version. |
18 |
|
|
*/ |
19 |
rda |
1096 |
// definition |
20 |
|
|
|
21 |
rda |
1305 |
class NCache |
22 |
|
|
{ |
23 |
|
|
function __construct() { } |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* Factory. |
27 |
|
|
* |
28 |
|
|
* @param string $path where cache file store is located, relative to app path. |
29 |
|
|
* @param integer $timeout in seconds |
30 |
|
|
* |
31 |
|
|
* @return NCache |
32 |
|
|
*/ |
33 |
|
|
function build($path, $timeout = 3600) |
34 |
|
|
{ |
35 |
|
|
$path = __DIR__ . '/' . $path; |
36 |
|
|
|
37 |
|
|
if (!is_dir($path)) |
38 |
|
|
return null; |
39 |
|
|
|
40 |
|
|
if ($timeout < 60) |
41 |
|
|
$timeout = 60; |
42 |
|
|
|
43 |
|
|
$i = new self; |
44 |
|
|
$i->_path = $path; |
45 |
|
|
$i->_timeout = $timeout; |
46 |
|
|
|
47 |
|
|
return $i; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Get value for $key. |
52 |
|
|
* |
53 |
|
|
* @param mixed $key |
54 |
|
|
* |
55 |
|
|
* @return mixed |
56 |
|
|
*/ |
57 |
|
|
function get($key = null) |
58 |
|
|
{ |
59 |
|
|
if (is_null($key)) |
60 |
|
|
return false; |
61 |
|
|
|
62 |
|
|
$filename = $this->_get_filename($key); |
63 |
|
|
|
64 |
|
|
if ($this->_is_valid_file($filename, $this->_timeout)) { |
65 |
|
|
return unserialize(file_get_contents($filename)); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
return null; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Save $value under $key. |
73 |
|
|
* |
74 |
|
|
* @param mixed $key |
75 |
|
|
* @param mixed $value |
76 |
|
|
*/ |
77 |
|
|
function set($key, $value) |
78 |
|
|
{ |
79 |
|
|
if (is_null($key)) |
80 |
|
|
return false; |
81 |
|
|
|
82 |
|
|
$filename = $this->_get_filename($key); |
83 |
|
|
file_put_contents($filename, serialize($value)); |
84 |
|
|
|
85 |
|
|
return true; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Get cache file from key. |
90 |
|
|
* |
91 |
|
|
* @param mixed $key |
92 |
|
|
* |
93 |
|
|
* @return string |
94 |
|
|
*/ |
95 |
|
|
private function _get_filename($key) |
96 |
|
|
{ |
97 |
|
|
$key = hash('sha1', serialize($key)); |
98 |
|
|
|
99 |
|
|
return $this->_path . '/' . $key . '.cache'; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* Check that the cache file exists and has not expired. |
104 |
|
|
* |
105 |
|
|
* @param string $filename |
106 |
|
|
* |
107 |
|
|
* @return boolean |
108 |
|
|
*/ |
109 |
|
|
private function _is_valid_file($filename, $timeout) |
110 |
|
|
{ |
111 |
|
|
if (!file_exists($filename)) { |
112 |
|
|
//error_log(sprintf('Could not find %s', $filename), 0); |
113 |
|
|
return false; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
if (filemtime($filename) + $timeout < time()) { |
117 |
|
|
//error_log(sprintf('%s timestamp expired (timeout was %ds.).', $filename, $timeout)); |
118 |
|
|
unlink($filename); |
119 |
|
|
return false; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
//error_log(sprintf('Found %s', $filename)); |
123 |
|
|
return true; |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
rda |
1096 |
class l10n |
128 |
|
|
{ |
129 |
|
|
public static $t; |
130 |
|
|
|
131 |
|
|
/** |
132 |
|
|
* Load langs/$lang.lang into global $_t array. |
133 |
|
|
* |
134 |
|
|
* @param string $lang |
135 |
|
|
* |
136 |
|
|
* @return void |
137 |
|
|
*/ |
138 |
rda |
1306 |
function load($lang) |
139 |
|
|
{ |
140 |
|
|
global $_t; |
141 |
|
|
$_t = array(); |
142 |
|
|
|
143 |
|
|
if ($lang == 'en') |
144 |
|
|
return; |
145 |
|
|
|
146 |
|
|
$lang_file = __DIR__ . '/langs/' . $lang . '.lang'; |
147 |
|
|
$cache_file = __DIR__ . '/var/tmp/cache/nav_lang_' . $lang . '.php'; |
148 |
|
|
$lang_ts = filemtime($lang_file); |
149 |
|
|
|
150 |
|
|
if (file_exists($cache_file)) { |
151 |
|
|
include $cache_file; |
152 |
|
|
if ($_ts > $lang_ts) |
153 |
|
|
return; |
154 |
|
|
} |
155 |
|
|
|
156 |
rda |
1096 |
if (file_exists($lang_file)) { |
157 |
rda |
1306 |
|
158 |
rda |
1096 |
$f = file($lang_file); |
159 |
rda |
1306 |
|
160 |
rda |
1096 |
foreach ($f as $k => $v) { |
161 |
rda |
1306 |
|
162 |
|
|
if (substr($v, 0, 1) == ';' |
163 |
|
|
&& !empty($f[$k+1])) |
164 |
|
|
{ |
165 |
rda |
1096 |
$_t[trim(substr($v, 1))] = trim($f[$k+1]); |
166 |
|
|
} |
167 |
|
|
} |
168 |
rda |
1306 |
|
169 |
|
|
// |
170 |
|
|
$_t_data = var_export($_t, true); |
171 |
|
|
$cache = <<<P |
172 |
|
|
<?php |
173 |
|
|
/**! Generated. Do not edit. */ |
174 |
|
|
|
175 |
|
|
// filemtime($lang_file) |
176 |
|
|
\$_ts = $lang_ts; |
177 |
|
|
|
178 |
|
|
// $lang strings |
179 |
|
|
global \$_t; |
180 |
|
|
\$_t = $_t_data; |
181 |
|
|
P; |
182 |
|
|
file_put_contents($cache_file, $cache); |
183 |
rda |
1096 |
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
/** |
187 |
|
|
* Get value for key $s in global array $_t. |
188 |
|
|
* |
189 |
|
|
* @param string $s |
190 |
|
|
* |
191 |
|
|
* @return string |
192 |
|
|
*/ |
193 |
|
|
function _t($s) { |
194 |
|
|
if (trim($s) == '') |
195 |
|
|
return ''; |
196 |
|
|
|
197 |
|
|
global $_t; |
198 |
|
|
|
199 |
rda |
2046 |
$s = array_key_exists($s, $_t) ? $_t[$s] : $s; |
200 |
|
|
$s = trim(str_replace(array('{ok}', '{OK}', '{Ok}', '{oK}'), '', $s)); |
201 |
|
|
|
202 |
|
|
return $s; |
203 |
rda |
1096 |
} |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/** |
207 |
|
|
* Produce navigation HTML code. |
208 |
|
|
* |
209 |
|
|
* @param boolean $wrap = false should it be wrapped in a <header id="nav" /> element? |
210 |
|
|
* @param string $lang = 'en' |
211 |
|
|
* @param string $inject = null |
212 |
rda |
1307 |
* @param string $vhost = 'www.mageia.org' |
213 |
|
|
* @param object $cache |
214 |
rda |
1096 |
* |
215 |
|
|
* @return string HTML code |
216 |
|
|
*/ |
217 |
rda |
1307 |
function _mgnav_html($wrap = false, $lang = 'en', $inject = null, $vhost = 'www.mageia.org', $cache = null) |
218 |
rda |
1096 |
{ |
219 |
rda |
1307 |
$key = array($wrap, $lang, $inject, $vhost); |
220 |
|
|
|
221 |
|
|
if (!is_null($cache) && ($h = $cache->get($key))) { |
222 |
rda |
1309 |
apache_note('navCacheHit', 1); |
223 |
rda |
1307 |
return $h; |
224 |
|
|
} |
225 |
|
|
|
226 |
rda |
1309 |
apache_note('navCacheHit', 0); |
227 |
|
|
|
228 |
rda |
1254 |
$lang = _lang_check($lang); |
229 |
rda |
1154 |
|
230 |
rda |
1307 |
l10n::load($lang, $cache); |
231 |
rda |
1096 |
|
232 |
|
|
$tn = array( |
233 |
rda |
1307 |
array('mageia', '//$S/$L/map/', 'Mageia', l10n::_t('Go to mageia.org site map.')), |
234 |
|
|
array('about', '//$S/$L/about/', l10n::_t('About us'), l10n::_t('Learn more about Mageia.')), |
235 |
|
|
array('downloads', '//$S/$L/downloads/', l10n::_t('Downloads'), l10n::_t('Download Mageia ISO and updates.')), |
236 |
|
|
array('support', '//$S/$L/support/', l10n::_t('Support'), l10n::_t('Get support from Mageia community.')), |
237 |
|
|
array('community', '//$S/$L/community/', l10n::_t('Community'), l10n::_t('')), |
238 |
|
|
array('contribute', '//$S/$L/contribute/', l10n::_t('Contribute'), l10n::_t('You too can build Mageia with us!')), |
239 |
akien |
2586 |
array('donate', '//$S/$L/donate/', l10n::_t('Donate'), l10n::_t('')), |
240 |
rda |
2028 |
array('you', '//identity.mageia.org/', l10n::_t('You'), l10n::_t('Your Mageia online account.')), |
241 |
|
|
array('contact', '//$S/$L/contact/', l10n::_t('Contact'), l10n::_t('Contact Us')) |
242 |
rda |
1096 |
// <search> |
243 |
|
|
); |
244 |
|
|
|
245 |
|
|
$s = array(); |
246 |
|
|
foreach ($tn as $i) { |
247 |
|
|
$s[] = sprintf('<li><a href="%s" class="%s" title="%s">%s</a></li>', |
248 |
|
|
str_replace( |
249 |
|
|
array('$L', '$S'), |
250 |
|
|
array($lang, $vhost), |
251 |
|
|
$i[1] |
252 |
|
|
), |
253 |
|
|
$i[0], |
254 |
|
|
$i[3], |
255 |
|
|
$i[2] |
256 |
|
|
); |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
if (!is_null($inject)) |
260 |
|
|
$s[] = sprintf('<li>%s</li>', $inject); |
261 |
|
|
|
262 |
|
|
$s = implode($s); |
263 |
rda |
2028 |
$h = sprintf('<!--googleoff: all--><nav id="mgnav"><ul id="nav">%s</ul></nav><!--googleon: all-->', $s); |
264 |
rda |
1096 |
|
265 |
|
|
if ($wrap) |
266 |
|
|
$h = sprintf('<header id="hmgn">%s</header>', $h); |
267 |
|
|
|
268 |
rda |
1307 |
if (!is_null($cache)) |
269 |
|
|
$cache->set($key, $h); |
270 |
|
|
|
271 |
rda |
1096 |
return $h; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
/** |
275 |
|
|
* Returns CSS definition ready to be inserted into a HTML document. |
276 |
|
|
* |
277 |
|
|
* @return string |
278 |
|
|
*/ |
279 |
|
|
function _mgnav_style() |
280 |
|
|
{ |
281 |
|
|
return '<style>' . file_get_contents(__DIR__ . '/css/source.css') . '</style>'; |
282 |
|
|
} |
283 |
rda |
1154 |
|
284 |
|
|
/** |
285 |
|
|
* Get the primary language subtag only.<p></p> |
286 |
|
|
*/ |
287 |
rda |
1254 |
function _lang_check($s = null) |
288 |
rda |
1154 |
{ |
289 |
rda |
2028 |
if (is_null($s)) { |
290 |
rda |
1257 |
return 'en'; |
291 |
rda |
2028 |
} |
292 |
rda |
1254 |
|
293 |
|
|
$supported = array( |
294 |
|
|
'cs', |
295 |
|
|
'de', |
296 |
|
|
'el', 'en', 'eo', 'es', 'et', |
297 |
|
|
'fi', 'fr', |
298 |
filip |
1930 |
'id', 'it', |
299 |
rda |
1254 |
'lv', |
300 |
|
|
'nb', 'nl', |
301 |
|
|
'pl', 'pt', 'pt-br', |
302 |
|
|
'ro', 'ru', |
303 |
|
|
'sl', |
304 |
|
|
'tr', |
305 |
|
|
'uk', |
306 |
|
|
'zh-cn', 'zh-tw' |
307 |
|
|
); |
308 |
|
|
|
309 |
|
|
if (in_array($s, $supported)) |
310 |
|
|
return $s; |
311 |
|
|
|
312 |
rda |
1257 |
$sub = explode('-', $s); |
313 |
|
|
$sub = strtolower($sub[0]); |
314 |
|
|
|
315 |
rda |
1254 |
if (in_array($sub, $supported)) |
316 |
rda |
1257 |
return $sub; |
317 |
rda |
1254 |
|
318 |
|
|
return 'en'; |
319 |
rda |
1154 |
} |