diff --git a/demo/plugins/cacheresource.mysql.php b/demo/plugins/cacheresource.mysql.php index e9494dbf..fda09bb7 100644 --- a/demo/plugins/cacheresource.mysql.php +++ b/demo/plugins/cacheresource.mysql.php @@ -1,5 +1,7 @@ db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id'); $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id'); diff --git a/demo/plugins/cacheresource.pdo.php b/demo/plugins/cacheresource.pdo.php index c4c44de6..8be18ec9 100644 --- a/demo/plugins/cacheresource.pdo.php +++ b/demo/plugins/cacheresource.pdo.php @@ -1,5 +1,7 @@ pdo = $pdo; $this->table = $table; diff --git a/demo/plugins/resource.extendsall.php b/demo/plugins/resource.extendsall.php index 6441b47a..09e9258d 100644 --- a/demo/plugins/resource.extendsall.php +++ b/demo/plugins/resource.extendsall.php @@ -1,5 +1,6 @@ uid ] = $s; $uid .= $s->filepath; $timestamp = $s->timestamp > $timestamp ? $s->timestamp : $timestamp; - } catch (SmartyException $e) { + } catch (Exception $e) { } } if (!$sources) { diff --git a/demo/plugins/resource.mysql.php b/demo/plugins/resource.mysql.php index e42b536d..7b151a78 100644 --- a/demo/plugins/resource.mysql.php +++ b/demo/plugins/resource.mysql.php @@ -1,6 +1,7 @@ db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); diff --git a/demo/plugins/resource.mysqls.php b/demo/plugins/resource.mysqls.php index 84338a76..84e2ed20 100644 --- a/demo/plugins/resource.mysqls.php +++ b/demo/plugins/resource.mysqls.php @@ -1,6 +1,7 @@ db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); } diff --git a/docs/programmers/api-variables/variable-default-config-handler-func.md b/docs/programmers/api-variables/variable-default-config-handler-func.md index 0d6ec5e0..90891426 100644 --- a/docs/programmers/api-variables/variable-default-config-handler-func.md +++ b/docs/programmers/api-variables/variable-default-config-handler-func.md @@ -8,7 +8,7 @@ resource. > > The default handler is currently only invoked for file resources. It > is not triggered when the resource itself cannot be found, in which -> case a SmartyException is thrown. +> case a \Smarty\Exception is thrown. > The default handler is currently only invoked for file resources. It > is not triggered when the resource itself cannot be found, in which -> case a SmartyException is thrown. +> case a \Smarty\Exception is thrown. db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new \Smarty\Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id'); $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id'); diff --git a/docs/programmers/plugins/plugins-resources.md b/docs/programmers/plugins/plugins-resources.md index 716984d1..a54b1e99 100644 --- a/docs/programmers/plugins/plugins-resources.md +++ b/docs/programmers/plugins/plugins-resources.md @@ -63,7 +63,7 @@ example. try { $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new \Smarty\Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); diff --git a/docs/programmers/resources/resources-custom.md b/docs/programmers/resources/resources-custom.md index d679afcb..57b3d5fe 100644 --- a/docs/programmers/resources/resources-custom.md +++ b/docs/programmers/resources/resources-custom.md @@ -49,7 +49,7 @@ on the functions you are supposed to provide. try { $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new \Smarty\Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); diff --git a/docs/programmers/resources/template-resources.md b/docs/programmers/resources/template-resources.md index 7bb5d752..e442fdd3 100644 --- a/docs/programmers/resources/template-resources.md +++ b/docs/programmers/resources/template-resources.md @@ -59,7 +59,7 @@ on the functions you are supposed to provide. try { $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty"); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new \Smarty\Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); diff --git a/plugins/block.textformat.php b/plugins/block.textformat.php index 3935a4a1..398d66bc 100644 --- a/plugins/block.textformat.php +++ b/plugins/block.textformat.php @@ -33,7 +33,7 @@ use Smarty\Template; * * @return string content re-formatted * @author Monte Ohrt - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_block_textformat($params, $content, Template $template, &$repeat) { diff --git a/plugins/function.fetch.php b/plugins/function.fetch.php index 745846f9..de766d5f 100644 --- a/plugins/function.fetch.php +++ b/plugins/function.fetch.php @@ -6,6 +6,7 @@ * @subpackage PluginsFunction */ +use Smarty\Exception; use Smarty\Template; /** @@ -21,7 +22,7 @@ use Smarty\Template; * @param array $params parameters * @param Template $template template object * - * @throws SmartyException + * @throws Exception * @return string|null if the assign parameter is passed, Smarty assigns the result to a template variable */ function smarty_function_fetch($params, $template) @@ -196,7 +197,7 @@ function smarty_function_fetch($params, $template) } else { $content = @file_get_contents($params[ 'file' ]); if ($content === false) { - throw new SmartyException("{fetch} cannot read resource '" . $params[ 'file' ] . "'"); + throw new Exception("{fetch} cannot read resource '" . $params[ 'file' ] . "'"); } } if (!empty($params[ 'assign' ])) { diff --git a/plugins/function.html_checkboxes.php b/plugins/function.html_checkboxes.php index 81a44dd3..7e370784 100644 --- a/plugins/function.html_checkboxes.php +++ b/plugins/function.html_checkboxes.php @@ -43,7 +43,7 @@ use Smarty\Template; * * @return string * @uses smarty_function_escape_special_chars() - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_function_html_checkboxes($params, Template $template) { diff --git a/plugins/function.html_image.php b/plugins/function.html_image.php index 055d6e61..7275436c 100644 --- a/plugins/function.html_image.php +++ b/plugins/function.html_image.php @@ -6,6 +6,7 @@ * @subpackage PluginsFunction */ +use Smarty\Exception; use Smarty\Template; /** @@ -33,7 +34,7 @@ use Smarty\Template; * @param array $params parameters * @param Template $template template object * - * @throws SmartyException + * @throws Exception * @return string * @uses smarty_function_escape_special_chars() */ @@ -62,7 +63,7 @@ function smarty_function_html_image($params, Template $template) if (!is_array($_val)) { $$_key = smarty_function_escape_special_chars($_val); } else { - throw new SmartyException( + throw new Exception( "html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE ); @@ -77,7 +78,7 @@ function smarty_function_html_image($params, Template $template) if (!is_array($_val)) { $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; } else { - throw new SmartyException( + throw new Exception( "html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE ); diff --git a/plugins/function.html_options.php b/plugins/function.html_options.php index 73afdd80..d47b98bb 100644 --- a/plugins/function.html_options.php +++ b/plugins/function.html_options.php @@ -35,7 +35,7 @@ use Smarty\Template; * * @return string * @uses smarty_function_escape_special_chars() - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_function_html_options($params, Template $template) { diff --git a/plugins/function.html_radios.php b/plugins/function.html_radios.php index ec1453a9..d3eb2e76 100644 --- a/plugins/function.html_radios.php +++ b/plugins/function.html_radios.php @@ -43,7 +43,7 @@ use Smarty\Template; * * @return string * @uses smarty_function_escape_special_chars() - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_function_html_radios($params, Template $template) { diff --git a/plugins/function.html_select_date.php b/plugins/function.html_select_date.php index 77483abe..89f9b5e7 100644 --- a/plugins/function.html_select_date.php +++ b/plugins/function.html_select_date.php @@ -43,7 +43,7 @@ use Smarty\Template; * @param \Smarty\Template $template * * @return string - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_function_html_select_date($params, Template $template) { diff --git a/plugins/function.html_select_time.php b/plugins/function.html_select_time.php index 30c25b21..b283090e 100644 --- a/plugins/function.html_select_time.php +++ b/plugins/function.html_select_time.php @@ -25,7 +25,7 @@ use Smarty\Template; * * @return string * @uses smarty_make_timestamp() - * @throws \SmartyException + * @throws \Smarty\Exception */ function smarty_function_html_select_time($params, Template $template) { diff --git a/plugins/modifiercompiler.escape.php b/plugins/modifiercompiler.escape.php index 0fc50774..1c9b024b 100644 --- a/plugins/modifiercompiler.escape.php +++ b/plugins/modifiercompiler.escape.php @@ -5,6 +5,9 @@ * @package Smarty * @subpackage PluginsModifierCompiler */ + +use Smarty\Exception; + /** * Smarty escape modifier plugin * Type: modifier @@ -18,7 +21,7 @@ * @param \Smarty\Compiler\Template $compiler * * @return string with compiled code - * @throws SmartyException + * @throws Exception */ function smarty_modifiercompiler_escape($params, \Smarty\Compiler\Template $compiler) { @@ -53,7 +56,7 @@ function smarty_modifiercompiler_escape($params, \Smarty\Compiler\Template $comp $params[ 0 ] . ', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", " "<\/", " Smarty Compiler: ' . $this->message . ' <-- '; + } + + /** + * @param int $line + */ + public function setLine($line) { + $this->line = $line; + } + + /** + * The template source snippet relating to the error + * + * @type string|null + */ + public $source = null; + + /** + * The raw text of the error message + * + * @type string|null + */ + public $desc = null; + + /** + * The resource identifier or template name + * + * @type string|null + */ + public $template = null; +} diff --git a/src/Data.php b/src/Data.php index e8c2e345..1dc2121b 100644 --- a/src/Data.php +++ b/src/Data.php @@ -48,7 +48,7 @@ abstract class Data /** * parent template (if any) * - * @var Smarty|Template|Smarty_Data + * @var Smarty|Template|DataObject */ public $parent = null; @@ -126,7 +126,7 @@ abstract class Data if ($tpl_var !== '' && isset($value)) { if (!isset($this->tpl_vars[ $tpl_var ])) { $tpl_var_inst = $this->_getVariable($tpl_var, null, true, false); - if ($tpl_var_inst instanceof Smarty_Undefined_Variable) { + if ($tpl_var_inst instanceof UndefinedVariable) { $this->tpl_vars[ $tpl_var ] = new \Smarty\Variable(null, $nocache); } else { $this->tpl_vars[ $tpl_var ] = clone $tpl_var_inst; @@ -313,7 +313,7 @@ abstract class Data // force a notice $x = $$varName; } - return new Smarty_Undefined_Variable; + return new UndefinedVariable; } /** @@ -566,12 +566,12 @@ abstract class Data /** * gets a stream variable * - * @api Smarty::getStreamVariable() - * * @param string $variable the stream of the variable * * @return mixed - * @throws \SmartyException + * @throws \Smarty\Exception + *@api Smarty::getStreamVariable() + * */ public function getStreamVariable($variable) { @@ -586,7 +586,7 @@ abstract class Data } $smarty = $this->smarty ?? $this; if ($smarty->error_unassigned) { - throw new SmartyException('Undefined stream variable "' . $variable . '"'); + throw new Exception('Undefined stream variable "' . $variable . '"'); } else { return null; } diff --git a/src/DataObject.php b/src/DataObject.php new file mode 100644 index 00000000..2f80c989 --- /dev/null +++ b/src/DataObject.php @@ -0,0 +1,71 @@ +dataObjectName = 'Data_object ' . (isset($name) ? "'{$name}'" : self::$count); + $this->smarty = $smarty; + if (is_object($_parent)) { + // when object set up back pointer + $this->parent = $_parent; + } elseif (is_array($_parent)) { + // set up variable values + foreach ($_parent as $_key => $_val) { + $this->tpl_vars[$_key] = new Variable($_val); + } + } elseif ($_parent !== null) { + throw new Exception('Wrong type for template variables'); + } + } +} diff --git a/src/Debug.php b/src/Debug.php index 012b8f0b..4f483d12 100644 --- a/src/Debug.php +++ b/src/Debug.php @@ -188,7 +188,7 @@ class Debug extends Data * @param bool $full * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function display_debug($obj, $full = false) { @@ -254,7 +254,7 @@ class Debug extends Data /** * Recursively gets variables from all template/data scopes * - * @param \Smarty\Template|\Smarty_Data $obj object to debug + * @param \Smarty\Template|\Smarty\DataObject $obj object to debug * * @return \StdClass */ diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 00000000..0f75f568 --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,16 @@ + Smarty: ' . $this->message . ' <-- '; + } +} diff --git a/src/Resource/BasePlugin.php b/src/Resource/BasePlugin.php index 0c3ee3f7..abb6d8b0 100644 --- a/src/Resource/BasePlugin.php +++ b/src/Resource/BasePlugin.php @@ -64,8 +64,8 @@ abstract class BasePlugin * @param \Smarty $smarty smarty object * @param string $type name of the resource * - * @throws \SmartyException * @return BasePlugin Resource Handler + *@throws \Smarty\Exception */ public static function load(\Smarty $smarty, $type) { @@ -108,7 +108,7 @@ abstract class BasePlugin } // TODO: try default_(template|config)_handler // give up - throw new \SmartyException("Unknown resource type '{$type}'"); + throw new \Smarty\Exception("Unknown resource type '{$type}'"); } /** @@ -143,7 +143,7 @@ abstract class BasePlugin * name of * * @return string unique resource name - * @throws \SmartyException + * @throws \Smarty\Exception */ public static function getUniqueTemplateName($obj, $template_resource) { @@ -171,7 +171,7 @@ abstract class BasePlugin * @param string $template_resource resource identifier * * @return \Smarty\Template\Source Source Object - * @throws \SmartyException + * @throws \Smarty\Exception */ public static function source( \Smarty\Template $_template = null, @@ -187,7 +187,7 @@ abstract class BasePlugin * @param \Smarty\Template\Source $source source object * * @return string template source - * @throws \SmartyException if source cannot be loaded + * @throws \Smarty\Exception if source cannot be loaded */ abstract public function getContent(\Smarty\Template\Source $source); diff --git a/src/Resource/CustomPlugin.php b/src/Resource/CustomPlugin.php index 7b9100d9..2d62dd98 100644 --- a/src/Resource/CustomPlugin.php +++ b/src/Resource/CustomPlugin.php @@ -11,7 +11,7 @@ namespace Smarty\Resource; use Smarty; use Smarty\Template; use Smarty\Template\Source; -use SmartyException; +use Smarty\Exception; /** * Smarty Resource Plugin @@ -72,14 +72,14 @@ abstract class CustomPlugin extends BasePlugin { * @param Source $source source object * * @return string template source - * @throws SmartyException if source cannot be loaded + * @throws Exception if source cannot be loaded */ public function getContent(Source $source) { $this->fetch($source->name, $content, $timestamp); if (isset($content)) { return $content; } - throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); + throw new Exception("Unable to read template {$source->type} '{$source->name}'"); } /** diff --git a/src/Resource/ExtendsPlugin.php b/src/Resource/ExtendsPlugin.php index f4b9fba8..19a9598c 100644 --- a/src/Resource/ExtendsPlugin.php +++ b/src/Resource/ExtendsPlugin.php @@ -27,7 +27,7 @@ class ExtendsPlugin extends BasePlugin * @param \Smarty\Template\Source $source source object * @param \Smarty\Template $_template template object * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function populate(\Smarty\Template\Source $source, \Smarty\Template $_template = null) { @@ -40,7 +40,7 @@ class ExtendsPlugin extends BasePlugin /* @var \Smarty\Template\Source $_s */ $_s = \Smarty\Template\Source::load(null, $smarty, $component); if ($_s->type === 'php') { - throw new \SmartyException("Resource type {$_s->type} cannot be used with the extends resource type"); + throw new \Smarty\Exception("Resource type {$_s->type} cannot be used with the extends resource type"); } $sources[ $_s->uid ] = $_s; $uid .= $_s->filepath; @@ -78,12 +78,12 @@ class ExtendsPlugin extends BasePlugin * @param \Smarty\Template\Source $source source object * * @return string template source - * @throws \SmartyException if source cannot be loaded + * @throws \Smarty\Exception if source cannot be loaded */ public function getContent(\Smarty\Template\Source $source) { if (!$source->exists) { - throw new \SmartyException("Unable to load template '{$source->type}:{$source->name}'"); + throw new \Smarty\Exception("Unable to load template '{$source->type}:{$source->name}'"); } $_components = array_reverse($source->components); $_content = ''; diff --git a/src/Resource/FilePlugin.php b/src/Resource/FilePlugin.php index 7fc4e36f..bd6c957f 100644 --- a/src/Resource/FilePlugin.php +++ b/src/Resource/FilePlugin.php @@ -12,7 +12,7 @@ namespace Smarty\Resource; use Smarty; use Smarty\Template; use Smarty\Template\Source; -use SmartyException; +use Smarty\Exception; /** * Smarty Internal Plugin Resource File @@ -29,7 +29,7 @@ class FilePlugin extends BasePlugin { * @param Source $source source object * @param Template $_template template object * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function populate(Source $source, Template $_template = null) { $source->filepath = $this->buildFilepath($source, $_template); @@ -68,13 +68,13 @@ class FilePlugin extends BasePlugin { * @param Source $source source object * * @return string template source - * @throws SmartyException if source cannot be loaded + * @throws Exception if source cannot be loaded */ public function getContent(Source $source) { if ($source->exists) { return file_get_contents($source->filepath); } - throw new SmartyException( + throw new Exception( 'Unable to read ' . ($source->isConfig ? 'config' : 'template') . " {$source->type} '{$source->name}'" ); @@ -98,7 +98,7 @@ class FilePlugin extends BasePlugin { * @param Template $_template template object * * @return string fully qualified filepath - * @throws SmartyException + * @throws Exception */ protected function buildFilepath(Source $source, Template $_template = null) { $file = $source->name; @@ -112,7 +112,7 @@ class FilePlugin extends BasePlugin { && preg_match('#^[.]{1,2}[\\\/]#', $file) ) { if ($_template->parent->source->type !== 'file' && $_template->parent->source->type !== 'extends') { - throw new SmartyException("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); + throw new Exception("Template '{$file}' cannot be relative to template of resource type '{$_template->parent->source->type}'"); } // normalize path $path = diff --git a/src/Resource/PhpPlugin.php b/src/Resource/PhpPlugin.php index e52aef49..160d8133 100644 --- a/src/Resource/PhpPlugin.php +++ b/src/Resource/PhpPlugin.php @@ -5,7 +5,7 @@ namespace Smarty\Resource; use Smarty\Template; use Smarty\Template\Source; use Smarty\Template\Compiled; -use SmartyException; +use Smarty\Exception; /** * Smarty Internal Plugin Resource PHP @@ -52,13 +52,13 @@ class PhpPlugin extends FilePlugin { * @param Source $source source object * * @return string template source - * @throws SmartyException if source cannot be loaded + * @throws Exception if source cannot be loaded */ public function getContent(Source $source) { if ($source->exists) { return ''; } - throw new SmartyException("Unable to read template {$source->type} '{$source->name}'"); + throw new Exception("Unable to read template {$source->type} '{$source->name}'"); } /** @@ -86,14 +86,14 @@ class PhpPlugin extends FilePlugin { * @param Template $_template template object * * @return void - * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled + * @throws Exception if template cannot be loaded or allow_php_templates is disabled */ public function renderUncompiled(Source $source, Template $_template) { if (!$source->smarty->allow_php_templates) { - throw new SmartyException('PHP templates are disabled'); + throw new Exception('PHP templates are disabled'); } if (!$source->exists) { - throw new SmartyException( + throw new Exception( "Unable to load template '{$source->type}:{$source->name}'" . ($_template->_isSubTpl() ? " in '{$_template->parent->template_resource}'" : '') ); diff --git a/src/Block.php b/src/Runtime/Block.php similarity index 98% rename from src/Block.php rename to src/Runtime/Block.php index ff8e00ef..161fd3a3 100644 --- a/src/Block.php +++ b/src/Runtime/Block.php @@ -1,6 +1,6 @@ captureCount) { @@ -124,10 +124,10 @@ class CaptureRuntime { * * @param \Smarty\Template $_template * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function error(Template $_template) { - throw new SmartyException("Not matching {capture}{/capture} in '{$_template->template_resource}'"); + throw new \Smarty\Exception("Not matching {capture}{/capture} in '{$_template->template_resource}'"); } /** @@ -151,7 +151,7 @@ class CaptureRuntime { * * @param \Smarty\Template $_template * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function endRender(Template $_template) { if ($this->captureCount) { diff --git a/src/Runtime/InheritanceRuntime.php b/src/Runtime/InheritanceRuntime.php index d882611e..4626dfaf 100644 --- a/src/Runtime/InheritanceRuntime.php +++ b/src/Runtime/InheritanceRuntime.php @@ -3,7 +3,7 @@ namespace Smarty\Runtime; use Smarty\Template; use Smarty\Template\Source; -use SmartyException; +use Smarty\Exception; /** * Inheritance Runtime Methods processBlock, endChild, init @@ -29,7 +29,7 @@ class InheritanceRuntime { /** * Array of root child {block} objects * - * @var \Smarty\Block[] + * @var \Smarty\Runtime\Block[] */ public $childRoot = []; @@ -104,7 +104,7 @@ class InheritanceRuntime { * @param null|string $func function call name of inline template * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function endChild(Template $tpl, $template = null, $uid = null, $func = null) { --$this->inheritanceLevel; @@ -140,7 +140,7 @@ class InheritanceRuntime { * @param string $name * @param int|null $tplIndex index of outer level {block} if nested * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function instanceBlock(Template $tpl, $className, $name, $tplIndex = null) { $block = new $className($name, isset($tplIndex) ? $tplIndex : $this->tplIndex); @@ -162,15 +162,15 @@ class InheritanceRuntime { * Goto child block or render this * * @param Template $tpl - * @param \Smarty\Block $block - * @param \Smarty\Block|null $parent + * @param \Smarty\Runtime\Block $block + * @param \Smarty\Runtime\Block|null $parent * - * @throws SmartyException + * @throws Exception */ public function process( - Template $tpl, - \Smarty\Block $block, - \Smarty\Block $parent = null + Template $tpl, + \Smarty\Runtime\Block $block, + \Smarty\Runtime\Block $parent = null ) { if ($block->hide && !isset($block->child)) { return; @@ -206,12 +206,12 @@ class InheritanceRuntime { * Render child on \$smarty.block.child * * @param Template $tpl - * @param \Smarty\Block $block + * @param \Smarty\Runtime\Block $block * * @return null|string block content - * @throws SmartyException + * @throws Exception */ - public function callChild(Template $tpl, \Smarty\Block $block) { + public function callChild(Template $tpl, \Smarty\Runtime\Block $block) { if (isset($block->child)) { $this->process($tpl, $block->child, $block); } @@ -221,27 +221,27 @@ class InheritanceRuntime { * Render parent block on \$smarty.block.parent or {block append/prepend} * * @param Template $tpl - * @param \Smarty\Block $block + * @param \Smarty\Runtime\Block $block * @param string $tag * * @return null|string block content - * @throws SmartyException + * @throws Exception */ - public function callParent(Template $tpl, \Smarty\Block $block, $tag) { + public function callParent(Template $tpl, \Smarty\Runtime\Block $block, $tag) { if (isset($block->parent)) { $this->callBlock($block->parent, $tpl); } else { - throw new SmartyException("inheritance: illegal '{$tag}' used in child template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'"); + throw new Exception("inheritance: illegal '{$tag}' used in child template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'"); } } /** * render block * - * @param \Smarty\Block $block + * @param \Smarty\Runtime\Block $block * @param Template $tpl */ - public function callBlock(\Smarty\Block $block, Template $tpl) { + public function callBlock(\Smarty\Runtime\Block $block, Template $tpl) { $this->sourceStack[] = $tpl->source; $tpl->source = $this->sources[$block->tplIndex]; $block->callBlock($tpl); diff --git a/src/Runtime/MakeNocacheRuntime.php b/src/Runtime/MakeNocacheRuntime.php index 8a29a606..6be4c91c 100644 --- a/src/Runtime/MakeNocacheRuntime.php +++ b/src/Runtime/MakeNocacheRuntime.php @@ -19,14 +19,14 @@ class MakeNocacheRuntime { * @param \Smarty\Template $tpl * @param string $var variable name * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function save(Template $tpl, $var) { if (isset($tpl->tpl_vars[$var])) { $export = preg_replace('/^\\\\Smarty\\\\Variable::__set_state[(]|[)]$/', '', var_export($tpl->tpl_vars[$var], true)); if (preg_match('/(\w+)::__set_state/', $export, $match)) { - throw new SmartyException("{make_nocache \${$var}} in template '{$tpl->source->name}': variable does contain object '{$match[1]}' not implementing method '__set_state'"); + throw new \Smarty\Exception("{make_nocache \${$var}} in template '{$tpl->source->name}': variable does contain object '{$match[1]}' not implementing method '__set_state'"); } echo "/*%%SmartyNocache:{$tpl->compiled->nocache_hash}%%*/smarty->getRuntime('MakeNocache')->store(\$_smarty_tpl, '{$var}', ", '\\') . diff --git a/src/Runtime/TplFunctionRuntime.php b/src/Runtime/TplFunctionRuntime.php index ef6fa02c..89f3a2c2 100644 --- a/src/Runtime/TplFunctionRuntime.php +++ b/src/Runtime/TplFunctionRuntime.php @@ -21,7 +21,7 @@ class TplFunctionRuntime { * @param array $params parameter array * @param bool $nocache true if called nocache * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function callTemplateFunction(Template $tpl, $name, $params, $nocache) { $funcParam = $tpl->tplFunctions[$name] ?? ($tpl->smarty->tplFunctions[$name] ?? null); @@ -49,7 +49,7 @@ class TplFunctionRuntime { return; } } - throw new SmartyException("Unable to find template function '{$name}'"); + throw new \Smarty\Exception("Unable to find template function '{$name}'"); } /** diff --git a/src/Security.php b/src/Security.php new file mode 100644 index 00000000..3acd4662 --- /dev/null +++ b/src/Security.php @@ -0,0 +1,667 @@ + array('method_1', 'method_2'), // allowed methods listed + * 'class_2' => array(), // all methods of class allowed + * ) + * If set to null none is allowed. + * + * @var array + */ + public $trusted_static_methods = []; + + /** + * This is an array of trusted static properties. + * If empty access to all static classes and properties is allowed. + * Format: + * array ( + * 'class_1' => array('prop_1', 'prop_2'), // allowed properties listed + * 'class_2' => array(), // all properties of class allowed + * ) + * If set to null none is allowed. + * + * @var array + */ + public $trusted_static_properties = []; + + /** + * This is an array of trusted PHP functions. + * If empty all functions are allowed. + * To disable all PHP functions set $php_functions = null. + * + * @var array + */ + public $php_functions = ['isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',]; + + /** + * This is an array of trusted PHP modifiers. + * If empty all modifiers are allowed. + * To disable all modifier set $php_modifiers = null. + * + * @var array + */ + public $php_modifiers = ['escape', 'count', 'sizeof', 'nl2br',]; + + /** + * This is an array of allowed tags. + * If empty no restriction by allowed_tags. + * + * @var array + */ + public $allowed_tags = []; + + /** + * This is an array of disabled tags. + * If empty no restriction by disabled_tags. + * + * @var array + */ + public $disabled_tags = []; + + /** + * This is an array of allowed modifier plugins. + * If empty no restriction by allowed_modifiers. + * + * @var array + */ + public $allowed_modifiers = []; + + /** + * This is an array of disabled modifier plugins. + * If empty no restriction by disabled_modifiers. + * + * @var array + */ + public $disabled_modifiers = []; + + /** + * This is an array of disabled special $smarty variables. + * + * @var array + */ + public $disabled_special_smarty_vars = []; + + /** + * This is an array of trusted streams. + * If empty all streams are allowed. + * To disable all streams set $streams = null. + * + * @var array + */ + public $streams = ['file']; + + /** + * + flag if constants can be accessed from template + * + * @var boolean + */ + public $allow_constants = true; + + /** + * + flag if super globals can be accessed from template + * + * @var boolean + */ + public $allow_super_globals = true; + + /** + * max template nesting level + * + * @var int + */ + public $max_template_nesting = 0; + + /** + * current template nesting level + * + * @var int + */ + private $_current_template_nesting = 0; + + /** + * Cache for $resource_dir lookup + * + * @var array + */ + protected $_resource_dir = []; + + /** + * Cache for $template_dir lookup + * + * @var array + */ + protected $_template_dir = []; + + /** + * Cache for $config_dir lookup + * + * @var array + */ + protected $_config_dir = []; + + /** + * Cache for $secure_dir lookup + * + * @var array + */ + protected $_secure_dir = []; + + /** + * Cache for $php_resource_dir lookup + * + * @var array + */ + protected $_php_resource_dir = null; + + /** + * Cache for $trusted_dir lookup + * + * @var array + */ + protected $_trusted_dir = null; + + /** + * Cache for include path status + * + * @var bool + */ + protected $_include_path_status = false; + + /** + * Cache for $_include_array lookup + * + * @var array + */ + protected $_include_dir = []; + + /** + * @param Smarty $smarty + */ + public function __construct(Smarty $smarty) { + $this->smarty = $smarty; + } + + /** + * Check if PHP function is trusted. + * + * @param string $function_name + * @param object $compiler compiler object + * + * @return boolean true if function is trusted + */ + public function isTrustedPhpFunction($function_name, $compiler) { + if (isset($this->php_functions) + && (empty($this->php_functions) || in_array($function_name, $this->php_functions)) + ) { + return true; + } + $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting"); + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if static class is trusted. + * + * @param string $class_name + * @param object $compiler compiler object + * + * @return boolean true if class is trusted + */ + public function isTrustedStaticClass($class_name, $compiler) { + if (isset($this->static_classes) + && (empty($this->static_classes) || in_array($class_name, $this->static_classes)) + ) { + return true; + } + $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting"); + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if static class method/property is trusted. + * + * @param string $class_name + * @param string $params + * @param object $compiler compiler object + * + * @return boolean true if class method is trusted + */ + public function isTrustedStaticClassAccess($class_name, $params, $compiler) { + if (!isset($params[2])) { + // fall back + return $this->isTrustedStaticClass($class_name, $compiler); + } + if ($params[2] === 'method') { + $allowed = $this->trusted_static_methods; + $name = substr($params[0], 0, strpos($params[0], '(')); + } else { + $allowed = $this->trusted_static_properties; + // strip '$' + $name = substr($params[0], 1); + } + if (isset($allowed)) { + if (empty($allowed)) { + // fall back + return $this->isTrustedStaticClass($class_name, $compiler); + } + if (isset($allowed[$class_name]) + && (empty($allowed[$class_name]) || in_array($name, $allowed[$class_name])) + ) { + return true; + } + } + $compiler->trigger_template_error("access to static class '{$class_name}' {$params[2]} '{$name}' not allowed by security setting"); + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if PHP modifier is trusted. + * + * @param string $modifier_name + * @param object $compiler compiler object + * + * @return boolean true if modifier is trusted + * @deprecated + */ + public function isTrustedPhpModifier($modifier_name, $compiler) { + if (isset($this->php_modifiers) + && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers)) + ) { + return true; + } + $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting"); + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if tag is trusted. + * + * @param string $tag_name + * @param object $compiler compiler object + * + * @return boolean true if tag is trusted + */ + public function isTrustedTag($tag_name, $compiler) { + // check for internal always required tags + if (in_array( + $tag_name, + [ + 'assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', + 'private_object_block_function', 'private_object_function', 'private_registered_function', + 'private_registered_block', 'private_special_variable', 'private_print_expression', + 'private_modifier', + ] + ) + ) { + return true; + } + // check security settings + if (empty($this->allowed_tags)) { + if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) { + return true; + } else { + $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true); + } + } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) { + return true; + } else { + $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true); + } + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if special $smarty variable is trusted. + * + * @param string $var_name + * @param object $compiler compiler object + * + * @return boolean true if tag is trusted + */ + public function isTrustedSpecialSmartyVar($var_name, $compiler) { + if (!in_array($var_name, $this->disabled_special_smarty_vars)) { + return true; + } else { + $compiler->trigger_template_error( + "special variable '\$smarty.{$var_name}' not allowed by security setting", + null, + true + ); + } + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if modifier plugin is trusted. + * + * @param string $modifier_name + * @param object $compiler compiler object + * + * @return boolean true if tag is trusted + */ + public function isTrustedModifier($modifier_name, $compiler) { + // check for internal always allowed modifier + if (in_array($modifier_name, ['default'])) { + return true; + } + // check security settings + if (empty($this->allowed_modifiers)) { + if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) { + return true; + } else { + $compiler->trigger_template_error( + "modifier '{$modifier_name}' disabled by security setting", + null, + true + ); + } + } elseif (in_array($modifier_name, $this->allowed_modifiers) + && !in_array($modifier_name, $this->disabled_modifiers) + ) { + return true; + } else { + $compiler->trigger_template_error( + "modifier '{$modifier_name}' not allowed by security setting", + null, + true + ); + } + return false; // should not, but who knows what happens to the compiler in the future? + } + + /** + * Check if constants are enabled or trusted + * + * @param string $const constant name + * @param object $compiler compiler object + * + * @return bool + */ + public function isTrustedConstant($const, $compiler) { + if (in_array($const, ['true', 'false', 'null'])) { + return true; + } + if (!empty($this->trusted_constants)) { + if (!in_array(strtolower($const), $this->trusted_constants)) { + $compiler->trigger_template_error("Security: access to constant '{$const}' not permitted"); + return false; + } + return true; + } + if ($this->allow_constants) { + return true; + } + $compiler->trigger_template_error("Security: access to constants not permitted"); + return false; + } + + /** + * Check if stream is trusted. + * + * @param string $stream_name + * + * @return boolean true if stream is trusted + * @throws Exception if stream is not trusted + */ + public function isTrustedStream($stream_name) { + if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) { + return true; + } + throw new Exception("stream '{$stream_name}' not allowed by security setting"); + } + + /** + * Check if directory of file resource is trusted. + * + * @param string $filepath + * @param null|bool $isConfig + * + * @return bool true if directory is trusted + * @throws \Smarty\Exception if directory is not trusted + */ + public function isTrustedResourceDir($filepath, $isConfig = null) { + if ($this->_include_path_status !== $this->smarty->use_include_path) { + $_dir = + $this->smarty->use_include_path ? $this->smarty->getIncludePathDirs() : []; + if ($this->_include_dir !== $_dir) { + $this->_updateResourceDir($this->_include_dir, $_dir); + $this->_include_dir = $_dir; + } + $this->_include_path_status = $this->smarty->use_include_path; + } + $_dir = $this->smarty->getTemplateDir(); + if ($this->_template_dir !== $_dir) { + $this->_updateResourceDir($this->_template_dir, $_dir); + $this->_template_dir = $_dir; + } + $_dir = $this->smarty->getConfigDir(); + if ($this->_config_dir !== $_dir) { + $this->_updateResourceDir($this->_config_dir, $_dir); + $this->_config_dir = $_dir; + } + if ($this->_secure_dir !== $this->secure_dir) { + $this->secure_dir = (array)$this->secure_dir; + foreach ($this->secure_dir as $k => $d) { + $this->secure_dir[$k] = $this->smarty->_realpath($d . DIRECTORY_SEPARATOR, true); + } + $this->_updateResourceDir($this->_secure_dir, $this->secure_dir); + $this->_secure_dir = $this->secure_dir; + } + $addPath = $this->_checkDir($filepath, $this->_resource_dir); + if ($addPath !== false) { + $this->_resource_dir = array_merge($this->_resource_dir, $addPath); + } + return true; + } + + /** + * Check if URI (e.g. {fetch} or {html_image}) is trusted + * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}". + * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string" + * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}. + * + * @param string $uri + * + * @return boolean true if URI is trusted + * @throws Exception if URI is not trusted + * @uses $trusted_uri for list of patterns to match against $uri + */ + public function isTrustedUri($uri) { + $_uri = parse_url($uri); + if (!empty($_uri['scheme']) && !empty($_uri['host'])) { + $_uri = $_uri['scheme'] . '://' . $_uri['host']; + foreach ($this->trusted_uri as $pattern) { + if (preg_match($pattern, $_uri)) { + return true; + } + } + } + throw new Exception("URI '{$uri}' not allowed by security setting"); + } + + /** + * Remove old directories and its sub folders, add new directories + * + * @param array $oldDir + * @param array $newDir + */ + private function _updateResourceDir($oldDir, $newDir) { + foreach ($oldDir as $directory) { + // $directory = $this->smarty->_realpath($directory, true); + $length = strlen($directory); + foreach ($this->_resource_dir as $dir) { + if (substr($dir, 0, $length) === $directory) { + unset($this->_resource_dir[$dir]); + } + } + } + foreach ($newDir as $directory) { + // $directory = $this->smarty->_realpath($directory, true); + $this->_resource_dir[$directory] = true; + } + } + + /** + * Check if file is inside a valid directory + * + * @param string $filepath + * @param array $dirs valid directories + * + * @return array|bool + * @throws \Smarty\Exception + */ + private function _checkDir($filepath, $dirs) { + $directory = dirname($this->smarty->_realpath($filepath, true)) . DIRECTORY_SEPARATOR; + $_directory = []; + if (!preg_match('#[\\\\/][.][.][\\\\/]#', $directory)) { + while (true) { + // test if the directory is trusted + if (isset($dirs[$directory])) { + return $_directory; + } + // abort if we've reached root + if (!preg_match('#[\\\\/][^\\\\/]+[\\\\/]$#', $directory)) { + // give up + break; + } + // remember the directory to add it to _resource_dir in case we're successful + $_directory[$directory] = true; + // bubble up one level + $directory = preg_replace('#[\\\\/][^\\\\/]+[\\\\/]$#', DIRECTORY_SEPARATOR, $directory); + } + } + // give up + throw new Exception(sprintf('Smarty Security: not trusted file path \'%s\' ', $filepath)); + } + + /** + * Loads security class and enables security + * + * @param \Smarty $smarty + * @param string|Security $security_class if a string is used, it must be class-name + * + * @return \Smarty current Smarty instance for chaining + * @throws \Smarty\Exception when an invalid class name is provided + */ + public static function enableSecurity(Smarty $smarty, $security_class) { + if ($security_class instanceof Security) { + $smarty->security_policy = $security_class; + return $smarty; + } elseif (is_object($security_class)) { + throw new Exception("Class '" . get_class($security_class) . "' must extend Smarty_Security."); + } + if ($security_class === null) { + $security_class = $smarty->security_class; + } + if (!class_exists($security_class)) { + throw new Exception("Security class '$security_class' is not defined"); + } elseif ($security_class !== Security::class && !is_subclass_of($security_class, Security::class)) { + throw new Exception("Class '$security_class' must extend " . Security::class . "."); + } else { + $smarty->security_policy = new $security_class($smarty); + } + return $smarty; + } + + /** + * Start template processing + * + * @param $template + * + * @throws Exception + */ + public function startTemplate($template) { + if ($this->max_template_nesting > 0 && $this->_current_template_nesting++ >= $this->max_template_nesting) { + throw new Exception("maximum template nesting level of '{$this->max_template_nesting}' exceeded when calling '{$template->template_resource}'"); + } + } + + /** + * Exit template processing + */ + public function endTemplate() { + if ($this->max_template_nesting > 0) { + $this->_current_template_nesting--; + } + } + + /** + * Register callback functions call at start/end of template rendering + * + * @param \Smarty\Template $template + */ + public function registerCallBacks(Template $template) { + $template->startRenderCallbacks[] = [$this, 'startTemplate']; + $template->endRenderCallbacks[] = [$this, 'endTemplate']; + } +} diff --git a/src/Smarty.php b/src/Smarty.php index 05e9a98e..9af958ac 100644 --- a/src/Smarty.php +++ b/src/Smarty.php @@ -274,14 +274,14 @@ class Smarty extends \Smarty\TemplateBase * This should be instance of \Smarty_Security. * * @var string - * @see \Smarty_Security + * @see \Smarty\Security */ - public $security_class = \Smarty_Security::class; + public $security_class = \Smarty\Security::class; /** * implementation of security class * - * @var \Smarty_Security + * @var \Smarty\Security */ public $security_policy = null; @@ -592,7 +592,7 @@ class Smarty extends \Smarty\TemplateBase * @param string $resource_name template name * * @return bool status - * @throws \SmartyException + * @throws \Smarty\Exception */ public function templateExists($resource_name) { @@ -604,14 +604,14 @@ class Smarty extends \Smarty\TemplateBase /** * Loads security class and enables security * - * @param string|\Smarty_Security $security_class if a string is used, it must be class-name + * @param string|\Smarty\Security $security_class if a string is used, it must be class-name * * @return Smarty current Smarty instance for chaining - * @throws \SmartyException + * @throws \Smarty\Exception */ public function enableSecurity($security_class = null) { - \Smarty_Security::enableSecurity($this, $security_class); + \Smarty\Security::enableSecurity($this, $security_class); return $this; } @@ -845,7 +845,7 @@ class Smarty extends \Smarty\TemplateBase * @param boolean $do_clone flag is Smarty object shall be cloned * * @return \Smarty\Template template object - * @throws \SmartyException + * @throws \Smarty\Exception */ public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true) { @@ -909,7 +909,7 @@ class Smarty extends \Smarty\TemplateBase * @param \Smarty\Template $template * * @return string - * @throws \SmartyException + * @throws \Smarty\Exception */ public function _getTemplateId( $template_name, @@ -1224,9 +1224,6 @@ class Smarty extends \Smarty\TemplateBase /** * Empty cache for a specific template * - * @api Smarty::clearCache() - * @link https://www.smarty.net/docs/en/api.clear.cache.tpl - * * @param string $template_name template name * @param string $cache_id cache id * @param string $compile_id compile id @@ -1234,7 +1231,10 @@ class Smarty extends \Smarty\TemplateBase * @param string $type resource type * * @return int number of cache files deleted - * @throws \SmartyException + * @throws \Smarty\Exception + *@link https://www.smarty.net/docs/en/api.clear.cache.tpl + * + * @api Smarty::clearCache() */ public function clearCache( $template_name, @@ -1271,15 +1271,15 @@ class Smarty extends \Smarty\TemplateBase /** * Delete compiled template file * - * @api Smarty::clearCompiledTemplate() - * @link https://www.smarty.net/docs/en/api.clear.compiled.template.tpl - * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * * @return int number of template files deleted - * @throws \SmartyException + * @throws \Smarty\Exception + *@link https://www.smarty.net/docs/en/api.clear.compiled.template.tpl + * + * @api Smarty::clearCompiledTemplate() */ public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null) { @@ -1511,7 +1511,7 @@ class Smarty extends \Smarty\TemplateBase * @param string $content * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function cacheModifiedCheck(Template\Cached $cached, \Smarty\Template $_template, $content) { @@ -1572,7 +1572,7 @@ class Smarty extends \Smarty\TemplateBase * @param Template $template template object * * @return string the filtered content - *@throws SmartyException + *@throws Exception */ public function runFilter($type, $content, Template $template) { @@ -1748,8 +1748,8 @@ class Smarty extends \Smarty\TemplateBase * @param string $_filepath complete filepath * @param string $_contents file content * - * @throws SmartyException * @return boolean true + *@throws Exception */ public function writeFile($_filepath, $_contents) { @@ -1768,7 +1768,7 @@ class Smarty extends \Smarty\TemplateBase clearstatcache(); if (++$i === 3) { error_reporting($_error_reporting); - throw new SmartyException("unable to create directory {$_dirpath}"); + throw new Exception("unable to create directory {$_dirpath}"); } sleep(1); } @@ -1777,7 +1777,7 @@ class Smarty extends \Smarty\TemplateBase $_tmp_file = $_dirpath . DIRECTORY_SEPARATOR . str_replace(array('.', ','), '_', uniqid('wrt', true)); if (!file_put_contents($_tmp_file, $_contents)) { error_reporting($_error_reporting); - throw new SmartyException("unable to write file {$_tmp_file}"); + throw new Exception("unable to write file {$_tmp_file}"); } /* * Windows' rename() fails if the destination exists, @@ -1807,7 +1807,7 @@ class Smarty extends \Smarty\TemplateBase } if (!$success) { error_reporting($_error_reporting); - throw new SmartyException("unable to write file {$_filepath}"); + throw new Exception("unable to write file {$_filepath}"); } // set file permissions @chmod($_filepath, 0666 & ~umask()); @@ -1844,7 +1844,7 @@ class Smarty extends \Smarty\TemplateBase return $this->runtimes[$type] = new Smarty\Runtime\TplFunctionRuntime(); } - throw new \SmartyException('Trying to load invalid runtime ' . $type); + throw new \Smarty\Exception('Trying to load invalid runtime ' . $type); } /** @@ -1858,7 +1858,7 @@ class Smarty extends \Smarty\TemplateBase try { $this->getRuntime($type); return true; - } catch (\SmartyException $e) { + } catch (\Smarty\Exception $e) { return false; } } diff --git a/src/Template.php b/src/Template.php index d68c00e7..5f15bbf2 100644 --- a/src/Template.php +++ b/src/Template.php @@ -16,7 +16,7 @@ use Smarty\Template\Source; use Smarty\Template\Cached; use Smarty\Template\Compiled; use Smarty\Template\Config; -use SmartyException; +use Smarty\Exception; /** * Main class with template data structures and methods @@ -143,7 +143,7 @@ class Template extends TemplateBase { * seconds * @param bool $_isConfig * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function __construct( $template_resource, @@ -182,7 +182,7 @@ class Template extends TemplateBase { * * @return string * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function render($no_output_filter = true, $display = null) { if ($this->smarty->debugging) { @@ -193,7 +193,7 @@ class Template extends TemplateBase { } // checks if template exists if (!$this->source->exists) { - throw new SmartyException( + throw new Exception( "Unable to load template '{$this->source->type}:{$this->source->name}'" . ($this->_isSubTpl() ? " in '{$this->parent->template_resource}'" : '') ); @@ -273,7 +273,7 @@ class Template extends TemplateBase { * @param string $content_func function name * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function _subTemplateRender( $template, @@ -444,7 +444,7 @@ class Template extends TemplateBase { * @param bool $cache flag if called from cache file * * @return bool flag if compiled or cache file is valid - * @throws \SmartyException + * @throws \Smarty\Exception */ public function _decodeProperties(Template $tpl, $properties, $cache = false) { // on cache resources other than file check version stored in cache code @@ -543,7 +543,7 @@ class Template extends TemplateBase { * Get unique template id * * @return string - * @throws \SmartyException + * @throws \Smarty\Exception */ public function _getTemplateId() { return $this->templateId ?? $this->templateId = @@ -553,10 +553,10 @@ class Template extends TemplateBase { /** * runtime error not matching capture tags * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function capture_error() { - throw new SmartyException("Not matching {capture} open/close in '{$this->template_resource}'"); + throw new Exception("Not matching {capture} open/close in '{$this->template_resource}'"); } /** @@ -602,7 +602,7 @@ class Template extends TemplateBase { /** * Load compiler object * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function loadCompiler() { $this->compiler = @@ -650,7 +650,7 @@ class Template extends TemplateBase { * @param string $property_name property name * * @return mixed|Cached - * @throws SmartyException + * @throws Exception */ public function __get($property_name) { switch ($property_name) { @@ -669,7 +669,7 @@ class Template extends TemplateBase { return $this->smarty->$property_name; } } - throw new SmartyException("template property '$property_name' does not exist."); + throw new Exception("template property '$property_name' does not exist."); } /** @@ -678,7 +678,7 @@ class Template extends TemplateBase { * @param string $property_name property name * @param mixed $value value * - * @throws SmartyException + * @throws Exception */ public function __set($property_name, $value) { switch ($property_name) { @@ -694,7 +694,7 @@ class Template extends TemplateBase { return; } } - throw new SmartyException("invalid template property '$property_name'."); + throw new Exception("invalid template property '$property_name'."); } /** @@ -790,7 +790,7 @@ class Template extends TemplateBase { * configuration * * @return bool - * @throws \SmartyException + * @throws \Smarty\Exception */ public function mustCompile() { if (!$this->source->exists) { @@ -799,7 +799,7 @@ class Template extends TemplateBase { } else { $parent_resource = ''; } - throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}"); + throw new Exception("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}"); } if ($this->mustCompile === null) { $this->mustCompile = (!$this->source->handler->uncompiled && diff --git a/src/Template/Cached.php b/src/Template/Cached.php index ca76b654..1a05bf17 100644 --- a/src/Template/Cached.php +++ b/src/Template/Cached.php @@ -84,7 +84,7 @@ class Cached extends ResourceBase { * * @param Template $_template template object * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function __construct(Template $_template) { $this->compile_id = $_template->compile_id; @@ -282,7 +282,7 @@ class Cached extends ResourceBase { * @param Template $_template * @param bool $no_output_filter * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function updateCache(Template $_template, $no_output_filter) { ob_start(); @@ -315,7 +315,7 @@ class Cached extends ResourceBase { * @param Template $_template * @param bool $no_output_filter * - * @throws \SmartyException + * @throws \Smarty\Exception */ private function removeNoCacheHash(Template $_template, $no_output_filter) { $php_pattern = '/(<%|%>|<\?php|<\?|\?>|)/'; diff --git a/src/Template/Compiled.php b/src/Template/Compiled.php index 4a86981a..5b6060b5 100644 --- a/src/Template/Compiled.php +++ b/src/Template/Compiled.php @@ -88,13 +88,13 @@ class Compiled extends ResourceBase { * @param Template $_template * * @return string - * @throws Exception + * @throws \Smarty\Exception */ public function render(Template $_template) { // checks if template exists if (!$_template->source->exists) { $type = $_template->source->isConfig ? 'config' : 'template'; - throw new SmartyException("Unable to load {$type} '{$_template->source->type}:{$_template->source->name}'"); + throw new \Smarty\Exception("Unable to load {$type} '{$_template->source->type}:{$_template->source->name}'"); } if ($_template->smarty->debugging) { if (!isset($_template->smarty->_debug)) { @@ -200,7 +200,7 @@ class Compiled extends ResourceBase { * @param string $code compiled code * * @return bool success - * @throws \SmartyException + * @throws \Smarty\Exception */ public function write(Template $_template, $code) { if (!$_template->source->handler->recompiled) { diff --git a/src/Template/Config.php b/src/Template/Config.php index 0ab4e6a0..069fa81f 100644 --- a/src/Template/Config.php +++ b/src/Template/Config.php @@ -4,7 +4,7 @@ namespace Smarty\Template; use Smarty; use Smarty\Template; -use SmartyException; +use Smarty\Exception; /** * Smarty Config Resource Data Object @@ -65,7 +65,7 @@ class Config extends Source { * @param string $template_resource resource identifier * * @return Config Source Object - * @throws SmartyException + * @throws Exception */ public static function load( Template $_template = null, @@ -78,13 +78,13 @@ class Config extends Source { $template_resource = $_template->template_resource; } if (empty($template_resource)) { - throw new SmartyException('Source: Missing name'); + throw new Exception('Source: Missing name'); } // parse resource_name, load resource handler [$name, $type] = Smarty\Resource\BasePlugin::parseResourceName($template_resource, $smarty->default_config_type); // make sure configs are not loaded via anything smarty can't handle if (isset($_incompatible_resources[$type])) { - throw new SmartyException("Unable to use resource '{$type}' for config"); + throw new Exception("Unable to use resource '{$type}' for config"); } $source = new Config($smarty, $template_resource, $type, $name); $source->handler->populate($source, $_template); diff --git a/src/Template/ResourceBase.php b/src/Template/ResourceBase.php index bfd961f5..a5ce24fb 100644 --- a/src/Template/ResourceBase.php +++ b/src/Template/ResourceBase.php @@ -116,7 +116,7 @@ abstract class ResourceBase { $unifunc = $this->unifunc; } if (empty($unifunc) || !function_exists($unifunc)) { - throw new \SmartyException("Invalid compiled template for '{$_template->template_resource}'"); + throw new \Smarty\Exception("Invalid compiled template for '{$_template->template_resource}'"); } if ($_template->startRenderCallbacks) { foreach ($_template->startRenderCallbacks as $callback) { diff --git a/src/Template/Source.php b/src/Template/Source.php index bc6d6394..61f44d85 100644 --- a/src/Template/Source.php +++ b/src/Template/Source.php @@ -4,7 +4,7 @@ namespace Smarty\Template; use Smarty; use Smarty\Template; -use SmartyException; +use Smarty\Exception; /** * Smarty Resource Data Object @@ -136,7 +136,7 @@ class Source { * @param string $type type of resource * @param string $name resource name * - * @throws \SmartyException + * @throws \Smarty\Exception * @internal param \Smarty\Resource\Base $handler Resource Handler this source object communicates with */ public function __construct(Smarty $smarty, $resource, $type, $name) { @@ -157,7 +157,7 @@ class Source { * @param string $template_resource resource identifier * * @return Source Source Object - * @throws SmartyException + * @throws Exception */ public static function load( Template $_template = null, @@ -169,7 +169,7 @@ class Source { $template_resource = $_template->template_resource; } if (empty($template_resource)) { - throw new SmartyException('Source: Missing name'); + throw new Exception('Source: Missing name'); } // parse resource_name, load resource handler, identify unique resource name if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]([\s\S]*)$/', $template_resource, $match)) { @@ -207,7 +207,7 @@ class Source { * Get source content * * @return string - * @throws \SmartyException + * @throws \Smarty\Exception */ public function getContent() { return isset($this->content) ? $this->content : $this->handler->getContent($this); @@ -216,7 +216,7 @@ class Source { /** * get default content from template or config resource handler * - * @throws \SmartyException + * @throws \Smarty\Exception */ public function _getDefaultTemplate($default_handler) { $_content = $_timestamp = null; @@ -229,7 +229,7 @@ class Source { if ($this->exists) { $this->timestamp = filemtime($_return); } else { - throw new SmartyException( + throw new Exception( 'Default handler: Unable to load ' . ($this->isConfig ? 'config' : 'template') . " default file '{$_return}' for '{$this->type}:{$this->name}'" @@ -244,7 +244,7 @@ class Source { $this->handler = Smarty\Resource\BasePlugin::load($this->smarty, 'eval'); } else { $this->exists = false; - throw new SmartyException( + throw new Exception( 'Default handler: No ' . ($this->isConfig ? 'config' : 'template') . " default content for '{$this->type}:{$this->name}'" ); diff --git a/src/TemplateBase.php b/src/TemplateBase.php index a99f5cb6..bef82dfa 100644 --- a/src/TemplateBase.php +++ b/src/TemplateBase.php @@ -14,8 +14,8 @@ use Smarty\Cacheresource\Base; use Smarty\Data; use Smarty\Smarty; use Smarty\Template; -use Smarty_Data; -use SmartyException; +use Smarty\DataObject; +use Smarty\Exception; /** * Class with shared smarty/template methods @@ -92,7 +92,7 @@ abstract class TemplateBase extends Data { * @param object $parent next higher level of Smarty variables * * @return string rendered template output - * @throws SmartyException + * @throws Exception * @throws Exception */ public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null) { @@ -109,7 +109,7 @@ abstract class TemplateBase extends Data { * @param object $parent next higher level of Smarty variables * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function display($template = null, $cache_id = null, $compile_id = null, $parent = null) { // display template @@ -127,7 +127,7 @@ abstract class TemplateBase extends Data { * * @return bool cache status * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception * @link https://www.smarty.net/docs/en/api.is.cached.tpl * * @api Smarty::isCached() @@ -147,21 +147,21 @@ abstract class TemplateBase extends Data { * * @return mixed * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ private function _execute($template, $cache_id, $compile_id, $parent, $function) { $smarty = $this->_getSmartyObj(); $saveVars = true; if ($template === null) { if (!$this->_isTplObj()) { - throw new SmartyException($function . '():Missing \'$template\' parameter'); + throw new Exception($function . '():Missing \'$template\' parameter'); } else { $template = $this; } } elseif (is_object($template)) { /* @var Template $template */ if (!isset($template->_objType) || !$template->_isTplObj()) { - throw new SmartyException($function . '():Template object expected'); + throw new Exception($function . '():Template object expected'); } } else { // get template object @@ -262,7 +262,7 @@ abstract class TemplateBase extends Data { * @param mixed $cache_attr caching attributes if any * * @return \Smarty|\Smarty\Template - * @throws \SmartyException + * @throws \Smarty\Exception * @link https://www.smarty.net/docs/en/api.register.plugin.tpl * * @api Smarty::registerPlugin() @@ -270,11 +270,11 @@ abstract class TemplateBase extends Data { public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null) { $smarty = $this->_getSmartyObj(); if (isset($smarty->registered_plugins[$type][$name])) { - throw new SmartyException("Plugin tag '{$name}' already registered"); + throw new Exception("Plugin tag '{$name}' already registered"); } elseif (!is_callable($callback)) { - throw new SmartyException("Plugin '{$name}' not callable"); + throw new Exception("Plugin '{$name}' not callable"); } elseif ($cacheable && $cache_attr) { - throw new SmartyException("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); + throw new Exception("Cannot set caching attributes for plugin '{$name}' when it is cacheable."); } else { $smarty->registered_plugins[$type][$name] = [$callback, (bool)$cacheable, (array)$cache_attr]; } @@ -307,7 +307,7 @@ abstract class TemplateBase extends Data { * @param string $name filter name * * @return bool - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::loadFilter() * @link https://www.smarty.net/docs/en/api.load.filter.tpl * @@ -328,7 +328,7 @@ abstract class TemplateBase extends Data { $smarty->registered_filters[$type][$_filter_name] = $_plugin; return true; } - throw new SmartyException("{$type}filter '{$name}' not found or callable"); + throw new Exception("{$type}filter '{$name}' not found or callable"); } /** @@ -338,7 +338,7 @@ abstract class TemplateBase extends Data { * @param string $name filter name * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::unloadFilter() * * @link https://www.smarty.net/docs/en/api.unload.filter.tpl @@ -367,7 +367,7 @@ abstract class TemplateBase extends Data { * @param string|null $name optional filter name * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @link https://www.smarty.net/docs/en/api.register.filter.tpl * * @api Smarty::registerFilter() @@ -377,7 +377,7 @@ abstract class TemplateBase extends Data { $this->_checkFilterType($type); $name = isset($name) ? $name : $this->_getFilterName($callback); if (!is_callable($callback)) { - throw new SmartyException("{$type}filter '{$name}' not callable"); + throw new Exception("{$type}filter '{$name}' not callable"); } $smarty->registered_filters[$type][$name] = $callback; return $this; @@ -390,7 +390,7 @@ abstract class TemplateBase extends Data { * @param callback|string $callback * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::unregisterFilter() * * @link https://www.smarty.net/docs/en/api.unregister.filter.tpl @@ -421,7 +421,7 @@ abstract class TemplateBase extends Data { * @param array $block_methods list of block-methods * * @return \Smarty|\Smarty\Template - * @throws \SmartyException + * @throws \Smarty\Exception * @link https://www.smarty.net/docs/en/api.register.object.tpl * * @api Smarty::registerObject() @@ -438,7 +438,7 @@ abstract class TemplateBase extends Data { if (!empty($allowed_methods_properties)) { foreach ((array)$allowed_methods_properties as $method) { if (!is_callable([$object, $method]) && !property_exists($object, $method)) { - throw new SmartyException("Undefined method or property '$method' in registered object"); + throw new Exception("Undefined method or property '$method' in registered object"); } } } @@ -446,7 +446,7 @@ abstract class TemplateBase extends Data { if (!empty($block_methods)) { foreach ((array)$block_methods as $method) { if (!is_callable([$object, $method])) { - throw new SmartyException("Undefined method '$method' in registered object"); + throw new Exception("Undefined method '$method' in registered object"); } } } @@ -536,8 +536,8 @@ abstract class TemplateBase extends Data { * variables * @param null $name optional data block name * - * @return Smarty_Data data object - * @throws SmartyException + * @return DataObject data object + * @throws Exception * @api Smarty::createData() * @link https://www.smarty.net/docs/en/api.create.data.tpl * @@ -545,7 +545,7 @@ abstract class TemplateBase extends Data { public function createData(Data $parent = null, $name = null) { /* @var Smarty $smarty */ $smarty = $this->_getSmartyObj(); - $dataObj = new Smarty_Data($parent, $smarty, $name); + $dataObj = new DataObject($parent, $smarty, $name); if ($smarty->debugging) { \Smarty\Debug::register_data($dataObj); } @@ -582,7 +582,7 @@ abstract class TemplateBase extends Data { * @param string $object_name object name * * @return object - * @throws \SmartyException if no such object is found + * @throws \Smarty\Exception if no such object is found * @link https://www.smarty.net/docs/en/api.get.registered.object.tpl * * @api Smarty::getRegisteredObject() @@ -590,10 +590,10 @@ abstract class TemplateBase extends Data { public function getRegisteredObject($object_name) { $smarty = $this->_getSmartyObj(); if (!isset($smarty->registered_objects[$object_name])) { - throw new SmartyException("'$object_name' is not a registered object"); + throw new Exception("'$object_name' is not a registered object"); } if (!is_object($smarty->registered_objects[$object_name][0])) { - throw new SmartyException("registered '$object_name' is not an object"); + throw new Exception("registered '$object_name' is not an object"); } return $smarty->registered_objects[$object_name][0]; } @@ -617,7 +617,7 @@ abstract class TemplateBase extends Data { * to addto add * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::addLiterals() * */ @@ -635,7 +635,7 @@ abstract class TemplateBase extends Data { * to setto set * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::setLiterals() * */ @@ -655,14 +655,14 @@ abstract class TemplateBase extends Data { * @param Smarty $smarty * @param array $literals * - * @throws \SmartyException + * @throws \Smarty\Exception */ private function _setLiterals(Smarty $smarty, $literals) { $literals = array_combine($literals, $literals); $error = isset($literals[$smarty->left_delimiter]) ? [$smarty->left_delimiter] : []; $error = isset($literals[$smarty->right_delimiter]) ? $error[] = $smarty->right_delimiter : $error; if (!empty($error)) { - throw new SmartyException( + throw new Exception( 'User defined literal(s) "' . $error . '" may not be identical with left or right delimiter' ); @@ -675,11 +675,11 @@ abstract class TemplateBase extends Data { * * @param string $type * - * @throws \SmartyException + * @throws \Smarty\Exception */ private function _checkFilterType($type) { if (!isset($this->filterTypes[$type])) { - throw new SmartyException("Illegal filter type '{$type}'"); + throw new Exception("Illegal filter type '{$type}'"); } } @@ -709,7 +709,7 @@ abstract class TemplateBase extends Data { * register * * @return TemplateBase - * @throws \SmartyException + * @throws \Smarty\Exception * @api Smarty::registerClass() * @link https://www.smarty.net/docs/en/api.register.class.tpl * @@ -718,7 +718,7 @@ abstract class TemplateBase extends Data { $smarty = $this->_getSmartyObj(); // test if exists if (!class_exists($class_impl)) { - throw new SmartyException("Undefined class '$class_impl' in register template class"); + throw new Exception("Undefined class '$class_impl' in register template class"); } // register the class $smarty->registered_classes[$class_name] = $class_impl; @@ -766,7 +766,7 @@ abstract class TemplateBase extends Data { * @param callable $callback class/method name * * @return TemplateBase - * @throws SmartyException if $callback is not callable + * @throws Exception if $callback is not callable * @api Smarty::registerDefaultConfigHandler() * */ @@ -775,7 +775,7 @@ abstract class TemplateBase extends Data { if (is_callable($callback)) { $smarty->default_config_handler_func = $callback; } else { - throw new SmartyException('Default config handler not callable'); + throw new Exception('Default config handler not callable'); } return $this; } @@ -786,7 +786,7 @@ abstract class TemplateBase extends Data { * @param callable $callback class/method name * * @return TemplateBase - * @throws SmartyException if $callback is not callable + * @throws Exception if $callback is not callable * @link https://www.smarty.net/docs/en/api.register.default.plugin.handler.tpl * * @api Smarty::registerDefaultPluginHandler() @@ -796,7 +796,7 @@ abstract class TemplateBase extends Data { if (is_callable($callback)) { $smarty->default_plugin_handler_func = $callback; } else { - throw new SmartyException("Default plugin handler '$callback' not callable"); + throw new Exception("Default plugin handler '$callback' not callable"); } return $this; } @@ -807,7 +807,7 @@ abstract class TemplateBase extends Data { * @param callable $callback class/method name * * @return TemplateBase - * @throws SmartyException if $callback is not callable + * @throws Exception if $callback is not callable * @api Smarty::registerDefaultTemplateHandler() * */ @@ -816,7 +816,7 @@ abstract class TemplateBase extends Data { if (is_callable($callback)) { $smarty->default_template_handler_func = $callback; } else { - throw new SmartyException('Default template handler not callable'); + throw new Exception('Default template handler not callable'); } return $this; } @@ -862,14 +862,14 @@ abstract class TemplateBase extends Data { * @param string $tpl_name * * @return TemplateBase - * @throws SmartyException if file is not readable + * @throws Exception if file is not readable * @api Smarty::setDebugTemplate() * */ public function setDebugTemplate($tpl_name) { $smarty = $this->_getSmartyObj(); if (!is_readable($tpl_name)) { - throw new SmartyException("Unknown file '{$tpl_name}'"); + throw new Exception("Unknown file '{$tpl_name}'"); } $smarty->debug_tpl = $tpl_name; return $this; diff --git a/src/UndefinedVariable.php b/src/UndefinedVariable.php new file mode 100644 index 00000000..fed1d9b5 --- /dev/null +++ b/src/UndefinedVariable.php @@ -0,0 +1,33 @@ +dataObjectName = 'Data_object ' . (isset($name) ? "'{$name}'" : self::$count); - $this->smarty = $smarty; - if (is_object($_parent)) { - // when object set up back pointer - $this->parent = $_parent; - } elseif (is_array($_parent)) { - // set up variable values - foreach ($_parent as $_key => $_val) { - $this->tpl_vars[ $_key ] = new \Smarty\Variable($_val); - } - } elseif ($_parent !== null) { - throw new SmartyException('Wrong type for template variables'); - } - } -} diff --git a/src/sysplugins/smarty_security.php b/src/sysplugins/smarty_security.php deleted file mode 100644 index d6347b6b..00000000 --- a/src/sysplugins/smarty_security.php +++ /dev/null @@ -1,682 +0,0 @@ - array('method_1', 'method_2'), // allowed methods listed - * 'class_2' => array(), // all methods of class allowed - * ) - * If set to null none is allowed. - * - * @var array - */ - public $trusted_static_methods = array(); - - /** - * This is an array of trusted static properties. - * If empty access to all static classes and properties is allowed. - * Format: - * array ( - * 'class_1' => array('prop_1', 'prop_2'), // allowed properties listed - * 'class_2' => array(), // all properties of class allowed - * ) - * If set to null none is allowed. - * - * @var array - */ - public $trusted_static_properties = array(); - - /** - * This is an array of trusted PHP functions. - * If empty all functions are allowed. - * To disable all PHP functions set $php_functions = null. - * - * @var array - */ - public $php_functions = array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',); - - /** - * This is an array of trusted PHP modifiers. - * If empty all modifiers are allowed. - * To disable all modifier set $php_modifiers = null. - * - * @var array - */ - public $php_modifiers = array('escape', 'count', 'sizeof', 'nl2br',); - - /** - * This is an array of allowed tags. - * If empty no restriction by allowed_tags. - * - * @var array - */ - public $allowed_tags = array(); - - /** - * This is an array of disabled tags. - * If empty no restriction by disabled_tags. - * - * @var array - */ - public $disabled_tags = array(); - - /** - * This is an array of allowed modifier plugins. - * If empty no restriction by allowed_modifiers. - * - * @var array - */ - public $allowed_modifiers = array(); - - /** - * This is an array of disabled modifier plugins. - * If empty no restriction by disabled_modifiers. - * - * @var array - */ - public $disabled_modifiers = array(); - - /** - * This is an array of disabled special $smarty variables. - * - * @var array - */ - public $disabled_special_smarty_vars = array(); - - /** - * This is an array of trusted streams. - * If empty all streams are allowed. - * To disable all streams set $streams = null. - * - * @var array - */ - public $streams = array('file'); - - /** - * + flag if constants can be accessed from template - * - * @var boolean - */ - public $allow_constants = true; - - /** - * + flag if super globals can be accessed from template - * - * @var boolean - */ - public $allow_super_globals = true; - - /** - * max template nesting level - * - * @var int - */ - public $max_template_nesting = 0; - - /** - * current template nesting level - * - * @var int - */ - private $_current_template_nesting = 0; - - /** - * Cache for $resource_dir lookup - * - * @var array - */ - protected $_resource_dir = array(); - - /** - * Cache for $template_dir lookup - * - * @var array - */ - protected $_template_dir = array(); - - /** - * Cache for $config_dir lookup - * - * @var array - */ - protected $_config_dir = array(); - - /** - * Cache for $secure_dir lookup - * - * @var array - */ - protected $_secure_dir = array(); - - /** - * Cache for $php_resource_dir lookup - * - * @var array - */ - protected $_php_resource_dir = null; - - /** - * Cache for $trusted_dir lookup - * - * @var array - */ - protected $_trusted_dir = null; - - /** - * Cache for include path status - * - * @var bool - */ - protected $_include_path_status = false; - - /** - * Cache for $_include_array lookup - * - * @var array - */ - protected $_include_dir = array(); - - /** - * @param Smarty $smarty - */ - public function __construct($smarty) - { - $this->smarty = $smarty; - } - - /** - * Check if PHP function is trusted. - * - * @param string $function_name - * @param object $compiler compiler object - * - * @return boolean true if function is trusted - */ - public function isTrustedPhpFunction($function_name, $compiler) - { - if (isset($this->php_functions) - && (empty($this->php_functions) || in_array($function_name, $this->php_functions)) - ) { - return true; - } - $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting"); - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if static class is trusted. - * - * @param string $class_name - * @param object $compiler compiler object - * - * @return boolean true if class is trusted - */ - public function isTrustedStaticClass($class_name, $compiler) - { - if (isset($this->static_classes) - && (empty($this->static_classes) || in_array($class_name, $this->static_classes)) - ) { - return true; - } - $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting"); - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if static class method/property is trusted. - * - * @param string $class_name - * @param string $params - * @param object $compiler compiler object - * - * @return boolean true if class method is trusted - */ - public function isTrustedStaticClassAccess($class_name, $params, $compiler) - { - if (!isset($params[ 2 ])) { - // fall back - return $this->isTrustedStaticClass($class_name, $compiler); - } - if ($params[ 2 ] === 'method') { - $allowed = $this->trusted_static_methods; - $name = substr($params[ 0 ], 0, strpos($params[ 0 ], '(')); - } else { - $allowed = $this->trusted_static_properties; - // strip '$' - $name = substr($params[ 0 ], 1); - } - if (isset($allowed)) { - if (empty($allowed)) { - // fall back - return $this->isTrustedStaticClass($class_name, $compiler); - } - if (isset($allowed[ $class_name ]) - && (empty($allowed[ $class_name ]) || in_array($name, $allowed[ $class_name ])) - ) { - return true; - } - } - $compiler->trigger_template_error("access to static class '{$class_name}' {$params[2]} '{$name}' not allowed by security setting"); - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if PHP modifier is trusted. - * - * @param string $modifier_name - * @param object $compiler compiler object - * @deprecated - * @return boolean true if modifier is trusted - */ - public function isTrustedPhpModifier($modifier_name, $compiler) - { - if (isset($this->php_modifiers) - && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers)) - ) { - return true; - } - $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting"); - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if tag is trusted. - * - * @param string $tag_name - * @param object $compiler compiler object - * - * @return boolean true if tag is trusted - */ - public function isTrustedTag($tag_name, $compiler) - { - // check for internal always required tags - if (in_array( - $tag_name, - array( - 'assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', - 'private_object_block_function', 'private_object_function', 'private_registered_function', - 'private_registered_block', 'private_special_variable', 'private_print_expression', - 'private_modifier' - ) - ) - ) { - return true; - } - // check security settings - if (empty($this->allowed_tags)) { - if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) { - return true; - } else { - $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true); - } - } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) { - return true; - } else { - $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true); - } - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if special $smarty variable is trusted. - * - * @param string $var_name - * @param object $compiler compiler object - * - * @return boolean true if tag is trusted - */ - public function isTrustedSpecialSmartyVar($var_name, $compiler) - { - if (!in_array($var_name, $this->disabled_special_smarty_vars)) { - return true; - } else { - $compiler->trigger_template_error( - "special variable '\$smarty.{$var_name}' not allowed by security setting", - null, - true - ); - } - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if modifier plugin is trusted. - * - * @param string $modifier_name - * @param object $compiler compiler object - * - * @return boolean true if tag is trusted - */ - public function isTrustedModifier($modifier_name, $compiler) - { - // check for internal always allowed modifier - if (in_array($modifier_name, array('default'))) { - return true; - } - // check security settings - if (empty($this->allowed_modifiers)) { - if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) { - return true; - } else { - $compiler->trigger_template_error( - "modifier '{$modifier_name}' disabled by security setting", - null, - true - ); - } - } elseif (in_array($modifier_name, $this->allowed_modifiers) - && !in_array($modifier_name, $this->disabled_modifiers) - ) { - return true; - } else { - $compiler->trigger_template_error( - "modifier '{$modifier_name}' not allowed by security setting", - null, - true - ); - } - return false; // should not, but who knows what happens to the compiler in the future? - } - - /** - * Check if constants are enabled or trusted - * - * @param string $const constant name - * @param object $compiler compiler object - * - * @return bool - */ - public function isTrustedConstant($const, $compiler) - { - if (in_array($const, array('true', 'false', 'null'))) { - return true; - } - if (!empty($this->trusted_constants)) { - if (!in_array(strtolower($const), $this->trusted_constants)) { - $compiler->trigger_template_error("Security: access to constant '{$const}' not permitted"); - return false; - } - return true; - } - if ($this->allow_constants) { - return true; - } - $compiler->trigger_template_error("Security: access to constants not permitted"); - return false; - } - - /** - * Check if stream is trusted. - * - * @param string $stream_name - * - * @return boolean true if stream is trusted - * @throws SmartyException if stream is not trusted - */ - public function isTrustedStream($stream_name) - { - if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) { - return true; - } - throw new SmartyException("stream '{$stream_name}' not allowed by security setting"); - } - - /** - * Check if directory of file resource is trusted. - * - * @param string $filepath - * @param null|bool $isConfig - * - * @return bool true if directory is trusted - * @throws \SmartyException if directory is not trusted - */ - public function isTrustedResourceDir($filepath, $isConfig = null) - { - if ($this->_include_path_status !== $this->smarty->use_include_path) { - $_dir = - $this->smarty->use_include_path ? $this->smarty->getIncludePathDirs() : array(); - if ($this->_include_dir !== $_dir) { - $this->_updateResourceDir($this->_include_dir, $_dir); - $this->_include_dir = $_dir; - } - $this->_include_path_status = $this->smarty->use_include_path; - } - $_dir = $this->smarty->getTemplateDir(); - if ($this->_template_dir !== $_dir) { - $this->_updateResourceDir($this->_template_dir, $_dir); - $this->_template_dir = $_dir; - } - $_dir = $this->smarty->getConfigDir(); - if ($this->_config_dir !== $_dir) { - $this->_updateResourceDir($this->_config_dir, $_dir); - $this->_config_dir = $_dir; - } - if ($this->_secure_dir !== $this->secure_dir) { - $this->secure_dir = (array)$this->secure_dir; - foreach ($this->secure_dir as $k => $d) { - $this->secure_dir[ $k ] = $this->smarty->_realpath($d . DIRECTORY_SEPARATOR, true); - } - $this->_updateResourceDir($this->_secure_dir, $this->secure_dir); - $this->_secure_dir = $this->secure_dir; - } - $addPath = $this->_checkDir($filepath, $this->_resource_dir); - if ($addPath !== false) { - $this->_resource_dir = array_merge($this->_resource_dir, $addPath); - } - return true; - } - - /** - * Check if URI (e.g. {fetch} or {html_image}) is trusted - * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}". - * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string" - * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}. - * - * @param string $uri - * - * @return boolean true if URI is trusted - * @throws SmartyException if URI is not trusted - * @uses $trusted_uri for list of patterns to match against $uri - */ - public function isTrustedUri($uri) - { - $_uri = parse_url($uri); - if (!empty($_uri[ 'scheme' ]) && !empty($_uri[ 'host' ])) { - $_uri = $_uri[ 'scheme' ] . '://' . $_uri[ 'host' ]; - foreach ($this->trusted_uri as $pattern) { - if (preg_match($pattern, $_uri)) { - return true; - } - } - } - throw new SmartyException("URI '{$uri}' not allowed by security setting"); - } - - /** - * Remove old directories and its sub folders, add new directories - * - * @param array $oldDir - * @param array $newDir - */ - private function _updateResourceDir($oldDir, $newDir) - { - foreach ($oldDir as $directory) { - // $directory = $this->smarty->_realpath($directory, true); - $length = strlen($directory); - foreach ($this->_resource_dir as $dir) { - if (substr($dir, 0, $length) === $directory) { - unset($this->_resource_dir[ $dir ]); - } - } - } - foreach ($newDir as $directory) { - // $directory = $this->smarty->_realpath($directory, true); - $this->_resource_dir[ $directory ] = true; - } - } - - /** - * Check if file is inside a valid directory - * - * @param string $filepath - * @param array $dirs valid directories - * - * @return array|bool - * @throws \SmartyException - */ - private function _checkDir($filepath, $dirs) - { - $directory = dirname($this->smarty->_realpath($filepath, true)) . DIRECTORY_SEPARATOR; - $_directory = array(); - if (!preg_match('#[\\\\/][.][.][\\\\/]#', $directory)) { - while (true) { - // test if the directory is trusted - if (isset($dirs[ $directory ])) { - return $_directory; - } - // abort if we've reached root - if (!preg_match('#[\\\\/][^\\\\/]+[\\\\/]$#', $directory)) { - // give up - break; - } - // remember the directory to add it to _resource_dir in case we're successful - $_directory[ $directory ] = true; - // bubble up one level - $directory = preg_replace('#[\\\\/][^\\\\/]+[\\\\/]$#', DIRECTORY_SEPARATOR, $directory); - } - } - // give up - throw new SmartyException(sprintf('Smarty Security: not trusted file path \'%s\' ', $filepath)); - } - - /** - * Loads security class and enables security - * - * @param \Smarty $smarty - * @param string|Smarty_Security $security_class if a string is used, it must be class-name - * - * @return \Smarty current Smarty instance for chaining - * @throws \SmartyException when an invalid class name is provided - */ - public static function enableSecurity(Smarty $smarty, $security_class) - { - if ($security_class instanceof Smarty_Security) { - $smarty->security_policy = $security_class; - return $smarty; - } elseif (is_object($security_class)) { - throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security."); - } - if ($security_class === null) { - $security_class = $smarty->security_class; - } - if (!class_exists($security_class)) { - throw new SmartyException("Security class '$security_class' is not defined"); - } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) { - throw new SmartyException("Class '$security_class' must extend Smarty_Security."); - } else { - $smarty->security_policy = new $security_class($smarty); - } - return $smarty; - } - - /** - * Start template processing - * - * @param $template - * - * @throws SmartyException - */ - public function startTemplate($template) - { - if ($this->max_template_nesting > 0 && $this->_current_template_nesting++ >= $this->max_template_nesting) { - throw new SmartyException("maximum template nesting level of '{$this->max_template_nesting}' exceeded when calling '{$template->template_resource}'"); - } - } - - /** - * Exit template processing - */ - public function endTemplate() - { - if ($this->max_template_nesting > 0) { - $this->_current_template_nesting--; - } - } - - /** - * Register callback functions call at start/end of template rendering - * - * @param \Smarty\Template $template - */ - public function registerCallBacks(Template $template) - { - $template->startRenderCallbacks[] = array($this, 'startTemplate'); - $template->endRenderCallbacks[] = array($this, 'endTemplate'); - } -} diff --git a/src/sysplugins/smarty_undefined_variable.php b/src/sysplugins/smarty_undefined_variable.php deleted file mode 100644 index 4dab16aa..00000000 --- a/src/sysplugins/smarty_undefined_variable.php +++ /dev/null @@ -1,33 +0,0 @@ -file = $filename; - } - if ($line) { - $this->line = $line; - } - } - - /** - * @return string - */ - public function __toString() - { - return ' --> Smarty Compiler: ' . $this->message . ' <-- '; - } - - /** - * @param int $line - */ - public function setLine($line) - { - $this->line = $line; - } - - /** - * The template source snippet relating to the error - * - * @type string|null - */ - public $source = null; - - /** - * The raw text of the error message - * - * @type string|null - */ - public $desc = null; - - /** - * The resource identifier or template name - * - * @type string|null - */ - public $template = null; -} diff --git a/src/sysplugins/smartyexception.php b/src/sysplugins/smartyexception.php deleted file mode 100644 index 7f7b9aa4..00000000 --- a/src/sysplugins/smartyexception.php +++ /dev/null @@ -1,19 +0,0 @@ - Smarty: ' . (self::$escape ? htmlentities($this->message) : $this->message) . ' <-- '; - } -} diff --git a/tests/PHPUnit_Smarty.php b/tests/PHPUnit_Smarty.php index 5d194ebd..bc2fb363 100644 --- a/tests/PHPUnit_Smarty.php +++ b/tests/PHPUnit_Smarty.php @@ -4,6 +4,7 @@ * */ +use Smarty\Exception; use Smarty\TemplateBase; use Smarty\Template; @@ -166,7 +167,7 @@ class PHPUnit_Smarty extends PHPUnit\Framework\TestCase PHPUnit_Smarty::$pdo = new PDO(DB_DSN, DB_USER, DB_PASSWD); } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $timezone = date_default_timezone_get(); $j = PHPUnit_Smarty::$pdo->exec("SET time_zone = '{$timezone}';"); diff --git a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php index 26be5df2..bde93205 100644 --- a/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php +++ b/tests/UnitTests/CacheResourceTests/_shared/PHPunitplugins/cacheresource.mysqltest.php @@ -1,5 +1,6 @@ db = PHPUnit_Smarty::$pdo; } catch (PDOException $e) { - throw new SmartyException('Mysql Resource failed: ' . $e->getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, content FROM output_cache WHERE id = :id'); $this->fetchTimestamp = $this->db->prepare('SELECT modified FROM output_cache WHERE id = :id'); diff --git a/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php b/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php index 7c358412..6153d2c1 100644 --- a/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php +++ b/tests/UnitTests/ConfigFileTests/defaultHandler/DefaultConfigHandlerTest.php @@ -32,7 +32,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty */ public function testUnknownConfigFile() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Unable to load config \'file:foo.conf\''); $this->smarty->configLoad('foo.conf'); } @@ -42,7 +42,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty */ public function testRegisterUnknownDefaultConfigHandler() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Default config handler'); $this->expectExceptionMessage('not callable'); $this->smarty->registerDefaultConfigHandler('foo'); @@ -52,7 +52,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty * test default config handler replacement (config data) * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function testDefaultConfigHandlerReplacement() { @@ -65,7 +65,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty * test default config handler replacement (other config file) * * @throws \Exception - * @throws \SmartyException + * @throws \Smarty\Exception */ public function testDefaultConfigHandlerReplacementByConfigFile() { @@ -76,7 +76,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty public function testDefaultConfigHandlerReplacementByConfigFileFail() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage("Unable to load config default file 'no.conf' for 'file:fo.conf'"); $this->smarty->registerDefaultConfigHandler('configHandlerFile'); $this->smarty->configLoad('fo.conf'); @@ -88,7 +88,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty */ public function testDefaultConfigHandlerReplacementReturningFalse() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Unable to load config \'file:foo.conf\''); $this->smarty->configLoad('foo.conf'); } @@ -98,7 +98,7 @@ class DefaultConfigHandlerTest extends PHPUnit_Smarty */ public function testDefaultConfigHandlerReplacementReturningFalse1() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('No config default content for \'file:bla.conf\''); $this->smarty->registerDefaultConfigHandler('configHandlerData'); $this->smarty->configLoad('bla.conf'); diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php index 9d4f5cdd..393fce2d 100644 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php +++ b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqlstest.php @@ -1,4 +1,7 @@ getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); } diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php index be4ad610..b9e55564 100644 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php +++ b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/PHPunitplugins/resource.mysqltest.php @@ -1,4 +1,7 @@ getMessage()); + throw new Exception('Mysql Resource failed: ' . $e->getMessage()); } $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name'); $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name'); diff --git a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/ResourceMysqlPluginTest.php b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/ResourceMysqlPluginTest.php index 6309eff9..a0e7b699 100644 --- a/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/ResourceMysqlPluginTest.php +++ b/tests/UnitTests/ResourceTests/Custom/DemoPluginMysql/ResourceMysqlPluginTest.php @@ -143,7 +143,7 @@ if (MysqlResourceEnable == true) { * */ public function testUnknownTemplate() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Unable to load template \'mysqlstest:foo.tpl\''); $this->assertEquals('foo', $this->smarty->fetch('mysqlstest:foo.tpl')); } diff --git a/tests/UnitTests/ResourceTests/File/FileResourceTest.php b/tests/UnitTests/ResourceTests/File/FileResourceTest.php index c4e8c069..f6528fa9 100644 --- a/tests/UnitTests/ResourceTests/File/FileResourceTest.php +++ b/tests/UnitTests/ResourceTests/File/FileResourceTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\Exception; + /** * class for file resource tests * @@ -69,7 +71,7 @@ class FileResourceTest extends PHPUnit_Smarty */ public function testTemplateFileNotExists3() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Unable to'); $this->expectExceptionMessage('notthere.tpl'); $this->smarty->fetch('notthere.tpl'); @@ -222,7 +224,7 @@ class FileResourceTest extends PHPUnit_Smarty */ public function testRelativeIncludeFail() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('Unable to'); $this->smarty->fetch('relative_sub.tpl'); } @@ -234,7 +236,7 @@ class FileResourceTest extends PHPUnit_Smarty */ public function testRelativeIncludeFailOtherDir() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('./hello.tpl'); $this->smarty->addTemplateDir('./templates_2'); $this->smarty->fetch('relative_notexist.tpl'); @@ -331,7 +333,7 @@ class FileResourceTest extends PHPUnit_Smarty return; } - catch (SmartyException $e) { + catch (Exception $e) { // this was expected to fail } } else { diff --git a/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php b/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php index e5eedcc1..fadd0f73 100644 --- a/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php +++ b/tests/UnitTests/ResourceTests/Stream/StreamResourceTest.php @@ -146,7 +146,7 @@ class StreamResourceTest extends PHPUnit_Smarty public function testTemplateStramNotExists3() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('\'global:notthere\''); $this->smarty->fetch('global:notthere'); } diff --git a/tests/UnitTests/SecurityTests/SecurityTest.php b/tests/UnitTests/SecurityTests/SecurityTest.php index 7631a0a6..e2b6a2f3 100644 --- a/tests/UnitTests/SecurityTests/SecurityTest.php +++ b/tests/UnitTests/SecurityTests/SecurityTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\CompilerException; + /** * class for security test * @@ -27,18 +29,6 @@ class SecurityTest extends PHPUnit_Smarty $this->cleanDirs(); } -/** - * test that security is loaded -' *'/ - public function testSecurityReenable() - { - $this->smarty->disableSecurity(); - $this->smarty->enableSecurity('Security'); - $this->smarty->fetch('helloworld.tpl'); - $this->smarty->disableSecurity(); - $this->smarty->enableSecurity('Security'); - $this->smarty->fetch('helloworld.tpl'); - } /** * test that security is loaded */ @@ -62,7 +52,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedPHPFunction() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('PHP function \'sizeof\' not allowed by security setting'); $this->smarty->security_policy->php_functions = array('null'); $this->smarty->fetch('string:{assign var=foo value=[1,2,3,4,5]}{sizeof($foo)}'); @@ -95,7 +85,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedModifier() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('modifier \'sizeof\' not allowed by security setting'); $this->smarty->security_policy->php_modifiers = array('null'); @$this->smarty->fetch('string:{assign var=foo value=[1,2,3,4,5]}{$foo|@sizeof}'); @@ -128,7 +118,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotAllowedTags2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('tag \'cycle\' not allowed by security setting'); $this->smarty->security_policy->allowed_tags = array('counter'); $this->smarty->fetch('string:{counter}{cycle values="1,2"}'); @@ -141,7 +131,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testDisabledTags() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('tag \'cycle\' disabled by security setting'); $this->smarty->security_policy->disabled_tags = array('cycle'); $this->smarty->fetch('string:{counter}{cycle values="1,2"}'); @@ -171,7 +161,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotAllowedModifier() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('modifier \'lower\' not allowed by security setting'); $this->smarty->security_policy->allowed_modifiers = array('upper'); $this->smarty->fetch('string:{"hello"|upper}{"world"|lower}'); @@ -184,7 +174,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testDisabledModifier() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('modifier \'lower\' disabled by security setting'); $this->smarty->security_policy->disabled_modifiers = array('lower'); $this->smarty->fetch('string:{"hello"|upper}{"world"|lower}'); @@ -235,7 +225,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedDirectory() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('not trusted file path'); $this->smarty->security_policy->secure_dir = array(str_replace('\\', '/', __DIR__ . '/templates_3/')); $this->smarty->fetch('string:{include file="templates_2/hello.tpl"}'); @@ -267,7 +257,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedStaticClass() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('access to static class \'mysecuritystaticclass\' not allowed by security setting'); $this->smarty->security_policy->static_classes = array('null'); $this->smarty->fetch('string:{mysecuritystaticclass::square(5)}'); @@ -278,7 +268,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedStaticClassEval() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('dynamic static class not allowed by security setting'); $this->smarty->security_policy->static_classes = array('null'); $this->smarty->fetch('string:{$test = "mysecuritystaticclass"}{$test::square(5)}'); @@ -289,7 +279,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedStaticClassSmartyVar() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('dynamic static class not allowed by security setting'); $this->smarty->security_policy->static_classes = array('null'); $this->smarty->fetch('string:{$smarty.template_object::square(5)}'); @@ -332,7 +322,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testTemplateNotTrustedStream() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('stream \'global\' not allowed by security setting'); stream_wrapper_register("global", "ResourceStreamSecurity") or die("Failed to register protocol"); @@ -361,7 +351,7 @@ class SecurityTest extends PHPUnit_Smarty */ public function testNotTrustedUri() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('URI \'https://www.smarty.net/docs/en/preface.tpl\' not allowed by security setting'); $this->smarty->security_policy->trusted_uri = array(); $this->assertStringContainsString('Preface | Smarty', $this->smarty->fetch('string:{fetch file="https://www.smarty.net/docs/en/preface.tpl"}')); @@ -371,7 +361,7 @@ class SecurityTest extends PHPUnit_Smarty * In security mode, accessing $smarty.template_object should be illegal. */ public function testSmartyTemplateObject() { - $this->expectException(SmartyCompilerException::class); + $this->expectException(CompilerException::class); $this->smarty->display('string:{$smarty.template_object}'); } @@ -387,10 +377,7 @@ class mysecuritystaticclass return $i * $i; } } -class Security extends Smarty_Security -{ -} #[AllowDynamicProperties] class ResourceStreamSecurity { diff --git a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php b/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php index 4532f6ed..293c9074 100644 --- a/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/BlockPlugin/CompileBlockPluginTest.php @@ -50,7 +50,7 @@ class CompileBlockPluginTest extends PHPUnit_Smarty */ public function testBlockPluginUnknown() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unknown tag \'bar\''); $this->assertEquals("hello world", $this->smarty->fetch('unknown.tpl')); } @@ -76,7 +76,7 @@ class CompileBlockPluginTest extends PHPUnit_Smarty */ public function testBlockPluginRegisteredFunction2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('block tag \'blockplugintest\' not callable'); $this->assertEquals('block test', $this->smarty->fetch('registered.tpl')); } @@ -103,7 +103,7 @@ class CompileBlockPluginTest extends PHPUnit_Smarty */ public function testBlockPluginRegisteredStatic2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('block tag \'blockpluginstatic\' not callable'); $this->assertEquals('static block test', $this->smarty->fetch('registered_static.tpl')); } @@ -130,7 +130,7 @@ class CompileBlockPluginTest extends PHPUnit_Smarty */ public function testBlockPluginRegisteredMethod2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('block tag \'blockpluginmethod\' not callable'); $this->assertEquals('method block test', $this->smarty->fetch('registered_method.tpl')); } @@ -157,7 +157,7 @@ class CompileBlockPluginTest extends PHPUnit_Smarty */ public function testBlockPluginRegisteredObject2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('block tag \'myobject\' not callable'); $this->assertEquals('object block test', $this->smarty->fetch('registered_object.tpl')); } diff --git a/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php b/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php index a21ec2a1..9d39fa87 100644 --- a/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php @@ -759,7 +759,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyBlockChildOutsideBlock_025() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('\'{$smarty.block.child}\' used outside {block} tags'); $this->smarty->fetch('025_parent.tpl'); } @@ -769,7 +769,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyBlockParentOutsideBlock_026() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('\'{$smarty.block.parent}\' used outside {block} tags'); $this->smarty->fetch('026_child.tpl'); } @@ -779,7 +779,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyBlockParentInParent_027() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('illegal \'{$smarty.block.parent}\''); $this->smarty->fetch('027_parent.tpl'); } @@ -1149,7 +1149,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyBlockWrongBlockName_036() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('$smarty.block is not defined'); $this->smarty->fetch('036_parent.tpl'); } @@ -1158,7 +1158,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyParentBlockCalledInParent_036_1() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('inheritance: illegal \'{$smarty.block.parent}\' used in child template'); $this->smarty->fetch('036_1_parent.tpl'); } @@ -1167,7 +1167,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyParentBlockCalledInParent_036_2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('inheritance: illegal \'{block_parent}\' used in child template'); $this->smarty->fetch('036_2_parent.tpl'); } @@ -1176,7 +1176,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyParentBlockCalledInParent_036_3() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('inheritance: illegal \'{parent}\' used in child template'); $this->smarty->fetch('036_3_parent.tpl'); } @@ -1186,7 +1186,7 @@ class CompileBlockExtendsTest extends PHPUnit_Smarty */ public function testSmartyBlockMissigBlockName_037() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('$smarty.block is not defined'); $this->smarty->fetch('037_parent.tpl'); } diff --git a/tests/UnitTests/TemplateSource/TagTests/ConfigLoad/CompileConfigLoadTest.php b/tests/UnitTests/TemplateSource/TagTests/ConfigLoad/CompileConfigLoadTest.php index 18cbe4cb..971e7c20 100644 --- a/tests/UnitTests/TemplateSource/TagTests/ConfigLoad/CompileConfigLoadTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/ConfigLoad/CompileConfigLoadTest.php @@ -165,7 +165,7 @@ class CompileConfigLoadTest extends PHPUnit_Smarty */ public function testConfigSyntaxError_009() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('Syntax error in config file'); $this->smarty->fetch('009_error.tpl'); } diff --git a/tests/UnitTests/TemplateSource/TagTests/Delimiter/CompileDelimiterTest.php b/tests/UnitTests/TemplateSource/TagTests/Delimiter/CompileDelimiterTest.php index 1100beb8..7125199a 100644 --- a/tests/UnitTests/TemplateSource/TagTests/Delimiter/CompileDelimiterTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/Delimiter/CompileDelimiterTest.php @@ -43,7 +43,7 @@ class CompileDelimiterTest extends PHPUnit_Smarty public function testLeftDelimiterError() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('nocache option not allowed'); $tpl = $this->smarty->createTemplate('string:x{ldelim nocache}x'); $this->assertEquals('x{x', $this->smarty->fetch($tpl)); diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionFetchTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionFetchTest.php index 13e9f3a3..f5f56b56 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionFetchTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/PluginFunctionFetchTest.php @@ -45,7 +45,7 @@ class PluginFunctionFetchTest extends PHPUnit_Smarty */ public function testFetchInvalidUri() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('{fetch} cannot read resource \'https://foo.smarty.net/foo.dat\''); $this->smarty->fetch('string:{fetch file="https://foo.smarty.net/foo.dat"}'); } @@ -58,7 +58,7 @@ class PluginFunctionFetchTest extends PHPUnit_Smarty */ public function testFetchSecurity() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('not trusted file path'); $this->cleanDirs(); $dir=$this->smarty->getTemplateDir(); @@ -73,7 +73,7 @@ class PluginFunctionFetchTest extends PHPUnit_Smarty */ public function testFetchSecurity2() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('not trusted file path'); $this->cleanDirs(); $this->smarty->getTemplateDir(); diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php index 5cd13dd3..40d48a74 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php @@ -19,7 +19,7 @@ class PluginModifierExplodeTest extends \PHPUnit_Smarty /** * @return void - * @throws \SmartyException + * @throws \Smarty\Exception * * @dataProvider explodeDataProvider */ diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php index 27feeac3..b01b2bca 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierNumberFormatTest.php @@ -18,7 +18,7 @@ class PluginModifierNumberFormatTest extends \PHPUnit_Smarty /** * @return void - * @throws \SmartyException + * @throws \Smarty\Exception * * @dataProvider numberFormatDataProvider */ diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php index a4b6a12e..e5c8f7be 100644 --- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierReplaceTest.php @@ -18,7 +18,7 @@ class PluginModifierReplaceTest extends \PHPUnit_Smarty /** * @return void - * @throws \SmartyException + * @throws \Smarty\Exception * * @dataProvider replaceDataProvider */ diff --git a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php index 6d1da7f5..bb4a0f6b 100644 --- a/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/TemplateFunction/CompileFunctionTest.php @@ -6,6 +6,8 @@ * @author Uwe Tews */ +use Smarty\CompilerException; + /** * class for {function} tag tests * @@ -437,7 +439,7 @@ class CompileFunctionTest extends PHPUnit_Smarty * Test handling of function names that are a security risk */ public function testIllegalFunctionName() { - $this->expectException(SmartyCompilerException::class); + $this->expectException(CompilerException::class); $this->smarty->fetch('string:{function name=\'rce(){};echo "hi";function \'}{/function}'); } diff --git a/tests/UnitTests/TemplateSource/TagTests/_Attributes/AttributeTest.php b/tests/UnitTests/TemplateSource/TagTests/_Attributes/AttributeTest.php index c6778469..8372e6ac 100644 --- a/tests/UnitTests/TemplateSource/TagTests/_Attributes/AttributeTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/_Attributes/AttributeTest.php @@ -32,7 +32,7 @@ class AttributeTest extends PHPUnit_Smarty */ public function testRequiredAttributeVar() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('missing \'var\' attribute'); $this->smarty->fetch('string:{assign value=1}'); } @@ -42,7 +42,7 @@ class AttributeTest extends PHPUnit_Smarty */ public function testUnexpectedAttribute() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unexpected \'bar\' attribute'); $this->smarty->fetch('string:{assign var=foo value=1 bar=2}'); } @@ -52,7 +52,7 @@ class AttributeTest extends PHPUnit_Smarty */ public function testIllegalOptionValue() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('for option flag \'nocache\''); $this->expectExceptionMessage('illegal value'); $this->smarty->fetch('string:{assign var=foo value=1 nocache=buh}'); @@ -63,7 +63,7 @@ class AttributeTest extends PHPUnit_Smarty */ public function testTooManyShorthands() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('too many shorthand attributes'); $this->smarty->fetch('string:{assign foo 1 2}'); } diff --git a/tests/UnitTests/TemplateSource/TagTests/_Error/CompileErrorTest.php b/tests/UnitTests/TemplateSource/TagTests/_Error/CompileErrorTest.php index 05e1e160..a2905665 100644 --- a/tests/UnitTests/TemplateSource/TagTests/_Error/CompileErrorTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/_Error/CompileErrorTest.php @@ -30,7 +30,7 @@ class CompileErrorTest extends PHPUnit_Smarty */ public function testNoneExistingTemplateError() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('no.tpl'); $this->smarty->fetch('eval:{include file=\'no.tpl\'}'); } @@ -40,7 +40,7 @@ class CompileErrorTest extends PHPUnit_Smarty */ public function testUnknownTagError() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unknown tag \'unknown\''); $this->smarty->fetch('eval:{unknown}'); } @@ -50,7 +50,7 @@ class CompileErrorTest extends PHPUnit_Smarty */ public function testUnclosedTagError() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unclosed {if} tag'); $this->smarty->fetch('eval:{if true}'); } @@ -60,7 +60,7 @@ class CompileErrorTest extends PHPUnit_Smarty */ public function testSyntaxError() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('Unexpected "}"'); $this->smarty->fetch('eval:{assign var=}'); } diff --git a/tests/UnitTests/TemplateSource/ValueTests/DoubleQuoted/DoubleQuotedStringTest.php b/tests/UnitTests/TemplateSource/ValueTests/DoubleQuoted/DoubleQuotedStringTest.php index b6830230..6db86142 100644 --- a/tests/UnitTests/TemplateSource/ValueTests/DoubleQuoted/DoubleQuotedStringTest.php +++ b/tests/UnitTests/TemplateSource/ValueTests/DoubleQuoted/DoubleQuotedStringTest.php @@ -84,7 +84,7 @@ class DoubleQuotedStringTest extends PHPUnit_Smarty */ public function testDoubleQuotedUnclosedBlock_001() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unclosed \'{if}\' in doubled quoted string'); $this->smarty->fetch('001_unclosedBlock.tpl'); } diff --git a/tests/UnitTests/TemplateSource/ValueTests/Modifier/ModifierTest.php b/tests/UnitTests/TemplateSource/ValueTests/Modifier/ModifierTest.php index f783f63b..c2551c0f 100644 --- a/tests/UnitTests/TemplateSource/ValueTests/Modifier/ModifierTest.php +++ b/tests/UnitTests/TemplateSource/ValueTests/Modifier/ModifierTest.php @@ -100,7 +100,7 @@ class ModifierTest extends PHPUnit_Smarty */ public function testUnknownModifier() { - $this->expectException('SmartyCompilerException'); + $this->expectException(\Smarty\CompilerException::class); $this->expectExceptionMessage('unknown modifier \'unknown\''); $this->smarty->fetch('eval:{"hello world"|unknown}'); } diff --git a/tests/UnitTests/TemplateSource/ValueTests/SmartySpecialVars/Error/SmartyErrorTest.php b/tests/UnitTests/TemplateSource/ValueTests/SmartySpecialVars/Error/SmartyErrorTest.php index 7b7bbd68..5cf5a832 100644 --- a/tests/UnitTests/TemplateSource/ValueTests/SmartySpecialVars/Error/SmartyErrorTest.php +++ b/tests/UnitTests/TemplateSource/ValueTests/SmartySpecialVars/Error/SmartyErrorTest.php @@ -28,7 +28,7 @@ class SmartyErrorTest extends PHPUnit_Smarty * test undefined Smarty special variable */ public function testSmartyError() { - $this->expectException('SmartyException'); + $this->expectException(\Smarty\Exception::class); $this->expectExceptionMessage('$smarty.foo is not defined'); $this->assertEquals(\Smarty\Smarty::SMARTY_VERSION, $this->smarty->fetch('error.tpl')); } diff --git a/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php b/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php index 39a99240..2918bbea 100644 --- a/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php +++ b/tests/UnitTests/__shared/PHPunitplugins/function.checkconfigvar.php @@ -6,6 +6,7 @@ * @subpackage PHPunitPlugin */ +use Smarty\DataObject; use Smarty\Template; /** @@ -30,7 +31,7 @@ function smarty_function_checkconfigvar($params, $template) $output .= "#{$ptr->source->name}:\${$var} ="; $output .= isset($ptr->config_vars[$var]) ? preg_replace('/\s/', '', var_export($ptr->config_vars[$var], true)) : 'null'; $ptr = $ptr->parent; - } elseif (in_array('data', $types) && $ptr instanceof Smarty_Data) { + } elseif (in_array('data', $types) && $ptr instanceof DataObject) { $output .= "#data:\${$var} ="; $output .= isset($ptr->config_vars[$var]) ? preg_replace('/\s/', '', var_export($ptr->config_vars[$var], true)) : 'null'; $ptr = $ptr->parent; diff --git a/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php b/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php index 81472c27..0c2ef0ac 100644 --- a/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php +++ b/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php @@ -6,6 +6,7 @@ * @subpackage PHPunitPlugin */ +use Smarty\DataObject; use Smarty\Template; /** @@ -36,7 +37,7 @@ function smarty_function_checkvar($params, $template) $i ++; } $ptr = $ptr->parent; - } elseif (in_array('data', $types) && $ptr instanceof Smarty_Data) { + } elseif (in_array('data', $types) && $ptr instanceof DataObject) { $output .= "#data:\${$var} ="; $output .= isset($ptr->tpl_vars[$var]) ? preg_replace('/\s/', '', var_export($ptr->tpl_vars[$var]->value, true)) : '>unassigned<'; $ptr = $ptr->parent;