/**
  * Compiles code for the {include} tag
  *
  * @param  array                                  $args      array with attributes from parser
  * @param  Smarty_Internal_SmartyTemplateCompiler $compiler  compiler object
  * @param  array                                  $parameter array with compilation parameter
  *
  * @throws SmartyCompilerException
  * @return string compiled code
  */
 public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter)
 {
     // check and get attributes
     $_attr = $this->getAttributes($compiler, $args);
     // save possible attributes
     $include_file = $_attr['file'];
     if ($compiler->has_variable_string || !(substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2) || substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0) {
         $variable_template = true;
     } else {
         $variable_template = false;
     }
     if (isset($_attr['assign'])) {
         // output will be stored in a smarty variable instead of being displayed
         $_assign = $_attr['assign'];
     }
     $_parent_scope = Smarty::SCOPE_LOCAL;
     if (isset($_attr['scope'])) {
         $_attr['scope'] = trim($_attr['scope'], "'\"");
         if ($_attr['scope'] == 'parent') {
             $_parent_scope = Smarty::SCOPE_PARENT;
         } elseif ($_attr['scope'] == 'root') {
             $_parent_scope = Smarty::SCOPE_ROOT;
         } elseif ($_attr['scope'] == 'global') {
             $_parent_scope = Smarty::SCOPE_GLOBAL;
         }
     }
     //
     if ($variable_template || $compiler->loopNesting > 0) {
         $_cache_tpl = 'true';
     } else {
         $_cache_tpl = 'false';
     }
     // assume caching is off
     $_caching = Smarty::CACHING_OFF;
     if ($_attr['nocache'] === true) {
         $compiler->tag_nocache = true;
     }
     $call_nocache = $compiler->tag_nocache || $compiler->nocache;
     // caching was on and {include} is not in nocache mode
     if ($compiler->template->caching && !$compiler->nocache && !$compiler->tag_nocache) {
         $_caching = self::CACHING_NOCACHE_CODE;
     }
     // flag if included template code should be merged into caller
     $merge_compiled_includes = ($compiler->smarty->merge_compiled_includes || $compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes || $_attr['inline'] === true) && !$compiler->template->source->handler->recompiled;
     if ($merge_compiled_includes && $_attr['inline'] !== true) {
         // variable template name ?
         if ($variable_template) {
             $merge_compiled_includes = false;
             if ($compiler->template->caching) {
                 // must use individual cache file
                 //$_attr['caching'] = 1;
             }
             if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes && $_attr['inline'] !== true) {
                 $compiler->trigger_template_error(' variable template file names not allow within {block} tags');
             }
         }
         // variable compile_id?
         if (isset($_attr['compile_id'])) {
             if (!(substr_count($_attr['compile_id'], '"') == 2 || substr_count($_attr['compile_id'], "'") == 2 || is_numeric($_attr['compile_id'])) || substr_count($_attr['compile_id'], '(') != 0 || substr_count($_attr['compile_id'], '$_smarty_tpl->') != 0) {
                 $merge_compiled_includes = false;
                 if ($compiler->template->caching) {
                     // must use individual cache file
                     //$_attr['caching'] = 1;
                 }
                 if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes && $_attr['inline'] !== true) {
                     $compiler->trigger_template_error(' variable compile_id not allow within {block} tags');
                 }
             }
         }
     }
     /*
      * if the {include} tag provides individual parameter for caching or compile_id
      * the subtemplate must not be included into the common cache file and is treated like
      * a call in nocache mode.
      *
      */
     if ($_attr['nocache'] !== true && $_attr['caching']) {
         $_caching = $_new_caching = (int) $_attr['caching'];
         $call_nocache = true;
     } else {
         $_new_caching = Smarty::CACHING_LIFETIME_CURRENT;
     }
     if (isset($_attr['cache_lifetime'])) {
         $_cache_lifetime = $_attr['cache_lifetime'];
         $call_nocache = true;
         $_caching = $_new_caching;
     } else {
         $_cache_lifetime = '$_smarty_tpl->cache_lifetime';
     }
     if (isset($_attr['cache_id'])) {
         $_cache_id = $_attr['cache_id'];
         $call_nocache = true;
         $_caching = $_new_caching;
     } else {
         $_cache_id = '$_smarty_tpl->cache_id';
     }
     if (isset($_attr['compile_id'])) {
         $_compile_id = $_attr['compile_id'];
     } else {
         $_compile_id = '$_smarty_tpl->compile_id';
     }
     // if subtemplate will be called in nocache mode do not merge
     if ($compiler->template->caching && $call_nocache) {
         $merge_compiled_includes = false;
     }
     $has_compiled_template = false;
     if ($merge_compiled_includes) {
         if ($compiler->template->caching && ($compiler->tag_nocache || $compiler->nocache) && $_caching != self::CACHING_NOCACHE_CODE) {
             //                $merge_compiled_includes = false;
             if ($compiler->inheritance && $compiler->smarty->inheritance_merge_compiled_includes) {
                 $compiler->trigger_template_error(' invalid caching mode of subtemplate within {block} tags');
             }
         }
         $c_id = isset($_attr['compile_id']) ? $_attr['compile_id'] : $compiler->template->compile_id;
         // we must observe different compile_id and caching
         $uid = sha1($c_id . ($_caching ? '--caching' : '--nocaching'));
         $tpl_name = null;
         /** @var Smarty_Internal_Template $_smarty_tpl
          * used in evaluated code
          */
         $_smarty_tpl = $compiler->template;
         eval("\$tpl_name = @{$include_file};");
         if (!isset($compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid])) {
             $compiler->smarty->allow_ambiguous_resources = true;
             $tpl = new $compiler->smarty->template_class($tpl_name, $compiler->smarty, $compiler->template, $compiler->template->cache_id, $c_id, $_caching);
             if (!$tpl->source->handler->uncompiled && $tpl->source->exists) {
                 $tpl->compiled = new Smarty_Template_Compiled();
                 $tpl->compiled->nocache_hash = $compiler->parent_compiler->template->compiled->nocache_hash;
                 $tpl->loadCompiler();
                 // save unique function name
                 $compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func'] = $tpl->compiled->unifunc = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
                 if ($compiler->inheritance) {
                     $tpl->compiler->inheritance = true;
                 }
                 // make sure whole chain gets compiled
                 $tpl->mustCompile = true;
                 $tpl->compiler->suppressTemplatePropertyHeader = true;
                 $compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['nocache_hash'] = $tpl->compiled->nocache_hash;
                 // get compiled code
                 $compiled_code = Smarty_Internal_Extension_CodeFrame::createFunctionFrame($tpl, $tpl->compiler->compileTemplate($tpl, null, $compiler->parent_compiler));
                 unset($tpl->compiler);
                 // remove header code
                 $compiled_code = preg_replace("/(<\\?php \\/\\*%%SmartyHeaderCode:{$tpl->compiled->nocache_hash}%%\\*\\/(.+?)\\/\\*\\/%%SmartyHeaderCode%%\\*\\/\\?>\n)/s", '', $compiled_code);
                 if ($tpl->compiled->has_nocache_code) {
                     // replace nocache_hash
                     $compiled_code = str_replace("{$tpl->compiled->nocache_hash}", $compiler->template->compiled->nocache_hash, $compiled_code);
                     $compiler->template->compiled->has_nocache_code = true;
                 }
                 $compiler->parent_compiler->mergedSubTemplatesCode[$tpl->compiled->unifunc] = $compiled_code;
                 $has_compiled_template = true;
                 unset($tpl);
             }
         } else {
             $has_compiled_template = true;
         }
     }
     // delete {include} standard attributes
     unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['compile_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
     // remaining attributes must be assigned as smarty variable
     $_vars_nc = '';
     if (!empty($_attr)) {
         if ($_parent_scope == Smarty::SCOPE_LOCAL) {
             $_pairs = array();
             // create variables
             foreach ($_attr as $key => $value) {
                 $_pairs[] = "'{$key}'=>{$value}";
                 $_vars_nc .= "\$_smarty_tpl->tpl_vars['{$key}'] =  new Smarty_Variable({$value});\n";
             }
             $_vars = 'array(' . join(',', $_pairs) . ')';
         } else {
             $compiler->trigger_template_error('variable passing not allowed in parent/global scope', null, true);
         }
     } else {
         $_vars = 'array()';
     }
     $this->logInclude($compiler, $include_file, $variable_template);
     $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache && $_compile_id != '$_smarty_tpl->compile_id';
     if ($has_compiled_template && !$call_nocache) {
         $_output = "<?php /*  Call merged included template \"" . $tpl_name . "\" */\n";
         if ($update_compile_id) {
             $_output .= $compiler->makeNocacheCode("\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n");
         }
         if (!empty($_vars_nc) && $_caching == 9999 && $compiler->template->caching) {
             //$compiler->suppressNocacheProcessing = false;
             $_output .= substr($compiler->processNocacheCode('<?php ' . $_vars_nc . "?>\n", true), 6, -3);
             //$compiler->suppressNocacheProcessing = true;
         }
         if (isset($_assign)) {
             $_output .= "ob_start();\n";
             $_output .= "\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}');\n";
             $_output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n";
         } else {
             $_output .= "\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}');\n";
         }
         if ($update_compile_id) {
             $_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n");
         }
         $_output .= "/*  End of included template \"" . $tpl_name . "\" */?>\n";
         return $_output;
     }
     if ($call_nocache) {
         $compiler->tag_nocache = true;
     }
     $_output = "<?php ";
     if ($update_compile_id) {
         $_output .= "\$_compile_id_save[] = \$_smarty_tpl->compile_id;\n\$_smarty_tpl->compile_id = {$_compile_id};\n";
     }
     // was there an assign attribute
     if (isset($_assign)) {
         $_output .= "ob_start();\n";
         $_output .= "\$_smarty_tpl->getSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl});\n";
         $_output .= "\$_smarty_tpl->tpl_vars[{$_assign}] = new Smarty_Variable(ob_get_clean());\n";
     } else {
         $_output .= "\$_smarty_tpl->getSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl});\n";
     }
     if ($update_compile_id) {
         $_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n";
     }
     $_output .= "?>\n";
     return $_output;
 }
 /**
  * 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->compiled->file_dependency[$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->getTimeStamp(), $this->template->source->type);
     if ($this->smarty->debugging) {
         $this->smarty->_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->getContent()) . "\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) {
         $this->smarty->_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_Method_ConfigLoad::_loadConfigVars($_smarty_tpl, ' . var_export($this->config_data, true) . '); ?>';
     return $template_header . Smarty_Internal_Extension_CodeFrame::create($this->template, $code);
 }
 /**
  * Method to compile a Smarty template
  *
  * @param  Smarty_Internal_Template                 $template template object to compile
  * @param  bool                                     $nocache  true is shall be compiled in nocache mode
  * @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
  *
  * @return bool true if compiling succeeded, false if it failed
  */
 public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, $parent_compiler = null)
 {
     // save template object in compiler class
     $this->template = $template;
     if (isset($this->template->smarty->security_policy)) {
         $this->php_handling = $this->template->smarty->security_policy->php_handling;
     } else {
         $this->php_handling = $this->template->smarty->php_handling;
     }
     $this->parent_compiler = $parent_compiler ? $parent_compiler : $this;
     $nocache = isset($nocache) ? $nocache : false;
     if (empty($template->properties['nocache_hash'])) {
         $template->properties['nocache_hash'] = $this->nocache_hash;
     } else {
         $this->nocache_hash = $template->properties['nocache_hash'];
     }
     $save_source = $this->template->source;
     // template header code
     $template_header = '';
     if (!$this->suppressHeader) {
         $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";
     }
     if (empty($this->template->source->components)) {
         $this->sources = array($template->source);
     } else {
         // we have array of inheritance templates by extends: resource
         $this->sources = array_reverse($template->source->components);
     }
     $loop = 0;
     // the $this->sources array can get additional elements while compiling by the {extends} tag
     while ($this->template->source = array_shift($this->sources)) {
         $this->smarty->_current_file = $this->template->source->filepath;
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::start_compile($this->template);
         }
         $no_sources = count($this->sources);
         $this->parent_compiler->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->getTimeStamp(), $this->template->source->type);
         $loop++;
         if ($no_sources) {
             $this->inheritance_child = true;
         } else {
             $this->inheritance_child = false;
         }
         // flag for nochache sections
         $this->nocache = $nocache;
         $this->tag_nocache = false;
         // reset has nocache code flag
         $this->template->has_nocache_code = false;
         $this->has_variable_string = false;
         $this->prefix_code = array();
         $_compiled_code = '';
         // get template source
         $_content = $this->template->source->getContent();
         if ($_content != '') {
             // run pre filter if required
             if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) {
                 $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
             }
             // call compiler
             $_compiled_code = $this->doCompile($_content, true);
         }
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::end_compile($this->template);
         }
     }
     // restore source
     $this->template->source = $save_source;
     unset($save_source);
     $this->smarty->_current_file = $this->template->source->filepath;
     // free memory
     unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex);
     // return compiled code to template object
     $merged_code = '';
     if (!empty($this->mergedSubTemplatesCode)) {
         foreach ($this->mergedSubTemplatesCode as $code) {
             $merged_code .= $code;
         }
     }
     // run post filter if required on compiled template code
     if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter && $_compiled_code != '') {
         $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template);
     }
     if ($this->suppressTemplatePropertyHeader) {
         $_compiled_code .= $merged_code;
     } else {
         $_compiled_code = $template_header . Smarty_Internal_Extension_CodeFrame::create($template, $_compiled_code) . $merged_code;
     }
     if (!empty($this->templateFunctionCode)) {
         // run post filter if required on compiled template code
         if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter) {
             $_compiled_code .= Smarty_Internal_Filter_Handler::runFilter('post', $this->templateFunctionCode, $template);
         } else {
             $_compiled_code .= $this->templateFunctionCode;
         }
     }
     $this->parent_compiler = null;
     $this->template = null;
     return $_compiled_code;
 }
 /**
  * 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);
 }
Ejemplo n.º 5
0
 /**
  * Writes the content to cache resource
  *
  * @param Smarty_Internal_Template $_template
  * @param string                   $content
  *
  * @return bool
  */
 public function writeCachedContent(Smarty_Internal_Template $_template, $content)
 {
     if ($_template->source->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED)) {
         // don't write cache file
         return false;
     }
     $_template->properties['cache_lifetime'] = $_template->cache_lifetime;
     $_template->properties['unifunc'] = 'content_' . str_replace(['.', ','], '_', uniqid('', true));
     $content = Smarty_Internal_Extension_CodeFrame::create($_template, $content, true);
     if (!empty($_template->properties['tpl_function'])) {
         foreach ($_template->properties['tpl_function'] as $funcParam) {
             if (is_file($funcParam['compiled_filepath'])) {
                 // read compiled file
                 $code = file_get_contents($funcParam['compiled_filepath']);
                 // grab template function
                 if (preg_match("/\\/\\* {$funcParam['call_name']} \\*\\/([\\S\\s]*?)\\/\\*\\/ {$funcParam['call_name']} \\*\\//", $code, $match)) {
                     unset($code);
                     $content .= "<?php " . $match[0] . "?>\n";
                 }
             }
         }
     }
     return $this->write($_template, $content);
 }
