- bugfix multiple {include} with relative filepath within {block}{/block} could fail https://github.com/smarty-php/smarty/issues/246

This commit is contained in:
uwetews
2016-07-19 03:37:01 +02:00
parent afb971f84b
commit f39e61762c
7 changed files with 26 additions and 76 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
19.07.2016
- bugfix multiple {include} with relative filepath within {block}{/block} could fail https://github.com/smarty-php/smarty/issues/246
18.07.2016
- bugfix {foreach} if key variable and item@key attribute have been used both the key variable was not updated https://github.com/smarty-php/smarty/issues/254
- bugfix modifier on plugins like {plugin|modifier ... } did fail when the plugin does return an array https://github.com/smarty-php/smarty/issues/228

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/84';
const SMARTY_VERSION = '3.1.30-dev/85';
/**
* define variable scopes

View File

@@ -65,13 +65,6 @@ class Smarty_Internal_Block
*/
public $tplIndex = 0;
/**
* Nesting level of called sub-templates
*
* @var int
*/
public $subTemplateNesting = 0;
/**
* Smarty_Internal_Block constructor.
* - if outer level {block} of child template ($state == 1) save it as child root block

View File

@@ -99,7 +99,7 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inh
private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler)
{
$compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
"<?php \$_smarty_tpl->inheritance->endChild(\$_smarty_tpl);\n?>\n");
"<?php \$_smarty_tpl->inheritance->endChild();\n?>\n");
}
/**

View File

@@ -43,13 +43,8 @@ class Smarty_Internal_Resource_File extends Smarty_Resource
) {
throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'");
}
$parentPath = $_template->parent->source->filepath;
// if we are inside an {block} tag the path must be relative to template of {block}
if (isset($_template->inheritance) && $path = $_template->inheritance->getBlockFilepath()) {
$parentPath = $path;
}
// normalize path
$path = $source->smarty->_realpath(dirname($parentPath) . DS . $file);
$path = $source->smarty->_realpath(dirname($_template->parent->source->filepath) . DS . $file);
// files relative to a template only get one shot
return is_file($path) ? $path : false;
}

View File

@@ -53,11 +53,11 @@ class Smarty_Internal_Runtime_Inheritance
public $sources = array();
/**
* Call stack of block objects
* Stack of source objects while executing block code
*
* @var Smarty_Internal_Block[]
* @var Smarty_Template_Source[]
*/
public $blockCallStack = array();
public $sourceStack = array();
/**
* Initialize inheritance
@@ -75,6 +75,9 @@ class Smarty_Internal_Runtime_Inheritance
$tpl->inheritance->init($tpl, $initChild, $blockNames);
return;
}
$this->tplIndex ++;
$this->sources[ $this->tplIndex ] = $tpl->source;
// start of child sub template(s)
if ($initChild) {
$this->state = 1;
@@ -83,13 +86,8 @@ class Smarty_Internal_Runtime_Inheritance
ob_start();
}
$this->inheritanceLevel ++;
$tpl->startRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateStart');
$tpl->endRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateEnd');
}
// in parent state {include} will not increment template index
if ($this->state != 3) {
$this->tplIndex ++;
$this->sources[ $this->tplIndex ] = $tpl->source;
// $tpl->startRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateStart');
// $tpl->endRenderCallbacks[ 'inheritance' ] = array($this, 'subTemplateEnd');
}
// if state was waiting for parent change state to parent
if ($this->state == 2) {
@@ -101,9 +99,8 @@ class Smarty_Internal_Runtime_Inheritance
* End of child template(s)
* - if outer level is reached flush output buffer and switch to wait for parent template state
*
* @param \Smarty_Internal_Template $tpl template object of caller
*/
public function endChild(Smarty_Internal_Template $tpl)
public function endChild()
{
$this->inheritanceLevel --;
if (!$this->inheritanceLevel) {
@@ -162,10 +159,7 @@ class Smarty_Internal_Runtime_Inheritance
$this->callParent($tpl, $block);
}
if ($block->callsChild || !isset($block->child) || ($block->child->hide && !isset($block->child->child))) {
$block->subTemplateNesting = 0;
$this->blockCallStack[] = $block;
$block->callBlock($tpl);
array_pop($this->blockCallStack);
$this->callBlock($block, $tpl);
} else {
$this->process($tpl, $block->child, $block);
}
@@ -175,10 +169,7 @@ class Smarty_Internal_Runtime_Inheritance
if ($block->callsChild || !isset($block->child) ||
($block->child->hide && !isset($block->child->child))
) {
$block->subTemplateNesting = 0;
$this->blockCallStack[] = $block;
$block->callBlock($tpl);
array_pop($this->blockCallStack);
$this->callBlock($block, $tpl);
} else {
$this->process($tpl, $block->child, $block);
}
@@ -211,48 +202,21 @@ class Smarty_Internal_Runtime_Inheritance
public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block)
{
if (isset($block->parent)) {
$block->parent->subTemplateNesting = 0;
$this->blockCallStack[] = $block->parent;
$block->parent->callBlock($tpl);
array_pop($this->blockCallStack);
$this->callBlock($block->parent, $tpl);
} else {
throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'");
}
}
/**
* Return source filepath of current {block} if not in sub-template
*
* @return bool|string filepath or false
* @param \Smarty_Internal_Block $block
* @param \Smarty_Internal_Template $tpl
*/
public function getBlockFilepath()
public function callBlock(Smarty_Internal_Block $block, Smarty_Internal_Template $tpl)
{
$count = count($this->blockCallStack);
if ($count && $this->blockCallStack[ $count - 1 ]->subTemplateNesting === 0) {
return $this->sources[ $this->blockCallStack[ $count - 1 ]->tplIndex ]->filepath;
}
return false;
}
/**
* Increment sub-template nesting count in current block object
*/
public function subTemplateStart()
{
$count = count($this->blockCallStack);
if ($count) {
$this->blockCallStack[ $count - 1 ]->subTemplateNesting ++;
}
}
/**
* Decrement sub-template nesting count in current block object
*/
public function subTemplateEnd()
{
$count = count($this->blockCallStack);
if ($count && $this->blockCallStack[ $count - 1 ]->subTemplateNesting) {
$this->blockCallStack[ $count - 1 ]->subTemplateNesting --;
}
$this->sourceStack[] = $tpl->source;
$tpl->source = $this->sources[ $block->tplIndex ];
$block->callBlock($tpl);
$tpl->source = array_pop($this->sourceStack);
}
}

View File

@@ -218,12 +218,7 @@ abstract class Smarty_Resource
if ($obj->_objType == 2 && $_file_is_dotted &&
($obj->source->type == 'file' || $obj->parent->source->type == 'extends')
) {
$parentPath = $obj->parent->source->filepath;
// if we are inside an {block} tag the path must be relative to template of {block}
if (isset($obj->inheritance) && $path = $obj->inheritance->getBlockFilepath()) {
$parentPath = $path;
}
$name = $smarty->_realpath(dirname($parentPath) . DS . $name);
$name = $smarty->_realpath(dirname($obj->parent->source->filepath) . DS . $name);
}
return $resource->buildUniqueResourceName($smarty, $name);
}