Ejemplo n.º 1
0
 /**
  * returns the plugin type of a plugin and adds it to the used plugins array if required
  *
  * @param string $name plugin name, as found in the template
  *
  * @throws Exception
  *
  * @return int $pluginType as a multi bit flag composed of the \Dwoo\plugin types constants
  */
 protected function getPluginType($name)
 {
     $pluginType = -1;
     $tmpName = $name;
     if ($this->securityPolicy === null && (function_exists($name) || strtolower($name) === 'isset' || strtolower($name) === 'empty') || $this->securityPolicy !== null && array_key_exists(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) !== false) {
         $phpFunc = true;
     } else {
         if ($this->securityPolicy !== null && function_exists($name) && array_key_exists(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) === false) {
             throw new Exception\SecurityException('Call to a disallowed php function : ' . $name);
         }
     }
     if (isset($this->templatePlugins[$name])) {
         $pluginType = Core::TEMPLATE_PLUGIN | Core::COMPILABLE_PLUGIN;
     } else {
         if (isset($this->customPlugins[$name])) {
             $pluginType = $this->customPlugins[$name]['type'] | Core::CUSTOM_PLUGIN;
         } else {
             // It's a Block/Function class
             $classPrefixArray = array(Core::PLUGIN_BLOCK_CLASS_PREFIX_NAME, Core::PLUGIN_FUNC_CLASS_PREFIX_NAME);
             foreach ($classPrefixArray as $value) {
                 if (class_exists($value . Core::underscoreToCamel($name))) {
                     try {
                         $reflectionClass = new \ReflectionClass($value . Core::underscoreToCamel($name));
                         if ($reflectionClass->isSubclassOf('\\Dwoo\\Block\\Plugin')) {
                             $pluginType = Core::BLOCK_PLUGIN;
                         } else {
                             $pluginType = Core::CLASS_PLUGIN;
                         }
                         if ($reflectionClass->implementsInterface('\\Dwoo\\ICompilable') || $reflectionClass->implementsInterface('\\Dwoo\\ICompilable\\Block')) {
                             $pluginType |= Core::COMPILABLE_PLUGIN;
                         }
                     } catch (\ReflectionException $Exception) {
                     }
                 }
             }
             // It's a block/function/smarty function
             if ($pluginType === -1) {
                 $functionNameArray = array(Core::PLUGIN_BLOCK_FUNCTION_PREFIX_NAME . Core::underscoreToCamel($name) => Core::FUNC_PLUGIN, Core::PLUGIN_FUNC_FUNCTION_PREFIX_NAME . Core::underscoreToCamel($name) => Core::FUNC_PLUGIN, Core::PLUGIN_BLOCK_FUNCTION_PREFIX_NAME . Core::underscoreToCamel($name) . 'Compile' => Core::FUNC_PLUGIN | Core::COMPILABLE_PLUGIN, Core::PLUGIN_FUNC_FUNCTION_PREFIX_NAME . Core::underscoreToCamel($name) . 'Compile' => Core::FUNC_PLUGIN | Core::COMPILABLE_PLUGIN, 'smartyModifier' => Core::SMARTY_MODIFIER, 'smartyFunction' => Core::SMARTY_FUNCTION, 'smartyBlock' => Core::SMARTY_BLOCK);
                 foreach ($functionNameArray as $key => $value) {
                     Autoloader::loadFunction($key);
                     if (function_exists($key) !== false) {
                         $pluginType = $value;
                     }
                 }
             }
             // Otherwise, it's not a class/function, we try to load plugin
             if ($pluginType === -1) {
                 try {
                     $this->dwoo->getLoader()->loadPlugin($tmpName, isset($phpFunc) === false);
                 } catch (Exception $e) {
                     if (isset($phpFunc)) {
                         $pluginType = Core::NATIVE_PLUGIN;
                     } elseif (is_object($this->dwoo->getPluginProxy()) && $this->dwoo->getPluginProxy()->handles($tmpName)) {
                         $pluginType = Core::PROXY_PLUGIN;
                     } else {
                         throw new PluginException(sprintf(PluginException::NOT_FOUND, Core::underscoreToCamel($name)));
                     }
                 }
             }
         }
     }
     if (($pluginType & Core::COMPILABLE_PLUGIN) === 0 && ($pluginType & Core::NATIVE_PLUGIN) === 0 && ($pluginType & Core::PROXY_PLUGIN) === 0) {
         $this->addUsedPlugin($name, $pluginType);
     }
     return $pluginType;
 }
