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