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.

UserAgent::getLanguages()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Http;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class UserAgent
20
 *
21
 * @package O2System\Framework\Http
22
 */
23
class UserAgent
24
{
25
    /**
26
     * List of platforms to compare against current user agent
27
     *
28
     * @var array
29
     */
30
    public static $platforms = [];
31
32
    /**
33
     * List of browsers to compare against current user agent
34
     *
35
     * @var array
36
     */
37
    public static $browsers = [];
38
39
    /**
40
     * List of mobile browsers to compare against current user agent
41
     *
42
     * @var array
43
     */
44
    public static $mobiles = [];
45
46
    /**
47
     * List of robots to compare against current user agent
48
     *
49
     * @var array
50
     */
51
    public static $robots = [];
52
53
    /**
54
     * Current user-agent
55
     *
56
     * @var string
57
     */
58
    public $string = null;
59
60
    /**
61
     * Flag for if the user-agent belongs to a browser
62
     *
63
     * @var bool
64
     */
65
    public $isBrowser = false;
66
67
    /**
68
     * Flag for if the user-agent is a robot
69
     *
70
     * @var bool
71
     */
72
    public $isRobot = false;
73
74
    /**
75
     * Flag for if the user-agent is a mobile browser
76
     *
77
     * @var bool
78
     */
79
    public $isMobile = false;
80
81
    /**
82
     * Languages accepted by the current user agent
83
     *
84
     * @var array
85
     */
86
    public $languages = [];
87
88
    /**
89
     * Character sets accepted by the current user agent
90
     *
91
     * @var array
92
     */
93
    public $charsets = [];
94
95
    /**
96
     * Current user-agent platform
97
     *
98
     * @var string
99
     */
100
    public $platform = '';
101
102
    /**
103
     * Current user-agent browser
104
     *
105
     * @var string
106
     */
107
    public $browser = '';
108
109
    /**
110
     * Current user-agent version
111
     *
112
     * @var string
113
     */
114
    public $browserVersion = '';
115
116
    /**
117
     * Current user-agent mobile name
118
     *
119
     * @var string
120
     */
121
    public $mobile = '';
122
123
    /**
124
     * Current user-agent device type
125
     *
126
     * @type string
127
     */
128
    public $device = '';
129
130
    /**
131
     * Current user-agent robot name
132
     *
133
     * @var string
134
     */
135
    public $robot = '';
136
137
    /**
138
     * HTTP Referer
139
     *
140
     * @var    mixed
141
     */
142
    public $referer;
143
144
    // --------------------------------------------------------------------
145
146
    /**
147
     * UserAgent::__construct
148
     *
149
     * Sets the User Agent and runs the compilation routine
150
     *
151
     * @return    void
152
     */
153
    public function __construct()
154
    {
155
        if (isset($_SERVER[ 'HTTP_USER_AGENT' ])) {
156
            $this->string = trim($_SERVER[ 'HTTP_USER_AGENT' ]);
157
        }
158
159
        if ($this->string !== null && $this->loadAgentFile()) {
160
            $this->compileData();
161
        }
162
    }
163
164
    // --------------------------------------------------------------------
165
166
    /**
167
     * UserAgent::loadAgentFile
168
     *
169
     * Compile the User Agent Data
170
     *
171
     * @return    bool
172
     */
173
    protected function loadAgentFile()
174
    {
175
        if (($found = is_file(PATH_FRAMEWORK . 'Config/UserAgents.php'))) {
176
            include(PATH_FRAMEWORK . 'Config/UserAgents.php');
177
        }
178
179
        if ($found !== true) {
180
            return false;
181
        }
182
183
        $return = false;
184
185
        if (isset($platforms)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $platforms seems to never exist and therefore isset should always be false.
Loading history...
186
            static::$platforms = $platforms;
187
            unset($platforms);
188
            $return = true;
189
        }
190
191
        if (isset($browsers)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $browsers seems to never exist and therefore isset should always be false.
Loading history...
192
            static::$browsers = $browsers;
193
            unset($browsers);
194
            $return = true;
195
        }
196
197
        if (isset($mobiles)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mobiles seems to never exist and therefore isset should always be false.
Loading history...
198
            static::$mobiles = $mobiles;
199
            unset($mobiles);
200
            $return = true;
201
        }
202
203
        if (isset($robots)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $robots seems to never exist and therefore isset should always be false.
Loading history...
204
            static::$robots = $robots;
205
            unset($robots);
206
            $return = true;
207
        }
208
209
        return $return;
210
    }
211
212
    // --------------------------------------------------------------------
213
214
    /**
215
     * UserAgent::compileData
216
     *
217
     * Compile the User Agent Data
218
     */
219
    protected function compileData()
220
    {
221
        $this->setPlatform();
222
223
        foreach (['setRobot', 'setBrowser', 'setMobile'] as $function) {
224
            if ($this->$function() === true) {
225
                break;
226
            }
227
        }
228
    }
229
230
    // --------------------------------------------------------------------
231
232
    /**
233
     * UserAgent::setPlatform
234
     *
235
     * Set the Platform
236
     *
237
     * @return bool
238
     */
239
    protected function setPlatform()
240
    {
241
        if (is_array(static::$platforms) && count(static::$platforms) > 0) {
242
            foreach (static::$platforms as $key => $val) {
243
                if (preg_match('|' . preg_quote($key) . '|i', $this->string)) {
244
                    $this->platform = $val;
245
246
                    return true;
247
                }
248
            }
249
        }
250
251
        $this->platform = 'Unknown Platform';
252
253
        return false;
254
    }
255
256
    // --------------------------------------------------------------------
257
258
    /**
259
     * UserAgent::isBrowser
260
     *
261
     * Is Browser
262
     *
263
     * @param string|null $key
264
     *
265
     * @return bool
266
     */
267
    public function isBrowser($key = null)
268
    {
269
        if ( ! $this->isBrowser) {
270
            return false;
271
        }
272
273
        // No need to be specific, it's a browser
274
        if ($key === null) {
275
            return true;
276
        }
277
278
        // Check for a specific browser
279
        return (isset(static::$browsers[ $key ]) && $this->browser === static::$browsers[ $key ]);
280
    }
281
282
    // --------------------------------------------------------------------
283
284
    /**
285
     * Is Robot
286
     *
287
     * @param    string $key
288
     *
289
     * @return    bool
290
     */
291
    public function isRobot($key = null)
292
    {
293
        if ( ! $this->isRobot) {
294
            return false;
295
        }
296
297
        // No need to be specific, it's a robot
298
        if ($key === null) {
299
            return true;
300
        }
301
302
        // Check for a specific robot
303
        return (isset(static::$robots[ $key ]) && $this->robot === static::$robots[ $key ]);
304
    }
305
306
    // --------------------------------------------------------------------
307
308
    /**
309
     * UserAgent::isMobile
310
     *
311
     * Is Mobile
312
     *
313
     * @param  string $key
314
     *
315
     * @return bool
316
     */
317
    public function isMobile($key = null)
318
    {
319
        if ( ! $this->isMobile) {
320
            return false;
321
        }
322
323
        // No need to be specific, it's a mobile
324
        if ($key === null) {
325
            return true;
326
        }
327
328
        // Check for a specific robot
329
        return (isset(static::$mobiles[ $key ]) && $this->mobile === static::$mobiles[ $key ]);
330
    }
331
332
    // --------------------------------------------------------------------
333
334
    /**
335
     * UserAgent::isReferral
336
     *
337
     * Is this a referral from another site?
338
     *
339
     * @return bool
340
     */
341
    public function isReferral()
342
    {
343
        if ( ! isset($this->referer)) {
344
            if (empty($_SERVER[ 'HTTP_REFERER' ])) {
345
                $this->referer = false;
346
            } else {
347
                $referer_host = @parse_url($_SERVER[ 'HTTP_REFERER' ], PHP_URL_HOST);
348
                $own_host = parse_url(base_url(), PHP_URL_HOST);
349
350
                $this->referer = ($referer_host && $referer_host !== $own_host);
351
            }
352
        }
353
354
        return $this->referer;
355
    }
356
357
    // --------------------------------------------------------------------
358
359
    /**
360
     * UserAgent::getString
361
     *
362
     * Gets the user agent string.
363
     *
364
     * @return string
365
     */
366
    public function getString()
367
    {
368
        return $this->string;
369
    }
370
371
    // --------------------------------------------------------------------
372
373
    /**
374
     * UserAgent::getPlatform
375
     *
376
     * Gets the user platform name.
377
     *
378
     * @return string
379
     */
380
    public function getPlatform()
381
    {
382
        return $this->platform;
383
    }
384
385
    // --------------------------------------------------------------------
386
387
    /**
388
     * UserAgent::getBrowser
389
     *
390
     * Gets the user browser name.
391
     *
392
     * @return string
393
     */
394
    public function getBrowser()
395
    {
396
        return $this->browser;
397
    }
398
399
    // --------------------------------------------------------------------
400
401
    /**
402
     * UserAgent::getBrowserVersion
403
     *
404
     * Gets the user browser version.
405
     *
406
     * @return string
407
     */
408
    public function getBrowserVersion()
409
    {
410
        return $this->browserVersion;
411
    }
412
413
    // --------------------------------------------------------------------
414
415
    /**
416
     * UserAgent::getRobot
417
     *
418
     * Gets the robot name.
419
     *
420
     * @return string
421
     */
422
    public function getRobot()
423
    {
424
        return $this->robot;
425
    }
426
427
    // --------------------------------------------------------------------
428
429
    /**
430
     * UserAgent::getMobile
431
     *
432
     * Gets the user mobile device name.
433
     *
434
     * @return string
435
     */
436
    public function getMobile()
437
    {
438
        return $this->mobile;
439
    }
440
441
    // --------------------------------------------------------------------
442
443
    /**
444
     * UserAgent::isHttpReferrerExists
445
     *
446
     * Determine if the server http referrer exists.
447
     *
448
     * @return bool
449
     */
450
    public function isHttpReferrerExists()
451
    {
452
        return empty($_SERVER[ 'HTTP_REFERER' ]) ? '' : trim($_SERVER[ 'HTTP_REFERER' ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($_SERVER['H...SERVER['HTTP_REFERER']) returns the type string which is incompatible with the documented return type boolean.
Loading history...
453
    }
454
455
    // --------------------------------------------------------------------
456
457
    /**
458
     * UserAgent::isLanguageAllowed
459
     *
460
     * Checks for a particular language.
461
     *
462
     * @param string $lang
463
     *
464
     * @return bool
465
     */
466
    public function isLanguageAllowed($lang = 'en')
467
    {
468
        return in_array(strtolower($lang), $this->getLanguages(), true);
469
    }
470
471
    // --------------------------------------------------------------------
472
473
    /**
474
     * UserAgent::getLanguages
475
     *
476
     * Gets the accepted languages.
477
     *
478
     * @return    array
479
     */
480
    public function getLanguages()
481
    {
482
        if (count($this->languages) === 0) {
483
            $this->setLanguages();
484
        }
485
486
        return $this->languages;
487
    }
488
489
    // --------------------------------------------------------------------
490
491
    /**
492
     * UserAgent::setLanguages
493
     *
494
     * Sets the accepted languages.
495
     *
496
     * @return void
497
     */
498
    protected function setLanguages()
499
    {
500
        if ((count($this->languages) === 0) && ! empty($_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ])) {
501
            $this->languages = explode(
502
                ',',
503
                preg_replace(
504
                    '/(;\s?q=[0-9\.]+)|\s/i',
505
                    '',
506
                    strtolower(trim($_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ]))
507
                )
508
            );
509
        }
510
511
        if (count($this->languages) === 0) {
512
            $this->languages = ['Undefined'];
513
        }
514
    }
515
    // --------------------------------------------------------------------
516
517
    /**
518
     * UserAgent::isCharsetAllowed
519
     *
520
     * Checks for a particular character set.
521
     *
522
     * @param string $charset
523
     *
524
     * @return    bool
525
     */
526
    public function isCharsetAllowed($charset = 'utf-8')
527
    {
528
        return in_array(strtolower($charset), $this->getCharsets(), true);
529
    }
530
531
    // --------------------------------------------------------------------
532
533
    /**
534
     * UserAgent::getCharsets
535
     *
536
     * Gets the accepted character set.
537
     *
538
     * @return array
539
     */
540
    public function getCharsets()
541
    {
542
        if (count($this->charsets) === 0) {
543
            $this->setCharsets();
544
        }
545
546
        return $this->charsets;
547
    }
548
549
    // --------------------------------------------------------------------
550
551
    /**
552
     * UserAgent::setCharsets
553
     *
554
     * Sets the accepted character sets
555
     *
556
     * @return void
557
     */
558
    protected function setCharsets()
559
    {
560
        if ((count($this->charsets) === 0) && ! empty($_SERVER[ 'HTTP_ACCEPT_CHARSET' ])) {
561
            $this->charsets = explode(
562
                ',',
563
                preg_replace(
564
                    '/(;\s?q=.+)|\s/i',
565
                    '',
566
                    strtolower(trim($_SERVER[ 'HTTP_ACCEPT_CHARSET' ]))
567
                )
568
            );
569
        }
570
571
        if (count($this->charsets) === 0) {
572
            $this->charsets = ['Undefined'];
573
        }
574
    }
575
576
    // --------------------------------------------------------------------
577
578
    /**
579
     * UserAgent::parse
580
     *
581
     * Parse a custom user-agent string
582
     *
583
     * @param string $string
584
     *
585
     * @return void
586
     */
587
    public function parse($string)
588
    {
589
        // Reset values
590
        $this->isBrowser = false;
591
        $this->isRobot = false;
592
        $this->isMobile = false;
593
        $this->browser = '';
594
        $this->browserVersion = '';
595
        $this->mobile = '';
596
        $this->robot = '';
597
598
        // Set the new user-agent string and parse it, unless empty
599
        $this->string = $string;
600
601
        if ( ! empty($string)) {
602
            $this->compileData();
603
        }
604
    }
605
606
    // --------------------------------------------------------------------
607
608
    /**
609
     * UserAgent::setBrowser
610
     *
611
     * Sets the user agent browser.
612
     *
613
     * @return    bool
614
     */
615
    protected function setBrowser()
616
    {
617
        if (is_array(static::$browsers) && count(static::$browsers) > 0) {
618
            foreach (static::$browsers as $key => $val) {
619
                if (preg_match('|' . $key . '.*?([0-9\.]+)|i', $this->string, $match)) {
620
                    $this->isBrowser = true;
621
                    $this->browserVersion = $match[ 1 ];
622
                    $this->browser = $val;
623
                    $this->setMobile();
624
625
                    return true;
626
                }
627
            }
628
        }
629
630
        return false;
631
    }
632
633
    // --------------------------------------------------------------------
634
635
    /**
636
     * UserAgent::setMobile
637
     *
638
     * Sets the user agent mobile device.
639
     *
640
     * @return bool
641
     */
642
    protected function setMobile()
643
    {
644
        if (is_array(static::$mobiles) && count(static::$mobiles) > 0) {
645
            foreach (static::$mobiles as $key => $val) {
646
                if (false !== (stripos($this->string, $key))) {
647
                    $this->isMobile = true;
648
                    $this->mobile = $val;
649
650
                    return true;
651
                }
652
            }
653
        }
654
655
        return false;
656
    }
657
658
    // --------------------------------------------------------------------
659
660
    /**
661
     * UserAgent::setRobot
662
     *
663
     * Sets the user agent robot.
664
     *
665
     * @return bool
666
     */
667
    protected function setRobot()
668
    {
669
        if (is_array(static::$robots) && count(static::$robots) > 0) {
670
            foreach (static::$robots as $key => $val) {
671
                if (preg_match('|' . preg_quote($key) . '|i', $this->string)) {
672
                    $this->isRobot = true;
673
                    $this->robot = $val;
674
                    $this->setMobile();
675
676
                    return true;
677
                }
678
            }
679
        }
680
681
        return false;
682
    }
683
}