- md5
- 2812216283e873d800e293ae2d6bc58f
- sha1
- 3b6630a59a9cede1412a3a9c6b732713c8cbb4bb
- sha256
- b830c86127b17097c89c25e9bc3a8aac6e13a7c626dd208cb7ad3cfd4c9ac416
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
<?php
/**
* @package Joomla.Administrator
* @subpackage com_installer
*
* @copyright (C) 2014 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Installer\Administrator\Model;
\defined('_JEXEC') or die;
use Exception;
use JDatabaseQuery;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Table\UpdateSite as UpdateSiteTable;
use Joomla\Component\Installer\Administrator\Helper\InstallerHelper;
use Joomla\Database\ParameterType;
use RuntimeException;
/**
* Installer Update Sites Model
*
* @since 3.4
*/
class UpdatesitesModel extends InstallerModel
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
* @param MVCFactoryInterface $factory The factory.
*
* @since 1.6
* @see \Joomla\CMS\MVC\Model\ListModel
*/
public function __construct($config = [], MVCFactoryInterface $factory = null)
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = [
'update_site_name',
'name',
'client_id',
'client',
'client_translated',
'status',
'type',
'type_translated',
'folder',
'folder_translated',
'update_site_id',
'enabled',
'supported'
];
}
parent::__construct($config, $factory);
}
/**
* Enable/Disable an extension.
*
* @param array $eid Extension ids to un/publish
* @param int $value Publish value
*
* @return boolean True on success
*
* @throws Exception on ACL error
* @since 3.4
*
*/
public function publish(&$eid = [], $value = 1)
{
if (!Factory::getUser()->authorise('core.edit.state', 'com_installer'))
{
throw new Exception(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), 403);
}
$result = true;
// Ensure eid is an array of extension ids
if (!is_array($eid))
{
$eid = [$eid];
}
// Get a table object for the extension type
$table = new UpdateSiteTable($this->getDbo());
// Enable the update site in the table and store it in the database
foreach ($eid as $i => $id)
{
$table->load($id);
$table->enabled = $value;
if (!$table->store())
{
$this->setError($table->getError());
$result = false;
}
}
return $result;
}
/**
* Deletes an update site.
*
* @param array $ids Extension ids to delete.
*
* @return void
*
* @throws Exception on ACL error
* @since 3.6
*
*/
public function delete($ids = [])
{
if (!Factory::getUser()->authorise('core.delete', 'com_installer'))
{
throw new Exception(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), 403);
}
// Ensure eid is an array of extension ids
if (!is_array($ids))
{
$ids = [$ids];
}
$db = $this->getDbo();
$app = Factory::getApplication();
$count = 0;
// Gets the update site names.
$query = $db->getQuery(true)
->select($db->quoteName(['update_site_id', 'name']))
->from($db->quoteName('#__update_sites'))
->whereIn($db->quoteName('update_site_id'), $ids);
$db->setQuery($query);
$updateSitesNames = $db->loadObjectList('update_site_id');
// Gets Joomla core update sites Ids.
$joomlaUpdateSitesIds = $this->getJoomlaUpdateSitesIds(0);
// Enable the update site in the table and store it in the database
foreach ($ids as $i => $id)
{
// Don't allow to delete Joomla Core update sites.
if (in_array((int) $id, $joomlaUpdateSitesIds))
{
$app->enqueueMessage(Text::sprintf('COM_INSTALLER_MSG_UPDATESITES_DELETE_CANNOT_DELETE', $updateSitesNames[$id]->name), 'error');
continue;
}
// Delete the update site from all tables.
try
{
$id = (int) $id;
$query = $db->getQuery(true)
->delete($db->quoteName('#__update_sites'))
->where($db->quoteName('update_site_id') . ' = :id')
->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
$db->execute();
$query = $db->getQuery(true)
->delete($db->quoteName('#__update_sites_extensions'))
->where($db->quoteName('update_site_id') . ' = :id')
->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
$db->execute();
$query = $db->getQuery(true)
->delete($db->quoteName('#__updates'))
->where($db->quoteName('update_site_id') . ' = :id')
->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
$db->execute();
$count++;
}
catch (RuntimeException $e)
{
$app->enqueueMessage(
Text::sprintf(
'COM_INSTALLER_MSG_UPDATESITES_DELETE_ERROR',
$updateSitesNames[$id]->name, $e->getMessage()
), 'error'
);
}
}
if ($count > 0)
{
$app->enqueueMessage(Text::plural('COM_INSTALLER_MSG_UPDATESITES_N_DELETE_UPDATESITES_DELETED', $count), 'message');
}
}
/**
* Fetch the Joomla update sites ids.
*
* @param integer $column Column to return. 0 for update site ids, 1 for extension ids.
*
* @return array Array with joomla core update site ids.
*
* @since 3.6.0
*/
protected function getJoomlaUpdateSitesIds($column = 0)
{
$db = $this->getDbo();
// Fetch the Joomla core update sites ids and their extension ids. We search for all except the core joomla extension with update sites.
$query = $db->getQuery(true)
->select($db->quoteName(['use.update_site_id', 'e.extension_id']))
->from($db->quoteName('#__update_sites_extensions', 'use'))
->join(
'LEFT',
$db->quoteName('#__update_sites', 'us'),
$db->quoteName('us.update_site_id') . ' = ' . $db->quoteName('use.update_site_id')
)
->join(
'LEFT',
$db->quoteName('#__extensions', 'e'),
$db->quoteName('e.extension_id') . ' = ' . $db->quoteName('use.extension_id')
)
->where('('
. '(' . $db->quoteName('e.type') . ' = ' . $db->quote('file') .
' AND ' . $db->quoteName('e.element') . ' = ' . $db->quote('joomla') . ')'
. ' OR (' . $db->quoteName('e.type') . ' = ' . $db->quote('package') . ' AND ' . $db->quoteName('e.element')
. ' = ' . $db->quote('pkg_en-GB') . ') OR (' . $db->quoteName('e.type') . ' = ' . $db->quote('component')
. ' AND ' . $db->quoteName('e.element') . ' = ' . $db->quote('com_joomlaupdate') . ')'
. ')'
);
$db->setQuery($query);
return $db->loadColumn($column);
}
/**
* Rebuild update sites tables.
*
* @return void
*
* @throws Exception on ACL error
* @since 3.6
*
*/
public function rebuild(): void
{
if (!Factory::getUser()->authorise('core.admin', 'com_installer'))
{
throw new Exception(Text::_('COM_INSTALLER_MSG_UPDATESITES_REBUILD_NOT_PERMITTED'), 403);
}
$db = $this->getDbo();
$app = Factory::getApplication();
// Check if Joomla Extension plugin is enabled.
if (!PluginHelper::isEnabled('extension', 'joomla'))
{
$query = $db->getQuery(true)
->select($db->quoteName('extension_id'))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('element') . ' = ' . $db->quote('joomla'))
->where($db->quoteName('folder') . ' = ' . $db->quote('extension'));
$db->setQuery($query);
$pluginId = (int) $db->loadResult();
$link = Route::_('index.php?option=com_plugins&task=plugin.edit&extension_id=' . $pluginId);
$app->enqueueMessage(Text::sprintf('COM_INSTALLER_MSG_UPDATESITES_REBUILD_EXTENSION_PLUGIN_NOT_ENABLED', $link), 'error');
return;
}
$clients = [JPATH_SITE, JPATH_ADMINISTRATOR, JPATH_API];
$extensionGroupFolders = ['components', 'modules', 'plugins', 'templates', 'language', 'manifests'];
$pathsToSearch = [];
// Identifies which folders to search for manifest files.
foreach ($clients as $clientPath)
{
foreach ($extensionGroupFolders as $extensionGroupFolderName)
{
// Components, modules, plugins, templates, languages and manifest (files, libraries, etc)
if ($extensionGroupFolderName !== 'plugins')
{
foreach (glob($clientPath . '/' . $extensionGroupFolderName . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $extensionFolderPath)
{
$pathsToSearch[] = $extensionFolderPath;
}
}
else
{
// Plugins (another directory level is needed)
foreach (glob($clientPath . '/' . $extensionGroupFolderName . '/*',
GLOB_NOSORT | GLOB_ONLYDIR
) as $pluginGroupFolderPath)
{
foreach (glob($pluginGroupFolderPath . '/*', GLOB_NOSORT | GLOB_ONLYDIR) as $extensionFolderPath)
{
$pathsToSearch[] = $extensionFolderPath;
}
}
}
}
}
// Gets Joomla core update sites Ids.
$joomlaUpdateSitesIds = $this->getJoomlaUpdateSitesIds(0);
// First backup any custom extra_query for the sites
$query = $db->getQuery(true)
->select('TRIM(' . $db->quoteName('location') . ') AS ' . $db->quoteName('location') . ', ' . $db->quoteName('extra_query'))
->from($db->quoteName('#__update_sites'));
$db->setQuery($query);
$backupExtraQuerys = $db->loadAssocList('location');
// Delete from all tables (except joomla core update sites).
$query = $db->getQuery(true)
->delete($db->quoteName('#__update_sites'))
->whereNotIn($db->quoteName('update_site_id'), $joomlaUpdateSitesIds);
$db->setQuery($query);
$db->execute();
$query = $db->getQuery(true)
->delete($db->quoteName('#__update_sites_extensions'))
->whereNotIn($db->quoteName('update_site_id'), $joomlaUpdateSitesIds);
$db->setQuery($query);
$db->execute();
$query = $db->getQuery(true)
->delete($db->quoteName('#__updates'))
->whereNotIn($db->quoteName('update_site_id'), $joomlaUpdateSitesIds);
$db->setQuery($query);
$db->execute();
$count = 0;
// Gets Joomla core extension Ids.
$joomlaCoreExtensionIds = $this->getJoomlaUpdateSitesIds(1);
// Search for updateservers in manifest files inside the folders to search.
foreach ($pathsToSearch as $extensionFolderPath)
{
$tmpInstaller = new Installer;
$tmpInstaller->setPath('source', $extensionFolderPath);
// Main folder manifests (higher priority)
$parentXmlfiles = Folder::files($tmpInstaller->getPath('source'), '.xml$', false, true);
// Search for children manifests (lower priority)
$allXmlFiles = Folder::files($tmpInstaller->getPath('source'), '.xml$', 1, true);
// Create a unique array of files ordered by priority
$xmlfiles = array_unique(array_merge($parentXmlfiles, $allXmlFiles));
if (!empty($xmlfiles))
{
foreach ($xmlfiles as $file)
{
// Is it a valid Joomla installation manifest file?
$manifest = $tmpInstaller->isManifest($file);
if ($manifest !== null)
{
/**
* Search if the extension exists in the extensions table. Excluding Joomla
* core extensions (id < 10000) and discovered extensions.
*/
$query = $db->getQuery(true)
->select($db->quoteName('extension_id'))
->from($db->quoteName('#__extensions'))
->where(
[
$db->quoteName('type') . ' = :type',
$db->quoteName('state') . ' != -1',
]
)
->extendWhere(
'AND',
[
$db->quoteName('name') . ' = :name',
$db->quoteName('name') . ' = :pkgname',
],
'OR'
)
->whereNotIn($db->quoteName('extension_id'), $joomlaCoreExtensionIds)
->bind(':name', $manifest->name)
->bind(':pkgname', $manifest->packagename)
->bind(':type', $manifest['type']);
$db->setQuery($query);
$eid = (int) $db->loadResult();
if ($eid && $manifest->updateservers)
{
// Set the manifest object and path
$tmpInstaller->manifest = $manifest;
$tmpInstaller->setPath('manifest', $file);
// Remove last extra_query as we are in a foreach
$tmpInstaller->extraQuery = '';
if ($tmpInstaller->manifest->updateservers
&& $tmpInstaller->manifest->updateservers->server
&& isset($backupExtraQuerys[trim((string) $tmpInstaller->manifest->updateservers->server)]))
{
$tmpInstaller->extraQuery = $backupExtraQuerys[trim((string) $tmpInstaller->manifest->updateservers->server)]['extra_query'];
}
// Load the extension plugin (if not loaded yet).
PluginHelper::importPlugin('extension', 'joomla');
// Fire the onExtensionAfterUpdate
$app->triggerEvent('onExtensionAfterUpdate', ['installer' => $tmpInstaller, 'eid' => $eid]);
$count++;
}
}
}
}
}
if ($count > 0)
{
$app->enqueueMessage(Text::_('COM_INSTALLER_MSG_UPDATESITES_REBUILD_SUCCESS'), 'message');
}
else
{
$app->enqueueMessage(Text::_('COM_INSTALLER_MSG_UPDATESITES_REBUILD_MESSAGE'), 'message');
}
// Flush the system cache to ensure extra_query is correctly loaded next time.
$this->cleanCache('_system');
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*
* @since 4.0.0
*/
public function getItems()
{
$items = parent::getItems();
array_walk($items,
static function ($item) {
$data = new CMSObject($item);
$item->downloadKey = InstallerHelper::getDownloadKey($data);
}
);
return $items;
}
/**
* 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 3.4
*/
protected function populateState($ordering = 'name', $direction = 'asc')
{
// Load the filter state.
$stateKeys = [
'search' => 'string',
'client_id' => 'int',
'enabled' => 'string',
'type' => 'string',
'folder' => 'string',
'supported' => 'int',
];
foreach ($stateKeys as $key => $filterType)
{
$stateKey = 'filter.' . $key;
switch ($filterType)
{
case 'int':
case 'bool':
$default = null;
break;
default:
$default = '';
break;
}
$stateValue = $this->getUserStateFromRequest(
$this->context . '.' . $stateKey, 'filter_' . $key, $default, $filterType
);
$this->setState($stateKey, $stateValue);
}
parent::populateState($ordering, $direction);
}
protected function getStoreId($id = '')
{
$id .= ':' . $this->getState('search');
$id .= ':' . $this->getState('client_id');
$id .= ':' . $this->getState('enabled');
$id .= ':' . $this->getState('type');
$id .= ':' . $this->getState('folder');
$id .= ':' . $this->getState('supported');
return parent::getStoreId($id);
}
/**
* Method to get the database query
*
* @return JDatabaseQuery The database query
*
* @since 3.4
*/
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select(
$db->quoteName(
[
's.update_site_id',
's.name',
's.type',
's.location',
's.enabled',
's.checked_out',
's.checked_out_time',
's.extra_query',
'e.extension_id',
'e.name',
'e.type',
'e.element',
'e.folder',
'e.client_id',
'e.state',
'e.manifest_cache',
'u.name'
],
[
'update_site_id',
'update_site_name',
'update_site_type',
'location',
'enabled',
'checked_out',
'checked_out_time',
'extra_query',
'extension_id',
'name',
'type',
'element',
'folder',
'client_id',
'state',
'manifest_cache',
'editor'
]
)
)
->from($db->quoteName('#__update_sites', 's'))
->join(
'INNER',
$db->quoteName('#__update_sites_extensions', 'se'),
$db->quoteName('se.update_site_id') . ' = ' . $db->quoteName('s.update_site_id')
)
->join(
'INNER',
$db->quoteName('#__extensions', 'e'),
$db->quoteName('e.extension_id') . ' = ' . $db->quoteName('se.extension_id')
)
->join(
'LEFT',
$db->quoteName('#__users', 'u'),
$db->quoteName('s.checked_out') . ' = ' . $db->quoteName('u.id')
)
->where($db->quoteName('state') . ' = 0');
// Process select filters.
$supported = $this->getState('filter.supported');
$enabled = $this->getState('filter.enabled');
$type = $this->getState('filter.type');
$clientId = $this->getState('filter.client_id');
$folder = $this->getState('filter.folder');
if ($enabled !== '')
{
$enabled = (int) $enabled;
$query->where($db->quoteName('s.enabled') . ' = :enabled')
->bind(':enabled', $enabled, ParameterType::INTEGER);
}
if ($type)
{
$query->where($db->quoteName('e.type') . ' = :type')
->bind(':type', $type);
}
if ($clientId !== null && $clientId !== '')
{
$clientId = (int) $clientId;
$query->where($db->quoteName('e.client_id') . ' = :clientId')
->bind(':clientId', $clientId, ParameterType::INTEGER);
}
if ($folder !== '' && in_array($type, ['plugin', 'library', ''], true))
{
$folderForBinding = $folder === '*' ? '' : $folder;
$query->where($db->quoteName('e.folder') . ' = :folder')
->bind(':folder', $folderForBinding);
}
// Process search filter (update site id).
$search = $this->getState('filter.search');
if (!empty($search) && stripos($search, 'id:') === 0)
{
$uid = (int) substr($search, 3);
$query->where($db->quoteName('s.update_site_id') . ' = :siteId')
->bind(':siteId', $uid, ParameterType::INTEGER);
}
if (is_numeric($supported))
{
switch ($supported)
{
// Show Update Sites which support Download Keys
case 1:
$supportedIDs = InstallerHelper::getDownloadKeySupportedSites($enabled);
break;
// Show Update Sites which are missing Download Keys
case -1:
$supportedIDs = InstallerHelper::getDownloadKeyExistsSites(false, $enabled);
break;
// Show Update Sites which have valid Download Keys
case 2:
$supportedIDs = InstallerHelper::getDownloadKeyExistsSites(true, $enabled);
break;
}
if (!empty($supportedIDs))
{
// Don't remove array_values(). whereIn expect a zero-based array.
$query->whereIn($db->qn('s.update_site_id'), array_values($supportedIDs));
}
else
{
// In case of an empty list of IDs we apply a fake filter to effectively return no data
$query->where($db->qn('s.update_site_id') . ' = 0');
}
}
/**
* Note: The search for name, ordering and pagination are processed by the parent InstallerModel class (in
* extension.php).
*/
return $query;
}
}
The same content is in 4 releases, Joomla 4.0.0-rc2 to 4.0.0-rc5. See them all, or every version of this file.