- md5
- 3b9a03ed9c29f5ffe675316dc9db618f
- sha1
- 7e4921af35555a28e5444d48edc88a4ac25ebbbd
- sha256
- a263dfc884fe8e42d8450751d2fd5759620ec99b21681ed11a83d7aa07bb90c6
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Installer\Adapter;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Table\Update;
use Joomla\Database\ParameterType;
/**
* Template installer
*
* @since 3.1
*/
class TemplateAdapter extends InstallerAdapter
{
/**
* The install client ID
*
* @var integer
* @since 3.4
*/
protected $clientId;
/**
* Method to check if the extension is already present in the database
*
* @return void
*
* @since 3.4
* @throws \RuntimeException
*/
protected function checkExistingExtension()
{
try
{
$this->currentExtensionId = $this->extension->find(
array(
'element' => $this->element,
'type' => $this->type,
'client_id' => $this->clientId,
)
);
}
catch (\RuntimeException $e)
{
// Install failed, roll back changes
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_ROLLBACK',
Text::_('JLIB_INSTALLER_' . $this->route),
$e->getMessage()
),
$e->getCode(),
$e
);
}
}
/**
* Method to copy the extension's base files from the `<files>` tag(s) and the manifest file
*
* @return void
*
* @since 3.4
* @throws \RuntimeException
*/
protected function copyBaseFiles()
{
// Copy all the necessary files
if ($this->parent->parseFiles($this->getManifest()->files, -1) === false)
{
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
'files'
)
);
}
if ($this->parent->parseFiles($this->getManifest()->images, -1) === false)
{
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
'images'
)
);
}
if ($this->parent->parseFiles($this->getManifest()->css, -1) === false)
{
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
'css'
)
);
}
// If there is a manifest script, let's copy it.
if ($this->manifest_script)
{
$path['src'] = $this->parent->getPath('source') . '/' . $this->manifest_script;
$path['dest'] = $this->parent->getPath('extension_root') . '/' . $this->manifest_script;
if ($this->parent->isOverwrite() || !file_exists($path['dest']))
{
if (!$this->parent->copyFiles(array($path)))
{
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_MANIFEST',
Text::_('JLIB_INSTALLER_' . strtoupper($this->getRoute()))
)
);
}
}
}
}
/**
* Method to finalise the installation processing
*
* @return void
*
* @since 3.1
* @throws \RuntimeException
*/
protected function finaliseInstall()
{
// Clobber any possible pending updates
/** @var Update $update */
$update = Table::getInstance('update');
$uid = $update->find(
array(
'element' => $this->element,
'type' => $this->type,
'client_id' => $this->clientId,
)
);
if ($uid)
{
$update->delete($uid);
}
// Lastly, we will copy the manifest file to its appropriate place.
if ($this->route !== 'discover_install')
{
if (!$this->parent->copyManifest(-1))
{
// Install failed, rollback changes
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_COPY_SETUP',
Text::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
}
}
/**
* Method to finalise the uninstallation processing
*
* @return boolean
*
* @since 4.0.0
* @throws \RuntimeException
*/
protected function finaliseUninstall(): bool
{
$db = $this->parent->getDbo();
$query = $db->getQuery(true);
$element = $this->extension->element;
$clientId = $this->extension->client_id;
$extensionId = $this->extension->extension_id;
// Set menu that assigned to the template back to default template
$subQuery = $db->getQuery(true)
->select($db->quoteName('s.id'))
->from($db->quoteName('#__template_styles', 's'))
->where(
[
$db->quoteName('s.template') . ' = :element',
$db->quoteName('s.client_id') . ' = :clientId',
]
);
$query->bind(':element', $element)
->bind(':clientId', $clientId, ParameterType::INTEGER);
$query->update($db->quoteName('#__menu'))
->set($db->quoteName('template_style_id') . ' = 0')
->where($db->quoteName('template_style_id') . ' IN (' . (string) $subQuery . ')');
$db->setQuery($query);
$db->execute();
// Remove the template's styles
$query = $db->getQuery(true)
->delete($db->quoteName('#__template_styles'))
->where(
[
$db->quoteName('template') . ' = :template',
$db->quoteName('client_id') . ' = :client_id',
]
)
->bind(':template', $element)
->bind(':client_id', $clientId, ParameterType::INTEGER);
$db->setQuery($query);
$db->execute();
// Remove the schema version
$query = $db->getQuery(true)
->delete($db->quoteName('#__schemas'))
->where($db->quoteName('extension_id') . ' = :extension_id')
->bind(':extension_id', $extensionId, ParameterType::INTEGER);
$db->setQuery($query);
$db->execute();
// Clobber any possible pending updates
$update = Table::getInstance('update');
$uid = $update->find(
[
'element' => $this->extension->element,
'type' => $this->type,
'client_id' => $this->extension->client_id,
]
);
if ($uid)
{
$update->delete($uid);
}
$this->extension->delete();
return true;
}
/**
* Custom loadLanguage method
*
* @param string $path The path where to find language files.
*
* @return void
*
* @since 3.1
*/
public function loadLanguage($path = null)
{
$source = $this->parent->getPath('source');
$basePath = $this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE;
if (!$source)
{
$this->parent->setPath('source', $basePath . '/templates/' . $this->parent->extension->element);
}
$this->setManifest($this->parent->getManifest());
$client = (string) $this->getManifest()->attributes()->client;
// Load administrator language if not set.
if (!$client)
{
$client = 'ADMINISTRATOR';
}
$base = \constant('JPATH_' . strtoupper($client));
$extension = 'tpl_' . $this->getName();
$source = $path ?: $base . '/templates/' . $this->getName();
$this->doLoadLanguage($extension, $source, $base);
}
/**
* Method to parse optional tags in the manifest
*
* @return void
*
* @since 3.4
*/
protected function parseOptionalTags()
{
$this->parent->parseMedia($this->getManifest()->media);
$this->parent->parseLanguages($this->getManifest()->languages, $this->clientId);
}
/**
* Overloaded method to parse queries for template installations
*
* @return void
*
* @since 3.4
* @throws \RuntimeException
*/
protected function parseQueries()
{
if (\in_array($this->route, array('install', 'discover_install')))
{
$db = $this->db;
$query = $db->getQuery(true);
$lang = Factory::getLanguage();
$debug = $lang->setDebug(false);
$columns = [
$db->quoteName('template'),
$db->quoteName('client_id'),
$db->quoteName('home'),
$db->quoteName('title'),
$db->quoteName('params'),
$db->quoteName('inheritable'),
$db->quoteName('parent'),
];
$values = $query->bindArray(
[
$this->extension->element,
$this->extension->client_id,
'0',
Text::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', Text::_($this->extension->name)),
$this->extension->params,
(int) $this->manifest->inheritable,
$this->manifest->parent ?: '',
],
[
ParameterType::STRING,
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::STRING,
ParameterType::STRING,
ParameterType::INTEGER,
ParameterType::STRING,
]
);
$lang->setDebug($debug);
// Insert record in #__template_styles
$query->insert($db->quoteName('#__template_styles'))
->columns($columns)
->values(implode(',', $values));
// There is a chance this could fail but we don't care...
$db->setQuery($query)->execute();
}
}
/**
* Prepares the adapter for a discover_install task
*
* @return void
*
* @since 3.4
*/
public function prepareDiscoverInstall()
{
$client = ApplicationHelper::getClientInfo($this->extension->client_id);
$manifestPath = $client->path . '/templates/' . $this->extension->element . '/templateDetails.xml';
$this->parent->manifest = $this->parent->isManifest($manifestPath);
$this->parent->setPath('manifest', $manifestPath);
$this->setManifest($this->parent->getManifest());
}
/**
* Removes this extension's files
*
* @return void
*
* @since 4.0.0
* @throws \RuntimeException
*/
protected function removeExtensionFiles()
{
// Remove files
$this->parent->removeFiles($this->getManifest()->media);
$this->parent->removeFiles($this->getManifest()->languages, $this->extension->client_id);
// Delete the template directory
if (Folder::exists($this->parent->getPath('extension_root')))
{
Folder::delete($this->parent->getPath('extension_root'));
}
else
{
Log::add(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY'), Log::WARNING, 'jerror');
}
}
/**
* Method to do any prechecks and setup the install paths for the extension
*
* @return void
*
* @since 3.4
* @throws \RuntimeException
*/
protected function setupInstallPaths()
{
// Get the client application target
$cname = (string) $this->getManifest()->attributes()->client;
if ($cname)
{
// Attempt to map the client to a base path
$client = ApplicationHelper::getClientInfo($cname, true);
if ($client === false)
{
throw new \RuntimeException(Text::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_UNKNOWN_CLIENT', $cname));
}
$basePath = $client->path;
$this->clientId = $client->id;
}
else
{
// No client attribute was found so we assume the site as the client
$basePath = JPATH_SITE;
$this->clientId = 0;
}
// Set the template root path
if (empty($this->element))
{
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_MOD_INSTALL_NOFILE',
Text::_('JLIB_INSTALLER_' . strtoupper($this->route))
)
);
}
$this->parent->setPath('extension_root', $basePath . '/templates/' . $this->element);
}
/**
* Method to do any prechecks and setup the uninstall job
*
* @return void
*
* @since 4.0.0
*/
protected function setupUninstall()
{
$this->parent->extension = $this->extension;
$name = $this->extension->element;
$clientId = $this->extension->client_id;
// For a template the id will be the template name which represents the subfolder of the templates folder that the template resides in.
if (!$name)
{
throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY'));
}
// Deny remove default template
$db = $this->parent->getDbo();
$query = $db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__template_styles'))
->where(
[
$db->quoteName('home') . ' = ' . $db->quote('1'),
$db->quoteName('template') . ' = :template',
$db->quoteName('client_id') . ' = :client_id',
]
)
->bind(':template', $name)
->bind(':client_id', $clientId);
$db->setQuery($query);
if ($db->loadResult() != 0)
{
throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT'));
}
// Get the template root path
$client = ApplicationHelper::getClientInfo($clientId);
if (!$client)
{
throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT'));
}
$this->parent->setPath('extension_root', $client->path . '/templates/' . strtolower($name));
$this->parent->setPath('source', $this->parent->getPath('extension_root'));
// We do findManifest to avoid problem when uninstalling a list of extensions: getManifest cache its manifest file
$this->parent->findManifest();
$manifest = $this->parent->getManifest();
if (!($manifest instanceof \SimpleXMLElement))
{
// Kill the extension entry
$this->extension->delete($this->extension->extension_id);
// Make sure we delete the folders
Folder::delete($this->parent->getPath('extension_root'));
throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST'));
}
// Attempt to load the language file; might have uninstall strings
$this->loadLanguage();
}
/**
* Method to store the extension to the database
*
* @return void
*
* @since 3.4
* @throws \RuntimeException
*/
protected function storeExtension()
{
// Discover installs are stored a little differently
if ($this->route === 'discover_install')
{
$manifest_details = Installer::parseXMLInstallFile($this->parent->getPath('manifest'));
$this->extension->manifest_cache = json_encode($manifest_details);
$this->extension->state = 0;
$this->extension->name = $manifest_details['name'];
$this->extension->enabled = 1;
$this->extension->params = $this->parent->getParams();
if (!$this->extension->store())
{
// Install failed, roll back changes
throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_DISCOVER_STORE_DETAILS'));
}
return;
}
// Was there a template already installed with the same name?
if ($this->currentExtensionId)
{
if (!$this->parent->isOverwrite())
{
// Install failed, roll back changes
throw new \RuntimeException(
Text::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_ALREADY_INSTALLED')
);
}
// Load the entry and update the manifest_cache
$this->extension->load($this->currentExtensionId);
}
else
{
$this->extension->type = 'template';
$this->extension->element = $this->element;
// There is no folder for templates
$this->extension->folder = '';
$this->extension->enabled = 1;
$this->extension->protected = 0;
$this->extension->access = 1;
$this->extension->client_id = $this->clientId;
$this->extension->params = $this->parent->getParams();
$this->extension->changelogurl = $this->changelogurl;
}
// Name might change in an update
$this->extension->name = $this->name;
// Update the manifest cache for the entry
$this->extension->manifest_cache = $this->parent->generateManifestCache();
$this->extension->changelogurl = $this->changelogurl;
if (!$this->extension->store())
{
// Install failed, roll back changes
throw new \RuntimeException(
Text::sprintf(
'JLIB_INSTALLER_ABORT_ROLLBACK',
Text::_('JLIB_INSTALLER_' . strtoupper($this->route)),
$this->extension->getError()
)
);
}
}
/**
* Discover existing but uninstalled templates
*
* @return array Extension list
*/
public function discover()
{
$results = array();
$site_list = Folder::folders(JPATH_SITE . '/templates');
$admin_list = Folder::folders(JPATH_ADMINISTRATOR . '/templates');
$site_info = ApplicationHelper::getClientInfo('site', true);
$admin_info = ApplicationHelper::getClientInfo('administrator', true);
foreach ($site_list as $template)
{
if (file_exists(JPATH_SITE . "/templates/$template/templateDetails.xml"))
{
if ($template === 'system')
{
// Ignore special system template
continue;
}
$manifest_details = Installer::parseXMLInstallFile(JPATH_SITE . "/templates/$template/templateDetails.xml");
$extension = Table::getInstance('extension');
$extension->set('type', 'template');
$extension->set('client_id', $site_info->id);
$extension->set('element', $template);
$extension->set('folder', '');
$extension->set('name', $template);
$extension->set('state', -1);
$extension->set('manifest_cache', json_encode($manifest_details));
$extension->set('params', '{}');
$results[] = $extension;
}
}
foreach ($admin_list as $template)
{
if (file_exists(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml"))
{
if ($template === 'system')
{
// Ignore special system template
continue;
}
$manifest_details = Installer::parseXMLInstallFile(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml");
$extension = Table::getInstance('extension');
$extension->set('type', 'template');
$extension->set('client_id', $admin_info->id);
$extension->set('element', $template);
$extension->set('folder', '');
$extension->set('name', $template);
$extension->set('state', -1);
$extension->set('manifest_cache', json_encode($manifest_details));
$extension->set('params', '{}');
$results[] = $extension;
}
}
return $results;
}
/**
* Refreshes the extension table cache
*
* @return boolean Result of operation, true if updated, false on failure
*
* @since 3.1
*/
public function refreshManifestCache()
{
// Need to find to find where the XML file is since we don't store this normally.
$client = ApplicationHelper::getClientInfo($this->parent->extension->client_id);
$manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
$this->parent->manifest = $this->parent->isManifest($manifestPath);
$this->parent->setPath('manifest', $manifestPath);
$manifest_details = Installer::parseXMLInstallFile($this->parent->getPath('manifest'));
$this->parent->extension->manifest_cache = json_encode($manifest_details);
$this->parent->extension->name = $manifest_details['name'];
try
{
return $this->parent->extension->store();
}
catch (\RuntimeException $e)
{
Log::add(Text::_('JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE'), Log::WARNING, 'jerror');
return false;
}
}
}
The same content is in 27 releases, Joomla 4.0.0-beta6 to 4.1.0-alpha3. See them all, or every version of this file.