|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MVC; |
|
4
|
|
|
|
|
5
|
|
|
$GLOBALS['router_state'] = []; |
|
6
|
|
|
|
|
7
|
|
|
class router extends themes |
|
8
|
|
|
{ |
|
9
|
|
|
public $root; |
|
10
|
|
|
public $PHP_ERROR_FILE = PHP_ERROR_FILE; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
parent::__construct(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Safe redirect, support any conditions. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function safe_redirect(string $path) |
|
21
|
|
|
{ |
|
22
|
|
|
if (ob_get_level()) { |
|
23
|
|
|
ob_end_clean(); |
|
24
|
|
|
} |
|
25
|
|
|
ob_start(); |
|
26
|
|
|
header('HTTP/1.1 503 Service Unavailable'); |
|
27
|
|
|
header('Status: 503 Service Unavailable'); |
|
28
|
|
|
header('Retry-After: 3600'); |
|
29
|
|
|
header("refresh:5; url=$path"); |
|
30
|
|
|
|
|
31
|
|
|
if (!headers_sent()) { |
|
32
|
|
|
header("Location: $path", true, 302); |
|
33
|
|
|
} else { |
|
34
|
|
|
echo '<!DOCTYPE html> |
|
35
|
|
|
<html> |
|
36
|
|
|
|
|
37
|
|
|
<head> |
|
38
|
|
|
<title>Temporarily Unavailable</title> |
|
39
|
|
|
<meta name="robots" content="none" /> |
|
40
|
|
|
<meta http-equiv="refresh" content="5; url=' . $path . '" /> |
|
41
|
|
|
</head> |
|
42
|
|
|
|
|
43
|
|
|
<body> |
|
44
|
|
|
You will redirected automatically |
|
45
|
|
|
</body> |
|
46
|
|
|
<script> |
|
47
|
|
|
location.href = `' . $path . '`; |
|
48
|
|
|
document.body.innerHTML = ""; |
|
49
|
|
|
throw ""; |
|
50
|
|
|
</script> |
|
51
|
|
|
|
|
52
|
|
|
</html>'; |
|
53
|
|
|
} |
|
54
|
|
|
exit; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function redirect(string $path) |
|
58
|
|
|
{ |
|
59
|
|
|
return self::safe_redirect($path); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function no_direct() |
|
63
|
|
|
{ |
|
64
|
|
|
if (1 == count(get_included_files())) { |
|
65
|
|
|
header('Location: /'); // Send to index |
|
66
|
|
|
die('403'); // Must include to stop PHP from continuing |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public $env; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Define environtment |
|
74
|
|
|
* * development / production. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function environtment($env = 'production') |
|
79
|
|
|
{ |
|
80
|
|
|
$env = strtolower($env); //$GLOBALS['router']['env'] |
|
81
|
|
|
switch ($env) { |
|
82
|
|
|
case 'development': |
|
83
|
|
|
//error_reporting(-1); |
|
84
|
|
|
ini_set('display_errors', 1); |
|
85
|
|
|
ini_set('display_startup_errors', 1); |
|
86
|
|
|
error_reporting(E_ALL); |
|
87
|
|
|
ini_set('log_errors', 1); |
|
88
|
|
|
ini_set('error_log', \Filemanager\file::file($this->PHP_ERROR_FILE, '')); |
|
|
|
|
|
|
89
|
|
|
break; |
|
90
|
|
|
|
|
91
|
|
|
case 'production': |
|
92
|
|
|
ini_set('display_errors', 0); |
|
93
|
|
|
if (version_compare(PHP_VERSION, '5.3', '>=')) { |
|
94
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
|
95
|
|
|
} else { |
|
96
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); |
|
97
|
|
|
} |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
if (empty($env)) { |
|
101
|
|
|
if (ob_get_level()) { |
|
102
|
|
|
ob_end_clean(); |
|
103
|
|
|
} |
|
104
|
|
|
header('HTTP/1.1 503 Service Unavailable.', true, 503); |
|
105
|
|
|
echo 'The application environment is not set correctly.'; |
|
106
|
|
|
exit(1); // EXIT_ERROR |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
file_put_contents(ROOT . '/tmp/environtment.txt', $env, true); |
|
|
|
|
|
|
110
|
|
|
$GLOBALS['router_state']['env'] = $this->env = $env; |
|
111
|
|
|
|
|
112
|
|
|
return $env; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function is_production() |
|
116
|
|
|
{ |
|
117
|
|
|
return 'production' == $this->get_env(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private static $env_status = null; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get framework environtment. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function get_env() |
|
128
|
|
|
{ |
|
129
|
|
|
if (isset($GLOBALS['router_state']['env'])) { |
|
130
|
|
|
return $GLOBALS['router_state']['env']; |
|
131
|
|
|
} |
|
132
|
|
|
if (!self::$env_status) { |
|
133
|
|
|
self::$env_status = \Filemanager\file::get(\Filemanager\file::tmp() . '/environtment.txt'); |
|
134
|
|
|
} |
|
135
|
|
|
if (!self::$env_status) { |
|
136
|
|
|
\Cookie\helper::del('environtment'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return self::$env_status; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function is_req($any, $alternative = null) |
|
143
|
|
|
{ |
|
144
|
|
|
if (isset($_REQUEST[(string) $any])) { |
|
145
|
|
|
return $_REQUEST[(string) $any]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $alternative; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* check if one of the headers exists. |
|
153
|
|
|
* |
|
154
|
|
|
* ```php |
|
155
|
|
|
* if ($this->is_reqs(['DNT', 'Connection'])) |
|
156
|
|
|
* ``` |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $anys |
|
159
|
|
|
* |
|
160
|
|
|
* @return bool|string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function is_reqs(array $anys) |
|
163
|
|
|
{ |
|
164
|
|
|
foreach ($anys as $any) { |
|
165
|
|
|
if (isset($_REQUEST[(string) $any])) { |
|
166
|
|
|
return $_REQUEST[(string) $any]; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function is_post($any, $alternative = null) |
|
172
|
|
|
{ |
|
173
|
|
|
if (isset($_POST[(string) $any])) { |
|
174
|
|
|
return $_POST[(string) $any]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $alternative; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Check if header request has $any. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $any |
|
184
|
|
|
* |
|
185
|
|
|
* @return string|null |
|
186
|
|
|
*/ |
|
187
|
|
|
public function is_header(string $any) |
|
188
|
|
|
{ |
|
189
|
|
|
$allHeaders = getallheaders(); |
|
190
|
|
|
|
|
191
|
|
|
return array_key_exists($any, $allHeaders) ? $allHeaders[$any] : null; |
|
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Check browser no-cache request (hard reload) |
|
196
|
|
|
* |
|
197
|
|
|
* @return boolean |
|
198
|
|
|
*/ |
|
199
|
|
|
public function is_hard_reload() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->is_header('Cache-Control') == 'no-cache' && $this->is_header('Pragma') == 'no-cache'; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Find router from parameter URL. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function findRoute() |
|
208
|
|
|
{ |
|
209
|
|
|
$route = strtok($_SERVER['REQUEST_URI'], '?'); |
|
210
|
|
|
if (empty($route) || preg_match('/^\//s', $route)) { |
|
211
|
|
|
$re = preg_replace('/^\//s', '', $route); |
|
212
|
|
|
$router = $re; |
|
213
|
|
|
if ($re) { |
|
214
|
|
|
if (!$re || empty($re)) { |
|
215
|
|
|
$router = 'index'; |
|
216
|
|
|
} elseif (preg_match('/\/$/', $re)) { |
|
217
|
|
|
$router .= 'index'; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
if (empty($re)) { |
|
221
|
|
|
$router .= 'index'; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
if (empty($router)) { |
|
226
|
|
|
exit(var_dump($router, $re)); |
|
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if (is_dir($router)) { |
|
230
|
|
|
echo 'Unable access'; |
|
231
|
|
|
|
|
232
|
|
|
return; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $router; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function config($router) |
|
239
|
|
|
{ |
|
240
|
|
|
return helper::platformSlashes($this->view . '/' . $router); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function GUID() |
|
244
|
|
|
{ |
|
245
|
|
|
if (true === function_exists('com_create_guid')) { |
|
246
|
|
|
return trim(com_create_guid(), '{}'); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.