1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception; |
5
|
|
|
use UserAgentParser\Model; |
6
|
|
|
use Wurfl\CustomDevice; |
7
|
|
|
use Wurfl\Manager as WurflManager; |
8
|
|
|
|
9
|
|
|
class Wurfl extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @var WurflManager |
14
|
|
|
*/ |
15
|
|
|
private $parser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @param WurflManager $parser |
20
|
|
|
*/ |
21
|
9 |
|
public function __construct(WurflManager $parser) |
22
|
|
|
{ |
23
|
9 |
|
$this->setParser($parser); |
24
|
9 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function getName() |
27
|
|
|
{ |
28
|
1 |
|
return 'Wurfl'; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function getComposerPackageName() |
32
|
|
|
{ |
33
|
1 |
|
return 'mimmi20/wurfl'; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function getVersion() |
37
|
|
|
{ |
38
|
1 |
|
return $this->getParser()->getWurflInfo()->version; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @param WurflManager $parser |
44
|
|
|
*/ |
45
|
9 |
|
public function setParser(WurflManager $parser) |
46
|
|
|
{ |
47
|
9 |
|
$this->parser = $parser; |
48
|
9 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @return WurflManager |
53
|
|
|
*/ |
54
|
7 |
|
public function getParser() |
55
|
|
|
{ |
56
|
7 |
|
return $this->parser; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $value |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
3 |
|
private function isRealBrand($value) |
65
|
|
|
{ |
66
|
3 |
|
if ($value === '' || $value === null) { |
67
|
2 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
if ($value == 'Generic') { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $value |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
3 |
|
private function isRealModel($value) |
83
|
|
|
{ |
84
|
3 |
|
if ($value === '' || $value === null) { |
85
|
2 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if (substr((string) $value, 0, 7) == 'Android') { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
if (substr((string) $value, 0, 7) == 'Firefox') { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
if (substr((string) $value, 0, 7) == 'Generic') { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if (substr((string) $value, 0, 12) == 'unrecognized') { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
if (substr((string) $value, 0, 14) == 'Windows Mobile') { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
if (substr((string) $value, 0, 13) == 'Windows Phone') { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
if (substr((string) $value, 0, 10) == 'Windows RT') { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* |
121
|
|
|
* @param CustomDevice $device |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
5 |
|
private function hasResult(CustomDevice $device) |
125
|
|
|
{ |
126
|
5 |
|
if ($device->id !== null && $device->id != '' && $device->id !== 'generic') { |
127
|
4 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
5 |
|
public function parse($userAgent, array $headers = []) |
134
|
|
|
{ |
135
|
5 |
|
$parser = $this->getParser(); |
136
|
|
|
|
137
|
5 |
|
$deviceRaw = $parser->getDeviceForUserAgent($userAgent); |
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
* No result found? |
141
|
|
|
*/ |
142
|
5 |
|
if ($this->hasResult($deviceRaw) !== true) { |
143
|
1 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/* |
147
|
|
|
* Hydrate the model |
148
|
|
|
*/ |
149
|
4 |
|
$result = new Model\UserAgent(); |
150
|
4 |
|
$result->setProviderResultRaw([ |
151
|
4 |
|
'virtual' => $deviceRaw->getAllVirtualCapabilities(), |
152
|
4 |
|
'all' => $deviceRaw->getAllCapabilities(), |
153
|
4 |
|
]); |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* Bot detection |
157
|
|
|
*/ |
158
|
4 |
|
if ($deviceRaw->getVirtualCapability('is_robot') === 'true') { |
159
|
1 |
|
$bot = $result->getBot(); |
160
|
1 |
|
$bot->setIsBot(true); |
161
|
|
|
|
162
|
|
|
// brand_name seems to be always google, so dont use it |
163
|
|
|
|
164
|
1 |
|
return $result; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/* |
168
|
|
|
* browser |
169
|
|
|
*/ |
170
|
3 |
|
$browser = $result->getBrowser(); |
171
|
|
|
|
172
|
3 |
|
$browser->setName($deviceRaw->getVirtualCapability('advertised_browser')); |
173
|
3 |
|
$browser->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_browser_version')); |
174
|
|
|
|
175
|
|
|
/* |
176
|
|
|
* operatingSystem |
177
|
|
|
*/ |
178
|
3 |
|
$operatingSystem = $result->getOperatingSystem(); |
179
|
|
|
|
180
|
3 |
|
$operatingSystem->setName($deviceRaw->getVirtualCapability('advertised_device_os')); |
181
|
3 |
|
$operatingSystem->getVersion()->setComplete($deviceRaw->getVirtualCapability('advertised_device_os_version')); |
182
|
|
|
|
183
|
|
|
/* |
184
|
|
|
* device |
185
|
|
|
*/ |
186
|
3 |
|
$device = $result->getDevice(); |
187
|
|
|
|
188
|
3 |
|
if ($deviceRaw->getVirtualCapability('is_full_desktop') !== 'true') { |
189
|
3 |
|
if ($this->isRealModel($deviceRaw->getCapability('model_name')) === true) { |
190
|
1 |
|
$device->setModel($deviceRaw->getCapability('model_name')); |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
if ($this->isRealBrand($deviceRaw->getCapability('brand_name')) === true) { |
194
|
1 |
|
$device->setBrand($deviceRaw->getCapability('brand_name')); |
195
|
|
|
} |
196
|
|
|
|
197
|
3 |
|
if ($deviceRaw->getVirtualCapability('is_mobile') === 'true') { |
198
|
1 |
|
$device->setIsMobile(true); |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
if ($deviceRaw->getVirtualCapability('is_touchscreen') === 'true') { |
202
|
1 |
|
$device->setIsTouch(true); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// @see the list of all types http://web.wurfl.io/ |
207
|
3 |
|
$device->setType($deviceRaw->getVirtualCapability('form_factor')); |
208
|
|
|
|
209
|
3 |
|
return $result; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|