1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework; |
3
|
|
|
|
4
|
|
|
final class RequestUtils |
5
|
|
|
{ |
6
|
|
|
public static function explode($string) |
7
|
|
|
{ |
8
|
|
|
if (false !== strpos($string, '?')) { |
9
|
|
|
return explode('?', $string, 2); |
10
|
|
|
} elseif (false !== strpos($string, '&')) { |
11
|
|
|
return explode('&', $string, 2); |
12
|
|
|
} |
13
|
|
|
return [$string, null]; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function transform($string) |
17
|
|
|
{ |
18
|
|
|
$string = str_replace(['?','&','=','//'], ['','/','/','/0/'], $string); |
19
|
|
|
return trim($string, ' /'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function format($string) |
23
|
|
|
{ |
24
|
|
|
$data = []; |
25
|
|
|
$parts = self::split($string); |
26
|
|
|
$num = count($parts); |
27
|
|
|
for ($position = 0; $position < $num; $position +=2) { |
28
|
|
|
$data[$parts[$position]] = $position == $num -1 ? null : |
29
|
|
|
$parts[$position + 1]; |
30
|
|
|
} |
31
|
|
|
return $data; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function split($string) |
35
|
|
|
{ |
36
|
|
|
$parts = explode('/', $string); |
37
|
|
|
$parts = array_map('urldecode', $parts); |
38
|
|
|
return array_diff($parts, ['']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function removeSuffix($string, $suffixes = []) |
42
|
|
|
{ |
43
|
|
|
if (is_array($suffixes)) { |
44
|
|
|
$stringRev = strrev($string); |
45
|
|
|
foreach ($suffixes as $suffix) { |
46
|
|
|
$suffixRev = strrev($suffix); |
47
|
|
|
$suffixLen = strlen($suffix); |
48
|
|
|
if (0 === strncasecmp($suffixRev, $stringRev, $suffixLen)) { |
49
|
|
|
return strrev(substr($stringRev, $suffixLen)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $string; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function sanitizeString($string) |
57
|
|
|
{ |
58
|
|
|
// Strip tags, optionally strip or encode special characters. |
59
|
|
|
$string = filter_var($string, FILTER_SANITIZE_STRING); |
60
|
|
|
$unwanted = [ |
61
|
|
|
"`", |
62
|
|
|
//"'", |
63
|
|
|
//'"', |
64
|
|
|
"\b", |
65
|
|
|
"\n", |
66
|
|
|
"\r", |
67
|
|
|
"\t", |
68
|
|
|
//"?", |
69
|
|
|
//"!", |
70
|
|
|
//"~", |
71
|
|
|
//"#", |
72
|
|
|
//"^", |
73
|
|
|
//"&", |
74
|
|
|
//"*", |
75
|
|
|
//"=", |
76
|
|
|
//"[", |
77
|
|
|
//"]", |
78
|
|
|
//":", |
79
|
|
|
//";", |
80
|
|
|
//",", |
81
|
|
|
//"|", |
82
|
|
|
"\\", |
83
|
|
|
//"{", |
84
|
|
|
//"}", |
85
|
|
|
//"(", |
86
|
|
|
//")", |
87
|
|
|
"\$" |
88
|
|
|
]; |
89
|
|
|
$string = str_replace($unwanted, '', $string); |
90
|
|
|
return $string; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function parse($string, $path, $filename, $suffixes) |
94
|
|
|
{ |
95
|
|
|
$pathLen = strlen($path); |
96
|
|
|
if (0 === strncasecmp($path, $string, $pathLen)) { |
97
|
|
|
$string = substr($string, $pathLen); |
98
|
|
|
} |
99
|
|
|
$filenameLen = strlen($filename); |
100
|
|
|
if (0 === strncasecmp($filename, $string, $filenameLen)) { |
101
|
|
|
$string = substr($string, $filenameLen); |
102
|
|
|
} |
103
|
|
|
list($target, $query) = self::explode($string); |
104
|
|
|
$target = self::removeSuffix( |
105
|
|
|
self::transform($target), |
106
|
|
|
$suffixes |
107
|
|
|
); |
108
|
|
|
$query = self::transform($query); |
109
|
|
|
return [$target, $query]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|