From e4c38f73f5a46ade09cdfe481fe12ac323b99ea6 Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Wed, 26 Dec 2012 15:52:21 +0100 Subject: [PATCH] // Added a sandbox in order to check files before doing anything with them --- cache/sandbox/index.php | 35 +++++++++++++ .../admin/AdminTranslationsController.php | 51 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 cache/sandbox/index.php diff --git a/cache/sandbox/index.php b/cache/sandbox/index.php new file mode 100644 index 000000000..88e934d59 --- /dev/null +++ b/cache/sandbox/index.php @@ -0,0 +1,35 @@ + +* @copyright 2007-2012 PrestaShop SA +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); +header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); + +header("Cache-Control: no-store, no-cache, must-revalidate"); +header("Cache-Control: post-check=0, pre-check=0", false); +header("Pragma: no-cache"); + +header("Location: ../"); +exit; diff --git a/controllers/admin/AdminTranslationsController.php b/controllers/admin/AdminTranslationsController.php index 2c47f04d7..367d78607 100644 --- a/controllers/admin/AdminTranslationsController.php +++ b/controllers/admin/AdminTranslationsController.php @@ -593,6 +593,35 @@ class AdminTranslationsControllerCore extends AdminController } } } + + public static function checkTranslationFile($content) + { + $lines = array_map('trim', explode("\n", $content)); + $global = false; + foreach ($lines as $line) + { + if (in_array($line, array('', ''))) + continue; + if (!$global && preg_match('/^global\s+\$([a-z0-9-_]+)\s*;$/i', $line, $matches)) + { + $global = $matches[1]; + continue; + } + if ($global != false && preg_match('/^\$'.preg_quote($global, '/').'\s*=\s*array\(\s*\)\s*;$/i', $line)) + continue; + if (!$global && preg_match('/^\$([a-z0-9-_]+)\s*=\s*array\(\s*\)\s*;$/i', $line, $matches)) + { + $global = $matches[1]; + continue; + } + if (preg_match('/^\$'.preg_quote($global, '/').'\[\''._PS_TRANS_PATTERN_.'\'\]\s*=\s*\''._PS_TRANS_PATTERN_.'\'\s*;$/i', $line)) + continue; + if (preg_match('/^return\s+\$'.preg_quote($global, '/').'\s*;$/i', $line, $matches)) + continue; + return false; + } + return true; + } public function submitImportLang() { @@ -607,6 +636,28 @@ class AdminTranslationsControllerCore extends AdminController { $themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME)); $files_list = $gz->listContent(); + + $uniqid = uniqid(); + $sandbox = _PS_CACHE_DIR_.'sandbox'.DIRECTORY_SEPARATOR.$uniqid.DIRECTORY_SEPARATOR; + if ($gz->extract($sandbox, false)) + { + foreach ($files_list as $file2check) + { + if (preg_match('@^[0-9a-z-_/\\\\]+\.php$@i', $file2check['filename'])) + { + if (!AdminTranslationsController::checkTranslationFile(file_get_contents($sandbox.$file2check['filename']))) + $this->errors[] = sprintf(Tools::displayError('Validation failed for: %s'), $file2check['filename']); + } + elseif (!preg_match('@^[0-9a-z-_/\\\\]+\.(html|tpl|txt)$@i', $file2check['filename'])) + $this->errors[] = sprintf(Tools::displayError('Unidentified file found: %s'), $file2check['filename']); + } + + } + Tools::deleteDirectory($sandbox, true); + + if (count($this->errors)) + return false; + if ($gz->extract(_PS_TRANSLATIONS_DIR_.'../', false)) { AdminTranslationsController::checkAndAddMailsFiles($iso_code, $files_list);