This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
update.php
508 lines (406 loc) · 11.1 KB
/
update.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/**
* @package Joomla.Cli
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
/**
* Manage and Update Joomla installation and extensions
*
* Called with --core: php update.php --core
* Updates the core
*
* Called with --extensions: php update.php --extensions
* Updates all extensions
*
* Called with --extension: php update.php --extension=extension_id (int)
* Updates the extension with the given id
*
* Called with --sitename: php update.php --sitename
* Outputs the sitename from the configuration.php as json object
*
* Called with --info: php update.php --info
* Outputs json encoded informations about installed extensions and
* available extensions
*
* Called with --installpackage: php update.php --installpackage=package.zip (path)
* Installs archived extension package that has to be placed in the tmp folder
*
*
* Called with --installurl: php update.php --installurl=url/package.zip (url)
* Installs archived extension package from a URL
*
* Called with --remove: php update.php --remove=extension_id (int)
* Removes the extension with the given id
*/
if (php_sapi_name() != 'cli')
{
exit(1);
}
// We are a valid entry point.
const _JEXEC = 1;
/**
* Set initial value for debug to improve "undefined notice" in fof.
* Do not change this value at this point, because fof causes a fatal error
* calling to a member function logAddLogger() on null.
*/
const JDEBUG = 0;
// Define core extension id
const CORE_EXTENSION_ID = 700;
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// Load the JApplicationCli class
JLoader::import('joomla.application.cli');
JLoader::import('joomla.application.component.helper');
JLoader::import('joomla.filesystem.folder');
JLoader::import('joomla.filesystem.file');
/**
* Manage Joomla extensions and core on the commandline
*
* @since 3.5.1
*/
class JoomlaCliUpdate extends JApplicationCli
{
/**
* The Installer Model
*
* @var InstallerModelUpdate
*/
protected $updater = null;
/**
* Joomla! Site Application
*
* @var JApplicationSite
*/
protected $app = null;
/**
* Entry point for the script
*
* @return void
*/
public function doExecute()
{
$_SERVER['HTTP_HOST'] = 'localhost';
$this->app = JFactory::getApplication('site');
if ($this->input->get('sitename', false))
{
$this->out($this->getSiteInfo());
return;
}
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_installer/models');
$this->updater = JModelLegacy::getInstance('Update', 'InstallerModel');
if ($this->input->get('core', false))
{
$this->out(json_encode(array('700' => $this->updateCore())));
return;
}
if ($this->input->get('info', false))
{
$this->out(json_encode($this->infoInstalledVersions()));
return;
}
if ($this->input->get('extensions', false))
{
$this->out(json_encode($this->updateExtensions()));
return;
}
$extension_id = $this->input->get('extension', false, 'INTEGER');
if (!empty($extension_id))
{
$this->out(json_encode($this->updateExtension($extension_id)));
return;
}
$installPackage = $this->input->get('installpackage', '', 'PATH');
if (!empty($installPackage))
{
$this->out(json_encode(array('result' => $this->installExtension($installPackage, 'folder'))));
return;
}
$installUrl = $this->input->get('installurl', '', 'STRING');
if (!empty($installUrl))
{
$this->out(json_encode(array('result' => $this->installExtension($installUrl, 'url'))));
return;
}
$remove = $this->input->get('remove', '');
if ($remove != '')
{
return $this->removeExtension($remove);
}
}
/**
* Remove an extension
*
* @param int $param Extension id
*
* @return bool
*/
protected function removeExtension($param)
{
$id = (int) $param;
$result = true;
$installer = JInstaller::getInstance();
$row = JTable::getInstance('extension');
$row->load($id);
if ($row->type && $row->type != 'language')
{
$result = $installer->uninstall($row->type, $id);
}
return $result;
}
/**
* Installs an extension (From tmp_path or URL)
*
* @param string $filename
* @param string $method
*
* @return bool
*/
public function installExtension($filename, $method)
{
if ($method == 'url')
{
$filename = JInstallerHelper::downloadPackage($filename);
}
$tmp_path = $this->app->get('tmp_path');
$path = $tmp_path . '/' . basename($filename);
$package = JInstallerHelper::unpack($path, true);
if ($package['type'] === false)
{
return false;
}
$jInstaller = JInstaller::getInstance();
$result = $jInstaller->install($package['extractdir']);
JInstallerHelper::cleanupInstall($path, $package['extractdir']);
return $result;
}
/**
* Gets the Information about all installed extensions from the database and checked the database.
* Outputs to the cli as json string
*
* @return void
*/
public function infoInstalledVersions()
{
$lang = JFactory::getLanguage();
$langFiles = $this->getLanguageFiles();
foreach ($langFiles as $file)
{
$file = str_replace(array('en-GB.', '.ini'), '', $file);
$lang->load($file, JPATH_ADMINISTRATOR, 'en-GB', true, false);
}
// Get All extensions
$extensions = $this->getAllExtensions();
$this->updater->purge();
$this->findUpdates(0);
$updates = $this->getUpdates();
$toUpdate = array();
$upToDate = array();
foreach ($extensions as &$extension)
{
$extension['name'] = JText::_($extension['name']);
if (array_key_exists($extension['extension_id'], $updates))
{
$tmp = $extension;
$tmp['currentVersion'] = json_decode($tmp['manifest_cache'], true)['version'];
$tmp['newVersion'] = $updates[$tmp['extension_id']]['version'];
$tmp['needsUpdate'] = true;
$toUpdate[] = $tmp;
}
else
{
$tmp = $extension;
$tmp['currentVersion'] = json_decode($tmp['manifest_cache'], true)['version'];
$tmp['newVersion'] = $tmp['currentVersion'];
$tmp['needsUpdate'] = false;
$upToDate[] = $tmp;
}
}
return array_merge($toUpdate, $upToDate);
}
/**
* Get the list of available language sys files
*
* @return array List of languages
*/
private function getLanguageFiles()
{
return JFolder::files(JPATH_ADMINISTRATOR . '/language/en-GB/', '\.sys\.ini');
}
/**
* Update Core Joomla
*
* @return bool success
*/
public function updateCore()
{
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_joomlaupdate/models');
$jUpdate = JModelLegacy::getInstance('Default', 'JoomlaupdateModel');
$jUpdate->purge();
$jUpdate->refreshUpdates(true);
$updateInformation = $jUpdate->getUpdateInformation();
if (!empty($updateInformation['hasUpdate']))
{
$packagefile = JInstallerHelper::downloadPackage($updateInformation['object']->downloadurl->_data);
$tmp_path = $this->app->get('tmp_path');
$packagefile = $tmp_path . '/' . $packagefile;
$package = JInstallerHelper::unpack($packagefile, true);
JFolder::copy($package['extractdir'], JPATH_BASE, '', true);
$result = $jUpdate->finaliseUpgrade();
if ($result)
{
// Remove the xml
if (file_exists(JPATH_BASE . '/joomla.xml'))
{
JFile::delete(JPATH_BASE . '/joomla.xml');
}
JInstallerHelper::cleanupInstall($packagefile, $package['extractdir']);
return true;
}
}
return false;
}
/**
* Update a single extension
*
* @param int $eid - The extension_id
*
* @return bool success
*/
public function updateExtension($eid)
{
$this->updater->purge();
$this->findUpdates($eid);
$update_id = $this->getUpdateIds($eid);
$this->updater->update($update_id);
$result = $this->updater->getState('result');
return $result;
}
/**
* Update Extensions
*
* @return array Array with success information for each extension
*/
public function updateExtensions()
{
$this->updater->purge();
$this->findUpdates();
// Get the objects
$extensions = $this->getUpdateIds();
$result = array();
foreach ($extensions as $e)
{
// Check if ist core or extension
if ($e->extension_id == CORE_EXTENSION_ID)
{
$result[$e->extension_id] = $this->updateCore();
}
else
{
$this->updater->update([$e->update_id]);
$result[$e->extension_id] = $this->updater->getState('result');
}
}
return $result;
}
/**
* Find updates
*
* @param int $eid The extension id
*
* @return void
*/
private function findUpdates($eid = 0)
{
$updater = JUpdater::getInstance();
// Fills potential updates into the table '#__updates for ALL extensions
$updater->findUpdates($eid);
}
/**
* Get the update
*
* @param int|null $eid The extenion id or null for all
*
* @return object|array
*/
private function getUpdateIds($eid = null)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('update_id, extension_id')
->from('#__updates')
->where($db->qn('extension_id') . ' <> 0');
if (!is_null($eid))
{
$query->where($db->qn('extension_id') . ' = ' . $db->q($eid));
}
$db->setQuery($query);
$result = $db->loadObjectList();
if (!$result)
{
return array();
}
if ($eid)
{
// Return only update id
return (array) $result[0]->update_id;
}
return $result;
}
/**
* Get available updates from #__updates
*
* @return array AssocList with available updates
*/
private function getUpdates()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__updates')
->where($db->qn('extension_id') . ' <> 0');
$db->setQuery($query);
return $db->loadAssocList('extension_id');
}
/**
* Get all extensions
*
* @return array AssocList with all extensions from #__extensions
*/
private function getAllExtensions()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__extensions');
$db->setQuery($query);
return $db->loadAssocList('extension_id');
}
/**
* Get the sitename json encoded out of the Joomla config
*
* @return string The json encoded result
*/
public function getSiteInfo()
{
$info = new stdClass();
$info->sitename = JFactory::getApplication()->getCfg('sitename');
return json_encode($info);
}
}
JApplicationCli::getInstance('JoomlaCliUpdate')->execute();