GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f4c35f...6cea97 )
by やかみ
02:26
created

Request::sessionStart()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 0
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
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
     * Class constructor
49
     *
50
     * Initialize Request.
51
     *
52
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
53
     */
54
    public function __construct()
55
    {
56
        Hook::listen(__CLASS__);
57
    }
58
59
    /**
60
     * Internal method used to retrieve values from given arrays.
61
     *
62
     * @param  array $source
63
     * @param  mixed $key
64
     * @param  mixed $default
65
     * @return mixed
66
     */
67
    protected function getRequestParams(&$source, $key = null, $default = null)
68
    {
69
        // If $key is NULL, it means that the whole $source is requested
70
        if (!isset($key) || $key == null) {
71
            $key = array_keys($source);
72
        }
73
74
        if (is_array($key)) {
75
            $output = [];
76
            foreach ($key as $k) {
77
                $output[$k] = $this->getRequestParams($source, $k);
78
            }
79
80
            return $output;
81
        }
82
83
        if (isset($source[$key])) {
84
            $value = $source[$key];
85
        } else {
86
            return $default;
87
        }
88
89
        return $value;
90
91
    }
92
93
    /**
94
     * Fetch an item from the GET array
95
     *
96
     * @param  mixed $key
97
     * @return mixed
98
     */
