Commit 848d2a8a by Seldaek

! BC Break: {include} and {extends} now support the include path properly,

which means that if you include "foo/bar.html" from _any_ template and you have an include path set on your template object, it will look in all those paths for foo/bar.html. If you use relative paths, for example if you include "../foo/bar.html" AND have an include path set, you will now have a problem, because you can't mix both approaches, otherwise you should be fine, so to fix this you should convert your relative includes/extends * Dwoo->get() is now stricter as to what it accepts as a "template", only Dwoo_ITemplate objects or valid filenames are accepted * Foreach and other similar plugins that support "else" now only count() their input before processing when an else block follows git-svn-id: svn://dwoo.org/dwoo/trunk@163 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 2b4ad8af
[2008-0-] 0.9.4 / 1.0.0 ?
[2008-09-] 0.9.4
! BC Break: {include} and {extends} now support the include path properly,
which means that if you include "foo/bar.html" from _any_ template and you
have an include path set on your template object, it will look in all those
paths for foo/bar.html. If you use relative paths, for example
if you include "../foo/bar.html" AND have an include path set, you will now
have a problem, because you can't mix both approaches, otherwise you should
be fine, so to fix this you should convert your relative includes/extends
+ API: Added Dwoo_Compilation_Exception methods getCompiler() and
getTemplate() so you can catch the exception and use those to build a nicer
error view with all the details you want
......@@ -8,6 +15,10 @@
however since it is really bad practice I won't spend time fixing edge
cases, which are $ and '/" characters inside the string. Those will break
it and that's it.. if you really care feel free to send a patch
* Dwoo->get() is now stricter as to what it accepts as a "template", only
Dwoo_ITemplate objects or valid filenames are accepted
* Foreach and other similar plugins that support "else" now only count()
their input before processing when an else block follows
* Fixed compiler bug that created a parse error when you had comments in an
extended template
* Fixed extends bug when extending files in other directories using relative
......
......@@ -5,6 +5,7 @@ set_include_path(get_include_path() . PATH_SEPARATOR . DWOO_DIRECTORY);
include 'Dwoo/IPluginProxy.php';
include 'Dwoo/ILoader.php';
include 'Dwoo/IElseable.php';
include 'Dwoo/Loader.php';
include 'Dwoo/Exception.php';
include 'Dwoo/Security/Policy.php';
......@@ -352,15 +353,8 @@ class Dwoo
// auto-create template if required
if ($_tpl instanceof Dwoo_ITemplate) {
// valid, skip
} elseif (is_string($_tpl)) {
if (file_exists($_tpl)) {
} elseif (is_string($_tpl) && file_exists($_tpl)) {
$_tpl = new Dwoo_Template_File($_tpl);
} elseif (strstr($_tpl, ':')) {
$_bits = explode(':', $_tpl, 2);
$_tpl = $this->templateFactory($_bits[0], $_bits[1]);
} else {
$_tpl = new Dwoo_Template_String($_tpl);
}
} else {
throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
}
......@@ -889,10 +883,11 @@ class Dwoo
* @param string $compileId the unique compiler identifier
* @return Dwoo_ITemplate
*/
public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null)
public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
{
if (isset($this->resources[$resourceName])) {
return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId);
// TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe
return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate);
} else {
throw new Dwoo_Exception('Unknown resource type : '.$resourceName);
}
......@@ -944,7 +939,10 @@ class Dwoo
*/
public function triggerError($message, $level=E_USER_NOTICE)
{
trigger_error('Dwoo error (in '.$this->template->getResourceIdentifier().') : '.$message, $level);
if (!($tplIdentifier = $this->template->getResourceIdentifier())) {
$tplIdentifier = $this->template->getResourceName();
}
trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level);
}
/*
......
......@@ -525,13 +525,16 @@ class Dwoo_Compiler implements Dwoo_ICompiler
/**
* returns the template that is being compiled
*
* @param bool $fromPointer if set to true, only the source from the current pointer position is returned
* @param mixed $fromPointer if set to true, only the source from the current pointer
* position is returned, if a number is given it overrides the current pointer
* @return string the template or partial template
*/
public function getTemplateSource($fromPointer = false)
{
if ($fromPointer) {
if ($fromPointer === true) {
return substr($this->templateSource, $this->pointer);
} elseif (is_numeric($fromPointer)) {
return substr($this->templateSource, $fromPointer);
} else {
return $this->templateSource;
}
......@@ -872,7 +875,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$params = $this->mapParams($params, array($class, 'init'), $paramtype);
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'buffer' => null);
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'class' => $class, 'buffer' => null);
$this->curBlock =& $this->stack[count($this->stack)-1];
return call_user_func(array($class,'preProcessing'), $this, $params, '', '', $type);
}
......@@ -896,7 +899,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$params = $this->mapParams($params, array($class, 'init'), $paramtype);
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => true, 'class'=>$class, 'buffer' => null);
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => true, 'class' => $class, 'buffer' => null);
$this->curBlock =& $this->stack[count($this->stack)-1];
return call_user_func(array($class,'preProcessing'), $this, $params, '', '', $type);
}
......@@ -915,7 +918,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if (class_exists($class, false) === false) {
$this->dwoo->getLoader()->loadPlugin($type);
}
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'buffer' => null);
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'class' => $class, 'buffer' => null);
$this->curBlock =& $this->stack[count($this->stack)-1];
}
......@@ -981,7 +984,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($b['type']===$type) {
return $this->stack[key($this->stack)];
}
$this->removeTopBlock();
$this->push($this->removeTopBlock(), 0);
}
} else {
end($this->stack);
......@@ -1037,8 +1040,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler
*/
public function getCompiledParams(array $params)
{
foreach ($params as &$p)
$p = $p[0];
foreach ($params as $k=>$p) {
if (is_array($p)) {
$params[$k] = $p[0];
}
}
return $params;
}
......@@ -1050,8 +1056,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler
*/
public function getRealParams(array $params)
{
foreach ($params as &$p)
$p = $p[1];
foreach ($params as $k=>$p) {
if (is_array($p)) {
$params[$k] = $p[1];
}
}
return $params;
}
......@@ -1124,10 +1133,19 @@ class Dwoo_Compiler implements Dwoo_ICompiler
return $this->parse($in, $from+1, $to, false, 'root', $pointer);
} elseif ($curBlock === 'root' && preg_match('#^/([a-z][a-z0-9_]*)?#i', $substr, $match)) {
// close block
if (!empty($match[1]) && $match[1] == 'else') {
throw new Dwoo_Compilation_Exception($this, 'Else blocks must not be closed explicitly, they are automatically closed when their parent block is closed');
}
if (!empty($match[1]) && $match[1] == 'elseif') {
throw new Dwoo_Compilation_Exception($this, 'Elseif blocks must not be closed explicitly, they are automatically closed when their parent block is closed or a new else/elseif block is declared after them');
}
if ($pointer !== null) {
$pointer += strlen($match[0]);
}
if (empty($match[1])) {
if ($this->curBlock['type'] == 'else' || $this->curBlock['type'] == 'elseif') {
$pointer -= strlen($match[0]);
}
if ($this->debug) echo 'TOP BLOCK CLOSED<br />';
return $this->removeTopBlock();
} else {
......
<?php
/**
* interface that represents a block plugin that supports the else functionality
*
* the else block will enter an "hasElse" parameter inside the parameters array
* of the closest parent implementing this interface, the hasElse parameter contains
* the else output that should be appended to the block's content (see foreach or other
* block for examples)
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.9.1
* @date 2008-05-30
* @package Dwoo
*/
interface Dwoo_IElseable
{
}
......@@ -122,9 +122,13 @@ interface Dwoo_ITemplate
public function getCompiler();
/**
* returns a new template object from the given include name, null if no include is
* returns a new template object from the given resource identifier, null if no include is
* possible (resource not found), or false if include is not permitted by this resource type
*
* this method should also check if $dwoo->getSecurityPolicy() is null or not and do the
* necessary permission checks if required, if the security policy prevents the template
* generation it should throw a new Dwoo_Security_Exception with a relevant message
*
* @param mixed $resourceId the resource identifier
* @param int $cacheTime duration of the cache validity for this template,
* if null it defaults to the Dwoo instance that will
......@@ -134,7 +138,9 @@ interface Dwoo_ITemplate
* to the current url
* @param string $compileId the unique compiled identifier, which is used to distinguish this
* template from others, if null it defaults to the filename+bits of the path
* @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
* an include, extends or any other plugin)
* @return Dwoo_ITemplate|null|false
*/
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null);
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null);
}
......@@ -123,6 +123,9 @@ class Dwoo_Security_Policy
/**
* adds a directory to the safelist for includes and other file-access plugins
*
* note that all the includePath directories you provide to the Dwoo_Template_File class
* are automatically marked as safe
*
* @param mixed $path a path name or an array of paths
*/
public function allowDirectory($path)
......
......@@ -219,33 +219,61 @@ class Dwoo_Template_File extends Dwoo_Template_String
* to the current url
* @param string $compileId the unique compiled identifier, which is used to distinguish this
* template from others, if null it defaults to the filename+bits of the path
* @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
* an include, extends or any other plugin)
* @return Dwoo_Template_File|null
*/
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null)
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
{
if (DIRECTORY_SEPARATOR === '\\') {
$resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId);
}
$resourceId = strtr($resourceId, '\\', '/');
$includePath = null;
if (file_exists($resourceId) === false) {
$tpl = $dwoo->getTemplate();
if ($tpl instanceof Dwoo_Template_File) {
$resourceId = dirname($tpl->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
if ($parentTemplate === null) {
$parentTemplate = $dwoo->getTemplate();
}
if ($parentTemplate instanceof Dwoo_Template_File) {
if ($includePath = $parentTemplate->getIncludePath()) {
if (strstr($resourceId, '../')) {
throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)');
}
} else {
$resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId;
if (file_exists($resourceId) === false) {
return null;
}
}
} else {
return null;
}
}
// prevent template recursion if security is in effect
if ($policy = $dwoo->getSecurityPolicy()) {
$tpl = $dwoo->getTemplate();
if ($tpl instanceof Dwoo_Template_File && $resourceId === $tpl->getResourceIdentifier()) {
return $dwoo->triggerError('You can not include a template into itself', E_USER_WARNING);
while (true) {
if (preg_match('{^([a-z]+?)://}i', $resourceId)) {
throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.');
}
if ($includePath) {
break;
}
$resourceId = realpath($resourceId);
$dirs = $policy->getAllowedDirectories();
foreach ($dirs as $dir=>$dummy) {
if (strpos($resourceId, $dir) === 0) {
break 2;
}
}
throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>');
}
}
return new Dwoo_Template_File($resourceId, $cacheTime, $cacheId, $compileId);
return new Dwoo_Template_File($resourceId, $cacheTime, $cacheId, $compileId, $includePath);
}
/**
......
......@@ -346,8 +346,9 @@ class Dwoo_Template_String implements Dwoo_ITemplate
}
/**
* returns false as this template type does not support inclusions
* returns a new template string object with the resource id being the template source code
*
* @param Dwoo $dwoo the dwoo instance requiring it
* @param mixed $resourceId the filename (relative to this template's dir) of the template to include
* @param int $cacheTime duration of the cache validity for this template,
* if null it defaults to the Dwoo instance that will
......@@ -357,11 +358,13 @@ class Dwoo_Template_String implements Dwoo_ITemplate
* to the current url
* @param string $compileId the unique compiled identifier, which is used to distinguish this
* template from others, if null it defaults to the filename+bits of the path
* @return false
* @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
* an include, extends or any other plugin)
* @return Dwoo_Template_String
*/
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null)
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
{
return false;
return new self($resourceId, $cacheTime, $cacheId, $compileId);
}
/**
......
......@@ -34,41 +34,35 @@
*/
class Dwoo_Plugin_else extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
{
public static $types = array
(
'if' => true, 'elseif' => true, 'for' => true,
'foreach' => true, 'loop' => true, 'with' => true
);
public function init()
{
}
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
$preContent = '';
while (true) {
$preContent .= $compiler->removeTopBlock();
$block =& $compiler->getCurrentBlock();
$out = '';
while (!isset(self::$types[$block['type']])) {
$out .= $compiler->removeTopBlock();
$block =& $compiler->getCurrentBlock();
$interfaces = class_implements($block['class'], false);
if (in_array('Dwoo_IElseable', $interfaces) !== false) {
break;
}
}
$out .= $block['params']['postOutput'];
$block['params']['postOutput'] = '';
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return $out . " else {\n".Dwoo_Compiler::PHP_CLOSE;
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return $preContent;
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
if (isset($params['postOutput'])) {
return $content . $params['postOutput'];
} else {
return $content;
if (!isset($params['initialized'])) {
return '';
}
$block =& $compiler->getCurrentBlock();
$block['params']['hasElse'] = Dwoo_Compiler::PHP_OPEN."else {\n".Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return '';
}
}
......@@ -20,57 +20,46 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_elseif extends Dwoo_Plugin_if implements Dwoo_ICompilable_Block
class Dwoo_Plugin_elseif extends Dwoo_Plugin_if implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public static $types = array
(
'if' => true, 'elseif' => true
);
public function init(array $rest)
{
}
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
// delete this block
$compiler->removeTopBlock();
// fetch the top of the stack
$parent =& $compiler->getCurrentBlock();
// loop until we get an elseif or if block
$out = '';
while (!isset(self::$types[$parent['type']])) {
$out .= $compiler->removeTopBlock();
$parent =& $compiler->getCurrentBlock();
$preContent = '';
while (true) {
$preContent .= $compiler->removeTopBlock();
$block =& $compiler->getCurrentBlock();
$interfaces = class_implements($block['class'], false);
if (in_array('Dwoo_IElseable', $interfaces) !== false) {
break;
}
}
//
$out .= $parent['params']['postOutput'];
$parent['params']['postOutput'] = '';
// reinsert this block
$compiler->injectBlock($type, $params, 1);
// generate post-output
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return $preContent;
}
if ($out === '') {
$out = Dwoo_Compiler::PHP_OPEN."\n}";
} else {
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
if (!isset($params['initialized'])) {
return '';
}
$params = $compiler->getCompiledParams($params);
return $out . " elseif (".implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n" . Dwoo_Compiler::PHP_CLOSE;
}
$pre = Dwoo_Compiler::PHP_OPEN."elseif (".implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n" . Dwoo_Compiler::PHP_CLOSE;
$post = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
if (isset($params['postOutput'])) {
return $content . $params['postOutput'];
} else {
return $content;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
$block =& $compiler->getCurrentBlock();
$block['params']['hasElse'] = $pre . $content . $post;
return '';
}
}
......@@ -25,7 +25,7 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public static $cnt=0;
......@@ -35,8 +35,17 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$params = $compiler->getCompiledParams($params);
$tpl = $compiler->getTemplateSource(true);
$tpl = $compiler->getTemplateSource($params['tplPointer']);
// assigns params
$from = $params['from'];
......@@ -112,17 +121,12 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
$postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
}
// end loop
$postOut .= "\n\t}\n}\n";
// get block params and save the post-processing output already
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = $postOut . Dwoo_Compiler::PHP_CLOSE;
$postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
return $out;
if (isset($params['hasElse'])) {
$postOut .= $params['hasElse'];
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOutput'];
return $out . $content . $postOut;
}
}
......@@ -34,7 +34,7 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public static $cnt=0;
......@@ -44,8 +44,17 @@ class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$params = $compiler->getCompiledParams($params);
$tpl = $compiler->getTemplateSource(true);
$tpl = $compiler->getTemplateSource($params['tplPointer']);
// assigns params
$src = $params['from'];
......@@ -90,59 +99,53 @@ class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
// gets foreach id
$cnt = self::$cnt++;
// builds pre processing output
$out = Dwoo_Compiler::PHP_OPEN . "\n".'$_fh'.$cnt.'_data = '.$src.';';
// build pre content output
$pre = Dwoo_Compiler::PHP_OPEN . "\n".'$_fh'.$cnt.'_data = '.$src.';';
// adds foreach properties
if ($usesAny) {
$out .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
if ($usesIndex) $out .="\n\t".'"index" => 0,';
if ($usesIteration) $out .="\n\t".'"iteration" => 1,';
if ($usesFirst) $out .="\n\t".'"first" => null,';
if ($usesLast) $out .="\n\t".'"last" => null,';
if ($usesShow) $out .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true, true),';
if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
$out.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
$pre .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
if ($usesIndex) $pre .="\n\t".'"index" => 0,';
if ($usesIteration) $pre .="\n\t".'"iteration" => 1,';
if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
$pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
}
// checks if foreach must be looped
$out .= "\n".'if ($this->isArray($_fh'.$cnt.'_data, true, true) === true)'."\n{";
$pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true, true' : '').') === true)'."\n{";
// iterates over keys
$out .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
$pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
// updates properties
if ($usesFirst) {
$out .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
$pre .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
}
if ($usesLast) {
$out .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
$pre .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
}
$out .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
$pre .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
// build post processing output and cache it
$postOut = Dwoo_Compiler::PHP_OPEN . "\n";
// build post content output
$post = Dwoo_Compiler::PHP_OPEN . "\n";
if (isset($implode)) {
$postOut .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
$post .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
"\n\t".'echo '.$implode.";\n}\n";
}
$postOut .= '/* -- foreach end output */';
$post .= '/* -- foreach end output */';
// update properties
if ($usesIndex) {
$postOut.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
$post.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
}
if ($usesIteration) {
$postOut.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
$post.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
}
// end loop
$postOut .= "\n\t}\n}\n";
// get block params and save the post-processing output already
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = $postOut . Dwoo_Compiler::PHP_CLOSE;
return $out;
$post .= "\n\t}\n}".Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOutput'];
return $pre . $content . $post;
}
}
......@@ -27,23 +27,22 @@ class Dwoo_Plugin_foreachelse extends Dwoo_Block_Plugin implements Dwoo_ICompila
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
$foreach =& $compiler->findBlock('foreach', true);
$out = $foreach['params']['postOutput'];
$foreach['params']['postOutput'] = '';
$with =& $compiler->findBlock('foreach', true);
$compiler->injectBlock($type, $params, 1);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
if (substr($out, -strlen(Dwoo_Compiler::PHP_CLOSE)) === Dwoo_Compiler::PHP_CLOSE) {
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
} else {
$out .= Dwoo_Compiler::PHP_OPEN;
}
return $out . "else\n{" . Dwoo_Compiler::PHP_CLOSE;
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . Dwoo_Compiler::PHP_OPEN.'}'.Dwoo_Compiler::PHP_CLOSE;
if (!isset($params['initialized'])) {
return '';
}
$block =& $compiler->getCurrentBlock();
$block['params']['hasElse'] = Dwoo_Compiler::PHP_OPEN."else {\n".Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return '';
}
}
......@@ -27,23 +27,22 @@ class Dwoo_Plugin_forelse extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
$foreach =& $compiler->findBlock('for', true);
$out = $foreach['params']['postOutput'];
$foreach['params']['postOutput'] = '';
$with =& $compiler->findBlock('for', true);
$compiler->injectBlock($type, $params, 1);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
if (substr($out, -strlen(Dwoo_Compiler::PHP_CLOSE)) === Dwoo_Compiler::PHP_CLOSE) {
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
} else {
$out .= Dwoo_Compiler::PHP_OPEN;
}
return $out . "else\n{" . Dwoo_Compiler::PHP_CLOSE;
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . Dwoo_Compiler::PHP_OPEN.'}'.Dwoo_Compiler::PHP_CLOSE;
if (!isset($params['initialized'])) {
return '';
}
$block =& $compiler->getCurrentBlock();
$block['params']['hasElse'] = Dwoo_Compiler::PHP_OPEN."else {\n".Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return '';
}
}
......@@ -32,7 +32,7 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public function init(array $rest)
{
......@@ -165,16 +165,21 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$params = $compiler->getCompiledParams($params);
return Dwoo_Compiler::PHP_OPEN.'if ('.implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
$pre = Dwoo_Compiler::PHP_OPEN.'if ('.implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
$post = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOutput'];
return $pre . $content . $post;
}
}
......@@ -43,7 +43,7 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public static $cnt=0;
......@@ -53,8 +53,17 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$params = $compiler->getCompiledParams($params);
$tpl = $compiler->getTemplateSource(true);
$tpl = $compiler->getTemplateSource($params['tplPointer']);
// assigns params
$src = $params['from'];
......@@ -75,52 +84,46 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
$cnt = self::$cnt++;
// builds pre processing output
$out = Dwoo_Compiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';';
$pre = Dwoo_Compiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';';
// adds foreach properties
if ($usesAny) {
$out .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
if ($usesIndex) $out .="\n\t".'"index" => 0,';
if ($usesIteration) $out .="\n\t".'"iteration" => 1,';
if ($usesFirst) $out .="\n\t".'"first" => null,';
if ($usesLast) $out .="\n\t".'"last" => null,';
if ($usesShow) $out .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true, true),';
if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
$out.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
$pre .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
if ($usesIndex) $pre .="\n\t".'"index" => 0,';
if ($usesIteration) $pre .="\n\t".'"iteration" => 1,';
if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
$pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
}
// checks if foreach must be looped
$out .= "\n".'if ($this->isArray($_loop'.$cnt.'_data, true, true) === true)'."\n{";
// checks if the loop must be looped
$pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true, true' : '').') === true)'."\n{";
// iterates over keys
$out .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
$pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
// updates properties
if ($usesFirst) {
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
$pre .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
}
if ($usesLast) {
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
$pre .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
}
$out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope(array("-loop-"));' . "\n/* -- loop start output */\n".Dwoo_Compiler::PHP_CLOSE;
$pre .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope(array("-loop-"));' . "\n/* -- loop start output */\n".Dwoo_Compiler::PHP_CLOSE;
// build post processing output and cache it
$postOut = Dwoo_Compiler::PHP_OPEN . "\n".'/* -- loop end output */'."\n\t\t".'$this->setScope($_loop'.$cnt.'_scope, true);';
$post = Dwoo_Compiler::PHP_OPEN . "\n".'/* -- loop end output */'."\n\t\t".'$this->setScope($_loop'.$cnt.'_scope, true);';
// update properties
if ($usesIndex) {
$postOut.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
$post.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
}
if ($usesIteration) {
$postOut.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
$post.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
}
// end loop
$postOut .= "\n\t}\n}\n";
// get block params and save the post-processing output already
$currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = $postOut . Dwoo_Compiler::PHP_CLOSE;
return $out;
$post .= "\n\t}\n}\n" . Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOutput'];
return $pre . $content . $post;
}
}
......@@ -43,7 +43,7 @@
* @date 2008-05-30
* @package Dwoo
*/
class Dwoo_Plugin_with extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
class Dwoo_Plugin_with extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
protected static $cnt=0;
......@@ -53,19 +53,29 @@ class Dwoo_Plugin_with extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$rparams = $compiler->getRealParams($params);
$cparams = $compiler->getCompiledParams($params);
$compiler->setScope($rparams['var']);
$params =& $compiler->getCurrentBlock();
$params['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n/* -- end with output */\n".'$this->setScope($_with'.(self::$cnt).', true);'."\n}\n".Dwoo_Compiler::PHP_CLOSE;
return Dwoo_Compiler::PHP_OPEN.'if ('.$cparams['var'].')'."\n{\n".'$_with'.(self::$cnt++).' = $this->setScope("'.$rparams['var'].'");'."\n/* -- start with output */\n".Dwoo_Compiler::PHP_CLOSE;
$pre = Dwoo_Compiler::PHP_OPEN. 'if ('.$cparams['var'].')'."\n{\n".
'$_with'.(self::$cnt).' = $this->setScope("'.$rparams['var'].'");'.
"\n/* -- start with output */\n".Dwoo_Compiler::PHP_CLOSE;
$post = Dwoo_Compiler::PHP_OPEN. "\n/* -- end with output */\n".
'$this->setScope($_with'.(self::$cnt++).', true);'.
"\n}\n".Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$post .= $params['hasElse'];
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOutput'];
return $pre . $content . $post;
}
}
......@@ -27,23 +27,22 @@ class Dwoo_Plugin_withelse extends Dwoo_Block_Plugin implements Dwoo_ICompilable
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
$foreach =& $compiler->findBlock('with', true);
$out = $foreach['params']['postOutput'];
$foreach['params']['postOutput'] = '';
$with =& $compiler->findBlock('with', true);
$compiler->injectBlock($type, $params, 1);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
if (substr($out, -strlen(Dwoo_Compiler::PHP_CLOSE)) === Dwoo_Compiler::PHP_CLOSE) {
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
} else {
$out .= Dwoo_Compiler::PHP_OPEN;
}
return $out . "else\n{" . Dwoo_Compiler::PHP_CLOSE;
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . Dwoo_Compiler::PHP_OPEN.'}'.Dwoo_Compiler::PHP_CLOSE;
if (!isset($params['initialized'])) {
return '';
}
$block =& $compiler->getCurrentBlock();
$block['params']['hasElse'] = Dwoo_Compiler::PHP_OPEN."else {\n".Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return '';
}
}
......@@ -60,7 +60,7 @@ function Dwoo_Plugin_escape(Dwoo $dwoo, $value='', $format='html', $charset=null
case 'mail':
return str_replace(array('@', '.'), array('&nbsp;(AT)&nbsp;', '&nbsp;(DOT)&nbsp;'), (string) $value);
default:
$dwoo->triggerError('Escape\'s format argument must be one of : html, htmlall, url, urlpathinfo, hex, hexentity, javascript or mail, "'.$format.'" given.', E_USER_WARNING);
return $dwoo->triggerError('Escape\'s format argument must be one of : html, htmlall, url, urlpathinfo, hex, hexentity, javascript or mail, "'.$format.'" given.', E_USER_WARNING);
}
}
......@@ -36,7 +36,7 @@ function Dwoo_Plugin_eval(Dwoo $dwoo, $var, $assign = null)
}
$tpl = new Dwoo_Template_String($var);
$out = $dwoo->get($var, $dwoo->readVar('_parent'));
$out = $dwoo->get($tpl, $dwoo->readVar('_parent'));
if ($assign !== null) {
$dwoo->assignInScope($out, $assign);
......
......@@ -40,6 +40,7 @@ class Dwoo_Plugin_extends extends Dwoo_Plugin implements Dwoo_ICompilable
}
$inheritanceTree = array(array('source'=>$compiler->getTemplateSource()));
$curPath = dirname($compiler->getDwoo()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR;
$curTpl = $compiler->getDwoo()->getTemplate();
while (!empty($file)) {
if ($file === '""' || $file === "''" || (substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'')) {
......@@ -48,53 +49,38 @@ class Dwoo_Plugin_extends extends Dwoo_Plugin implements Dwoo_ICompilable
}
if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) {
// resource:identifier given, extract them
$resource = $m[1];
$identifier = $m[2];
} else {
$resource = 'file';
// get the current template's resource
$resource = $curTpl->getResourceName();
$identifier = substr($file, 1, -1);
}
if (!preg_match('#^([a-z]:[/\\\\]|[/\\\\])#i', $identifier)) {
$identifier = realpath($curPath . $identifier);
}
if ($resource === 'file' && $policy = $compiler->getSecurityPolicy()) {
while (true) {
if (preg_match('{^([a-z]+?)://}i', $identifier)) {
throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources.');
}
$dirs = $policy->getAllowedDirectories();
foreach ($dirs as $dir=>$dummy) {
if (strpos($identifier, $dir) === 0) {
break 2;
}
}
throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$identifier.'</em>');
}
}
try {
$parent = $compiler->getDwoo()->templateFactory($resource, $identifier);
$parent = $compiler->getDwoo()->templateFactory($resource, $identifier, null, null, null, $curTpl);
} catch (Dwoo_Security_Exception $e) {
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Security restriction : '.$e->getMessage());
} catch (Dwoo_Exception $e) {
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource <em>'.$resource.'</em> was not added to Dwoo, can not include <em>'.$identifier.'</em>');
throw new Dwoo_Compilation_Exception($compiler, 'Extends : '.$e->getMessage());
}
if ($parent === null) {
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "'.$resource.':'.$identifier.'" was not found.');
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "'.$resource.':'.$identifier.'" not found.');
} elseif ($parent === false) {
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Extending "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.');
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "'.$resource.'" does not support extends.');
}
$newParent = array('source'=>$parent->getSource(), 'resource'=>$resource, 'identifier'=>$identifier, 'uid'=>$parent->getUid());
$curTpl = $parent;
$newParent = array('source'=>$parent->getSource(), 'resource'=>$resource, 'identifier'=>$parent->getResourceIdentifier(), 'uid'=>$parent->getUid());
if (array_search($newParent, $inheritanceTree, true) !== false) {
throw new Dwoo_Compilation_Exception($compiler, 'Extends : Recursive template inheritance detected');
}
$inheritanceTree[] = $newParent;
if (preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?)'.self::$r.'/i', $parent->getSource(), $match)) {
if (preg_match('/^'.self::$l.'extends\s+(?:file=)?\s*(\S+?|(["\']).+?\2)'.self::$r.'/i', $parent->getSource(), $match)) {
$curPath = dirname($identifier) . DIRECTORY_SEPARATOR;
$file = (substr($match[1], 0, 1) !== '"' && substr($match[1], 0, 1) !== '"') ? '"'.str_replace('"', '\\"', $match[1]).'"' : $match[1];
} else {
......
......@@ -25,7 +25,7 @@ function Dwoo_Plugin_extendsCheck_compile(Dwoo_Compiler $compiler, $file, $uid)
$resource = $m[1];
$identifier = $m[2];
return '// check for modification in '.$resource.':'.$identifier.'
return '// checking for modification in '.$resource.':'.$identifier.'
try {
$tpl = $this->templateFactory("'.$resource.'", "'.$identifier.'");
} catch (Dwoo_Exception $e) {
......@@ -34,6 +34,6 @@ try {
if ($tpl === null)
$this->triggerError(\'Extends : Resource "'.$resource.':'.$identifier.'" was not found.\', E_USER_WARNING);
elseif ($tpl === false)
$this->triggerError(\'Include : Extending "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.\', E_USER_WARNING);
$this->triggerError(\'Extends : Resource "'.$resource.'" does not support extends.\', E_USER_WARNING);
if ($tpl->getUid() != "'.substr($uid, 1, -1).'") { ob_end_clean(); return false; }';
}
......@@ -33,7 +33,8 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
return;
}
if (preg_match('#^([a-z]{2,}):(.*)#i', $file, $m)) {
if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) {
// resource:identifier given, extract them
$resource = $m[1];
$identifier = $m[2];
} else {
......@@ -42,37 +43,18 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
$identifier = $file;
}
if ($resource === 'file' && $policy = $dwoo->getSecurityPolicy()) {
while (true) {
if (preg_match('{^([a-z]+?)://}i', $identifier)) {
throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$identifier.'</em>.');
}
$identifier = realpath($identifier);
$dirs = $policy->getAllowedDirectories();
foreach ($dirs as $dir=>$dummy) {
if (strpos($identifier, $dir) === 0) {
break 2;
}
}
throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$identifier.'</em>');
}
}
if ($resource === 'file' && strpos($identifier, '..') !== false) {
$identifier = realpath(dirname($dwoo->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $identifier);
}
try {
$include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
} catch (Dwoo_Security_Exception $e) {
return $dwoo->triggerError('Include : Security restriction : '.$e->getMessage(), E_USER_WARNING);
} catch (Dwoo_Exception $e) {
$dwoo->triggerError('Include : Resource <em>'.$resource.'</em> was not added to Dwoo, can not include <em>'.$identifier.'</em>', E_USER_WARNING);
return $dwoo->triggerError('Include : '.$e->getMessage(), E_USER_WARNING);
}
if ($include === null) {
return;
return $dwoo->triggerError('Include : Resource "'.$resource.':'.$identifier.'" not found.', E_USER_WARNING);
} elseif ($include === false) {
$dwoo->triggerError('Include : Including "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.', E_USER_WARNING);
return $dwoo->triggerError('Include : Resource "'.$resource.'" does not support includes.', E_USER_WARNING);
}
if ($dwoo->isArray($data)) {
......
......@@ -96,7 +96,7 @@ function Dwoo_Plugin_mailto(Dwoo $dwoo, $address, $text=null, $subject=null, $en
case 'hex':
if (strpos($address, '?') !== false) {
$dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
return $dwoo->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
}
$out = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
......@@ -116,7 +116,7 @@ function Dwoo_Plugin_mailto(Dwoo $dwoo, $address, $text=null, $subject=null, $en
return $out.'</a>';
default:
$dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING);
return $dwoo->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING);
}
}
......@@ -217,7 +217,6 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->assertEquals("13", $tpl->getCacheTime());
$this->assertEquals(null, $tpl->getCompiler());
$this->assertEquals(false, Dwoo_Template_String::templateFactory($this->dwoo, 'foo', 5));
}
public function testCachedTemplateAndClearCache()
......
......@@ -248,15 +248,18 @@ class FuncTests extends PHPUnit_Framework_TestCase
$this->assertEquals("ab", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler));
$tpl = new Dwoo_Template_String('{include file=\'file:/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test.html\'}');
$tpl = new Dwoo_Template_File(TEST_DIRECTORY.'/resources/inctest.html');
$tpl->forceCompilation();
$this->assertEquals("", $this->dwoo->get($tpl, array('foo'=>'a', 'bar'=>'b'), $this->compiler));
$this->assertEquals("34", $this->dwoo->get($tpl, array(), $this->compiler));
}
$tpl = new Dwoo_Template_File(TEST_DIRECTORY.'/resources/inctest.html');
public function testIncludeString()
{
$tpl = new Dwoo_Template_String('{include file=$tpl foo=$a bar=$b}');
$tpl->forceCompilation();
$this->assertEquals("34", $this->dwoo->get($tpl, array(), $this->compiler));
$this->assertEquals("AB", $this->dwoo->get($tpl, array('a'=>'A', 'b'=>'B', 'tpl'=>'{$a}{$b}')));
}
public function testIncludeParent()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment