/** * fetches a rendered hiweb_tpl 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 hiweb_tpl 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 * * @throws Exception * @throws SmurtyException * @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 ($cache_id !== null && is_object($cache_id)) { $parent = $cache_id; $cache_id = null; } if ($parent === null && ($this instanceof hiweb_tpl || is_string($template))) { $parent = $this; } // create template object if necessary $_template = $template instanceof $this->template_class ? $template : $this->smurty->createTemplate($template, $cache_id, $compile_id, $parent, false); // if called by hiweb_tpl object make sure we use current caching status if ($this instanceof hiweb_tpl) { $_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(hiweb_tpl::$global_tpl_vars)) { $tpl_vars = array_merge(hiweb_tpl::$global_tpl_vars, $tpl_vars); } $_template->tpl_vars = $tpl_vars; $_template->config_vars = $config_vars; } // dummy local smurty variable if (!isset($_template->tpl_vars['smurty'])) { $_template->tpl_vars['smurty'] = new Smurty_Variable(); } if (isset($this->smurty->error_reporting)) { $_smurty_old_error_level = error_reporting($this->smurty->error_reporting); } // check URL debugging control if (!$this->smurty->debugging && $this->smurty->debugging_ctrl == 'URL') { if (isset($_SERVER['QUERY_STRING'])) { $_query_string = $_SERVER['QUERY_STRING']; } else { $_query_string = ''; } if (false !== strpos($_query_string, $this->smurty->smurty_debug_id)) { if (false !== strpos($_query_string, $this->smurty->smurty_debug_id . '=on')) { // enable debugging for this browser session setcookie('SMURTY_DEBUG', true); $this->smurty->debugging = true; } elseif (false !== strpos($_query_string, $this->smurty->smurty_debug_id . '=off')) { // disable debugging for this browser session setcookie('SMURTY_DEBUG', false); $this->smurty->debugging = false; } else { // enable debugging for this page $this->smurty->debugging = true; } } else { if (isset($_COOKIE['SMURTY_DEBUG'])) { $this->smurty->debugging = true; } } } // must reset merge template date $_template->smurty->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 Smurty_Internal_Template) { $parent_resource = " in '{$_template->parent->template_resource}'"; } else { $parent_resource = ''; } throw new SmurtyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}"); } // read from cache or render if (!($_template->caching == hiweb_tpl::CACHING_LIFETIME_CURRENT || $_template->caching == hiweb_tpl::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) { // render template (not loaded and not in cache) if (!$_template->source->uncompiled) { /** @var Smurty_Internal_Template $_smurty_tpl * used in evaluated code */ $_smurty_tpl = $_template; if ($_template->source->recompiled) { $code = $_template->compiler->compileTemplate($_template); if ($this->smurty->debugging) { Smurty_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->smurty->force_compile && !$_template->compiled->isCompiled) { $_template->compileTemplateSource(); $code = file_get_contents($_template->compiled->filepath); eval("?>" . $code); unset($code); $_template->compiled->loaded = true; $_template->compiled->isCompiled = true; } if ($this->smurty->debugging) { Smurty_Internal_Debug::start_render($_template); } if (!$_template->compiled->loaded) { include $_template->compiled->filepath; if ($_template->mustCompile) { // recompile and load again $_template->compileTemplateSource(); $code = file_get_contents($_template->compiled->filepath); eval("?>" . $code); unset($code); $_template->compiled->isCompiled = true; } $_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 SmurtyException("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->smurty->debugging) { Smurty_Internal_Debug::start_render($_template); } try { ob_start(); $_template->source->renderUncompiled($_template); } catch (Exception $e) { ob_get_clean(); throw $e; } } else { throw new SmurtyException("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 Smurty_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->smurty->debugging) { Smurty_Internal_Debug::end_render($_template); } // write to cache when nessecary if (!$_template->source->recompiled && ($_template->caching == hiweb_tpl::CACHING_LIFETIME_SAVED || $_template->caching == hiweb_tpl::CACHING_LIFETIME_CURRENT)) { if ($this->smurty->debugging) { Smurty_Internal_Debug::start_cache($_template); } $_template->properties['has_nocache_code'] = false; // get text between non-cached items $cache_split = preg_split("!/\\*%%SmurtyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmurtyNocache:{$_template->properties['nocache_hash']}%%\\*/!s", $_output); // get non-cached items preg_match_all("!/\\*%%SmurtyNocache:{$_template->properties['nocache_hash']}%%\\*\\/(.+?)/\\*/%%SmurtyNocache:{$_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'; ?>\n", $curr_split); if (isset($cache_parts[0][$curr_idx])) { $_template->properties['has_nocache_code'] = true; // remove nocache tags from cache output $output .= preg_replace("!/\\*/?%%SmurtyNocache:{$_template->properties['nocache_hash']}%%\\*/!", '', $cache_parts[0][$curr_idx]); } } if (!$no_output_filter && !$_template->has_nocache_code && (isset($this->smurty->autoload_filters['output']) || isset($this->smurty->registered_filters['output']))) { $output = Smurty_Internal_Filter_Handler::runFilter('output', $output, $_template); } // rendering (must be done before writing cache file because of {function} nocache handling) /** @var Smurty_Internal_Template $_smurty_tpl * used in evaluated code */ $_smurty_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->smurty->debugging) { Smurty_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 = 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->smurty->debugging) { Smurty_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->smurty->debugging) { Smurty_Internal_Debug::end_cache($_template); } } if ((!$this->caching || $_template->has_nocache_code || $_template->source->recompiled) && !$no_output_filter && (isset($this->smurty->autoload_filters['output']) || isset($this->smurty->registered_filters['output']))) { $_output = Smurty_Internal_Filter_Handler::runFilter('output', $_output, $_template); } if (isset($this->error_reporting)) { error_reporting($_smurty_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['SMURTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMURTY_PHPUNIT_HEADERS'][] = '304 Not Modified'; } break; default: header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); break; } } else { switch (PHP_SAPI) { case 'cli': if (!empty($_SERVER['SMURTY_PHPUNIT_DISABLE_HEADERS'])) { $_SERVER['SMURTY_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->smurty->debugging) { Smurty_Internal_Debug::display_debug($_template); } 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; } }
/** * Method to compile a hiweb_tpl template * * @param Smurty_Internal_Template $template template object to compile * @param bool $nocache true is shall be compiled in nocache mode * * @return bool true if compiling succeeded, false if it failed */ public function compileTemplate(Smurty_Internal_Template $template, $nocache = false) { if (empty($template->properties['nocache_hash'])) { $template->properties['nocache_hash'] = $this->nocache_hash; } else { $this->nocache_hash = $template->properties['nocache_hash']; } // flag for nochache sections $this->nocache = $nocache; $this->tag_nocache = false; // save template object in compiler class $this->template = $template; // reset has nocache code flag $this->template->has_nocache_code = false; $save_source = $this->template->source; // template header code $template_header = ''; if (!$this->suppressHeader) { $template_header .= "<?php /* hiweb_tpl version " . hiweb_tpl::HIWEB_TPL_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->smurty->_current_file = $this->template->source->filepath; if ($this->smurty->debugging) { Smurty_Internal_Debug::start_compile($this->template); } $no_sources = count($this->sources); if ($loop || $no_sources) { $this->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->timestamp, $this->template->source->type); } $loop++; if ($no_sources) { $this->inheritance_child = true; } else { $this->inheritance_child = false; } do { $_compiled_code = ''; // flag for aborting current and start recompile $this->abort_and_recompile = false; // get template source $_content = $this->template->source->content; if ($_content != '') { // run prefilter if required if ((isset($this->smurty->autoload_filters['pre']) || isset($this->smurty->registered_filters['pre'])) && !$this->suppressFilter) { $_content = Smurty_Internal_Filter_Handler::runFilter('pre', $_content, $template); } // call compiler $_compiled_code = $this->doCompile($_content); } } while ($this->abort_and_recompile); if ($this->smurty->debugging) { Smurty_Internal_Debug::end_compile($this->template); } } // restore source $this->template->source = $save_source; unset($save_source); $this->smurty->_current_file = $this->template->source->filepath; // free memory unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex, $this->template); self::$_tag_objects = array(); // return compiled code to template object $merged_code = ''; if (!$this->suppressMergedTemplates && !empty($this->merged_templates)) { foreach ($this->merged_templates as $code) { $merged_code .= $code; } } // run postfilter if required on compiled template code if ((isset($this->smurty->autoload_filters['post']) || isset($this->smurty->registered_filters['post'])) && !$this->suppressFilter && $_compiled_code != '') { $_compiled_code = Smurty_Internal_Filter_Handler::runFilter('post', $_compiled_code, $template); } if ($this->suppressTemplatePropertyHeader) { $code = $_compiled_code . $merged_code; } else { $code = $template_header . $template->createTemplateCodeFrame($_compiled_code) . $merged_code; } // unset content because template inheritance could have replace source with parent code unset($template->source->content); return $code; }