1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use DateTimeZone; |
6
|
|
|
use UserAgentParser\Exception; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
abstract class AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of the provider |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Homepage of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $homepage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Composer package name |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $packageName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Version string for caching |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $version; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Last update date |
41
|
|
|
* |
42
|
|
|
* @var DateTime |
43
|
|
|
*/ |
44
|
|
|
private $updateDate; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Per default the provider cannot detect anything |
48
|
|
|
* Activate them in $detectionCapabilities |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $allDetectionCapabilities = [ |
53
|
|
|
'browser' => [ |
54
|
|
|
'name' => false, |
55
|
|
|
'version' => false, |
56
|
|
|
], |
57
|
|
|
|
58
|
|
|
'renderingEngine' => [ |
59
|
|
|
'name' => false, |
60
|
|
|
'version' => false, |
61
|
|
|
], |
62
|
|
|
|
63
|
|
|
'operatingSystem' => [ |
64
|
|
|
'name' => false, |
65
|
|
|
'version' => false, |
66
|
|
|
], |
67
|
|
|
|
68
|
|
|
'device' => [ |
69
|
|
|
'model' => false, |
70
|
|
|
'brand' => false, |
71
|
|
|
'type' => false, |
72
|
|
|
'isMobile' => false, |
73
|
|
|
'isTouch' => false, |
74
|
|
|
], |
75
|
|
|
|
76
|
|
|
'bot' => [ |
77
|
|
|
'isBot' => false, |
78
|
|
|
'name' => false, |
79
|
|
|
'type' => false, |
80
|
|
|
], |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set this in each Provider implementation |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
protected $detectionCapabilities = []; |
89
|
|
|
|
90
|
|
|
protected $defaultValues = [ |
91
|
|
|
'general' => [], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the name of the provider |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function getName() |
100
|
|
|
{ |
101
|
1 |
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the homepage |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public function getHomepage() |
110
|
|
|
{ |
111
|
1 |
|
return $this->homepage; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the package name |
116
|
|
|
* |
117
|
|
|
* @return string null |
118
|
|
|
*/ |
119
|
5 |
|
public function getPackageName() |
120
|
|
|
{ |
121
|
5 |
|
return $this->packageName; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the version of the provider |
126
|
|
|
* |
127
|
|
|
* @return string null |
128
|
|
|
*/ |
129
|
2 |
|
public function getVersion() |
130
|
|
|
{ |
131
|
2 |
|
if ($this->version !== null) { |
132
|
1 |
|
return $this->version; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
if ($this->getPackageName() === null) { |
136
|
1 |
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
2 |
|
$packages = $this->getPackages(); |
140
|
|
|
|
141
|
2 |
|
if ($packages === null) { |
142
|
1 |
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
foreach ($packages as $package) { |
146
|
2 |
|
if ($package->name === $this->getPackageName()) { |
147
|
1 |
|
$this->version = $package->version; |
148
|
|
|
|
149
|
2 |
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
return $this->version; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the last change date of the provider |
158
|
|
|
* |
159
|
|
|
* @return DateTime null |
160
|
|
|
*/ |
161
|
2 |
|
public function getUpdateDate() |
162
|
|
|
{ |
163
|
2 |
|
if ($this->updateDate !== null) { |
164
|
1 |
|
return $this->updateDate; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
if ($this->getPackageName() === null) { |
168
|
1 |
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
2 |
|
$packages = $this->getPackages(); |
172
|
|
|
|
173
|
2 |
|
if ($packages === null) { |
174
|
1 |
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
2 |
|
foreach ($packages as $package) { |
178
|
2 |
|
if ($package->name === $this->getPackageName()) { |
179
|
1 |
|
$updateDate = new DateTime($package->time, new DateTimeZone('UTC')); |
180
|
|
|
|
181
|
1 |
|
$this->updateDate = $updateDate; |
182
|
|
|
|
183
|
2 |
|
break; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
2 |
|
return $this->updateDate; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
* @return array null |
193
|
|
|
*/ |
194
|
4 |
|
private function getPackages() |
195
|
|
|
{ |
196
|
4 |
|
if (! file_exists('composer.lock')) { |
197
|
2 |
|
return; |
198
|
|
|
} |
199
|
|
|
|
200
|
4 |
|
$content = file_get_contents('composer.lock'); |
201
|
4 |
|
if ($content === false || $content === '') { |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
4 |
|
$content = json_decode($content); |
206
|
|
|
|
207
|
4 |
|
return array_merge($content->packages, $content->{'packages-dev'}); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* What kind of capabilities this provider can detect |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
1 |
|
public function getDetectionCapabilities() |
216
|
|
|
{ |
217
|
1 |
|
return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* |
222
|
|
|
* @param mixed $value |
223
|
|
|
* @param string $group |
224
|
|
|
* @param string $part |
225
|
|
|
* @return boolean |
226
|
|
|
*/ |
227
|
2 |
|
protected function isRealResult($value, $group = null, $part = null) |
228
|
|
|
{ |
229
|
2 |
|
$value = (string) $value; |
230
|
2 |
|
$value = trim($value); |
231
|
|
|
|
232
|
2 |
|
if ($value === '') { |
233
|
1 |
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
2 |
|
$regexes = $this->defaultValues['general']; |
237
|
|
|
|
238
|
2 |
|
if ($group !== null && $part !== null && isset($this->defaultValues[$group][$part])) { |
239
|
1 |
|
$regexes = array_merge($regexes, $this->defaultValues[$group][$part]); |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
foreach ($regexes as $regex) { |
243
|
1 |
|
if (preg_match($regex, $value) === 1) { |
244
|
1 |
|
return false; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
2 |
|
return true; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Parse the given user agent and return a result if possible |
253
|
|
|
* |
254
|
|
|
* @param string $userAgent |
255
|
|
|
* @param array $headers |
256
|
|
|
* |
257
|
|
|
* @throws Exception\NoResultFoundException |
258
|
|
|
* |
259
|
|
|
* @return Model\UserAgent |
260
|
|
|
*/ |
261
|
|
|
abstract public function parse($userAgent, array $headers = []); |
262
|
|
|
} |
263
|
|
|
|