update.php
16 KB
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* @package Joomla.Administrator
* @subpackage com_installer
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
jimport('joomla.updater.update');
use Joomla\Utilities\ArrayHelper;
use Joomla\CMS\Installer\InstallerHelper;
/**
* Installer Update Model
*
* @since 1.6
*/
class InstallerModelUpdate extends JModelList
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'name', 'u.name',
'client_id', 'u.client_id', 'client_translated',
'type', 'u.type', 'type_translated',
'folder', 'u.folder', 'folder_translated',
'extension_id', 'u.extension_id',
);
}
parent::__construct($config);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = 'u.name', $direction = 'asc')
{
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
$this->setState('filter.client_id', $this->getUserStateFromRequest($this->context . '.filter.client_id', 'filter_client_id', null, 'int'));
$this->setState('filter.type', $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string'));
$this->setState('filter.folder', $this->getUserStateFromRequest($this->context . '.filter.folder', 'filter_folder', '', 'string'));
$app = JFactory::getApplication();
$this->setState('message', $app->getUserState('com_installer.message'));
$this->setState('extension_message', $app->getUserState('com_installer.extension_message'));
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
parent::populateState($ordering, $direction);
}
/**
* Method to get the database query
*
* @return JDatabaseQuery The database query
*
* @since 1.6
*/
protected function getListQuery()
{
$db = $this->getDbo();
// Grab updates ignoring new installs
$query = $db->getQuery(true)
->select('u.*')
->select($db->quoteName('e.manifest_cache'))
->from($db->quoteName('#__updates', 'u'))
->join('LEFT', $db->quoteName('#__extensions', 'e') . ' ON ' . $db->quoteName('e.extension_id') . ' = ' . $db->quoteName('u.extension_id'))
->where($db->quoteName('u.extension_id') . ' != ' . $db->quote(0));
// Process select filters.
$clientId = $this->getState('filter.client_id');
$type = $this->getState('filter.type');
$folder = $this->getState('filter.folder');
$extensionId = $this->getState('filter.extension_id');
if ($type)
{
$query->where($db->quoteName('u.type') . ' = ' . $db->quote($type));
}
if ($clientId != '')
{
$query->where($db->quoteName('u.client_id') . ' = ' . (int) $clientId);
}
if ($folder != '' && in_array($type, array('plugin', 'library', '')))
{
$query->where($db->quoteName('u.folder') . ' = ' . $db->quote($folder == '*' ? '' : $folder));
}
if ($extensionId)
{
$query->where($db->quoteName('u.extension_id') . ' = ' . $db->quote((int) $extensionId));
}
else
{
$query->where($db->quoteName('u.extension_id') . ' != ' . $db->quote(0))
->where($db->quoteName('u.extension_id') . ' != ' . $db->quote(700));
}
// Process search filter.
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'eid:') !== false)
{
$query->where($db->quoteName('u.extension_id') . ' = ' . (int) substr($search, 4));
}
else
{
if (stripos($search, 'uid:') !== false)
{
$query->where($db->quoteName('u.update_site_id') . ' = ' . (int) substr($search, 4));
}
elseif (stripos($search, 'id:') !== false)
{
$query->where($db->quoteName('u.update_id') . ' = ' . (int) substr($search, 3));
}
else
{
$query->where($db->quoteName('u.name') . ' LIKE ' . $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true)) . '%'));
}
}
}
return $query;
}
/**
* Translate a list of objects
*
* @param array $items The array of objects
*
* @return array The array of translated objects
*
* @since 3.5
*/
protected function translate(&$items)
{
foreach ($items as &$item)
{
$item->client_translated = $item->client_id ? JText::_('JADMINISTRATOR') : JText::_('JSITE');
$manifest = json_decode($item->manifest_cache);
$item->current_version = isset($manifest->version) ? $manifest->version : JText::_('JLIB_UNKNOWN');
$item->type_translated = JText::_('COM_INSTALLER_TYPE_' . strtoupper($item->type));
$item->folder_translated = $item->folder ?: JText::_('COM_INSTALLER_TYPE_NONAPPLICABLE');
$item->install_type = $item->extension_id ? JText::_('COM_INSTALLER_MSG_UPDATE_UPDATE') : JText::_('COM_INSTALLER_NEW_INSTALL');
}
return $items;
}
/**
* Returns an object list
*
* @param string $query The query
* @param int $limitstart Offset
* @param int $limit The number of records
*
* @return array
*
* @since 3.5
*/
protected function _getList($query, $limitstart = 0, $limit = 0)
{
$db = $this->getDbo();
$listOrder = $this->getState('list.ordering', 'u.name');
$listDirn = $this->getState('list.direction', 'asc');
// Process ordering.
if (in_array($listOrder, array('client_translated', 'folder_translated', 'type_translated')))
{
$db->setQuery($query);
$result = $db->loadObjectList();
$this->translate($result);
$result = ArrayHelper::sortObjects($result, $listOrder, strtolower($listDirn) === 'desc' ? -1 : 1, true, true);
$total = count($result);
if ($total < $limitstart)
{
$limitstart = 0;
$this->setState('list.start', 0);
}
return array_slice($result, $limitstart, $limit ?: null);
}
else
{
$query->order($db->quoteName($listOrder) . ' ' . $db->escape($listDirn));
$result = parent::_getList($query, $limitstart, $limit);
$this->translate($result);
return $result;
}
}
/**
* Get the count of disabled update sites
*
* @return integer
*
* @since 3.4
*/
public function getDisabledUpdateSites()
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__update_sites'))
->where($db->quoteName('enabled') . ' = 0');
$db->setQuery($query);
return $db->loadResult();
}
/**
* Finds updates for an extension.
*
* @param int $eid Extension identifier to look for
* @param int $cache_timeout Cache timout
* @param int $minimum_stability Minimum stability for updates {@see JUpdater} (0=dev, 1=alpha, 2=beta, 3=rc, 4=stable)
*
* @return boolean Result
*
* @since 1.6
*/
public function findUpdates($eid = 0, $cache_timeout = 0, $minimum_stability = JUpdater::STABILITY_STABLE)
{
JUpdater::getInstance()->findUpdates($eid, $cache_timeout, $minimum_stability);
return true;
}
/**
* Removes all of the updates from the table.
*
* @return boolean result of operation
*
* @since 1.6
*/
public function purge()
{
$db = $this->getDbo();
// Note: TRUNCATE is a DDL operation
// This may or may not mean depending on your database
$db->setQuery('TRUNCATE TABLE #__updates');
try
{
$db->execute();
}
catch (JDatabaseExceptionExecuting $e)
{
$this->_message = JText::_('JLIB_INSTALLER_FAILED_TO_PURGE_UPDATES');
return false;
}
// Reset the last update check timestamp
$query = $db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('last_check_timestamp') . ' = ' . $db->quote(0));
$db->setQuery($query);
$db->execute();
$this->_message = JText::_('JLIB_INSTALLER_PURGED_UPDATES');
return true;
}
/**
* Enables any disabled rows in #__update_sites table
*
* @return boolean result of operation
*
* @since 1.6
*/
public function enableSites()
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->update($db->quoteName('#__update_sites'))
->set($db->quoteName('enabled') . ' = 1')
->where($db->quoteName('enabled') . ' = 0');
$db->setQuery($query);
try
{
$db->execute();
}
catch (JDatabaseExceptionExecuting $e)
{
$this->_message .= JText::_('COM_INSTALLER_FAILED_TO_ENABLE_UPDATES');
return false;
}
if ($rows = $db->getAffectedRows())
{
$this->_message .= JText::plural('COM_INSTALLER_ENABLED_UPDATES', $rows);
}
return true;
}
/**
* Update function.
*
* Sets the "result" state with the result of the operation.
*
* @param array $uids Array[int] List of updates to apply
* @param int $minimum_stability The minimum allowed stability for installed updates {@see JUpdater}
*
* @return void
*
* @since 1.6
*/
public function update($uids, $minimum_stability = JUpdater::STABILITY_STABLE)
{
$result = true;
foreach ($uids as $uid)
{
$update = new JUpdate;
$instance = JTable::getInstance('update');
$instance->load($uid);
$update->loadFromXml($instance->detailsurl, $minimum_stability);
$update->set('extra_query', $instance->extra_query);
$this->preparePreUpdate($update, $instance);
// Install sets state and enqueues messages
$res = $this->install($update);
if ($res)
{
$instance->delete($uid);
}
$result = $res & $result;
}
// Clear the cached extension data and menu cache
$this->cleanCache('_system', 0);
$this->cleanCache('_system', 1);
$this->cleanCache('com_modules', 0);
$this->cleanCache('com_modules', 1);
$this->cleanCache('com_plugins', 0);
$this->cleanCache('com_plugins', 1);
$this->cleanCache('mod_menu', 0);
$this->cleanCache('mod_menu', 1);
// Set the final state
$this->setState('result', $result);
}
/**
* Handles the actual update installation.
*
* @param JUpdate $update An update definition
*
* @return boolean Result of install
*
* @since 1.6
*/
private function install($update)
{
$app = JFactory::getApplication();
if (!isset($update->get('downloadurl')->_data))
{
JError::raiseWarning('', JText::_('COM_INSTALLER_INVALID_EXTENSION_UPDATE'));
return false;
}
$url = trim($update->downloadurl->_data);
$sources = $update->get('downloadSources', array());
if ($extra_query = $update->get('extra_query'))
{
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= $extra_query;
}
$mirror = 0;
while (!($p_file = InstallerHelper::downloadPackage($url)) && isset($sources[$mirror]))
{
$name = $sources[$mirror];
$url = trim($name->url);
if ($extra_query)
{
$url .= (strpos($url, '?') === false) ? '?' : '&';
$url .= $extra_query;
}
$mirror++;
}
// Was the package downloaded?
if (!$p_file)
{
JError::raiseWarning('', JText::sprintf('COM_INSTALLER_PACKAGE_DOWNLOAD_FAILED', $url));
return false;
}
$config = JFactory::getConfig();
$tmp_dest = $config->get('tmp_path');
// Unpack the downloaded package file
$package = InstallerHelper::unpack($tmp_dest . '/' . $p_file);
// Get an installer instance
$installer = JInstaller::getInstance();
$update->set('type', $package['type']);
// Check the package
$check = InstallerHelper::isChecksumValid($package['packagefile'], $update);
// The validation was not successful. Just a warning for now.
// TODO: In Joomla 4 this will abort the installation
if ($check === InstallerHelper::HASH_NOT_VALIDATED)
{
$app->enqueueMessage(JText::_('COM_INSTALLER_INSTALL_CHECKSUM_WRONG'), 'error');
}
// Install the package
if (!$installer->update($package['dir']))
{
// There was an error updating the package
$app->enqueueMessage(
JText::sprintf('COM_INSTALLER_MSG_UPDATE_ERROR',
JText::_('COM_INSTALLER_TYPE_TYPE_' . strtoupper($package['type']))
), 'error'
);
$result = false;
}
else
{
// Package updated successfully
$app->enqueueMessage(
JText::sprintf('COM_INSTALLER_MSG_UPDATE_SUCCESS',
JText::_('COM_INSTALLER_TYPE_TYPE_' . strtoupper($package['type']))
)
);
$result = true;
}
// Quick change
$this->type = $package['type'];
// TODO: Reconfigure this code when you have more battery life left
$this->setState('name', $installer->get('name'));
$this->setState('result', $result);
$app->setUserState('com_installer.message', $installer->message);
$app->setUserState('com_installer.extension_message', $installer->get('extension_message'));
// Cleanup the install files
if (!is_file($package['packagefile']))
{
$config = JFactory::getConfig();
$package['packagefile'] = $config->get('tmp_path') . '/' . $package['packagefile'];
}
InstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);
return $result;
}
/**
* Method to get the row form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return mixed A JForm object on success, false on failure
*
* @since 2.5.2
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
$form = JForm::getInstance('com_installer.update', 'update', array('load_data' => $loadData));
// Check for an error.
if ($form == false)
{
$this->setError($form->getMessage());
return false;
}
// Check the session for previously entered form data.
$data = $this->loadFormData();
// Bind the form data if present.
if (!empty($data))
{
$form->bind($data);
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 2.5.2
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState($this->context, array());
return $data;
}
/**
* Method to add parameters to the update
*
* @param JUpdate $update An update definition
* @param JTableUpdate $table The update instance from the database
*
* @return void
*
* @since 3.7.0
*/
protected function preparePreUpdate($update, $table)
{
jimport('joomla.filesystem.file');
switch ($table->type)
{
// Components could have a helper which adds additional data
case 'component':
$ename = str_replace('com_', '', $table->element);
$fname = $ename . '.php';
$cname = ucfirst($ename) . 'Helper';
$path = JPATH_ADMINISTRATOR . '/components/' . $table->element . '/helpers/' . $fname;
if (JFile::exists($path))
{
require_once $path;
if (class_exists($cname) && is_callable(array($cname, 'prepareUpdate')))
{
call_user_func_array(array($cname, 'prepareUpdate'), array(&$update, &$table));
}
}
break;
// Modules could have a helper which adds additional data
case 'module':
$cname = str_replace('_', '', $table->element) . 'Helper';
$path = ($table->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/modules/' . $table->element . '/helper.php';
if (JFile::exists($path))
{
require_once $path;
if (class_exists($cname) && is_callable(array($cname, 'prepareUpdate')))
{
call_user_func_array(array($cname, 'prepareUpdate'), array(&$update, &$table));
}
}
break;
// If we have a plugin, we can use the plugin trigger "onInstallerBeforePackageDownload"
// But we should make sure, that our plugin is loaded, so we don't need a second "installer" plugin
case 'plugin':
$cname = str_replace('plg_', '', $table->element);
JPluginHelper::importPlugin($table->folder, $cname);
break;
}
}
}