From 6af4598a2a3f0bba2c56de7c3b2058c3c093ad23 Mon Sep 17 00:00:00 2001 From: mohrt Date: Tue, 19 Jun 2001 20:04:43 +0000 Subject: [PATCH] commit updates, add debug template --- NEWS | 5 +- Smarty.addons.php | 24 +++++ Smarty.class.php | 103 ++++++++++++++++++- demo/templates/debug.tpl | 21 ++++ demo/templates/footer.tpl | 1 + docs.sgml | 209 ++++++++++++++++++++++++++++++++++++-- libs/Smarty.class.php | 103 ++++++++++++++++++- templates/debug.tpl | 21 ++++ templates/footer.tpl | 1 + 9 files changed, 471 insertions(+), 17 deletions(-) create mode 100644 demo/templates/debug.tpl create mode 100644 templates/debug.tpl diff --git a/NEWS b/NEWS index 29673085..6e20d552 100644 --- a/NEWS +++ b/NEWS @@ -1,10 +1,13 @@ + - added regex_replace modifier, documented (Monte) + - added debugging console feature, documented (Monte) + - added custom function assign_debug_info, documented (Monte) - added 'scope' attribute for {config_load}, 'global' is now deprecated but is still supported. (Andrei) - reduced template symbol table pollution by moving config array into the class itself. (Andrei) - fixed a bug with passing quoted arguments to modifiers inside {if} statements. (Andrei, Sam Beckwith) - - added security features for third party template editing. (Monte) + - added security features for third party template editing, documented (Monte) - added assign custom function, documented. (Monte) - fixed bug with template header using version instead of _version. (Monte) - fixed a problem with putting $ followed by numbers inside {strip} and diff --git a/Smarty.addons.php b/Smarty.addons.php index 575c1d5c..82a7fbba 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -177,6 +177,15 @@ function smarty_mod_replace($string, $search, $replace) return str_replace($search, $replace, $string); } +/*======================================================================*\ + Function: smarty_mod_regex_replace + Purpose: regular epxression search/replace +\*======================================================================*/ +function smarty_mod_regex_replace($string, $search, $replace) +{ + return preg_replace($search, $replace, $string); +} + /*======================================================================*\ Function: smarty_mod_strip_tags Purpose: strip html tags from text @@ -598,6 +607,21 @@ function smarty_func_counter() { return true; } +/*======================================================================*\ + Function: smarty_func_assign_debug_info + Purpose: assign debug info to the template +\*======================================================================*/ +function smarty_func_assign_debug_info($args, &$smarty_obj) { + $assigned_vars = $smarty_obj->_tpl_vars; + ksort($assigned_vars); + $included_templates = $smarty_obj->_included_tpls; + sort($included_templates); + $smarty_obj->assign("_debug_keys",array_keys($assigned_vars)); + $smarty_obj->assign("_debug_vals",array_values($assigned_vars)); + $smarty_obj->assign("_debug_tpls",$included_templates); + return true; +} + /* vim: set expandtab: */ ?> diff --git a/Smarty.class.php b/Smarty.class.php index 297deb5f..f03ac825 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -59,7 +59,7 @@ class Smarty var $template_dir = './templates'; // name of directory for templates var $compile_dir = './templates_c'; // name of directory for compiled templates var $config_dir = './configs'; // directory where config files are located - + var $global_assign = array( 'HTTP_SERVER_VARS' => array( 'SCRIPT_NAME' ) ); // variables from the GLOBALS array // that are implicitly assigned @@ -114,7 +114,7 @@ class Smarty var $left_delimiter = '{'; // template tag delimiters. var $right_delimiter = '}'; - + var $compiler_funcs = array( ); @@ -124,7 +124,8 @@ class Smarty 'math' => 'smarty_func_math', 'fetch' => 'smarty_func_fetch', 'counter' => 'smarty_func_counter', - 'assign' => 'smarty_func_assign' + 'assign' => 'smarty_func_assign', + 'assign_debug_info' => 'smarty_func_assign_debug_info' ); var $custom_mods = array( 'lower' => 'strtolower', @@ -136,6 +137,7 @@ class Smarty 'date_format' => 'smarty_mod_date_format', 'string_format' => 'smarty_mod_string_format', 'replace' => 'smarty_mod_replace', + 'regex_replace' => 'smarty_mod_regex_replace', 'strip_tags' => 'smarty_mod_strip_tags', 'default' => 'smarty_mod_default', 'count_characters' => 'smarty_mod_count_characters', @@ -145,6 +147,8 @@ class Smarty ); var $show_info_header = false; // display HTML info header at top of page output + var $show_info_include = true; // display HTML comments at top & bottom of + // each included template var $compiler_class = 'Smarty_Compiler'; // the compiler class used by // Smarty to compile templates @@ -166,6 +170,7 @@ class Smarty var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' var $_version = '1.4.2'; // Smarty version number var $_extract = false; // flag for custom functions + var $_included_tpls = array(); // list of included templates /*======================================================================*\ Function: Smarty @@ -485,8 +490,10 @@ class Smarty \*======================================================================*/ function fetch($tpl_file, $cache_id = null, $display = false) { - global $HTTP_SERVER_VARS; + global $HTTP_SERVER_VARS, $QUERY_STRING, $HTTP_COOKIE_VARS; + $this->_included_tpls[] = $tpl_file; + if ($this->caching) { // cache name = template path + cache_id $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file)); @@ -528,18 +535,30 @@ class Smarty } else { $info_header = ''; } - + // if we just need to display the results, don't perform output // buffering - for speed if ($display && !$this->caching) { echo $info_header; $this->_process_template($tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } } else { ob_start(); echo $info_header; $this->_process_template($tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } $results = ob_get_contents(); ob_end_clean(); } @@ -557,6 +576,72 @@ class Smarty } } +/*======================================================================*\ + Function: _display_debug_info + Purpose: display debugging information +\*======================================================================*/ + function _display_debug_info() + { + asort($this->_debug_tpl); + reset($this->_debug_tpl); + ksort($this->_tpl_vars); + reset($this->_tpl_vars); + echo "\n"; + } + +/*======================================================================*\ + Function: _print_debug_array + Purpose: display debugging information +\*======================================================================*/ + function _print_debug_array($array,$level) + { + foreach($array as $key => $val) { + for($x=0; $x<$level; $x++) { + echo '_smarty_console.document.write("   "); '; + echo "\n"; + } + $search = array('!"!','![\r\t\n]!'); + $replace = array("'",' '); + echo '_smarty_console.document.write("{\$'.$key.'} = '.preg_replace($search,$replace,htmlspecialchars(substr($val,0,50))).'
"); '; + echo "\n"; + if(is_array($val)) { + $this->_print_debug_array($val,$level+1); + } + } + } + /*======================================================================*\ Function: _process_template() Purpose: @@ -751,8 +836,16 @@ class Smarty array_unshift($this->_config, $this->_config[0]); $this->_process_template($_smarty_include_tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } + $this->_included_tpls[] = $_smarty_include_tpl_file; + array_shift($this->_config); } diff --git a/demo/templates/debug.tpl b/demo/templates/debug.tpl new file mode 100644 index 00000000..fad4204d --- /dev/null +++ b/demo/templates/debug.tpl @@ -0,0 +1,21 @@ +{* Smarty *} + +{assign_debug_info} + + diff --git a/demo/templates/footer.tpl b/demo/templates/footer.tpl index e04310fd..25dcaeaf 100644 --- a/demo/templates/footer.tpl +++ b/demo/templates/footer.tpl @@ -1,2 +1,3 @@ +{include file="debug.tpl"} diff --git a/docs.sgml b/docs.sgml index 18bb330f..e05e57a2 100644 --- a/docs.sgml +++ b/docs.sgml @@ -14,7 +14,7 @@
andrei@php.net
- Version 1.4.2 + Version 1.4.3 2001ispi of Lincoln, Inc. @@ -436,16 +436,72 @@ chmod 700 cache modifiers instead. + + $security + + $security true/false, default is false. Security is good for + situations when you have untrusted parties editing the templates + (via ftp for example) and you want to reduce the risk of system + security compromises through the template language. Turning on + security enforces the following rules to the template language, + unless specifially overridden with $security_settings: + + + If $php_handling is set to SMARTY_PHP_ALLOW, this is + implicitly changed to SMARTY_PHP_PASSTHRU + PHP functions are not allowed in IF statements, + except those specified in the $security_settings + templates can only be included from directories + listed in the $secure_dir array + local files can only be fetched from directories + listed in the $secure_dir array using {fetch} + {php}{/php} tags are not allowed + PHP functions are not allowed as modifiers, except + those specified in the $security_settings + + + NOTE: Security features were added to Smarty 1.4.3. + + + + $secure_dir + + This is an array of all local directories that are considered + secure. {include} and {fetch} use this when security is enabled. + + + + $security_settings + + These are used to override or specify the security settings when + security is enabled. These are the possible settings: + + + PHP_HANDLING - true/false. If set to true, the + $php_handling setting is not checked for security. + IF_FUNCS - This is an array of the names of permitted + PHP functions in IF statements. + INCLUDE_ANY - true/false. If set to true, any + template can be included from the file system, regardless of the + $secure_dir list. + PHP_TAGS - true/false. If set to true, {php}{/php} + tags are permitted in the templates. + MODIFIER_FUNCS - This is an array of the names of permitted + PHP functions used as variable modifiers. + + $left_delimiter - This is the left delimiter used by the template language. Default is "{". + This is the left delimiter used by the template language. + Default is "{". $right_delimiter - This is the right delimiter used by the template language. Default is "}". + This is the right delimiter used by the template language. + Default is "}". @@ -523,6 +579,27 @@ $smarty->assign("Address",$address); // passing an associative array $smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); + + + + + assign_debug_info + + This is used to assign debugging data to the template. This is + used exlusively by the debugging console. + + +assign + + +// passing name/value pairs +$smarty->assign("Name","Fred"); +$smarty->assign("Address",$address); + +// passing an associative array +$smarty->assign(array("city" => "Lincoln","state" => "Nebraska")); + @@ -1620,13 +1697,32 @@ Intro = """This is a value that spans more n/a The name of the section to load + + scope + string + no + local + + How the scope of the loaded variables are treated, + which must be one of local, parent or global. local + means variables are loaded into the local template + context. parent means variables are loaded into both + the local context and the parent template that called + it. global means variables are available to all + templates. NOTE: This was added to Smarty 1.4.3. + + global boolean No No - Whether or not variables are global (visible to - parent templates) + + Whether or not variables are visible to the parent + template, same as scope=parent. NOTE: This attribute is + deprecated by the scope attribute, but still supported. + If scope is supplied, this value is ignored. + @@ -2565,7 +2661,10 @@ OUTPUT: assign is used for assigning template variables during the execution - of the template. assign was added to Smarty 1.4.3. + of the template. + + + NOTE: This was added to Smarty 1.4.3. assign @@ -3809,6 +3908,68 @@ OUTPUT: Two Convicts Evade Noose, Jury Hung. two convicts evade noose, jury hung. + + + + + regex_replace + + + + + + + + + + Parameter Position + Type + Required + Default + Description + + + + + 1 + string + Yes + n/a + This is the regular expression to be replaced. + + + 2 + string + Yes + n/a + This is the string of text to replace with. + + + + + + A regular expression search and replace on a variable. Use the + syntax for preg_replace() from the PHP manual. + + + NOTE: This function was added to Smarty 1.4.3. + + +regex_replace + + +{* replace each carriage return, tab & new line with a space *} + +{$articleTitle} +{$articleTitle|regex_replace:"/[\r\t\n]/":" "} + +OUTPUT: + +Infertility unlikely to + be passed on, experts say +Infertility unlikely to be passed on, experts say + + @@ -4142,6 +4303,42 @@ s m o k e r s a r e p. . . + + Debugging Console + + There is a dubugging console included with Smarty. The console informs you + of all the included templates and assigned variables for the current + invocation of the template, very useful information during development! A + template named "debug.tpl" is included with the distribution of Smarty. Put + this template where it can be included from a template within your + application. Include the template somewhere toward the bottom of the page. + If you have a footer template, this would be an excellent place to include + debug.tpl since it will automatically be included at the bottom of every + page that uses that footer. When you load the page, a javascript console + window should pop up and give you the names of all the included templates + and assigned variables for the current page. To disable the debugging + console, simply remove the include statement from your template. + + + NOTE: This feature was added to Smarty 1.4.3. + + +Including Debugging Console + + +footer.tpl +---------- + +{* We will include the debugging console in the footer template. *} +{* debug.tpl is in our default template directory. *} + +{include file="debug.tpl"} +<BODY> +<HTML> + + + + Troubleshooting diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 297deb5f..f03ac825 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -59,7 +59,7 @@ class Smarty var $template_dir = './templates'; // name of directory for templates var $compile_dir = './templates_c'; // name of directory for compiled templates var $config_dir = './configs'; // directory where config files are located - + var $global_assign = array( 'HTTP_SERVER_VARS' => array( 'SCRIPT_NAME' ) ); // variables from the GLOBALS array // that are implicitly assigned @@ -114,7 +114,7 @@ class Smarty var $left_delimiter = '{'; // template tag delimiters. var $right_delimiter = '}'; - + var $compiler_funcs = array( ); @@ -124,7 +124,8 @@ class Smarty 'math' => 'smarty_func_math', 'fetch' => 'smarty_func_fetch', 'counter' => 'smarty_func_counter', - 'assign' => 'smarty_func_assign' + 'assign' => 'smarty_func_assign', + 'assign_debug_info' => 'smarty_func_assign_debug_info' ); var $custom_mods = array( 'lower' => 'strtolower', @@ -136,6 +137,7 @@ class Smarty 'date_format' => 'smarty_mod_date_format', 'string_format' => 'smarty_mod_string_format', 'replace' => 'smarty_mod_replace', + 'regex_replace' => 'smarty_mod_regex_replace', 'strip_tags' => 'smarty_mod_strip_tags', 'default' => 'smarty_mod_default', 'count_characters' => 'smarty_mod_count_characters', @@ -145,6 +147,8 @@ class Smarty ); var $show_info_header = false; // display HTML info header at top of page output + var $show_info_include = true; // display HTML comments at top & bottom of + // each included template var $compiler_class = 'Smarty_Compiler'; // the compiler class used by // Smarty to compile templates @@ -166,6 +170,7 @@ class Smarty var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' var $_version = '1.4.2'; // Smarty version number var $_extract = false; // flag for custom functions + var $_included_tpls = array(); // list of included templates /*======================================================================*\ Function: Smarty @@ -485,8 +490,10 @@ class Smarty \*======================================================================*/ function fetch($tpl_file, $cache_id = null, $display = false) { - global $HTTP_SERVER_VARS; + global $HTTP_SERVER_VARS, $QUERY_STRING, $HTTP_COOKIE_VARS; + $this->_included_tpls[] = $tpl_file; + if ($this->caching) { // cache name = template path + cache_id $cache_tpl_md5 = md5(realpath($this->template_dir.'/'.$tpl_file)); @@ -528,18 +535,30 @@ class Smarty } else { $info_header = ''; } - + // if we just need to display the results, don't perform output // buffering - for speed if ($display && !$this->caching) { echo $info_header; $this->_process_template($tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } } else { ob_start(); echo $info_header; $this->_process_template($tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } $results = ob_get_contents(); ob_end_clean(); } @@ -557,6 +576,72 @@ class Smarty } } +/*======================================================================*\ + Function: _display_debug_info + Purpose: display debugging information +\*======================================================================*/ + function _display_debug_info() + { + asort($this->_debug_tpl); + reset($this->_debug_tpl); + ksort($this->_tpl_vars); + reset($this->_tpl_vars); + echo "\n"; + } + +/*======================================================================*\ + Function: _print_debug_array + Purpose: display debugging information +\*======================================================================*/ + function _print_debug_array($array,$level) + { + foreach($array as $key => $val) { + for($x=0; $x<$level; $x++) { + echo '_smarty_console.document.write("   "); '; + echo "\n"; + } + $search = array('!"!','![\r\t\n]!'); + $replace = array("'",' '); + echo '_smarty_console.document.write("{\$'.$key.'} = '.preg_replace($search,$replace,htmlspecialchars(substr($val,0,50))).'
"); '; + echo "\n"; + if(is_array($val)) { + $this->_print_debug_array($val,$level+1); + } + } + } + /*======================================================================*\ Function: _process_template() Purpose: @@ -751,8 +836,16 @@ class Smarty array_unshift($this->_config, $this->_config[0]); $this->_process_template($_smarty_include_tpl_file, $compile_path); + if($this->show_info_include) { + echo "\n\n"; + } include($compile_path); + if($this->show_info_include) { + echo "\n\n"; + } + $this->_included_tpls[] = $_smarty_include_tpl_file; + array_shift($this->_config); } diff --git a/templates/debug.tpl b/templates/debug.tpl new file mode 100644 index 00000000..fad4204d --- /dev/null +++ b/templates/debug.tpl @@ -0,0 +1,21 @@ +{* Smarty *} + +{assign_debug_info} + + diff --git a/templates/footer.tpl b/templates/footer.tpl index e04310fd..25dcaeaf 100644 --- a/templates/footer.tpl +++ b/templates/footer.tpl @@ -1,2 +1,3 @@ +{include file="debug.tpl"}