99
    public function get($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_GET which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
    {
101
        return $this->getRequestParams($_GET, $key, $default);
102
    }
103
104
    /**
105
     * Fetch an item from the POST array
106
     *
107
     * @param  mixed $key
108
     * @param  mixed $default
109
     * @return mixed
110
     */
111
    public function post($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
post uses the super-global variable $_POST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
112
    {
113
        $rawPostData = file_get_contents('php://input');
114
        $source = json_decode($rawPostData, true);
115
        if (json_last_error() != JSON_ERROR_NONE) {
116
            $source = $_POST;
117
        }
118
119
        return $this->getRequestParams($source, $key, $default);
120
    }
121
122
    /**
123
     * Fetch an item from the SERVER array
124
     *
125
     * @param  mixed $key
126
     * @param  mixed $default
127
     * @return mixed
128
     */
129
    public function server($key = null, $default = null)
0 ignored issues
show
Coding Style introduced by
server uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
130
    {
131
        return $this->getRequestParams($_SERVER, $key, $default);
132
    }
133
134
    /**
135
     * Set or Get cookie
136
     *
137
     * @param  mixed  $key
138
     * @param  string $value
139
     * @param  mixed  $options
140
     * @return mixed
141
     */
142 2
    public function cookie($key = '', $value = '', $options = null)
0 ignored issues
show
Coding Style introduced by
cookie uses the super-global variable $_COOKIE which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
143
    {
144
        $defaultOptions = [
145 2
            'prefix' => '',
146
            'expire' => 86400,
147
            'path' => '/',
148
            'secure' => false,
149
            'httponly' => false,
150
        ];
151
152 2
        if (!is_null($options)) {
153
            if (is_numeric($options)) {
154
                $options = ['expire' => $options];
155
            } elseif (is_string($options)) {
156
                parse_str($options, $options);
157
            }
158
159
            $options = array_merge($defaultOptions, array_change_key_case($options));
160
        }
161
162 2
        if (!empty($options['httponly'])) {
163
            ini_set('session.cookie_httponly', 1);
164
        }
165
166 2
        if (is_null($key)) {
167
            if (empty($_COOKIE)) {
168
                return null;
169
            }
170
171
            $prefix = empty($value) ? $options['prefix'] : $value;
172
            if (!empty($prefix)) {
173
                foreach ($_COOKIE as $key => $val) {
174
                    if (0 === stripos($key, $prefix)) {
175
                        setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
176
                        unset($_COOKIE[$key]);
177
                    }
178
                }
179
            }
180
181
            return null;
182 2
        } elseif ('' === $key) {
183
            // Get All Cookie
184
            return $_COOKIE;
185
        }
186
187 2
        $key = $options['prefix'] . str_replace('.', '_', $key);
188 2
        if ('' === $value) {
189 2
            if (isset($_COOKIE[$key])) {
190 1
                $value = $_COOKIE[$key];
191 1
                if (0 === strpos($value, 'kotori:')) {
192
                    $value = substr($value, 6);
193
                    return array_map('urldecode', json_decode(MAGIC_QUOTES_GPC ? stripslashes($value) : $value, true));
194
                } else {
195 1
                    return $value;
196
                }
197
            } else {
198 1
                return null;
199
            }
200
        } else {
201 2
            if (is_null($value)) {
202 1
                setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
203 1
                unset($_COOKIE[$key]); // Delete Cookie
204
            } else {
205
                // Set Cookie
206 2
                if (is_array($value)) {
207
                    $value = 'kotori:' . json_encode(array_map('urlencode', $value));
208
                }
209
210 2
                $expire = !empty($options['expire']) ? time() + intval($options['expire']) : 0;
211 2
                setcookie($key, $value, $expire, $options['path'], $options['domain'], $options['secure'], $options['httponly']);
212 2
                $_COOKIE[$key] = $value;
213
            }
214
        }
215
216 2
        return null;
217
    }
218
219
    /**
220
     * Session Initialize
221
     *
222
     * @return void
223
     */
224
    public function sessionStart()
225
    {
226
        if (PHP_SESSION_ACTIVE != session_status()) {
227
            ini_set('session.auto_start', 0);
228
            $config = Container::get('config')->get('session');
229
            if ($config['adapter'] != '') {
230
                $class = '\\Kotori\\Http\\Session\\' . ucfirst($config['adapter']);
231
                if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
232
                    throw new NotFoundException('error session handler:' . $class);
233
                }
234
            }
235
236
            session_name('KOTORI_SESSID');
237
            session_start();
238
        }
239
    }
240
241
    /**
242
     * Set or Get session
243
     *
244
     * @param  mixed  $key
245
     * @param  string $value
246
     * @return mixed
247
     */
248 2
    public function session($key = '', $value = '')
0 ignored issues
show
Coding Style introduced by
session uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
249
    {
250 2
        if (is_null($key)) {
251
            if (empty($_SESSION)) {
252
                return null;
253
            }
254
255
            unset($_SESSION);
256 2
        } elseif ('' === $key) {
257
            // Get All Session
258
            return $_SESSION;
259
        }
260
261 2
        if ('' === $value) {
262 2
            return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
263
        } else {
264 2
            if (is_null($value)) {
265 1
                unset($_SESSION[$key]);
266
            } else {
267 2
                $_SESSION[$key] = $value;
268
            }
269
        }
270
271 2
        return null;
272
    }
273
274
    /**
275
     * Is HTTPS?
276
     *
277
     * Determines if the application is accessed via an encrypted
278
     * (HTTPS) connection.
279
     *
280
     * @return  boolean
281
     */
282 2
    public function isSecure()
0 ignored issues
show
Coding Style introduced by
isSecure uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
283
    {
284 2
        if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
285
            return true;
286 2
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) {
287
            return true;
288 2
        } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
289
            return true;
290
        }
291
292 2
        return false;
293
    }
294
295
    /**
296
     * Base URL
297
     *
298
     * Returns base url
299
     *
300
     * @return string
301
     */
302 1
    public function getBaseUrl()
0 ignored issues
show
Coding Style introduced by
getBaseUrl uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
303
    {
304 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'])) {
305 1
            $base_url = ($this->isSecure() ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']
306 1
            . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
307
        } else {
308
            $base_url = 'http://localhost/';
309
        }
310
311 1
        return rtrim($base_url, '/') . '/';
312
    }
313
314
    /**
315
     * Returns Client Ip Address
316
     *
317
     * @param  int     $type
318
     * @return string
319
     */
320 1
    public function getClientIp($type = 0)
