forked from srobo/srweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
424 lines (275 loc) · 10.1 KB
/
index.php
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
if (!ob_start('ob_gzhandler')) ob_start();
//get user configuration
require('config.inc.php');
//get smarty code
require(SMARTY_DIR . '/Smarty.class.php');
//get classes for constructing hierachical menu
require('classes/MenuItem.class.php');
require('classes/Menu.class.php');
//get class from extracting metadata and content
require('classes/Content.class.php');
require('classes/CacheWrapper.class.php');
//get instance of smarty
$smarty = new Smarty();
//$smarty->debugging = true;
//configure smarty
$smarty->template_dir = TEMPLATE_DIR;
$smarty->compile_dir = COMPILED_TEMPLATE_DIR;
$smarty->cache_dir = CACHE_DIR;
//get the page to serve (excl. language)
$page = getPage();
//get the prefered languages
$orderedLanguages = getOrderedLanguages();
//default language is en -- English
$language = 'en';
//use lowercase array keys for comparison (no mixed case issues then)
$accepted_languages = array_change_key_case($ACCEPTED_LANGUAGES, CASE_LOWER);
foreach($orderedLanguages as $l){
if (isset($accepted_languages[strtolower($l)]) && file_exists(CONTENT_DIR . '/' . $accepted_languages[strtolower($l)] . '/' . $page)){
$language = $accepted_languages[strtolower($l)];
break;
}
}//foreach
/* Some smarty variables that are accessible to all templates */
$smarty->assign('live_site', LIVE_SITE);
/*
* Some pages need to be treated differently (they use a different template, for
* example); this is handled here.
*/
if ($page == 'home'){
$file = 'home-en';
foreach ($orderedLanguages as $l){
if (isset($accepted_languages[strtolower($l)]) && file_exists('templates/' . 'home-' . $accepted_languages[strtolower($l)] . '.tpl')){
$file = 'home-' . $accepted_languages[strtolower($l)];
break;
}
}
$smarty->assign('side_menu', constructMenuHierachy());
$smarty->assign('root_uri', ROOT_URI);
$smarty->assign('base_uri', BASE_URI);
$smarty->display($file . '.tpl');
ob_end_flush();
exit(0);
} elseif ($page == 'news/index'){
$smarty->assign('p', isset($_GET['p']) ? $_GET['p'] : '1');
$smarty->assign('root_uri', ROOT_URI);
$smarty->assign('base_uri', BASE_URI);
$smarty->display('news.tpl');
ob_end_flush();
exit(0);
} else {
$pageInDocs = preg_match('/docs\/.+/', $page);
//the physical file to serve, including the language in the path
$fileToServe = CONTENT_DIR . '/' . $language . '/' . $page;
//before we go ahead and serve it, see if we can use what the
//user's browser has cached.
if (function_exists('apache_request_headers') && !$pageInDocs){
$headers = apache_request_headers();
//if the file hasn't changed since the client last saw it, send a 304
if (isset($headers['If-Modified-Since'])
&& (strtotime($headers['If-Modified-Since']) == filemtime($fileToServe))){
header('Last-Modified: ' . gmdate('D, d M Y H:i:s',
filemtime($fileToServe)).' GMT', false, $page == "404" ? 404 : 304);
header('Connection: close');
} else {
//otherwise serve it
header('Last-Modified: ' . gmdate('D, d M Y H:i:s',
filemtime($fileToServe)).' GMT');
}//if else isset if-mod-since
}//if function_exists
//do some caching stuff
$content = CacheWrapper::getCacheItem('[page_content:' . $fileToServe . ':' . filemtime($fileToServe) . ']', 86400/*1 day*/, function(){
global $fileToServe;
$c = new Content($fileToServe);
$c->getParsedContent();
return $c;
});
//if the file is a special redirection file, do the redirection
if ($content->getMeta('REDIRECT') != ""){
header("HTTP/1.1 302 Found");
header("Location: " . $content->getMeta('REDIRECT'));
}
//make the content object accessible in the templates (used by a custom smarty plugin)
$smarty->assign('content', $content);
$header_file = 'header-en.tpl';
foreach ($orderedLanguages as $l){
if (isset($accepted_languages[strtolower($l)]) && file_exists('templates/' . 'header-' . $accepted_languages[strtolower($l)] . '.tpl')){
$header_file = 'header-' . $accepted_languages[strtolower($l)] . '.tpl';
break;
}
}
$footer_file = 'footer-en.tpl';
foreach ($orderedLanguages as $l){
if (isset($accepted_languages[strtolower($l)]) && file_exists('templates/' . 'footer-' . $accepted_languages[strtolower($l)] . '.tpl')){
$footer_file = 'footer-' . $accepted_languages[strtolower($l)] . '.tpl';
break;
}
}
//get ready to display the template
$smarty->assign('original', $language . '/' . $page);
$smarty->assign('content_dir', CONTENT_DIR);
$smarty->assign('root_uri', ROOT_URI);
$smarty->assign('base_uri', BASE_URI);
$smarty->assign('page_id', str_replace('/', '_', $page));
$smarty->assign('header_file', $header_file);
$smarty->assign('footer_file', $footer_file);
if ($pageInDocs){
$smarty->assign('docsNav', constructDocsNavHierarchy());
$smarty->display('docs.tpl');
} else
$smarty->display('content.tpl');
ob_end_flush();
}//if-else
/*
* ===========================================================
* FUNCTIONS
* ===========================================================
*/
/*
* Returns an ordered array (most prefered first) of languages
* the client is happy with. If it's not set, then 'en' is the
* default.
*
* For more information on this header, see the RFC:
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
*/
function getOrderedLanguages(){
//check to see if we can get at the headers
if (!function_exists('apache_request_headers'))
return array('en');
$headers = apache_request_headers();
//check to see if Accept-Language header is present
if (!in_array('Accept-Language', array_keys($headers)))
return array('en');
//get each of the language tags
$tags = explode(',', $headers['Accept-Language']);
$pref_array = array();
if (count($tags) > 0){
//get, by group, the following: language, country code, preference
$pattern = '/([A-Za-z]{2}-?[A-Za-z]{0,2})(;q=[01]\.[0-9]+)?/';
foreach ($tags as $tag){
preg_match($pattern, $tag, $matches);
//no 'q' preference === preference of 1
$preference = array_key_exists(2, $matches) ? (float)str_replace(';q=', '', $matches[2]) : (float)1;
$pref_array[$matches[1]] = $preference;
}//foreach
//sort thee array, in reverse numeric order, and return the keys (that's the language tag)
arsort($pref_array, SORT_NUMERIC);
return array_keys($pref_array);
} else {
return array('en');
}//if-else
}
/*
* Returns a list of allowed files/directories, recursively,
* from the given $directory. Mmmmm... recursion.
*/
function getAllowedPages($directory) {
$array_items = array();
if ($handle = opendir($directory)) {
//read each item (file/dir) in $directory
while (false !== ($file = readdir($handle))) {
//ignore . and ..
if (substr($file, 0, 1) != '.') {
if (is_dir($directory. "/" . $file)){
//get the listing for the directory as well
$array_items = array_merge($array_items, getAllowedPages($directory. "/" . $file));
//get the bit of path after the content dir
$pattern = '/^' . str_replace('/', '\/', CONTENT_DIR . '/default') . '\/(.+)$/';
preg_match($pattern, $directory. "/" . $file, $matches);
$array_items[] = $matches[1] . '/';
continue;
}//if is_dir
$file = $directory . "/" . $file;
//ignore the extension fof the file paths, and get just the bit after 'content/'
$pattern = '/^' . str_replace('/', '\/', CONTENT_DIR . '/default') . '\/(.+)$/';
preg_match($pattern, $file, $matches);
$array_items[] = $matches[1];
}//if (not . or ..)
}//while
closedir($handle);
}//if opened
//remove spurious NULLs
for ($i=0; $i<count($array_items); $i++)
if ($array_items[$i] === NULL)
unset($array_items[$i]);
return $array_items;
}//getAllowedPages
/*
* Returns the page to serve.
*/
function getPage(){
$page = 'home';
//the 'default' directory is a symlink to the 'en' -- more explanatory
$allowed_pages = getAllowedPages(CONTENT_DIR . '/default');
if (isset($_GET['page'])){
//can we serve the page?
if (in_array($_GET['page'], $allowed_pages)){
$page = $_GET['page'];
//is the request actually a directory?
} elseif (in_array($_GET['page'] . '/', $allowed_pages)) {
//redirect the browser to the more correct URL with trailing slash
header('HTTP/1.1 302 Found');
header('Location: ' . ROOT_URI . $_GET['page'] . '/');
} else {
//page not allowed / page not found -- respond with a 404
header('HTTP/1.1 404 Not Found');
$page = '404';
log404();
}//if-elseif-else
//append 'index' if the 'page' requested is a directory
if (substr($page, -1, 1) == '/')
$page .= 'index';
}//if isset
return $page;
}//getPage
/*
* Constructs a Menu (object) with a load of MenuItems (objects)
* and to pass to the makemenu plugin (function.makemenu.php)
* in the plugins/ dir. (For producing a ul/li-based menu).
*/
function constructMenuHierachy(){
global $MENU_PAGES;
$allowed_pages = getAllowedPages(CONTENT_DIR . '/default');
$menu = new Menu();
//ignore any menu page that doesn't exist
$menu_pages = array_intersect($MENU_PAGES, $allowed_pages);
//add each to the hierachy
foreach (array_keys($menu_pages) as $index){
$path = $menu_pages[$index];
if (gettype($index) == 'string'){
$menu->addToHierachy($path, ROOT_URI, $index);
} else {
$menu->addToHierachy($path, ROOT_URI);
}
}
return $menu;
}//constructMenuHeirachy
function constructDocsNavHierarchy(){
$docsPages = getAllowedPages(CONTENT_DIR . '/default/docs');
natcasesort($docsPages);
$menu = new Menu();
foreach($docsPages as $item){
if (substr($item, -5, 5) == 'index')
continue;
$text = explode('/', $item);
$text = $text[count($text)-1];
$menu->addToHierachy($item, ROOT_URI, str_replace('_', ' ', $text));
}
return $menu;
}
function log404(){
touch(LOG404_FILE);
if (LOG404_ENABLED
&& filesize(LOG404_FILE) < 2 << 20/*2MB*/
&& isset($_SERVER['HTTP_REFERER'])
&& strpos($_SERVER['HTTP_REFERER'], BASE_URI) !== false
&& ! preg_match('/\/~.*/', $_SERVER['HTTP_REFERER'])){
$f = fopen(LOG404_FILE, 'a');
$line = '{/' . str_replace(BASE_URI, '', $_SERVER['HTTP_REFERER']) . '} was referring to {' . str_replace(ROOT_URI, '/', $_SERVER['REQUEST_URI']) . '} on ' . date('r') . "\n";
fwrite($f, $line);
fclose($f);
}
}//log404
?>