1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
<?php
/**
*
* @package extension
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\extension;
/**
* Class to handle version checking and comparison for the extensions
*/
class version_helper
{
/**
* @var string Host
*/
protected $host = '';
/**
* @var string Path to file
*/
protected $path = '';
/**
* @var string File name
*/
protected $file = '';
/**
* @var string Extension name
*/
protected $extension = '';
/**
* @var string Current version installed
*/
protected $current_version = '';
/** @var \phpbb\cache\service */
protected $cache;
/** @var \phpbb\user */
protected $user;
/**
* @var array Metadata of the latest versio
*/
protected $latest_version_metadata;
/**
* Constructor
*
* @param \phpbb\cache\service $cache
* @param \phpbb\user $user
*/
public function __construct(\phpbb\cache\service $cache, \phpbb\user $user)
{
$this->cache = $cache;
$this->user = $user;
}
/**
* Set the informations concerning the current version from the metadata
*
* @param array $metadata
* @throws \RuntimeException
*/
public function set_metadata($metadata)
{
if (! isset($metadata['extra']['version-check']))
{
throw new \RuntimeException($this->user->lang('NO_VERSIONCHECK'));
}
$meta_vc = $metadata['extra']['version-check'];
$this->set_file_location($meta_vc['host'], $meta_vc['directory'], $meta_vc['filename']);
$this->set_extension($metadata['name']);
$this->set_current_version($metadata['version']);
}
/**
* Set the name of the extention
*
* @param string Name of the extension
* @return version_helper
*/
public function set_extension($extension)
{
$this->extension = $extension;
return $this;
}
/**
* Set location to the file
*
* @param string $host Host (e.g. version.phpbb.com)
* @param string $path Path to file (e.g. /phpbb)
* @param string $file File name (Default: composer.json)
* @return version_helper
*/
public function set_file_location($host, $path, $file = 'composer.json')
{
$this->host = $host;
$this->path = $path;
$this->file = $file;
return $this;
}
/**
* Set current version
*
* @param string $version The current version
* @return version_helper
*/
public function set_current_version($version)
{
$this->current_version = $version;
return $this;
}
/**
* Wrapper for version_compare() that allows using uppercase A and B
* for alpha and beta releases.
*
* See http://www.php.net/manual/en/function.version-compare.php
*
* @param string $version1 First version number
* @param string $version2 Second version number
* @param string $operator Comparison operator (optional)
*
* @return mixed Boolean (true, false) if comparison operator is specified.
* Integer (-1, 0, 1) otherwise.
*/
public function compare($version1, $version2, $operator = null)
{
return phpbb_version_compare($version1, $version2, $operator);
}
/**
* Say if the extension is up to date or not
*
* The informations about the lastest version are retrieved if needed
*
* @return bool true if the version is up to date
* @throws \RuntimeException
*/
public function is_uptodate()
{
if (empty($this->latest_version_metadata))
{
$this->get_version();
}
return $this->compare($this->current_version, $this->latest_version_metadata['version'], '>=');
}
/**
* Return the latest version number
*
* @return The latest version number ready to be displayed.
*/
public function get_latest_version() {
if (empty($this->latest_version_metadata))
{
$this->get_version();
}
return htmlspecialchars($this->latest_version_metadata['version']);
}
/**
* Return the latest download link
*
* @return The latest download link if existed, an empty string otherwise.
*/
public function get_latest_download_link() {
if (empty($this->latest_version_metadata))
{
$this->get_version();
}
return isset($this->latest_version_metadata['extra']['download']) ? $this->latest_version_metadata['extra']['download']: '';
}
/**
* Return the latest announcement link
*
* @return The latest announcement link if existed, an empty string otherwise.
*/
public function get_latest_announcement_link() {
if (empty($this->latest_version_metadata))
{
$this->get_version();
}
return isset($this->latest_version_metadata['extra']['announcement']) ? $this->latest_version_metadata['extra']['announcement']: '';
}
/**
* Obtains the latest version information
*
* @param bool $force_update Ignores cached data. Defaults to false.
* @return string Version info
* @throws \RuntimeException
*/
public function get_version($force_update = false)
{
echo 'Force : ' . $force_update;
$cache_file = 'versioncheck_ext_' . $this->extension . ':' . $this->host . $this->path . $this->file;
$info = $this->cache->get($cache_file);
if ($info === false || $force_update)
{
$errstr = $errno = '';
$info = get_remote_file($this->host, $this->path, $this->file, $errstr, $errno);
if (!empty($errstr))
{
throw new \RuntimeException($errstr);
}
$info = json_decode($info, true);
if (empty($info['version']))
{
$this->user->add_lang('acp/common');
throw new \RuntimeException($this->user->lang('VERSIONCHECK_FAIL'));
}
// Replace & with & on announcement and download links
if (isset($info['extra']['announcement']))
{
$info['extra']['announcement'] = str_replace('&', '&', $info['extra']['announcement']);
}
if (isset($info['extra']['download']))
{
$info['extra']['download'] = str_replace('&', '&', $info['extra']['download']);
}
$this->cache->put($cache_file, $info, 86400); // 24 hours
}
$this->latest_version_metadata = $info;
return $info;
}
}
|