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