CoreFiles by mySites.guru
TemplateController.php1,088 lines, 32,543 bytes, from Joomla_4.1.2-Stable-Full_Package.zip
Raw History Compare with 4.1.1
md5
c6d0afd6f5d06c6e1a3041862b8bf9c4
sha1
6dd7bbc5f4c7e2afcf708807cb466a4d9f28b0b6
sha256
63d9b18c6647584ec2d5bcea7e5e6437883a119edb5bd918447bf411fdf481f4
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
<?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;

\defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Filesystem\Path;
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\Input\Input;
use Joomla\Utilities\ArrayHelper;

/**
 * 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 = array(), 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', array(), 'string');
		$values = array('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 = array())
	{
		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(), '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;
		}
		elseif ($data['extension_id'] != $model->getState('extension.id'))
		{
			$this->setMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_ID_FILENAME_MISMATCH'), 'error');

			return;
		}
		elseif (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(), 'warning');
				}
				else
				{
					$this->app->enqueueMessage($errors[$i], 'warning');
				}
			}

			// 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) != false)
			{
				$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 = array('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');

		$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();

		return true;
	}
}

The same content is in 3 releases, Joomla 4.1.1 to 4.2.0-alpha1. See them all, or every version of this file.