- md5
- f467383ecd22983138b94167a6afb4fb
- sha1
- f358c1dbc7a2c4662ee0d356052a59ed7018f55c
- sha256
- 511db13fb3c1b76bb7710c55b0732773a77d42db3bc023d5e9d243c59345452c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
<?php
/**
* @package Joomla.Administrator
* @subpackage com_templates
*
* @copyright (C) 2008 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\Model;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Image\Image;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Templates\Administrator\Helper\TemplateHelper;
use Joomla\Component\Templates\Administrator\Helper\TemplatesHelper;
use Joomla\Database\ParameterType;
use Joomla\Filesystem\Path;
use Joomla\Utilities\ArrayHelper;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Template model class.
*
* @since 1.6
*/
class TemplateModel extends FormModel
{
/**
* The information in a template
*
* @var \stdClass
* @since 1.6
*/
protected $template = null;
/**
* The path to the template
*
* @var string
* @since 3.2
*/
protected $element = null;
/**
* The path to the static assets
*
* @var string
* @since 4.1.0
*/
protected $mediaElement = null;
/**
* Internal method to get file properties.
*
* @param string $path The base path.
* @param string $name The file name.
*
* @return object
*
* @since 1.6
*/
protected function getFile($path, $name)
{
$temp = new \stdClass();
if ($this->getTemplate()) {
$path = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? 'site' : 'administrator') . DIRECTORY_SEPARATOR . $this->template->element, '', $path);
$path = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? '' : 'administrator' . DIRECTORY_SEPARATOR) . 'templates' . DIRECTORY_SEPARATOR . $this->template->element, '', $path);
$temp->name = $name;
$temp->id = urlencode(base64_encode(str_replace('\\', '//', $path)));
return $temp;
}
}
/**
* Method to store file information.
*
* @param string $path The base path.
* @param string $name The file name.
* @param stdClass $template The std class object of template.
*
* @return object stdClass object.
*
* @since 4.0.0
*/
protected function storeFileInfo($path, $name, $template)
{
$temp = new \stdClass();
$temp->id = base64_encode($path . $name);
$temp->client = $template->client_id;
$temp->template = $template->element;
$temp->extension_id = $template->extension_id;
if ($coreFile = $this->getCoreFile($path . $name, $template->client_id)) {
$temp->coreFile = md5_file($coreFile);
} else {
$temp->coreFile = null;
}
return $temp;
}
/**
* Method to get all template list.
*
* @return object stdClass object
*
* @since 4.0.0
*/
public function getTemplateList()
{
// Get a db connection.
$db = $this->getDatabase();
// Create a new query object.
$query = $db->getQuery(true);
// Select the required fields from the table
$query->select(
$this->getState(
'list.select',
'a.extension_id, a.name, a.element, a.client_id'
)
);
$query->from($db->quoteName('#__extensions', 'a'))
->where($db->quoteName('a.enabled') . ' = 1')
->where($db->quoteName('a.type') . ' = ' . $db->quote('template'));
// Reset the query.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();
return $results;
}
/**
* Method to get all updated file list.
*
* @param boolean $state The optional parameter if you want unchecked list.
* @param boolean $all The optional parameter if you want all list.
* @param boolean $cleanup The optional parameter if you want to clean record which is no more required.
*
* @return object stdClass object
*
* @since 4.0.0
*/
public function getUpdatedList($state = false, $all = false, $cleanup = false)
{
// Get a db connection.
$db = $this->getDatabase();
// Create a new query object.
$query = $db->getQuery(true);
// Select the required fields from the table
$query->select(
$this->getState(
'list.select',
'a.template, a.hash_id, a.extension_id, a.state, a.action, a.client_id, a.created_date, a.modified_date'
)
);
$template = $this->getTemplate();
$query->from($db->quoteName('#__template_overrides', 'a'));
if (!$all) {
$teid = (int) $template->extension_id;
$query->where($db->quoteName('extension_id') . ' = :teid')
->bind(':teid', $teid, ParameterType::INTEGER);
}
if ($state) {
$query->where($db->quoteName('state') . ' = 0');
}
$query->order($db->quoteName('a.modified_date') . ' DESC');
// Reset the query.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$pks = $db->loadObjectList();
if ($state) {
return $pks;
}
$results = [];
foreach ($pks as $pk) {
$client = ApplicationHelper::getClientInfo($pk->client_id);
$path = Path::clean($client->path . '/templates/' . $pk->template . base64_decode($pk->hash_id));
if (file_exists($path)) {
$results[] = $pk;
} elseif ($cleanup) {
$cleanupIds = [];
$cleanupIds[] = $pk->hash_id;
$this->publish($cleanupIds, -3, $pk->extension_id);
}
}
return $results;
}
/**
* Method to get a list of all the core files of override files.
*
* @return array An array of all core files.
*
* @since 4.0.0
*/
public function getCoreList()
{
// Get list of all templates
$templates = $this->getTemplateList();
// Initialize the array variable to store core file list.
$this->coreFileList = [];
$app = Factory::getApplication();
foreach ($templates as $template) {
$client = ApplicationHelper::getClientInfo($template->client_id);
$element = Path::clean($client->path . '/templates/' . $template->element . '/');
$path = Path::clean($element . 'html/');
if (is_dir($path)) {
$this->prepareCoreFiles($path, $element, $template);
}
}
// Sort list of stdClass array.
usort(
$this->coreFileList,
function ($a, $b) {
return strcmp($a->id, $b->id);
}
);
return $this->coreFileList;
}
/**
* Prepare core files.
*
* @param string $dir The path of the directory to scan.
* @param string $element The path of the template element.
* @param \stdClass $template The stdClass object of template.
*
* @return void
*
* @since 4.0.0
*/
public function prepareCoreFiles($dir, $element, $template)
{
$dirFiles = scandir($dir);
foreach ($dirFiles as $key => $value) {
if (in_array($value, ['.', '..', 'node_modules'])) {
continue;
}
if (is_dir($dir . $value)) {
$relativePath = str_replace($element, '', $dir . $value);
$this->prepareCoreFiles($dir . $value . '/', $element, $template);
} else {
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
$allowedFormat = $this->checkFormat($ext);
if ($allowedFormat === true) {
$relativePath = str_replace($element, '', $dir);
$info = $this->storeFileInfo('/' . $relativePath, $value, $template);
if ($info) {
$this->coreFileList[] = $info;
}
}
}
}
}
/**
* Method to update status of list.
*
* @param array $ids The base path.
* @param array $value The file name.
* @param integer $exid The template extension id.
*
* @return integer Number of files changed.
*
* @since 4.0.0
*/
public function publish($ids, $value, $exid)
{
$db = $this->getDatabase();
foreach ($ids as $id) {
if ($value === -3) {
$deleteQuery = $db->getQuery(true)
->delete($db->quoteName('#__template_overrides'))
->where($db->quoteName('hash_id') . ' = :hashid')
->where($db->quoteName('extension_id') . ' = :exid')
->bind(':hashid', $id)
->bind(':exid', $exid, ParameterType::INTEGER);
try {
// Set the query using our newly populated query object and execute it.
$db->setQuery($deleteQuery);
$result = $db->execute();
} catch (\RuntimeException $e) {
return $e;
}
} elseif ($value === 1 || $value === 0) {
$updateQuery = $db->getQuery(true)
->update($db->quoteName('#__template_overrides'))
->set($db->quoteName('state') . ' = :state')
->where($db->quoteName('hash_id') . ' = :hashid')
->where($db->quoteName('extension_id') . ' = :exid')
->bind(':state', $value, ParameterType::INTEGER)
->bind(':hashid', $id)
->bind(':exid', $exid, ParameterType::INTEGER);
try {
// Set the query using our newly populated query object and execute it.
$db->setQuery($updateQuery);
$result = $db->execute();
} catch (\RuntimeException $e) {
return $e;
}
}
}
return $result;
}
/**
* Method to get a list of all the files to edit in a template.
*
* @return array A nested array of relevant files.
*
* @since 1.6
*/
public function getFiles()
{
$result = [];
if ($template = $this->getTemplate()) {
$app = Factory::getApplication();
$client = ApplicationHelper::getClientInfo($template->client_id);
$path = Path::clean($client->path . '/templates/' . $template->element . '/');
$lang = Factory::getLanguage();
// Load the core and/or local language file(s).
$lang->load('tpl_' . $template->element, $client->path)
|| (!empty($template->xmldata->parent) && $lang->load('tpl_' . $template->xmldata->parent, $client->path))
|| $lang->load('tpl_' . $template->element, $client->path . '/templates/' . $template->element)
|| (!empty($template->xmldata->parent) && $lang->load('tpl_' . $template->xmldata->parent, $client->path . '/templates/' . $template->xmldata->parent));
$this->element = $path;
if (!is_writable($path)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_DIRECTORY_NOT_WRITABLE'), 'error');
}
if (is_dir($path)) {
$result = $this->getDirectoryTree($path);
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_TEMPLATE_FOLDER_NOT_FOUND'), 'error');
return false;
}
// Clean up override history
$this->getUpdatedList(false, true, true);
}
return $result;
}
/**
* Get the directory tree.
*
* @param string $dir The path of the directory to scan
*
* @return array
*
* @since 3.2
*/
public function getDirectoryTree($dir)
{
$result = [];
$dirFiles = scandir($dir);
foreach ($dirFiles as $key => $value) {
if (!in_array($value, ['.', '..', 'node_modules'])) {
if (is_dir($dir . $value)) {
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? 'site' : 'administrator') . DIRECTORY_SEPARATOR . $this->template->element, '', $dir . $value);
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? '' : 'administrator' . DIRECTORY_SEPARATOR) . 'templates' . DIRECTORY_SEPARATOR . $this->template->element, '', $relativePath);
$result[str_replace('\\', '//', $relativePath)] = $this->getDirectoryTree($dir . $value . '/');
} else {
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
$allowedFormat = $this->checkFormat($ext);
if ($allowedFormat == true) {
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? 'site' : 'administrator') . DIRECTORY_SEPARATOR . $this->template->element, '', $dir . $value);
$relativePath = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR . ($this->template->client_id === 0 ? '' : 'administrator' . DIRECTORY_SEPARATOR) . 'templates' . DIRECTORY_SEPARATOR . $this->template->element, '', $relativePath);
$result[] = $this->getFile($relativePath, $value);
}
}
}
}
return !empty($result) ? $result : ['.'];
}
/**
* Method to get the core file of override file
*
* @param string $file Override file
* @param integer $client_id Client Id
*
* @return string $corefile The full path and file name for the target file, or boolean false if the file is not found in any of the paths.
*
* @since 4.0.0
*/
public function getCoreFile($file, $client_id)
{
$app = Factory::getApplication();
$filePath = Path::clean($file);
$explodeArray = explode(DIRECTORY_SEPARATOR, $filePath);
// Only allow html/ folder
if ($explodeArray['1'] !== 'html') {
return false;
}
$fileName = basename($filePath);
$type = $explodeArray['2'];
$client = ApplicationHelper::getClientInfo($client_id);
$componentPath = Path::clean($client->path . '/components/');
$modulePath = Path::clean($client->path . '/modules/');
$layoutPath = Path::clean(JPATH_ROOT . '/layouts/');
// For modules
if (stristr($type, 'mod_') !== false) {
$folder = $explodeArray['2'];
$htmlPath = Path::clean($modulePath . $folder . '/tmpl/');
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
} elseif (stristr($type, 'com_') !== false) {
// For components
$folder = $explodeArray['2'];
$subFolder = $explodeArray['3'];
$fileName = $this->getSafeName($fileName);
// The new scheme, if a view has a tmpl folder
$newHtmlPath = Path::clean($componentPath . $folder . '/tmpl/' . $subFolder . '/');
if (!$coreFile = Path::find($newHtmlPath, $fileName)) {
// The old scheme, the views are directly in the component/tmpl folder
$oldHtmlPath = Path::clean($componentPath . $folder . '/views/' . $subFolder . '/tmpl/');
$coreFile = Path::find($oldHtmlPath, $fileName);
return $coreFile;
}
return $coreFile;
} elseif (stristr($type, 'layouts') !== false) {
// For Layouts
$subtype = $explodeArray['3'];
if (stristr($subtype, 'com_')) {
$folder = $explodeArray['3'];
$subFolder = array_slice($explodeArray, 4, -1);
$subFolder = implode(DIRECTORY_SEPARATOR, $subFolder);
$htmlPath = Path::clean($componentPath . $folder . '/layouts/' . $subFolder);
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
} elseif (stristr($subtype, 'joomla') || stristr($subtype, 'libraries') || stristr($subtype, 'plugins')) {
$subFolder = array_slice($explodeArray, 3, -1);
$subFolder = implode(DIRECTORY_SEPARATOR, $subFolder);
$htmlPath = Path::clean($layoutPath . $subFolder);
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
}
}
return false;
}
/**
* Creates a safe file name for the given name.
*
* @param string $name The filename
*
* @return string $fileName The filtered name without Date
*
* @since 4.0.0
*/
private function getSafeName($name)
{
if (strpos($name, '-') !== false && preg_match('/[0-9]/', $name)) {
// Get the extension
$extension = File::getExt($name);
// Remove ( Date ) from file
$explodeArray = explode('-', $name);
$size = count($explodeArray);
$date = $explodeArray[$size - 2] . '-' . str_replace('.' . $extension, '', $explodeArray[$size - 1]);
if ($this->validateDate($date)) {
$nameWithoutExtension = implode('-', array_slice($explodeArray, 0, -2));
// Filtered name
$name = $nameWithoutExtension . '.' . $extension;
}
}
return $name;
}
/**
* Validate Date in file name.
*
* @param string $date Date to validate.
*
* @return boolean Return true if date is valid and false if not.
*
* @since 4.0.0
*/
private function validateDate($date)
{
$format = 'Ymd-His';
$valid = Date::createFromFormat($format, $date);
return $valid && $valid->format($format) === $date;
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since 1.6
*/
protected function populateState()
{
$app = Factory::getApplication();
// Load the User state.
$pk = $app->getInput()->getInt('id');
$this->setState('extension.id', $pk);
// Load the parameters.
$params = ComponentHelper::getParams('com_templates');
$this->setState('params', $params);
}
/**
* Method to get the template information.
*
* @return mixed Object if successful, false if not and internal error is set.
*
* @since 1.6
*/
public function &getTemplate()
{
if (empty($this->template)) {
$pk = (int) $this->getState('extension.id');
$db = $this->getDatabase();
$app = Factory::getApplication();
// Get the template information.
$query = $db->getQuery(true)
->select($db->quoteName(['extension_id', 'client_id', 'element', 'name', 'manifest_cache']))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('extension_id') . ' = :pk')
->where($db->quoteName('type') . ' = ' . $db->quote('template'))
->bind(':pk', $pk, ParameterType::INTEGER);
$db->setQuery($query);
try {
$result = $db->loadObject();
} catch (\RuntimeException $e) {
$app->enqueueMessage($e->getMessage(), 'warning');
$this->template = false;
return false;
}
if (empty($result)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'), 'error');
$this->template = false;
} else {
$this->template = $result;
// Client ID is not always an integer, so enforce here
$this->template->client_id = (int) $this->template->client_id;
if (!isset($this->template->xmldata)) {
$this->template->xmldata = TemplatesHelper::parseXMLTemplateFile($this->template->client_id === 0 ? JPATH_ROOT : JPATH_ROOT . '/administrator', $this->template->element);
}
}
}
return $this->template;
}
/**
* Method to check if new template name already exists
*
* @return boolean true if name is not used, false otherwise
*
* @since 2.5
*/
public function checkNewName()
{
$db = $this->getDatabase();
$name = $this->getState('new_name');
$query = $db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__extensions'))
->where($db->quoteName('name') . ' = :name')
->bind(':name', $name);
$db->setQuery($query);
return ($db->loadResult() == 0);
}
/**
* Method to check if new template name already exists
*
* @return string name of current template
*
* @since 2.5
*/
public function getFromName()
{
return $this->getTemplate()->element;
}
/**
* Method to check if new template name already exists
*
* @return boolean true if name is not used, false otherwise
*
* @since 2.5
*/
public function copy()
{
$app = Factory::getApplication();
if ($template = $this->getTemplate()) {
$client = ApplicationHelper::getClientInfo($template->client_id);
$fromPath = Path::clean($client->path . '/templates/' . $template->element . '/');
// Delete new folder if it exists
$toPath = $this->getState('to_path');
if (Folder::exists($toPath)) {
if (!Folder::delete($toPath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
}
// Copy all files from $fromName template to $newName folder
if (!Folder::copy($fromPath, $toPath)) {
return false;
}
// Check manifest for additional files
$manifest = simplexml_load_file($toPath . '/templateDetails.xml');
// Copy language files from global folder
if ($languages = $manifest->languages) {
$folder = (string) $languages->attributes()->folder;
$languageFiles = $languages->language;
Folder::create($toPath . '/' . $folder . '/' . $languageFiles->attributes()->tag);
foreach ($languageFiles as $languageFile) {
$src = Path::clean($client->path . '/language/' . $languageFile);
$dst = Path::clean($toPath . '/' . $folder . '/' . $languageFile);
if (is_file($src)) {
File::copy($src, $dst);
}
}
}
// Copy media files
if ($media = $manifest->media) {
$folder = (string) $media->attributes()->folder;
$destination = (string) $media->attributes()->destination;
Folder::copy(JPATH_SITE . '/media/' . $destination, $toPath . '/' . $folder);
}
// Adjust to new template name
if (!$this->fixTemplateName()) {
return false;
}
return true;
}
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
return false;
}
/**
* Method to delete tmp folder
*
* @return boolean true if delete successful, false otherwise
*
* @since 2.5
*/
public function cleanup()
{
// Clear installation messages
$app = Factory::getApplication();
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
// Delete temporary directory
return Folder::delete($this->getState('to_path'));
}
/**
* Method to rename the template in the XML files and rename the language files
*
* @return boolean true if successful, false otherwise
*
* @since 2.5
*/
protected function fixTemplateName()
{
// Rename Language files
// Get list of language files
$result = true;
$files = Folder::files($this->getState('to_path'), '\.ini$', true, true);
$newName = strtolower($this->getState('new_name'));
$template = $this->getTemplate();
$oldName = $template->element;
$manifest = json_decode($template->manifest_cache);
foreach ($files as $file) {
$newFile = '/' . str_replace($oldName, $newName, basename($file));
$result = File::move($file, dirname($file) . $newFile) && $result;
}
// Edit XML file
$xmlFile = $this->getState('to_path') . '/templateDetails.xml';
if (is_file($xmlFile)) {
$contents = file_get_contents($xmlFile);
$pattern[] = '#<name>\s*' . $manifest->name . '\s*</name>#i';
$replace[] = '<name>' . $newName . '</name>';
$pattern[] = '#<language(.*)' . $oldName . '(.*)</language>#';
$replace[] = '<language${1}' . $newName . '${2}</language>';
$pattern[] = '#<media(.*)' . $oldName . '(.*)>#';
$replace[] = '<media${1}' . $newName . '${2}>';
$contents = preg_replace($pattern, $replace, $contents);
$result = File::write($xmlFile, $contents) && $result;
}
return $result;
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return Form|boolean A Form object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = [], $loadData = true)
{
$app = Factory::getApplication();
// Codemirror or Editor None should be enabled
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select('COUNT(*)')
->from('#__extensions as a')
->where(
'(a.name =' . $db->quote('plg_editors_codemirror') .
' AND a.enabled = 1) OR (a.name =' .
$db->quote('plg_editors_none') .
' AND a.enabled = 1)'
);
$db->setQuery($query);
$state = $db->loadResult();
if ((int) $state < 1) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning');
}
// Get the form.
$form = $this->loadForm('com_templates.source', 'source', ['control' => 'jform', 'load_data' => $loadData]);
if (empty($form)) {
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
$data = $this->getSource();
$this->preprocessData('com_templates.source', $data);
return $data;
}
/**
* Method to get a single record.
*
* @return mixed Object on success, false on failure.
*
* @since 1.6
*/
public function &getSource()
{
$app = Factory::getApplication();
$item = new \stdClass();
if (!$this->template) {
$this->getTemplate();
}
if ($this->template) {
$input = Factory::getApplication()->getInput();
$fileName = base64_decode($input->get('file'));
$fileName = str_replace('//', '/', $fileName);
$isMedia = $input->getInt('isMedia', 0);
$fileName = $isMedia ? Path::clean(JPATH_ROOT . '/media/templates/' . ($this->template->client_id === 0 ? 'site' : 'administrator') . '/' . $this->template->element . $fileName)
: Path::clean(JPATH_ROOT . ($this->template->client_id === 0 ? '' : '/administrator') . '/templates/' . $this->template->element . $fileName);
try {
$filePath = Path::check($fileName);
} catch (\Exception $e) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'), 'error');
return;
}
if (file_exists($filePath)) {
$item->extension_id = $this->getState('extension.id');
$item->filename = Path::clean($fileName);
$item->source = file_get_contents($filePath);
$item->filePath = Path::clean($filePath);
$ds = DIRECTORY_SEPARATOR;
$cleanFileName = str_replace(JPATH_ROOT . ($this->template->client_id === 1 ? $ds . 'administrator' . $ds : $ds) . 'templates' . $ds . $this->template->element, '', $fileName);
if ($coreFile = $this->getCoreFile($cleanFileName, $this->template->client_id)) {
$item->coreFile = $coreFile;
$item->core = file_get_contents($coreFile);
}
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'), 'error');
}
}
return $item;
}
/**
* Method to store the source file contents.
*
* @param array $data The source data to save.
*
* @return boolean True on success, false otherwise and internal error set.
*
* @since 1.6
*/
public function save($data)
{
// Get the template.
$template = $this->getTemplate();
if (empty($template)) {
return false;
}
$app = Factory::getApplication();
$fileName = base64_decode($app->getInput()->get('file'));
$isMedia = $app->getInput()->getInt('isMedia', 0);
$fileName = $isMedia ? JPATH_ROOT . '/media/templates/' . ($this->template->client_id === 0 ? 'site' : 'administrator') . '/' . $this->template->element . $fileName :
JPATH_ROOT . '/' . ($this->template->client_id === 0 ? '' : 'administrator/') . 'templates/' . $this->template->element . $fileName;
$filePath = Path::clean($fileName);
// Include the extension plugins for the save events.
PluginHelper::importPlugin('extension');
$user = get_current_user();
chown($filePath, $user);
Path::setPermissions($filePath, '0644');
// Try to make the template file writable.
if (!is_writable($filePath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE'), 'warning');
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_FILE_PERMISSIONS', Path::getPermissions($filePath)), 'warning');
if (!Path::isOwner($filePath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_CHECK_FILE_OWNERSHIP'), 'warning');
}
return false;
}
// Make sure EOL is Unix
$data['source'] = str_replace(["\r\n", "\r"], "\n", $data['source']);
// If the asset file for the template ensure we have valid template so we don't instantly destroy it
if (str_ends_with($fileName, '/joomla.asset.json') && json_decode($data['source']) === null) {
$this->setError(Text::_('COM_TEMPLATES_ERROR_ASSET_FILE_INVALID_JSON'));
return false;
}
$return = File::write($filePath, $data['source']);
if (!$return) {
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName), 'error');
return false;
}
// Get the extension of the changed file.
$explodeArray = explode('.', $fileName);
$ext = end($explodeArray);
if ($ext == 'less') {
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_COMPILE_LESS', $fileName));
}
return true;
}
/**
* Get overrides folder.
*
* @param string $name The name of override.
* @param string $path Location of override.
*
* @return object containing override name and path.
*
* @since 3.2
*/
public function getOverridesFolder($name, $path)
{
$folder = new \stdClass();
$folder->name = $name;
$folder->path = base64_encode($path . $name);
return $folder;
}
/**
* Get a list of overrides.
*
* @return array containing overrides.
*
* @since 3.2
*/
public function getOverridesList()
{
if ($template = $this->getTemplate()) {
$client = ApplicationHelper::getClientInfo($template->client_id);
$componentPath = Path::clean($client->path . '/components/');
$modulePath = Path::clean($client->path . '/modules/');
$pluginPath = Path::clean(JPATH_ROOT . '/plugins/');
$layoutPath = Path::clean(JPATH_ROOT . '/layouts/');
$components = Folder::folders($componentPath);
foreach ($components as $component) {
// Collect the folders with views
$folders = Folder::folders($componentPath . '/' . $component, '^view[s]?$', false, true);
$folders = array_merge($folders, Folder::folders($componentPath . '/' . $component, '^tmpl?$', false, true));
if (!$folders) {
continue;
}
foreach ($folders as $folder) {
// The subfolders are views
$views = Folder::folders($folder);
foreach ($views as $view) {
// The old scheme, if a view has a tmpl folder
$path = $folder . '/' . $view . '/tmpl';
// The new scheme, the views are directly in the component/tmpl folder
if (!is_dir($path) && substr($folder, -4) == 'tmpl') {
$path = $folder . '/' . $view;
}
// Check if the folder exists
if (!is_dir($path)) {
continue;
}
$result['components'][$component][] = $this->getOverridesFolder($view, Path::clean($folder . '/'));
}
}
}
foreach (Folder::folders($pluginPath) as $pluginGroup) {
foreach (Folder::folders($pluginPath . '/' . $pluginGroup) as $plugin) {
if (file_exists($pluginPath . '/' . $pluginGroup . '/' . $plugin . '/tmpl/')) {
$pluginLayoutPath = Path::clean($pluginPath . '/' . $pluginGroup . '/');
$result['plugins'][$pluginGroup][] = $this->getOverridesFolder($plugin, $pluginLayoutPath);
}
}
}
$modules = Folder::folders($modulePath);
foreach ($modules as $module) {
$result['modules'][] = $this->getOverridesFolder($module, $modulePath);
}
$layoutFolders = Folder::folders($layoutPath);
foreach ($layoutFolders as $layoutFolder) {
$layoutFolderPath = Path::clean($layoutPath . '/' . $layoutFolder . '/');
$layouts = Folder::folders($layoutFolderPath);
foreach ($layouts as $layout) {
$result['layouts'][$layoutFolder][] = $this->getOverridesFolder($layout, $layoutFolderPath);
}
}
// Check for layouts in component folders
foreach ($components as $component) {
if (file_exists($componentPath . '/' . $component . '/layouts/')) {
$componentLayoutPath = Path::clean($componentPath . '/' . $component . '/layouts/');
if ($componentLayoutPath) {
$layouts = Folder::folders($componentLayoutPath);
foreach ($layouts as $layout) {
$result['layouts'][$component][] = $this->getOverridesFolder($layout, $componentLayoutPath);
}
}
}
}
}
if (!empty($result)) {
return $result;
}
}
/**
* Create overrides.
*
* @param string $override The override location.
*
* @return boolean true if override creation is successful, false otherwise
*
* @since 3.2
*/
public function createOverride($override)
{
if ($template = $this->getTemplate()) {
$app = Factory::getApplication();
$explodeArray = explode(DIRECTORY_SEPARATOR, $override);
$name = end($explodeArray);
$client = ApplicationHelper::getClientInfo($template->client_id);
if (stristr($name, 'mod_') != false) {
$htmlPath = Path::clean($client->path . '/templates/' . $template->element . '/html/' . $name);
} elseif (stristr($override, 'com_') != false) {
$size = count($explodeArray);
$url = Path::clean($explodeArray[$size - 3] . '/' . $explodeArray[$size - 1]);
if ($explodeArray[$size - 2] == 'layouts') {
$htmlPath = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/' . $url);
} else {
$htmlPath = Path::clean($client->path . '/templates/' . $template->element . '/html/' . $url);
}
} elseif (stripos($override, Path::clean(JPATH_ROOT . '/plugins/')) === 0) {
$size = count($explodeArray);
$layoutPath = Path::clean('plg_' . $explodeArray[$size - 2] . '_' . $explodeArray[$size - 1]);
$htmlPath = Path::clean($client->path . '/templates/' . $template->element . '/html/' . $layoutPath);
} else {
$layoutPath = implode('/', array_slice($explodeArray, -2));
$htmlPath = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/' . $layoutPath);
}
// Check Html folder, create if not exist
if (!Folder::exists($htmlPath) && !Folder::create($htmlPath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FOLDER_ERROR'), 'error');
return false;
}
if (stristr($name, 'mod_') != false) {
$return = $this->createTemplateOverride(Path::clean($override . '/tmpl'), $htmlPath);
} elseif (stristr($override, 'com_') != false && stristr($override, 'layouts') == false) {
$path = $override . '/tmpl';
// View can also be in the top level folder
if (!is_dir($path)) {
$path = $override;
}
$return = $this->createTemplateOverride(Path::clean($path), $htmlPath);
} elseif (stripos($override, Path::clean(JPATH_ROOT . '/plugins/')) === 0) {
$return = $this->createTemplateOverride(Path::clean($override . '/tmpl'), $htmlPath);
} else {
$return = $this->createTemplateOverride($override, $htmlPath);
}
if ($return) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_OVERRIDE_CREATED') . str_replace(JPATH_ROOT, '', $htmlPath));
return true;
}
$app->enqueueMessage(Text::_('COM_TEMPLATES_OVERRIDE_FAILED'), 'error');
}
return false;
}
/**
* Create override folder & file
*
* @param string $overridePath The override location
* @param string $htmlPath The html location
*
* @return boolean True on success. False otherwise.
*/
public function createTemplateOverride($overridePath, $htmlPath)
{
$return = false;
if (empty($overridePath) || empty($htmlPath)) {
return $return;
}
// Get list of template folders
$folders = Folder::folders($overridePath, null, true, true);
if (!empty($folders)) {
foreach ($folders as $folder) {
$htmlFolder = $htmlPath . str_replace($overridePath, '', $folder);
if (!Folder::exists($htmlFolder)) {
Folder::create($htmlFolder);
}
}
}
// Get list of template files (Only get *.php file for template file)
$files = Folder::files($overridePath, '.php', true, true);
if (empty($files)) {
return true;
}
foreach ($files as $file) {
$overrideFilePath = str_replace($overridePath, '', $file);
$htmlFilePath = $htmlPath . $overrideFilePath;
if (is_file($htmlFilePath)) {
// Generate new unique file name base on current time
$today = Factory::getDate();
$htmlFilePath = File::stripExt($htmlFilePath) . '-' . $today->format('Ymd-His') . '.' . File::getExt($htmlFilePath);
}
$return = File::copy($file, $htmlFilePath, '', true);
}
return $return;
}
/**
* Delete a particular file.
*
* @param string $file The relative location of the file.
*
* @return boolean True if file deletion is successful, false otherwise
*
* @since 3.2
*/
public function deleteFile($file)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$filePath = $this->getBasePath() . urldecode(base64_decode($file));
$return = File::delete($filePath);
if (!$return) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_DELETE_ERROR'), 'error');
return false;
}
return true;
}
}
/**
* Create new file.
*
* @param string $name The name of file.
* @param string $type The extension of the file.
* @param string $location Location for the new file.
*
* @return boolean true if file created successfully, false otherwise
*
* @since 3.2
*/
public function createFile($name, $type, $location)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$base = $this->getBasePath();
if (file_exists(Path::clean($base . '/' . $location . '/' . $name . '.' . $type))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
return false;
}
if (!fopen(Path::clean($base . '/' . $location . '/' . $name . '.' . $type), 'x')) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_CREATE_ERROR'), 'error');
return false;
}
// Check if the format is allowed and will be showed in the backend
$check = $this->checkFormat($type);
// Add a message if we are not allowed to show this file in the backend.
if (!$check) {
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_WARNING_FORMAT_WILL_NOT_BE_VISIBLE', $type), 'warning');
}
return true;
}
}
/**
* Upload new file.
*
* @param array $file The uploaded file array.
* @param string $location Location for the new file.
*
* @return boolean True if file uploaded successfully, false otherwise
*
* @since 3.2
*/
public function uploadFile($file, $location)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = $this->getBasePath();
$fileName = File::makeSafe($file['name']);
$err = null;
if (!TemplateHelper::canUpload($file, $err)) {
// Can't upload the file
return false;
}
if (file_exists(Path::clean($path . '/' . $location . '/' . $file['name']))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
return false;
}
if (!File::upload($file['tmp_name'], Path::clean($path . '/' . $location . '/' . $fileName))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_UPLOAD_ERROR'), 'error');
return false;
}
$url = Path::clean($location . '/' . $fileName);
return $url;
}
}
/**
* Create new folder.
*
* @param string $name The name of the new folder.
* @param string $location Location for the new folder.
*
* @return boolean True if override folder is created successfully, false otherwise
*
* @since 3.2
*/
public function createFolder($name, $location)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = Path::clean($location . '/');
$base = $this->getBasePath();
if (file_exists(Path::clean($base . $path . $name))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FOLDER_EXISTS'), 'error');
return false;
}
if (!Folder::create(Path::clean($base . $path . $name))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FOLDER_CREATE_ERROR'), 'error');
return false;
}
return true;
}
}
/**
* Delete a folder.
*
* @param string $location The name and location of the folder.
*
* @return boolean True if override folder is deleted successfully, false otherwise
*
* @since 3.2
*/
public function deleteFolder($location)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$base = $this->getBasePath();
$path = Path::clean($location . '/');
if (!file_exists($base . $path)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FOLDER_NOT_EXISTS'), 'error');
return false;
}
$return = Folder::delete($base . $path);
if (!$return) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FOLDER_DELETE_ERROR'), 'error');
return false;
}
return true;
}
}
/**
* Rename a file.
*
* @param string $file The name and location of the old file
* @param string $name The new name of the file.
*
* @return string Encoded string containing the new file location.
*
* @since 3.2
*/
public function renameFile($file, $name)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = $this->getBasePath();
$fileName = base64_decode($file);
$explodeArray = explode('.', $fileName);
$type = end($explodeArray);
$explodeArray = explode('/', $fileName);
$newName = str_replace(end($explodeArray), $name . '.' . $type, $fileName);
if (file_exists($path . $newName)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
return false;
}
if (!rename($path . $fileName, $path . $newName)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_RENAME_ERROR'), 'error');
return false;
}
return base64_encode($newName);
}
}
/**
* Get an image address, height and width.
*
* @return array an associative array containing image address, height and width.
*
* @since 3.2
*/
public function getImage()
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$fileName = base64_decode($app->getInput()->get('file'));
$path = $this->getBasePath();
$uri = Uri::root(false) . ltrim(str_replace(JPATH_ROOT, '', $this->getBasePath()), '/');
if (file_exists(Path::clean($path . $fileName))) {
$JImage = new Image(Path::clean($path . $fileName));
$image['address'] = $uri . $fileName;
$image['path'] = $fileName;
$image['height'] = $JImage->getHeight();
$image['width'] = $JImage->getWidth();
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_IMAGE_FILE_NOT_FOUND'), 'error');
return false;
}
return $image;
}
}
/**
* Crop an image.
*
* @param string $file The name and location of the file
* @param string $w width.
* @param string $h height.
* @param string $x x-coordinate.
* @param string $y y-coordinate.
*
* @return boolean true if image cropped successfully, false otherwise.
*
* @since 3.2
*/
public function cropImage($file, $w, $h, $x, $y)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = $this->getBasePath() . base64_decode($file);
try {
$image = new Image($path);
$properties = $image->getImageFileProperties($path);
switch ($properties->mime) {
case 'image/webp':
$imageType = \IMAGETYPE_WEBP;
break;
case 'image/png':
$imageType = \IMAGETYPE_PNG;
break;
case 'image/gif':
$imageType = \IMAGETYPE_GIF;
break;
default:
$imageType = \IMAGETYPE_JPEG;
}
$image->crop($w, $h, $x, $y, false);
$image->toFile($path, $imageType);
return true;
} catch (\Exception $e) {
$app->enqueueMessage($e->getMessage(), 'error');
}
}
}
/**
* Resize an image.
*
* @param string $file The name and location of the file
* @param string $width The new width of the image.
* @param string $height The new height of the image.
*
* @return boolean true if image resize successful, false otherwise.
*
* @since 3.2
*/
public function resizeImage($file, $width, $height)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = $this->getBasePath() . base64_decode($file);
try {
$image = new Image($path);
$properties = $image->getImageFileProperties($path);
switch ($properties->mime) {
case 'image/webp':
$imageType = \IMAGETYPE_WEBP;
break;
case 'image/png':
$imageType = \IMAGETYPE_PNG;
break;
case 'image/gif':
$imageType = \IMAGETYPE_GIF;
break;
default:
$imageType = \IMAGETYPE_JPEG;
}
$image->resize($width, $height, false, Image::SCALE_FILL);
$image->toFile($path, $imageType);
return true;
} catch (\Exception $e) {
$app->enqueueMessage($e->getMessage(), 'error');
}
}
}
/**
* Template preview.
*
* @return object object containing the id of the template.
*
* @since 3.2
*/
public function getPreview()
{
$app = Factory::getApplication();
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName(['id', 'client_id']));
$query->from($db->quoteName('#__template_styles'));
$query->where($db->quoteName('template') . ' = :template')
->bind(':template', $this->template->element);
$db->setQuery($query);
try {
$result = $db->loadObject();
} catch (\RuntimeException $e) {
$app->enqueueMessage($e->getMessage(), 'warning');
}
if (empty($result)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'), 'warning');
} else {
return $result;
}
}
/**
* Rename a file.
*
* @return mixed array on success, false on failure
*
* @since 3.2
*/
public function getFont()
{
if ($template = $this->getTemplate()) {
$app = Factory::getApplication();
$client = ApplicationHelper::getClientInfo($template->client_id);
$relPath = base64_decode($app->getInput()->get('file'));
$explodeArray = explode('/', $relPath);
$fileName = end($explodeArray);
$path = $this->getBasePath() . base64_decode($app->getInput()->get('file'));
if (stristr($client->path, 'administrator') == false) {
$folder = '/templates/';
} else {
$folder = '/administrator/templates/';
}
$uri = Uri::root(true) . $folder . $template->element;
if (file_exists(Path::clean($path))) {
$font['address'] = $uri . $relPath;
$font['rel_path'] = $relPath;
$font['name'] = $fileName;
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND'), 'error');
return false;
}
return $font;
}
}
/**
* Copy a file.
*
* @param string $newName The name of the copied file
* @param string $location The final location where the file is to be copied
* @param string $file The name and location of the file
*
* @return boolean true if image resize successful, false otherwise.
*
* @since 3.2
*/
public function copyFile($newName, $location, $file)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$relPath = base64_decode($file);
$explodeArray = explode('.', $relPath);
$ext = end($explodeArray);
$path = $this->getBasePath();
$newPath = Path::clean($path . $location . '/' . $newName . '.' . $ext);
if (file_exists($newPath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_EXISTS'), 'error');
return false;
}
if (File::copy($path . $relPath, $newPath)) {
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_FILE_COPY_SUCCESS', $newName . '.' . $ext));
return true;
}
}
return false;
}
/**
* Get the compressed files.
*
* @return array if file exists, false otherwise
*
* @since 3.2
*/
public function getArchive()
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$path = $this->getBasePath() . base64_decode($app->getInput()->get('file'));
if (file_exists(Path::clean($path))) {
$files = [];
$zip = new \ZipArchive();
if ($zip->open($path) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
$files[] = $entry;
}
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL'), 'error');
return false;
}
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_FONT_FILE_NOT_FOUND'), 'error');
return false;
}
return $files;
}
}
/**
* Extract contents of an archive file.
*
* @param string $file The name and location of the file
*
* @return boolean true if image extraction is successful, false otherwise.
*
* @since 3.2
*/
public function extractArchive($file)
{
if ($this->getTemplate()) {
$app = Factory::getApplication();
$relPath = base64_decode($file);
$explodeArray = explode('/', $relPath);
$fileName = end($explodeArray);
$path = $this->getBasePath() . base64_decode($file);
if (file_exists(Path::clean($path . '/' . $fileName))) {
$zip = new \ZipArchive();
if ($zip->open(Path::clean($path . '/' . $fileName)) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
if (file_exists(Path::clean($path . '/' . $entry))) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_EXISTS'), 'error');
return false;
}
}
$zip->extractTo($path);
return true;
}
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_OPEN_FAIL'), 'error');
} else {
$app->enqueueMessage(Text::_('COM_TEMPLATES_FILE_ARCHIVE_NOT_FOUND'), 'error');
}
}
return false;
}
/**
* Check if the extension is allowed and will be shown in the template manager
*
* @param string $ext The extension to check if it is allowed
*
* @return boolean true if the extension is allowed false otherwise
*
* @since 3.6.0
*/
protected function checkFormat($ext)
{
if (!isset($this->allowedFormats)) {
$params = ComponentHelper::getParams('com_templates');
$imageTypes = explode(',', $params->get('image_formats', 'gif,bmp,jpg,jpeg,png,webp'));
$sourceTypes = explode(',', $params->get('source_formats', 'txt,less,ini,xml,js,php,css,scss,sass,json'));
$fontTypes = explode(',', $params->get('font_formats', 'woff,woff2,ttf,otf'));
$archiveTypes = explode(',', $params->get('compressed_formats', 'zip'));
$this->allowedFormats = array_merge($imageTypes, $sourceTypes, $fontTypes, $archiveTypes);
$this->allowedFormats = array_map('strtolower', $this->allowedFormats);
}
return in_array(strtolower($ext), $this->allowedFormats);
}
/**
* Method to get a list of all the files to edit in a template's media folder.
*
* @return array A nested array of relevant files.
*
* @since 4.1.0
*/
public function getMediaFiles()
{
$result = [];
$template = $this->getTemplate();
if (!isset($template->xmldata)) {
$template->xmldata = TemplatesHelper::parseXMLTemplateFile($template->client_id === 0 ? JPATH_ROOT : JPATH_ROOT . '/administrator', $template->element);
}
if (!isset($template->xmldata->inheritable) || (isset($template->xmldata->parent) && $template->xmldata->parent === '')) {
return $result;
}
$app = Factory::getApplication();
$path = Path::clean(JPATH_ROOT . '/media/templates/' . ($template->client_id === 0 ? 'site' : 'administrator') . '/' . $template->element . '/');
$this->mediaElement = $path;
if (!is_writable($path)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_DIRECTORY_NOT_WRITABLE'), 'error');
}
if (is_dir($path)) {
$result = $this->getDirectoryTree($path);
}
return $result;
}
/**
* Method to resolve the base folder.
*
* @return string The absolute path for the base.
*
* @since 4.1.0
*/
private function getBasePath()
{
$app = Factory::getApplication();
$isMedia = $app->getInput()->getInt('isMedia', 0);
return $isMedia ? JPATH_ROOT . '/media/templates/' . ($this->template->client_id === 0 ? 'site' : 'administrator') . '/' . $this->template->element :
JPATH_ROOT . '/' . ($this->template->client_id === 0 ? '' : 'administrator/') . 'templates/' . $this->template->element;
}
/**
* Method to create the templateDetails.xml for the child template
*
* @return boolean true if name is not used, false otherwise
*
* @since 4.1.0
*/
public function child()
{
$app = Factory::getApplication();
$template = $this->getTemplate();
if (!(array) $template) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
$client = ApplicationHelper::getClientInfo($template->client_id);
$fromPath = Path::clean($client->path . '/templates/' . $template->element . '/templateDetails.xml');
// Delete new folder if it exists
$toPath = $this->getState('to_path');
if (Folder::exists($toPath)) {
if (!Folder::delete($toPath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
} else {
if (!Folder::create($toPath)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
}
// Create the html folder
Folder::create($toPath . '/html');
// Copy the template definition from the parent template
if (!File::copy($fromPath, $toPath . '/templateDetails.xml')) {
return false;
}
// Check manifest for additional files
$newName = strtolower($this->getState('new_name'));
$template = $this->getTemplate();
// Edit XML file
$xmlFile = Path::clean($this->getState('to_path') . '/templateDetails.xml');
if (!is_file($xmlFile)) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
return false;
}
try {
$xml = simplexml_load_string(file_get_contents($xmlFile));
} catch (\Exception $e) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_READ'), 'error');
return false;
}
$user = $this->getCurrentUser();
unset($xml->languages);
unset($xml->media);
unset($xml->files);
unset($xml->parent);
unset($xml->inheritable);
// Remove the update parts
unset($xml->update);
unset($xml->updateservers);
if (isset($xml->creationDate)) {
$xml->creationDate = (new Date('now'))->format('F Y');
} else {
$xml->addChild('creationDate', (new Date('now'))->format('F Y'));
}
if (isset($xml->author)) {
$xml->author = $user->name;
} else {
$xml->addChild('author', $user->name);
}
if (isset($xml->authorEmail)) {
$xml->authorEmail = $user->email;
} else {
$xml->addChild('authorEmail', $user->email);
}
$files = $xml->addChild('files');
$files->addChild('filename', 'templateDetails.xml');
$files->addChild('folder', 'html');
// Media folder
$media = $xml->addChild('media');
$media->addAttribute('folder', 'media');
$media->addAttribute('destination', 'templates/' . ($template->client_id === 0 ? 'site/' : 'administrator/') . $template->element . '_' . $newName);
$media->addChild('folder', 'css');
$media->addChild('folder', 'js');
$media->addChild('folder', 'images');
$media->addChild('folder', 'scss');
$xml->name = $template->element . '_' . $newName;
if (isset($xml->namespace)) {
$xml->namespace .= '_' . ucfirst($newName);
}
$xml->inheritable = 0;
$files = $xml->addChild('parent', $template->element);
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$result = File::write($xmlFile, $dom->saveXML());
if (!$result) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
// Create an empty media folder structure
if (
!Folder::create($toPath . '/media')
|| !Folder::create($toPath . '/media/css')
|| !Folder::create($toPath . '/media/js')
|| !Folder::create($toPath . '/media/images')
|| !Folder::create($toPath . '/media/scss')
) {
return false;
}
return true;
}
/**
* Method to get the parent template existing styles
*
* @return array array of id,titles of the styles
*
* @since 4.1.3
*/
public function getAllTemplateStyles()
{
$template = $this->getTemplate();
if (empty($template->xmldata->inheritable)) {
return [];
}
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName(['id', 'title']))
->from($db->quoteName('#__template_styles'))
->where($db->quoteName('client_id') . ' = :client_id', 'AND')
->where($db->quoteName('template') . ' = :template')
->orWhere($db->quoteName('parent') . ' = :parent')
->bind(':client_id', $template->client_id, ParameterType::INTEGER)
->bind(':template', $template->element)
->bind(':parent', $template->element);
$db->setQuery($query);
return $db->loadObjectList();
}
/**
* Method to copy selected styles to the child template
*
* @return boolean true if name is not used, false otherwise
*
* @since 4.1.3
*/
public function copyStyles()
{
$app = Factory::getApplication();
$template = $this->getTemplate();
$newName = strtolower($this->getState('new_name'));
$applyStyles = $this->getState('stylesToCopy');
// Get a db connection.
$db = $this->getDatabase();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(['title', 'params']))
->from($db->quoteName('#__template_styles'))
->whereIn($db->quoteName('id'), ArrayHelper::toInteger($applyStyles));
// Reset the query using our newly populated query object.
$db->setQuery($query);
try {
$parentStyle = $db->loadObjectList();
} catch (\Exception $e) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_STYLE_NOT_FOUND'), 'error');
return false;
}
foreach ($parentStyle as $style) {
$query = $db->getQuery(true);
$styleName = Text::sprintf('COM_TEMPLATES_COPY_CHILD_TEMPLATE_STYLES', ucfirst($template->element . '_' . $newName), $style->title);
// Insert columns and values
$columns = ['id', 'template', 'client_id', 'home', 'title', 'inheritable', 'parent', 'params'];
$values = [0, $db->quote($template->element . '_' . $newName), (int) $template->client_id, $db->quote('0'), $db->quote($styleName), 0, $db->quote($template->element), $db->quote($style->params)];
$query
->insert($db->quoteName('#__template_styles'))
->columns($db->quoteName($columns))
->values(implode(',', $values));
$db->setQuery($query);
try {
$db->execute();
} catch (\Exception $e) {
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_READ'), 'error');
return false;
}
}
return true;
}
}
The same content is in 2 releases, Joomla 5.0.0-beta2 to 5.0.0-beta3. See them all, or every version of this file.