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