Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
detector.php
1<?php
2
11
13{
17 public function detectBrowser(?string $userAgent): Browser
18 {
19 $browser = $this->getKnownAgent($userAgent);
20
21 if ($browser === null)
22 {
23 if (ini_get('browscap') != '')
24 {
25 $info = get_browser($userAgent);
26
27 if ($info)
28 {
29 $deviceType = $this->getDeviceType($info);
30 $browser = new Browser($info->browser, $info->platform, $deviceType);
31 }
32 }
33 }
34
35 if ($browser === null)
36 {
37 $browser = new Browser();
38 }
39
40 $browser->setUserAgent($userAgent);
41
42 return $browser;
43 }
44
45 protected function getKnownAgent(?string $userAgent)
46 {
47 if ($userAgent !== null)
48 {
49 if (str_contains($userAgent, 'Bitrix24.Disk'))
50 {
51 if (str_contains($userAgent, 'Windows'))
52 {
53 return new Browser('Bitrix24.Disk', 'Windows', DeviceType::DESKTOP);
54 }
55 return new Browser('Bitrix24.Disk', 'macOS', DeviceType::DESKTOP);
56 }
57
58 if (str_contains($userAgent, 'BitrixDesktop'))
59 {
60 if (str_contains($userAgent, 'Windows'))
61 {
62 return new Browser('Bitrix24.Desktop', 'Windows', DeviceType::DESKTOP);
63 }
64 if (str_contains($userAgent, 'Mac OS'))
65 {
66 return new Browser('Bitrix24.Desktop', 'macOS', DeviceType::DESKTOP);
67 }
68 return new Browser('Bitrix24.Desktop', 'Linux', DeviceType::DESKTOP);
69 }
70
71 if (str_contains($userAgent, 'BitrixMobile') || str_contains($userAgent, 'Bitrix24/'))
72 {
73 if (str_contains($userAgent, 'iPhone') || str_contains($userAgent, 'iPad') || str_contains($userAgent, 'Darwin'))
74 {
75 $device = (str_contains($userAgent, 'iPad') ? DeviceType::TABLET : DeviceType::MOBILE_PHONE);
76 return new Browser('Bitrix24.Mobile', 'iOS', $device);
77 }
78
79 $device = (str_contains($userAgent, 'Tablet') ? DeviceType::TABLET : DeviceType::MOBILE_PHONE);
80 return new Browser('Bitrix24.Mobile', 'Android', $device);
81 }
82 }
83 return null;
84 }
85
86 protected function getDeviceType($info)
87 {
88 if ($info->istablet)
89 {
90 $deviceType = DeviceType::TABLET;
91 }
92 elseif ($info->ismobiledevice)
93 {
94 $deviceType = DeviceType::MOBILE_PHONE;
95 }
96 elseif ($info->device_type == 'TV Device')
97 {
98 $deviceType = DeviceType::TV;
99 }
100 elseif ($info->device_type == 'Desktop')
101 {
102 $deviceType = DeviceType::DESKTOP;
103 }
104 else
105 {
106 $deviceType = DeviceType::UNKNOWN;
107 }
108
109 return $deviceType;
110 }
111}
getKnownAgent(?string $userAgent)
Definition detector.php:45
detectBrowser(?string $userAgent)
Definition detector.php:17