1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use Jenssegers\Agent\Agent; |
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction for jenssegers/agent |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see https://github.com/jenssegers/agent |
15
|
|
|
*/ |
16
|
|
|
class JenssegersAgent extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'JenssegersAgent'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'https://github.com/jenssegers/agent'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Composer package name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName = 'jenssegers/agent'; |
38
|
|
|
|
39
|
|
|
protected $detectionCapabilities = [ |
40
|
|
|
|
41
|
|
|
'browser' => [ |
42
|
|
|
'name' => true, |
43
|
|
|
'version' => true, |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
'renderingEngine' => [ |
47
|
|
|
'name' => false, |
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' => false, |
62
|
|
|
], |
63
|
|
|
|
64
|
|
|
'bot' => [ |
65
|
|
|
'isBot' => true, |
66
|
|
|
'name' => true, |
67
|
|
|
'type' => false, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
72
|
|
|
|
73
|
|
|
'general' => [], |
74
|
|
|
|
75
|
|
|
'browser' => [ |
76
|
|
|
'name' => [ |
77
|
|
|
'/^GenericBrowser$/i', |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Used for unitTests mocking |
84
|
|
|
* |
85
|
|
|
* @var Agent |
86
|
|
|
*/ |
87
|
|
|
private $parser; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* |
91
|
|
|
* @throws PackageNotLoadedException |
92
|
|
|
*/ |
93
|
14 |
|
public function __construct() |
94
|
|
|
{ |
95
|
14 |
|
$this->checkIfInstalled(); |
96
|
14 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @return Agent |
101
|
|
|
*/ |
102
|
7 |
|
public function getParser() |
103
|
|
|
{ |
104
|
7 |
|
if ($this->parser !== null) { |
105
|
6 |
|
return $this->parser; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return new Agent(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @param array $resultRaw |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
6 |
|
private function hasResult(array $resultRaw) |
118
|
|
|
{ |
119
|
6 |
|
if ($resultRaw['isMobile'] === true || $resultRaw['isRobot'] === true) { |
120
|
3 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
if ($this->isRealResult($resultRaw['browserName'], 'browser', 'name') === true || $this->isRealResult($resultRaw['osName']) === true || $this->isRealResult($resultRaw['botName']) === true) { |
124
|
2 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @param Model\Bot $bot |
133
|
|
|
* @param array $resultRaw |
134
|
|
|
*/ |
135
|
2 |
|
private function hydrateBot(Model\Bot $bot, array $resultRaw) |
136
|
|
|
{ |
137
|
2 |
|
$bot->setIsBot(true); |
138
|
2 |
|
$bot->setName($this->getRealResult($resultRaw['botName'])); |
139
|
2 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* |
143
|
|
|
* @param Model\Browser $browser |
144
|
|
|
* @param array $resultRaw |
145
|
|
|
*/ |
146
|
3 |
|
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
147
|
|
|
{ |
148
|
3 |
|
if ($this->isRealResult($resultRaw['browserName'], 'browser', 'name') === true) { |
149
|
1 |
|
$browser->setName($resultRaw['browserName']); |
150
|
1 |
|
$browser->getVersion()->setComplete($this->getRealResult($resultRaw['browserVersion'])); |
151
|
|
|
} |
152
|
3 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* |
156
|
|
|
* @param Model\OperatingSystem $os |
157
|
|
|
* @param array $resultRaw |
158
|
|
|
*/ |
159
|
3 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw) |
160
|
|
|
{ |
161
|
3 |
|
if ($this->isRealResult($resultRaw['osName']) === true) { |
162
|
1 |
|
$os->setName($resultRaw['osName']); |
163
|
1 |
|
$os->getVersion()->setComplete($this->getRealResult($resultRaw['osVersion'])); |
164
|
|
|
} |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* @param Model\Device $device |
170
|
|
|
* @param array $resultRaw |
171
|
|
|
*/ |
172
|
3 |
|
private function hydrateDevice(Model\Device $device, array $resultRaw) |
173
|
|
|
{ |
174
|
3 |
|
if ($resultRaw['isMobile'] === true) { |
175
|
1 |
|
$device->setIsMobile(true); |
176
|
|
|
} |
177
|
|
|
|
178
|
3 |
|
$device->setModel($resultRaw['deviceModel']); |
179
|
3 |
|
$device->setType($resultRaw['type']); |
180
|
3 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the kind of device. |
184
|
|
|
* |
185
|
|
|
* @param Agent $parser |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
6 |
|
public function getDeviceKind(Agent $parser) |
190
|
|
|
{ |
191
|
6 |
|
$kind = 'unavailable'; |
192
|
|
|
|
193
|
6 |
|
if ($parser->isTablet()) { |
194
|
|
|
$kind = 'Tablet'; |
195
|
6 |
|
} elseif ($parser->isMobile() || $parser->isPhone()) { |
196
|
1 |
|
$kind = 'Mobile'; |
197
|
5 |
|
} elseif ($parser->isDesktop()) { |
198
|
|
|
$kind = 'Desktop'; |
199
|
|
|
} |
200
|
|
|
|
201
|
6 |
|
return $kind; |
202
|
|
|
} |
203
|
|
|
|
204
|
6 |
|
public function parse($userAgent, array $headers = []) |
205
|
|
|
{ |
206
|
6 |
|
$parser = $this->getParser(); |
207
|
6 |
|
$parser->setHttpHeaders($headers); |
208
|
6 |
|
$parser->setUserAgent($userAgent); |
209
|
|
|
|
210
|
|
|
/* |
211
|
|
|
* Since Mobile_Detect to a regex comparison on every call |
212
|
|
|
* We cache it here for all checks and hydration |
213
|
|
|
*/ |
214
|
6 |
|
$browserName = $parser->browser(); |
215
|
6 |
|
$osName = $parser->platform(); |
216
|
|
|
|
217
|
|
|
$resultCache = [ |
218
|
6 |
|
'browserName' => $browserName, |
219
|
6 |
|
'browserVersion' => $parser->version($browserName), |
220
|
|
|
|
221
|
6 |
|
'osName' => $osName, |
222
|
6 |
|
'osVersion' => $parser->version($osName), |
223
|
|
|
|
224
|
6 |
|
'deviceModel' => $parser->device(), |
225
|
6 |
|
'isMobile' => $parser->isMobile(), |
226
|
6 |
|
'isTablet' => $parser->isTablet(), |
227
|
6 |
|
'isDesktop' => $parser->isDesktop(), |
228
|
6 |
|
'type' => $this->getDeviceKind($parser), |
229
|
|
|
|
230
|
6 |
|
'isRobot' => $parser->isRobot(), |
231
|
6 |
|
'botName' => $parser->robot(), |
232
|
|
|
]; |
233
|
|
|
|
234
|
|
|
/* |
235
|
|
|
* No result found? |
236
|
|
|
*/ |
237
|
6 |
|
if ($this->hasResult($resultCache) !== true) { |
238
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/* |
242
|
|
|
* Hydrate the model |
243
|
|
|
*/ |
244
|
5 |
|
$result = new Model\UserAgent($this->getName(), $this->getVersion()); |
245
|
5 |
|
$result->setProviderResultRaw($resultCache); |
246
|
|
|
|
247
|
|
|
/* |
248
|
|
|
* Bot detection |
249
|
|
|
*/ |
250
|
5 |
|
if ($resultCache['isRobot'] === true) { |
251
|
2 |
|
$this->hydrateBot($result->getBot(), $resultCache); |
252
|
|
|
|
253
|
2 |
|
return $result; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/* |
257
|
|
|
* hydrate the result |
258
|
|
|
*/ |
259
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultCache); |
260
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultCache); |
261
|
3 |
|
$this->hydrateDevice($result->getDevice(), $resultCache); |
262
|
|
|
|
263
|
3 |
|
return $result; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|