PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / matomo / device-detector / ClientHints.php
independent-analytics / vendor / matomo / device-detector Last commit date
Cache 2 years ago Parser 2 years ago Yaml 2 years ago regexes 2 years ago ClientHints.php 2 years ago DeviceDetector.php 2 years ago autoload.php 2 years ago phpcs.xml 2 years ago
ClientHints.php
290 lines
1 <?php
2
3 /**
4 * Device Detector - The Universal Device Detection library for parsing User Agents
5 *
6 * @link https://matomo.org
7 *
8 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
9 */
10 declare (strict_types=1);
11 namespace IAWP_SCOPED\DeviceDetector;
12
13 /** @internal */
14 class ClientHints
15 {
16 /**
17 * Represents `Sec-CH-UA-Arch` header field: The underlying architecture's instruction set
18 *
19 * @var string
20 */
21 protected $architecture = '';
22 /**
23 * Represents `Sec-CH-UA-Bitness` header field: The underlying architecture's bitness
24 *
25 * @var string
26 */
27 protected $bitness = '';
28 /**
29 * Represents `Sec-CH-UA-Mobile` header field: whether the user agent should receive a specifically "mobile" UX
30 *
31 * @var bool
32 */
33 protected $mobile = \false;
34 /**
35 * Represents `Sec-CH-UA-Model` header field: the user agent's underlying device model
36 *
37 * @var string
38 */
39 protected $model = '';
40 /**
41 * Represents `Sec-CH-UA-Platform` header field: the platform's brand
42 *
43 * @var string
44 */
45 protected $platform = '';
46 /**
47 * Represents `Sec-CH-UA-Platform-Version` header field: the platform's major version
48 *
49 * @var string
50 */
51 protected $platformVersion = '';
52 /**
53 * Represents `Sec-CH-UA-Full-Version` header field: the platform's major version
54 *
55 * @var string
56 */
57 protected $uaFullVersion = '';
58 /**
59 * Represents `Sec-CH-UA-Full-Version-List` header field: the full version for each brand in its brand list
60 *
61 * @var array
62 */
63 protected $fullVersionList = [];
64 /**
65 * Represents `x-requested-with` header field: Android app id
66 * @var string
67 */
68 protected $app = '';
69 /**
70 * Constructor
71 *
72 * @param string $model `Sec-CH-UA-Model` header field
73 * @param string $platform `Sec-CH-UA-Platform` header field
74 * @param string $platformVersion `Sec-CH-UA-Platform-Version` header field
75 * @param string $uaFullVersion `Sec-CH-UA-Full-Version` header field
76 * @param array $fullVersionList `Sec-CH-UA-Full-Version-List` header field
77 * @param bool $mobile `Sec-CH-UA-Mobile` header field
78 * @param string $architecture `Sec-CH-UA-Arch` header field
79 * @param string $bitness `Sec-CH-UA-Bitness`
80 * @param string $app `HTTP_X-REQUESTED-WITH`
81 */
82 public function __construct(string $model = '', string $platform = '', string $platformVersion = '', string $uaFullVersion = '', array $fullVersionList = [], bool $mobile = \false, string $architecture = '', string $bitness = '', string $app = '')
83 {
84 $this->model = $model;
85 $this->platform = $platform;
86 $this->platformVersion = $platformVersion;
87 $this->uaFullVersion = $uaFullVersion;
88 $this->fullVersionList = $fullVersionList;
89 $this->mobile = $mobile;
90 $this->architecture = $architecture;
91 $this->bitness = $bitness;
92 $this->app = $app;
93 }
94 /**
95 * Magic method to directly allow accessing the protected properties
96 *
97 * @param string $variable
98 *
99 * @return mixed
100 *
101 * @throws \Exception
102 */
103 public function __get(string $variable)
104 {
105 if (\property_exists($this, $variable)) {
106 return $this->{$variable};
107 }
108 throw new \Exception('Invalid ClientHint property requested.');
109 }
110 /**
111 * Returns if the client hints
112 *
113 * @return bool
114 */
115 public function isMobile() : bool
116 {
117 return $this->mobile;
118 }
119 /**
120 * Returns the Architecture
121 *
122 * @return string
123 */
124 public function getArchitecture() : string
125 {
126 return $this->architecture;
127 }
128 /**
129 * Returns the Bitness
130 *
131 * @return string
132 */
133 public function getBitness() : string
134 {
135 return $this->bitness;
136 }
137 /**
138 * Returns the device model
139 *
140 * @return string
141 */
142 public function getModel() : string
143 {
144 return $this->model;
145 }
146 /**
147 * Returns the Operating System
148 *
149 * @return string
150 */
151 public function getOperatingSystem() : string
152 {
153 return $this->platform;
154 }
155 /**
156 * Returns the Operating System version
157 *
158 * @return string
159 */
160 public function getOperatingSystemVersion() : string
161 {
162 return $this->platformVersion;
163 }
164 /**
165 * Returns the Browser name
166 *
167 * @return array<string, string>
168 */
169 public function getBrandList() : array
170 {
171 if (\is_array($this->fullVersionList) && \count($this->fullVersionList)) {
172 $brands = \array_column($this->fullVersionList, 'brand');
173 $versions = \array_column($this->fullVersionList, 'version');
174 if (\count($brands) === \count($versions)) {
175 // @phpstan-ignore-next-line
176 return \array_combine($brands, $versions);
177 }
178 }
179 return [];
180 }
181 /**
182 * Returns the Browser version
183 *
184 * @return string
185 */
186 public function getBrandVersion() : string
187 {
188 if (!empty($this->uaFullVersion)) {
189 return $this->uaFullVersion;
190 }
191 return '';
192 }
193 /**
194 * Returns the Android app id
195 *
196 * @return string
197 */
198 public function getApp() : string
199 {
200 return $this->app;
201 }
202 /**
203 * Factory method to easily instantiate this class using an array containing all available (client hint) headers
204 *
205 * @param array $headers
206 *
207 * @return ClientHints
208 */
209 public static function factory(array $headers) : ClientHints
210 {
211 $model = $platform = $platformVersion = $uaFullVersion = $architecture = $bitness = '';
212 $app = '';
213 $mobile = \false;
214 $fullVersionList = [];
215 foreach ($headers as $name => $value) {
216 switch (\str_replace('_', '-', \strtolower((string) $name))) {
217 case 'http-sec-ch-ua-arch':
218 case 'sec-ch-ua-arch':
219 case 'arch':
220 case 'architecture':
221 $architecture = \trim($value, '"');
222 break;
223 case 'http-sec-ch-ua-bitness':
224 case 'sec-ch-ua-bitness':
225 case 'bitness':
226 $bitness = \trim($value, '"');
227 break;
228 case 'http-sec-ch-ua-mobile':
229 case 'sec-ch-ua-mobile':
230 case 'mobile':
231 $mobile = \true === $value || '1' === $value || '?1' === $value;
232 break;
233 case 'http-sec-ch-ua-model':
234 case 'sec-ch-ua-model':
235 case 'model':
236 $model = \trim($value, '"');
237 break;
238 case 'http-sec-ch-ua-full-version':
239 case 'sec-ch-ua-full-version':
240 case 'uafullversion':
241 $uaFullVersion = \trim($value, '"');
242 break;
243 case 'http-sec-ch-ua-platform':
244 case 'sec-ch-ua-platform':
245 case 'platform':
246 $platform = \trim($value, '"');
247 break;
248 case 'http-sec-ch-ua-platform-version':
249 case 'sec-ch-ua-platform-version':
250 case 'platformversion':
251 $platformVersion = \trim($value, '"');
252 break;
253 case 'brands':
254 if (!empty($fullVersionList)) {
255 break;
256 }
257 // use this only if no other header already set the list
258 case 'fullversionlist':
259 $fullVersionList = \is_array($value) ? $value : $fullVersionList;
260 break;
261 case 'http-sec-ch-ua':
262 case 'sec-ch-ua':
263 if (!empty($fullVersionList)) {
264 break;
265 }
266 // use this only if no other header already set the list
267 case 'http-sec-ch-ua-full-version-list':
268 case 'sec-ch-ua-full-version-list':
269 $reg = '/^"([^"]+)"; ?v="([^"]+)"(?:, )?/';
270 $list = [];
271 while (\preg_match($reg, $value, $matches)) {
272 $list[] = ['brand' => $matches[1], 'version' => $matches[2]];
273 $value = \substr($value, \strlen($matches[0]));
274 }
275 if (\count($list)) {
276 $fullVersionList = $list;
277 }
278 break;
279 case 'http-x-requested-with':
280 case 'x-requested-with':
281 if ('xmlhttprequest' !== \strtolower($value)) {
282 $app = $value;
283 }
284 break;
285 }
286 }
287 return new self($model, $platform, $platformVersion, $uaFullVersion, $fullVersionList, $mobile, $architecture, $bitness, $app);
288 }
289 }
290