1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UAParser\Parser; |
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction for ua-parser/uap-php |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see https://github.com/ua-parser/uap-php |
15
|
|
|
*/ |
16
|
|
|
class UAParser extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'UAParser'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'https://github.com/ua-parser/uap-php'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Composer package name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName = 'ua-parser/uap-php'; |
38
|
|
|
|
39
|
|
|
protected $detectionCapabilities = [ |
40
|
|
|
|
41
|
|
|
'browser' => [ |
42
|
|
|
'name' => true, |
43
|
|
|
'version' => true, |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
'renderingEngine' => [ |
47
|
|
|
'name' => false, |
48
|
|
|
'version' => false, |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
'operatingSystem' => [ |
52
|
|
|
'name' => true, |
53
|
|
|
'version' => true, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
'device' => [ |
57
|
|
|
'model' => true, |
58
|
|
|
'brand' => true, |
59
|
|
|
'type' => false, |
60
|
|
|
'isMobile' => false, |
61
|
|
|
'isTouch' => false, |
62
|
|
|
], |
63
|
|
|
|
64
|
|
|
'bot' => [ |
65
|
|
|
'isBot' => true, |
66
|
|
|
'name' => true, |
67
|
|
|
'type' => false, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
72
|
|
|
|
73
|
|
|
'general' => [ |
74
|
|
|
'/^Other$/i', |
75
|
|
|
|
76
|
|
|
], |
77
|
|
|
|
78
|
|
|
'device' => [ |
79
|
|
|
|
80
|
|
|
'brand' => [ |
81
|
|
|
'/^Generic/i', |
82
|
|
|
'/^unknown$/i', |
83
|
|
|
], |
84
|
|
|
|
85
|
|
|
'model' => [ |
86
|
|
|
'/^generic$/i', |
87
|
|
|
'/^Smartphone$/i', |
88
|
|
|
'/^Feature Phone$/i', |
89
|
|
|
'/^iOS-Device$/i', |
90
|
|
|
'/^Tablet$/i', |
91
|
|
|
'/^Touch$/i', |
92
|
|
|
'/^Windows$/i', |
93
|
|
|
'/^Windows Phone$/i', |
94
|
|
|
'/^Android$/i', |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
|
98
|
|
|
'bot' => [ |
99
|
|
|
'name' => [ |
100
|
|
|
'/^Other$/i', |
101
|
|
|
'/^crawler$/i', |
102
|
|
|
'/^robot$/i', |
103
|
|
|
'/^crawl$/i', |
104
|
|
|
'/^Spider$/i', |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
private $parser; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @param Parser $parser |
114
|
|
|
* @throws PackageNotLoadedException |
115
|
|
|
*/ |
116
|
18 |
|
public function __construct(Parser $parser = null) |
117
|
|
|
{ |
118
|
18 |
|
if ($parser === null && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
119
|
1 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
120
|
|
|
} |
121
|
|
|
|
122
|
17 |
|
$this->parser = $parser; |
123
|
17 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @return Parser |
128
|
|
|
*/ |
129
|
10 |
|
public function getParser() |
130
|
|
|
{ |
131
|
10 |
|
if ($this->parser !== null) { |
132
|
10 |
|
return $this->parser; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
$this->parser = Parser::create(); |
136
|
|
|
|
137
|
1 |
|
return $this->parser; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @param \UAParser\Result\Client $resultRaw |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
9 |
|
private function hasResult(\UAParser\Result\Client $resultRaw) |
147
|
|
|
{ |
148
|
9 |
|
if ($this->isBot($resultRaw) === true) { |
149
|
2 |
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
if ($this->isRealResult($resultRaw->ua->family)) { |
153
|
1 |
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
6 |
|
if ($this->isRealResult($resultRaw->os->family)) { |
157
|
2 |
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) { |
161
|
1 |
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* @param \UAParser\Result\Client $resultRaw |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
9 |
|
private function isBot(\UAParser\Result\Client $resultRaw) |
174
|
|
|
{ |
175
|
9 |
|
if ($resultRaw->device->family === 'Spider') { |
176
|
2 |
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
7 |
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* |
184
|
|
|
* @param Model\Bot $bot |
185
|
|
|
* @param \UAParser\Result\Client $resultRaw |
186
|
|
|
*/ |
187
|
2 |
|
private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw) |
188
|
|
|
{ |
189
|
2 |
|
$bot->setIsBot(true); |
190
|
2 |
|
$bot->setName($this->getRealResult($resultRaw->ua->family, 'bot', 'name')); |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* |
195
|
|
|
* @param Model\Browser $browser |
196
|
|
|
* @param \UAParser\Result\UserAgent $uaRaw |
197
|
|
|
*/ |
198
|
4 |
|
private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw) |
199
|
|
|
{ |
200
|
4 |
|
$browser->setName($this->getRealResult($uaRaw->family)); |
201
|
|
|
|
202
|
4 |
|
$browser->getVersion()->setMajor($this->getRealResult($uaRaw->major)); |
203
|
4 |
|
$browser->getVersion()->setMinor($this->getRealResult($uaRaw->minor)); |
204
|
4 |
|
$browser->getVersion()->setPatch($this->getRealResult($uaRaw->patch)); |
205
|
4 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* |
209
|
|
|
* @param Model\OperatingSystem $os |
210
|
|
|
* @param \UAParser\Result\OperatingSystem $osRaw |
211
|
|
|
*/ |
212
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw) |
213
|
|
|
{ |
214
|
4 |
|
$os->setName($this->getRealResult($osRaw->family)); |
215
|
|
|
|
216
|
4 |
|
$os->getVersion()->setMajor($this->getRealResult($osRaw->major)); |
217
|
4 |
|
$os->getVersion()->setMinor($this->getRealResult($osRaw->minor)); |
218
|
4 |
|
$os->getVersion()->setPatch($this->getRealResult($osRaw->patch)); |
219
|
4 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* |
223
|
|
|
* @param Model\UserAgent $device |
224
|
|
|
* @param \UAParser\Result\Device $deviceRaw |
225
|
|
|
*/ |
226
|
4 |
|
private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw) |
227
|
|
|
{ |
228
|
4 |
|
$device->setModel($this->getRealResult($deviceRaw->model, 'device', 'model')); |
229
|
4 |
|
$device->setBrand($this->getRealResult($deviceRaw->brand, 'device', 'brand')); |
230
|
4 |
|
} |
231
|
|
|
|
232
|
9 |
|
public function parse($userAgent, array $headers = []) |
233
|
|
|
{ |
234
|
9 |
|
$parser = $this->getParser(); |
235
|
|
|
|
236
|
|
|
/* @var $resultRaw \UAParser\Result\Client */ |
237
|
9 |
|
$resultRaw = $parser->parse($userAgent); |
238
|
|
|
|
239
|
|
|
/* |
240
|
|
|
* No result found? |
241
|
|
|
*/ |
242
|
9 |
|
if ($this->hasResult($resultRaw) !== true) { |
243
|
3 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/* |
247
|
|
|
* Hydrate the model |
248
|
|
|
*/ |
249
|
6 |
|
$result = new Model\UserAgent(); |
250
|
6 |
|
$result->setProviderResultRaw($resultRaw); |
251
|
|
|
|
252
|
|
|
/* |
253
|
|
|
* Bot detection |
254
|
|
|
*/ |
255
|
6 |
|
if ($this->isBot($resultRaw) === true) { |
256
|
2 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
257
|
|
|
|
258
|
2 |
|
return $result; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/* |
262
|
|
|
* hydrate the result |
263
|
|
|
*/ |
264
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw->ua); |
265
|
|
|
// renderingEngine not available |
266
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os); |
267
|
4 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw->device); |
268
|
|
|
|
269
|
4 |
|
return $result; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|