/** * Initialize compiler */ public function __construct($smarty) { $this->smarty = $smarty; // get required plugins Smarty_Internal_Plugin_Loader::loadPlugin('Smarty_Internal_Configfilelexer', $this->smarty->plugins_dir); Smarty_Internal_Plugin_Loader::loadPlugin('Smarty_Internal_Configfileparser', $this->smarty->plugins_dir); $this->config_data['sections'] = array(); $this->config_data['vars'] = array(); }
/** * load a filter of specified type and name * * @param string $type filter type * @param string $name filter name * @return bool */ function loadFilter($type, $name) { $_plugin = "smarty_{$type}filter_{$name}"; $_filter_name = $_plugin; if (Smarty_Internal_Plugin_Loader::loadPlugin($_plugin, $this->smarty->plugins_dir)) { if (class_exists($_plugin, false)) { $_plugin = array($_plugin, 'execute'); } if (is_callable($_plugin)) { return $this->smarty->registered_filters[$type][$_filter_name] = $_plugin; } } throw new SmartyException("{$type}filter \"{$name}\" not callable"); return false; }
/** * Compiles code for modifier execution * * @param array $args array with attributes from parser * @param object $compiler compiler object * @param array $parameter array with compilation parameter * @return string compiled code */ public function compile($args, $compiler, $parameter) { $this->compiler = $compiler; $this->smarty = $this->compiler->smarty; // check and get attributes $_attr = $this->_get_attributes($args); $output = $parameter['value']; // loop over list of modifiers foreach ($parameter['modifierlist'] as $single_modifier) { $modifier = $single_modifier[0]; $single_modifier[0] = $output; $params = implode(',', $single_modifier); // check for registered modifier if (isset($compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier])) { $function = $compiler->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$modifier][0]; if (!is_array($function)) { $output = "{$function}({$params})"; } else { if (is_object($function[0])) { $output = '$_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][\'' . $modifier . '\'][0][0]->' . $function[1] . '(' . $params . ')'; } else { $output = $function[0] . '::' . $function[1] . '(' . $params . ')'; } } // check for plugin modifiercompiler } else if (Smarty_Internal_Plugin_Loader::loadPlugin('smarty_modifiercompiler_' . $modifier, $compiler->smarty->plugins_dir)) { $plugin = 'smarty_modifiercompiler_' . $modifier; $output = $plugin($single_modifier, $compiler); // check for plugin modifier } else if ($function = $this->compiler->getPlugin($modifier, Smarty::PLUGIN_MODIFIER)) { $output = "{$function}({$params})"; // check if trusted PHP function } else if (is_callable($modifier)) { // check if modifier allowed if (!is_object($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedModifier($modifier, $this->compiler)) { $output = "{$modifier}({$params})"; } } else { $this->compiler->trigger_template_error ("unknown modifier \"" . $modifier . "\"", $this->compiler->lex->taglineno); } } return $output; }
/** * Run filters over content * * The filters will be lazy loaded if required * class name format: Smarty_FilterType_FilterName * plugin filename format: filtertype.filtername.php * Smarty2 filter plugins could be used * * @param string $type the type of filter ('pre','post','output' or 'variable') which shall run * @param string $content the content which shall be processed by the filters * @return string the filtered content */ static function runFilter($type, $content, $smarty, $template, $flag = null) { $output = $content; if ($type != 'variable' || ($smarty->variable_filter && $flag !== false) || $flag === true) { // loop over autoload filters of specified type if (!empty($smarty->autoload_filters[$type])) { foreach ((array)$smarty->autoload_filters[$type] as $name) { $plugin_name = "Smarty_{$type}filter_{$name}"; if (Smarty_Internal_Plugin_Loader::loadPlugin($plugin_name, $smarty->plugins_dir)) { if (function_exists($plugin_name)) { // use loaded Smarty2 style plugin $output = $plugin_name($output, $smarty); } elseif (class_exists($plugin_name, false)) { // loaded class of filter plugin $output = call_user_func(array($plugin_name, 'execute'), $output, $smarty, $template); } } else { // nothing found, throw exception throw new SmartyException("Unable to load filter {$plugin_name}"); } } } // loop over registerd filters of specified type if (!empty($smarty->registered_filters[$type])) { foreach ($smarty->registered_filters[$type] as $key => $name) { if (is_array($smarty->registered_filters[$type][$key])) { $output = call_user_func($smarty->registered_filters[$type][$key], $output, $smarty, $template); } else { $output = $smarty->registered_filters[$type][$key]($output, $smarty, $template); } } } } // return filtered output return $output; }
/** * lazy loads internal compile plugin for tag and calls the compile methode * * compile objects cached for reuse. * class name format: Smarty_Internal_Compile_TagName * plugin filename format: Smarty_Internal_Tagname.php * * @param $tag string tag name * @param $args array with tag attributes * @param $param1 optional parameter * @param $param2 optional parameter * @param $param3 optional parameter * @return string compiled code */ public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) { // re-use object if already exists if (isset(self::$_tag_objects[$tag])) { // compile this tag return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); } // lazy load internal compiler plugin $class_name = 'Smarty_Internal_Compile_' . $tag; if (Smarty_Internal_Plugin_Loader::loadPlugin($class_name, $this->smarty->plugins_dir)) { // use plugin if found self::$_tag_objects[$tag] = new $class_name; // compile this tag return self::$_tag_objects[$tag]->compile($args, $this, $param1, $param2, $param3); } // no internal compile plugin for this tag return false; }
/** * Load template resource handler by type * * @param string $resource_type template resource type * @return object resource handler object */ protected function loadTemplateResourceHandler ($resource_type) { // try registered resource if (isset($this->smarty->registered_resources[$resource_type])) { return new Smarty_Internal_Resource_Registered($this); } else { // try sysplugins dir if (in_array($resource_type, array('file', 'string', 'extends', 'php', 'stream', 'eval'))) { $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($resource_type); return new $_resource_class($this->smarty); } else { // try plugins dir $_resource_class = 'Smarty_Resource_' . ucfirst($resource_type); if (Smarty_Internal_Plugin_Loader::loadPlugin($_resource_class,$this->smarty->plugins_dir)) { if (class_exists($_resource_class, false)) { return new $_resource_class($this->smarty); } else { return new Smarty_Internal_Resource_Registered($this, $resource_type); } } else { // try streams $_known_stream = stream_get_wrappers(); if (in_array($resource_type, $_known_stream)) { // is known stream if (is_object($this->smarty->security_policy)) { $this->smarty->security_policy->isTrustedStream($resource_type); } return new Smarty_Internal_Resource_Stream($this->smarty); } else { throw new SmartyException('Unkown resource type \'' . $resource_type . '\''); } } } } }
/** * Loads cache resource. * * @param string $type cache resource type * @return object of cache resource */ public function loadCacheResource($type = null) { if (!isset($type)) { $type = $this->caching_type; } // already loaded? if (isset($this->cache_resource_objects[$type])) { return $this->cache_resource_objects[$type]; } if (in_array($type, $this->cache_resource_types)) { $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); return $this->cache_resource_objects[$type] = new $cache_resource_class($this); } else { // try plugins dir $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); if (Smarty_Internal_Plugin_Loader::loadPlugin($cache_resource_class, $this->plugins_dir)) { return $this->cache_resource_objects[$type] = new $cache_resource_class($this); } else { throw new SmartyException("Unable to load cache resource '{$type}'"); } } }