/
updater.php
272 lines (239 loc) · 6.71 KB
/
updater.php
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* @package Joomla.Platform
* @subpackage Updater
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.path');
jimport('joomla.base.adapter');
jimport('joomla.utilities.arrayhelper');
/**
* Updater Class
*
* @since 11.1
*/
class JUpdater extends JAdapter
{
/**
* Development snapshots, nightly builds, pre-release versions and so on
*
* @const integer
* @since 3.4
*/
const STABILITY_DEV = 0;
/**
* Alpha versions (work in progress, things are likely to be broken)
*
* @const integer
* @since 3.4
*/
const STABILITY_ALPHA = 1;
/**
* Beta versions (major functionality in place, show-stopper bugs are likely to be present)
*
* @const integer
* @since 3.4
*/
const STABILITY_BETA = 2;
/**
* Release Candidate versions (almost stable, minor bugs might be present)
*
* @const integer
* @since 3.4
*/
const STABILITY_RC = 3;
/**
* Stable versions (production quality code)
*
* @const integer
* @since 3.4
*/
const STABILITY_STABLE = 4;
/**
* @var JUpdater JUpdater instance container.
* @since 11.3
*/
protected static $instance;
/**
* Constructor
*
* @since 11.1
*/
public function __construct()
{
// Adapter base path, class prefix
parent::__construct(__DIR__, 'JUpdater');
}
/**
* Returns a reference to the global Installer object, only creating it
* if it doesn't already exist.
*
* @return JUpdater An installer object
*
* @since 11.1
*/
public static function getInstance()
{
if (!isset(self::$instance))
{
self::$instance = new JUpdater;
}
return self::$instance;
}
/**
* Finds an update for an extension
*
* @param int|array $eid Extension Identifier or list of Extension Identifiers; if zero use all sites
* @param integer $cacheTimeout How many seconds to cache update information; if zero, force reload the update information
* @param integer $minimum_stability Minimum stability for the updates; 0=dev, 1=alpha, 2=beta, 3=rc, 4=stable
*
* @return boolean True if there are updates
*
* @since 11.1
*/
public function findUpdates($eid = 0, $cacheTimeout = 0, $minimum_stability = self::STABILITY_STABLE)
{
$db = $this->getDBO();
$query = $db->getQuery(true);
$retval = false;
$query->select('DISTINCT a.update_site_id, a.type, a.location, a.last_check_timestamp, a.extra_query')
->from('#__update_sites AS a')
->where('a.enabled = 1');
if ($eid)
{
$query->join('INNER', '#__update_sites_extensions AS b ON a.update_site_id = b.update_site_id');
if (is_array($eid))
{
$query->where('b.extension_id IN (' . implode(',', $eid) . ')');
}
elseif ((int) $eid)
{
$query->where('b.extension_id = ' . $eid);
}
}
$db->setQuery($query);
$results = $db->loadAssocList();
$result_count = count($results);
$now = time();
for ($i = 0; $i < $result_count; $i++)
{
$result = &$results[$i];
$this->setAdapter($result['type']);
if (!isset($this->_adapters[$result['type']]))
{
// Ignore update sites requiring adapters we don't have installed
continue;
}
if ($cacheTimeout > 0)
{
if (isset($result['last_check_timestamp']) && ($now - $result['last_check_timestamp'] <= $cacheTimeout))
{
// Ignore update sites whose information we have fetched within
// the cache time limit
$retval = true;
continue;
}
}
$result['minimum_stability'] = $minimum_stability;
/** @var JUpdateAdapter $adapter */
$adapter = $this->_adapters[$result['type']];
$update_result = $adapter->findUpdate($result);
if (is_array($update_result))
{
if (array_key_exists('update_sites', $update_result) && count($update_result['update_sites']))
{
$results = JArrayHelper::arrayUnique(array_merge($results, $update_result['update_sites']));
$result_count = count($results);
}
if (array_key_exists('updates', $update_result) && count($update_result['updates']))
{
for ($k = 0, $count = count($update_result['updates']); $k < $count; $k++)
{
$current_update = &$update_result['updates'][$k];
$current_update->extra_query = $result['extra_query'];
$update = JTable::getInstance('update');
$extension = JTable::getInstance('extension');
$uid = $update
->find(
array(
'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
'client_id' => strtolower($current_update->get('client_id')),
'folder' => strtolower($current_update->get('folder'))
)
);
$eid = $extension
->find(
array(
'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
'client_id' => strtolower($current_update->get('client_id')),
'folder' => strtolower($current_update->get('folder'))
)
);
if (!$uid)
{
// Set the extension id
if ($eid)
{
// We have an installed extension, check the update is actually newer
$extension->load($eid);
$data = json_decode($extension->manifest_cache, true);
if (version_compare($current_update->version, $data['version'], '>') == 1)
{
$current_update->extension_id = $eid;
$current_update->store();
}
}
else
{
// A potentially new extension to be installed
$current_update->store();
}
}
else
{
$update->load($uid);
// If there is an update, check that the version is newer then replaces
if (version_compare($current_update->version, $update->version, '>') == 1)
{
$current_update->store();
}
}
}
}
}
// Finally, update the last update check timestamp
$query = $db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('last_check_timestamp') . ' = ' . $db->quote($now))
->where($db->quoteName('update_site_id') . ' = ' . $db->quote($result['update_site_id']));
$db->setQuery($query);
$db->execute();
}
return $retval;
}
/**
* Finds an update for an extension
*
* @param integer $id Id of the extension
*
* @return mixed
*
* @since 11.1
*/
public function update($id)
{
$updaterow = JTable::getInstance('update');
$updaterow->load($id);
$update = new JUpdate;
if ($update->loadFromXML($updaterow->detailsurl))
{
return $update->install();
}
return false;
}
}