Ejemplo n.º 6
0
 /**
  * Writes the content to cache resource
  *
  * @param Smarty_Internal_Template $_template
  * @param string                   $content
  *
  * @return bool
  */
 public function writeCachedContent(Smarty_Internal_Template $_template, $content)
 {
     if ($_template->source->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED)) {
         // don't write cache file
         return false;
     }
     $_template->properties['cache_lifetime'] = $_template->cache_lifetime;
     $_template->properties['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
     $content = Smarty_Internal_Extension_CodeFrame::create($_template, $content, true);
     return $this->write($_template, $content);
 }
 /**
  * Method to compile a Smarty template
  *
  * @param  Smarty_Internal_Template                 $template template object to compile
  * @param  bool                                     $nocache  true is shall be compiled in nocache mode
  * @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
  *
  * @return bool true if compiling succeeded, false if it failed
  * @throws \Exception
  */
 public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, Smarty_Internal_TemplateCompilerBase $parent_compiler = null)
 {
     // get code frame of compiled template
     $_compiled_code = "<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n         compiled from \"" . $template->source->filepath . "\" */ ?>\n" . Smarty_Internal_Extension_CodeFrame::create($template, $this->compileTemplateSource($template, $nocache, $parent_compiler), $this->postFilter($this->blockOrFunctionCode) . join('', $this->mergedSubTemplatesCode));
     return $_compiled_code;
 }
 /**
  * Method to compile a Smarty template
  *
  * @param  Smarty_Internal_Template $template template object to compile
  * @param  bool                     $nocache  true is shall be compiled in nocache mode
  *
  * @param Smarty_Internal_TemplateCompilerBase                    $parent_compiler
  *
  * @return bool true if compiling succeeded, false if it failed
  * @throws \SmartyException
  */
 public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, Smarty_Internal_TemplateCompilerBase $parent_compiler = null)
 {
     $this->parent_compiler = $parent_compiler ? $parent_compiler : $this;
     $nocache = isset($nocache) ? $nocache : false;
     // flag for nochache sections
     $this->nocache = $nocache;
     $this->tag_nocache = false;
     // save template object in compiler class
     $this->template = $template;
     // template header code
     $template_header = '';
     if (!$this->suppressHeader) {
         $template_header .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") . "\n";
         $template_header .= "         compiled from \"" . $this->template->source->filepath . "\" */\n";
     }
     if ($isExtendsResource = !empty($this->template->source->components)) {
         // we have array of inheritance templates by extends: resource
         $this->sources = array_reverse($template->source->components);
     }
     // the $this->sources array can get additional elements while compiling by the {extends} tag
     if (!$isExtendsResource) {
         $this->inheritance_child = $this->template->source->isChild;
         $this->smarty->_current_file = $this->template->source->filepath;
         if ($this->smarty->debugging) {
             Smarty_Internal_Debug::start_compile($this->template);
         }
         if (!($this->template->source->recompiled || $this->template->source->type == 'string' || $this->template->source->type == 'extends')) {
             $this->parent_compiler->resourceInfo[$this->template->source->uid] = $this->template->source->getResourceInfo($this->template);
         }
     }
     do {
         // flag for aborting current and start recompile
         $this->abort_and_recompile = false;
         if ($isExtendsResource) {
             $_content = '';
         } else {
             // get template source
             $_content = $this->template->source->content;
         }
         if ($_content != '') {
             // run prefilter if required
             if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) {
                 $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
             }
         }
         // call compiler
         $_compiled_code = $this->doCompile($_content);
     } while ($this->abort_and_recompile);
     if ($this->smarty->debugging) {
         Smarty_Internal_Debug::end_compile($this->template);
     }
     // free memory
     unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex);
     self::$_tag_objects = array();
     // return compiled code to template object
     // run postfilter if required on compiled template code
     if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter && $_compiled_code != '') {
         $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template);
     }
     // unset content because template inheritance could have replace source with parent code
     unset($template->source->content);
     $this->parent_compiler = null;
     $this->template = null;
     if ($this->suppressTemplatePropertyHeader) {
         return $_compiled_code;
     } else {
         $properties = array('resourceInfo' => $this->resourceInfo, 'isInheritanceChild' => $this->inheritance_child);
         if (isset($this->templateProperties['tpl_function'])) {
             $properties['templateFunctions'] = $this->templateProperties['tpl_function'];
         }
         if (isset($this->templateProperties['block_function'])) {
             $properties['blockFunctions'] = $this->templateProperties['block_function'];
         }
         foreach ($this->plugins as $tmp) {
             foreach ($tmp as $plugin) {
                 $properties['plugins'][$plugin['file']] = $plugin['function'];
             }
         }
         foreach ($this->nocachePlugins as $tmp) {
             foreach ($tmp as $plugin) {
                 $properties['nocachePlugins'][$plugin['file']] = $plugin['function'];
             }
         }
         return Smarty_Internal_Extension_CodeFrame::create($template, $properties, $_compiled_code, $template_header);
     }
 }
 /**
  * Method to compile a Smarty template
  *
  * @param  Smarty_Internal_Template                 $template template object to compile
  * @param  bool                                     $nocache  true is shall be compiled in nocache mode
  * @param null|Smarty_Internal_TemplateCompilerBase $parent_compiler
  *
  * @return bool true if compiling succeeded, false if it failed
  * @throws \Exception
  */
 public function compileTemplate(Smarty_Internal_Template $template, $nocache = null, $parent_compiler = null)
 {
     if (property_exists($template->smarty, 'plugin_search_order')) {
         $this->plugin_search_order = $template->smarty->plugin_search_order;
     }
     // save template object in compiler class
     $this->template = $template;
     $this->savedSource = $this->template->source;
     try {
         if (isset($this->template->smarty->security_policy)) {
             $this->php_handling = $this->template->smarty->security_policy->php_handling;
         } else {
             $this->php_handling = $this->template->smarty->php_handling;
         }
         $this->parent_compiler = $parent_compiler ? $parent_compiler : $this;
         $nocache = isset($nocache) ? $nocache : false;
         if (empty($template->compiled->nocache_hash)) {
             $template->compiled->nocache_hash = $this->nocache_hash;
         } else {
             $this->nocache_hash = $template->compiled->nocache_hash;
         }
         // template header code
         $template_header = '';
         if (!$this->suppressHeader) {
             $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";
         }
         $_compiled_code = '';
         $this->smarty->_current_file = $this->template->source->filepath;
         if ($this->smarty->debugging) {
             $this->smarty->_debug->start_compile($this->template);
         }
         $this->parent_compiler->template->compiled->file_dependency[$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->getTimeStamp(), $this->template->source->type);
         // compile for child template?
         $this->inheritance_child = $this->template->isChild;
         // flag for nochache sections
         $this->nocache = $nocache;
         $this->tag_nocache = false;
         // reset has nocache code flag
         $this->template->compiled->has_nocache_code = false;
         $this->has_variable_string = false;
         $this->prefix_code = array();
         // get template source
         if (!empty($this->template->source->components)) {
             // we have array of inheritance templates by extends: resource
             $this->sources = array_reverse($template->source->components);
             $_content = '';
         } else {
             // get template source
             $_content = $this->template->source->getContent();
         }
         if ($_content != '') {
             // run pre filter if required
             if ($_content != '' && ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter)) {
                 $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
             }
         }
         // call compiler
         $_compiled_code = $this->doCompile($_content, true);
         if ($this->smarty->debugging) {
             $this->smarty->_debug->end_compile($this->template);
         }
         // free memory
         $this->parser = null;
         // restore source
         $this->template->source = $this->savedSource;
         $this->savedSource = null;
         $this->smarty->_current_file = $this->template->source->filepath;
         // return compiled code to template object
         $merged_code = '';
         if (!empty($this->mergedSubTemplatesCode)) {
             foreach ($this->mergedSubTemplatesCode as $code) {
                 $merged_code .= $code;
             }
         }
         // run post filter if required on compiled template code
         if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter && $_compiled_code != '') {
             $_compiled_code = Smarty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template);
         }
         if ($this->suppressTemplatePropertyHeader) {
             $_compiled_code .= $merged_code;
         } else {
             $_compiled_code = $template_header . Smarty_Internal_Extension_CodeFrame::create($template, $_compiled_code) . $merged_code;
         }
         if (!empty($this->templateFunctionCode)) {
             // run post filter if required on compiled template code
             if ((isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) && !$this->suppressFilter) {
                 $_compiled_code .= Smarty_Internal_Filter_Handler::runFilter('post', $this->templateFunctionCode, $template);
             } else {
                 $_compiled_code .= $this->templateFunctionCode;
             }
         }
     } catch (Exception $e) {
         $this->_tag_stack = array();
         $this->_tag_objects = array();
         // restore source
         $this->template->source = $this->savedSource;
         $this->savedSource = null;
         $this->smarty->_current_file = $this->template->source->filepath;
         // free memory
         $this->parent_compiler = null;
         $this->template = null;
         $this->parser = null;
         throw $e;
     }
     $this->parent_compiler = null;
     $this->template = null;
     return $_compiled_code;
 }
Ejemplo n.º 10
0
 /**
  * Writes the content to cache resource
  *
  * @param Smarty_Internal_Template $_template
  * @param \Smarty_Internal_Context $context
  * @param string                   $content
  *
  * @return bool
  */
 public function writeCachedContent(Smarty_Internal_Template $_template, Smarty_Internal_Context $context = null, $content = '')
 {
     if ($_template->source->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED)) {
         // don't write cache file
         return false;
     }
     $properties = array('isCache' => true, 'cacheLifetime' => $_template->cache_lifetime);
     if ($context) {
         $properties['resourceInfo'] = $context->resourceInfo;
         if ($context->hasNocacheCode) {
             $properties['hasNocacheCode'] = true;
         }
         foreach ($context->templateFunctions as $name => $data) {
             unset($data['obj']);
             $properties['templateFunctions'][$name] = $data;
         }
         if (!empty($context->nocachePlugins)) {
             $properties['plugins'] = $context->nocachePlugins;
         }
         $content = !empty($content) ? $content : $context->getContent();
     }
     return $this->write($_template, Smarty_Internal_Extension_CodeFrame::create($_template, $properties, $content, '', true));
 }