forked from JavierGonzalez/POL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
$maxsim.php
123 lines (82 loc) · 3.26 KB
/
$maxsim.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
<?php # maxsim.tech — MIT License — Copyright (c) 2005 Javier González González <[email protected]>
maxsim:
$maxsim['version'] = '0.5.11';
ob_start();
maxsim_router();
maxsim_get();
$maxsim['debug']['timing']['router'] = microtime(true);
foreach ((array) $maxsim['autoload'] AS $file) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext === 'php')
include_once($file);
else if ($ext === 'ini')
if ($key = ltrim(basename($file, '.'.$ext), '+'))
define($key, (array)parse_ini_file($file, true, INI_SCANNER_TYPED));
else if ($ext === 'json')
if ($key = ltrim(basename($file, '.'.$ext), '+'))
${$key} = (array)json_decode(file_get_contents($file), true);
}
$maxsim['debug']['timing']['autoload'] = microtime(true);
include_once($maxsim['app']); #
$maxsim['debug']['timing']['app'] = microtime(true);
if (isset($maxsim['redirect'])) {
$_SERVER['REQUEST_URI'] = $maxsim['redirect'];
unset($maxsim['redirect']);
goto maxsim;
}
if (isset($maxsim['output']) AND $maxsim['output'] === 'text')
header('content-type: text/plain');
else if (isset($maxsim['output']) AND $maxsim['output'] === 'json' AND is_array($echo)) {
ob_end_clean();
header('content-type: application/json');
echo json_encode((array)$echo, JSON_PRETTY_PRINT);
} else if (isset($maxsim['output']) AND file_exists($maxsim['output'].'/index.php')) {
$echo = ob_get_contents();
ob_end_clean();
if ($echo === '') {
http_response_code(404);
$echo = (is_string($maxsim['template'][404])?$maxsim['template'][404]:'Error 404: NOT FOUND.');
}
include($maxsim['output'].'/index.php');
}
exit;
function maxsim_router() {
global $maxsim;
$levels = explode('/', explode('?', $_SERVER['REQUEST_URI'])[0]);
$maxsim['autoload'] = [];
foreach ($levels AS $id => $level) {
$path[] = $level;
if (!$ls = glob(($id?implode('/', array_filter($path)).'/':'').'*'))
break;
maxsim_autoload($ls);
foreach ($ls AS $file)
if (basename($file) === 'index.php')
$maxsim['app'] = $file;
foreach ($ls AS $file)
if (isset($levels[$id+1]) AND basename($file) === $levels[$id+1].'.php')
$maxsim['app'] = $file;
}
}
function maxsim_autoload(array $ls, bool $autoload_files=false) {
global $maxsim;
foreach ($ls AS $file)
if (preg_match('/\.(php|js|css|ini|json)$/', basename($file)))
if (!isset($maxsim['autoload']) OR !in_array($file, (array)$maxsim['autoload']))
if ($autoload_files OR substr(basename($file),0,1) === '+')
$maxsim['autoload'][] = $file;
foreach ($ls AS $dir)
if (!fnmatch('*.*', basename($dir)))
if (substr(basename($dir),0,1) === '+')
maxsim_autoload(glob($dir.'/*'), true);
}
function maxsim_get() {
global $_GET, $maxsim;
$app_level = count(explode('/', $maxsim['app']))-1;
$url = explode('?', $_SERVER['REQUEST_URI'])[0];
if (substr($maxsim['app'],-9) === 'index.php')
$url = '/index'.$url;
$id = 0;
foreach (array_filter(explode('/', $url)) AS $level => $value)
if ($level-$app_level > 0)
$_GET[$id++] = $value;
}