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' => false, |
58
|
|
|
'brand' => false, |
59
|
|
|
'type' => false, |
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
|
3 |
|
} |
178
|
|
|
|
179
|
6 |
|
public function parse($userAgent, array $headers = []) |
180
|
|
|
{ |
181
|
6 |
|
$parser = $this->getParser(); |
182
|
6 |
|
$parser->setHttpHeaders($headers); |
183
|
6 |
|
$parser->setUserAgent($userAgent); |
184
|
|
|
|
185
|
|
|
/* |
186
|
|
|
* Since Mobile_Detect to a regex comparison on every call |
187
|
|
|
* We cache it here for all checks and hydration |
188
|
|
|
*/ |
189
|
6 |
|
$browserName = $parser->browser(); |
190
|
6 |
|
$osName = $parser->platform(); |
191
|
|
|
|
192
|
|
|
$resultCache = [ |
193
|
6 |
|
'browserName' => $browserName, |
194
|
6 |
|
'browserVersion' => $parser->version($browserName), |
195
|
|
|
|
196
|
6 |
|
'osName' => $osName, |
197
|
6 |
|
'osVersion' => $parser->version($osName), |
198
|
|
|
|
199
|
6 |
|
'deviceModel' => $parser->device(), |
200
|
6 |
|
'isMobile' => $parser->isMobile(), |
201
|
|
|
|
202
|
6 |
|
'isRobot' => $parser->isRobot(), |
203
|
6 |
|
'botName' => $parser->robot(), |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* No result found? |
208
|
|
|
*/ |
209
|
6 |
|
if ($this->hasResult($resultCache) !== true) { |
210
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/* |
214
|
|
|
* Hydrate the model |
215
|
|
|
*/ |
216
|
5 |
|
$result = new Model\UserAgent($this->getName(), $this->getVersion()); |
217
|
5 |
|
$result->setProviderResultRaw($resultCache); |
218
|
|
|
|
219
|
|
|
/* |
220
|
|
|
* Bot detection |
221
|
|
|
*/ |
222
|
5 |
|
if ($resultCache['isRobot'] === true) { |
223
|
2 |
|
$this->hydrateBot($result->getBot(), $resultCache); |
224
|
|
|
|
225
|
2 |
|
return $result; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/* |
229
|
|
|
* hydrate the result |
230
|
|
|
*/ |
231
|
3 |
|
$this->hydrateBrowser($result->getBrowser(), $resultCache); |
232
|
3 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultCache); |
233
|
3 |
|
$this->hydrateDevice($result->getDevice(), $resultCache); |
234
|
|
|
|
235
|
3 |
|
return $result; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|