Пример #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;
 }
 /**
  * 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;
 }