forked from matomo-org/device-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceDetector.php
1163 lines (1006 loc) · 32.4 KB
/
DeviceDetector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link https://matomo.org
*
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
declare(strict_types=1);
namespace DeviceDetector;
use DeviceDetector\Cache\CacheInterface;
use DeviceDetector\Cache\StaticCache;
use DeviceDetector\Parser\AbstractBotParser;
use DeviceDetector\Parser\Bot;
use DeviceDetector\Parser\Client\AbstractClientParser;
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\Client\FeedReader;
use DeviceDetector\Parser\Client\Library;
use DeviceDetector\Parser\Client\MediaPlayer;
use DeviceDetector\Parser\Client\MobileApp;
use DeviceDetector\Parser\Client\PIM;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Parser\Device\Camera;
use DeviceDetector\Parser\Device\CarBrowser;
use DeviceDetector\Parser\Device\Console;
use DeviceDetector\Parser\Device\HbbTv;
use DeviceDetector\Parser\Device\Mobile;
use DeviceDetector\Parser\Device\Notebook;
use DeviceDetector\Parser\Device\PortableMediaPlayer;
use DeviceDetector\Parser\Device\ShellTv;
use DeviceDetector\Parser\OperatingSystem;
use DeviceDetector\Parser\VendorFragment;
use DeviceDetector\Yaml\ParserInterface as YamlParser;
use DeviceDetector\Yaml\Spyc;
/**
* Class DeviceDetector
*
* Magic Device Type Methods:
* @method bool isSmartphone()
* @method bool isFeaturePhone()
* @method bool isTablet()
* @method bool isPhablet()
* @method bool isConsole()
* @method bool isPortableMediaPlayer()
* @method bool isCarBrowser()
* @method bool isTV()
* @method bool isSmartDisplay()
* @method bool isSmartSpeaker()
* @method bool isCamera()
* @method bool isWearable()
* @method bool isPeripheral()
*
* Magic Client Type Methods:
* @method bool isBrowser()
* @method bool isFeedReader()
* @method bool isMobileApp()
* @method bool isPIM()
* @method bool isLibrary()
* @method bool isMediaPlayer()
*/
class DeviceDetector
{
/**
* Current version number of DeviceDetector
*/
public const VERSION = '6.4.1';
/**
* Constant used as value for unknown browser / os
*/
public const UNKNOWN = 'UNK';
/**
* Holds all registered client types
* @var array
*/
protected $clientTypes = [];
/**
* Holds the useragent that should be parsed
* @var string
*/
protected $userAgent = '';
/**
* Holds the client hints that should be parsed
* @var ?ClientHints
*/
protected $clientHints = null;
/**
* Holds the operating system data after parsing the UA
* @var ?array
*/
protected $os = null;
/**
* Holds the client data after parsing the UA
* @var ?array
*/
protected $client = null;
/**
* Holds the device type after parsing the UA
* @var ?int
*/
protected $device = null;
/**
* Holds the device brand data after parsing the UA
* @var string
*/
protected $brand = '';
/**
* Holds the device model data after parsing the UA
* @var string
*/
protected $model = '';
/**
* Holds bot information if parsing the UA results in a bot
* (All other information attributes will stay empty in that case)
*
* If $discardBotInformation is set to true, this property will be set to
* true if parsed UA is identified as bot, additional information will be not available
*
* If $skipBotDetection is set to true, bot detection will not be performed and isBot will
* always be false
*
* @var array|bool|null
*/
protected $bot = null;
/**
* @var bool
*/
protected $discardBotInformation = false;
/**
* @var bool
*/
protected $skipBotDetection = false;
/**
* Holds the cache class used for caching the parsed yml-Files
* @var CacheInterface|null
*/
protected $cache = null;
/**
* Holds the parser class used for parsing yml-Files
* @var YamlParser|null
*/
protected $yamlParser = null;
/**
* @var array<AbstractClientParser>
*/
protected $clientParsers = [];
/**
* @var array<AbstractDeviceParser>
*/
protected $deviceParsers = [];
/**
* @var array<AbstractBotParser>
*/
public $botParsers = [];
/**
* @var bool
*/
private $parsed = false;
/**
* Constructor
*
* @param string $userAgent UA to parse
* @param ClientHints $clientHints Browser client hints to parse
*/
public function __construct(string $userAgent = '', ?ClientHints $clientHints = null)
{
if ('' !== $userAgent) {
$this->setUserAgent($userAgent);
}
if ($clientHints instanceof ClientHints) {
$this->setClientHints($clientHints);
}
$this->addClientParser(new FeedReader());
$this->addClientParser(new MobileApp());
$this->addClientParser(new MediaPlayer());
$this->addClientParser(new PIM());
$this->addClientParser(new Browser());
$this->addClientParser(new Library());
$this->addDeviceParser(new HbbTv());
$this->addDeviceParser(new ShellTv());
$this->addDeviceParser(new Notebook());
$this->addDeviceParser(new Console());
$this->addDeviceParser(new CarBrowser());
$this->addDeviceParser(new Camera());
$this->addDeviceParser(new PortableMediaPlayer());
$this->addDeviceParser(new Mobile());
$this->addBotParser(new Bot());
}
/**
* @param string $methodName
* @param array $arguments
*
* @return bool
*/
public function __call(string $methodName, array $arguments): bool
{
foreach (AbstractDeviceParser::getAvailableDeviceTypes() as $deviceName => $deviceType) {
if (\strtolower($methodName) === 'is' . \strtolower(\str_replace(' ', '', $deviceName))) {
return $this->getDevice() === $deviceType;
}
}
foreach ($this->clientTypes as $client) {
if (\strtolower($methodName) === 'is' . \strtolower(\str_replace(' ', '', $client))) {
return $this->getClient('type') === $client;
}
}
throw new \BadMethodCallException("Method {$methodName} not found");
}
/**
* Sets the useragent to be parsed
*
* @param string $userAgent
*/
public function setUserAgent(string $userAgent): void
{
if ($this->userAgent !== $userAgent) {
$this->reset();
}
$this->userAgent = $userAgent;
}
/**
* Sets the browser client hints to be parsed
*
* @param ?ClientHints $clientHints
*/
public function setClientHints(?ClientHints $clientHints = null): void
{
if ($this->clientHints !== $clientHints) {
$this->reset();
}
$this->clientHints = $clientHints;
}
/**
* @param AbstractClientParser $parser
*
* @throws \Exception
*/
public function addClientParser(AbstractClientParser $parser): void
{
$this->clientParsers[] = $parser;
$this->clientTypes[] = $parser->getName();
}
/**
* @return array<AbstractClientParser>
*/
public function getClientParsers(): array
{
return $this->clientParsers;
}
/**
* @param AbstractDeviceParser $parser
*
* @throws \Exception
*/
public function addDeviceParser(AbstractDeviceParser $parser): void
{
$this->deviceParsers[] = $parser;
}
/**
* @return array<AbstractDeviceParser>
*/
public function getDeviceParsers(): array
{
return $this->deviceParsers;
}
/**
* @param AbstractBotParser $parser
*/
public function addBotParser(AbstractBotParser $parser): void
{
$this->botParsers[] = $parser;
}
/**
* @return array<AbstractBotParser>
*/
public function getBotParsers(): array
{
return $this->botParsers;
}
/**
* Sets whether to discard additional bot information
* If information is discarded it's only possible check whether UA was detected as bot or not.
* (Discarding information speeds up the detection a bit)
*
* @param bool $discard
*/
public function discardBotInformation(bool $discard = true): void
{
$this->discardBotInformation = $discard;
}
/**
* Sets whether to skip bot detection.
* It is needed if we want bots to be processed as a simple clients. So we can detect if it is mobile client,
* or desktop, or enything else. By default all this information is not retrieved for the bots.
*
* @param bool $skip
*/
public function skipBotDetection(bool $skip = true): void
{
$this->skipBotDetection = $skip;
}
/**
* Returns if the parsed UA was identified as a Bot
*
* @return bool
*
* @see bots.yml for a list of detected bots
*
*/
public function isBot(): bool
{
return !empty($this->bot);
}
/**
* Returns if the parsed UA was identified as a touch enabled device
*
* Note: That only applies to windows 8 tablets
*
* @return bool
*/
public function isTouchEnabled(): bool
{
$regex = 'Touch';
return !!$this->matchUserAgent($regex);
}
/**
* Returns if the parsed UA is detected as a mobile device
*
* @return bool
*/
public function isMobile(): bool
{
// Client hints indicate a mobile device
if ($this->clientHints instanceof ClientHints && $this->clientHints->isMobile()) {
return true;
}
// Mobile device types
if (\in_array($this->device, [
AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE,
AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE,
AbstractDeviceParser::DEVICE_TYPE_TABLET,
AbstractDeviceParser::DEVICE_TYPE_PHABLET,
AbstractDeviceParser::DEVICE_TYPE_CAMERA,
AbstractDeviceParser::DEVICE_TYPE_PORTABLE_MEDIA_PAYER,
])
) {
return true;
}
// non mobile device types
if (\in_array($this->device, [
AbstractDeviceParser::DEVICE_TYPE_TV,
AbstractDeviceParser::DEVICE_TYPE_SMART_DISPLAY,
AbstractDeviceParser::DEVICE_TYPE_CONSOLE,
])
) {
return false;
}
// Check for browsers available for mobile devices only
if ($this->usesMobileBrowser()) {
return true;
}
$osName = $this->getOs('name');
if (empty($osName) || self::UNKNOWN === $osName) {
return false;
}
return !$this->isBot() && !$this->isDesktop();
}
/**
* Returns if the parsed UA was identified as desktop device
* Desktop devices are all devices with an unknown type that are running a desktop os
*
* @return bool
*
* @see OperatingSystem::$desktopOsArray
*
*/
public function isDesktop(): bool
{
$osName = $this->getOsAttribute('name');
if (empty($osName) || self::UNKNOWN === $osName) {
return false;
}
// Check for browsers available for mobile devices only
if ($this->usesMobileBrowser()) {
return false;
}
return OperatingSystem::isDesktopOs($osName);
}
/**
* Returns the operating system data extracted from the parsed UA
*
* If $attr is given only that property will be returned
*
* @param string $attr property to return(optional)
*
* @return array|string|null
*/
public function getOs(string $attr = '')
{
if ('' === $attr) {
return $this->os;
}
return $this->getOsAttribute($attr);
}
/**
* Returns the client data extracted from the parsed UA
*
* If $attr is given only that property will be returned
*
* @param string $attr property to return(optional)
*
* @return array|string|null
*/
public function getClient(string $attr = '')
{
if ('' === $attr) {
return $this->client;
}
return $this->getClientAttribute($attr);
}
/**
* Returns the device type extracted from the parsed UA
*
* @return int|null
*
* @see AbstractDeviceParser::$deviceTypes for available device types
*
*/
public function getDevice(): ?int
{
return $this->device;
}
/**
* Returns the device type extracted from the parsed UA
*
* @return string
*
* @see AbstractDeviceParser::$deviceTypes for available device types
*
*/
public function getDeviceName(): string
{
if (null !== $this->getDevice()) {
return AbstractDeviceParser::getDeviceName($this->getDevice());
}
return '';
}
/**
* Returns the device brand extracted from the parsed UA
*
* @return string
*
* @see self::$deviceBrand for available device brands
*
* @deprecated since 4.0 - short codes might be removed in next major release
*/
public function getBrand(): string
{
return AbstractDeviceParser::getShortCode($this->brand);
}
/**
* Returns the full device brand name extracted from the parsed UA
*
* @return string
*
* @see self::$deviceBrand for available device brands
*
*/
public function getBrandName(): string
{
return $this->brand;
}
/**
* Returns the device model extracted from the parsed UA
*
* @return string
*/
public function getModel(): string
{
return $this->model;
}
/**
* Returns the user agent that is set to be parsed
*
* @return string
*/
public function getUserAgent(): string
{
return $this->userAgent;
}
/**
* Returns the client hints that is set to be parsed
*
* @return ?ClientHints
*/
public function getClientHints(): ?ClientHints
{
return $this->clientHints;
}
/**
* Returns the bot extracted from the parsed UA
*
* @return array|bool|null
*/
public function getBot()
{
return $this->bot;
}
/**
* Returns true, if userAgent was already parsed with parse()
*
* @return bool
*/
public function isParsed(): bool
{
return $this->parsed;
}
/**
* Triggers the parsing of the current user agent
*/
public function parse(): void
{
if ($this->isParsed()) {
return;
}
$this->parsed = true;
// skip parsing for empty useragents or those not containing any letter (if no client hints were provided)
if ((empty($this->userAgent) || !\preg_match('/([a-z])/i', $this->userAgent))
&& empty($this->clientHints)
) {
return;
}
$this->parseBot();
if ($this->isBot()) {
return;
}
$this->parseOs();
/**
* Parse Clients
* Clients might be browsers, Feed Readers, Mobile Apps, Media Players or
* any other application accessing with an parseable UA
*/
$this->parseClient();
$this->parseDevice();
}
/**
* Parses a useragent and returns the detected data
*
* ATTENTION: Use that method only for testing or very small applications
* To get fast results from DeviceDetector you need to make your own implementation,
* that should use one of the caching mechanisms. See README.md for more information.
*
* @param string $ua UserAgent to parse
* @param ?ClientHints $clientHints Client Hints to parse
*
* @return array
*
* @deprecated
*
* @internal
*
*/
public static function getInfoFromUserAgent(string $ua, ?ClientHints $clientHints = null): array
{
static $deviceDetector;
if (!($deviceDetector instanceof DeviceDetector)) {
$deviceDetector = new DeviceDetector();
}
$deviceDetector->setUserAgent($ua);
$deviceDetector->setClientHints($clientHints);
$deviceDetector->parse();
if ($deviceDetector->isBot()) {
return [
'user_agent' => $deviceDetector->getUserAgent(),
'bot' => $deviceDetector->getBot(),
];
}
/** @var array $client */
$client = $deviceDetector->getClient();
$browserFamily = 'Unknown';
if ($deviceDetector->isBrowser()
&& true === \is_array($client)
&& true === \array_key_exists('family', $client)
&& null !== $client['family']
) {
$browserFamily = $client['family'];
}
unset($client['short_name'], $client['family']);
/** @var array $os */
$os = $deviceDetector->getOs();
$osFamily = $os['family'] ?? 'Unknown';
unset($os['short_name'], $os['family']);
return [
'user_agent' => $deviceDetector->getUserAgent(),
'os' => $os,
'client' => $client,
'device' => [
'type' => $deviceDetector->getDeviceName(),
'brand' => $deviceDetector->getBrandName(),
'model' => $deviceDetector->getModel(),
],
'os_family' => $osFamily,
'browser_family' => $browserFamily,
];
}
/**
* Sets the Cache class
*
* @param CacheInterface $cache
*/
public function setCache(CacheInterface $cache): void
{
$this->cache = $cache;
}
/**
* Returns Cache object
*
* @return CacheInterface
*/
public function getCache(): CacheInterface
{
if (!empty($this->cache)) {
return $this->cache;
}
return new StaticCache();
}
/**
* Sets the Yaml Parser class
*
* @param YamlParser $yamlParser
*/
public function setYamlParser(YamlParser $yamlParser): void
{
$this->yamlParser = $yamlParser;
}
/**
* Returns Yaml Parser object
*
* @return YamlParser
*/
public function getYamlParser(): YamlParser
{
if (!empty($this->yamlParser)) {
return $this->yamlParser;
}
return new Spyc();
}
/**
* @param string $attr
*
* @return string
*/
protected function getClientAttribute(string $attr): string
{
if (!isset($this->client[$attr])) {
return self::UNKNOWN;
}
return $this->client[$attr];
}
/**
* @param string $attr
*
* @return string
*/
protected function getOsAttribute(string $attr): string
{
if (!isset($this->os[$attr])) {
return self::UNKNOWN;
}
return $this->os[$attr];
}
/**
* Returns if the parsed UA contains the 'Android; Tablet;' fragment
*
* @return bool
*/
protected function hasAndroidTableFragment(): bool
{
$regex = 'Android( [.0-9]+)?; Tablet;|Tablet(?! PC)|.*\-tablet$';
return !!$this->matchUserAgent($regex);
}
/**
* Returns if the parsed UA contains the 'Android; Mobile;' fragment
*
* @return bool
*/
protected function hasAndroidMobileFragment(): bool
{
$regex = 'Android( [.0-9]+)?; Mobile;|.*\-mobile$';
return !!$this->matchUserAgent($regex);
}
/**
* Returns if the parsed UA contains the 'Android; Mobile VR;' fragment
*
* @return bool
*/
protected function hasAndroidVRFragment(): bool
{
$regex = 'Android( [.0-9]+)?; Mobile VR;| VR ';
return !!$this->matchUserAgent($regex);
}
/**
* Returns if the parsed UA contains the 'Desktop;', 'Desktop x32;', 'Desktop x64;' or 'Desktop WOW64;' fragment
*
* @return bool
*/
protected function hasDesktopFragment(): bool
{
$regex = 'Desktop(?: (x(?:32|64)|WOW64))?;';
return !!$this->matchUserAgent($regex);
}
/**
* Returns if the parsed UA contains usage of a mobile only browser
*
* @return bool
*/
protected function usesMobileBrowser(): bool
{
return 'browser' === $this->getClient('type')
&& Browser::isMobileOnlyBrowser($this->getClientAttribute('name'));
}
/**
* Parses the UA for bot information using the Bot parser
*/
protected function parseBot(): void
{
if ($this->skipBotDetection) {
$this->bot = false;
return;
}
$parsers = $this->getBotParsers();
foreach ($parsers as $parser) {
$parser->setYamlParser($this->getYamlParser());
$parser->setCache($this->getCache());
$parser->setUserAgent($this->getUserAgent());
$parser->setClientHints($this->getClientHints());
if ($this->discardBotInformation) {
$parser->discardDetails();
}
$bot = $parser->parse();
if (!empty($bot)) {
$this->bot = $bot;
break;
}
}
}
/**
* Tries to detect the client (e.g. browser, mobile app, ...)
*/
protected function parseClient(): void
{
$parsers = $this->getClientParsers();
foreach ($parsers as $parser) {
$parser->setYamlParser($this->getYamlParser());
$parser->setCache($this->getCache());
$parser->setUserAgent($this->getUserAgent());
$parser->setClientHints($this->getClientHints());
$client = $parser->parse();
if (!empty($client)) {
$this->client = $client;
break;
}
}
}
/**
* Tries to detect the device type, model and brand
*/
protected function parseDevice(): void
{
$parsers = $this->getDeviceParsers();
foreach ($parsers as $parser) {
$parser->setYamlParser($this->getYamlParser());
$parser->setCache($this->getCache());
$parser->setUserAgent($this->getUserAgent());
$parser->setClientHints($this->getClientHints());
if ($parser->parse()) {
$this->device = $parser->getDeviceType();
$this->model = $parser->getModel();
$this->brand = $parser->getBrand();
break;
}
}
/**
* If no model could be parsed from useragent, we use the one from client hints if available
*/
if ($this->clientHints instanceof ClientHints && empty($this->model)) {
$this->model = $this->clientHints->getModel();
}
/**
* If no brand has been assigned try to match by known vendor fragments
*/
if (empty($this->brand)) {
$vendorParser = new VendorFragment($this->getUserAgent());
$vendorParser->setYamlParser($this->getYamlParser());
$vendorParser->setCache($this->getCache());
$this->brand = $vendorParser->parse()['brand'] ?? '';
}
$osName = $this->getOsAttribute('name');
$osFamily = $this->getOsAttribute('family');
$osVersion = $this->getOsAttribute('version');
$clientName = $this->getClientAttribute('name');
$appleOsNames = ['iPadOS', 'tvOS', 'watchOS', 'iOS', 'Mac'];
/**
* if it's fake UA then it's best not to identify it as Apple running Android OS or GNU/Linux
*/
if ('Apple' === $this->brand && !\in_array($osName, $appleOsNames)) {
$this->device = null;
$this->brand = '';
$this->model = '';
}
/**
* Assume all devices running iOS / Mac OS are from Apple
*/
if (empty($this->brand) && \in_array($osName, $appleOsNames)) {
$this->brand = 'Apple';
}
/**
* All devices containing VR fragment are assumed to be a wearable
*/
if (null === $this->device && $this->hasAndroidVRFragment()) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_WEARABLE;
}
/**
* Chrome on Android passes the device type based on the keyword 'Mobile'
* If it is present the device should be a smartphone, otherwise it's a tablet
* See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent
* Note: We do not check for browser (family) here, as there might be mobile apps using Chrome, that won't have
* a detected browser, but can still be detected. So we check the useragent for Chrome instead.
*/
if (null === $this->device && 'Android' === $osFamily
&& $this->matchUserAgent('Chrome/[.0-9]*')
) {
if ($this->matchUserAgent('(?:Mobile|eliboM)')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
} else {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
}
/**
* Some UA contain the fragment 'Pad/APad', so we assume those devices as tablets
*/
if (AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE === $this->device && $this->matchUserAgent('Pad/APad')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
/**
* Some UA contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices as tablets
*/
if (null === $this->device && ($this->hasAndroidTableFragment()
|| $this->matchUserAgent('Opera Tablet'))
) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
/**
* Some user agents simply contain the fragment 'Android; Mobile;', so we assume those devices as smartphones
*/
if (null === $this->device && $this->hasAndroidMobileFragment()) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
}
/**
* Android up to 3.0 was designed for smartphones only. But as 3.0, which was tablet only, was published
* too late, there were a bunch of tablets running with 2.x
* With 4.0 the two trees were merged and it is for smartphones and tablets
*
* So were are expecting that all devices running Android < 2 are smartphones
* Devices running Android 3.X are tablets. Device type of Android 2.X and 4.X+ are unknown