1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Http\Message\Uri; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
19
|
|
|
use O2System\Spl\Exceptions\RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Segments |
23
|
|
|
* |
24
|
|
|
* @package O2System\Kernel\Http\Message\Uri |
25
|
|
|
*/ |
26
|
|
|
class Segments |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Segments::$string |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $string; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Segments::$parts |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $parts; |
41
|
|
|
|
42
|
|
|
// ------------------------------------------------------------------------ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Segments::__construct |
46
|
|
|
* |
47
|
|
|
* @param string|null $string |
48
|
|
|
*/ |
49
|
|
|
public function __construct($string = null) |
50
|
|
|
{ |
51
|
|
|
if (is_null($string)) { |
52
|
|
|
if (kernel()->services->has('config')) { |
53
|
|
|
if (config()->offsetExists('uri')) { |
|
|
|
|
54
|
|
|
$protocol = strtoupper(config('uri')->offsetGet('protocol')); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
empty($protocol) && $protocol = 'REQUEST_URI'; |
59
|
|
|
|
60
|
|
|
switch ($protocol) { |
61
|
|
|
case 'AUTO': |
62
|
|
|
case 'REQUEST_URI': |
63
|
|
|
$string = $this->parseRequestUri(); |
64
|
|
|
break; |
65
|
|
|
case 'QUERY_STRING': |
66
|
|
|
$string = $this->parseQueryString(); |
67
|
|
|
break; |
68
|
|
|
case 'PATH_INFO': |
69
|
|
|
default: |
70
|
|
|
$string = isset($_SERVER[ $protocol ]) |
71
|
|
|
? $_SERVER[ $protocol ] |
72
|
|
|
: $this->parseRequestUri(); |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} elseif (is_array($string)) { |
|
|
|
|
77
|
|
|
$string = implode('/', $string); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$string = str_replace(['\\', '_'], ['/', '-'], $string); |
81
|
|
|
$string = trim(remove_invisible_characters($string, false), '/'); |
82
|
|
|
$this->setParts(explode('/', $string)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// ------------------------------------------------------------------------ |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Segments::parseRequestUri |
89
|
|
|
* |
90
|
|
|
* Parse REQUEST_URI |
91
|
|
|
* |
92
|
|
|
* Will parse REQUEST_URI and automatically detect the URI from it, |
93
|
|
|
* while fixing the query string if necessary. |
94
|
|
|
* |
95
|
|
|
* @access protected |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function parseRequestUri() |
99
|
|
|
{ |
100
|
|
|
if ( ! isset($_SERVER[ 'REQUEST_URI' ], $_SERVER[ 'SCRIPT_NAME' ])) { |
101
|
|
|
return ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$uri = parse_url($_SERVER[ 'REQUEST_URI' ]); |
105
|
|
|
$query = isset($uri[ 'query' ]) |
106
|
|
|
? $uri[ 'query' ] |
107
|
|
|
: ''; |
108
|
|
|
$uri = isset($uri[ 'path' ]) |
109
|
|
|
? $uri[ 'path' ] |
110
|
|
|
: ''; |
111
|
|
|
|
112
|
|
|
if (isset($_SERVER[ 'SCRIPT_NAME' ][ 0 ])) { |
113
|
|
|
if (strpos($uri, $_SERVER[ 'SCRIPT_NAME' ]) === 0) { |
114
|
|
|
$uri = (string)substr($uri, strlen($_SERVER[ 'SCRIPT_NAME' ])); |
115
|
|
|
} elseif (strpos($uri, dirname($_SERVER[ 'SCRIPT_NAME' ])) === 0) { |
116
|
|
|
$uri = (string)substr($uri, strlen(dirname($_SERVER[ 'SCRIPT_NAME' ]))); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct |
121
|
|
|
// URI is found, and also fixes the QUERY_STRING server var and $_GET array. |
122
|
|
|
if (trim($uri, '/') === '' AND strncmp($query, '/', 1) === 0) { |
123
|
|
|
$query = explode('?', $query, 2); |
124
|
|
|
$uri = $query[ 0 ]; |
125
|
|
|
|
126
|
|
|
$_SERVER[ 'QUERY_STRING' ] = isset($query[ 1 ]) |
127
|
|
|
? $query[ 1 ] |
128
|
|
|
: ''; |
129
|
|
|
} else { |
130
|
|
|
$_SERVER[ 'QUERY_STRING' ] = $query; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (isset($_GET[ 'SEGMENTS_STRING' ])) { |
134
|
|
|
$uri = $_GET[ 'SEGMENTS_STRING' ]; |
135
|
|
|
unset($_GET[ 'SEGMENTS_STRING' ]); |
136
|
|
|
|
137
|
|
|
$_SERVER[ 'QUERY_STRING' ] = str_replace([ |
138
|
|
|
'SEGMENTS_STRING=' . $uri . '&', |
139
|
|
|
'SEGMENTS_STRING=' . $uri, |
140
|
|
|
], '', $_SERVER[ 'QUERY_STRING' ]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
144
|
|
|
|
145
|
|
|
if ($uri === '/' || $uri === '') { |
146
|
|
|
return '/'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $uri; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// ------------------------------------------------------------------------ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Segments::parseQueryString |
156
|
|
|
* |
157
|
|
|
* Parse QUERY_STRING |
158
|
|
|
* |
159
|
|
|
* Will parse QUERY_STRING and automatically detect the URI from it. |
160
|
|
|
* |
161
|
|
|
* @access protected |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
protected function parseQueryString() |
165
|
|
|
{ |
166
|
|
|
$uri = isset($_SERVER[ 'QUERY_STRING' ]) |
167
|
|
|
? $_SERVER[ 'QUERY_STRING' ] |
168
|
|
|
: @getenv('QUERY_STRING'); |
169
|
|
|
|
170
|
|
|
if (trim($uri, '/') === '') { |
171
|
|
|
return ''; |
172
|
|
|
} elseif (strncmp($uri, '/', 1) === 0) { |
173
|
|
|
$uri = explode('?', $uri, 2); |
174
|
|
|
$_SERVER[ 'QUERY_STRING' ] = isset($uri[ 1 ]) |
175
|
|
|
? $uri[ 1 ] |
176
|
|
|
: ''; |
177
|
|
|
$uri = rawurldecode($uri[ 0 ]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
181
|
|
|
|
182
|
|
|
return $uri; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// -------------------------------------------------------------------- |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Segments::getString |
189
|
|
|
* |
190
|
|
|
* Get String |
191
|
|
|
* |
192
|
|
|
* Get Requested Uri String |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getString() |
197
|
|
|
{ |
198
|
|
|
return empty($this->string) |
199
|
|
|
? '/' |
200
|
|
|
: $this->string; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// ------------------------------------------------------------------------ |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Segments::addString |
207
|
|
|
* |
208
|
|
|
* @param string $string |
209
|
|
|
* |
210
|
|
|
* @return \O2System\Kernel\Http\Message\Uri\Segments |
211
|
|
|
*/ |
212
|
|
|
public function addString($string) |
213
|
|
|
{ |
214
|
|
|
$string = $this->string . '/' . trim($string, '/'); |
215
|
|
|
|
216
|
|
|
return $this->withString($string); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// ------------------------------------------------------------------------ |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Segments::withString |
223
|
|
|
* |
224
|
|
|
* @param string $string |
225
|
|
|
* |
226
|
|
|
* @return \O2System\Kernel\Http\Message\Uri\Segments |
227
|
|
|
*/ |
228
|
|
|
public function withString($string) |
229
|
|
|
{ |
230
|
|
|
$string = trim(remove_invisible_characters($string, false), '/'); |
231
|
|
|
|
232
|
|
|
return $this->withParts(explode('/', $string)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// ------------------------------------------------------------------------ |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Segments::withParts |
239
|
|
|
* |
240
|
|
|
* @param array $parts |
241
|
|
|
* |
242
|
|
|
* @return \O2System\Kernel\Http\Message\Uri\Segments |
243
|
|
|
*/ |
244
|
|
|
public function withParts(array $parts) |
245
|
|
|
{ |
246
|
|
|
$uri = clone $this; |
247
|
|
|
$uri->setParts($parts); |
248
|
|
|
|
249
|
|
|
return $uri; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
// ------------------------------------------------------------------------ |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Segments::addParts |
256
|
|
|
* |
257
|
|
|
* @param array $parts |
258
|
|
|
* |
259
|
|
|
* @return \O2System\Kernel\Http\Message\Uri\Segments |
260
|
|
|
*/ |
261
|
|
|
public function addParts(array $parts) |
262
|
|
|
{ |
263
|
|
|
$parts = array_merge($this->parts, $parts); |
264
|
|
|
|
265
|
|
|
return $this->withParts($parts); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// ------------------------------------------------------------------------ |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Segments::getPart |
272
|
|
|
* |
273
|
|
|
* Get Segment |
274
|
|
|
* |
275
|
|
|
* @param int $n (n) of Uri Segments |
276
|
|
|
* |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
|
|
public function getPart($n) |
280
|
|
|
{ |
281
|
|
|
return isset($this->parts[ $n ]) |
282
|
|
|
? $this->parts[ $n ] |
283
|
|
|
: false; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// ------------------------------------------------------------------------ |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Segments::getParts |
290
|
|
|
* |
291
|
|
|
* Get Segments |
292
|
|
|
* |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
public function getParts() |
296
|
|
|
{ |
297
|
|
|
return $this->parts; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// ------------------------------------------------------------------------ |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Segments::setParts |
304
|
|
|
* |
305
|
|
|
* @param array $parts |
306
|
|
|
* |
307
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
308
|
|
|
*/ |
309
|
|
|
protected function setParts(array $parts) |
310
|
|
|
{ |
311
|
|
|
if (count($parts)) { |
312
|
|
|
$validSegments = []; |
313
|
|
|
|
314
|
|
|
if (count($parts)) { |
315
|
|
|
foreach ($parts as $part) { |
316
|
|
|
// Filter segments for security |
317
|
|
|
if ($part = trim($this->filterPart($part))) { |
318
|
|
|
if (class_exists('O2System\Framework', false)) { |
319
|
|
|
if (false !== ($language = language()->registered($part))) { |
|
|
|
|
320
|
|
|
language()->setDefault($part); |
321
|
|
|
|
322
|
|
|
continue; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
if ( ! in_array($part, $validSegments)) { |
327
|
|
|
$validSegments[] = $part; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$validSegments = array_filter($validSegments); |
334
|
|
|
array_unshift($validSegments, null); |
335
|
|
|
|
336
|
|
|
unset($validSegments[ 0 ]); |
337
|
|
|
|
338
|
|
|
$this->parts = $validSegments; |
339
|
|
|
$this->string = implode('/', $this->parts); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
// ------------------------------------------------------------------------ |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Segments::hasPart |
347
|
|
|
* |
348
|
|
|
* Has Segment |
349
|
|
|
* |
350
|
|
|
* @param string $part |
351
|
|
|
* @param bool $isCaseSensitive |
352
|
|
|
* |
353
|
|
|
* @return bool |
354
|
|
|
*/ |
355
|
|
|
public function hasPart($part, $isCaseSensitive = false) |
356
|
|
|
{ |
357
|
|
|
return (bool)in_array($part, $this->parts, $isCaseSensitive); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
// ------------------------------------------------------------------------ |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Segments::getTotalParts |
364
|
|
|
* |
365
|
|
|
* Get Total Segments |
366
|
|
|
* |
367
|
|
|
* @return int |
368
|
|
|
*/ |
369
|
|
|
public function getTotalParts() |
370
|
|
|
{ |
371
|
|
|
return count($this->parts); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
// ------------------------------------------------------------------------ |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Segments::__toString |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
public function __toString() |
382
|
|
|
{ |
383
|
|
|
if (count($this->parts)) { |
384
|
|
|
return implode('/', $this->parts); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return ''; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
// ------------------------------------------------------------------------ |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Segments::filterPart |
394
|
|
|
* |
395
|
|
|
* Filters segments for malicious characters. |
396
|
|
|
* |
397
|
|
|
* @param string $string URI String |
398
|
|
|
* |
399
|
|
|
* @return mixed |
400
|
|
|
* @throws RuntimeException |
401
|
|
|
*/ |
402
|
|
|
protected function filterPart($string) |
403
|
|
|
{ |
404
|
|
|
if (function_exists('config')) { |
405
|
|
|
$config = config('uri'); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if (empty($config)) { |
409
|
|
|
$config = new SplArrayObject([ |
410
|
|
|
'permittedChars' => 'a-z 0-9~%.:_\-@#', |
411
|
|
|
'suffix' => null, |
412
|
|
|
]); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
if ( ! empty($string) AND |
416
|
|
|
! empty($config->offsetGet('permittedChars')) AND |
417
|
|
|
! preg_match('/^[' . $config->offsetGet('permittedChars') . ']+$/i', $string) AND |
418
|
|
|
! is_cli() |
419
|
|
|
) { |
420
|
|
|
throw new RuntimeException('E_URI_HAS_DISALLOWED_CHARACTERS', 105); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
$regex = ['$', '(', ')', '%28', '%29', 'index', '.php', '.phtml']; // Bad |
424
|
|
|
$replace = ['$', '(', ')', '(', ')']; // Good |
425
|
|
|
|
426
|
|
|
if ( ! empty($config)) { |
427
|
|
|
array_push($regex, $config->offsetGet('suffix')); |
428
|
|
|
array_push($replace, ''); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
// Convert programatic characters to entities and return |
432
|
|
|
return str_replace($regex, $replace, $string); |
433
|
|
|
} |
434
|
|
|
} |