Skip to content
Permalink
Browse files

Refactor theme configuration loading.

This also deals with both, uppercase and lowercase [C|c]onfig.xml
files.

Remaining part of the huge commit, see discussion there:

Impossible to upload some themes - issue #579
c745204
  • Loading branch information...
getdatakick committed Sep 6, 2018
1 parent 840be74 commit f3a171dbd88003b52f6fed1d0ff774d263eb10bd
Showing with 98 additions and 57 deletions.
  1. +3 −0 .gitignore
  2. +82 −32 classes/Theme.php
  3. +13 −25 controllers/admin/AdminThemesController.php
@@ -36,7 +36,10 @@ composer.lock
/config/settings.inc.php
/config/settings.old.php
/config/xml/*
!/config/xml/themes/
/config/xml/themes/*
!/config/xml/themes/default.xml
!/config/xml/themes/community-theme-default.xml

/config/defines_custom.inc.php
/config/settings_custom.inc.php
@@ -192,30 +192,20 @@ public static function getThemeInfo($idTheme)
$theme = new Theme((int) $idTheme);
$themeArr = [];
if (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
$configFile = _PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml';
} elseif ($theme->name == 'community-theme-default') {
$configFile = _PS_ROOT_DIR_.'/config/xml/themes/default.xml';
} else {
$configFile = false;
}
if ($configFile) {
$xmlTheme = $theme->loadConfigFile();
if ($xmlTheme) {
$themeArr['theme_id'] = (int) $theme->id;
$xmlTheme = @simplexml_load_file($configFile);
if ($xmlTheme !== false) {
foreach ($xmlTheme->attributes() as $key => $value) {
$themeArr['theme_'.$key] = (string) $value;
}
foreach ($xmlTheme->attributes() as $key => $value) {
$themeArr['theme_'.$key] = (string) $value;
}
foreach ($xmlTheme->author->attributes() as $key => $value) {
$themeArr['author_'.$key] = (string) $value;
}
foreach ($xmlTheme->author->attributes() as $key => $value) {
$themeArr['author_'.$key] = (string) $value;
}
if ($themeArr['theme_name'] == 'community-theme-default') {
$themeArr['tc'] = Module::isEnabled('themeconfigurator');
}
if ($themeArr['theme_name'] == 'community-theme-default') {
$themeArr['tc'] = Module::isEnabled('themeconfigurator');
}
} else {
// If no xml we use data from database
@@ -241,9 +231,12 @@ public static function getNonInstalledTheme()
$notInstalledTheme = [];
foreach (glob(_PS_ALL_THEMES_DIR_.'*', GLOB_ONLYDIR) as $themeDir) {
$dir = basename($themeDir);
$configFile = _PS_ALL_THEMES_DIR_.$dir.'/config.xml';
if (!in_array($dir, $installedThemeDirectories) && @filemtime($configFile)) {
if ($xmlTheme = @simplexml_load_file($configFile)) {
if (!in_array($dir, $installedThemeDirectories)) {
$xmlTheme = static::loadConfigFromFile(_PS_ALL_THEMES_DIR_.$dir.'/Config.xml', true);
if (! $xmlTheme) {
$xmlTheme = static::loadConfigFromFile(_PS_ALL_THEMES_DIR_.$dir.'/config.xml', true);
}
if ($xmlTheme) {
$theme = [];
foreach ($xmlTheme->attributes() as $key => $value) {
$theme[$key] = (string) $value;
@@ -557,18 +550,75 @@ public function toggleDefaultRightColumn()
*/
public function getConfiguration()
{
$target = _PS_ROOT_DIR_.'/themes/'.$this->name.'/Config.xml';
if (!file_exists($target)) {
$target = _PS_ROOT_DIR_.'/themes/'.$this->name.'/config.xml';
if (!file_exists($target)) {
return [];
$ob = $this->loadConfigFile();
if ($ob) {
// convert SimpleXMLElement to array
return json_decode(json_encode($ob), true);
}
return [];
}
/**
* Get the configuration file as SimpleXMLElement
*
* @param boolean $validate - if true, configuration file will be validated
*
* @return SimpleXMLElement | false
*
* @since 1.0.7
*/
public function loadConfigFile($validate = false)
{
return static::loadConfigFromFile($this->getConfigFilePath(), $validate);
}
/**
* Get the path to configuration file
*
* @return string | false
*
* @since 1.0.7
*/
protected function getConfigFilePath()
{
$path = _PS_ROOT_DIR_.'/config/xml/themes/'.$this->directory.'.xml';
if (file_exists($path)) {
return $path;
}
if ($this->name == 'community-theme-default') {
$path = _PS_ROOT_DIR_.'/config/xml/themes/default.xml';
if (file_exists($path)) {
return $path;
}
}
$xmlfile = file_get_contents($target);
$ob = simplexml_load_string($xmlfile);
$json = json_encode($ob);
return json_decode($json, true);
return false;
}
/**
* Get the configuration file as SimpleXMLElement
*
* @param string $filePath - path to xml config file to load
* @param boolean $validate - if true, configuration file will be validated
*
* @return SimpleXMLElement | false
*
* @since 1.0.7
*/
public static function loadConfigFromFile($filePath, $validate)
{
if (file_exists($filePath)) {
$content = @simplexml_load_file($filePath);
if ($content && $validate && !static::validateConfigFile($content)) {
return false;
}
return $content;
}
return false;
}
/**
@@ -1695,14 +1695,20 @@ protected function installTheme($themeDir, $sandbox = false, $redirect = true)
mkdir($sandbox);
Tools::recurseCopy(_PS_ALL_THEMES_DIR_.$themeDir, $sandbox.$themeDir);
}
$xmlFile = $sandbox.$themeDir.'/config.xml';
if (!$this->checkXmlFields($xmlFile)) {
$configFile = '/Config.xml';
$xml = Theme::loadConfigFromFile($sandbox.$themeDir.$configFile, true);
if (! $xml) {
$configFile = '/config.xml';
$xml = Theme::loadConfigFromFile($sandbox.$themeDir.$configFile, true);
}
if (! $xml) {
$this->errors[] = $this->l('Bad configuration file');
} else {
$importedTheme = $this->importThemeXmlConfig(simplexml_load_file($xmlFile));
$importedTheme = $this->importThemeXmlConfig($xml);
foreach ($importedTheme as $theme) {
if (Validate::isLoadedObject($theme)) {
if (!copy($sandbox.$themeDir.'/Config.xml', _PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
if (!copy($sandbox.$themeDir.$configFile, _PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
$this->errors[] = $this->l('Can\'t copy configuration file');
}
$targetDir = _PS_ALL_THEMES_DIR_.$theme->directory;
@@ -2143,13 +2149,7 @@ public function renderChooseThemeModule()
{
$theme = new Theme((int) Tools::getValue('id_theme'));
$xml = false;
if (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
$xml = @simplexml_load_file(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml');
} elseif (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/default.xml')) {
$xml = @simplexml_load_file(_PS_ROOT_DIR_.'/config/xml/themes/default.xml');
}
$xml = $theme->loadConfigFile();
if ($xml) {
$themeModule = $this->getModules($xml);
@@ -2269,13 +2269,7 @@ public function renderChooseThemeModule()
continue;
}
$oldXmlName = 'default.xml';
if (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/'.$shopTheme->directory.'.xml')) {
$oldXmlName = $shopTheme->directory.'.xml';
}
$shopXml = @simplexml_load_file(_PS_ROOT_DIR_.'/config/xml/themes/'.$oldXmlName);
$shopXml = $shopTheme->loadConfigFile();
if (!$shopXml) {
continue;
}
@@ -2428,13 +2422,7 @@ public function processThemeInstall()
}
}
$xml = false;
if (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml')) {
$xml = @simplexml_load_file(_PS_ROOT_DIR_.'/config/xml/themes/'.$theme->directory.'.xml');
} elseif (file_exists(_PS_ROOT_DIR_.'/config/xml/themes/default.xml')) {
$xml = @simplexml_load_file(_PS_ROOT_DIR_.'/config/xml/themes/default.xml');
}
$xml = $theme->loadConfigFile();
if ($xml) {
$moduleHook = [];
foreach ($xml->modules->hooks->hook as $row) {

0 comments on commit f3a171d

Please sign in to comment.
You can’t perform that action at this time.