1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
5
|
|
|
use UserAgentParser\Model; |
6
|
|
|
use Wurfl\CustomDevice; |
7
|
|
|
use Wurfl\Manager as WurflManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction for mimmi20/wurfl |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see https://github.com/mimmi20/Wurfl |
15
|
|
|
*/ |
16
|
|
|
class Wurfl extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'Wurfl'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'https://github.com/mimmi20/Wurfl'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Composer package name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName = 'mimmi20/wurfl'; |
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' => true, |
60
|
|
|
'isMobile' => true, |
61
|
|
|
'isTouch' => true, |
62
|
|
|
], |
63
|
|
|
|
64
|
|
|
'bot' => [ |
65
|
|
|
'isBot' => true, |
66
|
|
|
'name' => false, |
67
|
|
|
'type' => false, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
72
|
|
|
|
73
|
|
|
'general' => [], |
74
|
|
|
|
75
|
|
|
'operatingSystem' => [ |
76
|
|
|
'name' => [ |
77
|
|
|
'/^Unknown$/i', |
78
|
|
|
'/^en$/i', |
79
|
|
|
'/^en_US$/i', |
80
|
|
|
'/^Desktop$/i', |
81
|
|
|
'/^Mobile$/i', |
82
|
|
|
'/^Randomized by FreeSafeIP.com$/i', |
83
|
|
|
], |
84
|
|
|
], |
85
|
|
|
|
86
|
|
|
'device' => [ |
87
|
|
|
|
88
|
|
|
'brand' => [ |
89
|
|
|
'/^Generic$/i', |
90
|
|
|
], |
91
|
|
|
|
92
|
|
|
'model' => [ |
93
|
|
|
'/^Android/i', |
94
|
|
|
'/^Windows Phone/i', |
95
|
|
|
'/^Windows Mobile/i', |
96
|
|
|
'/^Firefox/i', |
97
|
|
|
'/^unrecognized/i', |
98
|
|
|
'/^Generic/i', |
99
|
|
|
'/^Disguised as Macintosh$/i', |
100
|
|
|
'/^Windows RT/i', |
101
|
|
|
'/^Tablet on Android$/i', |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
* @var WurflManager |
109
|
|
|
*/ |
110
|
|
|
private $parser; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* @param WurflManager $parser |
115
|
|
|
*/ |
116
|
18 |
|
public function __construct(WurflManager $parser) |
117
|
|
|
{ |
118
|
18 |
|
$this->parser = $parser; |
119
|
18 |
|
} |
120
|
|
|
|
121
|
3 |
|
public function getVersion() |
122
|
|
|
{ |
123
|
3 |
|
$version = $this->getParser()->getWurflInfo()->version; |
124
|
|
|
|
125
|
3 |
|
preg_match("/\d+(?:\.\d+)+/", $version, $result); |
126
|
|
|
|
127
|
3 |
|
if (isset($result[0])) { |
128
|
2 |
|
return $result[0]; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
2 |
|
public function getUpdateDate() |
135
|
|
|
{ |
136
|
|
|
// 2015-10-16 11:09:44 -0400 |
|
|
|
|
137
|
2 |
|
$lastUpdated = $this->getParser()->getWurflInfo()->lastUpdated; |
138
|
|
|
|
139
|
2 |
|
if ($lastUpdated == '') { |
140
|
1 |
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
$date = \DateTime::createFromFormat('Y-m-d H:i:s O', $lastUpdated); |
144
|
1 |
|
$date->setTimezone(new \DateTimeZone('UTC')); |
145
|
|
|
|
146
|
1 |
|
return $date; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* |
151
|
|
|
* @return WurflManager |
152
|
|
|
*/ |
153
|
13 |
|
public function getParser() |
154
|
|
|
{ |
155
|
13 |
|
return $this->parser; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
* @param CustomDevice $device |
161
|
|
|
* @return boolean |
162
|
|
|
*/ |
163
|
7 |
|
private function hasResult(CustomDevice $device) |
164
|
|
|
{ |
165
|
7 |
|
if ($device->id !== null && $device->id != '' && $device->id !== 'generic') { |
166
|
6 |
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* |
174
|
|
|
* @param Model\Browser $browser |
175
|
|
|
* @param CustomDevice $deviceRaw |
176
|
|
|
*/ |
177
|
5 |
|
private function hydrateBrowser(Model\Browser $browser, CustomDevice $deviceRaw) |
178
|
|
|
{ |
179
|
5 |
|
$browser->setName($deviceRaw->getVirtualCapability('advertised_browser')); |
180
|
5 |
|
$browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version')); |
181
|
5 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* |
185
|
|
|
* @param Model\OperatingSystem $os |
186
|
|
|
* @param CustomDevice $deviceRaw |
187
|
|
|
*/ |
188
|
5 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, CustomDevice $deviceRaw) |
189
|
|
|
{ |
190
|
5 |
|
$os->setName($this->getRealResult($deviceRaw->getVirtualCapability('advertised_device_os'), 'operatingSystem', 'name')); |
191
|
5 |
|
$os->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version')); |
192
|
5 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* |
196
|
|
|
* @param Model\UserAgent $device |
197
|
|
|
* @param CustomDevice $deviceRaw |
198
|
|
|
*/ |
199
|
5 |
|
private function hydrateDevice(Model\Device $device, CustomDevice $deviceRaw) |
200
|
|
|
{ |
201
|
|
|
// @see the list of all types http://web.wurfl.io/ |
202
|
5 |
|
$device->setType($deviceRaw->getVirtualCapability('form_factor')); |
203
|
|
|
|
204
|
5 |
|
if ($deviceRaw->getVirtualCapability('is_full_desktop') === 'true' || $deviceRaw->getVirtualCapability('is_full_desktop') === true) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
5 |
|
$device->setModel($this->getRealResult($deviceRaw->getCapability('model_name'), 'device', 'model')); |
209
|
5 |
|
$device->setBrand($this->getRealResult($deviceRaw->getCapability('brand_name'), 'device', 'brand')); |
210
|
|
|
|
211
|
5 |
|
if ($deviceRaw->getVirtualCapability('is_mobile') === 'true' || $deviceRaw->getVirtualCapability('is_mobile') === true) { |
212
|
1 |
|
$device->setIsMobile(true); |
213
|
|
|
} |
214
|
|
|
|
215
|
5 |
|
if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true' || $deviceRaw->getVirtualCapability('is_touchscreen') === true) { |
216
|
1 |
|
$device->setIsTouch(true); |
217
|
|
|
} |
218
|
5 |
|
} |
219
|
|
|
|
220
|
7 |
|
public function parse($userAgent, array $headers = []) |
221
|
|
|
{ |
222
|
7 |
|
$parser = $this->getParser(); |
223
|
|
|
|
224
|
7 |
|
$deviceRaw = $parser->getDeviceForUserAgent($userAgent); |
225
|
|
|
|
226
|
|
|
/* |
227
|
|
|
* No result found? |
228
|
|
|
*/ |
229
|
7 |
|
if ($this->hasResult($deviceRaw) !== true) { |
230
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* Hydrate the model |
235
|
|
|
*/ |
236
|
6 |
|
$result = new Model\UserAgent(); |
237
|
6 |
|
$result->setProviderResultRaw([ |
238
|
6 |
|
'virtual' => $deviceRaw->getAllVirtualCapabilities(), |
239
|
6 |
|
'all' => $deviceRaw->getAllCapabilities(), |
240
|
|
|
]); |
241
|
|
|
|
242
|
|
|
/* |
243
|
|
|
* Bot detection |
244
|
|
|
*/ |
245
|
6 |
|
if ($deviceRaw->getVirtualCapability('is_robot') === 'true' || $deviceRaw->getVirtualCapability('is_robot') === true) { |
246
|
1 |
|
$bot = $result->getBot(); |
247
|
1 |
|
$bot->setIsBot(true); |
248
|
|
|
|
249
|
|
|
// brand_name seems to be always google, so dont use it |
250
|
|
|
|
251
|
1 |
|
return $result; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/* |
255
|
|
|
* hydrate the result |
256
|
|
|
*/ |
257
|
5 |
|
$this->hydrateBrowser($result->getBrowser(), $deviceRaw); |
258
|
|
|
// renderingEngine not available |
259
|
5 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $deviceRaw); |
260
|
5 |
|
$this->hydrateDevice($result->getDevice(), $deviceRaw); |
261
|
|
|
|
262
|
5 |
|
return $result; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.