1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 1998-2015 Browser Capabilities Project |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a |
6
|
|
|
* copy of this software and associated documentation files (the "Software"), |
7
|
|
|
* to deal in the Software without restriction, including without limitation |
8
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
10
|
|
|
* Software is furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included |
13
|
|
|
* in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category Browscap-PHP |
24
|
|
|
* @package Helper |
25
|
|
|
* @copyright 1998-2015 Browser Capabilities Project |
26
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
27
|
|
|
* @link https://github.com/browscap/browscap-php/ |
28
|
|
|
* @since added with version 3.0 |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
namespace BrowscapPHP\Helper; |
32
|
|
|
|
33
|
|
|
use FileLoader\Exception as LoaderException; |
34
|
|
|
use FileLoader\Loader; |
35
|
|
|
use Psr\Log\LoggerInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* class to load the browscap.ini |
39
|
|
|
* |
40
|
|
|
* @category Browscap-PHP |
41
|
|
|
* @package Helper |
42
|
|
|
* @author Thomas Müller <[email protected]> |
43
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
44
|
|
|
* @version 3.0 |
45
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
46
|
|
|
* @link https://github.com/browscap/browscap-php/ |
47
|
|
|
*/ |
48
|
|
|
class IniLoader |
49
|
|
|
{ |
50
|
|
|
const PHP_INI_LITE = 'Lite_PHP_BrowscapINI'; |
51
|
|
|
const PHP_INI_FULL = 'Full_PHP_BrowscapINI'; |
52
|
|
|
const PHP_INI = 'PHP_BrowscapINI'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Options for update capabilities |
56
|
|
|
* |
57
|
|
|
* $remoteTimeUrl: The location to use to check out if a new version of the |
58
|
|
|
* browscap.ini file is available. |
59
|
|
|
* $remoteIniUrl: The location from which download the ini file. |
60
|
|
|
* The placeholder for the file should be represented by a %s. |
61
|
|
|
* $timeout: The timeout for the requests. |
62
|
|
|
*/ |
63
|
|
|
private $remoteIniUrl = 'http://browscap.org/stream?q=%q'; |
64
|
|
|
private $remoteTimeUrl = 'http://browscap.org/version'; |
65
|
|
|
private $remoteVersionUrl = 'http://browscap.org/version-number'; |
66
|
|
|
private $timeout = 5; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The path of the local version of the browscap.ini file from which to |
70
|
|
|
* update (to be set only if used). |
71
|
|
|
* |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private $localFile = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* an logger instance |
78
|
|
|
* |
79
|
|
|
* @var \Psr\Log\LoggerInterface |
80
|
|
|
*/ |
81
|
|
|
private $logger = null; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* an file loader instance |
85
|
|
|
* |
86
|
|
|
* @var \FileLoader\Loader |
87
|
|
|
*/ |
88
|
|
|
private $loader = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
private $remoteFilename = self::PHP_INI; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Options for the updater. The array should be overwritten, |
97
|
|
|
* containing all options as keys, set to the default value. |
98
|
|
|
* |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
private $options = array(); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* sets the logger |
105
|
|
|
* |
106
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
107
|
|
|
* |
108
|
|
|
* @return \BrowscapPHP\Helper\IniLoader |
109
|
|
|
*/ |
110
|
6 |
|
public function setLogger(LoggerInterface $logger) |
111
|
|
|
{ |
112
|
6 |
|
$this->logger = $logger; |
113
|
|
|
|
114
|
6 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* returns the logger |
119
|
|
|
* |
120
|
|
|
* @return \Psr\Log\LoggerInterface |
121
|
|
|
*/ |
122
|
|
|
public function getLogger() |
123
|
|
|
{ |
124
|
|
|
return $this->logger; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* sets the loader |
129
|
|
|
* |
130
|
|
|
* @param \FileLoader\Loader $loader |
131
|
|
|
* |
132
|
|
|
* @return \BrowscapPHP\Helper\IniLoader |
133
|
|
|
*/ |
134
|
6 |
|
public function setLoader(Loader $loader) |
135
|
|
|
{ |
136
|
6 |
|
$this->loader = $loader; |
137
|
|
|
|
138
|
6 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* creates the ini loader |
143
|
|
|
* |
144
|
|
|
* @return \FileLoader\Loader |
145
|
|
|
*/ |
146
|
11 |
|
public function getLoader() |
147
|
|
|
{ |
148
|
11 |
|
if (null === $this->loader) { |
149
|
5 |
|
$this->loader = new Loader(); |
150
|
|
|
} |
151
|
|
|
|
152
|
11 |
|
if (null !== $this->localFile) { |
153
|
3 |
|
$this->loader->setLocalFile($this->localFile); |
154
|
|
|
} |
155
|
|
|
|
156
|
11 |
|
$this->loader->setOptions($this->options); |
157
|
|
|
|
158
|
11 |
|
return $this->loader; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* sets the name of the local file |
163
|
|
|
* |
164
|
|
|
* @param string $filename the file name |
165
|
|
|
* |
166
|
|
|
* @throws \BrowscapPHP\Helper\Exception |
167
|
|
|
* @return \BrowscapPHP\Helper\IniLoader |
168
|
|
|
*/ |
169
|
6 |
|
public function setLocalFile($filename = null) |
170
|
|
|
{ |
171
|
6 |
|
if (empty($filename)) { |
172
|
2 |
|
throw new Exception( |
173
|
2 |
|
'the filename can not be empty', |
174
|
|
|
Exception::LOCAL_FILE_MISSING |
175
|
2 |
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
4 |
|
$this->localFile = $filename; |
179
|
|
|
|
180
|
4 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* sets the name of the local ini file |
185
|
|
|
* |
186
|
|
|
* @param string $name the file name |
187
|
|
|
* |
188
|
|
|
* @throws \BrowscapPHP\Helper\Exception |
189
|
|
|
* @return \BrowscapPHP\Helper\IniLoader |
190
|
|
|
*/ |
191
|
3 |
|
public function setRemoteFilename($name = null) |
192
|
|
|
{ |
193
|
3 |
|
if (empty($name)) { |
194
|
1 |
|
throw new Exception( |
195
|
1 |
|
'the filename can not be empty', |
196
|
|
|
Exception::INI_FILE_MISSING |
197
|
1 |
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
2 |
|
$this->remoteFilename = $name; |
201
|
|
|
|
202
|
2 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* returns the of the remote location for updating the ini file |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
7 |
|
public function getRemoteIniUrl() |
211
|
|
|
{ |
212
|
7 |
|
return str_replace('%q', $this->remoteFilename, $this->remoteIniUrl); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* returns the of the remote location for checking the version of the ini file |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
3 |
|
public function getRemoteTimeUrl() |
221
|
|
|
{ |
222
|
3 |
|
return $this->remoteTimeUrl; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* returns the of the remote location for checking the version of the ini file |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
3 |
|
public function getRemoteVersionUrl() |
231
|
|
|
{ |
232
|
3 |
|
return $this->remoteVersionUrl; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* returns the timeout |
237
|
|
|
* |
238
|
|
|
* @return integer |
239
|
|
|
*/ |
240
|
11 |
|
public function getTimeout() |
241
|
|
|
{ |
242
|
11 |
|
return $this->timeout; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Sets multiple loader options at once |
247
|
|
|
* |
248
|
|
|
* @param array $options |
249
|
|
|
* |
250
|
|
|
* @return IniLoader |
251
|
|
|
*/ |
252
|
1 |
|
public function setOptions(array $options) |
253
|
|
|
{ |
254
|
1 |
|
$this->options = $options; |
255
|
|
|
|
256
|
1 |
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* loads the ini file from a remote or local location and returns the content of the file |
261
|
|
|
* |
262
|
|
|
* @throws \BrowscapPHP\Helper\Exception |
263
|
|
|
* @return string the content of the loaded ini file |
264
|
|
|
*/ |
265
|
5 |
View Code Duplication |
public function load() |
|
|
|
|
266
|
|
|
{ |
267
|
5 |
|
$internalLoader = $this->getLoader(); |
268
|
|
|
$internalLoader |
269
|
5 |
|
->setRemoteDataUrl($this->getRemoteIniUrl()) |
270
|
5 |
|
->setTimeout($this->getTimeout()) |
271
|
|
|
; |
272
|
|
|
|
273
|
|
|
try { |
274
|
|
|
// Get updated .ini file |
275
|
5 |
|
return $internalLoader->load(); |
276
|
2 |
|
} catch (LoaderException $exception) { |
277
|
2 |
|
throw new Exception('could not load the data file', 0, $exception); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the remote file update timestamp |
283
|
|
|
* |
284
|
|
|
* @throws \BrowscapPHP\Helper\Exception |
285
|
|
|
* @return integer the remote modification timestamp |
286
|
|
|
*/ |
287
|
2 |
|
public function getMTime() |
288
|
|
|
{ |
289
|
2 |
|
$internalLoader = $this->getLoader(); |
290
|
|
|
$internalLoader |
291
|
2 |
|
->setRemoteVerUrl($this->getRemoteTimeUrl()) |
292
|
2 |
|
->setTimeout($this->getTimeout()) |
293
|
|
|
; |
294
|
|
|
|
295
|
|
|
try { |
296
|
|
|
// Get updated timestamp |
297
|
2 |
|
$remoteDatetime = $internalLoader->getMTime(); |
298
|
2 |
|
} catch (LoaderException $exception) { |
299
|
1 |
|
throw new Exception('could not load the new remote time', 0, $exception); |
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
$rfcDate = \DateTime::createFromFormat(\DateTime::RFC2822, $remoteDatetime); |
303
|
1 |
|
$timestamp = ($rfcDate instanceof \DateTime ? $rfcDate->getTimestamp() : (int) $remoteDatetime); |
304
|
|
|
|
305
|
1 |
|
if (!$timestamp) { |
306
|
|
|
throw new Exception('Bad datetime format from ' . $this->getRemoteTimeUrl(), Exception::INVALID_DATETIME); |
307
|
|
|
} |
308
|
|
|
|
309
|
1 |
|
return $timestamp; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Gets the remote file update timestamp |
314
|
|
|
* |
315
|
|
|
* @throws \BrowscapPHP\Helper\Exception |
316
|
|
|
* @return integer the remote modification timestamp |
317
|
|
|
*/ |
318
|
3 |
View Code Duplication |
public function getRemoteVersion() |
|
|
|
|
319
|
|
|
{ |
320
|
3 |
|
$internalLoader = $this->getLoader(); |
321
|
|
|
$internalLoader |
322
|
3 |
|
->setRemoteVerUrl($this->getRemoteVersionUrl()) |
323
|
3 |
|
->setTimeout($this->getTimeout()) |
324
|
|
|
; |
325
|
|
|
|
326
|
|
|
try { |
327
|
|
|
// Get updated timestamp |
328
|
3 |
|
return (int) $internalLoader->getMTime(); |
329
|
1 |
|
} catch (LoaderException $exception) { |
330
|
1 |
|
throw new Exception('could not load the new version', 0, $exception); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.