0 ignored issues
show
Coding Style introduced by
getClientIp uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
321
    {
322 1
        $type = $type ? 1 : 0;
323
324 1
        if ($this->ip !== null) {
325
            return $this->ip[$type];
326
        }
327
328 1
        if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
329
            $this->ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
330 1
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
331
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
332
            $pos = array_search('unknown', $arr);
333
            if (false !== $pos) {
334
                unset($arr[$pos]);
335
            }
336
337
            $this->ip = trim($arr[0]);
338 1
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
339
            $this->ip = $_SERVER['HTTP_CLIENT_IP'];
340 1
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
341 1
            $this->ip = $_SERVER['REMOTE_ADDR'];
342
        }
343
344
        // Check ip
345 1
        $long = sprintf("%u", ip2long($this->ip));
346 1
        $this->ip = $long ? [$this->ip, $long] : ['0.0.0.0', 0];
347 1
        return $this->ip[$type];
348
    }
349
350
    /**
351
     * Returns Http Host
352
     *
353
     * @return string
354
     */
355 1
    public function getHostName()
0 ignored issues
show
Coding Style introduced by
getHostName uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
356
    {
357 1
        $possibleHostSources = ['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR'];
358
        $sourceTransformations = [
359 1
            "HTTP_X_FORWARDED_HOST" => function ($value) {
360
                $elements = explode(',', $value);
361
                return trim(end($elements));
362 1
            },
363
        ];
364 1
        $host = '';
365 1
        foreach ($possibleHostSources as $source) {
366 1
            if (!empty($host)) {
367 1
                break;
368
            }
369
370 1
            if (empty($_SERVER[$source])) {
371 1
                continue;
372
            }
373
374 1
            $host = $_SERVER[$source];
375 1
            if (array_key_exists($source, $sourceTransformations)) {
376 1
                $host = $sourceTransformations[$source]($host);
377
            }
378
        }
379
380
        // Remove port number from host
381 1
        $host = preg_replace('/:\d+$/', '', $host);
382
383 1
        return trim($host);
384
    }
385
386
    /**
387
     * Detect whether request method is GET
388
     *
389
     * @return boolean
390
     */
391
    public function isGet()
0 ignored issues
show
Coding Style introduced by
isGet uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
392
    {
393
        return 'GET' == $_SERVER['REQUEST_METHOD'];
394
    }
395
396
    /**
397
     * Detect whether request method is POST
398
     *
399
     * @return boolean
400
     */
401
    public function isPost()
0 ignored issues
show
Coding Style introduced by
isPost uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
402
    {
403
        return 'POST' == $_SERVER['REQUEST_METHOD'];
404
    }
405
406
    /**
407
     * Detect whether request method is PUT
408
     *
409
     * @return boolean
410
     */
411
    public function isPut()
0 ignored issues
show
Coding Style introduced by
isPut uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
412
    {
413
        return 'PUT' == $_SERVER['REQUEST_METHOD'];
414
    }
415
416
    /**
417
     * Detect whether request method is AJAX
418
     *
419
     * @return boolean
420
     */
421
    public function isAjax()
0 ignored issues
show
Coding Style introduced by
isAjax uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
422
    {
423
        return ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? true : false;
424
    }
425
426
    /**
427
     * Is CLI?
428
     *
429
     * Test to see if a request was made from the command line.
430
     *
431
     * @return  boolean
432
     */
433 3
    public function isCli()
434
    {
435 3
        return PHP_SAPI === 'cli';
436
    }
437
438
    /**
439
     * Detect whether user agent is Mobile
440
     *
441
     * @return boolean
442
     */
443 1
    public function isMobile()
0 ignored issues
show
Coding Style introduced by
isMobile uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
444
    {
445 1
        $userAgent = isset($_SERVER['USER_AGENT']) ? $_SERVER['USER_AGENT'] : null;
446 1
        if ($userAgent == null) {
447 1
            return false;
448
        }
449
450
        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));
451
    }
452
}
453