-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
443 lines (371 loc) · 16.7 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
// ---------------------------------------------------------------------------------------
// CONFIG
// ---------------------------------------------------------------------------------------
/** Default language code used if requested language not supported */
define('DEFAULT_LANGUAGE_CODE', 'en');
/**
* Path inside views directory that will be called if a requested URL was not found instead of throwing a 404 error.
* Can be empty or false to disable and throw 404 error instead
*/
define('NOT_FOUND_PAGE', 'error/');
/**
* Path from this framework root to the favicon file that should be returned if browser requests the favicon.
* If empty or false the favicon will be looked up in the views folder 'views/favicon.ico'
*/
define('FAVICON_FILE', 'static/images/favicons/favicon.ico');
/**
* If a requested URI does not exist (cannot be found in the views directory e.g. 'start/123') it will be checked
* if the URI starts with one of the following keys of the array. If a match is found the physical file declared as
* value will be executed instead of the fallback error page (e.g. 'start/123' will be executed by 'views/start/index.php').
* The constant 'REQUEST_PREFIX' will contain the matched key and the constant 'REQUEST_SUFFIX' will contain the
* part of the URI that comes immediately after the prefix (language code prefixes in URI will be ignored and don't need to be added in the array keys,
* additionally URIs must not start with '/' e.g. not '/start/' but instead 'start/').
*/
define('PREFIX_FALLBACKS', array(
'start/' => 'views/start/index.php' // just an example, can be removed
));
/**
* Every request will be redirected to HTTPs (execpt if client is on localhost)
*/
define('HTTPS_REDIRECT', true);
/**
* If true the framework will remove the 'www.' prefix from the domain name by redirecting to the domain without 'www.'.
*/
define('WWW_REDIRECT', true);
// Cookie settings for remembering users language
define('LANGUAGE_COOKIE_NAME', 'L'); // name of cookie to store users last visited language (empty or false to disable)
define('LANGUAGE_COOKIE_EXPIRE_SEC', 5184000); // 60 days
define('LANGUAGE_COOKIE_DOMAIN', $_SERVER['SERVER_NAME']); // domain at which the cookie is readable
// Constants defining paths to private files that can only be accessed with include() or require_once()
define('CSS_COMPONENTS', 'css-components/');
define('JS_COMPONENTS', 'js-components/');
define('SCRIPTS', 'scripts/');
define('STATICS', 'static/');
define('TRANSLATIONS', 'translations/');
define('VIEWS', 'views/');
define('ENVIRONMENT_FILE', '.env');
// Name of CSS and JavaScript directory inside the 'static/' directory. Canno publicly be referenced. No trailing '/'
define('_CSS', 'css');
define('_JS', 'js');
// Caching times (seconds) for specific subdirectories inside the 'static' directory (0 to cache only for session, -1 to disable caching)
define('STATICS_CACHE_SECONDS', array(
'css' => 86400, // 1 day
'images' => 604800, // 1 week
'js' => 86400, // 1 day
));
/** If the URL ends with a directory following files will be search inside the requested directory otherwise 404 will be returned */
define('DEFAULT_INDEX_FILES', array('index.php', 'index.html'));
/** File extensions to mime type needed for HTTP headers. If needed file extension not found then fallback to function mime_content_type() */
define('FILE_EXTENSION_TO_MIME', array(
'css' => 'text/css',
'csv' => 'text/comma-separated-values',
'gif' => 'image/gif',
'htm' => 'text/html',
'html' => 'text/html',
'ico' => 'image/x-icon',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'js' => 'text/javascript',
'json' => 'application/json',
'pdf' => 'application/pdf',
'png' => 'image/png',
'shtml' => 'text/html',
'svg' => 'image/svg+xml',
'txt' => 'text/plain',
'xhtml' => 'application/xhtml+xml',
'xml' => 'text/xml'
));
/** Sequence that must be in front and after constants that are referenced in the JSON translation files */
define('TRANSLATION_CONSTANT_ESCAPE', '%%');
/** Path used in dev mode for sending updates to client */
define('DEV_SSE_URI', '.LUP');
/** Interval in milliseconds for checking file system for changes */
define('DEV_CHECK_FILE_CHANGES', 500);
// ---------------------------------------------------------------------------------------
// INTERAL PROCESSING OF REQUESTS
// ---------------------------------------------------------------------------------------
$IS_HTTPS = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ||
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' ||
!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) !== 'off');
// check if www. prefix should be removed
if(WWW_REDIRECT && str_starts_with($_SERVER['SERVER_NAME'], 'www.') && empty($_POST)){
header("Location: ".(($IS_HTTPS || HTTPS_REDIRECT) ? 'https' : 'http').
"://".substr($_SERVER['SERVER_NAME'], 4).$_SERVER['REQUEST_URI'].(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']));
exit(0);
}
// check if redirect to https is needed
if(HTTPS_REDIRECT && $_SERVER['REMOTE_ADDR'] !== "127.0.0.1" && $_SERVER['REMOTE_ADDR'] !== "::1" && !$IS_HTTPS && empty($_POST)){
header("Location: https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']));
exit(0);
}
/**
* Takes a path to a file and returns the MIME type based on the file extension.
* Uses 'FILE_EXTENSION_TO_MIME' array but falls back to mime_content_type() if extenion not defined.
* @param String $file Path to file the MIME type should be returned for
* @return String MIME type of file
*/
function file_to_mime($file){
$idx = strrpos($file, '.');
if($idx !== false){
$ext = strtolower(substr($file, $idx+1));
if(isset(FILE_EXTENSION_TO_MIME[$ext])) return FILE_EXTENSION_TO_MIME[$ext];
}
$type = mime_content_type($file);
return $type ? $type : 'text/plain';
}
// polyfill function str_starts_with($haystack, $needle): bool
if(!function_exists("str_starts_with")){
function str_starts_with($haystack, $needle){
if(!is_string($haystack) || !is_string($needle)) return false;
return substr($haystack, 0, min(strlen($haystack), strlen($needle))) === $needle;
}
}
// polyfill function str_ends_with($haystack, $needle): bool
if(!function_exists("str_ends_with")){
function str_ends_with($haystack, $needle){
if(!is_string($haystack) || !is_string($needle)) return false;
return substr($haystack, max(0, strlen($haystack)-strlen($needle))) === $needle;
}
}
// detect supported languages
$arr = array();
foreach(scandir(TRANSLATIONS) as $file){
if($file === "." || $file === ".." || $file === "globals.json" || !str_ends_with($file, '.json')) continue;
array_push($arr, substr($file, 0, -5));
}
define('SUPPORTED_LANGUAGES', $arr);
/**
* Converts a given language code into a supported language code or false if no matches found
* @param String $code Language code that should be checked
* @return String Supported language code clostest matching given language code or false if no close match
*/
function toSupportedLanguage($code){
if(!$code || !is_string($code)) return false;
$len = strlen($code);
if(in_array($code, SUPPORTED_LANGUAGES)) return $code;
if($len <= 2) return false;
$code = substr($code, 0, 2);
return in_array($code, SUPPORTED_LANGUAGES) ? $code : false;
}
// Trim to actual request
define('PROJECT_ROOT', substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))));
$requestWithQuery = substr($_SERVER['REQUEST_URI'], strlen(PROJECT_ROOT));
$len = strlen($_SERVER['QUERY_STRING']);
define('FULL_REQUEST', urldecode((!$len && !empty($requestWithQuery) && $requestWithQuery[-1] !== '?') ? $requestWithQuery : substr($requestWithQuery, 0, -($len+1))));
$fullRequestLen = strlen(FULL_REQUEST);
// Detect language
$lang = false;
$useLangCookie = (LANGUAGE_COOKIE_NAME && !empty(LANGUAGE_COOKIE_NAME));
$idx = strpos(FULL_REQUEST, '/');
if($idx > 0) $lang = toSupportedLanguage(substr(FULL_REQUEST, 0, $idx));
if(!$lang && $useLangCookie && isset($_COOKIE[LANGUAGE_COOKIE_NAME])) $lang = toSupportedLanguage($_COOKIE[LANGUAGE_COOKIE_NAME]);
if(!$lang && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$al = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$idx = strpos($al, ',');
$lang = toSupportedLanguage($idx ? substr($al, 0, $idx) : $al);
}
define('LANGUAGE_CODE', $lang ? $lang : DEFAULT_LANGUAGE_CODE);
if($useLangCookie) setcookie(LANGUAGE_COOKIE_NAME, LANGUAGE_CODE, (LANGUAGE_COOKIE_EXPIRE_SEC > 0 ? time()+LANGUAGE_COOKIE_EXPIRE_SEC : 0), '/', LANGUAGE_COOKIE_DOMAIN);
$langLen = strlen(LANGUAGE_CODE);
define('REQUEST', ($fullRequestLen < $langLen || strtolower(substr(FULL_REQUEST, 0, $langLen)) != $lang) ? FULL_REQUEST : substr(FULL_REQUEST, $langLen+1));
/** Load environment variables */
function reloadEnv(){
unset($_ENV);
if(($handle = fopen(ENVIRONMENT_FILE, "r"))){
while(($line = fgets($handle)) !== false){
$idx = strpos($line, "=");
if($idx < 0) continue;
$k = trim(substr($line, 0, $idx));
if(empty($k) || $k[0] === '#') continue;
$v = trim(substr($line, $idx+1));
$_ENV[$k] = $v;
putenv($k.'='.$v);
}
fclose($handle);
}
}
/** Returns true if server runs in development mode, otherwise false */
function isDevMode(){
return isset($_ENV['DEV']) && (empty($_ENV['DEV']) || $_ENV['DEV'] === '1' || strtolower($_ENV['DEV']) === 'true');
}
/**
* Function takes the path of a file from framework root, sets appropiate headers,
* echos the contents of the file and exits the execution of the script.
* If file does not exist the NOT_FOUND_PAGE will be used if defined otherwise responds with 404 error.
* @param String $file Path to the file
*/
function respondWithFile($file, $isAlreadyNotFound=false, $isInsideStatics=false, $prefix=false){
// dev mode SSE
if($file === VIEWS.DEV_SSE_URI){
reloadEnv();
if(isDevMode()){
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: Keep-Alive');
function initFiles($files=array(), $prefix=''){
$result = array();
foreach($files as $file){
if($file === '.' || $file === '..') continue;
$fullFile = $prefix.$file;
if(is_dir($fullFile))
array_push($result, ...initFiles(scandir($fullFile), $fullFile.'/'));
else
array_push($result, $fullFile.':'.filemtime($fullFile));
}
return $result;
}
$FILES = initFiles(scandir('.'));
$FILES_LEN = count($FILES);
/** Returns count of found files or -1 if file has changed */
function checkFiles($FILES=array(), $files=array(), $prefix=''){
$count = 0;
foreach($files as $file){
if($file === '.' || $file === '..') continue;
$fullFile = $prefix.$file;
if(is_dir($fullFile)){
$found = checkFiles($FILES, scandir($fullFile), $fullFile.'/');
if($found === -1) return -1;
$count = $count + $found;
} else if(in_array($fullFile.':'.filemtime($fullFile), $FILES)){
$count = $count + 1;
} else {
echo $fullFile.':'.filemtime($fullFile).'<br />';
return -1;
}
}
return $count;
}
while(!connection_aborted()){
usleep(DEV_CHECK_FILE_CHANGES*1000);
if(checkFiles($FILES, scandir('.')) !== $FILES_LEN){
echo "event: message\n";
echo "data: {\"reload\": true}";
echo "\n\n";
ob_end_flush();
flush();
$FILES = initFiles(scandir('.'));
$FILES_LEN = count($FILES);
}
}
exit(0);
}
}
// safety checks
$realFile = ($file ? realpath($file) : ''); $realRoot = realpath(__DIR__);
if(!$file || !file_exists($file) || strlen($realFile) < strlen($realRoot) || !str_starts_with($realFile, $realRoot)){
if($isInsideStatics) respondWithFile(VIEWS.REQUEST, $isAlreadyNotFound, false, $prefix);
// respond with prefixed fallbacks
foreach(PREFIX_FALLBACKS as $pref => $file)
if(str_starts_with(REQUEST, $pref) && file_exists($file)) respondWithFile($file, false, false, $pref);
// respond with not found page
header("HTTP/1.0 404 Not Found", true, 404); // signal search engines that page is an error page
if($isAlreadyNotFound || !NOT_FOUND_PAGE || empty(NOT_FOUND_PAGE)) exit();
respondWithFile(VIEWS.NOT_FOUND_PAGE, true, false, $prefix);
}
// if path is directory search inside directory
if(is_dir($file)){
if(!empty(FULL_REQUEST) && FULL_REQUEST[-1] !== '/'){
header('Location: '.str_repeat('../', substr_count(FULL_REQUEST, "/")).FULL_REQUEST.'/', true);
exit(0);
}
$notFound = true;
foreach(DEFAULT_INDEX_FILES as $indexName){
$path = $file.$indexName;
if(file_exists($path)){
$file = $path;
$notFound = false;
break;
}
}
if($notFound){
if($isInsideStatics) respondWithFile(VIEWS.REQUEST, $isAlreadyNotFound, false, $prefix);
respondWithFile(null, $isAlreadyNotFound, false, $prefix);
}
}
// execute PHP files
if(str_ends_with($file, '.php')){
// load language files, define constants and include config
define('REQUEST_PREFIX', $prefix === false ? REQUEST : $prefix);
define('REQUEST_SUFFIX', $prefix === false ? false : substr(REQUEST, strlen($prefix)));
define('REQUEST_SUFFIX_PARTS', !empty(REQUEST_SUFFIX) ? explode('/', REQUEST_SUFFIX[0] === '/' ? substr(REQUEST_SUFFIX, 1) : REQUEST_SUFFIX) : array());
define('REQUEST_PREFIX_BASE', str_repeat('../', max(0, count(REQUEST_SUFFIX_PARTS)-1) ));
define('ROOT_DEPTH', substr_count(FULL_REQUEST, "/"));
define('BASE_DEPTH', substr_count(REQUEST, "/"));
define('BASE', str_repeat('../', BASE_DEPTH));
define('ROOT', BASE.(ROOT_DEPTH != BASE_DEPTH ? '../' : ''));
foreach(scandir(STATICS) as $dir) if(is_dir(STATICS.$dir)) define(strtoupper($dir), ROOT.$dir.'/');
reloadEnv();
include('config.php');
$IS_DEV = isDevMode();
function replaceVariables($str){
$val = ""; $start = -1; $end = 0;
do {
$start = strpos($str, TRANSLATION_CONSTANT_ESCAPE, $end);
if($start === false){ $val .= substr($str, $end); break; }
$val .= substr($str, $end, $start-$end);
$start += strlen(TRANSLATION_CONSTANT_ESCAPE);
$end = strpos($str, TRANSLATION_CONSTANT_ESCAPE, $start);
if($end === false){ $val .= TRANSLATION_CONSTANT_ESCAPE.substr($str, $start); break; }
$len = $end - $start;
if($len !== 0) $val .= constant(substr($str, $start, $len));
$end += strlen(TRANSLATION_CONSTANT_ESCAPE);
} while(true);
return $val;
}
$arr = array();
$globals = json_decode(file_get_contents(TRANSLATIONS.'globals.json'), true);
foreach($globals as $key=>$value){
$arr[$key] = replaceVariables($value);
}
$trans = json_decode(file_get_contents(TRANSLATIONS.LANGUAGE_CODE.'.json'), true);
foreach($trans as $key=>$value){
$arr[$key] = replaceVariables($value);
}
define('TEXT', $arr);
include($file);
if($IS_DEV)
foreach(headers_list() as $header)
if(strpos($header, 'text/html') !== false){
echo '<script type="text/javascript">(function(){const es=new EventSource("'.ROOT.DEV_SSE_URI.'");es.addEventListener("message",function(event){console.log(event.data);if(JSON.parse(event.data).reload)window.location.reload(true);});})();</script>';
break;
}
exit(0);
}
// return contents of file
$mimeType = file_to_mime($file);
header('Content-Type: '.$mimeType);
if($mimeType === 'video/quicktime' || $mimeType === 'video/mp4' || $mimeType === 'video/x-m4v' || $mimeType === 'video/3gpp'){
// video files
header('Content-Length: '.filesize($file));
header('Accept-Ranges: bytes');
}
echo file_get_contents($file);
exit(0);
}
// process requests starting with a subdirectory inside of statics/
foreach(scandir(STATICS) as $dir){
if($dir === '.' || $dir === '..') continue;
if(str_starts_with(FULL_REQUEST, $dir)){
if(isset(STATICS_CACHE_SECONDS[$dir])){
require_once(SCRIPTS.'caching.php');
$cacheSec = STATICS_CACHE_SECONDS[$dir];
reloadEnv();
if($cacheSec >= 0 && !isDevMode()) setCache($cacheSec); else noCache();
}
if($dir === _CSS) include(CSS_COMPONENTS.'css-config.php');
if($dir === _JS) include(JS_COMPONENTS.'js-config.php');
respondWithFile(STATICS.FULL_REQUEST, false, true);
}
}
// process favicon.ico
if(FULL_REQUEST === 'favicon.ico') respondWithFile((FAVICON_FILE && !empty(FAVICON_FILE)) ? FAVICON_FILE : _VIEWS.'favicon.ico');
// process sitemap.xml
if(FULL_REQUEST === 'sitemap.xml'){
respondWithFile('sitemap.php'); // in root
return;
}
// all other files including PHP files
respondWithFile(VIEWS.REQUEST);
?>