1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use EndorphinStudio\Detector; |
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction for piwik/device-detector |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see https://github.com/endorphin-studio/browser-detector |
15
|
|
|
*/ |
16
|
|
|
class Endorphin extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'Endorphin'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'https://github.com/endorphin-studio/browser-detector'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Composer package name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName = 'endorphin-studio/browser-detector'; |
38
|
|
|
|
39
|
|
|
protected $detectionCapabilities = [ |
40
|
|
|
|
41
|
|
|
'browser' => [ |
42
|
|
|
'name' => true, |
43
|
|
|
'version' => true, |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
'renderingEngine' => [ |
47
|
|
|
'name' => true, |
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' => true, |
67
|
|
|
'type' => true, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
72
|
|
|
|
73
|
|
|
'general' => [ |
74
|
|
|
'/^N\\\\A$/i', |
75
|
|
|
], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @throws PackageNotLoadedException |
81
|
|
|
*/ |
82
|
|
|
public function __construct() |
83
|
|
|
{ |
84
|
|
|
if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
85
|
|
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* |
91
|
|
|
* @param Detector\DetectorResult $resultRaw |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
private function hasResult(Detector\DetectorResult $resultRaw) |
96
|
|
|
{ |
97
|
|
|
if ($resultRaw->OS instanceof Detector\OS) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($resultRaw->Browser instanceof Detector\Browser) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($resultRaw->Device instanceof Detector\Device) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($resultRaw->Robot instanceof Detector\Robot) { |
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @param Model\Bot $bot |
119
|
|
|
* @param Detector\Robot $resultRaw |
120
|
|
|
*/ |
121
|
|
|
private function hydrateBot(Model\Bot $bot, Detector\Robot $resultRaw) |
122
|
|
|
{ |
123
|
|
|
$bot->setIsBot(true); |
124
|
|
|
$bot->setName($this->getRealResult($resultRaw->getName())); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* @param Model\Browser $browser |
130
|
|
|
* @param Detector\Browser $resultRaw |
131
|
|
|
*/ |
132
|
|
|
private function hydrateBrowser(Model\Browser $browser, Detector\Browser $resultRaw) |
133
|
|
|
{ |
134
|
|
|
$browser->setName($this->getRealResult($resultRaw->getName())); |
|
|
|
|
135
|
|
|
$browser->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion())); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* @param Model\OperatingSystem $os |
141
|
|
|
* @param Detector\OS $resultRaw |
142
|
|
|
*/ |
143
|
|
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, Detector\OS $resultRaw) |
144
|
|
|
{ |
145
|
|
|
$os->setName($this->getRealResult($resultRaw->getName())); |
|
|
|
|
146
|
|
|
$os->getVersion()->setComplete($this->getRealResult($resultRaw->getVersion())); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* |
151
|
|
|
* @param Model\Device $device |
152
|
|
|
* @param Detector\Device $resultRaw |
153
|
|
|
*/ |
154
|
|
|
private function hydrateDevice(Model\Device $device, Detector\Device $resultRaw) |
155
|
|
|
{ |
156
|
|
|
$device->setModel($this->getRealResult($resultRaw->ModelName)); |
157
|
|
|
$device->setType($this->getRealResult($resultRaw->getType())); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function parse($userAgent, array $headers = []) |
161
|
|
|
{ |
162
|
|
|
$resultRaw = \EndorphinStudio\Detector\Detector::analyse($userAgent); |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* No result found? |
166
|
|
|
*/ |
167
|
|
|
if ($this->hasResult($resultRaw) !== true) { |
168
|
|
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/* |
172
|
|
|
* Hydrate the model |
173
|
|
|
*/ |
174
|
|
|
$result = new Model\UserAgent(); |
175
|
|
|
$result->setProviderResultRaw($resultRaw); |
176
|
|
|
|
177
|
|
|
/* |
178
|
|
|
* Bot detection |
179
|
|
|
*/ |
180
|
|
|
if ($resultRaw->Robot instanceof Detector\Robot) { |
|
|
|
|
181
|
|
|
$this->hydrateBot($result->getBot(), $resultRaw->Robot); |
|
|
|
|
182
|
|
|
|
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/* |
187
|
|
|
* hydrate the result |
188
|
|
|
*/ |
189
|
|
|
if ($resultRaw->Browser instanceof Detector\Browser) { |
190
|
|
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw->Browser); |
191
|
|
|
} |
192
|
|
|
if ($resultRaw->OS instanceof Detector\OS) { |
193
|
|
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->OS); |
194
|
|
|
} |
195
|
|
|
if ($resultRaw->Device instanceof Detector\Device) { |
196
|
|
|
$this->hydrateDevice($result->getDevice(), $resultRaw->Device); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.