|
1
|
|
|
<?php |
|
2
|
|
|
namespace UserAgentParser\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use UserAgentParser\Exception\InvalidArgumentException; |
|
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
|
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
|
7
|
|
|
use UserAgentParser\Model; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstraction for donatj/PhpUserAgent |
|
11
|
|
|
* |
|
12
|
|
|
* @author Martin Keckeis <[email protected]> |
|
13
|
|
|
* @license MIT |
|
14
|
|
|
* @see http://www.detectright.com/ |
|
15
|
|
|
*/ |
|
16
|
|
|
class DetectRight extends AbstractProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Name of the provider |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name = 'DetectRight'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Homepage of the provider |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $homepage = 'http://www.detectright.com/'; |
|
31
|
|
|
|
|
32
|
|
|
protected $detectionCapabilities = [ |
|
33
|
|
|
|
|
34
|
|
|
'browser' => [ |
|
35
|
|
|
'name' => true, |
|
36
|
|
|
'version' => true, |
|
37
|
|
|
], |
|
38
|
|
|
|
|
39
|
|
|
'renderingEngine' => [ |
|
40
|
|
|
'name' => false, |
|
41
|
|
|
'version' => false, |
|
42
|
|
|
], |
|
43
|
|
|
|
|
44
|
|
|
'operatingSystem' => [ |
|
45
|
|
|
'name' => true, |
|
46
|
|
|
'version' => true, |
|
47
|
|
|
], |
|
48
|
|
|
|
|
49
|
|
|
'device' => [ |
|
50
|
|
|
'model' => true, |
|
51
|
|
|
'brand' => true, |
|
52
|
|
|
'type' => true, |
|
53
|
|
|
'isMobile' => false, |
|
54
|
|
|
'isTouch' => true, |
|
55
|
|
|
], |
|
56
|
|
|
|
|
57
|
|
|
'bot' => [ |
|
58
|
|
|
'isBot' => true, |
|
59
|
|
|
'name' => true, |
|
60
|
|
|
'type' => false, |
|
61
|
|
|
], |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
protected $defaultValues = [ |
|
65
|
|
|
'general' => [ |
|
66
|
|
|
'/^UserAgent$/', |
|
67
|
|
|
], |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
private $dataFile; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $dataFile |
|
75
|
|
|
* @throws PackageNotLoadedException |
|
76
|
|
|
* @throws InvalidArgumentException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct($dataFile) |
|
79
|
|
|
{ |
|
80
|
|
|
if (! class_exists('\DetectRight')) { |
|
81
|
|
|
throw new PackageNotLoadedException('You need to download and include the package by hand from ' . $this->getHomepage() . ' to use this provider'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (! file_exists($dataFile)) { |
|
85
|
|
|
throw new InvalidArgumentException('Data file not found ' . $dataFile); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->dataFile = $dataFile; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getDataFile() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->dataFile; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $resultRaw |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
private function hasResult(array $resultRaw) |
|
107
|
|
|
{ |
|
108
|
|
|
if (isset($resultRaw['internalid']) && $resultRaw['internalid'] !== 0) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (isset($resultRaw['device_type']) && $this->isRealResult($resultRaw['device_type'])) { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if (isset($resultRaw['mobile_browser']) && $this->isRealResult($resultRaw['mobile_browser'])) { |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (isset($resultRaw['device_os']) && $this->isRealResult($resultRaw['device_os'])) { |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (isset($resultRaw['brand_name']) && $this->isRealResult($resultRaw['brand_name'])) { |
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* |
|
133
|
|
|
* @param Model\Bot $bot |
|
134
|
|
|
* @param array $resultRaw |
|
135
|
|
|
*/ |
|
136
|
|
|
private function hydrateBot(Model\Bot $bot, $resultRaw) |
|
137
|
|
|
{ |
|
138
|
|
|
$bot->setIsBot(true); |
|
139
|
|
|
|
|
140
|
|
|
if (isset($resultRaw['model_name'])) { |
|
141
|
|
|
$bot->setName($this->getRealResult($resultRaw['model_name'])); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* |
|
147
|
|
|
* @param Model\Browser $browser |
|
148
|
|
|
* @param array $resultRaw |
|
149
|
|
|
*/ |
|
150
|
|
|
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
|
151
|
|
|
{ |
|
152
|
|
|
if (isset($resultRaw['mobile_browser'])) { |
|
153
|
|
|
$browser->setName($this->getRealResult($resultRaw['mobile_browser'])); |
|
154
|
|
|
} |
|
155
|
|
|
if (isset($resultRaw['mobile_browser_version'])) { |
|
156
|
|
|
$browser->getVersion()->setComplete($this->getRealResult($resultRaw['mobile_browser_version'])); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* |
|
162
|
|
|
* @param Model\OperatingSystem $os |
|
163
|
|
|
* @param array $resultRaw |
|
164
|
|
|
*/ |
|
165
|
|
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw) |
|
166
|
|
|
{ |
|
167
|
|
|
if (isset($resultRaw['device_os'])) { |
|
168
|
|
|
$os->setName($this->getRealResult($resultRaw['device_os'])); |
|
169
|
|
|
} |
|
170
|
|
|
if (isset($resultRaw['device_os_version'])) { |
|
171
|
|
|
$os->getVersion()->setComplete($this->getRealResult($resultRaw['device_os_version'])); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* |
|
177
|
|
|
* @param Model\Device $device |
|
178
|
|
|
* @param array $resultRaw |
|
179
|
|
|
*/ |
|
180
|
|
|
private function hydrateDevice(Model\Device $device, array $resultRaw) |
|
181
|
|
|
{ |
|
182
|
|
|
if (isset($resultRaw['brand_name']) && $this->isRealResult($resultRaw['brand_name'])) { |
|
183
|
|
|
$device->setBrand($this->getRealResult($resultRaw['brand_name'])); |
|
184
|
|
|
|
|
185
|
|
|
if (isset($resultRaw['model_name'])) { |
|
186
|
|
|
$device->setModel($this->getRealResult($resultRaw['model_name'])); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (isset($resultRaw['device_type'])) { |
|
191
|
|
|
$device->setType($this->getRealResult($resultRaw['device_type'])); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
if (isset($resultRaw['has_touchscreen']) && $resultRaw['has_touchscreen'] === 1) { |
|
195
|
|
|
$device->setIsTouch(true); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function parse($userAgent, array $headers = []) |
|
200
|
|
|
{ |
|
201
|
|
|
$headers['User-Agent'] = $userAgent; |
|
202
|
|
|
|
|
203
|
|
|
\DetectRight::initialize('DRSQLite//' . realpath($this->getDataFile())); |
|
204
|
|
|
|
|
205
|
|
|
\DetectRight::adaptiveProfileOnDeviceNotFound(); |
|
206
|
|
|
$resultRaw = \DetectRight::getProfileFromHeaders($headers); |
|
207
|
|
|
|
|
208
|
|
|
/* |
|
209
|
|
|
* No result found? |
|
210
|
|
|
*/ |
|
211
|
|
|
if ($this->hasResult($resultRaw) !== true) { |
|
212
|
|
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/* |
|
216
|
|
|
* Hydrate the model |
|
217
|
|
|
*/ |
|
218
|
|
|
$result = new Model\UserAgent(); |
|
219
|
|
|
$result->setProviderResultRaw($resultRaw); |
|
220
|
|
|
|
|
221
|
|
|
/* |
|
222
|
|
|
* Bot detection |
|
223
|
|
|
*/ |
|
224
|
|
|
if (isset($resultRaw['device_type']) && $resultRaw['device_type'] === 'Bot') { |
|
225
|
|
|
$this->hydrateBot($result->getBot(), $resultRaw); |
|
226
|
|
|
|
|
227
|
|
|
return $result; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/* |
|
231
|
|
|
* hydrate the result |
|
232
|
|
|
*/ |
|
233
|
|
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
|
234
|
|
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); |
|
235
|
|
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
|
236
|
|
|
|
|
237
|
|
|
return $result; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|