1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Kotori.php |
4
|
|
|
* |
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
6
|
|
|
* |
7
|
|
|
* This content is released under the Apache 2 License |
8
|
|
|
* |
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
10
|
|
|
* |
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
12
|
|
|
* you may not use this file except in compliance with the License. |
13
|
|
|
* You may obtain a copy of the License at |
14
|
|
|
* |
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
16
|
|
|
* |
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20
|
|
|
* See the License for the specific language governing permissions and |
21
|
|
|
* limitations under the License. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Request Class |
26
|
|
|
* |
27
|
|
|
* @package Kotori |
28
|
|
|
* @subpackage Http |
29
|
|
|
* @author Kokororin |
30
|
|
|
* @link https://kotori.love |
31
|
|
|
*/ |
32
|
|
|
namespace Kotori\Http; |
33
|
|
|
|
34
|
|
|
use Kotori\Core\Container; |
35
|
|
|
use Kotori\Debug\Hook; |
36
|
|
|
use Kotori\Exception\NotFoundException; |
37
|
|
|
|
38
|
|
|
class Request |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Ip address |
42
|
|
|
* |
43
|
|
|
* @var mixed |
44
|
|
|
*/ |
45
|
|
|
protected $ip = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Http headers |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $headers = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Session has been started or not |
56
|
|
|
* |
57
|
|
|
* @var boolean |
58
|
|
|
*/ |
59
|
|
|
protected $hasSessionStarted = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Class constructor |
63
|
|
|
* |
64
|
|
|
* Initialize Request. |
65
|
|
|
* |
66
|
|
|
* @return void |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
Hook::listen(__CLASS__); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Internal method used to retrieve values from given arrays. |
75
|
|
|
* |
76
|
|
|
* @param array $source |
77
|
|
|
* @param mixed $key |
78
|
|
|
* @param mixed $default |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
protected function getRequestParams(&$source, $key = null, $default = null) |
82
|
|
|
{ |
83
|
|
|
// If $key is NULL, it means that the whole $source is requested |
84
|
|
|
if (!isset($key) || $key == null) { |
85
|
|
|
$key = array_keys($source); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (is_array($key)) { |
89
|
|
|
$output = []; |
90
|
|
|
foreach ($key as $k) { |
91
|
|
|
$output[$k] = $this->getRequestParams($source, $k); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $output; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (isset($source[$key])) { |
98
|
|
|
$value = $source[$key]; |
99
|
|
|
} else { |
100
|
|
|
return $default; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $value; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Fetch an item from the GET array |
109
|
|
|
* |
110
|
|
|
* @param mixed $key |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function get($key = null, $default = null) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
return $this->getRequestParams($_GET, $key, $default); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Fetch an item from the POST array |
120
|
|
|
* |
121
|
|
|
* @param mixed $key |
122
|
|
|
* @param mixed $default |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public function post($key = null, $default = null) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$rawPostData = file_get_contents('php://input'); |
128
|
|
|
$source = json_decode($rawPostData, true); |
129
|
|
|
if (json_last_error() != JSON_ERROR_NONE) { |
130
|
|
|
$source = $_POST; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->getRequestParams($source, $key, $default); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Fetch an item from the SERVER array |
138
|
|
|
* |
139
|
|
|
* @param mixed $key |
140
|
|
|
* @param mixed $default |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function server($key = null, $default = null) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return $this->getRequestParams($_SERVER, $key, $default); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set or Get cookie |
150
|
|
|
* |
151
|
|
|
* @param mixed $key |
152
|
|
|
* @param string $value |
153
|
|
|
* @param mixed $options |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
2 |
|
public function cookie($key = '', $value = '', $options = null) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$defaultOptions = [ |
159
|
2 |
|
'prefix' => '', |
160
|
|
|
'expire' => 86400, |
161
|
|
|
'path' => '/', |
162
|
|
|
'secure' => false, |
163
|
|
|
'httponly' => false, |
164
|
|
|
]; |
165
|
|
|
|
166
|
2 |
|
if (!is_null($options)) { |
167
|
|
|
if (is_numeric($options)) { |
168
|
|
|
$options = ['expire' => $options]; |
169
|
|
|
} elseif (is_string($options)) { |
170
|
|
|
parse_str($options, $options); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$options = array_merge($defaultOptions, array_change_key_case($options)); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
if (!empty($options['httponly'])) { |
177
|
|
|
ini_set('session.cookie_httponly', 1); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
if (is_null($key)) { |
181
|
|
|
if (empty($_COOKIE)) { |
182
|
|
|
return null; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$prefix = empty($value) ? $options['prefix'] : $value; |
186
|
|
|
if (!empty($prefix)) { |
187
|
|
|
foreach ($_COOKIE as $key => $val) { |
188
|
|
|
if (0 === stripos($key, $prefix)) { |
189
|
|
|
setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
190
|
|
|
unset($_COOKIE[$key]); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return null; |
196
|
2 |
|
} elseif ('' === $key) { |
197
|
|
|
// Get All Cookie |
198
|
|
|
return $_COOKIE; |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
$key = $options['prefix'] . str_replace('.', '_', $key); |
202
|
2 |
|
if ('' === $value) { |
203
|
2 |
|
if (isset($_COOKIE[$key])) { |
204
|
1 |
|
$value = $_COOKIE[$key]; |
205
|
1 |
|
if (0 === strpos($value, 'kotori:')) { |
206
|
|
|
$value = substr($value, 6); |
207
|
|
|
return array_map('urldecode', json_decode(MAGIC_QUOTES_GPC ? stripslashes($value) : $value, true)); |
208
|
|
|
} else { |
209
|
1 |
|
return $value; |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
1 |
|
return null; |
213
|
|
|
} |
214
|
|
|
} else { |
215
|
2 |
|
if (is_null($value)) { |
216
|
1 |
|
setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
217
|
1 |
|
unset($_COOKIE[$key]); // Delete Cookie |
218
|
|
|
} else { |
219
|
|
|
// Set Cookie |
220
|
2 |
|
if (is_array($value)) { |
221
|
|
|
$value = 'kotori:' . json_encode(array_map('urlencode', $value)); |
222
|
|
|
} |
223
|
|
|
|
224
|
2 |
|
$expire = !empty($options['expire']) ? time() + intval($options['expire']) : 0; |
225
|
2 |
|
setcookie($key, $value, $expire, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
226
|
2 |
|
$_COOKIE[$key] = $value; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Session Initialize |
235
|
|
|
* |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
public function sessionInit() |
239
|
|
|
{ |
240
|
|
|
ini_set('session.auto_start', 0); |
241
|
|
|
$config = Container::get('config')->get('session'); |
242
|
|
|
if ($config['adapter'] != '') { |
243
|
|
|
$class = '\\Kotori\\Http\\Session\\' . ucfirst($config['adapter']); |
244
|
|
|
if (!class_exists($class) || !session_set_save_handler(new $class($config))) { |
245
|
|
|
throw new NotFoundException('error session handler:' . $class); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
session_name('KOTORI_SESSID'); |
250
|
|
|
if ($config['auto_start']) { |
251
|
|
|
session_start(); |
252
|
|
|
$this->hasSessionStarted = true; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Start a Session |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
*/ |
261
|
2 |
|
public function sessionStart() |
262
|
|
|
{ |
263
|
2 |
|
if (!$this->hasSessionStarted) { |
264
|
1 |
|
if (PHP_SESSION_ACTIVE != session_status()) { |
265
|
1 |
|
session_start(); |
266
|
|
|
} |
267
|
|
|
|
268
|
1 |
|
$this->hasSessionStarted = true; |
269
|
|
|
} |
270
|
2 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set or Get session |
274
|
|
|
* |
275
|
|
|
* @param mixed $key |
276
|
|
|
* @param string $value |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
2 |
|
public function session($key = '', $value = '') |
|
|
|
|
280
|
|
|
{ |
281
|
2 |
|
$this->sessionStart(); |
282
|
|
|
|
283
|
2 |
|
if (is_null($key)) { |
284
|
|
|
if (empty($_SESSION)) { |
285
|
|
|
return null; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
unset($_SESSION); |
289
|
2 |
|
} elseif ('' === $key) { |
290
|
|
|
// Get All Session |
291
|
|
|
return $_SESSION; |
292
|
|
|
} |
293
|
|
|
|
294
|
2 |
|
if ('' === $value) { |
295
|
2 |
|
return isset($_SESSION[$key]) ? $_SESSION[$key] : null; |
296
|
|
|
} else { |
297
|
2 |
|
if (is_null($value)) { |
298
|
1 |
|
unset($_SESSION[$key]); |
299
|
|
|
} else { |
300
|
2 |
|
$_SESSION[$key] = $value; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
2 |
|
return null; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Is HTTPS? |
309
|
|
|
* |
310
|
|
|
* Determines if the application is accessed via an encrypted |
311
|
|
|
* (HTTPS) connection. |
312
|
|
|
* |
313
|
|
|
* @return boolean |
314
|
|
|
*/ |
315
|
2 |
|
public function isSecure() |
|
|
|
|
316
|
|
|
{ |
317
|
2 |
|
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { |
318
|
|
|
return true; |
319
|
2 |
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) { |
320
|
|
|
return true; |
321
|
2 |
|
} elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { |
322
|
|
|
return true; |
323
|
|
|
} |
324
|
|
|
|
325
|
2 |
|
return false; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Base URL |
330
|
|
|
* |
331
|
|
|
* Returns base url |
332
|
|
|
* |
333
|
|
|
* @return string |
334
|
|
|
*/ |
335
|
1 |
|
public function getBaseUrl() |
|
|
|
|
336
|
|
|
{ |
337
|
1 |
|
if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST'])) { |
338
|
1 |
|
$base_url = ($this->isSecure() ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] |
339
|
1 |
|
. substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
340
|
|
|
} else { |
341
|
|
|
$base_url = 'http://localhost/'; |
342
|
|
|
} |
343
|
|
|
|
344
|
1 |
|
return rtrim($base_url, '/') . '/'; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Returns Client Ip Address |
349
|
|
|
* |
350
|
|
|
* @param int $type |
351
|
|
|
* @return string |
352
|
|
|
*/ |
353
|
1 |
|
public function getClientIp($type = 0) |
|
|
|
|
354
|
|
|
{ |
355
|
1 |
|
$type = $type ? 1 : 0; |
356
|
|
|
|
357
|
1 |
|
if ($this->ip !== null) { |
358
|
|
|
return $this->ip[$type]; |
359
|
|
|
} |
360
|
|
|
|
361
|
1 |
|
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { |
362
|
|
|
$this->ip = $_SERVER['HTTP_CF_CONNECTING_IP']; |
363
|
1 |
|
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
364
|
|
|
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
365
|
|
|
$pos = array_search('unknown', $arr); |
366
|
|
|
if (false !== $pos) { |
367
|
|
|
unset($arr[$pos]); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$this->ip = trim($arr[0]); |
371
|
1 |
|
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { |
372
|
|
|
$this->ip = $_SERVER['HTTP_CLIENT_IP']; |
373
|
1 |
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) { |
374
|
1 |
|
$this->ip = $_SERVER['REMOTE_ADDR']; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// Check ip |
378
|
1 |
|
$long = sprintf("%u", ip2long($this->ip)); |
379
|
1 |
|
$this->ip = $long ? [$this->ip, $long] : ['0.0.0.0', 0]; |
380
|
1 |
|
return $this->ip[$type]; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Returns Http Host |
385
|
|
|
* |
386
|
|
|
* @return string |
387
|
|
|
*/ |
388
|
1 |
|
public function getHostName() |
|
|
|
|
389
|
|
|
{ |
390
|
1 |
|
$possibleHostSources = ['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR']; |
391
|
|
|
$sourceTransformations = [ |
392
|
1 |
|
"HTTP_X_FORWARDED_HOST" => function ($value) { |
393
|
|
|
$elements = explode(',', $value); |
394
|
|
|
return trim(end($elements)); |
395
|
1 |
|
}, |
396
|
|
|
]; |
397
|
1 |
|
$host = ''; |
398
|
1 |
|
foreach ($possibleHostSources as $source) { |
399
|
1 |
|
if (!empty($host)) { |
400
|
1 |
|
break; |
401
|
|
|
} |
402
|
|
|
|
403
|
1 |
|
if (empty($_SERVER[$source])) { |
404
|
1 |
|
continue; |
405
|
|
|
} |
406
|
|
|
|
407
|
1 |
|
$host = $_SERVER[$source]; |
408
|
1 |
|
if (array_key_exists($source, $sourceTransformations)) { |
409
|
1 |
|
$host = $sourceTransformations[$source]($host); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
// Remove port number from host |
414
|
1 |
|
$host = preg_replace('/:\d+$/', '', $host); |
415
|
|
|
|
416
|
1 |
|
return trim($host); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Return specified header |
421
|
|
|
* |
422
|
|
|
* @param string $name |
423
|
|
|
* @return string |
424
|
|
|
*/ |
425
|
1 |
|
public function getHeader($name) |
|
|
|
|
426
|
|
|
{ |
427
|
1 |
|
if (empty($this->headers)) { |
428
|
1 |
|
if (function_exists('apache_request_headers')) { |
429
|
|
|
$this->headers = apache_request_headers(); |
430
|
|
|
} else { |
431
|
1 |
|
$server = $_SERVER; |
432
|
1 |
|
foreach ($server as $key => $value) { |
433
|
1 |
|
if (0 === strpos($key, 'HTTP_')) { |
434
|
1 |
|
$key = str_replace('_', '-', strtolower(substr($key, 5))); |
435
|
1 |
|
$this->headers[$key] = $value; |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
1 |
|
if (isset($server['CONTENT_TYPE'])) { |
440
|
|
|
$this->headers['content-type'] = $server['CONTENT_TYPE']; |
441
|
|
|
} |
442
|
|
|
|
443
|
1 |
|
if (isset($server['CONTENT_LENGTH'])) { |
444
|
|
|
$this->headers['content-length'] = $server['CONTENT_LENGTH']; |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
|
448
|
1 |
|
$this->headers = array_change_key_case($this->headers); |
449
|
|
|
} |
450
|
|
|
|
451
|
1 |
|
$name = str_replace('_', '-', strtolower($name)); |
452
|
1 |
|
return isset($this->headers[$name]) ? $this->headers[$name] : null; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Detect whether request method is GET |
457
|
|
|
* |
458
|
|
|
* @return boolean |
459
|
|
|
*/ |
460
|
|
|
public function isGet() |
|
|
|
|
461
|
|
|
{ |
462
|
|
|
return 'get' == strtolower($_SERVER['REQUEST_METHOD']); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Detect whether request method is POST |
467
|
|
|
* |
468
|
|
|
* @return boolean |
469
|
|
|
*/ |
470
|
|
|
public function isPost() |
|
|
|
|
471
|
|
|
{ |
472
|
|
|
return 'post' == strtolower($_SERVER['REQUEST_METHOD']); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Detect whether request method is PUT |
477
|
|
|
* |
478
|
|
|
* @return boolean |
479
|
|
|
*/ |
480
|
|
|
public function isPut() |
|
|
|
|
481
|
|
|
{ |
482
|
|
|
return 'put' == strtolower($_SERVER['REQUEST_METHOD']); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Detect whether request method is PATCH |
487
|
|
|
* |
488
|
|
|
* @return boolean |
489
|
|
|
*/ |
490
|
|
|
public function isPatch() |
|
|
|
|
491
|
|
|
{ |
492
|
|
|
return 'patch' == strtolower($_SERVER['REQUEST_METHOD']); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Detect whether request method is DELETE |
497
|
|
|
* |
498
|
|
|
* @return boolean |
499
|
|
|
*/ |
500
|
|
|
public function isDelete() |
|
|
|
|
501
|
|
|
{ |
502
|
|
|
return 'delete' == strtolower($_SERVER['REQUEST_METHOD']); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Detect whether request method is OPTIONS |
507
|
|
|
* |
508
|
|
|
* @return boolean |
509
|
|
|
*/ |
510
|
|
|
public function isOptions() |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
return 'options' == strtolower($_SERVER['REQUEST_METHOD']); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Detect whether request method is AJAX |
517
|
|
|
* |
518
|
|
|
* @return boolean |
519
|
|
|
*/ |
520
|
|
|
public function isAjax() |
|
|
|
|
521
|
|
|
{ |
522
|
|
|
return ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? true : false; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
/** |
526
|
|
|
* Is CLI? |
527
|
|
|
* |
528
|
|
|
* Test to see if a request was made from the command line. |
529
|
|
|
* |
530
|
|
|
* @return boolean |
531
|
|
|
*/ |
532
|
3 |
|
public function isCli() |
533
|
|
|
{ |
534
|
3 |
|
return PHP_SAPI === 'cli'; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Detect whether user agent is Mobile |
539
|
|
|
* |
540
|
|
|
* @return boolean |
541
|
|
|
*/ |
542
|
1 |
|
public function isMobile() |
|
|
|
|
543
|
|
|
{ |
544
|
1 |
|
$userAgent = isset($_SERVER['USER_AGENT']) ? $_SERVER['USER_AGENT'] : null; |
545
|
1 |
|
if ($userAgent == null) { |
546
|
1 |
|
return false; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
return preg_match('/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $userAgent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($userAgent, 0, 4)); |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.