1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\helper; |
6
|
|
|
|
7
|
|
|
class Bootup |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* filter request inputs |
11
|
|
|
* |
12
|
|
|
* Ensures inputs are well formed UTF-8 |
13
|
|
|
* When not, assumes Windows-1252 and converts to UTF-8 |
14
|
|
|
* Tests only values, not keys |
15
|
|
|
* |
16
|
|
|
* @param int $normalization_form |
17
|
|
|
* @param string $leading_combining |
18
|
|
|
*/ |
19
|
1 |
|
public static function filterRequestInputs($normalization_form = \Normalizer::NFC, $leading_combining = '◌') |
20
|
|
|
{ |
21
|
|
|
$a = [ |
22
|
1 |
|
&$_FILES, |
23
|
1 |
|
&$_ENV, |
24
|
1 |
|
&$_GET, |
25
|
1 |
|
&$_POST, |
26
|
1 |
|
&$_COOKIE, |
27
|
1 |
|
&$_SERVER, |
28
|
1 |
|
&$_REQUEST, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
32
|
|
|
/** @noinspection ForeachSourceInspection */ |
33
|
1 |
|
foreach ($a[0] as &$r) { |
34
|
1 |
|
$a[] = [ |
35
|
1 |
|
&$r['name'], |
36
|
1 |
|
&$r['type'], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
1 |
|
unset($r, $a[0]); |
40
|
|
|
|
41
|
1 |
|
$len = \count($a) + 1; |
42
|
1 |
|
for ($i = 1; $i < $len; ++$i) { |
43
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
44
|
|
|
/** @noinspection ForeachSourceInspection */ |
45
|
1 |
|
foreach ($a[$i] as &$r) { |
46
|
|
|
/** @noinspection ReferenceMismatchInspection */ |
47
|
1 |
|
$s = $r; // $r is a reference, $s a copy |
48
|
1 |
|
if (\is_array($s)) { |
49
|
1 |
|
$a[$len++] = &$r; |
50
|
|
|
} else { |
51
|
1 |
|
$r = self::filterString($s, $normalization_form, $leading_combining); |
52
|
|
|
} |
53
|
|
|
} |
54
|
1 |
|
unset($r, $a[$i]); |
55
|
|
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Filter current REQUEST_URI . |
60
|
|
|
* |
61
|
|
|
* @param string|null $uri <p>If null is set, then the server REQUEST_URI will be used.</p> |
62
|
|
|
* @param bool $exit |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
1 |
|
public static function filterRequestUri($uri = null, $exit = true) |
67
|
|
|
{ |
68
|
1 |
|
if ($uri === null) { |
69
|
1 |
|
if (!isset($_SERVER['REQUEST_URI'])) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$uri = (string) $_SERVER['REQUEST_URI']; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$uriOrig = $uri; |
77
|
|
|
|
78
|
|
|
// |
79
|
|
|
// Ensures the URL is well formed UTF-8 |
80
|
|
|
// |
81
|
|
|
|
82
|
1 |
|
if (UTF8::is_utf8(\rawurldecode($uri)) === true) { |
83
|
1 |
|
return $uri; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// |
87
|
|
|
// When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL |
88
|
|
|
// |
89
|
|
|
|
90
|
1 |
|
$uri = (string) \preg_replace_callback( |
91
|
1 |
|
'/[\x80-\xFF]+/', |
92
|
|
|
/** |
93
|
|
|
* @param array $m |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
1 |
|
static function (array $m): string { |
98
|
1 |
|
return \rawurlencode($m[0]); |
99
|
1 |
|
}, |
100
|
1 |
|
$uri |
101
|
|
|
); |
102
|
|
|
|
103
|
1 |
|
$uri = (string) \preg_replace_callback( |
104
|
1 |
|
'/(?:%[89A-F][0-9A-F])+/i', |
105
|
|
|
/** |
106
|
|
|
* @param array $m |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
1 |
|
static function (array $m): string { |
111
|
1 |
|
return \rawurlencode(UTF8::rawurldecode($m[0])); |
112
|
1 |
|
}, |
113
|
1 |
|
$uri |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
if ( |
117
|
1 |
|
$uri !== $uriOrig |
118
|
|
|
&& |
119
|
1 |
|
$exit === true |
120
|
|
|
&& |
121
|
1 |
|
\headers_sent() === false |
122
|
|
|
) { |
123
|
|
|
// Use ob_start() to buffer content and avoid problem of headers already sent... |
124
|
|
|
$severProtocol = ($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'); |
125
|
|
|
\header($severProtocol . ' 301 Moved Permanently'); |
126
|
|
|
\header('Location: ' . $uri); |
127
|
|
|
exit(); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $uri; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Normalizes to UTF-8 NFC, converting from WINDOWS-1252 when needed. |
135
|
|
|
* |
136
|
|
|
* @param mixed $input |
137
|
|
|
* @param int $normalization_form |
138
|
|
|
* @param string $leading_combining |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
1 |
|
public static function filterString($input, int $normalization_form = \Normalizer::NFC, string $leading_combining = '◌') |
143
|
|
|
{ |
144
|
1 |
|
return UTF8::filter($input, $normalization_form, $leading_combining); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get random bytes via "random_bytes()" |
149
|
|
|
* |
150
|
|
|
* @param int $length <p>output length</p> |
151
|
|
|
* |
152
|
|
|
* @throws \Exception if it was not possible to gather sufficient entropy |
153
|
|
|
* |
154
|
|
|
* @return false|string |
155
|
|
|
* <strong>false</strong> on error |
156
|
|
|
*/ |
157
|
1 |
|
public static function get_random_bytes($length) |
158
|
|
|
{ |
159
|
1 |
|
if (!$length) { |
160
|
1 |
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
$length = (int) $length; |
164
|
|
|
|
165
|
1 |
|
if ($length <= 0) { |
166
|
1 |
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return \random_bytes($length); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* bootstrap |
174
|
|
|
*/ |
175
|
1 |
|
public static function initAll() |
176
|
|
|
{ |
177
|
1 |
|
\ini_set('default_charset', 'UTF-8'); |
178
|
|
|
|
179
|
|
|
// everything is init via composer, so we are done here ... |
180
|
1 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Determines if the current version of PHP is equal to or greater than the supplied value. |
184
|
|
|
* |
185
|
|
|
* @param string $version <p>e.g. "7.1"<p> |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
* Return <strong>true</strong> if the current version is $version or higher |
189
|
|
|
*/ |
190
|
11 |
|
public static function is_php($version): bool |
191
|
|
|
{ |
192
|
11 |
|
static $_IS_PHP; |
193
|
|
|
|
194
|
11 |
|
$version = (string) $version; |
195
|
|
|
|
196
|
11 |
|
if (!isset($_IS_PHP[$version])) { |
197
|
3 |
|
$_IS_PHP[$version] = \version_compare(\PHP_VERSION, $version, '>='); |
198
|
|
|
} |
199
|
|
|
|
200
|
11 |
|
return $_IS_PHP[$version]; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|