1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 zepi |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The RequestManager parses the request params and creates |
29
|
|
|
* the Request object for the input data. |
30
|
|
|
* |
31
|
|
|
* @package Zepi\Turbo\Manager |
32
|
|
|
* @author Matthias Zobrist <[email protected]> |
33
|
|
|
* @copyright Copyright (c) 2015 zepi |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
namespace Zepi\Turbo\Manager; |
37
|
|
|
|
38
|
|
|
use \Zepi\Turbo\Framework; |
39
|
|
|
use \Zepi\Turbo\Request\CliRequest; |
40
|
|
|
use \Zepi\Turbo\Request\WebRequest; |
41
|
|
|
use Zepi\Turbo\Request\RequestAbstract; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The RequestManager parses the request params and creates |
45
|
|
|
* the Request object for the input data. |
46
|
|
|
* |
47
|
|
|
* @author Matthias Zobrist <[email protected]> |
48
|
|
|
* @copyright Copyright (c) 2015 zepi |
49
|
|
|
*/ |
50
|
|
|
class RequestManager |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* @access protected |
54
|
|
|
* @var Framework |
55
|
|
|
*/ |
56
|
|
|
protected $framework; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructs the object |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* @param \Zepi\Turbo\Framework $framework |
63
|
|
|
*/ |
64
|
17 |
|
public function __construct(Framework $framework) |
65
|
|
|
{ |
66
|
17 |
|
$this->framework = $framework; |
67
|
17 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Builds the RequestAbstract object for the given input |
71
|
|
|
* data. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
*/ |
75
|
17 |
|
public function buildRequest() |
76
|
|
|
{ |
77
|
17 |
|
if (php_sapi_name() === 'cli') { |
78
|
17 |
|
return $this->buildCliRequest(); |
79
|
|
|
} else { |
80
|
|
|
return $this->buildWebRequest(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Builds the cli request object |
86
|
|
|
* |
87
|
|
|
* @access protected |
88
|
|
|
* @return \Zepi\Turbo\Request\CliRequest |
89
|
|
|
*/ |
90
|
17 |
|
protected function buildCliRequest() |
91
|
|
|
{ |
92
|
17 |
|
global $argv; |
93
|
|
|
|
94
|
17 |
|
$args = $argv; |
95
|
17 |
|
$params = array(); |
96
|
17 |
|
$route = ''; |
97
|
|
|
|
98
|
17 |
|
foreach ($args as $arg) { |
99
|
17 |
|
if ($arg === $_SERVER['PHP_SELF']) { |
100
|
17 |
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
17 |
|
if (strpos($arg, '-') === 0) { |
104
|
17 |
|
list($key, $value) = $this->parseArgument($arg); |
105
|
|
|
|
106
|
17 |
|
$params[$key] = $value; |
107
|
|
|
} else { |
108
|
|
|
if ($route !== '') { |
109
|
|
|
$route .= ' '; |
110
|
|
|
} |
111
|
|
|
|
112
|
17 |
|
$route .= $arg; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
17 |
|
$base = $argv[0]; |
117
|
|
|
|
118
|
17 |
|
$operatingSystem = $this->getOperatingSystem(); |
119
|
|
|
|
120
|
17 |
|
return new CliRequest($route, $params, $base, 'en_US', $operatingSystem); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Parses an argument and returns an array with key |
125
|
|
|
* and value. |
126
|
|
|
* |
127
|
|
|
* @param string $arg |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
17 |
|
protected function parseArgument($arg) |
131
|
|
|
{ |
132
|
17 |
|
$arg = ltrim($arg, '-'); |
133
|
|
|
|
134
|
17 |
|
$key = $arg; |
135
|
17 |
|
$value = true; |
136
|
|
|
|
137
|
17 |
|
if (strpos($arg, '=') !== false) { |
138
|
17 |
|
$key = substr($arg, 0, strpos($arg, '=')); |
139
|
17 |
|
$value = substr($arg, strpos($arg, '=') + 1); |
140
|
|
|
|
141
|
17 |
|
if (is_numeric($value)) { |
142
|
|
|
// Transform the value into the correct data type |
143
|
|
|
$value = $value * 1; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
17 |
|
return array($key, $value); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Builds the html request object |
152
|
|
|
* |
153
|
|
|
* @access protected |
154
|
|
|
* @return \Zepi\Turbo\Request\WebRequest |
155
|
|
|
*/ |
156
|
|
|
protected function buildWebRequest() |
157
|
|
|
{ |
158
|
|
|
$args = $_REQUEST; |
159
|
|
|
$params = array(); |
160
|
|
|
|
161
|
|
|
$route = $_SERVER['REQUEST_URI']; |
162
|
|
|
$posQuestionMark = strpos($route, '?'); |
163
|
|
|
if ($posQuestionMark !== false) { |
164
|
|
|
$route = substr($route, 0, $posQuestionMark); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$posIndex = strpos($route, 'index.php'); |
168
|
|
|
if ($posIndex !== false) { |
169
|
|
|
$route = substr($route, $posIndex + strlen('index.php')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Transform the arguments |
173
|
|
|
foreach ($args as $key => $value) { |
174
|
|
|
if (is_numeric($value)) { |
175
|
|
|
// Transform the value into the correct data type |
176
|
|
|
$value = $value * 1; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$params[$key] = $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Generate the full url and extract the base |
183
|
|
|
$scheme = $this->getScheme(); |
184
|
|
|
$fullUrl = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
185
|
|
|
|
186
|
|
|
$isSsl = false; |
187
|
|
|
if ($scheme == 'https') { |
188
|
|
|
$isSsl = true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$routePosition = strlen($fullUrl); |
192
|
|
|
if ($route !== '' && $route !== '/') { |
193
|
|
|
$routePosition = strpos($fullUrl, $route); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$method = $_SERVER['REQUEST_METHOD']; |
197
|
|
|
$requestedUrl = $this->getRequestedUrl(); |
198
|
|
|
$base = substr($fullUrl, 0, $routePosition); |
199
|
|
|
$headers = $this->getHeaders($_SERVER); |
200
|
|
|
$protocol = $_SERVER['SERVER_PROTOCOL']; |
201
|
|
|
|
202
|
|
|
$locale = 'en_US'; |
203
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
204
|
|
|
$locale = $this->getLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$operatingSystem = $this->getOperatingSystem(); |
208
|
|
|
|
209
|
|
|
return new WebRequest($method, $requestedUrl, $route, $params, $base, $locale, $operatingSystem, $isSsl, $headers, $protocol); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns the name of the operating system |
214
|
|
|
* |
215
|
|
|
* @access protected |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
17 |
|
protected function getOperatingSystem() |
219
|
|
|
{ |
220
|
17 |
|
$osRaw = strtolower(PHP_OS); |
221
|
|
|
|
222
|
17 |
|
if (strpos($osRaw, 'linux') !== false) { |
223
|
17 |
|
return RequestAbstract::OS_LINUX; |
224
|
|
|
} else if (strpos($osRaw, 'windows') !== false) { |
225
|
|
|
return RequestAbstract::OS_WINDOWS; |
226
|
|
|
} else { |
227
|
|
|
return RequestAbstract::OS_UNKNOWN; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the scheme of the request |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function getScheme() |
237
|
|
|
{ |
238
|
|
|
if (isset($_SERVER['REQUEST_SCHEME'])) { |
239
|
|
|
$scheme = $_SERVER['REQUEST_SCHEME']; |
240
|
|
|
} else if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) { |
241
|
|
|
$scheme = 'https'; |
242
|
|
|
} else { |
243
|
|
|
if ($_SERVER['SERVER_PORT'] == 443) { |
244
|
|
|
$scheme = 'https'; |
245
|
|
|
} else { |
246
|
|
|
$scheme = 'http'; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $scheme; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the requested url |
255
|
|
|
* |
256
|
|
|
* @access protected |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
protected function getRequestedUrl() |
260
|
|
|
{ |
261
|
|
|
$scheme = $this->getScheme(); |
262
|
|
|
|
263
|
|
|
return $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns an array with all headers of this request |
268
|
|
|
* |
269
|
|
|
* @access protected |
270
|
|
|
* @param array $params |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
protected function getHeaders($params) |
274
|
|
|
{ |
275
|
|
|
$headers = array(); |
276
|
|
|
|
277
|
|
|
foreach ($params as $name => $value) { |
278
|
|
|
if (substr($name, 0, 5) === 'HTTP_') { |
279
|
|
|
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
280
|
|
|
$headers[$key] = $value; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $headers; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns the best acceptable locale from the language header. |
289
|
|
|
* |
290
|
|
|
* @access protected |
291
|
|
|
* @param string $acceptLanguageHeader |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
protected function getLocale($acceptLanguageHeader) |
295
|
|
|
{ |
296
|
|
|
$acceptLanguageHeader = str_replace('-', '_', $acceptLanguageHeader); |
297
|
|
|
$locales = explode(',', $acceptLanguageHeader); |
298
|
|
|
|
299
|
|
|
$acceptableLocales = array(); |
300
|
|
|
foreach ($locales as $locale) { |
301
|
|
|
$priority = 1; |
302
|
|
|
if (strpos($locale, ';') !== false) { |
303
|
|
|
$priority = floatval(substr($locale, strpos($locale, ';'))); |
304
|
|
|
$locale = substr($locale, 0, strpos($locale, ';')); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$acceptableLocales[$priority] = $locale; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
krsort($acceptableLocales); |
311
|
|
|
|
312
|
|
|
// Get the first locale - it will have the highest priority |
313
|
|
|
$locale = array_shift($acceptableLocales); |
314
|
|
|
|
315
|
|
|
if ($locale == '') { |
316
|
|
|
$locale = 'en_US'; |
317
|
|
|
} else if (strpos($locale, '_') === false) { |
318
|
|
|
$locale = $locale . '_' . strtoupper($locale); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $locale; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|