1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider\Http; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use stdClass; |
7
|
|
|
use UserAgentParser\Exception; |
8
|
|
|
use UserAgentParser\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @see https://useragentapi.com/docs |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class UserAgentApiCom extends AbstractHttpProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Name of the provider |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'UserAgentApiCom'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Homepage of the provider |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $homepage = 'http://useragentapi.com/'; |
30
|
|
|
|
31
|
|
|
protected $detectionCapabilities = [ |
32
|
|
|
|
33
|
|
|
'browser' => [ |
34
|
|
|
'name' => true, |
35
|
|
|
'version' => true, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
'renderingEngine' => [ |
39
|
|
|
'name' => true, |
40
|
|
|
'version' => true, |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
'operatingSystem' => [ |
44
|
|
|
'name' => false, |
45
|
|
|
'version' => false, |
46
|
|
|
], |
47
|
|
|
|
48
|
|
|
'device' => [ |
49
|
|
|
'model' => false, |
50
|
|
|
'brand' => false, |
51
|
|
|
'type' => true, |
52
|
|
|
'isMobile' => false, |
53
|
|
|
'isTouch' => false, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
'bot' => [ |
57
|
|
|
'isBot' => true, |
58
|
|
|
'name' => true, |
59
|
|
|
'type' => false, |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
private static $uri = 'https://useragentapi.com/api/v3/json'; |
64
|
|
|
|
65
|
|
|
private $apiKey; |
66
|
|
|
|
67
|
16 |
|
public function __construct(Client $client, $apiKey) |
68
|
|
|
{ |
69
|
16 |
|
parent::__construct($client); |
70
|
|
|
|
71
|
16 |
|
$this->apiKey = $apiKey; |
72
|
16 |
|
} |
73
|
|
|
|
74
|
1 |
|
public function getVersion() |
75
|
|
|
{ |
76
|
1 |
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param string $userAgent |
82
|
|
|
* @param array $headers |
83
|
|
|
* @return stdClass |
84
|
|
|
* @throws Exception\RequestException |
85
|
|
|
*/ |
86
|
11 |
|
protected function getResult($userAgent, array $headers) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
/* |
89
|
|
|
* an empty UserAgent makes no sense |
90
|
|
|
*/ |
91
|
11 |
|
if ($userAgent == '') { |
92
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
93
|
|
|
} |
94
|
|
|
|
95
|
10 |
|
$parameters = '/' . $this->apiKey; |
96
|
10 |
|
$parameters .= '/' . urlencode($userAgent); |
97
|
|
|
|
98
|
10 |
|
$uri = self::$uri . $parameters; |
99
|
|
|
|
100
|
10 |
|
$request = new Request('GET', $uri); |
101
|
|
|
|
102
|
|
|
try { |
103
|
10 |
|
$response = $this->getResponse($request); |
104
|
3 |
|
} catch (Exception\RequestException $ex) { |
105
|
|
|
/* @var $prevEx \GuzzleHttp\Exception\ClientException */ |
106
|
3 |
|
$prevEx = $ex->getPrevious(); |
107
|
|
|
|
108
|
3 |
|
if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) { |
109
|
2 |
|
$content = $prevEx->getResponse() |
110
|
2 |
|
->getBody() |
111
|
2 |
|
->getContents(); |
112
|
2 |
|
$content = json_decode($content); |
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* Error |
116
|
|
|
*/ |
117
|
2 |
|
if (isset($content->error->code) && $content->error->code == 'key_invalid') { |
118
|
1 |
|
throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if (isset($content->error->code) && $content->error->code == 'useragent_invalid') { |
122
|
1 |
|
throw new Exception\RequestException('User agent is invalid ' . $userAgent); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
throw $ex; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
* no json returned? |
131
|
|
|
*/ |
132
|
7 |
|
$contentType = $response->getHeader('Content-Type'); |
133
|
7 |
|
if (! isset($contentType[0]) || $contentType[0] != 'application/json') { |
134
|
1 |
|
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
135
|
|
|
} |
136
|
|
|
|
137
|
6 |
|
$content = json_decode($response->getBody()->getContents()); |
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
* No result |
141
|
|
|
*/ |
142
|
6 |
|
if (isset($content->error->code) && $content->error->code == 'useragent_not_found') { |
143
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/* |
147
|
|
|
* Missing data? |
148
|
|
|
*/ |
149
|
5 |
|
if (! $content instanceof stdClass || ! isset($content->data)) { |
150
|
1 |
|
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Data is missing "' . $response->getBody()->getContents() . '"'); |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
return $content->data; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
* @param stdClass $resultRaw |
159
|
|
|
* @return boolean |
160
|
|
|
*/ |
161
|
4 |
|
private function isBot(stdClass $resultRaw) |
162
|
|
|
{ |
163
|
4 |
|
if (isset($resultRaw->platform_type) && $resultRaw->platform_type === 'Bot') { |
164
|
1 |
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
3 |
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
* @param Model\Bot $bot |
173
|
|
|
* @param stdClass $resultRaw |
174
|
|
|
*/ |
175
|
1 |
|
private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
176
|
|
|
{ |
177
|
1 |
|
$bot->setIsBot(true); |
178
|
|
|
|
179
|
1 |
|
if (isset($resultRaw->platform_name) && $this->isRealResult($resultRaw->platform_name) === true) { |
180
|
1 |
|
$bot->setName($resultRaw->platform_name); |
181
|
|
|
} |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* |
186
|
|
|
* @param Model\Browser $browser |
187
|
|
|
* @param stdClass $resultRaw |
188
|
|
|
*/ |
189
|
3 |
|
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
190
|
|
|
{ |
191
|
3 |
|
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) { |
192
|
1 |
|
$browser->setName($resultRaw->browser_name); |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
if (isset($resultRaw->browser_version) && $this->isRealResult($resultRaw->browser_version) === true) { |
196
|
1 |
|
$browser->getVersion()->setComplete($resultRaw->browser_version); |
197
|
|
|
} |
198
|
3 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
* @param Model\RenderingEngine $engine |
203
|
|
|
* @param stdClass $resultRaw |
204
|
|
|
*/ |
205
|
3 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
206
|
|
|
{ |
207
|
3 |
|
if (isset($resultRaw->engine_name) && $this->isRealResult($resultRaw->engine_name) === true) { |
208
|
1 |
|
$engine->setName($resultRaw->engine_name); |
209
|
|
|
} |
210
|
|
|
|
211
|
3 |
|
if (isset($resultRaw->engine_version) && $this->isRealResult($resultRaw->engine_version) === true) { |
212
|
1 |
|
$engine->getVersion()->setComplete($resultRaw->engine_version); |
213
|
|
|
} |
214
|
3 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* |
218
|
|
|
* @param Model\UserAgent $device |
219
|
|
|
* @param stdClass $resultRaw |
220
|
|
|
*/ |
221
|
3 |
|
private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
222
|
|
|
{ |
223
|
3 |
|
if (isset($resultRaw->platform_type) && $this->isRealResult($resultRaw->platform_type) === true) { |
224
|
1 |
|
$device->setType($resultRaw->platform_type); |
225
|
|
|
} |
226
|
3 |
|
} |
227
|
|
|
|
228
|
11 |
|
public function parse($userAgent, array $headers = []) |
229
|
|
|
{ |
230
|
11 |
|
$resultRaw = $this->getResult($userAgent, $headers); |
231
|
|
|
|
232
|
|
|
/* |
233
|
|
|
* Hydrate the model |
234
|
|
|
*/ |
235
|
4 |
|
$result = new Model\UserAgent(); |
236
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
237
|
|
|
|
238
|
|
|
/* |
239
|
|
|
* Bot detection |
240
|
|
|
*/ |
241
|
4 |
|
if ($this->isBot($resultRaw) === true) { |
242
|
1 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
243
|
|
|
|
244
|
1 |
|
return $result; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/* |
248
|
|
|
* hydrate the result |
249
|
|
|
*/ |
250
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
251
|
3 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw); |
252
|
3 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
253
|
|
|
|
254
|
3 |
|
return $result; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.