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
|
|
|
class NeutrinoApiCom extends AbstractHttpProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Name of the provider |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'NeutrinoApiCom'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Homepage of the provider |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $homepage = 'https://www.neutrinoapi.com/'; |
25
|
|
|
|
26
|
|
|
protected $detectionCapabilities = [ |
27
|
|
|
|
28
|
|
|
'browser' => [ |
29
|
|
|
'name' => true, |
30
|
|
|
'version' => true, |
31
|
|
|
], |
32
|
|
|
|
33
|
|
|
'renderingEngine' => [ |
34
|
|
|
'name' => false, |
35
|
|
|
'version' => false, |
36
|
|
|
], |
37
|
|
|
|
38
|
|
|
'operatingSystem' => [ |
39
|
|
|
'name' => true, |
40
|
|
|
'version' => true, |
41
|
|
|
], |
42
|
|
|
|
43
|
|
|
'device' => [ |
44
|
|
|
'model' => true, |
45
|
|
|
'brand' => true, |
46
|
|
|
'type' => true, |
47
|
|
|
'isMobile' => true, |
48
|
|
|
'isTouch' => false, |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
'bot' => [ |
52
|
|
|
'isBot' => true, |
53
|
|
|
'name' => true, |
54
|
|
|
'type' => false, |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
protected $defaultValues = [ |
59
|
|
|
'unknown', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
private static $uri = 'https://neutrinoapi.com/user-agent-info'; |
63
|
|
|
|
64
|
|
|
private $apiUserId; |
65
|
|
|
|
66
|
|
|
private $apiKey; |
67
|
|
|
|
68
|
18 |
|
public function __construct(Client $client, $apiUserId, $apiKey) |
69
|
|
|
{ |
70
|
18 |
|
parent::__construct($client); |
71
|
|
|
|
72
|
18 |
|
$this->apiUserId = $apiUserId; |
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
|
13 |
|
protected function getResult($userAgent, array $headers) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
/* |
91
|
|
|
* an empty UserAgent makes no sense |
92
|
|
|
*/ |
93
|
13 |
|
if ($userAgent == '') { |
94
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$params = [ |
98
|
12 |
|
'user-id' => $this->apiUserId, |
99
|
12 |
|
'api-key' => $this->apiKey, |
100
|
12 |
|
'output-format' => 'json', |
101
|
12 |
|
'output-case' => 'snake', |
102
|
|
|
|
103
|
12 |
|
'user-agent' => $userAgent, |
104
|
12 |
|
]; |
105
|
|
|
|
106
|
12 |
|
$body = http_build_query($params, null, '&'); |
107
|
|
|
|
108
|
12 |
|
$request = new Request('POST', self::$uri, [ |
109
|
12 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
110
|
12 |
|
], $body); |
111
|
|
|
|
112
|
|
|
try { |
113
|
12 |
|
$response = $this->getResponse($request); |
114
|
12 |
|
} catch (Exception\RequestException $ex) { |
115
|
|
|
/* @var $prevEx \GuzzleHttp\Exception\ClientException */ |
116
|
2 |
|
$prevEx = $ex->getPrevious(); |
117
|
|
|
|
118
|
2 |
View Code Duplication |
if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) { |
|
|
|
|
119
|
1 |
|
throw new Exception\InvalidCredentialsException('Your API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
throw $ex; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* no json returned? |
127
|
|
|
*/ |
128
|
10 |
|
$contentType = $response->getHeader('Content-Type'); |
129
|
10 |
View Code Duplication |
if (! isset($contentType[0]) || $contentType[0] != 'application/json;charset=UTF-8') { |
|
|
|
|
130
|
1 |
|
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
131
|
|
|
} |
132
|
|
|
|
133
|
9 |
|
$content = json_decode($response->getBody()->getContents()); |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* errors |
137
|
|
|
*/ |
138
|
9 |
|
if (isset($content->api_error)) { |
139
|
3 |
|
switch ($content->api_error) { |
140
|
|
|
|
141
|
3 |
View Code Duplication |
case 1: |
|
|
|
|
142
|
1 |
|
throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"'); |
143
|
|
|
break; |
|
|
|
|
144
|
|
|
|
145
|
2 |
|
case 2: |
146
|
1 |
|
throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" for ' . $this->getName()); |
147
|
|
|
break; |
|
|
|
|
148
|
|
|
|
149
|
1 |
View Code Duplication |
default: |
|
|
|
|
150
|
1 |
|
throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"'); |
151
|
|
|
break; |
|
|
|
|
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* Missing data? |
157
|
|
|
*/ |
158
|
6 |
|
if (! $content instanceof stdClass) { |
159
|
1 |
|
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
160
|
|
|
} |
161
|
|
|
|
162
|
5 |
|
return $content; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* |
167
|
|
|
* @param stdClass $resultRaw |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
5 |
View Code Duplication |
private function hasResult(stdClass $resultRaw) |
|
|
|
|
172
|
|
|
{ |
173
|
5 |
|
if (isset($resultRaw->type) && $this->isRealResult($resultRaw->type)) { |
174
|
4 |
|
return true; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* |
182
|
|
|
* @param stdClass $resultRaw |
183
|
|
|
* @return boolean |
184
|
|
|
*/ |
185
|
4 |
View Code Duplication |
private function isBot(stdClass $resultRaw) |
|
|
|
|
186
|
|
|
{ |
187
|
4 |
|
if (isset($resultRaw->type) && $resultRaw->type === 'robot') { |
188
|
1 |
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* |
196
|
|
|
* @param Model\Bot $bot |
197
|
|
|
* @param stdClass $resultRaw |
198
|
|
|
*/ |
199
|
1 |
View Code Duplication |
private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
|
|
|
|
200
|
|
|
{ |
201
|
1 |
|
$bot->setIsBot(true); |
202
|
|
|
|
203
|
1 |
|
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) { |
204
|
1 |
|
$bot->setName($resultRaw->browser_name); |
205
|
1 |
|
} |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* |
210
|
|
|
* @param Model\Browser $browser |
211
|
|
|
* @param stdClass $resultRaw |
212
|
|
|
*/ |
213
|
3 |
|
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
214
|
|
|
{ |
215
|
3 |
|
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) { |
216
|
1 |
|
$browser->setName($resultRaw->browser_name); |
217
|
1 |
|
} |
218
|
|
|
|
219
|
3 |
|
if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) { |
220
|
1 |
|
$browser->getVersion()->setComplete($resultRaw->version); |
221
|
1 |
|
} |
222
|
3 |
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* |
226
|
|
|
* @param Model\OperatingSystem $os |
227
|
|
|
* @param stdClass $resultRaw |
228
|
|
|
*/ |
229
|
3 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
230
|
|
|
{ |
231
|
3 |
|
if (isset($resultRaw->operating_system_family) && $this->isRealResult($resultRaw->operating_system_family) === true) { |
232
|
1 |
|
$os->setName($resultRaw->operating_system_family); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
3 |
|
if (isset($resultRaw->operating_system_version) && $this->isRealResult($resultRaw->operating_system_version) === true) { |
236
|
1 |
|
$os->getVersion()->setComplete($resultRaw->operating_system_version); |
237
|
1 |
|
} |
238
|
3 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* |
242
|
|
|
* @param Model\Device $device |
243
|
|
|
* @param stdClass $resultRaw |
244
|
|
|
*/ |
245
|
3 |
|
private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
246
|
|
|
{ |
247
|
3 |
|
if (isset($resultRaw->mobile_model) && $this->isRealResult($resultRaw->mobile_model) === true) { |
248
|
1 |
|
$device->setModel($resultRaw->mobile_model); |
249
|
1 |
|
} |
250
|
|
|
|
251
|
3 |
View Code Duplication |
if (isset($resultRaw->mobile_brand) && $this->isRealResult($resultRaw->mobile_brand) === true) { |
|
|
|
|
252
|
1 |
|
$device->setBrand($resultRaw->mobile_brand); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
3 |
View Code Duplication |
if (isset($resultRaw->type) && $this->isRealResult($resultRaw->type) === true) { |
|
|
|
|
256
|
3 |
|
$device->setType($resultRaw->type); |
257
|
3 |
|
} |
258
|
|
|
|
259
|
3 |
|
if (isset($resultRaw->is_mobile) && $resultRaw->is_mobile === true) { |
260
|
1 |
|
$device->setIsMobile(true); |
261
|
1 |
|
} |
262
|
3 |
|
} |
263
|
|
|
|
264
|
13 |
View Code Duplication |
public function parse($userAgent, array $headers = []) |
|
|
|
|
265
|
|
|
{ |
266
|
13 |
|
$resultRaw = $this->getResult($userAgent, $headers); |
267
|
|
|
|
268
|
|
|
/* |
269
|
|
|
* No result found? |
270
|
|
|
*/ |
271
|
5 |
|
if ($this->hasResult($resultRaw) !== true) { |
272
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/* |
276
|
|
|
* Hydrate the model |
277
|
|
|
*/ |
278
|
4 |
|
$result = new Model\UserAgent(); |
279
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
280
|
|
|
|
281
|
|
|
/* |
282
|
|
|
* Bot detection |
283
|
|
|
*/ |
284
|
4 |
|
if ($this->isBot($resultRaw) === true) { |
285
|
1 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
286
|
|
|
|
287
|
1 |
|
return $result; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/* |
291
|
|
|
* hydrate the result |
292
|
|
|
*/ |
293
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
294
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); |
295
|
3 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
296
|
|
|
|
297
|
3 |
|
return $result; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.