public function compileTemplateSource() { if (!$this->source->recompiled) { $this->properties['file_dependency'] = array(); if ($this->source->components) { $source = end($this->source->components); $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type); } else { $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type); } } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this); } if ($this->smarty->compile_locking && !$this->source->recompiled) { if ($saved_timestamp = $this->compiled->timestamp) { touch($this->compiled->filepath); } } try { $code = $this->compiler->compileTemplate($this); } catch (Exception $e) { if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) { touch($this->compiled->filepath, $saved_timestamp); } throw $e; } if (!$this->source->recompiled && $this->compiler->write_compiled_code) { $_filepath = $this->compiled->filepath; if ($_filepath === false) { throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to'); } Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty); $this->compiled->exists = true; $this->compiled->isCompiled = true; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this); } unset($this->compiler); }
/** * Compiles the template * * If the template is not evaluated the compiled template is saved on disk */ public function compileTemplateSource() { if (!$this->source->recompiled) { $this->properties['file_dependency'] = array(); if ($this->source->components) { // uses real resource for file dependency $source = end($this->source->components); $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type); } else { $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type); } } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this); } // compile locking if ($this->smarty->compile_locking && !$this->source->recompiled) { if ($saved_timestamp = $this->compiled->timestamp) { touch($this->compiled->filepath); } } // call compiler try { $code = $this->compiler->compileTemplate($this); } catch (Exception $e) { // restore old timestamp in case of error if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) { touch($this->compiled->filepath, $saved_timestamp); } throw $e; } // compiling succeded if (!$this->source->recompiled && $this->compiler->write_compiled_code) { // write compiled template $_filepath = $this->compiled->filepath; if ($_filepath === false) { throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to'); } Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty); $this->compiled->exists = true; $this->compiled->isCompiled = true; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this); } // release compiler object to free memory unset($this->compiler); }
/** * Compiles the template * * If the template is not evaluated the compiled template is saved on disk */ public function compileTemplateSource() { if (!$this->resource_object->isEvaluated) { $this->properties['file_dependency'] = array(); $this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp(), $this->resource_type); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this); } // compile template if (!is_object($this->compiler_object)) { // load compiler $this->smarty->loadPlugin($this->resource_object->compiler_class); $this->compiler_object = new $this->resource_object->compiler_class($this->resource_object->template_lexer_class, $this->resource_object->template_parser_class, $this->smarty); } // compile locking if ($this->smarty->compile_locking && !$this->resource_object->isEvaluated) { if ($saved_timestamp = $this->getCompiledTimestamp()) { touch($this->getCompiledFilepath()); } } // call compiler try { $this->compiler_object->compileTemplate($this); } catch (Exception $e) { // restore old timestamp in case of error if ($this->smarty->compile_locking && !$this->resource_object->isEvaluated && $saved_timestamp) { touch($this->getCompiledFilepath(), $saved_timestamp); } throw $e; } // compiling succeded if (!$this->resource_object->isEvaluated && $this->write_compiled_code) { // write compiled template Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty); } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this); } // release objects to free memory Smarty_Internal_TemplateCompilerBase::$_tag_objects = array(); unset($this->compiler_object->parser->root_buffer, $this->compiler_object->parser->current_buffer, $this->compiler_object->parser, $this->compiler_object->lex, $this->compiler_object->template, $this->compiler_object); }
/** * fetches a rendered Smarty template * * @param string $template the resource handle of the template file or template object * @param mixed $cache_id cache id to be used with this template * @param mixed $compile_id compile id to be used with this template * @param object $parent next higher level of Smarty variables * @param bool $display true: display, false: fetch * @param bool $merge_tpl_vars if true parent template variables merged in to local scope * @param bool $no_output_filter if true do not run output filter * @return string rendered template output */ public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false) { if ($template === null && $this instanceof $this->template_class) { $template = $this; } if (!empty($cache_id) && is_object($cache_id)) { $parent = $cache_id; $cache_id = null; } if ($parent === null && ($this instanceof Smarty || is_string($template))) { $parent = $this; } // create template object if necessary $_template = $template instanceof $this->template_class ? $template : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false); // if called by Smarty object make sure we use current caching status if ($this instanceof Smarty) { $_template->caching = $this->caching; } // merge all variable scopes into template if ($merge_tpl_vars) { // save local variables $save_tpl_vars = $_template->tpl_vars; $save_config_vars = $_template->config_vars; $ptr_array = array($_template); $ptr = $_template; while (isset($ptr->parent)) { $ptr_array[] = $ptr = $ptr->parent; } $ptr_array = array_reverse($ptr_array); $parent_ptr = reset($ptr_array); $tpl_vars = $parent_ptr->tpl_vars; $config_vars = $parent_ptr->config_vars; while ($parent_ptr = next($ptr_array)) { if (!empty($parent_ptr->tpl_vars)) { $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars); } if (!empty($parent_ptr->config_vars)) { $config_vars = array_merge($config_vars, $parent_ptr->config_vars); } } if (!empty(Smarty::$global_tpl_vars)) { $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars); } $_template->tpl_vars = $tpl_vars; $_template->config_vars = $config_vars; } // dummy local smarty variable if (!isset($_template->tpl_vars['smarty'])) { $_template->tpl_vars['smarty'] = new Smarty_Variable(); } if (isset($this->smarty->error_reporting)) { $_smarty_old_error_level = error_reporting($this->smarty->error_reporting); } // check URL debugging control if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') { if (isset($_SERVER['QUERY_STRING'])) { $_query_string = $_SERVER['QUERY_STRING']; } else { $_query_string = ''; } if (false !== strpos($_query_string, $this->smarty->smarty_debug_id)) { if (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=on')) { // enable debugging for this browser session setcookie('SMARTY_DEBUG', true); $this->smarty->debugging = true; } elseif (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=off')) { // disable debugging for this browser session setcookie('SMARTY_DEBUG', false); $this->smarty->debugging = false; } else { // enable debugging for this page $this->smarty->debugging = true; } } else { if (isset($_COOKIE['SMARTY_DEBUG'])) { $this->smarty->debugging = true; } } } // must reset merge template date $_template->smarty->merged_templates_func = array(); // get rendered template // disable caching for evaluated code if ($_template->source->recompiled) { $_template->caching = false; } // checks if template exists if (!$_template->source->exists) { if ($_template->parent instanceof Smarty_Internal_Template) { $parent_resource = " in '{$_template->parent->template_resource}'"; } else { $parent_resource = ''; } throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}"); } // read from cache or render if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) { // render template (not loaded and not in cache) if (!$_template->source->uncompiled) { $_smarty_tpl = $_template; if ($_template->source->recompiled) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($_template); } $code = $_template->compiler->compileTemplate($_template); if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($_template); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } try { ob_start(); eval("?>" . $code); unset($code); } catch (Exception $e) { ob_get_clean(); throw $e; } } else { if (!$_template->compiled->exists || $_template->smarty->force_compile && !$_template->compiled->isCompiled) { $_template->compileTemplateSource(); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } if (!$_template->compiled->loaded) { include $_template->compiled->filepath; if ($_template->mustCompile) { // recompile and load again $_template->compileTemplateSource(); include $_template->compiled->filepath; } $_template->compiled->loaded = true; } else { $_template->decodeProperties($_template->compiled->_properties, false); } try { ob_start(); if (empty($_template->properties['unifunc']) || !is_callable($_template->properties['unifunc'])) { throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'"); } array_unshift($_template->_capture_stack, array()); // // render compiled template // $_template->properties['unifunc']($_template); // any unclosed {capture} tags ? if (isset($_template->_capture_stack[0][0])) { $_template->capture_error(); } array_shift($_template->_capture_stack); } catch (Exception $e) { ob_get_clean(); throw $e; } } } else { if ($_template->source->uncompiled) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } try { ob_start(); $_template->source->renderUncompiled($_template); } catch (Exception $e) { ob_get_clean(); throw $e; } } else { throw new SmartyException("Resource '{$_template->source}->type' must have 'renderUncompiled' method"); } } $_output = ob_get_clean(); if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) { $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type); } if ($_template->parent instanceof Smarty_Internal_Template) { $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']); foreach ($_template->required_plugins as $code => $tmp1) { foreach ($tmp1 as $name => $tmp) { foreach ($tmp as $type => $data) { $_template->parent->required_plugins[$code][$name][$type] = $data; } } } } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_render($_template); } // write to cache when nessecary if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_cache($_template); } $_template->properties['has_nocache_code'] = false; // get text between non-cached items $cache_split = preg_split("!/\\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!s", $_output); // get non-cached items preg_match_all("!/\\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!s", $_output, $cache_parts); $output = ''; // loop over items, stitch back together foreach ($cache_split as $curr_idx => $curr_split) { // escape PHP tags in template content $output .= preg_replace('/(<%|%>|<\\?php|<\\?|\\?>)/', '<?php echo \'$1\'; ?>', $curr_split); if (isset($cache_parts[0][$curr_idx])) { $_template->properties['has_nocache_code'] = true; // remove nocache tags from cache output $output .= preg_replace("!/\\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!", '', $cache_parts[0][$curr_idx]); } } if (!$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template); } // rendering (must be done before writing cache file because of {function} nocache handling) $_smarty_tpl = $_template; try { ob_start(); eval("?>" . $output); $_output = ob_get_clean(); } catch (Exception $e) { ob_get_clean(); throw $e; } // write cache file content $_template->writeCachedContent($output); if ($this->smarty->debugging) { Smarty_Internal_Debug::end_cache($_template); } } else { // var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output); if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) { // replace nocache_hash $_output = preg_replace("/{$_template->properties['nocache_hash']}/", $_template->parent->properties['nocache_hash'], $_output); $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code; } } } else { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_cache($_template); } try { ob_start(); array_unshift($_template->_capture_stack, array()); // // render cached template // $_template->properties['unifunc']($_template); // any unclosed {capture} tags ? if (isset($_template->_capture_stack[0][0])) { $_template->capture_error(); } array_shift($_template->_capture_stack); $_output = ob_get_clean(); } catch (Exception $e) { ob_get_clean(); throw $e; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_cache($_template); } } if ((!$this->caching || $_template->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template); } if (isset($this->error_reporting)) { error_reporting($_smarty_old_error_level); } // display or fetch if ($display) { if ($this->caching && $this->cache_modified_check) { $_isCached = $_template->isCached() && !$_template->has_nocache_code; $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3); if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) { switch (PHP_SAPI) { case 'cgi': // php-cgi < 5.3 // php-cgi < 5.3 case 'cgi-fcgi': // php-cgi >= 5.3 // php-cgi >= 5.3 case 'fpm-fcgi': // php-fpm >= 5.3.3 header('Status: 304 Not Modified'); break; case 'cli': if (!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified'; } break; default: header('HTTP/1.1 304 Not Modified'); break; } } else { switch (PHP_SAPI) { case 'cli': if (!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT'; } break; default: header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT'); break; } echo $_output; } } else { echo $_output; } // debug output if ($this->smarty->debugging) { Smarty_Internal_Debug::display_debug($this); } if ($merge_tpl_vars) { // restore local variables $_template->tpl_vars = $save_tpl_vars; $_template->config_vars = $save_config_vars; } return; } else { if ($merge_tpl_vars) { // restore local variables $_template->tpl_vars = $save_tpl_vars; $_template->config_vars = $save_config_vars; } // return fetched content return $_output; } }
/** * restore file and line offset */ public function popTrace() { if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this->template); } $r = array_pop($this->trace_stack); $this->smarty->_current_file = $r[0]; $this->trace_filepath = $r[1]; $this->trace_uid = $r[2]; $this->trace_line_offset = $r[3]; if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this->template); } }
/** * Method to compile Smarty config source. * * @param Smarty_Internal_Template $template * * @return bool true if compiling succeeded, false if it failed */ public function compileTemplate(Smarty_Internal_Template $template) { $this->template = $template; $this->template->properties['file_dependency'][$this->template->source->uid] = [$this->template->source->name, $this->template->source->timestamp, $this->template->source->type]; // on empty config just return if ($template->source->content == '') { return true; } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this->template); } // init the lexer/parser to compile the config file $lex = new $this->lexer_class(str_replace(["\r\n", "\r"], "\n", $template->source->content) . "\n", $this); $parser = new $this->parser_class($lex, $this); if (function_exists('mb_internal_encoding') && (int) ini_get('mbstring.func_overload') & 2) { $mbEncoding = mb_internal_encoding(); mb_internal_encoding('ASCII'); } else { $mbEncoding = null; } if ($this->smarty->_parserdebug) { $parser->PrintTrace(); } // get tokens from lexer and parse them while ($lex->yylex()) { if ($this->smarty->_parserdebug) { echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n"; } $parser->doParse($lex->token, $lex->value); } // finish parsing process $parser->doParse(0, 0); if ($mbEncoding) { mb_internal_encoding($mbEncoding); } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this->template); } // template header code $template_header = "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n"; $template_header .= " compiled from \"" . $this->template->source->filepath . "\" */ ?>\n"; $code = '<?php Smarty_Internal_Extension_Config::loadConfigVars($_smarty_tpl, ' . var_export($this->config_data, true) . '); ?>'; return $template_header . Smarty_Internal_Extension_CodeFrame::create($this->template, $code); }
/** * push current file and line offset on stack for tracing {block} source lines * @param string $file new filename * @param string $uid uid of file * @param int $line line offset to source * @param bool $debug false debug end_compile shall not be called */ public function pushTrace($file, $uid, $line, $debug = TRUE) { if ($this->smarty->debugging && $debug) { Smarty_Internal_Debug::end_compile($this->template); } array_push($this->trace_stack, [$this->smarty->_current_file, $this->trace_filepath, $this->trace_uid, $this->trace_line_offset]); $this->trace_filepath = $this->smarty->_current_file = $file; $this->trace_uid = $uid; $this->trace_line_offset = $line; if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this->template); } }
public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false) { if ($template === null && $this instanceof $this->template_class) { $template = $this; } if (!empty($cache_id) && is_object($cache_id)) { $parent = $cache_id; $cache_id = null; } if ($parent === null && ($this instanceof Smarty || is_string($template))) { $parent = $this; } $_template = $template instanceof $this->template_class ? $template : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false); if ($this instanceof Smarty) { $_template->caching = $this->caching; } if ($merge_tpl_vars) { $save_tpl_vars = $_template->tpl_vars; $save_config_vars = $_template->config_vars; $ptr_array = array($_template); $ptr = $_template; while (isset($ptr->parent)) { $ptr_array[] = $ptr = $ptr->parent; } $ptr_array = array_reverse($ptr_array); $parent_ptr = reset($ptr_array); $tpl_vars = $parent_ptr->tpl_vars; $config_vars = $parent_ptr->config_vars; while ($parent_ptr = next($ptr_array)) { if (!empty($parent_ptr->tpl_vars)) { $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars); } if (!empty($parent_ptr->config_vars)) { $config_vars = array_merge($config_vars, $parent_ptr->config_vars); } } if (!empty(Smarty::$global_tpl_vars)) { $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars); } $_template->tpl_vars = $tpl_vars; $_template->config_vars = $config_vars; } if (!isset($_template->tpl_vars['smarty'])) { $_template->tpl_vars['smarty'] = new Smarty_Variable(); } if (isset($this->smarty->error_reporting)) { $_smarty_old_error_level = error_reporting($this->smarty->error_reporting); } if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') { if (isset($_SERVER['QUERY_STRING'])) { $_query_string = $_SERVER['QUERY_STRING']; } else { $_query_string = ''; } if (false !== strpos($_query_string, $this->smarty->smarty_debug_id)) { if (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=on')) { setcookie('SMARTY_DEBUG', true); $this->smarty->debugging = true; } elseif (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=off')) { setcookie('SMARTY_DEBUG', false); $this->smarty->debugging = false; } else { $this->smarty->debugging = true; } } else { if (isset($_COOKIE['SMARTY_DEBUG'])) { $this->smarty->debugging = true; } } } $_template->smarty->merged_templates_func = array(); if ($_template->source->recompiled) { $_template->caching = false; } if (!$_template->source->exists) { if ($_template->parent instanceof Smarty_Internal_Template) { $parent_resource = " in '{$_template->parent->template_resource}'"; } else { $parent_resource = ''; } throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}"); } if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) { if (!$_template->source->uncompiled) { $_smarty_tpl = $_template; if ($_template->source->recompiled) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($_template); } $code = $_template->compiler->compileTemplate($_template); if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($_template); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } try { ob_start(); eval("?>" . $code); unset($code); } catch (Exception $e) { ob_get_clean(); throw $e; } } else { if (!$_template->compiled->exists || $_template->smarty->force_compile && !$_template->compiled->isCompiled) { $_template->compileTemplateSource(); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } if (!$_template->compiled->loaded) { include $_template->compiled->filepath; if ($_template->mustCompile) { $_template->compileTemplateSource(); include $_template->compiled->filepath; } $_template->compiled->loaded = true; } else { $_template->decodeProperties($_template->compiled->_properties, false); } try { ob_start(); if (empty($_template->properties['unifunc']) || !is_callable($_template->properties['unifunc'])) { throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'"); } array_unshift($_template->_capture_stack, array()); $_template->properties['unifunc']($_template); if (isset($_template->_capture_stack[0][0])) { $_template->capture_error(); } array_shift($_template->_capture_stack); } catch (Exception $e) { ob_get_clean(); throw $e; } } } else { if ($_template->source->uncompiled) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_render($_template); } try { ob_start(); $_template->source->renderUncompiled($_template); } catch (Exception $e) { ob_get_clean(); throw $e; } } else { throw new SmartyException("Resource '{$_template->source}->type' must have 'renderUncompiled' method"); } } $_output = ob_get_clean(); if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) { $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type); } if ($_template->parent instanceof Smarty_Internal_Template) { $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']); foreach ($_template->required_plugins as $code => $tmp1) { foreach ($tmp1 as $name => $tmp) { foreach ($tmp as $type => $data) { $_template->parent->required_plugins[$code][$name][$type] = $data; } } } } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_render($_template); } if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_cache($_template); } $_template->properties['has_nocache_code'] = false; $cache_split = preg_split("!/\\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!s", $_output); preg_match_all("!/\\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!s", $_output, $cache_parts); $output = ''; foreach ($cache_split as $curr_idx => $curr_split) { $output .= preg_replace('/(<%|%>|<\\?php|<\\?|\\?>)/', '<?php echo \'$1\'; ?>', $curr_split); if (isset($cache_parts[0][$curr_idx])) { $_template->properties['has_nocache_code'] = true; $output .= preg_replace("!/\\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\\*/!", '', $cache_parts[0][$curr_idx]); } } if (!$no_output_filter && !$_template->has_nocache_code && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template); } $_smarty_tpl = $_template; try { ob_start(); eval("?>" . $output); $_output = ob_get_clean(); } catch (Exception $e) { ob_get_clean(); throw $e; } $_template->writeCachedContent($output); if ($this->smarty->debugging) { Smarty_Internal_Debug::end_cache($_template); } } else { if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) { $_output = str_replace("{$_template->properties['nocache_hash']}", $_template->parent->properties['nocache_hash'], $_output); $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code; } } } else { if ($this->smarty->debugging) { Smarty_Internal_Debug::start_cache($_template); } try { ob_start(); array_unshift($_template->_capture_stack, array()); $_template->properties['unifunc']($_template); if (isset($_template->_capture_stack[0][0])) { $_template->capture_error(); } array_shift($_template->_capture_stack); $_output = ob_get_clean(); } catch (Exception $e) { ob_get_clean(); throw $e; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_cache($_template); } } if ((!$this->caching || $_template->has_nocache_code || $_template->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template); } if (isset($this->error_reporting)) { error_reporting($_smarty_old_error_level); } if ($display) { if ($this->caching && $this->cache_modified_check) { $_isCached = $_template->isCached() && !$_template->has_nocache_code; $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3); if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) { switch (PHP_SAPI) { case 'cgi': case 'cgi-fcgi': case 'fpm-fcgi': header('Status: 304 Not Modified'); break; case 'cli': if (!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified'; } break; default: header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); break; } } else { switch (PHP_SAPI) { case 'cli': if (!empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT'; } break; default: header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT'); break; } echo $_output; } } else { echo $_output; } if ($this->smarty->debugging) { Smarty_Internal_Debug::display_debug($this); } if ($merge_tpl_vars) { $_template->tpl_vars = $save_tpl_vars; $_template->config_vars = $save_config_vars; } return; } else { if ($merge_tpl_vars) { $_template->tpl_vars = $save_tpl_vars; $_template->config_vars = $save_config_vars; } return $_output; } }
/** * Compiles the template * * If the template is not evaluated the compiled template is saved on disk */ public function compileTemplateSource() { if (!$this->resource_object->isEvaluated) { $this->properties['file_dependency'] = array(); $this->properties['file_dependency'][$this->templateUid] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp()); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this); } // compile template if (!is_object($this->compiler_object)) { // load compiler $this->smarty->loadPlugin($this->resource_object->compiler_class); $this->compiler_object = new $this->resource_object->compiler_class($this->resource_object->template_lexer_class, $this->resource_object->template_parser_class, $this->smarty); } // call compiler if ($this->compiler_object->compileTemplate($this)) { // compiling succeded if (!$this->resource_object->isEvaluated) { // write compiled template Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty); } } else { // error compiling template throw new Exception("Error compiling template {$this->getTemplateFilepath()}"); return false; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this); } }
/** * Method to compile Smarty config source. * * @param Smarty_Internal_Template $template * * @return bool true if compiling succeeded, false if it failed */ public function compileTemplate(Smarty_Internal_Template $template) { $this->template = $template; // on empty config just return if ($template->source->content == '') { return true; } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this->template); } // init the lexer/parser to compile the config file $lex = new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $template->source->content) . "\n", $this); $parser = new $this->parser_class($lex, $this); if (function_exists('mb_internal_encoding') && (int) ini_get('mbstring.func_overload') & 2) { $mbEncoding = mb_internal_encoding(); mb_internal_encoding('ASCII'); } else { $mbEncoding = null; } if ($this->smarty->_parserdebug) { $parser->PrintTrace(); } // get tokens from lexer and parse them while ($lex->yylex()) { if ($this->smarty->_parserdebug) { echo "<br>Parsing {$parser->yyTokenName[$lex->token]} Token {$lex->value} Line {$lex->line} \n"; } $parser->doParse($lex->token, $lex->value); } // finish parsing process $parser->doParse(0, 0); if ($mbEncoding) { mb_internal_encoding($mbEncoding); } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this->template); } $code = 'Smarty_Internal_Extension_Config::loadConfigVars($_smarty_tpl, ' . var_export($this->config_data, true) . ');' . "\n"; return Smarty_Internal_Extension_CodeFrame::create($this->template, array(), $code); }
/** * Compiles the template * * If the template is not evaluated the compiled template is saved on disk */ public function compileTemplateSource() { if (!$this->isEvaluated) { $this->properties['file_dependency']['F' . abs(crc32($this->getTemplateFilepath()))] = array($this->getTemplateFilepath(), $this->getTemplateTimestamp()); } if ($this->smarty->debugging) { Smarty_Internal_Debug::start_compile($this); } // compile template if (!is_object($this->compiler_object)) { // load compiler $this->smarty->loadPlugin($this->resource_object->compiler_class); $this->compiler_object = new $this->resource_object->compiler_class($this->resource_object->template_lexer_class, $this->resource_object->template_parser_class, $this->smarty); // load cacher if ($this->caching) { $this->smarty->loadPlugin($this->cacher_class); $this->cacher_object = new $this->cacher_class($this->smarty); } } // call compiler if ($this->compiler_object->compileTemplate($this)) { // compiling succeded if (!$this->isEvaluated()) { // write compiled template Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty); // make template and compiled file timestamp match /** $this->compiled_timestamp = null; touch($this->getCompiledFilepath(), $this->getTemplateTimestamp()); // daylight saving time problem on windows if ($this->template_timestamp != $this->getCompiledTimestamp()) { touch($this->getCompiledFilepath(), 2 * $this->template_timestamp - $this->compiled_timestamp); } **/ } } else { // error compiling template throw new Exception("Error compiling template {$this->getTemplateFilepath()}"); return false; } if ($this->smarty->debugging) { Smarty_Internal_Debug::end_compile($this); } }