diff --git a/libs/sysplugins/smarty_internal_method_unregistercacheresource.php b/libs/sysplugins/smarty_internal_method_unregistercacheresource.php new file mode 100644 index 0000000..1a157ff --- /dev/null +++ b/libs/sysplugins/smarty_internal_method_unregistercacheresource.php @@ -0,0 +1,40 @@ +_getSmartyObj(); + if (isset($smarty->registered_cache_resources[ $name ])) { + unset($smarty->registered_cache_resources[ $name ]); + } + return $obj; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_method_unregisterfilter.php b/libs/sysplugins/smarty_internal_method_unregisterfilter.php new file mode 100644 index 0000000..c5b88f5 --- /dev/null +++ b/libs/sysplugins/smarty_internal_method_unregisterfilter.php @@ -0,0 +1,42 @@ +_getSmartyObj(); + $this->_checkFilterType($type); + if (isset($smarty->registered_filters[ $type ])) { + $name = is_string($callback) ? $callback : $this->_getFilterName($callback); + if (isset($smarty->registered_filters[ $type ][ $name ])) { + unset($smarty->registered_filters[ $type ][ $name ]); + if (empty($smarty->registered_filters[ $type ])) { + unset($smarty->registered_filters[ $type ]); + } + } + } + return $obj; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_method_unregisterobject.php b/libs/sysplugins/smarty_internal_method_unregisterobject.php new file mode 100644 index 0000000..76d4866 --- /dev/null +++ b/libs/sysplugins/smarty_internal_method_unregisterobject.php @@ -0,0 +1,40 @@ +_getSmartyObj(); + if (isset($smarty->registered_objects[ $object_name ])) { + unset($smarty->registered_objects[ $object_name ]); + } + return $obj; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_method_unregisterplugin.php b/libs/sysplugins/smarty_internal_method_unregisterplugin.php new file mode 100644 index 0000000..b5f0d50 --- /dev/null +++ b/libs/sysplugins/smarty_internal_method_unregisterplugin.php @@ -0,0 +1,41 @@ +_getSmartyObj(); + if (isset($smarty->registered_plugins[ $type ][ $name ])) { + unset($smarty->registered_plugins[ $type ][ $name ]); + } + return $obj; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_method_unregisterresource.php b/libs/sysplugins/smarty_internal_method_unregisterresource.php new file mode 100644 index 0000000..ed4801d --- /dev/null +++ b/libs/sysplugins/smarty_internal_method_unregisterresource.php @@ -0,0 +1,40 @@ +_getSmartyObj(); + if (isset($smarty->registered_resources[ $type ])) { + unset($smarty->registered_resources[ $type ]); + } + return $obj; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_nocache_insert.php b/libs/sysplugins/smarty_internal_nocache_insert.php new file mode 100644 index 0000000..65fc143 --- /dev/null +++ b/libs/sysplugins/smarty_internal_nocache_insert.php @@ -0,0 +1,53 @@ +assign('{$_assign}' , {$_function} (" . var_export($_attr, true) . + ",\$_smarty_tpl), true);?>"; + } else { + $_output .= "echo {$_function}(" . var_export($_attr, true) . ",\$_smarty_tpl);?>"; + } + $_tpl = $_template; + while ($_tpl->_isSubTpl()) { + $_tpl = $_tpl->parent; + } + + return "/*%%SmartyNocache:{$_tpl->compiled->nocache_hash}%%*/" . $_output . + "/*/%%SmartyNocache:{$_tpl->compiled->nocache_hash}%%*/"; + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree.php b/libs/sysplugins/smarty_internal_parsetree.php new file mode 100644 index 0000000..32c9df0 --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree.php @@ -0,0 +1,54 @@ +data = null; + $this->subtrees = null; + } + +} + + diff --git a/libs/sysplugins/smarty_internal_parsetree_code.php b/libs/sysplugins/smarty_internal_parsetree_code.php new file mode 100644 index 0000000..d0f0b3f --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_code.php @@ -0,0 +1,42 @@ +data = $data; + } + + /** + * Return buffer content in parentheses + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string content + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + return sprintf("(%s)", $this->data); + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree_dq.php b/libs/sysplugins/smarty_internal_parsetree_dq.php new file mode 100644 index 0000000..8c2e0d9 --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_dq.php @@ -0,0 +1,95 @@ +subtrees[] = $subtree; + if ($subtree instanceof Smarty_Internal_ParseTree_Tag) { + $parser->block_nesting_level = count($parser->compiler->_tag_stack); + } + } + + /** + * Append buffer to subtree + * + * @param \Smarty_Internal_Templateparser $parser + * @param Smarty_Internal_ParseTree $subtree parse tree buffer + */ + public function append_subtree(Smarty_Internal_Templateparser $parser, Smarty_Internal_ParseTree $subtree) + { + $last_subtree = count($this->subtrees) - 1; + if ($last_subtree >= 0 && $this->subtrees[ $last_subtree ] instanceof Smarty_Internal_ParseTree_Tag && + $this->subtrees[ $last_subtree ]->saved_block_nesting < $parser->block_nesting_level + ) { + if ($subtree instanceof Smarty_Internal_ParseTree_Code) { + $this->subtrees[ $last_subtree ]->data = + $parser->compiler->appendCode($this->subtrees[ $last_subtree ]->data, + 'data . ';?>'); + } elseif ($subtree instanceof Smarty_Internal_ParseTree_DqContent) { + $this->subtrees[ $last_subtree ]->data = + $parser->compiler->appendCode($this->subtrees[ $last_subtree ]->data, + 'data . '";?>'); + } else { + $this->subtrees[ $last_subtree ]->data = + $parser->compiler->appendCode($this->subtrees[ $last_subtree ]->data, $subtree->data); + } + } else { + $this->subtrees[] = $subtree; + } + if ($subtree instanceof Smarty_Internal_ParseTree_Tag) { + $parser->block_nesting_level = count($parser->compiler->_tag_stack); + } + } + + /** + * Merge subtree buffer content together + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string compiled template code + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + $code = ''; + foreach ($this->subtrees as $subtree) { + if ($code !== "") { + $code .= "."; + } + if ($subtree instanceof Smarty_Internal_ParseTree_Tag) { + $more_php = $subtree->assign_to_var($parser); + } else { + $more_php = $subtree->to_smarty_php($parser); + } + + $code .= $more_php; + + if (!$subtree instanceof Smarty_Internal_ParseTree_DqContent) { + $parser->compiler->has_variable_string = true; + } + } + + return $code; + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree_dqcontent.php b/libs/sysplugins/smarty_internal_parsetree_dqcontent.php new file mode 100644 index 0000000..a8ca389 --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_dqcontent.php @@ -0,0 +1,42 @@ +data = $data; + } + + /** + * Return content as double quoted string + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string doubled quoted string + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + return '"' . $this->data . '"'; + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree_tag.php b/libs/sysplugins/smarty_internal_parsetree_tag.php new file mode 100644 index 0000000..9058aab --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_tag.php @@ -0,0 +1,69 @@ +data = $data; + $this->saved_block_nesting = $parser->block_nesting_level; + } + + /** + * Return buffer content + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string content + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + return $this->data; + } + + /** + * Return complied code that loads the evaluated output of buffer content into a temporary variable + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string template code + */ + public function assign_to_var(Smarty_Internal_Templateparser $parser) + { + $var = $parser->compiler->getNewPrefixVariable(); + $tmp = $parser->compiler->appendCode('', $this->data); + $tmp = $parser->compiler->appendCode($tmp, ""); + $parser->compiler->prefix_code[] = sprintf("%s", $tmp); + + return $var; + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree_template.php b/libs/sysplugins/smarty_internal_parsetree_template.php new file mode 100644 index 0000000..f603235 --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_template.php @@ -0,0 +1,128 @@ +subtrees)) { + $this->subtrees = array_merge($this->subtrees, $subtree->subtrees); + } else { + if ($subtree->data !== '') { + $this->subtrees[] = $subtree; + } + } + } + + /** + * Append array to subtree + * + * @param \Smarty_Internal_Templateparser $parser + * @param \Smarty_Internal_ParseTree[] $array + */ + public function append_array(Smarty_Internal_Templateparser $parser, $array = array()) + { + if (!empty($array)) { + $this->subtrees = array_merge($this->subtrees, (array) $array); + } + } + + /** + * Prepend array to subtree + * + * @param \Smarty_Internal_Templateparser $parser + * @param \Smarty_Internal_ParseTree[] $array + */ + public function prepend_array(Smarty_Internal_Templateparser $parser, $array = array()) + { + if (!empty($array)) { + $this->subtrees = array_merge((array) $array, $this->subtrees); + } + } + + /** + * Sanitize and merge subtree buffers together + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string template code content + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + $code = ''; + for ($key = 0, $cnt = count($this->subtrees); $key < $cnt; $key ++) { + if ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Text) { + $subtree = $this->subtrees[ $key ]->to_smarty_php($parser); + while ($key + 1 < $cnt && ($this->subtrees[ $key + 1 ] instanceof Smarty_Internal_ParseTree_Text || + $this->subtrees[ $key + 1 ]->data == '')) { + $key ++; + if ($this->subtrees[ $key ]->data == '') { + continue; + } + $subtree .= $this->subtrees[ $key ]->to_smarty_php($parser); + } + if ($subtree == '') { + continue; + } + $code .= preg_replace('/((<%)|(%>)|(<\?php)|(<\?)|(\?>)|(<\/?script))/', "\n", + $subtree); + continue; + } + if ($this->subtrees[ $key ] instanceof Smarty_Internal_ParseTree_Tag) { + $subtree = $this->subtrees[ $key ]->to_smarty_php($parser); + while ($key + 1 < $cnt && ($this->subtrees[ $key + 1 ] instanceof Smarty_Internal_ParseTree_Tag || + $this->subtrees[ $key + 1 ]->data == '')) { + $key ++; + if ($this->subtrees[ $key ]->data == '') { + continue; + } + $subtree = $parser->compiler->appendCode($subtree, $this->subtrees[ $key ]->to_smarty_php($parser)); + } + if ($subtree == '') { + continue; + } + $code .= $subtree; + continue; + } + $code .= $this->subtrees[ $key ]->to_smarty_php($parser); + } + return $code; + } +} diff --git a/libs/sysplugins/smarty_internal_parsetree_text.php b/libs/sysplugins/smarty_internal_parsetree_text.php new file mode 100644 index 0000000..b3100fa --- /dev/null +++ b/libs/sysplugins/smarty_internal_parsetree_text.php @@ -0,0 +1,40 @@ +data = $data; + } + + /** + * Return buffer content + * + * @param \Smarty_Internal_Templateparser $parser + * + * @return string text + */ + public function to_smarty_php(Smarty_Internal_Templateparser $parser) + { + return $this->data; + } +} diff --git a/libs/sysplugins/smarty_internal_resource_eval.php b/libs/sysplugins/smarty_internal_resource_eval.php new file mode 100644 index 0000000..d3088bd --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_eval.php @@ -0,0 +1,95 @@ +uid = $source->filepath = sha1($source->name); + $source->timestamp = $source->exists = true; + } + + /** + * Load template's source from $resource_name into current template object + * + * @uses decode() to decode base64 and urlencoded template_resources + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + */ + public function getContent(Smarty_Template_Source $source) + { + return $this->decode($source->name); + } + + /** + * decode base64 and urlencode + * + * @param string $string template_resource to decode + * + * @return string decoded template_resource + */ + protected function decode($string) + { + // decode if specified + if (($pos = strpos($string, ':')) !== false) { + if (!strncmp($string, 'base64', 6)) { + return base64_decode(substr($string, 7)); + } elseif (!strncmp($string, 'urlencode', 9)) { + return urldecode(substr($string, 10)); + } + } + + return $string; + } + + /** + * modify resource_name according to resource handlers specifications + * + * @param Smarty $smarty Smarty instance + * @param string $resource_name resource_name to make unique + * @param boolean $isConfig flag for config resource + * + * @return string unique resource name + */ + public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false) + { + return get_class($this) . '#' . $this->decode($resource_name); + } + + /** + * Determine basename for compiled filename + * + * @param Smarty_Template_Source $source source object + * + * @return string resource's basename + */ + public function getBasename(Smarty_Template_Source $source) + { + return ''; + } +} diff --git a/libs/sysplugins/smarty_internal_resource_extends.php b/libs/sysplugins/smarty_internal_resource_extends.php new file mode 100644 index 0000000..4f1f09e --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_extends.php @@ -0,0 +1,125 @@ +name); + $smarty = &$source->smarty; + $exists = true; + foreach ($components as $component) { + /* @var \Smarty_Template_Source $_s */ + $_s = Smarty_Template_Source::load(null, $smarty, $component); + if ($_s->type == 'php') { + throw new SmartyException("Resource type {$_s->type} cannot be used with the extends resource type"); + } + $sources[ $_s->uid ] = $_s; + $uid .= $_s->filepath; + if ($_template) { + $exists = $exists && $_s->exists; + } + } + $source->components = $sources; + $source->filepath = $_s->filepath; + $source->uid = sha1($uid . $source->smarty->_joined_template_dir); + $source->exists = $exists; + if ($_template) { + $source->timestamp = $_s->timestamp; + } + } + + /** + * populate Source Object with timestamp and exists from Resource + * + * @param Smarty_Template_Source $source source object + */ + public function populateTimestamp(Smarty_Template_Source $source) + { + $source->exists = true; + /* @var \Smarty_Template_Source $_s */ + foreach ($source->components as $_s) { + $source->exists = $source->exists && $_s->exists; + } + $source->timestamp = $source->exists ? $_s->getTimeStamp() : false; + } + + /** + * Load template's source from files into current template object + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + * @throws SmartyException if source cannot be loaded + */ + public function getContent(Smarty_Template_Source $source) + { + if (!$source->exists) { + throw new SmartyException("Unable to load template '{$source->type}:{$source->name}'"); + } + + $_components = array_reverse($source->components); + + $_content = ''; + /* @var \Smarty_Template_Source $_s */ + foreach ($_components as $_s) { + // read content + $_content .= $_s->getContent(); + } + return $_content; + } + + /** + * Determine basename for compiled filename + * + * @param Smarty_Template_Source $source source object + * + * @return string resource's basename + */ + public function getBasename(Smarty_Template_Source $source) + { + return str_replace(':', '.', basename($source->filepath)); + } + + /* + * Disable timestamp checks for extends resource. + * The individual source components will be checked. + * + * @return bool + */ + public function checkTimestamps() + { + return false; + } +} diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php new file mode 100644 index 0000000..a58771a --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -0,0 +1,176 @@ +name; + // absolute file ? + if ($file[ 0 ] == '/' || $file[ 1 ] == ':') { + $file = $source->smarty->_realpath($file, true); + return is_file($file) ? $file : false; + } + // go relative to a given template? + if ($file[ 0 ] == '.' && $_template && $_template->_isSubTpl() && + preg_match('#^[.]{1,2}[\\\/]#', $file) + ) { + if ($_template->parent->source->type != 'file' && $_template->parent->source->type != 'extends' && + !isset($_template->parent->_cache[ 'allow_relative_path' ]) + ) { + throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); + } + // normalize path + $path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . $source->smarty->ds . $file); + // files relative to a template only get one shot + return is_file($path) ? $path : false; + } + // normalize $source->smarty->ds + if (strpos($file, $source->smarty->ds == '/' ? '\\' : '/') !== false) { + $file = str_replace($source->smarty->ds == '/' ? '\\' : '/', $source->smarty->ds, $file); + } + + $_directories = $source->smarty->getTemplateDir(null, $source->isConfig); + // template_dir index? + if ($file[ 0 ] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) { + $file = $fileMatch[ 2 ]; + $_indices = explode(',', $fileMatch[ 1 ]); + $_index_dirs = array(); + foreach ($_indices as $index) { + $index = trim($index); + // try string indexes + if (isset($_directories[ $index ])) { + $_index_dirs[] = $_directories[ $index ]; + } elseif (is_numeric($index)) { + // try numeric index + $index = (int) $index; + if (isset($_directories[ $index ])) { + $_index_dirs[] = $_directories[ $index ]; + } else { + // try at location index + $keys = array_keys($_directories); + if (isset($_directories[ $keys[ $index ] ])) { + $_index_dirs[] = $_directories[ $keys[ $index ] ]; + } + } + } + } + if (empty($_index_dirs)) { + // index not found + return false; + } else { + $_directories = $_index_dirs; + } + } + + // relative file name? + foreach ($_directories as $_directory) { + $path = $_directory . $file; + if (is_file($path)) { + return (strpos($path, '.' . $source->smarty->ds) !== false) ? $source->smarty->_realpath($path) : $path; + } + } + if (!isset($_index_dirs)) { + // Could be relative to cwd + $path = $source->smarty->_realpath($file, true); + if (is_file($path)) { + return $path; + } + } + // Use include path ? + if ($source->smarty->use_include_path) { + return $source->smarty->ext->_getIncludePath->getIncludePath($_directories, $file, $source->smarty); + } + return false; + } + + /** + * populate Source Object with meta data from Resource + * + * @param Smarty_Template_Source $source source object + * @param Smarty_Internal_Template $_template template object + */ + public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) + { + $source->filepath = $this->buildFilepath($source, $_template); + + if ($source->filepath !== false) { + if (isset($source->smarty->security_policy) && is_object($source->smarty->security_policy)) { + $source->smarty->security_policy->isTrustedResourceDir($source->filepath, $source->isConfig); + } + $source->exists = true; + $source->uid = sha1($source->filepath . ($source->isConfig ? $source->smarty->_joined_config_dir : + $source->smarty->_joined_template_dir)); + $source->timestamp = filemtime($source->filepath); + } else { + $source->timestamp = $source->exists = false; + } + } + + /** + * populate Source Object with timestamp and exists from Resource + * + * @param Smarty_Template_Source $source source object + */ + public function populateTimestamp(Smarty_Template_Source $source) + { + if (!$source->exists) { + $source->timestamp = $source->exists = is_file($source->filepath); + } + if ($source->exists) { + $source->timestamp = filemtime($source->filepath); + } + } + + /** + * Load template's source from file into current template object + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + * @throws SmartyException if source cannot be loaded + */ + public function getContent(Smarty_Template_Source $source) + { + if ($source->exists) { + return file_get_contents($source->filepath); + } + throw new SmartyException('Unable to read ' . ($source->isConfig ? 'config' : 'template') . + " {$source->type} '{$source->name}'"); + } + + /** + * Determine basename for compiled filename + * + * @param Smarty_Template_Source $source source object + * + * @return string resource's basename + */ + public function getBasename(Smarty_Template_Source $source) + { + return basename($source->filepath); + } +} diff --git a/libs/sysplugins/smarty_internal_resource_php.php b/libs/sysplugins/smarty_internal_resource_php.php new file mode 100644 index 0000000..6e9351d --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_php.php @@ -0,0 +1,111 @@ +short_open_tag = function_exists('ini_get') ? ini_get('short_open_tag') : 1; + } + + /** + * Load template's source from file into current template object + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + * @throws SmartyException if source cannot be loaded + */ + public function getContent(Smarty_Template_Source $source) + { + if ($source->exists) { + return ''; + } + throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); + } + + /** + * populate compiled object with compiled filepath + * + * @param Smarty_Template_Compiled $compiled compiled object + * @param Smarty_Internal_Template $_template template object (is ignored) + */ + public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template) + { + $compiled->filepath = $_template->source->filepath; + $compiled->timestamp = $_template->source->timestamp; + $compiled->exists = $_template->source->exists; + $compiled->file_dependency[ $_template->source->uid ] = + array($compiled->filepath, + $compiled->timestamp, + $_template->source->type,); + } + + /** + * Render and output the template (without using the compiler) + * + * @param Smarty_Template_Source $source source object + * @param Smarty_Internal_Template $_template template object + * + * @return void + * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled + */ + public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template) + { + if (!$source->smarty->allow_php_templates) { + throw new SmartyException("PHP templates are disabled"); + } + if (!$source->exists) { + throw new SmartyException("Unable to load template {$source->type} '{$source->name}'" . + ($_template->_isSubTpl() ? " in '{$_template->parent->template_resource}'" : '')); + } + + // prepare variables + extract($_template->getTemplateVars()); + + // include PHP template with short open tags enabled + if (function_exists('ini_set')) { + ini_set('short_open_tag', '1'); + } + /** @var Smarty_Internal_Template $_smarty_template + * used in included file + */ + $_smarty_template = $_template; + include($source->filepath); + if (function_exists('ini_set')) { + ini_set('short_open_tag', $this->short_open_tag); + } + } +} diff --git a/libs/sysplugins/smarty_internal_resource_registered.php b/libs/sysplugins/smarty_internal_resource_registered.php new file mode 100644 index 0000000..8535257 --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_registered.php @@ -0,0 +1,99 @@ +filepath = $source->type . ':' . $source->name; + $source->uid = sha1($source->filepath . $source->smarty->_joined_template_dir); + $source->timestamp = $this->getTemplateTimestamp($source); + $source->exists = !!$source->timestamp; + } + + /** + * populate Source Object with timestamp and exists from Resource + * + * @param Smarty_Template_Source $source source object + * + * @return void + */ + public function populateTimestamp(Smarty_Template_Source $source) + { + $source->timestamp = $this->getTemplateTimestamp($source); + $source->exists = !!$source->timestamp; + } + + /** + * Get timestamp (epoch) the template source was modified + * + * @param Smarty_Template_Source $source source object + * + * @return integer|boolean timestamp (epoch) the template was modified, false if resources has no timestamp + */ + public function getTemplateTimestamp(Smarty_Template_Source $source) + { + // return timestamp + $time_stamp = false; + call_user_func_array($source->smarty->registered_resources[ $source->type ][ 0 ][ 1 ], + array($source->name, &$time_stamp, $source->smarty)); + + return is_numeric($time_stamp) ? (int) $time_stamp : $time_stamp; + } + + /** + * Load template's source by invoking the registered callback into current template object + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + * @throws SmartyException if source cannot be loaded + */ + public function getContent(Smarty_Template_Source $source) + { + // return template string + $content = null; + $t = call_user_func_array($source->smarty->registered_resources[ $source->type ][ 0 ][ 0 ], + array($source->name, &$content, $source->smarty)); + if (is_bool($t) && !$t) { + throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); + } + + return $content; + } + + /** + * Determine basename for compiled filename + * + * @param Smarty_Template_Source $source source object + * + * @return string resource's basename + */ + public function getBasename(Smarty_Template_Source $source) + { + return basename($source->name); + } +} diff --git a/libs/sysplugins/smarty_internal_resource_stream.php b/libs/sysplugins/smarty_internal_resource_stream.php new file mode 100644 index 0000000..b4248fb --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_stream.php @@ -0,0 +1,80 @@ +resource, '://') !== false) { + $source->filepath = $source->resource; + } else { + $source->filepath = str_replace(':', '://', $source->resource); + } + $source->uid = false; + $source->content = $this->getContent($source); + $source->timestamp = $source->exists = !!$source->content; + } + + /** + * Load template's source from stream into current template object + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + * @throws SmartyException if source cannot be loaded + */ + public function getContent(Smarty_Template_Source $source) + { + $t = ''; + // the availability of the stream has already been checked in Smarty_Resource::fetch() + $fp = fopen($source->filepath, 'r+'); + if ($fp) { + while (!feof($fp) && ($current_line = fgets($fp)) !== false) { + $t .= $current_line; + } + fclose($fp); + + return $t; + } else { + return false; + } + } + + /** + * modify resource_name according to resource handlers specifications + * + * @param Smarty $smarty Smarty instance + * @param string $resource_name resource_name to make unique + * @param boolean $isConfig flag for config resource + * + * @return string unique resource name + */ + public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false) + { + return get_class($this) . '#' . $resource_name; + } +} diff --git a/libs/sysplugins/smarty_internal_resource_string.php b/libs/sysplugins/smarty_internal_resource_string.php new file mode 100644 index 0000000..f2610d9 --- /dev/null +++ b/libs/sysplugins/smarty_internal_resource_string.php @@ -0,0 +1,107 @@ +uid = $source->filepath = sha1($source->name . $source->smarty->_joined_template_dir); + $source->timestamp = $source->exists = true; + } + + /** + * Load template's source from $resource_name into current template object + * + * @uses decode() to decode base64 and urlencoded template_resources + * + * @param Smarty_Template_Source $source source object + * + * @return string template source + */ + public function getContent(Smarty_Template_Source $source) + { + return $this->decode($source->name); + } + + /** + * decode base64 and urlencode + * + * @param string $string template_resource to decode + * + * @return string decoded template_resource + */ + protected function decode($string) + { + // decode if specified + if (($pos = strpos($string, ':')) !== false) { + if (!strncmp($string, 'base64', 6)) { + return base64_decode(substr($string, 7)); + } elseif (!strncmp($string, 'urlencode', 9)) { + return urldecode(substr($string, 10)); + } + } + + return $string; + } + + /** + * modify resource_name according to resource handlers specifications + * + * @param Smarty $smarty Smarty instance + * @param string $resource_name resource_name to make unique + * @param boolean $isConfig flag for config resource + * + * @return string unique resource name + */ + public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false) + { + return get_class($this) . '#' . $this->decode($resource_name); + } + + /** + * Determine basename for compiled filename + * Always returns an empty string. + * + * @param Smarty_Template_Source $source source object + * + * @return string resource's basename + */ + public function getBasename(Smarty_Template_Source $source) + { + return ''; + } + + /* + * Disable timestamp checks for string resource. + * + * @return bool + */ + public function checkTimestamps() + { + return false; + } +} + diff --git a/libs/sysplugins/smarty_internal_runtime_cachemodify.php b/libs/sysplugins/smarty_internal_runtime_cachemodify.php new file mode 100644 index 0000000..a91874a --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_cachemodify.php @@ -0,0 +1,68 @@ +isCached() && !$_template->compiled->has_nocache_code; + $_last_modified_date = + @substr($_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ], 0, strpos($_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ], 'GMT') + 3); + if ($_isCached && $cached->timestamp <= strtotime($_last_modified_date)) { + switch (PHP_SAPI) { + case 'cgi': // php-cgi < 5.3 + case 'cgi-fcgi': // php-cgi >= 5.3 + case 'fpm-fcgi': // php-fpm >= 5.3.3 + header('Status: 304 Not Modified'); + break; + + case 'cli': + if ( /* ^phpunit */ + !empty($_SERVER[ 'SMARTY_PHPUNIT_DISABLE_HEADERS' ]) /* phpunit$ */ + ) { + $_SERVER[ 'SMARTY_PHPUNIT_HEADERS' ][] = '304 Not Modified'; + } + break; + + default: + if ( /* ^phpunit */ + !empty($_SERVER[ 'SMARTY_PHPUNIT_DISABLE_HEADERS' ]) /* phpunit$ */ + ) { + $_SERVER[ 'SMARTY_PHPUNIT_HEADERS' ][] = '304 Not Modified'; + } else { + header($_SERVER[ 'SERVER_PROTOCOL' ] . ' 304 Not Modified'); + } + break; + } + } else { + switch (PHP_SAPI) { + case 'cli': + if ( /* ^phpunit */ + !empty($_SERVER[ 'SMARTY_PHPUNIT_DISABLE_HEADERS' ]) /* phpunit$ */ + ) { + $_SERVER[ 'SMARTY_PHPUNIT_HEADERS' ][] = + 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $cached->timestamp) . ' GMT'; + } + break; + default: + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cached->timestamp) . ' GMT'); + break; + } + echo $content; + } + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_cacheresourcefile.php b/libs/sysplugins/smarty_internal_runtime_cacheresourcefile.php new file mode 100644 index 0000000..38ad41c --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_cacheresourcefile.php @@ -0,0 +1,142 @@ +use_sub_dirs ? '/' : '^'; + $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0; + $_dir = $smarty->getCacheDir(); + if ($_dir == '/') { //We should never want to delete this! + return 0; + } + $_dir_length = strlen($_dir); + if (isset($_cache_id)) { + $_cache_id_parts = explode('|', $_cache_id); + $_cache_id_parts_count = count($_cache_id_parts); + if ($smarty->use_sub_dirs) { + foreach ($_cache_id_parts as $id_part) { + $_dir .= $id_part . $smarty->ds; + } + } + } + if (isset($resource_name)) { + $_save_stat = $smarty->caching; + $smarty->caching = true; + $tpl = new $smarty->template_class($resource_name, $smarty); + $smarty->caching = $_save_stat; + + // remove from template cache + $tpl->source; // have the template registered before unset() + + if ($tpl->source->exists) { + $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); + } else { + return 0; + } + } + $_count = 0; + $_time = time(); + if (file_exists($_dir)) { + $_cacheDirs = new RecursiveDirectoryIterator($_dir); + $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); + foreach ($_cache as $_file) { + if (substr(basename($_file->getPathname()), 0, 1) == '.') { + continue; + } + $_filepath = (string) $_file; + // directory ? + if ($_file->isDir()) { + if (!$_cache->isDot()) { + // delete folder if empty + @rmdir($_file->getPathname()); + } + } else { + // delete only php files + if (substr($_filepath, - 4) !== '.php') { + continue; + } + $_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length))); + $_parts_count = count($_parts); + // check name + if (isset($resource_name)) { + if ($_parts[ $_parts_count - 1 ] != $_resourcename_parts) { + continue; + } + } + // check compile id + if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ]) || + $_parts[ $_parts_count - 2 - $_compile_id_offset ] != $_compile_id) + ) { + continue; + } + // check cache id + if (isset($_cache_id)) { + // count of cache id parts + $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : + $_parts_count - 1 - $_compile_id_offset; + if ($_parts_count < $_cache_id_parts_count) { + continue; + } + for ($i = 0; $i < $_cache_id_parts_count; $i ++) { + if ($_parts[ $i ] != $_cache_id_parts[ $i ]) { + continue 2; + } + } + } + if (is_file($_filepath)) { + // expired ? + if (isset($exp_time)) { + if ($exp_time < 0) { + preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match); + if ($_time < (@filemtime($_filepath) + $match[ 1 ])) { + continue; + } + } else { + if ($_time - @filemtime($_filepath) < $exp_time) { + continue; + } + } + } + $_count += @unlink($_filepath) ? 1 : 0; + if (function_exists('opcache_invalidate') + && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) + ) { + opcache_invalidate($_filepath, true); + } elseif (function_exists('apc_delete_file')) { + apc_delete_file($_filepath); + } + } + } + } + } + return $_count; + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_runtime_capture.php b/libs/sysplugins/smarty_internal_runtime_capture.php new file mode 100644 index 0000000..18da7e8 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_capture.php @@ -0,0 +1,169 @@ +isRegistered) { + $this->register($_template); + } + $this->captureStack[] = array($buffer, + $assign, + $append); + $this->captureCount ++; + ob_start(); + } + + /** + * Register callbacks in template class + * + * @param \Smarty_Internal_Template $_template + */ + private function register(Smarty_Internal_Template $_template) + { + $_template->startRenderCallbacks[] = array($this, + 'startRender'); + $_template->endRenderCallbacks[] = array($this, + 'endRender'); + $this->startRender($_template); + $this->isRegistered = true; + } + + /** + * Start render callback + * + * @param \Smarty_Internal_Template $_template + */ + public function startRender(Smarty_Internal_Template $_template) + { + $this->countStack[] = $this->captureCount; + $this->captureCount = 0; + } + + /** + * Close capture section + * + * @param \Smarty_Internal_Template $_template + * + * @throws \SmartyException + */ + public function close(Smarty_Internal_Template $_template) + { + if ($this->captureCount) { + list($buffer, $assign, $append) = array_pop($this->captureStack); + $this->captureCount --; + if (isset($assign)) { + $_template->assign($assign, ob_get_contents()); + } + if (isset($append)) { + $_template->append($append, ob_get_contents()); + } + $this->namedBuffer[ $buffer ] = ob_get_clean(); + } else { + $this->error($_template); + } + } + + /** + * Error exception on not matching {capture}{/capture} + * + * @param \Smarty_Internal_Template $_template + * + * @throws \SmartyException + */ + public function error(Smarty_Internal_Template $_template) + { + throw new SmartyException("Not matching {capture}{/capture} in \"{$_template->template_resource}\""); + } + + /** + * Return content of named capture buffer by key or as array + * + * @param \Smarty_Internal_Template $_template + * @param string|null $name + * + * @return string|string[]|null + */ + public function getBuffer(Smarty_Internal_Template $_template, $name = null) + { + if (isset($name)) { + return isset($this->namedBuffer[ $name ]) ? $this->namedBuffer[ $name ] : null; + } else { + return $this->namedBuffer; + } + } + + /** + * End render callback + * + * @param \Smarty_Internal_Template $_template + * + * @throws \SmartyException + */ + public function endRender(Smarty_Internal_Template $_template) + { + if ($this->captureCount) { + $this->error($_template); + } else { + $this->captureCount = array_pop($this->countStack); + } + } + +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_runtime_codeframe.php b/libs/sysplugins/smarty_internal_runtime_codeframe.php new file mode 100644 index 0000000..ceb386f --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_codeframe.php @@ -0,0 +1,98 @@ +compiled->has_nocache_code; + $properties[ 'file_dependency' ] = $_template->compiled->file_dependency; + $properties[ 'includes' ] = $_template->compiled->includes; + } else { + $properties[ 'has_nocache_code' ] = $_template->cached->has_nocache_code; + $properties[ 'file_dependency' ] = $_template->cached->file_dependency; + $properties[ 'cache_lifetime' ] = $_template->cache_lifetime; + } + $output = "source->filepath) . "\" */\n\n"; + $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n"; + $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' . + ($cache ? 'true' : 'false') . ")"; + $output .= "if ({$dec}) {\n"; + $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n"; + if (!$cache && !empty($compiler->tpl_function)) { + $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " . + var_export($compiler->tpl_function, true) . ");\n"; + } + if ($cache && isset($_template->smarty->ext->_tplFunction)) { + $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " . + var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n"; + + } + // include code for plugins + if (!$cache) { + if (!empty($_template->compiled->required_plugins[ 'compiled' ])) { + foreach ($_template->compiled->required_plugins[ 'compiled' ] as $tmp) { + foreach ($tmp as $data) { + $file = addslashes($data[ 'file' ]); + if (is_array($data[ 'function' ])) { + $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n"; + } else { + $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n"; + } + } + } + } + if ($_template->caching && !empty($_template->compiled->required_plugins[ 'nocache' ])) { + $_template->compiled->has_nocache_code = true; + $output .= "echo '/*%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/smarty; "; + foreach ($_template->compiled->required_plugins[ 'nocache' ] as $tmp) { + foreach ($tmp as $data) { + $file = addslashes($data[ 'file' ]); + if (is_array($data[ 'function' ])) { + $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n"); + } else { + $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n"); + } + } + } + $output .= "?>/*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/';\n"; + } + } + $output .= "?>\n"; + $output .= $content; + $output .= ""; + $output .= $functions; + $output .= "[\n]?<\?php\s*/', '/\?>\s*$/'), array("\n", ''), $output); + } +} \ No newline at end of file diff --git a/libs/sysplugins/smarty_internal_runtime_filterhandler.php b/libs/sysplugins/smarty_internal_runtime_filterhandler.php new file mode 100644 index 0000000..f6cfa7c --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_filterhandler.php @@ -0,0 +1,69 @@ +smarty->autoload_filters[ $type ])) { + foreach ((array) $template->smarty->autoload_filters[ $type ] as $name) { + $plugin_name = "Smarty_{$type}filter_{$name}"; + if (function_exists($plugin_name)) { + $callback = $plugin_name; + } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) { + $callback = array($plugin_name, 'execute'); + } elseif ($template->smarty->loadPlugin($plugin_name, false)) { + if (function_exists($plugin_name)) { + // use loaded Smarty2 style plugin + $callback = $plugin_name; + } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) { + // loaded class of filter plugin + $callback = array($plugin_name, 'execute'); + } else { + throw new SmartyException("Auto load {$type}-filter plugin method \"{$plugin_name}::execute\" not callable"); + } + } else { + // nothing found, throw exception + throw new SmartyException("Unable to auto load {$type}-filter plugin \"{$plugin_name}\""); + } + $content = call_user_func($callback, $content, $template); + } + } + // loop over registered filters of specified type + if (!empty($template->smarty->registered_filters[ $type ])) { + foreach ($template->smarty->registered_filters[ $type ] as $key => $name) { + $content = call_user_func($template->smarty->registered_filters[ $type ][ $key ], $content, $template); + } + } + // return filtered output + return $content; + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php new file mode 100644 index 0000000..8b60768 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -0,0 +1,151 @@ +count($from); + } else { + settype($from, 'array'); + } + } + if (!isset($total)) { + $total = empty($from) ? 0 : (($needTotal || isset($properties[ 'total' ])) ? count($from) : 1); + } + if (isset($tpl->tpl_vars[ $item ])) { + $saveVars[ 'item' ] = array($item, + $tpl->tpl_vars[ $item ]); + } + $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); + if ($total === 0) { + $from = null; + } else { + if ($key) { + if (isset($tpl->tpl_vars[ $key ])) { + $saveVars[ 'key' ] = array($key, + $tpl->tpl_vars[ $key ]); + } + $tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache); + } + } + if ($needTotal) { + $tpl->tpl_vars[ $item ]->total = $total; + } + if ($name) { + $namedVar = "__smarty_foreach_{$name}"; + if (isset($tpl->tpl_vars[ $namedVar ])) { + $saveVars[ 'named' ] = array($namedVar, + $tpl->tpl_vars[ $namedVar ]); + } + $namedProp = array(); + if (isset($properties[ 'total' ])) { + $namedProp[ 'total' ] = $total; + } + if (isset($properties[ 'iteration' ])) { + $namedProp[ 'iteration' ] = 0; + } + if (isset($properties[ 'index' ])) { + $namedProp[ 'index' ] = - 1; + } + if (isset($properties[ 'show' ])) { + $namedProp[ 'show' ] = ($total > 0); + } + $tpl->tpl_vars[ $namedVar ] = new Smarty_Variable($namedProp); + } + $this->stack[] = $saveVars; + return $from; + } + + /** + * + * [util function] counts an array, arrayAccess/traversable or PDOStatement object + * + * @param mixed $value + * + * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 + * for empty elements + */ + public function count($value) + { + if ($value instanceof IteratorAggregate) { + // Note: getIterator() returns a Traversable, not an Iterator + // thus rewind() and valid() methods may not be present + return iterator_count($value->getIterator()); + } elseif ($value instanceof Iterator) { + return $value instanceof Generator ? 1 : iterator_count($value); + } elseif ($value instanceof Countable) { + return count($value); + } elseif ($value instanceof PDOStatement) { + return $value->rowCount(); + } elseif ($value instanceof Traversable) { + return iterator_count($value); + } + return count((array) $value); + } + + /** + * Restore saved variables + * + * will be called by {break n} or {continue n} for the required number of levels + * + * @param \Smarty_Internal_Template $tpl + * @param int $levels number of levels + */ + public function restore(Smarty_Internal_Template $tpl, $levels = 1) + { + while ($levels) { + $saveVars = array_pop($this->stack); + if (!empty($saveVars)) { + if (isset($saveVars[ 'item' ])) { + $item = &$saveVars[ 'item' ]; + $tpl->tpl_vars[ $item[ 0 ] ]->value = $item[ 1 ]->value; + } + if (isset($saveVars[ 'key' ])) { + $tpl->tpl_vars[ $saveVars[ 'key' ][ 0 ] ] = $saveVars[ 'key' ][ 1 ]; + } + if (isset($saveVars[ 'named' ])) { + $tpl->tpl_vars[ $saveVars[ 'named' ][ 0 ] ] = $saveVars[ 'named' ][ 1 ]; + } + } + $levels --; + } + } + +} diff --git a/libs/sysplugins/smarty_internal_runtime_getincludepath.php b/libs/sysplugins/smarty_internal_runtime_getincludepath.php new file mode 100644 index 0000000..e12b0c5 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_getincludepath.php @@ -0,0 +1,182 @@ +_include_path != $_i_path) { + $this->_include_dirs = array(); + $this->_include_path = $_i_path; + $_dirs = (array) explode(PATH_SEPARATOR, $_i_path); + foreach ($_dirs as $_path) { + if (is_dir($_path)) { + $this->_include_dirs[] = $smarty->_realpath($_path . $smarty->ds, true); + } + } + return true; + } + return false; + } + + /** + * return array with include path directories + * + * @param \Smarty $smarty + * + * @return array + */ + public function getIncludePathDirs(Smarty $smarty) + { + $this->isNewIncludePath($smarty); + return $this->_include_dirs; + } + + /** + * Return full file path from PHP include_path + * + * @param string[] $dirs + * @param string $file + * @param \Smarty $smarty + * + * @return bool|string full filepath or false + * + */ + public function getIncludePath($dirs, $file, Smarty $smarty) + { + //if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = false)) { + if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : + $this->_has_stream_include = function_exists('stream_resolve_include_path')) + ) { + $this->isNewIncludePath($smarty); + } + // try PHP include_path + foreach ($dirs as $dir) { + $dir_n = isset($this->number[ $dir ]) ? $this->number[ $dir ] : $this->number[ $dir ] = $this->counter ++; + if (isset($this->isFile[ $dir_n ][ $file ])) { + if ($this->isFile[ $dir_n ][ $file ]) { + return $this->isFile[ $dir_n ][ $file ]; + } else { + continue; + } + } + if (isset($this->_user_dirs[ $dir_n ])) { + if (false === $this->_user_dirs[ $dir_n ]) { + continue; + } else { + $dir = $this->_user_dirs[ $dir_n ]; + } + } else { + if ($dir[ 0 ] == '/' || $dir[ 1 ] == ':') { + $dir = str_ireplace(getcwd(), '.', $dir); + if ($dir[ 0 ] == '/' || $dir[ 1 ] == ':') { + $this->_user_dirs[ $dir_n ] = false; + continue; + } + } + $dir = substr($dir, 2); + $this->_user_dirs[ $dir_n ] = $dir; + } + if ($this->_has_stream_include) { + $path = stream_resolve_include_path($dir . (isset($file) ? $file : '')); + if ($path) { + return $this->isFile[ $dir_n ][ $file ] = $path; + } + } else { + foreach ($this->_include_dirs as $key => $_i_path) { + $path = isset($this->isPath[ $key ][ $dir_n ]) ? $this->isPath[ $key ][ $dir_n ] : + $this->isPath[ $key ][ $dir_n ] = is_dir($_dir_path = $_i_path . $dir) ? $_dir_path : false; + if ($path === false) { + continue; + } + if (isset($file)) { + $_file = $this->isFile[ $dir_n ][ $file ] = (is_file($path . $file)) ? $path . $file : false; + if ($_file) { + return $_file; + } + } else { + // no file was given return directory path + return $path; + } + } + } + } + return false; + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_inheritance.php b/libs/sysplugins/smarty_internal_runtime_inheritance.php new file mode 100644 index 0000000..5e4bd2a --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_inheritance.php @@ -0,0 +1,241 @@ +state == 3 && (strpos($tpl->template_resource, 'extendsall') === false)) { + $tpl->inheritance = new Smarty_Internal_Runtime_Inheritance(); + $tpl->inheritance->init($tpl, $initChild, $blockNames); + return; + } + $this->tplIndex ++; + $this->sources[ $this->tplIndex ] = $tpl->source; + + // start of child sub template(s) + if ($initChild) { + $this->state = 1; + if (!$this->inheritanceLevel) { + //grab any output of child templates + ob_start(); + } + $this->inheritanceLevel ++; + // $tpl->startRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateStart'); + // $tpl->endRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateEnd'); + } + // if state was waiting for parent change state to parent + if ($this->state == 2) { + $this->state = 3; + } + } + + /** + * End of child template(s) + * - if outer level is reached flush output buffer and switch to wait for parent template state + * + * @param \Smarty_Internal_Template $tpl + * @param null|string $template optional name of inheritance parent template + * @param null|string $uid uid of inline template + * @param null|string $func function call name of inline template + */ + public function endChild(Smarty_Internal_Template $tpl, $template = null, $uid = null, $func = null) + { + $this->inheritanceLevel --; + if (!$this->inheritanceLevel) { + ob_end_clean(); + $this->state = 2; + } + if (isset($template) && (($tpl->parent->_isTplObj() && $tpl->parent->source->type !== 'extends') || $tpl->smarty->extends_recursion)) { + $tpl->_subTemplateRender($template, $tpl->cache_id, $tpl->compile_id, $tpl->caching ? 9999 : 0, + $tpl->cache_lifetime, array(), 2, false, $uid, $func); + } + } + + /** + * Smarty_Internal_Block constructor. + * - if outer level {block} of child template ($state == 1) save it as child root block + * - otherwise process inheritance and render + * + * @param \Smarty_Internal_Template $tpl + * @param $className + * @param string $name + * @param int|null $tplIndex index of outer level {block} if nested + */ + public function instanceBlock(Smarty_Internal_Template $tpl, $className, $name, $tplIndex = null) + { + $block = new $className($name, isset($tplIndex) ? $tplIndex : $this->tplIndex); + if (isset($this->childRoot[ $name ])) { + $block->child = $this->childRoot[ $name ]; + } + if ($this->state == 1) { + $this->childRoot[ $name ] = $block; + return; + } + // make sure we got child block of child template of current block + while ($block->child && $block->tplIndex <= $block->child->tplIndex) { + $block->child = $block->child->child; + } + $this->process($tpl, $block); + } + + /** + * Goto child block or render this + * + * @param \Smarty_Internal_Template $tpl + * @param \Smarty_Internal_Block $block + * @param \Smarty_Internal_Block|null $parent + * + * @throws \SmartyException + */ + public function process(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block, + Smarty_Internal_Block $parent = null) + { + if ($block->hide && !isset($block->child)) { + return; + } + if (isset($block->child) && $block->child->hide && !isset($block->child->child)) { + $block->child = null; + } + $block->parent = $parent; + if ($block->append && !$block->prepend && isset($parent)) { + $this->callParent($tpl, $block); + } + if ($block->callsChild || !isset($block->child) || ($block->child->hide && !isset($block->child->child))) { + $this->callBlock($block, $tpl); + } else { + $this->process($tpl, $block->child, $block); + } + if ($block->prepend && isset($parent)) { + $this->callParent($tpl, $block); + if ($block->append) { + if ($block->callsChild || !isset($block->child) || + ($block->child->hide && !isset($block->child->child)) + ) { + $this->callBlock($block, $tpl); + } else { + $this->process($tpl, $block->child, $block); + } + } + } + $block->parent = null; + } + + /** + * Render child on {$smarty.block.child} + * + * @param \Smarty_Internal_Template $tpl + * @param \Smarty_Internal_Block $block + */ + public function callChild(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block) + { + if (isset($block->child)) { + $this->process($tpl, $block->child, $block); + } + } + + /** + * Render parent on {$smarty.block.parent} or {block append/prepend} * + * + * @param \Smarty_Internal_Template $tpl + * @param \Smarty_Internal_Block $block + * + * @param null $name + * + * @throws \SmartyException + */ + public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block, $name = null) + { + if (isset($name)) { + $block = $block->parent; + while (isset($block)) { + if (isset($block->subBlocks[ $name ])) { + } else { + $block = $block->parent; + } + } + return; + } else if (isset($block->parent)) { + $this->callBlock($block->parent, $tpl); + } else { + throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'"); + } + } + + /** + * @param \Smarty_Internal_Block $block + * @param \Smarty_Internal_Template $tpl + */ + public function callBlock(Smarty_Internal_Block $block, Smarty_Internal_Template $tpl) + { + $this->sourceStack[] = $tpl->source; + $tpl->source = $this->sources[ $block->tplIndex ]; + $block->callBlock($tpl); + $tpl->source = array_pop($this->sourceStack); + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_make_nocache.php b/libs/sysplugins/smarty_internal_runtime_make_nocache.php new file mode 100644 index 0000000..6538277 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_make_nocache.php @@ -0,0 +1,56 @@ +tpl_vars[ $var ])) { + $export = + preg_replace('/^Smarty_Variable::__set_state[(]|[)]$/', '', var_export($tpl->tpl_vars[ $var ], true)); + if (preg_match('/(\w+)::__set_state/', $export, $match)) { + throw new SmartyException("{make_nocache \${$var}} in template '{$tpl->source->name}': variable does contain object '{$match[1]}' not implementing method '__set_state'"); + } + echo "/*%%SmartyNocache:{$tpl->compiled->nocache_hash}%%*/smarty->ext->_make_nocache->store(\$_smarty_tpl, '{$var}', ", '\\') . + $export . ");?>\n/*/%%SmartyNocache:{$tpl->compiled->nocache_hash}%%*/"; + } + } + + /** + * Store variable value saved while rendering compiled template in cached template context + * + * @param \Smarty_Internal_Template $tpl + * @param string $var variable name + * @param array $properties + */ + public function store(Smarty_Internal_Template $tpl, $var, $properties) + { + // do not overwrite existing nocache variables + if (!isset($tpl->tpl_vars[ $var ]) || !$tpl->tpl_vars[ $var ]->nocache) { + $newVar = new Smarty_Variable(); + unset($properties[ 'nocache' ]); + foreach ($properties as $k => $v) { + $newVar->$k = $v; + } + $tpl->tpl_vars[ $var ] = $newVar; + } + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_tplfunction.php b/libs/sysplugins/smarty_internal_runtime_tplfunction.php new file mode 100644 index 0000000..f6b36f2 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_tplfunction.php @@ -0,0 +1,171 @@ +tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] : + (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : null); + if (isset($funcParam)) { + if (!$tpl->caching || ($tpl->caching && $nocache)) { + $function = $funcParam[ 'call_name' ]; + } else { + if (isset($funcParam[ 'call_name_caching' ])) { + $function = $funcParam[ 'call_name_caching' ]; + } else { + $function = $funcParam[ 'call_name' ]; + } + } + if (function_exists($function)) { + $this->saveTemplateVariables($tpl, $name); + $function ($tpl, $params); + $this->restoreTemplateVariables($tpl, $name); + return; + } + // try to load template function dynamically + if ($this->addTplFuncToCache($tpl, $name, $function)) { + $this->saveTemplateVariables($tpl, $name); + $function ($tpl, $params); + $this->restoreTemplateVariables($tpl, $name); + return; + } + } + throw new SmartyException("Unable to find template function '{$name}'"); + } + + /** + * Register template functions defined by template + * + * @param \Smarty|\Smarty_Internal_Template|\Smarty_Internal_TemplateBase $obj + * @param array $tplFunctions source information array of template functions defined in template + * @param bool $override if true replace existing functions with same name + */ + public function registerTplFunctions(Smarty_Internal_TemplateBase $obj, $tplFunctions, $override = true) + { + $obj->tplFunctions = + $override ? array_merge($obj->tplFunctions, $tplFunctions) : array_merge($tplFunctions, $obj->tplFunctions); + // make sure that the template functions are known in parent templates + if ($obj->_isSubTpl()) { + $obj->smarty->ext->_tplFunction->registerTplFunctions($obj->parent, $tplFunctions, false); + } else { + $obj->smarty->tplFunctions = $override ? array_merge($obj->smarty->tplFunctions, $tplFunctions) : + array_merge($tplFunctions, $obj->smarty->tplFunctions); + } + } + + /** + * Return source parameter array for single or all template functions + * + * @param \Smarty_Internal_Template $tpl template object + * @param null|string $name template function name + * + * @return array|bool|mixed + */ + public function getTplFunction(Smarty_Internal_Template $tpl, $name = null) + { + if (isset($name)) { + return isset($tpl->tplFunctions[ $name ]) ? $tpl->tplFunctions[ $name ] : + (isset($tpl->smarty->tplFunctions[ $name ]) ? $tpl->smarty->tplFunctions[ $name ] : false); + } else { + return empty($tpl->tplFunctions) ? $tpl->smarty->tplFunctions : $tpl->tplFunctions; + } + } + + /** + * + * Add template function to cache file for nocache calls + * + * @param Smarty_Internal_Template $tpl + * @param string $_name template function name + * @param string $_function PHP function name + * + * @return bool + */ + public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function) + { + $funcParam = $tpl->tplFunctions[ $_name ]; + if (is_file($funcParam[ 'compiled_filepath' ])) { + // read compiled file + $code = file_get_contents($funcParam[ 'compiled_filepath' ]); + // grab template function + if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) { + // grab source info from file dependency + preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1); + unset($code); + // make PHP function known + eval($match[ 0 ]); + if (function_exists($_function)) { + // search cache file template + $tplPtr = $tpl; + while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { + $tplPtr = $tplPtr->parent; + } + // add template function code to cache file + if (isset($tplPtr->cached)) { + /* @var Smarty_Template_Cached $cache */ + $cache = $tplPtr->cached; + $content = $cache->read($tplPtr); + if ($content) { + // check if we must update file dependency + if (!preg_match("/'{$funcParam['uid']}'(.*?)'nocache_hash'/", $content, $match2)) { + $content = preg_replace("/('file_dependency'(.*?)\()/", "\\1{$match1[0]}", $content); + } + $tplPtr->smarty->ext->_updateCache->write($cache, $tplPtr, + preg_replace('/\s*\?>\s*$/', "\n", $content) . + "\n" . preg_replace(array('/^\s*<\?php\s+/', + '/\s*\?>\s*$/',), "\n", + $match[ 0 ])); + } + } + return true; + } + } + } + return false; + } + + /** + * Save current template variables on stack + * + * @param \Smarty_Internal_Template $tpl + * @param string $name stack name + */ + public function saveTemplateVariables(Smarty_Internal_Template $tpl, $name) + { + $tpl->_cache[ 'varStack' ][] = + array('tpl' => $tpl->tpl_vars, 'config' => $tpl->config_vars, 'name' => "_tplFunction_{$name}"); + } + + /** + * Restore saved variables into template objects + * + * @param \Smarty_Internal_Template $tpl + * @param string $name stack name + */ + public function restoreTemplateVariables(Smarty_Internal_Template $tpl, $name) + { + if (isset($tpl->_cache[ 'varStack' ])) { + $vars = array_pop($tpl->_cache[ 'varStack' ]); + $tpl->tpl_vars = $vars[ 'tpl' ]; + $tpl->config_vars = $vars[ 'config' ]; + } + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_updatecache.php b/libs/sysplugins/smarty_internal_runtime_updatecache.php new file mode 100644 index 0000000..ceee3b7 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_updatecache.php @@ -0,0 +1,165 @@ +hashes[ $_template->compiled->nocache_hash ]); + if (!empty($cached->hashes)) { + $hash_array = array(); + foreach ($cached->hashes as $hash => $foo) { + $hash_array[] = "/{$hash}/"; + } + $content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content); + } + $_template->cached->has_nocache_code = false; + // get text between non-cached items + $cache_split = + preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s", + $content); + // get non-cached items + preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s", + $content, $cache_parts); + $content = ''; + // loop over items, stitch back together + foreach ($cache_split as $curr_idx => $curr_split) { + // escape PHP tags in template content + $content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|]*>)|(]*>)|(
]*>.*?]*>)#is', + $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $store[] = $match[ 0 ][ 0 ]; + $_length = strlen($match[ 0 ][ 0 ]); + $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@'; + $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] - $_offset, $_length); + + $_offset += $_length - strlen($replace); + ++ $_store; + } + } + $expressions = array(// replace multiple spaces between tags by a single space + '#(:SMARTY@!@|>)[\040\011]+(?=@!@SMARTY:|<)#s' => '\1 \2', + // remove newline between tags + '#(:SMARTY@!@|>)[\040\011]*[\n]\s*(?=@!@SMARTY:|<)#s' => '\1\2', + // remove multiple spaces between attributes (but not in attribute values!) + '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5', + '#>[\040\011]+$#Ss' => '> ', + '#>[\040\011]*[\n]\s*$#Ss' => '>', + $this->stripRegEx => '',); + + $text = preg_replace(array_keys($expressions), array_values($expressions), $text); + $_offset = 0; + if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $text, $matches, + PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) { + foreach ($matches as $match) { + $_length = strlen($match[ 0 ][ 0 ]); + $replace = $store[ $match[ 1 ][ 0 ] ]; + $text = substr_replace($text, $replace, $match[ 0 ][ 1 ] + $_offset, $_length); + + $_offset += strlen($replace) - $_length; + ++ $_store; + } + } + } else { + $text = preg_replace($this->stripRegEx, '', $text); + } + } + return new Smarty_Internal_ParseTree_Text($text); + } + return null; + } + + /** + * lazy loads internal compile plugin for tag and calls the compile method + * compile objects cached for reuse. + * class name format: Smarty_Internal_Compile_TagName + * plugin filename format: Smarty_Internal_TagName.php + * + * @param string $tag tag name + * @param array $args list of tag attributes + * @param mixed $param1 optional parameter + * @param mixed $param2 optional parameter + * @param mixed $param3 optional parameter + * + * @return string|bool compiled code or false + */ + public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) + { + /* @var Smarty_Internal_CompileBase $tagCompiler */ + $tagCompiler = $this->getTagCompiler($tag); + // compile this tag + return $tagCompiler === false ? false : $tagCompiler->compile($args, $this, $param1, $param2, $param3); + } + + /** + * lazy loads internal compile plugin for tag compile objects cached for reuse. + * + * class name format: Smarty_Internal_Compile_TagName + * plugin filename format: Smarty_Internal_TagName.php + * + * @param string $tag tag name + * + * @return Smarty_Internal_CompileBase|bool tag compiler object or false if not found + */ + public function getTagCompiler($tag) + { + // re-use object if already exists + if (!isset(self::$_tag_objects[ $tag ])) { + // lazy load internal compiler plugin + $_tag = explode('_', $tag); + $_tag = array_map('ucfirst', $_tag); + $class_name = 'Smarty_Internal_Compile_' . implode('_', $_tag); + if (class_exists($class_name) && + (!isset($this->smarty->security_policy) || $this->smarty->security_policy->isTrustedTag($tag, $this)) + ) { + self::$_tag_objects[ $tag ] = new $class_name; + } else { + self::$_tag_objects[ $tag ] = false; + } + } + return self::$_tag_objects[ $tag ]; + } + + /** + * Check for plugins and return function name + * + * @param $plugin_name + * @param string $plugin_type type of plugin + * + * @return string call name of function + */ + public function getPlugin($plugin_name, $plugin_type) + { + $function = null; + if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { + if (isset($this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ])) { + $function = + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ]; + } elseif (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ])) { + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ] = + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ]; + $function = + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ]; + } + } else { + if (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ])) { + $function = + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ]; + } elseif (isset($this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ])) { + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ] = + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ]; + $function = + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ]; + } + } + if (isset($function)) { + if ($plugin_type === 'modifier') { + $this->modifier_plugins[ $plugin_name ] = true; + } + + return $function; + } + // loop through plugin dirs and find the plugin + $function = 'smarty_' . $plugin_type . '_' . $plugin_name; + $file = $this->smarty->loadPlugin($function, false); + + if (is_string($file)) { + if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'file' ] = + $file; + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ $plugin_type ][ 'function' ] = + $function; + } else { + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'file' ] = + $file; + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ $plugin_type ][ 'function' ] = + $function; + } + if ($plugin_type === 'modifier') { + $this->modifier_plugins[ $plugin_name ] = true; + } + + return $function; + } + if (is_callable($function)) { + // plugin function is defined in the script + return $function; + } + + return false; + } + + /** + * Check for plugins by default plugin handler + * + * @param string $tag name of tag + * @param string $plugin_type type of plugin + * + * @return boolean true if found + */ + public function getPluginFromDefaultHandler($tag, $plugin_type) + { + $callback = null; + $script = null; + $cacheable = true; + $result = call_user_func_array($this->smarty->default_plugin_handler_func, array($tag, + $plugin_type, + $this->template, + &$callback, + &$script, + &$cacheable,)); + if ($result) { + $this->tag_nocache = $this->tag_nocache || !$cacheable; + if ($script !== null) { + if (is_file($script)) { + if ($this->template->caching && ($this->nocache || $this->tag_nocache)) { + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $tag ][ $plugin_type ][ 'file' ] = + $script; + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $tag ][ $plugin_type ][ 'function' ] = + $callback; + } else { + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $tag ][ $plugin_type ][ 'file' ] = + $script; + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $tag ][ $plugin_type ][ 'function' ] = + $callback; + } + require_once $script; + } else { + $this->trigger_template_error("Default plugin handler: Returned script file \"{$script}\" for \"{$tag}\" not found"); + } + } + if (is_callable($callback)) { + $this->default_handler_plugins[ $plugin_type ][ $tag ] = array($callback, + true, + array()); + + return true; + } else { + $this->trigger_template_error("Default plugin handler: Returned callback for \"{$tag}\" not callable"); + } + } + + return false; + } + + /** + * Append code segments and remove unneeded ?> \s*$/', $left) && preg_match('/^\s*<\?php\s+/', $right)) { + $left = preg_replace('/\s*\?>\s*$/', "\n", $left); + $left .= preg_replace('/^\s*<\?php\s+/', '', $right); + } else { + $left .= $right; + } + return $left; + } + + /** + * Inject inline code for nocache template sections + * This method gets the content of each template element from the parser. + * If the content is compiled code and it should be not cached the code is injected + * into the rendered output. + * + * @param string $content content of template element + * @param boolean $is_code true if content is compiled code + * + * @return string content + */ + public function processNocacheCode($content, $is_code) + { + // If the template is not evaluated and we have a nocache section and or a nocache tag + if ($is_code && !empty($content)) { + // generate replacement code + if ((!($this->template->source->handler->recompiled) || $this->forceNocache) && $this->template->caching && + !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache) + ) { + $this->template->compiled->has_nocache_code = true; + $_output = addcslashes($content, '\'\\'); + $_output = str_replace("^#^", "'", $_output); + $_output = "nocache_hash}%%*/" . $_output . + "/*/%%SmartyNocache:{$this->nocache_hash}%%*/';?>\n"; + // make sure we include modifier plugins for nocache code + foreach ($this->modifier_plugins as $plugin_name => $dummy) { + if (isset($this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ 'modifier' ])) { + $this->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ $plugin_name ][ 'modifier' ] = + $this->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin_name ][ 'modifier' ]; + } + } + } else { + $_output = $content; + } + } else { + $_output = $content; + } + $this->modifier_plugins = array(); + $this->suppressNocacheProcessing = false; + $this->tag_nocache = false; + + return $_output; + } + + /** + * Get Id + * + * @param string $input + * + * @return bool|string + */ + public function getId($input) + { + if (preg_match('~^([\'"]*)([0-9]*[a-zA-Z_]\w*)\1$~', $input, $match)) { + return $match[ 2 ]; + } + return false; + } + + /** + * Get variable name from string + * + * @param string $input + * + * @return bool|string + */ + public function getVariableName($input) + { + if (preg_match('~^[$]_smarty_tpl->tpl_vars\[[\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*\]->value$~', $input, $match)) { + return $match[ 1 ]; + } + return false; + } + + /** + * Set nocache flag in variable or create new variable + * + * @param string $varName + */ + public function setNocacheInVariable($varName) + { + // create nocache var to make it know for further compiling + if ($_var = $this->getId($varName)) { + if (isset($this->template->tpl_vars[ $_var ])) { + $this->template->tpl_vars[ $_var ] = clone $this->template->tpl_vars[ $_var ]; + $this->template->tpl_vars[ $_var ]->nocache = true; + } else { + $this->template->tpl_vars[ $_var ] = new Smarty_Variable(null, true); + } + } + } + + /** + * @param array $_attr tag attributes + * @param array $validScopes + * + * @return int|string + * @throws \SmartyCompilerException + */ + public function convertScope($_attr, $validScopes) + { + $_scope = 0; + if (isset($_attr[ 'scope' ])) { + $_scopeName = trim($_attr[ 'scope' ], "'\""); + if (is_numeric($_scopeName) && in_array($_scopeName, $validScopes)) { + $_scope = $_scopeName; + } elseif (is_string($_scopeName)) { + $_scopeName = trim($_scopeName, "'\""); + $_scope = isset($validScopes[ $_scopeName ]) ? $validScopes[ $_scopeName ] : false; + } else { + $_scope = false; + } + if ($_scope === false) { + $err = var_export($_scopeName, true); + $this->trigger_template_error("illegal value '{$err}' for \"scope\" attribute", null, true); + } + } + return $_scope; + } + + /** + * Generate nocache code string + * + * @param string $code PHP code + * + * @return string + */ + public function makeNocacheCode($code) + { + return "echo '/*%%SmartyNocache:{$this->nocache_hash}%%*//*/%%SmartyNocache:{$this->nocache_hash}%%*/';\n"; + } + + /** + * display compiler error messages without dying + * If parameter $args is empty it is a parser detected syntax error. + * In this case the parser is called to obtain information about expected tokens. + * If parameter $args contains a string this is used as error message + * + * @param string $args individual error message or null + * @param string $line line-number + * @param null|bool $tagline if true the line number of last tag + * + * @throws \SmartyCompilerException when an unexpected token is found + */ + public function trigger_template_error($args = null, $line = null, $tagline = null) + { + $lex = $this->parser->lex; + if ($tagline === true) { + // get line number of Tag + $line = $lex->taglineno; + } elseif (!isset($line)) { + // get template source line which has error + $line = $lex->line; + } else { + $line = (int) $line; + } + + if (in_array($this->template->source->type, array('eval', + 'string'))) { + $templateName = $this->template->source->type . ':' . trim(preg_replace('![\t\r\n]+!', ' ', + strlen($lex->data) > 40 ? + substr($lex->data, 0, 40) . + '...' : $lex->data)); + } else { + $templateName = $this->template->source->type . ':' . $this->template->source->filepath; + } + + // $line += $this->trace_line_offset; + $match = preg_split("/\n/", $lex->data); + $error_text = + 'Syntax error in template "' . (empty($this->trace_filepath) ? $templateName : $this->trace_filepath) . + '" on line ' . ($line + $this->trace_line_offset) . ' "' . + trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ])) . '" '; + if (isset($args)) { + // individual error message + $error_text .= $args; + } else { + $expect = array(); + // expected token from parser + $error_text .= ' - Unexpected "' . $lex->value . '"'; + if (count($this->parser->yy_get_expected_tokens($this->parser->yymajor)) <= 4) { + foreach ($this->parser->yy_get_expected_tokens($this->parser->yymajor) as $token) { + $exp_token = $this->parser->yyTokenName[ $token ]; + if (isset($lex->smarty_token_names[ $exp_token ])) { + // token type from lexer + $expect[] = '"' . $lex->smarty_token_names[ $exp_token ] . '"'; + } else { + // otherwise internal token name + $expect[] = $this->parser->yyTokenName[ $token ]; + } + } + $error_text .= ', expected one of: ' . implode(' , ', $expect); + } + } + $e = new SmartyCompilerException($error_text); + $e->line = $line; + $e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ])); + $e->desc = $args; + $e->template = $this->template->source->filepath; + throw $e; + } + + /** + * Return var_export() value with all white spaces removed + * + * @param mixed $value + * + * @return string + */ + public function getVarExport($value) + { + return preg_replace('/\s/', '', var_export($value, true)); + } + + /** + * Check if $value contains variable elements + * + * @param mixed $value + * + * @return bool|int + */ + public function isVariable($value) + { + if (is_string($value)) { + return preg_match('/[$(]/', $value); + } + if (is_bool($value) || is_numeric($value)) { + return false; + } + if (is_array($value)) { + foreach ($value as $k => $v) { + if ($this->isVariable($k) || $this->isVariable($v)) { + return true; + } + } + return false; + } + return false; + } + + /** + * Get new prefix variable name + * + * @return string + */ + public function getNewPrefixVariable() + { + ++ self::$prefixVariableNumber; + return $this->getPrefixVariable(); + } + + /** + * Get current prefix variable name + * + * @return string + */ + public function getPrefixVariable() + { + return '$_prefixVariable' . self::$prefixVariableNumber; + } + + /** + * append code to prefix buffer + * + * @param string $code + */ + public function appendPrefixCode($code) + { + $this->prefix_code[] = $code; + } + + /** + * get prefix code string + * + * @return string + */ + public function getPrefixCode() + { + $code = ''; + $prefixArray = array_merge($this->prefix_code, array_pop($this->prefixCodeStack)); + $this->prefixCodeStack[] = array(); + foreach ($prefixArray as $c) { + $code = $this->appendCode($code, $c); + } + $this->prefix_code = array(); + return $code; + } + +} diff --git a/libs/sysplugins/smarty_internal_templatelexer.php b/libs/sysplugins/smarty_internal_templatelexer.php new file mode 100644 index 0000000..a6d0ec2 --- /dev/null +++ b/libs/sysplugins/smarty_internal_templatelexer.php @@ -0,0 +1,1136 @@ + + */ +class Smarty_Internal_Templatelexer +{ + const TEXT = 1; + const TAG = 2; + const TAGBODY = 3; + const LITERAL = 4; + const DOUBLEQUOTEDSTRING = 5; + /** + * Source + * + * @var string + */ + public $data; + /** + * Source length + * + * @var int + */ + public $dataLength = null; + /** + * byte counter + * + * @var int + */ + public $counter; + /** + * token number + * + * @var int + */ + public $token; + /** + * token value + * + * @var string + */ + public $value; + /** + * current line + * + * @var int + */ + public $line; + /** + * tag start line + * + * @var + */ + public $taglineno; + /** + * php code type + * + * @var string + */ + public $phpType = ''; + /** + * escaped left delimiter + * + * @var string + */ + public $ldel = ''; + /** + * escaped left delimiter length + * + * @var int + */ + public $ldel_length = 0; + /** + * escaped right delimiter + * + * @var string + */ + public $rdel = ''; + + /** + * PHP start tag string + * + * @var string + */ + /** + * escaped right delimiter length + * + * @var int + */ + public $rdel_length = 0; + /** + * state number + * + * @var int + */ + public $state = 1; + /** + * Smarty object + * + * @var Smarty + */ + public $smarty = null; + /** + * compiler object + * + * @var Smarty_Internal_TemplateCompilerBase + */ + public $compiler = null; + /** + * trace file + * + * @var resource + */ + public $yyTraceFILE; + /** + * trace prompt + * + * @var string + */ + public $yyTracePrompt; + /** + * XML flag true while processing xml + * + * @var bool + */ + public $is_xml = false; + /** + * state names + * + * @var array + */ + public $state_name = array(1 => 'TEXT', 2 => 'TAG', 3 => 'TAGBODY', 4 => 'LITERAL', 5 => 'DOUBLEQUOTEDSTRING',); + /** + * token names + * + * @var array + */ + public $smarty_token_names = array( // Text for parser error messages + 'NOT' => '(!,not)', 'OPENP' => '(', 'CLOSEP' => ')', 'OPENB' => '[', + 'CLOSEB' => ']', 'PTR' => '->', 'APTR' => '=>', 'EQUAL' => '=', + 'NUMBER' => 'number', 'UNIMATH' => '+" , "-', 'MATH' => '*" , "/" , "%', + 'INCDEC' => '++" , "--', 'SPACE' => ' ', 'DOLLAR' => '$', + 'SEMICOLON' => ';', 'COLON' => ':', 'DOUBLECOLON' => '::', 'AT' => '@', + 'HATCH' => '#', 'QUOTE' => '"', 'BACKTICK' => '`', + 'VERT' => '"|" modifier', 'DOT' => '.', 'COMMA' => '","', + 'QMARK' => '"?"', 'ID' => 'id, name', 'TEXT' => 'text', + 'LDELSLASH' => '{/..} closing tag', 'LDEL' => '{...} Smarty tag', + 'COMMENT' => 'comment', 'AS' => 'as', 'TO' => 'to', + 'PHP' => '" '"<", "==" ... logical operator', + 'TLOGOP' => '"lt", "eq" ... logical operator; "is div by" ... if condition', + 'SCOND' => '"is even" ... if condition',); + /** + * literal tag nesting level + * + * @var int + */ + private $literal_cnt = 0; + /** + * storage for assembled token patterns + * + * @var string + */ + private $yy_global_pattern1 = null; + private $yy_global_pattern2 = null; + + /* + * Check if this tag is autoliteral + */ + private $yy_global_pattern3 = null; + private $yy_global_pattern4 = null; + private $yy_global_pattern5 = null; + private $_yy_state = 1; + private $_yy_stack = array(); + + /** + * constructor + * + * @param string $data template source + * @param Smarty_Internal_TemplateCompilerBase $compiler + */ + function __construct($data, Smarty_Internal_TemplateCompilerBase $compiler) + { + $this->data = $data; + $this->dataLength = strlen($data); + $this->counter = 0; + if (preg_match('/^\xEF\xBB\xBF/i', $this->data, $match)) { + $this->counter += strlen($match[0]); + } + $this->line = 1; + $this->smarty = $compiler->smarty; + $this->compiler = $compiler; + $this->pldel = preg_quote($this->smarty->left_delimiter, '/'); + $this->ldel = $this->pldel . ($this->smarty->auto_literal ? '(?!\\s+)' : '\\s*'); + $this->ldel_length = strlen($this->smarty->left_delimiter); + $this->rdel = preg_quote($this->smarty->right_delimiter, '/'); + $this->rdel_length = strlen($this->smarty->right_delimiter); + $this->smarty_token_names['LDEL'] = $this->smarty->left_delimiter; + $this->smarty_token_names['RDEL'] = $this->smarty->right_delimiter; + } + + public function PrintTrace() + { + $this->yyTraceFILE = fopen('php://output', 'w'); + $this->yyTracePrompt = '