2009-03-27 15:31:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-11-17 19:03:06 +00:00
|
|
|
* Smarty Internal Plugin Resource Extends
|
2009-03-27 15:31:47 +00:00
|
|
|
*
|
|
|
|
* Implements the file system as resource for Smarty which does extend a chain of template files templates
|
|
|
|
*
|
|
|
|
* @package Smarty
|
|
|
|
* @subpackage TemplateResources
|
|
|
|
* @author Uwe Tews
|
|
|
|
*/
|
|
|
|
/**
|
2009-11-17 19:03:06 +00:00
|
|
|
* Smarty Internal Plugin Resource Extends
|
2009-03-27 15:31:47 +00:00
|
|
|
*/
|
2009-11-17 19:03:06 +00:00
|
|
|
class Smarty_Internal_Resource_Extends {
|
2009-09-11 18:59:48 +00:00
|
|
|
public function __construct($smarty)
|
|
|
|
{
|
|
|
|
$this->smarty = $smarty;
|
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
// classes used for compiling Smarty templates from file resource
|
|
|
|
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
|
|
|
|
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
|
|
|
|
public $template_parser_class = 'Smarty_Internal_Templateparser';
|
|
|
|
|
2009-09-30 18:28:50 +00:00
|
|
|
/**
|
|
|
|
* Return flag if template source is existing
|
|
|
|
*
|
2009-10-04 18:12:30 +00:00
|
|
|
* @param object $template template object
|
|
|
|
* @return boolean result
|
2009-09-30 18:28:50 +00:00
|
|
|
*/
|
|
|
|
public function isExisting($template)
|
|
|
|
{
|
|
|
|
if ($template->getTemplateFilepath() === false) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
/**
|
|
|
|
* Get filepath to template source
|
|
|
|
*
|
2009-10-04 18:12:30 +00:00
|
|
|
* @param object $template template object
|
2009-03-27 15:31:47 +00:00
|
|
|
* @return string filepath to template source file
|
|
|
|
*/
|
2009-10-04 18:12:30 +00:00
|
|
|
public function getTemplateFilepath($template)
|
2009-03-27 15:31:47 +00:00
|
|
|
{
|
2009-10-04 18:12:30 +00:00
|
|
|
$_files = explode('|', $template->resource_name);
|
|
|
|
$_filepath = $template->buildTemplateFilepath ($_files[count($_files)-1]);
|
2009-09-30 18:28:50 +00:00
|
|
|
if ($_filepath !== false) {
|
2009-10-04 18:12:30 +00:00
|
|
|
if ($template->security) {
|
|
|
|
$template->smarty->security_handler->isTrustedResourceDir($_filepath);
|
2009-09-30 18:28:50 +00:00
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
|
|
|
return $_filepath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get timestamp to template source
|
|
|
|
*
|
2009-10-04 18:12:30 +00:00
|
|
|
* @param object $template template object
|
2009-03-27 15:31:47 +00:00
|
|
|
* @return integer timestamp of template source file
|
|
|
|
*/
|
2009-10-04 18:12:30 +00:00
|
|
|
public function getTemplateTimestamp($template)
|
2009-03-27 15:31:47 +00:00
|
|
|
{
|
2009-10-04 18:12:30 +00:00
|
|
|
return filemtime($template->getTemplateFilepath());
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read template source from file
|
|
|
|
*
|
2009-10-04 18:12:30 +00:00
|
|
|
* @param object $template template object
|
2009-03-27 15:31:47 +00:00
|
|
|
* @return string content of template source file
|
|
|
|
*/
|
2009-10-04 18:12:30 +00:00
|
|
|
public function getTemplateSource($template)
|
2009-03-27 15:31:47 +00:00
|
|
|
{
|
2009-10-04 18:12:30 +00:00
|
|
|
$this->template = $template;
|
2009-12-03 19:49:17 +00:00
|
|
|
$saved_filepath = $template->getTemplateFilepath();
|
2009-10-04 18:12:30 +00:00
|
|
|
$_files = explode('|', $template->resource_name);
|
2009-03-28 08:47:26 +00:00
|
|
|
$_files = array_reverse($_files);
|
2009-03-27 15:31:47 +00:00
|
|
|
foreach ($_files as $_file) {
|
2009-10-04 18:12:30 +00:00
|
|
|
$_filepath = $template->buildTemplateFilepath ($_file);
|
2009-09-30 18:28:50 +00:00
|
|
|
// read template file
|
|
|
|
if ($_filepath === false) {
|
|
|
|
throw new Exception("Unable to load template \"file : {$_file}\"");
|
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
if ($_file != $_files[0]) {
|
2009-10-04 18:12:30 +00:00
|
|
|
$template->properties['file_dependency']['F' . abs(crc32($_filepath))] = array($_filepath, filemtime($_filepath));
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
$template->template_filepath = $_filepath;
|
2009-03-27 15:31:47 +00:00
|
|
|
$_content = file_get_contents($_filepath);
|
|
|
|
if ($_file != $_files[count($_files)-1]) {
|
2009-10-04 18:12:30 +00:00
|
|
|
if (preg_match_all('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')/', $_content, $_open, PREG_OFFSET_CAPTURE) !=
|
|
|
|
preg_match_all('/(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/', $_content, $_close, PREG_OFFSET_CAPTURE)) {
|
2009-09-11 18:59:48 +00:00
|
|
|
$this->smarty->trigger_error(" unmatched {block} {/block} pairs");
|
|
|
|
}
|
2009-10-04 18:12:30 +00:00
|
|
|
$_block_count = count($_open[0]);
|
|
|
|
for ($_i = 0; $_i < $_block_count; $_i++) {
|
|
|
|
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
|
|
|
|
substr($_content, $_open[0][$_i][1] + strlen($_open[0][$_i][0]), $_close[0][$_i][1] - $_open[0][$_i][1] - strlen($_open[0][$_i][0])));
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath);
|
2009-09-11 18:59:48 +00:00
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
} else {
|
2009-10-04 18:12:30 +00:00
|
|
|
$template->template_source = $_content;
|
2009-09-11 18:59:48 +00:00
|
|
|
return true;
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
$template->template_filepath = $saved_filepath;
|
2009-09-11 18:59:48 +00:00
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
protected function saveBlockData($block_content, $block_tag, $_filepath)
|
2009-03-27 15:31:47 +00:00
|
|
|
{
|
2009-09-11 18:59:48 +00:00
|
|
|
if (0 == preg_match('/(.?)(name=)([^ ]*)/', $block_tag, $_match)) {
|
|
|
|
$this->smarty->trigger_error("\"" . $block_tag . "\" missing name attribute");
|
2009-03-27 15:31:47 +00:00
|
|
|
} else {
|
2009-09-11 18:59:48 +00:00
|
|
|
$_name = trim($_match[3], "\"'}");
|
2009-10-12 14:37:31 +00:00
|
|
|
if (isset($this->smarty->block_data[$_name])) {
|
2009-12-03 19:49:17 +00:00
|
|
|
if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
|
|
|
|
$this->smarty->block_data[$_name]['source'] =
|
|
|
|
str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']);
|
2009-10-12 14:37:31 +00:00
|
|
|
} elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] .= $block_content;
|
2009-10-12 14:37:31 +00:00
|
|
|
} elseif ($this->smarty->block_data[$_name]['mode'] == 'append') {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source'];
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-06-13 20:39:56 +00:00
|
|
|
} else {
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['source'] = $block_content;
|
2009-06-13 20:39:56 +00:00
|
|
|
}
|
2009-12-12 10:50:40 +00:00
|
|
|
if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'append';
|
2009-12-12 10:50:40 +00:00
|
|
|
} elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'prepend';
|
2009-06-13 20:39:56 +00:00
|
|
|
} else {
|
2009-10-12 14:37:31 +00:00
|
|
|
$this->smarty->block_data[$_name]['mode'] = 'replace';
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
$this->smarty->block_data[$_name]['file'] = $_filepath;
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-09-11 18:59:48 +00:00
|
|
|
}
|
2009-03-27 15:31:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return flag that this resource uses the compiler
|
|
|
|
*
|
|
|
|
* @return boolean true
|
|
|
|
*/
|
|
|
|
public function usesCompiler()
|
|
|
|
{
|
|
|
|
// template has tags, uses compiler
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return flag that this is not evaluated
|
|
|
|
*
|
|
|
|
* @return boolean false
|
|
|
|
*/
|
|
|
|
public function isEvaluated()
|
|
|
|
{
|
|
|
|
// save the compiled file to disk, do not evaluate
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get filepath to compiled template
|
|
|
|
*
|
2009-10-04 18:12:30 +00:00
|
|
|
* @param object $template template object
|
2009-03-27 15:31:47 +00:00
|
|
|
* @return string return path to compiled template
|
|
|
|
*/
|
2009-10-04 18:12:30 +00:00
|
|
|
public function getCompiledFilepath($template)
|
2009-03-27 15:31:47 +00:00
|
|
|
{
|
2009-12-03 19:49:17 +00:00
|
|
|
$_compile_id = isset($template->compile_id) ? preg_replace('![^\w\|]+!', '_', $template->compile_id) : null;
|
2009-10-04 18:12:30 +00:00
|
|
|
$_files = explode('|', $template->resource_name);
|
2009-12-17 16:58:44 +00:00
|
|
|
$_filepath = (string)abs(crc32($template->getTemplateFilepath()));
|
2009-03-27 15:31:47 +00:00
|
|
|
// if use_sub_dirs, break file into directories
|
2009-10-04 18:12:30 +00:00
|
|
|
if ($template->smarty->use_sub_dirs) {
|
2009-10-31 00:44:58 +00:00
|
|
|
$_filepath = substr($_filepath, 0, 2) . DS
|
|
|
|
. substr($_filepath, 2, 2) . DS
|
|
|
|
. substr($_filepath, 4, 2) . DS
|
2009-03-27 15:31:47 +00:00
|
|
|
. $_filepath;
|
|
|
|
}
|
2009-10-04 18:12:30 +00:00
|
|
|
$_compile_dir_sep = $template->smarty->use_sub_dirs ? DS : '^';
|
2009-11-17 17:46:03 +00:00
|
|
|
if (isset($_compile_id)) {
|
|
|
|
$_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-10-04 18:12:30 +00:00
|
|
|
if ($template->caching) {
|
2009-03-27 15:31:47 +00:00
|
|
|
$_cache = '.cache';
|
|
|
|
} else {
|
|
|
|
$_cache = '';
|
|
|
|
}
|
2009-10-04 18:12:30 +00:00
|
|
|
$_compile_dir = $template->smarty->compile_dir;
|
2009-08-29 22:57:29 +00:00
|
|
|
if (substr($_compile_dir, -1) != DS) {
|
|
|
|
$_compile_dir .= DS;
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
2009-12-03 19:49:17 +00:00
|
|
|
return $_compile_dir . $_filepath . '.' . $template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
|
2009-03-27 15:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|