|
1
|
|
|
<?php |
|
2
|
|
|
namespace UserAgentParser\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use DeviceDetector\DeviceDetector; |
|
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
|
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
|
7
|
|
|
use UserAgentParser\Model; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstraction for piwik/device-detector |
|
11
|
|
|
* |
|
12
|
|
|
* @author Martin Keckeis <[email protected]> |
|
13
|
|
|
* @license MIT |
|
14
|
|
|
* @see https://github.com/piwik/device-detector |
|
15
|
|
|
*/ |
|
16
|
|
|
class PiwikDeviceDetector extends AbstractProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Name of the provider |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name = 'PiwikDeviceDetector'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Homepage of the provider |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $homepage = 'https://github.com/piwik/device-detector'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Composer package name |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $packageName = 'piwik/device-detector'; |
|
38
|
|
|
|
|
39
|
|
|
protected $detectionCapabilities = [ |
|
40
|
|
|
|
|
41
|
|
|
'browser' => [ |
|
42
|
|
|
'name' => true, |
|
43
|
|
|
'version' => true, |
|
44
|
|
|
], |
|
45
|
|
|
|
|
46
|
|
|
'renderingEngine' => [ |
|
47
|
|
|
'name' => true, |
|
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' => true, |
|
60
|
|
|
'isMobile' => true, |
|
61
|
|
|
'isTouch' => true, |
|
62
|
|
|
], |
|
63
|
|
|
|
|
64
|
|
|
'bot' => [ |
|
65
|
|
|
'isBot' => true, |
|
66
|
|
|
'name' => true, |
|
67
|
|
|
'type' => true, |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
|
72
|
|
|
|
|
73
|
|
|
'general' => [ |
|
74
|
|
|
'/^UNK$/i', |
|
75
|
|
|
], |
|
76
|
|
|
|
|
77
|
|
|
'bot' => [ |
|
78
|
|
|
'name' => [ |
|
79
|
|
|
'/^Bot$/i', |
|
80
|
|
|
'/^Generic Bot$/i', |
|
81
|
|
|
], |
|
82
|
|
|
], |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @var DeviceDetector |
|
88
|
|
|
*/ |
|
89
|
|
|
private $parser; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @param DeviceDetector $parser |
|
94
|
|
|
* @throws PackageNotLoadedException |
|
95
|
|
|
*/ |
|
96
|
17 |
|
public function __construct(DeviceDetector $parser = null) |
|
97
|
|
|
{ |
|
98
|
17 |
|
if ($parser === null) { |
|
99
|
8 |
|
$this->checkIfInstalled(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
17 |
|
$this->parser = $parser; |
|
103
|
17 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* |
|
107
|
|
|
* @return DeviceDetector |
|
108
|
|
|
*/ |
|
109
|
10 |
|
public function getParser() |
|
110
|
|
|
{ |
|
111
|
10 |
|
if ($this->parser !== null) { |
|
112
|
10 |
|
return $this->parser; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
$this->parser = new DeviceDetector(); |
|
116
|
|
|
|
|
117
|
1 |
|
return $this->parser; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* |
|
122
|
|
|
* @param DeviceDetector $dd |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
7 |
|
private function getResultRaw(DeviceDetector $dd) |
|
127
|
|
|
{ |
|
128
|
|
|
$raw = [ |
|
129
|
7 |
|
'client' => $dd->getClient(), |
|
130
|
7 |
|
'operatingSystem' => $dd->getOs(), |
|
131
|
|
|
|
|
132
|
|
|
'device' => [ |
|
133
|
7 |
|
'brand' => $dd->getBrand(), |
|
134
|
7 |
|
'brandName' => $dd->getBrandName(), |
|
135
|
|
|
|
|
136
|
7 |
|
'model' => $dd->getModel(), |
|
137
|
|
|
|
|
138
|
7 |
|
'device' => $dd->getDevice(), |
|
139
|
7 |
|
'deviceName' => $dd->getDeviceName(), |
|
140
|
|
|
], |
|
141
|
|
|
|
|
142
|
7 |
|
'bot' => $dd->getBot(), |
|
143
|
|
|
|
|
144
|
|
|
'extra' => [ |
|
145
|
7 |
|
'isBot' => $dd->isBot(), |
|
146
|
|
|
|
|
147
|
|
|
// client |
|
148
|
7 |
|
'isBrowser' => $dd->isBrowser(), |
|
149
|
7 |
|
'isFeedReader' => $dd->isFeedReader(), |
|
150
|
7 |
|
'isMobileApp' => $dd->isMobileApp(), |
|
151
|
7 |
|
'isPIM' => $dd->isPIM(), |
|
152
|
7 |
|
'isLibrary' => $dd->isLibrary(), |
|
153
|
7 |
|
'isMediaPlayer' => $dd->isMediaPlayer(), |
|
154
|
|
|
|
|
155
|
|
|
// deviceType |
|
156
|
7 |
|
'isCamera' => $dd->isCamera(), |
|
157
|
7 |
|
'isCarBrowser' => $dd->isCarBrowser(), |
|
158
|
7 |
|
'isConsole' => $dd->isConsole(), |
|
159
|
7 |
|
'isFeaturePhone' => $dd->isFeaturePhone(), |
|
160
|
7 |
|
'isPhablet' => $dd->isPhablet(), |
|
161
|
7 |
|
'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(), |
|
162
|
7 |
|
'isSmartDisplay' => $dd->isSmartDisplay(), |
|
163
|
7 |
|
'isSmartphone' => $dd->isSmartphone(), |
|
164
|
7 |
|
'isTablet' => $dd->isTablet(), |
|
165
|
7 |
|
'isTV' => $dd->isTV(), |
|
166
|
|
|
|
|
167
|
|
|
// other special |
|
168
|
7 |
|
'isDesktop' => $dd->isDesktop(), |
|
169
|
7 |
|
'isMobile' => $dd->isMobile(), |
|
170
|
7 |
|
'isTouchEnabled' => $dd->isTouchEnabled(), |
|
171
|
|
|
], |
|
172
|
|
|
]; |
|
173
|
|
|
|
|
174
|
7 |
|
return $raw; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* |
|
179
|
|
|
* @param DeviceDetector $dd |
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
9 |
|
private function hasResult(DeviceDetector $dd) |
|
184
|
|
|
{ |
|
185
|
9 |
|
if ($dd->isBot() === true) { |
|
186
|
4 |
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
5 |
|
$client = $dd->getClient(); |
|
190
|
5 |
|
if (isset($client['name']) && $this->isRealResult($client['name'])) { |
|
191
|
1 |
|
return true; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
4 |
|
$os = $dd->getOs(); |
|
195
|
4 |
|
if (isset($os['name']) && $this->isRealResult($os['name'])) { |
|
196
|
1 |
|
return true; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
3 |
|
if ($dd->getDevice() !== null) { |
|
200
|
1 |
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
2 |
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* |
|
208
|
|
|
* @param Model\Bot $bot |
|
209
|
|
|
* @param array|boolean $botRaw |
|
210
|
|
|
*/ |
|
211
|
4 |
|
private function hydrateBot(Model\Bot $bot, $botRaw) |
|
212
|
|
|
{ |
|
213
|
4 |
|
$bot->setIsBot(true); |
|
214
|
|
|
|
|
215
|
4 |
|
if (isset($botRaw['name'])) { |
|
216
|
4 |
|
$bot->setName($this->getRealResult($botRaw['name'], 'bot', 'name')); |
|
217
|
|
|
} |
|
218
|
4 |
|
if (isset($botRaw['category'])) { |
|
219
|
2 |
|
$bot->setType($this->getRealResult($botRaw['category'])); |
|
220
|
|
|
} |
|
221
|
4 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* |
|
225
|
|
|
* @param Model\Browser $browser |
|
226
|
|
|
* @param array|string $clientRaw |
|
227
|
|
|
*/ |
|
228
|
3 |
|
private function hydrateBrowser(Model\Browser $browser, $clientRaw) |
|
229
|
|
|
{ |
|
230
|
3 |
|
if (isset($clientRaw['name'])) { |
|
231
|
1 |
|
$browser->setName($this->getRealResult($clientRaw['name'])); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
3 |
|
if (isset($clientRaw['version'])) { |
|
235
|
1 |
|
$browser->getVersion()->setComplete($this->getRealResult($clientRaw['version'])); |
|
236
|
|
|
} |
|
237
|
3 |
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* |
|
241
|
|
|
* @param Model\RenderingEngine $engine |
|
242
|
|
|
* @param array|string $clientRaw |
|
243
|
|
|
*/ |
|
244
|
3 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, $clientRaw) |
|
245
|
|
|
{ |
|
246
|
3 |
|
if (isset($clientRaw['engine'])) { |
|
247
|
2 |
|
$engine->setName($this->getRealResult($clientRaw['engine'])); |
|
248
|
|
|
} |
|
249
|
3 |
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* |
|
253
|
|
|
* @param Model\OperatingSystem $os |
|
254
|
|
|
* @param array|string $osRaw |
|
255
|
|
|
*/ |
|
256
|
3 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, $osRaw) |
|
257
|
|
|
{ |
|
258
|
3 |
|
if (isset($osRaw['name'])) { |
|
259
|
1 |
|
$os->setName($this->getRealResult($osRaw['name'])); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
3 |
|
if (isset($osRaw['version'])) { |
|
263
|
1 |
|
$os->getVersion()->setComplete($this->getRealResult($osRaw['version'])); |
|
264
|
|
|
} |
|
265
|
3 |
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* |
|
269
|
|
|
* @param Model\UserAgent $device |
|
270
|
|
|
* @param DeviceDetector $dd |
|
271
|
|
|
*/ |
|
272
|
3 |
|
private function hydrateDevice(Model\Device $device, DeviceDetector $dd) |
|
273
|
|
|
{ |
|
274
|
3 |
|
$device->setModel($this->getRealResult($dd->getModel())); |
|
275
|
3 |
|
$device->setBrand($this->getRealResult($dd->getBrandName())); |
|
276
|
3 |
|
$device->setType($this->getRealResult($dd->getDeviceName())); |
|
277
|
|
|
|
|
278
|
3 |
|
if ($dd->isMobile() === true) { |
|
279
|
1 |
|
$device->setIsMobile(true); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
3 |
|
if ($dd->isTouchEnabled() === true) { |
|
283
|
1 |
|
$device->setIsTouch(true); |
|
284
|
|
|
} |
|
285
|
3 |
|
} |
|
286
|
|
|
|
|
287
|
9 |
|
public function parse($userAgent, array $headers = []) |
|
288
|
|
|
{ |
|
289
|
9 |
|
$dd = $this->getParser(); |
|
290
|
|
|
|
|
291
|
9 |
|
$dd->setUserAgent($userAgent); |
|
292
|
9 |
|
$dd->parse(); |
|
293
|
|
|
|
|
294
|
|
|
/* |
|
295
|
|
|
* No result found? |
|
296
|
|
|
*/ |
|
297
|
9 |
|
if ($this->hasResult($dd) !== true) { |
|
298
|
2 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/* |
|
302
|
|
|
* Hydrate the model |
|
303
|
|
|
*/ |
|
304
|
7 |
|
$result = new Model\UserAgent($this->getName(), $this->getVersion()); |
|
305
|
7 |
|
$result->setProviderResultRaw($this->getResultRaw($dd)); |
|
306
|
|
|
|
|
307
|
|
|
/* |
|
308
|
|
|
* Bot detection |
|
309
|
|
|
*/ |
|
310
|
7 |
|
if ($dd->isBot() === true) { |
|
311
|
4 |
|
$this->hydrateBot($result->getBot(), $dd->getBot()); |
|
312
|
|
|
|
|
313
|
4 |
|
return $result; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/* |
|
317
|
|
|
* hydrate the result |
|
318
|
|
|
*/ |
|
319
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $dd->getClient()); |
|
320
|
3 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $dd->getClient()); |
|
321
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $dd->getOs()); |
|
322
|
3 |
|
$this->hydrateDevice($result->getDevice(), $dd); |
|
323
|
|
|
|
|
324
|
3 |
|
return $result; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|