1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use BrowserDetector\Detector; |
5
|
|
|
use Psr\Log\NullLogger; |
6
|
|
|
use Psr6NullCache\Adapter\MemoryCacheItemPool; |
7
|
|
|
use UaResult\Browser\BrowserInterface; |
8
|
|
|
use UaResult\Device\DeviceInterface; |
9
|
|
|
use UaResult\Engine\EngineInterface; |
10
|
|
|
use UaResult\Os\OsInterface; |
11
|
|
|
use UaResult\Result\Result; |
12
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
13
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
14
|
|
|
use UserAgentParser\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstraction for mimmi20/browser-detector |
18
|
|
|
* |
19
|
|
|
* @author Martin Keckeis <[email protected]> |
20
|
|
|
* @license MIT |
21
|
|
|
* @see https://github.com/donatj/PhpUserAgent |
22
|
|
|
*/ |
23
|
|
|
class Mimmi20BrowserDetector extends AbstractProvider |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Name of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name = 'Mimmi20BrowserDetector'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Homepage of the provider |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $homepage = 'https://github.com/mimmi20/BrowserDetector'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Composer package name |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $packageName = 'mimmi20/browser-detector'; |
45
|
|
|
|
46
|
|
|
protected $detectionCapabilities = [ |
47
|
|
|
|
48
|
|
|
'browser' => [ |
49
|
|
|
'name' => true, |
50
|
|
|
'version' => true, |
51
|
|
|
], |
52
|
|
|
|
53
|
|
|
'renderingEngine' => [ |
54
|
|
|
'name' => true, |
55
|
|
|
'version' => false, |
56
|
|
|
], |
57
|
|
|
|
58
|
|
|
'operatingSystem' => [ |
59
|
|
|
'name' => true, |
60
|
|
|
'version' => true, |
61
|
|
|
], |
62
|
|
|
|
63
|
|
|
'device' => [ |
64
|
|
|
'model' => true, |
65
|
|
|
'brand' => true, |
66
|
|
|
'type' => true, |
67
|
|
|
'isMobile' => false, |
68
|
|
|
'isTouch' => true, |
69
|
|
|
], |
70
|
|
|
|
71
|
|
|
'bot' => [ |
72
|
|
|
'isBot' => true, |
73
|
|
|
'name' => true, |
74
|
|
|
'type' => false, |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
protected $defaultValues = [ |
79
|
|
|
|
80
|
|
|
'general' => [ |
81
|
|
|
'/^unknown$/i', |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @var Detector |
88
|
|
|
*/ |
89
|
|
|
private $parser; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param Detector $parser |
94
|
|
|
* @throws PackageNotLoadedException |
95
|
|
|
*/ |
96
|
15 |
|
public function __construct(Detector $parser = null) |
97
|
|
|
{ |
98
|
15 |
|
if ($parser === null) { |
99
|
8 |
|
$this->checkIfInstalled(); |
100
|
|
|
} |
101
|
|
|
|
102
|
15 |
|
$this->parser = $parser; |
103
|
15 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* @return Detector |
108
|
|
|
*/ |
109
|
8 |
|
public function getParser() |
110
|
|
|
{ |
111
|
8 |
|
if ($this->parser !== null) { |
112
|
8 |
|
return $this->parser; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$cache = new MemoryCacheItemPool(); |
116
|
1 |
|
$logger = new NullLogger(); |
117
|
|
|
|
118
|
1 |
|
$this->parser = new Detector($cache, $logger); |
119
|
|
|
|
120
|
1 |
|
return $this->parser; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
* @param Result $result |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
7 |
|
private function hasResult(Result $result) |
130
|
|
|
{ |
131
|
7 |
|
if ($this->isRealResult($result->getBrowser() |
132
|
7 |
|
->getType() |
133
|
7 |
|
->getType())) { |
134
|
6 |
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @param Model\Bot $bot |
143
|
|
|
* @param BrowserInterface $browserRaw |
144
|
|
|
*/ |
145
|
2 |
|
private function hydrateBot(Model\Bot $bot, BrowserInterface $browserRaw) |
146
|
|
|
{ |
147
|
2 |
|
$bot->setIsBot(true); |
148
|
2 |
|
$bot->setName($this->getRealResult($browserRaw->getName())); |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @param Model\Browser $browser |
154
|
|
|
* @param BrowserInterface $browserRaw |
155
|
|
|
*/ |
156
|
4 |
|
private function hydrateBrowser(Model\Browser $browser, BrowserInterface $browserRaw) |
157
|
|
|
{ |
158
|
4 |
|
$browser->setName($this->getRealResult($browserRaw->getName())); |
159
|
4 |
|
$browser->getVersion()->setComplete($this->getRealResult($browserRaw->getVersion() |
160
|
4 |
|
->getVersion())); |
161
|
4 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* @param Model\RenderingEngine $engine |
166
|
|
|
* @param EngineInterface $engineRaw |
167
|
|
|
*/ |
168
|
4 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, EngineInterface $engineRaw) |
169
|
|
|
{ |
170
|
4 |
|
$engine->setName($this->getRealResult($engineRaw->getName())); |
171
|
4 |
|
$engine->getVersion()->setComplete($this->getRealResult($engineRaw->getVersion()->getVersion())); |
172
|
4 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* @param Model\OperatingSystem $os |
177
|
|
|
* @param OsInterface $osRaw |
178
|
|
|
*/ |
179
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, OsInterface $osRaw) |
180
|
|
|
{ |
181
|
4 |
|
$os->setName($this->getRealResult($osRaw->getName())); |
182
|
4 |
|
$os->getVersion()->setComplete($this->getRealResult($osRaw->getVersion()->getVersion())); |
183
|
4 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* |
187
|
|
|
* @param Model\UserAgent $device |
188
|
|
|
* @param DeviceInterface $deviceRaw |
189
|
|
|
*/ |
190
|
4 |
|
private function hydrateDevice(Model\Device $device, DeviceInterface $deviceRaw) |
191
|
|
|
{ |
192
|
4 |
|
$device->setModel($this->getRealResult($deviceRaw->getDeviceName())); |
193
|
4 |
|
$device->setBrand($this->getRealResult($deviceRaw->getBrand()->getBrandName())); |
194
|
4 |
|
$device->setType($this->getRealResult($deviceRaw->getType()->getName())); |
195
|
|
|
|
196
|
4 |
|
if ($deviceRaw->getPointingMethod() === 'touchscreen') { |
197
|
1 |
|
$device->setIsTouch(true); |
198
|
|
|
} |
199
|
4 |
|
} |
200
|
|
|
|
201
|
7 |
|
public function parse($userAgent, array $headers = []) |
202
|
|
|
{ |
203
|
7 |
|
$headers['HTTP_USER_AGENT'] = $userAgent; |
204
|
|
|
|
205
|
7 |
|
$detector = $this->getParser(); |
206
|
|
|
|
207
|
7 |
|
$parser = $detector->getBrowser($headers); |
208
|
|
|
|
209
|
|
|
/* |
210
|
|
|
* No result found? |
211
|
|
|
*/ |
212
|
7 |
|
if ($this->hasResult($parser) !== true) { |
213
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/* |
217
|
|
|
* Hydrate the model |
218
|
|
|
*/ |
219
|
6 |
|
$result = new Model\UserAgent($this->getName(), $this->getVersion()); |
220
|
6 |
|
$result->setProviderResultRaw($parser->toArray(true)); |
221
|
|
|
|
222
|
|
|
/* |
223
|
|
|
* Bot detection |
224
|
|
|
*/ |
225
|
6 |
|
if ($parser->getBrowser()->getType()->getType() === 'bot') { |
226
|
2 |
|
$this->hydrateBot($result->getBot(), $parser->getBrowser()); |
227
|
|
|
|
228
|
2 |
|
return $result; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/* |
232
|
|
|
* hydrate the result |
233
|
|
|
*/ |
234
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $parser->getBrowser()); |
235
|
4 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->getEngine()); |
236
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->getOs()); |
237
|
4 |
|
$this->hydrateDevice($result->getDevice(), $parser->getDevice()); |
238
|
|
|
|
239
|
4 |
|
return $result; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|