Ejemplo n.º 2
0
 public function fetch($filename, $cacheId = null, $compileId = null, $display = false)
 {
     $this->setCacheDir($this->cache_dir);
     $this->setCompileDir($this->compile_dir);
     if ($this->security) {
         $policy = new Policy();
         $policy->addPhpFunction(array_merge($this->security_settings['IF_FUNCS'], $this->security_settings['MODIFIER_FUNCS']));
         $phpTags = $this->security_settings['PHP_HANDLING'] ? SMARTY_PHP_ALLOW : $this->php_handling;
         if ($this->security_settings['PHP_TAGS']) {
             $phpTags = SMARTY_PHP_ALLOW;
         }
         switch ($phpTags) {
             case SMARTY_PHP_ALLOW:
             case SMARTY_PHP_PASSTHRU:
                 $phpTags = Policy::PHP_ALLOW;
                 break;
             case SMARTY_PHP_QUOTE:
                 $phpTags = Policy::PHP_ENCODE;
                 break;
             case SMARTY_PHP_REMOVE:
             default:
                 $phpTags = Policy::PHP_REMOVE;
                 break;
         }
         $policy->setPhpHandling($phpTags);
         $policy->setConstantHandling($this->security_settings['ALLOW_CONSTANTS']);
         if ($this->security_settings['INCLUDE_ANY']) {
             $policy->allowDirectory(preg_replace('{^((?:[a-z]:)?[\\\\/]).*}i', '$1', __FILE__));
         } else {
             $policy->allowDirectory($this->secure_dir);
         }
         $this->setSecurityPolicy($policy);
     }
     if (!empty($this->plugins_dir)) {
         foreach ($this->plugins_dir as $dir) {
             $this->getLoader()->addDirectory(rtrim($dir, '\\/'));
         }
     }
     $tpl = $this->makeTemplate($filename, $cacheId, $compileId);
     if ($this->force_compile) {
         $tpl->forceCompilation();
     }
     if ($this->caching > 0) {
         $this->cacheTime = $this->cache_lifetime;
     } else {
         $this->cacheTime = 0;
     }
     if ($this->compiler_class !== null) {
         if ($this->compiler_file !== null && !class_exists($this->compiler_class)) {
             include $this->compiler_file;
         }
         $this->compiler = new $this->compiler_class();
     } else {
         $this->compiler->addPreProcessor('smarty_compat', true);
         $this->compiler->setLooseOpeningHandling(true);
     }
     $this->compiler->setDelimiters($this->left_delimiter, $this->right_delimiter);
     return $this->get($tpl, $this->dataProvider, $this->compiler, $display === true);
 }
 /**
  * returns the plugin type of a plugin and adds it to the used plugins array if required
  * @param string $name plugin name, as found in the template
  * @throws Security\Exception
  * @throws Exception
  * @return int $pluginType as a multi bit flag composed of the \Dwoo\plugin types constants
  */
 protected function getPluginType($name)
 {
     $pluginType = -1;
     // Security Policy
     if ($this->securityPolicy === null && (function_exists($name) || strtolower($name) === 'isset' || strtolower($name) === 'empty') || $this->securityPolicy !== null && array_key_exists(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) !== false) {
         $phpFunc = true;
     } else {
         if ($this->securityPolicy !== null && function_exists($name) && array_key_exists(strtolower($name), $this->securityPolicy->getAllowedPhpFunctions()) === false) {
             throw new Security\Exception('Call to a disallowed php function : ' . $name);
         }
     }
     // Plugin type
     while ($pluginType <= 0) {
         if (isset($this->templatePlugins[$name])) {
             $pluginType = Core::TEMPLATE_PLUGIN | Core::COMPILABLE_PLUGIN;
         } else {
             if (isset($this->customPlugins[$name])) {
                 $pluginType = $this->customPlugins[$name]['type'] | Core::CUSTOM_PLUGIN;
             } else {
                 if (file_exists(Core::DWOO_DIRECTORY . '/Plugins/Blocks/Block' . ucfirst($name) . '.php')) {
                     try {
                         $reflectionClass = new \ReflectionClass(Core::PLUGIN_BLOCK_CLASS_PREFIX_NAME . ucfirst($name));
                         $pluginType = Core::BLOCK_PLUGIN;
                         if ($reflectionClass->implementsInterface('\\Dwoo\\ICompilable\\Block')) {
                             $pluginType |= Core::COMPILABLE_PLUGIN;
                         }
                     } catch (Exception $e) {
                     }
                 } else {
                     if (file_exists(Core::DWOO_DIRECTORY . '/Plugins/Functions/Function' . ucfirst($name) . '.php')) {
                         try {
                             $reflectionClass = new \ReflectionClass(Core::PLUGIN_FUNC_CLASS_PREFIX_NAME . ucfirst($name));
                             $pluginType = Core::CLASS_PLUGIN;
                             if ($reflectionClass->implementsInterface('\\Dwoo\\ICompilable')) {
                                 $pluginType |= Core::COMPILABLE_PLUGIN;
                             }
                         } catch (Exception $e) {
                         }
                     } else {
                         if (function_exists('smarty_modifier_' . $name) !== false) {
                             $pluginType = Core::SMARTY_MODIFIER;
                         } else {
                             if (function_exists('smarty_function_' . $name) !== false) {
                                 $pluginType = Core::SMARTY_FUNCTION;
                             } else {
                                 if (function_exists('smarty_block_' . $name) !== false) {
                                     $pluginType = Core::SMARTY_BLOCK;
                                 } else {
                                     if ($pluginType === -1) {
                                         try {
                                             $this->core->getLoader()->loadPlugin($name, isset($phpFunc) === false);
                                             $classPath = $this->core->getLoader()->getClassPath();
                                             if ($match = preg_grep('/^(Block|Function)?(' . $name . '+)/i', array_keys($classPath))) {
                                                 $index = array_values($match);
                                                 $className = function ($file) {
                                                     $namespace = null;
                                                     $classes = '';
                                                     $tokens = token_get_all(file_get_contents($file));
                                                     for ($i = 0; $i < count($tokens); $i++) {
                                                         switch ($tokens[$i][0]) {
                                                             case T_NAMESPACE:
                                                                 $i += 2;
                                                                 while ($tokens[$i][0] === T_STRING || $tokens[$i][0] === T_NS_SEPARATOR) {
                                                                     $namespace .= $tokens[$i++][1];
                                                                 }
                                                                 break;
                                                             case T_INTERFACE:
                                                             case T_CLASS:
                                                             case T_TRAIT:
                                                                 $i += 2;
                                                                 if ($namespace) {
                                                                     $classes = '\\' . $namespace . '\\' . $tokens[$i][1];
                                                                 } else {
                                                                     $classes = '\\' . $tokens[$i][1];
                                                                 }
                                                                 break;
                                                         }
                                                     }
                                                     return $classes;
                                                 };
                                                 $callback = $className($classPath[$index[0]]);
                                                 $rc = new \ReflectionClass($callback);
                                                 if (strpos($index[0], 'Block') !== false) {
                                                     $pluginType = Core::BLOCK_PLUGIN;
                                                     if ($rc->implementsInterface('\\Dwoo\\ICompilable\\Block')) {
                                                         $pluginType |= Core::COMPILABLE_PLUGIN;
                                                     }
                                                 } else {
                                                     if (strpos($index[0], 'Function') !== false) {
                                                         $pluginType = Core::CLASS_PLUGIN;
                                                         if ($rc->implementsInterface('\\Dwoo\\ICompilable')) {
                                                             $pluginType |= Core::COMPILABLE_PLUGIN;
                                                         }
                                                     }
                                                 }
                                             }
                                         } catch (Exception $e) {
                                             if (isset($phpFunc)) {
                                                 $pluginType = Core::NATIVE_PLUGIN;
                                             } else {
                                                 if (is_object($this->core->getPluginProxy()) && $this->core->getPluginProxy()->handles($name)) {
                                                     $pluginType = Core::PROXY_PLUGIN;
                                                     break;
                                                 } else {
                                                     throw $e;
                                                 }
                                             }
                                         }
                                     } else {
                                         throw new Exception('Plugin "' . $name . '" could not be found');
                                     }
                                     $pluginType++;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (($pluginType & Core::COMPILABLE_PLUGIN) === 0 && ($pluginType & Core::NATIVE_PLUGIN) === 0 && ($pluginType & Core::PROXY_PLUGIN) === 0) {
         $this->addUsedPlugin($name, $pluginType);
     }
     return $pluginType;
 }