1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Request; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Storing information from the request and calculating related essentials. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CRequestBasic |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Properties |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
private $requestUri; // Request URI from $_SERVER |
18
|
|
|
private $scriptName; // Scriptname from $_SERVER, actual scriptname part |
19
|
|
|
private $path; // Scriptname from $_SERVER, path-part |
20
|
|
|
|
21
|
|
|
private $route; // The route |
22
|
|
|
private $routeParts; // The route as an array |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
private $currentUrl; // Current url |
26
|
|
|
private $siteUrl; // Url to this site, http://dbwebb.se |
27
|
|
|
private $baseUrl; // Url to root dir, siteUrl . /some/installation/directory/ |
28
|
|
|
|
29
|
|
|
private $server; // Mapped to $_SERVER |
30
|
|
|
private $get; // Mapped to $_GET |
31
|
|
|
private $post; // Mapped to $_POST |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->setGlobals(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Read info from the globals. |
49
|
|
|
* |
50
|
|
|
* @param array $globals use to initiate globals with values. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function setGlobals($globals = []) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->server = isset($globals['server']) ? array_merge($_SERVER, $globals['server']) : $_SERVER; |
57
|
|
|
$this->get = isset($globals['get']) ? array_merge($_GET, $globals['get']) : $_GET; |
58
|
|
|
$this->post = isset($globals['post']) ? array_merge($_POST, $globals['post']) : $_POST; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Init the request class by reading information from the request. |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function init() |
69
|
|
|
{ |
70
|
|
|
$this->requestUri = $this->getServer('REQUEST_URI'); |
71
|
|
|
$scriptName = $this->getServer('SCRIPT_NAME'); |
72
|
|
|
$this->path = rtrim(dirname($scriptName), '/'); |
73
|
|
|
$this->scriptName = basename($scriptName); |
74
|
|
|
|
75
|
|
|
// The route and its parts |
76
|
|
|
$this->extractRoute(); |
77
|
|
|
|
78
|
|
|
// Prepare to create siteUrl and baseUrl by using currentUrl |
79
|
|
|
$this->currentUrl = $this->getCurrentUrl(); |
80
|
|
|
$parts = parse_url($this->currentUrl); |
81
|
|
|
$this->siteUrl = "{$parts['scheme']}://{$parts['host']}" . (isset($parts['port']) |
82
|
|
|
? ":{$parts['port']}" |
83
|
|
|
: ''); |
84
|
|
|
$this->baseUrl = $this->siteUrl . $this->path; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get site url. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getSiteUrl() |
97
|
|
|
{ |
98
|
|
|
return $this->siteUrl; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get base url. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getBaseUrl() |
109
|
|
|
{ |
110
|
|
|
return $this->baseUrl; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get script name. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getScriptName() |
121
|
|
|
{ |
122
|
|
|
return $this->scriptName; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get route parts. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getRouteParts() |
133
|
|
|
{ |
134
|
|
|
return $this->routeParts; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the route. |
141
|
|
|
* |
142
|
|
|
* @return string as the current extracted route |
143
|
|
|
*/ |
144
|
|
|
public function getRoute() |
145
|
|
|
{ |
146
|
|
|
return $this->route; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Extract the part containing the route. |
153
|
|
|
* |
154
|
|
|
* @return string as the current extracted route |
155
|
|
|
*/ |
156
|
|
|
public function extractRoute() |
157
|
|
|
{ |
158
|
|
|
$requestUri = $this->getServer('REQUEST_URI'); |
159
|
|
|
$scriptName = $this->getServer('SCRIPT_NAME'); |
160
|
|
|
$scriptPath = dirname($scriptName); |
161
|
|
|
$scriptFile = basename($scriptName); |
162
|
|
|
|
163
|
|
|
// Compare REQUEST_URI and SCRIPT_NAME as long they match, |
164
|
|
|
// leave the rest as current request. |
165
|
|
|
$i = 0; |
166
|
|
|
$len = min(strlen($requestUri), strlen($scriptPath)); |
167
|
|
|
while ($i < $len |
168
|
|
|
&& $requestUri[$i] == $scriptPath[$i] |
169
|
|
|
) { |
170
|
|
|
$i++; |
171
|
|
|
} |
172
|
|
|
$route = trim(substr($requestUri, $i), '/'); |
173
|
|
|
|
174
|
|
|
// Does the request start with script-name - remove it. |
175
|
|
|
$len1 = strlen($route); |
176
|
|
|
$len2 = strlen($scriptFile); |
177
|
|
|
|
178
|
|
|
if ($len2 <= $len1 |
179
|
|
|
&& substr_compare($scriptFile, $route, 0, $len2, true) === 0 |
180
|
|
|
) { |
181
|
|
|
$route = substr($route, $len2 + 1); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Remove the ?-part from the query when analysing controller/metod/arg1/arg2 |
185
|
|
|
$queryPos = strpos($route, '?'); |
186
|
|
|
if ($queryPos !== false) { |
187
|
|
|
$route = substr($route, 0, $queryPos); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$route = ($route === false) ? '' : $route; |
191
|
|
|
|
192
|
|
|
$this->route = $route; |
193
|
|
|
$this->routeParts = explode('/', trim($route, '/')); |
194
|
|
|
//var_dump($route); |
195
|
|
|
return $this->route; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the current url. |
202
|
|
|
* |
203
|
|
|
* @param boolean $queryString attach query string, default is true. |
204
|
|
|
* |
205
|
|
|
* @return string as current url. |
206
|
|
|
*/ |
207
|
|
|
public function getCurrentUrl($queryString = true) |
208
|
|
|
{ |
209
|
|
|
$rs = $this->getServer('REQUEST_SCHEME'); |
210
|
|
|
$https = $this->getServer('HTTPS') == 'on' ? true : false; |
211
|
|
|
$sn = $this->getServer('SERVER_NAME'); |
212
|
|
|
$port = $this->getServer('SERVER_PORT'); |
213
|
|
|
|
214
|
|
|
$port = ($port == '80') |
215
|
|
|
? '' |
216
|
|
|
: (($port == 443 && $https) |
217
|
|
|
? '' |
218
|
|
|
: ':' . $port); |
219
|
|
|
|
220
|
|
|
if ($queryString) { |
221
|
|
|
$ru = rtrim($this->getServer('REQUEST_URI'), '/'); |
222
|
|
|
} else { |
223
|
|
|
$ru = rtrim(strtok($this->getServer('REQUEST_URI'), '?'), '/'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
$url = $rs ? $rs : 'http'; |
228
|
|
|
$url .= $https ? 's' : ''; |
229
|
|
|
$url .= '://'; |
230
|
|
|
$url .= $sn . $port . htmlspecialchars($ru); |
231
|
|
|
|
232
|
|
|
return $url; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get a value from the _SERVER array and use default if it is not set. |
239
|
|
|
* |
240
|
|
|
* @param string $key to check if it exists in the $_SERVER variable |
241
|
|
|
* @param string $default value to return as default |
242
|
|
|
* |
243
|
|
|
* @return mixed |
244
|
|
|
*/ |
245
|
|
|
public function getServer($key, $default = null) |
246
|
|
|
{ |
247
|
|
|
return isset($this->server[$key]) ? $this->server[$key] : $default; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set variable in the server array. |
254
|
|
|
* |
255
|
|
|
* @param mixed $key the key an the , or an key-value array |
256
|
|
|
* @param string $value the value of the key |
257
|
|
|
* |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function setServer($key, $value = null) |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
if (is_array($key)) { |
263
|
|
|
$this->server = array_merge($this->server, $key); |
264
|
|
|
} else { |
265
|
|
|
$this->server[$key] = $value; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get a value from the _GET array and use default if it is not set. |
273
|
|
|
* |
274
|
|
|
* @param string $key to check if it exists in the $_GET variable |
275
|
|
|
* @param string $default value to return as default |
276
|
|
|
* |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
|
|
public function getGet($key, $default = null) |
280
|
|
|
{ |
281
|
|
|
return isset($this->get[$key]) ? $this->get[$key] : $default; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set variable in the get array. |
288
|
|
|
* |
289
|
|
|
* @param mixed $key the key an the , or an key-value array |
290
|
|
|
* @param string $value the value of the key |
291
|
|
|
* |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
public function setGet($key, $value = null) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
if (is_array($key)) { |
297
|
|
|
$this->get = array_merge($this->get, $key); |
298
|
|
|
} else { |
299
|
|
|
$this->get[$key] = $value; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get a value from the _POST array and use default if it is not set. |
307
|
|
|
* |
308
|
|
|
* @param string $key to check if it exists in the $_POST variable |
309
|
|
|
* @param string $default value to return as default |
310
|
|
|
* |
311
|
|
|
* @return mixed |
312
|
|
|
*/ |
313
|
|
|
public function getPost($key = null, $default = null) |
314
|
|
|
{ |
315
|
|
|
if ($key) { |
|
|
|
|
316
|
|
|
return isset($this->post[$key]) ? $this->post[$key] : $default; |
317
|
|
|
} else { |
318
|
|
|
return $this->post; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: