- md5
- 2008c6701a899a11517592515039c02d
- sha1
- f951a64f6490da64fe9448c9c3abc420d6266276
- sha256
- b08ac98ef64530b9620a8779fef37fc0e94ddd4a73d272e228fa9612c8157536
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
<?php
/**
* @package Joomla.Administrator
* @subpackage com_templates
*
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Templates\Administrator\Controller;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Application\CMSWebApplicationInterface;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\Filesystem\Path;
use Joomla\Input\Input;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Template style controller class.
*
* @since 1.6
*/
class TemplateController extends BaseController
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
* @param ?MVCFactoryInterface $factory The factory.
* @param ?CMSApplication $app The Application for the dispatcher
* @param ?Input $input Input
*
* @since 1.6
* @see BaseController
*/
public function __construct($config = [], ?MVCFactoryInterface $factory = null, $app = null, $input = null)
{
parent::__construct($config, $factory, $app, $input);
$this->registerTask('apply', 'save');
$this->registerTask('unpublish', 'publish');
$this->registerTask('publish', 'publish');
$this->registerTask('deleteOverrideHistory', 'publish');
}
/**
* Method for closing the template.
*
* @return void
*
* @since 3.2
*/
public function cancel()
{
$this->setRedirect(Route::_('index.php?option=com_templates&view=templates', false));
}
/**
* Method for closing a file.
*
* @return void
*
* @since 3.2
*/
public function close()
{
$file = base64_encode('home');
$id = (int) $this->input->get('id', 0, 'int');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' .
$file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
/**
* Marked as Checked/Unchecked of override history.
*
* @return void
*
* @since 4.0.0
*/
public function publish()
{
// Check for request forgeries.
$this->checkToken();
$file = $this->input->get('file');
$id = $this->input->get('id');
$ids = (array) $this->input->get('cid', [], 'string');
$values = ['publish' => 1, 'unpublish' => 0, 'deleteOverrideHistory' => -3];
$task = $this->getTask();
$value = ArrayHelper::getValue($values, $task, 0, 'int');
if (empty($ids)) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_NO_FILE_SELECTED'), 'warning');
} else {
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
// Change the state of the records.
if (!$model->publish($ids, $value, $id)) {
$this->setMessage(implode('<br>', $model->getErrors()), 'warning');
} else {
if ($value === 1) {
$ntext = 'COM_TEMPLATES_N_OVERRIDE_CHECKED';
} elseif ($value === 0) {
$ntext = 'COM_TEMPLATES_N_OVERRIDE_UNCHECKED';
} elseif ($value === -3) {
$ntext = 'COM_TEMPLATES_N_OVERRIDE_DELETED';
}
$this->setMessage(Text::plural($ntext, \count($ids)));
}
}
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' .
$file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
/**
* Method for copying the template.
*
* @return boolean true on success, false otherwise
*
* @since 3.2
*/
public function copy()
{
// Check for request forgeries
$this->checkToken();
$app = $this->app;
$this->input->set('installtype', 'folder');
$newNameRaw = $this->input->get('new_name', null, 'string');
// Only accept letters, numbers and underscore for template name
$newName = preg_replace('/[^a-zA-Z0-9_]/', '', $newNameRaw);
$templateID = (int) $this->input->getInt('id', 0);
$file = (string) $this->input->get('file', '', 'cmd');
// Access check.
if (!$this->allowEdit()) {
$app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return false;
}
$this->setRedirect('index.php?option=com_templates&view=template&id=' . $templateID . '&file=' . $file);
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel('Template', 'Administrator');
$model->setState('new_name', $newName);
$model->setState('tmp_prefix', uniqid('template_copy_'));
$model->setState('to_path', $app->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
// Process only if we have a new name entered
if (\strlen($newName) > 0) {
if (!$this->app->getIdentity()->authorise('core.create', 'com_templates')) {
// User is not authorised to delete
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_CREATE_NOT_PERMITTED'), 'error');
return false;
}
// Check that new name is valid
if (($newNameRaw !== null) && ($newName !== $newNameRaw)) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME'), 'error');
return false;
}
// Check that new name doesn't already exist
if (!$model->checkNewName()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_DUPLICATE_TEMPLATE_NAME'), 'error');
return false;
}
// Check that from name does exist and get the folder name
$fromName = $model->getFromName();
if (!$fromName) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
return false;
}
// Call model's copy method
if (!$model->copy()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_COPY'), 'error');
return false;
}
// Call installation model
$this->input->set('install_directory', $app->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
/** @var \Joomla\Component\Installer\Administrator\Model\InstallModel $installModel */
$installModel = $this->app->bootComponent('com_installer')
->getMVCFactory()->createModel('Install', 'Administrator');
$this->app->getLanguage()->load('com_installer');
if (!$installModel->install()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_INSTALL'), 'error');
return false;
}
$this->setMessage(Text::sprintf('COM_TEMPLATES_COPY_SUCCESS', $newName));
$model->cleanup();
return true;
}
$this->setMessage(Text::sprintf('COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME'), 'error');
return false;
}
/**
* Method to get a model object, loading it if required.
*
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional (note, the empty array is atypical compared to other models).
*
* @return \Joomla\CMS\MVC\Model\BaseDatabaseModel The model.
*
* @since 3.2
*/
public function getModel($name = 'Template', $prefix = 'Administrator', $config = [])
{
return parent::getModel($name, $prefix, $config);
}
/**
* Method to check if you can add a new record.
*
* @return boolean
*
* @since 3.2
*/
protected function allowEdit()
{
return $this->app->getIdentity()->authorise('core.admin');
}
/**
* Saves a template source file.
*
* @return void
*
* @since 3.2
*/
public function save()
{
// Check for request forgeries.
$this->checkToken();
$data = $this->input->post->get('jform', [], 'array');
$task = $this->getTask();
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$fileName = (string) $this->input->getCmd('file', '');
$explodeArray = explode(':', str_replace('//', '/', base64_decode($fileName)));
// Access check.
if (!$this->allowEdit()) {
$this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
// Match the stored id's with the submitted.
if (empty($data['extension_id']) || empty($data['filename'])) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH'), 'error');
return;
}
if ($data['extension_id'] != $model->getState('extension.id')) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH'), 'error');
return;
}
if (str_ends_with(end($explodeArray), Path::clean($data['filename'], '/'))) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH'), 'error');
return;
}
// Validate the posted data.
$form = $model->getForm();
if (!$form) {
$this->setMessage($model->getError(), 'error');
return;
}
$data = $model->validate($form, $data);
// Check for validation errors.
if ($data === false) {
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) {
if ($errors[$i] instanceof \Exception) {
$this->app->enqueueMessage($errors[$i]->getMessage(), CMSWebApplicationInterface::MSG_ERROR);
} else {
$this->app->enqueueMessage($errors[$i], CMSWebApplicationInterface::MSG_ERROR);
}
}
// Redirect back to the edit screen.
$url = 'index.php?option=com_templates&view=template&id=' . $model->getState('extension.id') . '&file=' . $fileName . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
return;
}
// Attempt to save the data.
if (!$model->save($data)) {
// Redirect back to the edit screen.
$this->setMessage(Text::sprintf('JERROR_SAVE_FAILED', $model->getError()), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $model->getState('extension.id') . '&file=' . $fileName . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
return;
}
$this->setMessage(Text::_('COM_TEMPLATES_FILE_SAVE_SUCCESS'));
// Redirect the user based on the chosen task.
switch ($task) {
case 'apply':
// Redirect back to the edit screen.
$url = 'index.php?option=com_templates&view=template&id=' . $model->getState('extension.id') . '&file=' . $fileName . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
break;
default:
// Redirect to the list screen.
$file = base64_encode('home');
$id = (int) $this->input->get('id', 0, 'int');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
break;
}
}
/**
* Method for creating override.
*
* @return void
*
* @since 3.2
*/
public function overrides()
{
// Check for request forgeries.
$this->checkToken('get');
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$file = (string) $this->input->getCmd('file', '');
$override = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('folder', '')), 'path');
$id = (int) $this->input->get('id', 0, 'int');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
$model->createOverride($override);
// Redirect back to the edit screen.
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
/**
* Method for deleting a file.
*
* @return void
*
* @since 3.2
*/
public function delete()
{
// Check for request forgeries
$this->checkToken();
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (base64_decode(urldecode($file)) == '/index.php') {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_INDEX_DELETE'), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif (base64_decode(urldecode($file)) == '/joomla.asset.json') {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_ASSET_FILE_DELETE'), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif ($model->deleteFile($file)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_DELETE_SUCCESS'));
$file = base64_encode('home');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_FILE_DELETE'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for creating a new file.
*
* @return void
*
* @since 3.2
*/
public function createFile()
{
// Check for request forgeries
$this->checkToken();
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->get('file', '', 'cmd');
$name = (string) $this->input->get('name', '', 'cmd');
$location = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('address', '')), 'path');
$type = (string) $this->input->get('type', '', 'cmd');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if ($type == 'null') {
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FILE_TYPE'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif (!preg_match('/^(?!\.)(?!.*\.$)(?!.*\.\.)[a-zA-Z0-9_.-]+$/', $name)) {
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FILE_NAME'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif ($model->createFile($name, $type, $location)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_CREATE_SUCCESS'));
$file = urlencode(base64_encode($location . '/' . $name . '.' . $type));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_FILE_CREATE'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for uploading a file.
*
* @return void
*
* @since 3.2
*/
public function uploadFile()
{
// Check for request forgeries
$this->checkToken();
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$upload = $this->input->files->get('files');
$location = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('address', '')), 'path');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if ($return = $model->uploadFile($upload, $location)) {
$this->setMessage(Text::sprintf('COM_TEMPLATES_FILE_UPLOAD_SUCCESS', $upload['name']));
$redirect = base64_encode($return);
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $redirect . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_FILE_UPLOAD'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for creating a new folder.
*
* @return void
*
* @since 3.2
*/
public function createFolder()
{
// Check for request forgeries
$this->checkToken();
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$name = $this->input->get('name');
$location = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('address', '')), 'path');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $name)) {
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FOLDER_NAME'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif ($model->createFolder($name, $location)) {
$this->setMessage(Text::_('COM_TEMPLATES_FOLDER_CREATE_SUCCESS'));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_FOLDER_CREATE'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for deleting a folder.
*
* @return void
*
* @since 3.2
*/
public function deleteFolder()
{
// Check for request forgeries
$this->checkToken();
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$isMedia = (int) $this->input->get('isMedia', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$location = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('address', '')), 'path');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (empty($location)) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_ROOT_DELETE'), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} elseif ($model->deleteFolder($location)) {
$this->setMessage(Text::_('COM_TEMPLATES_FOLDER_DELETE_SUCCESS'));
if (stristr(base64_decode($file), $location)) {
$file = base64_encode('home');
}
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_FOLDER_DELETE_ERROR'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for renaming a file.
*
* @return void
*
* @since 3.2
*/
public function renameFile()
{
// Check for request forgeries
$this->checkToken();
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$id = (int) $this->input->get('id', 0, 'int');
$isMedia = (int) $this->input->get('isMedia', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$newName = $this->input->get('new_name');
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (base64_decode(urldecode($file)) == '/index.php') {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_RENAME_INDEX'), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} elseif (base64_decode(urldecode($file)) == '/joomla.asset.json') {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_RENAME_ASSET_FILE'), 'warning');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} elseif (!preg_match('/^(?!\.)(?!.*\.$)(?!.*\.\.)[a-zA-Z0-9_.-]+$/', $newName)) {
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FILE_NAME'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} elseif ($rename = $model->renameFile($file, $newName)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_RENAME_SUCCESS'));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $rename . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_FILE_RENAME'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $isMedia;
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for cropping an image.
*
* @return void
*
* @since 3.2
*/
public function cropImage()
{
// Check for request forgeries
$this->checkToken();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->get('file', '', 'cmd');
$x = $this->input->get('x');
$y = $this->input->get('y');
$w = $this->input->get('w');
$h = $this->input->get('h');
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (empty($w) && empty($h) && empty($x) && empty($y)) {
$this->setMessage(Text::_('COM_TEMPLATES_CROP_AREA_ERROR'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif ($model->cropImage($file, $w, $h, $x, $y)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_CROP_SUCCESS'));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_CROP_ERROR'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for resizing an image.
*
* @return void
*
* @since 3.2
*/
public function resizeImage()
{
// Check for request forgeries
$this->checkToken();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$width = $this->input->get('width');
$height = $this->input->get('height');
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if ($model->resizeImage($file, $width, $height)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_RESIZE_SUCCESS'));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_RESIZE_ERROR'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for copying a file.
*
* @return void
*
* @since 3.2
*/
public function copyFile()
{
// Check for request forgeries
$this->checkToken();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
$newName = $this->input->get('new_name');
$location = (string) InputFilter::getInstance(
[],
[],
InputFilter::ONLY_BLOCK_DEFINED_TAGS,
InputFilter::ONLY_BLOCK_DEFINED_ATTRIBUTES
)
->clean(base64_decode($this->input->getBase64('address', '')), 'path');
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if (!preg_match('/^[a-zA-Z0-9-_]+$/', $newName)) {
$this->setMessage(Text::_('COM_TEMPLATES_INVALID_FILE_NAME'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} elseif ($model->copyFile($newName, $location, $file)) {
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_COPY_FAIL'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file . '&isMedia=' . $this->input->getInt('isMedia', 0);
$this->setRedirect(Route::_($url, false));
}
}
/**
* Method for extracting an archive file.
*
* @return void
*
* @since 3.2
*/
public function extractArchive()
{
// Check for request forgeries
$this->checkToken();
$id = (int) $this->input->get('id', 0, 'int');
$file = (string) $this->input->getCmd('file', '');
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return;
}
if ($model->extractArchive($file)) {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_EXTRACT_SUCCESS'));
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
$this->setRedirect(Route::_($url, false));
} else {
$this->setMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_EXTRACT_FAIL'), 'error');
$url = 'index.php?option=com_templates&view=template&id=' . $id . '&file=' . $file;
$this->setRedirect(Route::_($url, false));
}
}
/**
* Fetch and report updates in \JSON format, for AJAX requests
*
* @return void
*
* @since 4.0.0
*/
public function ajax()
{
$app = $this->app;
if (!Session::checkToken('get')) {
$app->setHeader('status', 403, true);
$app->sendHeaders();
echo Text::_('JINVALID_TOKEN_NOTICE');
$app->close();
}
// Checks status of installer override plugin.
if (!PluginHelper::isEnabled('installer', 'override')) {
$error = ['installerOverride' => 'disabled'];
echo json_encode($error);
$app->close();
}
/** @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel();
$result = $model->getUpdatedList(true, true);
echo json_encode($result);
$app->close();
}
/**
* Method for creating a child template.
*
* @return boolean true on success, false otherwise
*
* @since 4.1.0
*/
public function child()
{
// Check for request forgeries
$this->checkToken();
// Access check.
if (!$this->allowEdit()) {
$this->app->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
return false;
}
$this->input->set('installtype', 'folder');
$newNameRaw = $this->input->get('new_name', null, 'string');
// Only accept letters, numbers and underscore for template name
$newName = preg_replace('/[^a-zA-Z0-9_]/', '', $newNameRaw);
$templateID = (int) $this->input->getInt('id', 0);
$file = (string) $this->input->get('file', '', 'cmd');
$extraStyles = (array) $this->input->get('style_ids', [], 'array');
$this->setRedirect('index.php?option=com_templates&view=template&id=' . $templateID . '&file=' . $file);
/* @var \Joomla\Component\Templates\Administrator\Model\TemplateModel $model */
$model = $this->getModel('Template', 'Administrator');
$model->setState('new_name', $newName);
$model->setState('tmp_prefix', uniqid('template_child_'));
$model->setState('to_path', $this->app->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
// Process only if we have a new name entered
if (!\strlen($newName)) {
$this->setMessage(Text::sprintf('COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME'), 'error');
return false;
}
// Process only if user is allowed to create child template
if (!$this->app->getIdentity()->authorise('core.create', 'com_templates')) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_CREATE_NOT_PERMITTED'), 'error');
return false;
}
// Check that new name is valid
if (($newNameRaw !== null) && ($newName !== $newNameRaw)) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME'), 'error');
return false;
}
// Check that new name doesn't already exist
if (!$model->checkNewName()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_DUPLICATE_TEMPLATE_NAME'), 'error');
return false;
}
// Check that from name does exist and get the folder name
$fromName = $model->getFromName();
if (!$fromName) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
return false;
}
// Call model's copy method
if (!$model->child()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_COPY'), 'error');
return false;
}
// Call installation model
$this->input->set('install_directory', $this->app->get('tmp_path') . '/' . $model->getState('tmp_prefix'));
/** @var \Joomla\Component\Installer\Administrator\Model\InstallModel $installModel */
$installModel = $this->app->bootComponent('com_installer')
->getMVCFactory()->createModel('Install', 'Administrator');
$this->app->getLanguage()->load('com_installer');
if (!$installModel->install()) {
$this->setMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_INSTALL'), 'error');
return false;
}
$this->setMessage(Text::sprintf('COM_TEMPLATES_CHILD_SUCCESS', $newName));
$model->cleanup();
if (\count($extraStyles) > 0) {
$model->setState('stylesToCopy', $extraStyles);
$model->copyStyles();
}
return true;
}
}
The same content is in 68 releases, Joomla 5.4.0-alpha1 to 6.2.0-beta2. See them all, or every version of this file.