Repository navigation
Expand file tree
/
Copy pathWhatIsMyBrowserCom.php
More file actions
311 lines (258 loc) · 9.5 KB
/
Copy pathWhatIsMyBrowserCom.php
File metadata and controls
311 lines (258 loc) · 9.5 KB
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
<?php
namespace UserAgentParser\Provider\Http;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use stdClass;
use UserAgentParser\Exception;
use UserAgentParser\Model;
/**
* Abstraction of useragentstring.com
*
* @author Martin Keckeis <martin.keckeis1@gmail.com>
* @license MIT
* @see https://developers.whatismybrowser.com/reference
*/
class WhatIsMyBrowserCom extends AbstractHttpProvider
{
/**
* Name of the provider
*
* @var string
*/
protected $name = 'WhatIsMyBrowserCom';
/**
* Homepage of the provider
*
* @var string
*/
protected $homepage = 'https://www.whatismybrowser.com/';
protected $detectionCapabilities = [
'browser' => [
'name' => true,
'version' => true,
],
'renderingEngine' => [
'name' => true,
'version' => true,
],
'operatingSystem' => [
'name' => true,
'version' => true,
],
'device' => [
'model' => true,
'brand' => true,
'type' => false,
'isMobile' => false,
'isTouch' => false,
],
'bot' => [
'isBot' => false,
'name' => false,
'type' => false,
],
];
protected $defaultValues = [
'general' => [],
'browser' => [
'name' => [
'/^Unknown Mobile Browser$/i',
'/^Unknown browser$/i',
'/^Webkit based browser$/i',
'/^a UNIX based OS$/i',
],
],
'operatingSystem' => [
'name' => [
'/^Smart TV$/i',
],
],
'device' => [
'model' => [
// HTC generic or large parser error (over 1000 found)
'/^HTC$/i',
'/^Mobile$/i',
'/^Android Phone$/i',
'/^Android Tablet$/i',
'/^Tablet$/i',
],
],
];
private static $uri = 'http://api.whatismybrowser.com/api/v1/user_agent_parse';
private $apiKey;
public function __construct(Client $client, $apiKey)
{
parent::__construct($client);
$this->apiKey = $apiKey;
}
public function getVersion()
{
return;
}
/**
*
* @param string $userAgent
* @param array $headers
* @return stdClass
* @throws Exception\RequestException
*/
protected function getResult($userAgent, array $headers)
{
/*
* an empty UserAgent makes no sense
*/
if ($userAgent == '') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
$params = [
'user_key' => $this->apiKey,
'user_agent' => $userAgent,
];
$body = http_build_query($params, null, '&');
$request = new Request('POST', self::$uri, [], $body);
$response = $this->getResponse($request);
/*
* no json returned?
*/
$contentType = $response->getHeader('Content-Type');
if (! isset($contentType[0]) || $contentType[0] != 'application/json') {
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
$content = json_decode($response->getBody()->getContents());
/*
* No result
*/
if (isset($content->message_code) && $content->message_code == 'no_user_agent') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
/*
* Limit exceeded
*/
if (isset($content->message_code) && $content->message_code == 'usage_limit_exceeded') {
throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
}
/*
* Error
*/
if (isset($content->message_code) && $content->message_code == 'no_api_user_key') {
throw new Exception\InvalidCredentialsException('Missing API key for ' . $this->getName());
}
if (isset($content->message_code) && $content->message_code == 'user_key_invalid') {
throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
}
if (! isset($content->result) || $content->result !== 'success') {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
/*
* Missing data?
*/
if (! $content instanceof stdClass || ! isset($content->parse) || ! $content->parse instanceof stdClass) {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
}
return $content->parse;
}
/**
*
* @param stdClass $resultRaw
*
* @return bool
*/
private function hasResult(stdClass $resultRaw)
{
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name, 'browser', 'name') === true) {
return true;
}
if (isset($resultRaw->layout_engine_name) && $this->isRealResult($resultRaw->layout_engine_name) === true) {
return true;
}
if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name, 'operatingSystem', 'name') === true) {
return true;
}
if (isset($resultRaw->operating_platform) && $this->isRealResult($resultRaw->operating_platform, 'device', 'model') === true) {
return true;
}
if (isset($resultRaw->operating_platform_vendor_name) && $this->isRealResult($resultRaw->operating_platform_vendor_name) === true) {
return true;
}
return false;
}
/**
*
* @param Model\Browser $browser
* @param stdClass $resultRaw
*/
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
{
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name, 'browser', 'name') === true) {
$browser->setName($resultRaw->browser_name);
}
if (isset($resultRaw->browser_version_full) && $this->isRealResult($resultRaw->browser_version_full) === true) {
$browser->getVersion()->setComplete($resultRaw->browser_version_full);
}
}
/**
*
* @param Model\RenderingEngine $engine
* @param stdClass $resultRaw
*/
private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
{
if (isset($resultRaw->layout_engine_name) && $this->isRealResult($resultRaw->layout_engine_name) === true) {
$engine->setName($resultRaw->layout_engine_name);
}
if (isset($resultRaw->layout_engine_version) && $this->isRealResult($resultRaw->layout_engine_version) === true) {
$engine->getVersion()->setComplete($resultRaw->layout_engine_version);
}
}
/**
*
* @param Model\OperatingSystem $os
* @param stdClass $resultRaw
*/
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
{
if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name, 'operatingSystem', 'name') === true) {
$os->setName($resultRaw->operating_system_name);
}
if (isset($resultRaw->operating_system_version_full) && $this->isRealResult($resultRaw->operating_system_version_full) === true) {
$os->getVersion()->setComplete($resultRaw->operating_system_version_full);
}
}
/**
*
* @param Model\UserAgent $device
* @param stdClass $resultRaw
*/
private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
{
if (isset($resultRaw->operating_platform) && $this->isRealResult($resultRaw->operating_platform, 'device', 'model') === true) {
$device->setModel($resultRaw->operating_platform);
}
if (isset($resultRaw->operating_platform_vendor_name) && $this->isRealResult($resultRaw->operating_platform_vendor_name) === true) {
$device->setBrand($resultRaw->operating_platform_vendor_name);
}
}
public function parse($userAgent, array $headers = [])
{
$resultRaw = $this->getResult($userAgent, $headers);
/*
* No result found?
*/
if ($this->hasResult($resultRaw) !== true) {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
/*
* Hydrate the model
*/
$result = new Model\UserAgent();
$result->setProviderResultRaw($resultRaw);
/*
* hydrate the result
*/
$this->hydrateBrowser($result->getBrowser(), $resultRaw);
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
$this->hydrateDevice($result->getDevice(), $resultRaw);
return $result;
}
}