Skip to content
This repository has been archived by the owner on Sep 17, 2018. It is now read-only.

Commit

Permalink
Load correct core files of override files (#2)
Browse files Browse the repository at this point in the history
Start implements loadcorefile() in administrator/components/com_templates/Model/TemplateModel.php
  • Loading branch information
anuragteapot authored and astridx committed May 23, 2018
1 parent 4c014d3 commit 3610beb
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
173 changes: 173 additions & 0 deletions administrator/components/com_templates/Model/TemplateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Templates\Administrator\Helper\TemplateHelper;
Expand Down Expand Up @@ -148,6 +153,174 @@ public function getDirectoryTree($dir)
return $result;
}

/**
* Method to get the core file of override file
*
* @param string $file Override file
* @param integer $client 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 __DEPLOY_VERSION__
*/
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')
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_ONLY_HTML_FOLDER_ALLOWED'), 'error');

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 = $this->findPath($htmlPath, $fileName);

return $coreFile;
}
elseif (stristr($type, 'com_') !== false)
{
// For components
$folder = $explodeArray['2'];
$subFolder = $explodeArray['3'];
$fileName = $this->getSafeName($fileName);

// The old scheme, if a view has a tmpl folder
$oldHtmlPath = Path::clean($componentPath . $folder . '/views/' . $subFolder . '/tmpl/');

if(!$coreFile = Path::find($oldHtmlPath, $fileName))
{
// The new scheme, the views are directly in the component/tmpl folder
$newHtmlPath = Path::clean($componentPath . $folder . '/tmpl/' . $subFolder . '/');
$coreFile = $this->findPath($newHtmlPath, $fileName);

return $coreFile;
}

return $coreFile;
}
elseif (stristr($type, 'layouts') !== false)
{
// For Jlayouts
$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 = $this->findPath($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 = $this->findPath($htmlPath, $fileName);

return $coreFile;
}
}

$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_CORE_FILE_NOT_FOUND'), 'error');

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 __DEPLOY_VERSION__
*/
private function getSafeName($name)
{
if (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 __DEPLOY_VERSION__
*/
private function validateDate($date)
{
$format = 'Ymd-His';
$valid = Date::createFromFormat($format, $date);

return $valid && $valid->format($format) == $date;
}

/**
* Find file in given folde
*
* @param string $path An path or array of path to search.
* @param string $file The file name to look for.
*
* @return mixed $path 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 __DEPLOY_VERSION__
*/
private function findPath($paths, $file)
{
$app = Factory::getApplication();

if(!$path = Path::find($paths, $file))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_CORE_FILE_NOT_FOUND'), 'error');
}

return $path;
}

/**
* Method to auto-populate the model state.
*
Expand Down
2 changes: 2 additions & 0 deletions administrator/language/en-GB/en-GB.com_templates.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ COM_TEMPLATES_DIRECTORY_NOT_WRITABLE="The template folder is not writable. Some
COM_TEMPLATES_ERR_XML="Template XML data not available"
COM_TEMPLATES_ERROR_CANNOT_DELETE_LAST_STYLE="Can't delete the last style of a template. To uninstall/delete a template go to: Extensions-> Manage-> Manage-> Select template to be deleted-> Select 'Uninstall'."
COM_TEMPLATES_ERROR_CANNOT_UNSET_DEFAULT_STYLE="Can't unset default style."
COM_TEMPLATES_ERROR_CORE_FILE_NOT_FOUND="Core file not found."
COM_TEMPLATES_ERROR_COULD_NOT_COPY="Unable to copy template files to temporary folder."
COM_TEMPLATES_ERROR_COULD_NOT_INSTALL="Unable to install new template from temporary folder."
COM_TEMPLATES_ERROR_COULD_NOT_WRITE="Unable to delete temporary folder."
Expand All @@ -63,6 +64,7 @@ COM_TEMPLATES_ERROR_IMAGE_FILE_NOT_FOUND="Image file not found."
COM_TEMPLATES_ERROR_INDEX_DELETE="The file index.php can't be deleted. Make changes in the editor if you want to change the file."
COM_TEMPLATES_ERROR_INVALID_FROM_NAME="Template to copy from can't be found."
COM_TEMPLATES_ERROR_INVALID_TEMPLATE_NAME="Invalid template name. Please use only letters, numbers, dashes and underscores."
COM_TEMPLATES_ERROR_ONLY_HTML_FOLDER_ALLOWED="Only html/ folder allowed."
COM_TEMPLATES_ERROR_RENAME_INDEX="The file index.php can't be renamed."
COM_TEMPLATES_ERROR_ROOT_DELETE="The root folder can't be deleted."
COM_TEMPLATES_ERROR_SAVE_DISABLED_TEMPLATE="Unable to save a style associated to a disabled template."
Expand Down

0 comments on commit 3610beb

Please sign in to comment.