1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use BrowscapPHP\Browscap; |
5
|
|
|
use DateTime; |
6
|
|
|
use stdClass; |
7
|
|
|
use UserAgentParser\Exception\InvalidArgumentException; |
8
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
9
|
|
|
use UserAgentParser\Model; |
10
|
|
|
|
11
|
|
|
abstract class AbstractBrowscap extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Homepage of the provider |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $homepage = 'https://github.com/browscap/browscap-php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Composer package name |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $packageName = 'browscap/browscap-php'; |
26
|
|
|
|
27
|
|
|
protected $defaultValues = [ |
28
|
|
|
|
29
|
|
|
'general' => [ |
30
|
|
|
'/^unknown$/i', |
31
|
|
|
], |
32
|
|
|
|
33
|
|
|
'browser' => [ |
34
|
|
|
'name' => [ |
35
|
|
|
'/^Default Browser$/i', |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
'device' => [ |
40
|
|
|
'model' => [ |
41
|
|
|
'/^general/i', |
42
|
|
|
'/desktop$/i', |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
'bot' => [ |
47
|
|
|
'name' => [ |
48
|
|
|
'/^General Crawlers/i', |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @var Browscap |
56
|
|
|
*/ |
57
|
|
|
private $parser; |
58
|
|
|
|
59
|
27 |
|
public function __construct(Browscap $parser, $expectedType = '') |
60
|
|
|
{ |
61
|
27 |
|
$this->parser = $parser; |
62
|
|
|
|
63
|
27 |
|
if ($expectedType !== $parser->getCache()->getType()) { |
64
|
1 |
|
throw new InvalidArgumentException('Expected the "' . $expectedType . '" data file. Instead got the "' . $parser->getCache()->getType() . '" data file'); |
65
|
|
|
} |
66
|
26 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function getVersion() |
69
|
|
|
{ |
70
|
1 |
|
return $this->getParser() |
71
|
1 |
|
->getCache() |
72
|
1 |
|
->getVersion(); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function getUpdateDate() |
76
|
|
|
{ |
77
|
1 |
|
$releaseDate = $this->getParser() |
78
|
1 |
|
->getCache() |
79
|
1 |
|
->getReleaseDate(); |
80
|
|
|
|
81
|
1 |
|
return DateTime::createFromFormat('D, d M Y H:i:s O', $releaseDate); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @return Browscap |
87
|
|
|
*/ |
88
|
15 |
|
public function getParser() |
89
|
|
|
{ |
90
|
15 |
|
return $this->parser; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @param stdClass $resultRaw |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
12 |
|
private function hasResult(stdClass $resultRaw) |
100
|
|
|
{ |
101
|
12 |
|
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) { |
102
|
8 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* |
110
|
|
|
* @param stdClass $resultRaw |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
8 |
|
private function isBot(stdClass $resultRaw) |
114
|
|
|
{ |
115
|
8 |
|
if (isset($resultRaw->crawler) && $resultRaw->crawler === true) { |
116
|
4 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
* @param Model\Bot $bot |
125
|
|
|
* @param stdClass $resultRaw |
126
|
|
|
*/ |
127
|
4 |
|
private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
128
|
|
|
{ |
129
|
4 |
|
$bot->setIsBot(true); |
130
|
|
|
|
131
|
4 |
|
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) { |
132
|
3 |
|
$bot->setName($resultRaw->browser); |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) { |
136
|
1 |
|
$bot->setType('RSS'); |
137
|
3 |
|
} elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) { |
138
|
2 |
|
$bot->setType($resultRaw->browser_type); |
139
|
|
|
} |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @param Model\Browser $browser |
145
|
|
|
* @param stdClass $resultRaw |
146
|
|
|
*/ |
147
|
4 |
|
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
148
|
|
|
{ |
149
|
4 |
|
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) { |
150
|
4 |
|
$browser->setName($resultRaw->browser); |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) { |
154
|
2 |
|
$browser->getVersion()->setComplete($resultRaw->version); |
155
|
|
|
} |
156
|
4 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
* @param Model\RenderingEngine $engine |
161
|
|
|
* @param stdClass $resultRaw |
162
|
|
|
*/ |
163
|
4 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
164
|
|
|
{ |
165
|
4 |
|
if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) { |
166
|
1 |
|
$engine->setName($resultRaw->renderingengine_name); |
167
|
|
|
} |
168
|
|
|
|
169
|
4 |
|
if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) { |
170
|
1 |
|
$engine->getVersion()->setComplete($resultRaw->renderingengine_version); |
171
|
|
|
} |
172
|
4 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* @param Model\OperatingSystem $os |
177
|
|
|
* @param stdClass $resultRaw |
178
|
|
|
*/ |
179
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
180
|
|
|
{ |
181
|
4 |
|
if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) { |
182
|
1 |
|
$os->setName($resultRaw->platform); |
183
|
|
|
} |
184
|
|
|
|
185
|
4 |
|
if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) { |
186
|
1 |
|
$os->getVersion()->setComplete($resultRaw->platform_version); |
187
|
|
|
} |
188
|
4 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
* @param Model\UserAgent $device |
193
|
|
|
* @param stdClass $resultRaw |
194
|
|
|
*/ |
195
|
4 |
|
private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
196
|
|
|
{ |
197
|
4 |
|
if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, 'device', 'model') === true) { |
198
|
1 |
|
$device->setModel($resultRaw->device_name); |
199
|
|
|
} |
200
|
|
|
|
201
|
4 |
|
if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) { |
202
|
1 |
|
$device->setBrand($resultRaw->device_brand_name); |
203
|
|
|
} |
204
|
|
|
|
205
|
4 |
|
if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) { |
206
|
|
|
// @todo convert to a common set of types (over all vendors) |
207
|
1 |
|
$device->setType($resultRaw->device_type); |
208
|
|
|
} |
209
|
|
|
|
210
|
4 |
|
if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) { |
211
|
1 |
|
$device->setIsMobile(true); |
212
|
|
|
} |
213
|
|
|
|
214
|
4 |
|
if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') { |
215
|
1 |
|
$device->setIsTouch(true); |
216
|
|
|
} |
217
|
4 |
|
} |
218
|
|
|
|
219
|
12 |
|
public function parse($userAgent, array $headers = []) |
220
|
|
|
{ |
221
|
12 |
|
$parser = $this->getParser(); |
222
|
|
|
|
223
|
|
|
/* @var $resultRaw \stdClass */ |
224
|
12 |
|
$resultRaw = $parser->getBrowser($userAgent); |
225
|
|
|
|
226
|
|
|
/* |
227
|
|
|
* No result found? |
228
|
|
|
*/ |
229
|
12 |
|
if ($this->hasResult($resultRaw) !== true) { |
230
|
4 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* Hydrate the model |
235
|
|
|
*/ |
236
|
8 |
|
$result = new Model\UserAgent(); |
237
|
8 |
|
$result->setProviderResultRaw($resultRaw); |
238
|
|
|
|
239
|
|
|
/* |
240
|
|
|
* Bot detection (does only work with full_php_browscap.ini) |
241
|
|
|
*/ |
242
|
8 |
|
if ($this->isBot($resultRaw) === true) { |
243
|
4 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
244
|
|
|
|
245
|
4 |
|
return $result; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/* |
249
|
|
|
* hydrate the result |
250
|
|
|
*/ |
251
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
252
|
4 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw); |
253
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); |
254
|
4 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
255
|
|
|
|
256
|
4 |
|
return $result; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|