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 && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
99
|
1 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
100
|
|
|
} |
101
|
|
|
|
102
|
16 |
|
$this->parser = $parser; |
103
|
16 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* @return DeviceDetector |
108
|
|
|
*/ |
109
|
9 |
|
public function getParser() |
110
|
|
|
{ |
111
|
9 |
|
if ($this->parser !== null) { |
112
|
9 |
|
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
|
6 |
|
private function getResultRaw(DeviceDetector $dd) |
127
|
|
|
{ |
128
|
|
|
$raw = [ |
129
|
6 |
|
'client' => $dd->getClient(), |
130
|
6 |
|
'operatingSystem' => $dd->getOs(), |
131
|
|
|
|
132
|
|
|
'device' => [ |
133
|
6 |
|
'brand' => $dd->getBrand(), |
134
|
6 |
|
'brandName' => $dd->getBrandName(), |
135
|
|
|
|
136
|
6 |
|
'model' => $dd->getModel(), |
137
|
|
|
|
138
|
6 |
|
'device' => $dd->getDevice(), |
139
|
6 |
|
'deviceName' => $dd->getDeviceName(), |
140
|
|
|
], |
141
|
|
|
|
142
|
6 |
|
'bot' => $dd->getBot(), |
143
|
|
|
|
144
|
|
|
'extra' => [ |
145
|
6 |
|
'isBot' => $dd->isBot(), |
146
|
|
|
|
147
|
|
|
// client |
148
|
6 |
|
'isBrowser' => $dd->isBrowser(), |
149
|
6 |
|
'isFeedReader' => $dd->isFeedReader(), |
150
|
6 |
|
'isMobileApp' => $dd->isMobileApp(), |
151
|
6 |
|
'isPIM' => $dd->isPIM(), |
152
|
6 |
|
'isLibrary' => $dd->isLibrary(), |
153
|
6 |
|
'isMediaPlayer' => $dd->isMediaPlayer(), |
154
|
|
|
|
155
|
|
|
// deviceType |
156
|
6 |
|
'isCamera' => $dd->isCamera(), |
157
|
6 |
|
'isCarBrowser' => $dd->isCarBrowser(), |
158
|
6 |
|
'isConsole' => $dd->isConsole(), |
159
|
6 |
|
'isFeaturePhone' => $dd->isFeaturePhone(), |
160
|
6 |
|
'isPhablet' => $dd->isPhablet(), |
161
|
6 |
|
'isPortableMediaPlayer' => $dd->isPortableMediaPlayer(), |
162
|
6 |
|
'isSmartDisplay' => $dd->isSmartDisplay(), |
163
|
6 |
|
'isSmartphone' => $dd->isSmartphone(), |
164
|
6 |
|
'isTablet' => $dd->isTablet(), |
165
|
6 |
|
'isTV' => $dd->isTV(), |
166
|
|
|
|
167
|
|
|
// other special |
168
|
6 |
|
'isDesktop' => $dd->isDesktop(), |
169
|
6 |
|
'isMobile' => $dd->isMobile(), |
170
|
6 |
|
'isTouchEnabled' => $dd->isTouchEnabled(), |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
|
174
|
6 |
|
return $raw; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* |
179
|
|
|
* @param DeviceDetector $dd |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
8 |
|
private function hasResult(DeviceDetector $dd) |
184
|
|
|
{ |
185
|
8 |
|
if ($dd->isBot() === true) { |
186
|
3 |
|
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
|
3 |
|
private function hydrateBot(Model\Bot $bot, $botRaw) |
212
|
|
|
{ |
213
|
3 |
|
$bot->setIsBot(true); |
214
|
|
|
|
215
|
3 |
|
if (isset($botRaw['name'])) { |
216
|
3 |
|
$bot->setName($this->getRealResult($botRaw['name'], 'bot', 'name')); |
217
|
|
|
} |
218
|
3 |
|
if (isset($botRaw['category'])) { |
219
|
1 |
|
$bot->setType($this->getRealResult($botRaw['category'])); |
220
|
|
|
} |
221
|
3 |
|
} |
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
|
8 |
|
public function parse($userAgent, array $headers = []) |
288
|
|
|
{ |
289
|
8 |
|
$dd = $this->getParser(); |
290
|
|
|
|
291
|
8 |
|
$dd->setUserAgent($userAgent); |
292
|
8 |
|
$dd->parse(); |
293
|
|
|
|
294
|
|
|
/* |
295
|
|
|
* No result found? |
296
|
|
|
*/ |
297
|
8 |
|
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
|
6 |
|
$result = new Model\UserAgent(); |
305
|
6 |
|
$result->setProviderResultRaw($this->getResultRaw($dd)); |
306
|
|
|
|
307
|
|
|
/* |
308
|
|
|
* Bot detection |
309
|
|
|
*/ |
310
|
6 |
|
if ($dd->isBot() === true) { |
311
|
3 |
|
$this->hydrateBot($result->getBot(), $dd->getBot()); |
312
|
|
|
|
313
|
3 |
|
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
|
|
|
|