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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
|
<?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.5';
/**
* 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
*/
if (null === $this->device && 'Android' === $osName && '' !== $osVersion) {
if (-1 === \version_compare($osVersion, '2.0')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
} elseif (\version_compare($osVersion, '3.0') >= 0
&& -1 === \version_compare($osVersion, '4.0')
) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
}
/**
* All detected feature phones running android are more likely a smartphone
*/
if (AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE === $this->device && 'Android' === $osFamily) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
}
/**
* All unknown devices under running Java ME are more likely features phones
*/
if ('Java ME' === $osName && null === $this->device) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE;
}
/**
* All devices running KaiOS are more likely features phones
*/
if ('KaiOS' === $osName) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE;
}
/**
* According to http://msdn.microsoft.com/en-us/library/ie/hh920767(v=vs.85).aspx
* Internet Explorer 10 introduces the "Touch" UA string token. If this token is present at the end of the
* UA string, the computer has touch capability, and is running Windows 8 (or later).
* This UA string will be transmitted on a touch-enabled system running Windows 8 (RT)
*
* As most touch enabled devices are tablets and only a smaller part are desktops/notebooks we assume that
* all Windows 8 touch devices are tablets.
*/
if (null === $this->device && ('Windows RT' === $osName || ('Windows' === $osName
&& \version_compare($osVersion, '8') >= 0)) && $this->isTouchEnabled()
) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
/**
* All devices running Puffin Secure Browser that contain letter 'D' are assumed to be desktops
*/
if (null === $this->device && $this->matchUserAgent('Puffin/(?:\d+[.\d]+)[LMW]D')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_DESKTOP;
}
/**
* All devices running Puffin Web Browser that contain letter 'P' are assumed to be smartphones
*/
if (null === $this->device && $this->matchUserAgent('Puffin/(?:\d+[.\d]+)[AIFLW]P')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
}
/**
* All devices running Puffin Web Browser that contain letter 'T' are assumed to be tablets
*/
if (null === $this->device && $this->matchUserAgent('Puffin/(?:\d+[.\d]+)[AILW]T')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
}
/**
* All devices running Opera TV Store are assumed to be a tv
*/
if ($this->matchUserAgent('Opera TV Store| OMI/')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* All devices running Coolita OS are assumed to be a tv
*/
if ('Coolita OS' === $osName) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* All devices that contain Andr0id in string are assumed to be a tv
*/
$hasDeviceTvType = false === \in_array($this->device, [
AbstractDeviceParser::DEVICE_TYPE_TV,
AbstractDeviceParser::DEVICE_TYPE_PERIPHERAL,
]) && $this->matchUserAgent('Andr0id|(?:Android(?: UHD)?|Google) TV|\(lite\) TV|BRAVIA| TV$');
if ($hasDeviceTvType) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* All devices running Tizen TV or SmartTV are assumed to be a tv
*/
if (null === $this->device && $this->matchUserAgent('SmartTV|Tizen.+ TV .+$')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* Devices running those clients are assumed to be a TV
*/
if (\in_array($clientName, [
'Kylo', 'Espial TV Browser', 'LUJO TV Browser', 'LogicUI TV Browser', 'Open TV Browser', 'Seraphic Sraf',
'Opera Devices', 'Crow Browser', 'Vewd Browser', 'TiviMate', 'Quick Search TV', 'QJY TV Browser', 'TV Bro',
])
) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* All devices containing TV fragment are assumed to be a tv
*/
if (null === $this->device && $this->matchUserAgent('\(TV;')) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_TV;
}
/**
* Set device type desktop if string ua contains desktop
*/
$hasDesktop = AbstractDeviceParser::DEVICE_TYPE_DESKTOP !== $this->device
&& false !== \strpos($this->userAgent, 'Desktop')
&& $this->hasDesktopFragment();
if ($hasDesktop) {
$this->device = AbstractDeviceParser::DEVICE_TYPE_DESKTOP;
}
// set device type to desktop for all devices running a desktop os that were not detected as another device type
if (null !== $this->device || !$this->isDesktop()) {
return;
}
$this->device = AbstractDeviceParser::DEVICE_TYPE_DESKTOP;
}
/**
* Tries to detect the operating system
*/
protected function parseOs(): void
{
$osParser = new OperatingSystem();
$osParser->setUserAgent($this->getUserAgent());
$osParser->setClientHints($this->getClientHints());
$osParser->setYamlParser($this->getYamlParser());
$osParser->setCache($this->getCache());
$this->os = $osParser->parse();
}
/**
* @param string $regex
*
* @return array|null
*/
protected function matchUserAgent(string $regex): ?array
{
$regex = '/(?:^|[^A-Z_-])(?:' . \str_replace('/', '\/', $regex) . ')/i';
if (\preg_match($regex, $this->userAgent, $matches)) {
return $matches;
}
return null;
}
/**
* Resets all detected data
*/
protected function reset(): void
{
$this->bot = null;
$this->client = null;
$this->device = null;
$this->os = null;
$this->brand = '';
$this->model = '';
$this->parsed = false;
}
}
|