Merge pull request #30 from WCFSolutions/templateEngineSandbox
authorAlexander Ebert <ebert@woltlab.com>
Mon, 25 Jul 2011 14:52:42 +0000 (16:52 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 25 Jul 2011 14:52:42 +0000 (16:52 +0200)
wcfsetup/install/files/lib/data/language/Language.class.php
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/template/TemplateEngine.class.php

index b3bf43295da81f6ecc82e094b87edb49ab956c56..1ed6e2667d4930670d6ca5bf36d86b3f2560331c 100644 (file)
@@ -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;
index a77702681898e61f4b033040914c57c4bec2ac97..6bec4163d956aa0adcbb89f9d602772decc70bab 100644 (file)
@@ -297,9 +297,12 @@ class WCF {
         * Loads the default cache resources.\r
         */\r
        protected function loadDefaultCacheResources() {\r
+               CacheHandler::getInstance()->addResource('languages', WCF_DIR.'cache/cache.languages.php', 'wcf\system\cache\CacheBuilderLanguage');\r
+               CacheHandler::getInstance()->addResource('spiders', WCF_DIR.'cache/cache.spiders.php', 'wcf\system\cache\CacheBuilderSpider');\r
                CacheHandler::getInstance()->addResource('languages', WCF_DIR.'cache/cache.languages.php', 'wcf\system\cache\builder\CacheBuilderLanguage');\r
                CacheHandler::getInstance()->addResource('spiders', WCF_DIR.'cache/cache.spiders.php', 'wcf\system\cache\builder\CacheBuilderSpider');\r
                if (defined('PACKAGE_ID')) {\r
+                       CacheHandler::getInstance()->addResource('coreObjects-'.PACKAGE_ID, WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php', 'wcf\system\cache\CacheBuilderCoreObject');\r
                        CacheHandler::getInstance()->addResource('coreObjects-'.PACKAGE_ID, WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php', 'wcf\system\cache\builder\CacheBuilderCoreObject');\r
                }\r
        }\r
index f17a10cbd3b0cacd9ad21d3c54bdfa5d6e843030..8b7f9080e7c71028c876c895079359548382aeba 100644 (file)
@@ -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<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<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();
                }
        }