From ceff8523b43a6a6c2dc897db267a67e27d377db1 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 25 Jul 2011 16:52:42 +0200 Subject: [PATCH] Merge pull request #30 from WCFSolutions/templateEngineSandbox --- .../lib/data/language/Language.class.php | 3 +- .../install/files/lib/system/WCF.class.php | 3 + .../system/template/TemplateEngine.class.php | 87 +++++++++++++++++-- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/wcfsetup/install/files/lib/data/language/Language.class.php b/wcfsetup/install/files/lib/data/language/Language.class.php index b3bf43295d..1ed6e2667d 100644 --- a/wcfsetup/install/files/lib/data/language/Language.class.php +++ b/wcfsetup/install/files/lib/data/language/Language.class.php @@ -112,8 +112,7 @@ class Language extends DatabaseObject { $staticItem = $this->get($item); if (isset($this->dynamicItems[$this->languageID][$item])) { - if (count($variables)) WCF::getTPL()->assign($variables); - return WCF::getTPL()->fetchString($this->dynamicItems[$this->languageID][$item]); + return WCF::getTPL()->fetchString($this->dynamicItems[$this->languageID][$item], $variables); } return $staticItem; diff --git a/wcfsetup/install/files/lib/system/WCF.class.php b/wcfsetup/install/files/lib/system/WCF.class.php index a777026818..6bec4163d9 100644 --- a/wcfsetup/install/files/lib/system/WCF.class.php +++ b/wcfsetup/install/files/lib/system/WCF.class.php @@ -297,9 +297,12 @@ class WCF { * Loads the default cache resources. */ protected function loadDefaultCacheResources() { + CacheHandler::getInstance()->addResource('languages', WCF_DIR.'cache/cache.languages.php', 'wcf\system\cache\CacheBuilderLanguage'); + CacheHandler::getInstance()->addResource('spiders', WCF_DIR.'cache/cache.spiders.php', 'wcf\system\cache\CacheBuilderSpider'); CacheHandler::getInstance()->addResource('languages', WCF_DIR.'cache/cache.languages.php', 'wcf\system\cache\builder\CacheBuilderLanguage'); CacheHandler::getInstance()->addResource('spiders', WCF_DIR.'cache/cache.spiders.php', 'wcf\system\cache\builder\CacheBuilderSpider'); if (defined('PACKAGE_ID')) { + CacheHandler::getInstance()->addResource('coreObjects-'.PACKAGE_ID, WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php', 'wcf\system\cache\CacheBuilderCoreObject'); CacheHandler::getInstance()->addResource('coreObjects-'.PACKAGE_ID, WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php', 'wcf\system\cache\builder\CacheBuilderCoreObject'); } } diff --git a/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php b/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php index f17a10cbd3..8b7f9080e7 100644 --- a/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php +++ b/wcfsetup/install/files/lib/system/template/TemplateEngine.class.php @@ -83,14 +83,21 @@ class TemplateEngine extends SingletonFactory { protected $templateGroupID = 0; /** - * Contains all available template variables and those assigned during runtime + * Contains all available template variables and those assigned during runtime. * * @var array */ protected $v = array(); /** - * Contains all templates with assigned template listeners + * Contains all cached vars for usage after execution in sandbox. + * + * @var array + */ + protected $sandboxVars = null; + + /** + * Contains all templates with assigned template listeners. * * @var array */ @@ -491,18 +498,61 @@ class TemplateEngine extends SingletonFactory { return $this->pluginNamespace.'TemplatePlugin'.StringUtil::firstCharToUpperCase(StringUtil::toLowerCase($type)).StringUtil::firstCharToUpperCase(StringUtil::toLowerCase($tag)); } + /** + * Enables execution in sandbox. + */ + public function enableSandbox() { + if ($this->sandboxVars === null) { + $this->sandboxVars = $this->v; + } + else { + throw new SystemException('TemplateEngine is already in sandbox mode. Disable the current sandbox mode before you enable a new one.'); + } + } + + /** + * Disables execution in sandbox. + */ + public function disableSandbox() { + if ($this->sandboxVars !== null) { + $this->v = $this->sandboxVars; + $this->sandboxVars = null; + } + else { + throw new SystemException('TemplateEngine is not in sandbox mode at the moment.'); + } + } + /** * Returns the output of a template. * * @param string $templateName + * @param array $variables + * @param boolean $sandbox enables execution in sandbox * @param integer $packageID * @return string output */ - public function fetch($templateName, $packageID = PACKAGE_ID) { + public function fetch($templateName, array $variables = array(), $sandbox = true, $packageID = PACKAGE_ID) { + // enable sandbox + if ($sandbox) { + $this->enableSandbox(); + } + + // add new template variables + if (count($variables)) { + $this->v = array_merge($this->v, $variables); + } + + // get output ob_start(); $this->display($templateName, $packageID, false); $output = ob_get_contents(); ob_end_clean(); + + // disable sandbox + if ($sandbox) { + $this->disableSandbox(); + } return $output; } @@ -511,13 +561,31 @@ class TemplateEngine extends SingletonFactory { * Executes a compiled template scripting source and returns the result. * * @param string $compiledSource + * @param array $variables + * @param boolean $sandbox enables execution in sandbox * @return string result */ - public function fetchString($compiledSource) { + public function fetchString($compiledSource, array $variables = array(), $sandbox = true) { + // enable sandbox + if ($sandbox) { + $this->enableSandbox(); + } + + // add new template variables + if (count($variables)) { + $this->v = array_merge($this->v, $variables); + } + + // get output ob_start(); eval('?>'.$compiledSource); $output = ob_get_contents(); ob_end_clean(); + + // disable sandbox + if ($sandbox) { + $this->disableSandbox(); + } return $output; } @@ -605,19 +673,22 @@ class TemplateEngine extends SingletonFactory { * @param boolean $sandbox enables execution in sandbox */ protected function includeTemplate($templateName, array $variables = array(), $sandbox = true, $packageID = PACKAGE_ID) { - // add new template variables + // enable sandbox if ($sandbox) { - $templateVars = $this->v; + $this->enableSandbox(); } - + + // add new template variables if (count($variables)) { $this->v = array_merge($this->v, $variables); } + // display template $this->display($templateName, $packageID, false); + // disable sandbox if ($sandbox) { - $this->v = $templateVars; + $this->disableSandbox(); } } -- 2.20.1