|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Almendra\Http\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\UriInterface as PsrUri; |
|
6
|
|
|
|
|
7
|
|
|
class URI |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Retrieve the parameters from the URI |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $uri The URI |
|
13
|
|
|
* @param boolean $serialized It's to be serialized? |
|
14
|
|
|
* @return mixed |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function getQueryParams($uri, $serialized = true) |
|
17
|
|
|
{ |
|
18
|
|
|
$params = ''; |
|
19
|
|
|
|
|
20
|
|
|
// do parameters exist? |
|
21
|
|
|
if (strstr($uri, '?')) { |
|
22
|
|
|
$params = substr($uri, strpos($uri, '?') + 1); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// check the parameters |
|
26
|
|
|
if (!self::isQueryParamsValid($params)) { |
|
27
|
|
|
throw new \InvalidArgumentException("Invalid query parameters"); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (!$serialized) { |
|
31
|
|
|
$params = static::deserializeQueryParams($params); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $params; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns the query params as an associative array |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $params The query parameters |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function deserializeQueryParams($params) |
|
44
|
|
|
{ |
|
45
|
|
|
$fields = []; |
|
46
|
|
|
|
|
47
|
|
|
$params = explode('&', $params); |
|
48
|
|
|
foreach ($params as $param) { |
|
49
|
|
|
$parts = explode('=', $param); |
|
50
|
|
|
|
|
51
|
|
|
// eliminate not defined parameters |
|
52
|
|
|
if (count($parts) != 2) { |
|
53
|
|
|
continue; |
|
54
|
|
|
} elseif (null === $parts[0] || '' === $parts[0]) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$fields[$parts[0]] = $parts[1]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $fields; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Serializes the query params |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $queryParams The query params |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function serializeQueryParams(array $queryParams) |
|
71
|
|
|
{ |
|
72
|
|
|
$params = ''; |
|
73
|
|
|
foreach ($queryParams as $key => $value) { |
|
74
|
|
|
$params .= $key . '=' . $value . '&'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return substr($params, 0, -1); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Check query parameters validity |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $params The query parameters |
|
84
|
|
|
* @return boolean true if valid |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function isQueryParamsValid($params) |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_array($params)) { |
|
89
|
|
|
throw new \InvalidArgumentException("Query parameters must be an string"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$params = self::deserializeQueryParams($params); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Percentage encode a query. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $query The query to be encoded. |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function percentEncode($query) |
|
104
|
|
|
{ |
|
105
|
|
|
return htmlentities(strip_tags($query), ENT_QUOTES); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function isQueryValid($query) |
|
109
|
|
|
{ |
|
110
|
|
|
if (!is_string($query) && !method_exists($query, '__toString')) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function isValid($uri) |
|
118
|
|
|
{ |
|
119
|
|
|
if (!($uri instanceof PsrUri) && !is_string($uri)) { |
|
120
|
|
|
return false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Retrieve the fragment from the URI |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $uri The URI |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function getQueryFragment($uri) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Validates a port within TCP and UDP ranges. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $port The port |
|
141
|
|
|
* @return boolean |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function isPortValid($port) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
return true; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Validates a path. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $path The path |
|
152
|
|
|
* @return boolean |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function isPathValid($path) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function validator() |
|
160
|
|
|
{ |
|
161
|
|
|
return [ |
|
162
|
|
|
'name' => [ |
|
163
|
|
|
'min' => 255, |
|
164
|
|
|
], |
|
165
|
|
|
|
|
166
|
|
|
'value' => [ |
|
167
|
|
|
], |
|
168
|
|
|
]; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.