1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider\Http; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7\Request; |
5
|
|
|
use stdClass; |
6
|
|
|
use UserAgentParser\Exception; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction of useragentstring.com |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see http://www.useragentstring.com/pages/api.php |
15
|
|
|
*/ |
16
|
|
|
class UserAgentStringCom extends AbstractHttpProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'UserAgentStringCom'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'http://www.useragentstring.com/'; |
31
|
|
|
|
32
|
|
|
protected $detectionCapabilities = [ |
33
|
|
|
|
34
|
|
|
'browser' => [ |
35
|
|
|
'name' => true, |
36
|
|
|
'version' => true, |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
'renderingEngine' => [ |
40
|
|
|
'name' => false, |
41
|
|
|
'version' => false, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
'operatingSystem' => [ |
45
|
|
|
'name' => true, |
46
|
|
|
'version' => true, |
47
|
|
|
], |
48
|
|
|
|
49
|
|
|
'device' => [ |
50
|
|
|
'model' => false, |
51
|
|
|
'brand' => false, |
52
|
|
|
'type' => false, |
53
|
|
|
'isMobile' => false, |
54
|
|
|
'isTouch' => false, |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
'bot' => [ |
58
|
|
|
'isBot' => true, |
59
|
|
|
'name' => true, |
60
|
|
|
'type' => true, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
protected $defaultValues = [ |
65
|
|
|
'general' => [ |
66
|
|
|
'/^unknown$/i', |
67
|
|
|
'/^--$/i', |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
private $botTypes = [ |
72
|
|
|
'Crawler', |
73
|
|
|
'Cloud Platform', |
74
|
|
|
'Feed Reader', |
75
|
|
|
'LinkChecker', |
76
|
|
|
'Validator', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
private static $uri = 'http://www.useragentstring.com/'; |
80
|
|
|
|
81
|
1 |
|
public function getVersion() |
82
|
|
|
{ |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
* @param string $userAgent |
89
|
|
|
* @param array $headers |
90
|
|
|
* @return stdClass |
91
|
|
|
* @throws Exception\RequestException |
92
|
|
|
*/ |
93
|
9 |
|
protected function getResult($userAgent, array $headers) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
/* |
96
|
|
|
* an empty UserAgent makes no sense |
97
|
|
|
*/ |
98
|
9 |
|
if ($userAgent == '') { |
99
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
100
|
|
|
} |
101
|
|
|
|
102
|
8 |
|
$parameters = 'uas=' . rawurlencode($userAgent); |
103
|
8 |
|
$parameters .= '&getJSON=all'; |
104
|
|
|
|
105
|
8 |
|
$uri = self::$uri . '?' . $parameters; |
106
|
|
|
|
107
|
8 |
|
$request = new Request('GET', $uri); |
108
|
|
|
|
109
|
8 |
|
$response = $this->getResponse($request); |
110
|
|
|
|
111
|
|
|
/* |
112
|
|
|
* no json returned? |
113
|
|
|
*/ |
114
|
8 |
|
$contentType = $response->getHeader('Content-Type'); |
115
|
8 |
|
if (! isset($contentType[0]) || $contentType[0] != 'application/json') { |
116
|
1 |
|
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
$content = json_decode($response->getBody()->getContents()); |
120
|
|
|
|
121
|
7 |
|
if (! $content instanceof stdClass) { |
122
|
1 |
|
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"'); |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
return $content; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* @param stdClass $resultRaw |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
6 |
|
private function hasResult(stdClass $resultRaw) |
135
|
|
|
{ |
136
|
6 |
|
if (isset($resultRaw->agent_type) && $this->isRealResult($resultRaw->agent_type)) { |
137
|
4 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
* @param stdClass $resultRaw |
146
|
|
|
* @return boolean |
147
|
|
|
*/ |
148
|
4 |
|
private function isBot(stdClass $resultRaw) |
149
|
|
|
{ |
150
|
4 |
|
if (isset($resultRaw->agent_type) && in_array($resultRaw->agent_type, $this->botTypes)) { |
151
|
1 |
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* |
159
|
|
|
* @param Model\Bot $bot |
160
|
|
|
* @param stdClass $resultRaw |
161
|
|
|
*/ |
162
|
1 |
|
private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
163
|
|
|
{ |
164
|
1 |
|
$bot->setIsBot(true); |
165
|
|
|
|
166
|
1 |
|
if (isset($resultRaw->agent_name)) { |
167
|
1 |
|
$bot->setName($this->getRealResult($resultRaw->agent_name)); |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
if (isset($resultRaw->agent_type)) { |
171
|
1 |
|
$bot->setType($this->getRealResult($resultRaw->agent_type)); |
172
|
|
|
} |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* |
177
|
|
|
* @param Model\Browser $browser |
178
|
|
|
* @param stdClass $resultRaw |
179
|
|
|
*/ |
180
|
3 |
|
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
181
|
|
|
{ |
182
|
3 |
|
if (isset($resultRaw->agent_name)) { |
183
|
1 |
|
$browser->setName($this->getRealResult($resultRaw->agent_name)); |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
if (isset($resultRaw->agent_version)) { |
187
|
2 |
|
$browser->getVersion()->setComplete($this->getRealResult($resultRaw->agent_version)); |
188
|
|
|
} |
189
|
3 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* |
193
|
|
|
* @param Model\OperatingSystem $os |
194
|
|
|
* @param stdClass $resultRaw |
195
|
|
|
*/ |
196
|
3 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
197
|
|
|
{ |
198
|
3 |
|
if (isset($resultRaw->os_name)) { |
199
|
1 |
|
$os->setName($this->getRealResult($resultRaw->os_name)); |
200
|
|
|
} |
201
|
|
|
|
202
|
3 |
|
if (isset($resultRaw->os_versionNumber)) { |
203
|
2 |
|
$os->getVersion()->setComplete($this->getRealResult($resultRaw->os_versionNumber)); |
204
|
|
|
} |
205
|
3 |
|
} |
206
|
|
|
|
207
|
9 |
|
public function parse($userAgent, array $headers = []) |
208
|
|
|
{ |
209
|
9 |
|
$resultRaw = $this->getResult($userAgent, $headers); |
210
|
|
|
|
211
|
|
|
/* |
212
|
|
|
* No result found? |
213
|
|
|
*/ |
214
|
6 |
|
if ($this->hasResult($resultRaw) !== true) { |
215
|
2 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/* |
219
|
|
|
* Hydrate the model |
220
|
|
|
*/ |
221
|
4 |
|
$result = new Model\UserAgent(); |
222
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
223
|
|
|
|
224
|
|
|
/* |
225
|
|
|
* Bot detection |
226
|
|
|
*/ |
227
|
4 |
|
if ($this->isBot($resultRaw) === true) { |
228
|
1 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
229
|
|
|
|
230
|
1 |
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* hydrate the result |
235
|
|
|
*/ |
236
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
237
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); |
238
|
|
|
|
239
|
3 |
|
return $result; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.