* moved config array into class itself

* added 'scope' attribute for config_load
This commit is contained in:
andrey
2001-06-15 18:55:28 +00:00
parent 6d66c568bd
commit 911da5cf03
7 changed files with 93 additions and 35 deletions

6
NEWS
View File

@@ -1,8 +1,12 @@
- 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} - fixed a bug with passing quoted arguments to modifiers inside {if}
statements. (Andrei, Sam Beckwith) statements. (Andrei, Sam Beckwith)
- added security features for third party template editing. (Monte) - added security features for third party template editing. (Monte)
- added assign custom function, documented. (Monte) - added assign custom function, documented. (Monte)
- fixed bug with template header using version instead of _version (Monte) - fixed bug with template header using version instead of _version. (Monte)
- fixed a problem with putting $ followed by numbers inside {strip} and - fixed a problem with putting $ followed by numbers inside {strip} and
{/strip} tags. (Andrei) {/strip} tags. (Andrei)
- fixed Config_File class to allow empty config paths (defaults to current - fixed Config_File class to allow empty config paths (defaults to current

View File

@@ -162,6 +162,7 @@ class Smarty
var $_tpl_vars = array(); // where assigned template vars are kept var $_tpl_vars = array(); // where assigned template vars are kept
var $_sections = array(); // keeps track of sections var $_sections = array(); // keeps track of sections
var $_conf_obj = null; // configuration object var $_conf_obj = null; // configuration object
var $_config = array(); // loaded configuration settings
var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty'
var $_version = '1.4.2'; // Smarty version number var $_version = '1.4.2'; // Smarty version number
var $_extract = false; // flag for custom functions var $_extract = false; // flag for custom functions
@@ -519,6 +520,9 @@ class Smarty
extract($this->_tpl_vars); extract($this->_tpl_vars);
/* Initialize config array. */
$this->_config = array(array());
if ($this->show_info_header) { if ($this->show_info_header) {
$info_header = '<!-- Smarty '.$this->_version.' '.strftime("%Y-%m-%d %H:%M:%S %Z").' -->'."\n\n"; $info_header = '<!-- Smarty '.$this->_version.' '.strftime("%Y-%m-%d %H:%M:%S %Z").' -->'."\n\n";
} else { } else {
@@ -739,15 +743,17 @@ class Smarty
Function: _smarty_include() Function: _smarty_include()
Purpose: called for included templates Purpose: called for included templates
\*======================================================================*/ \*======================================================================*/
function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars, function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars)
&$_smarty_config_parent)
{ {
$_smarty_config = $_smarty_config_parent;
$this->_tpl_vars = array_merge($this->_tpl_vars, $_smarty_include_vars); $this->_tpl_vars = array_merge($this->_tpl_vars, $_smarty_include_vars);
extract($this->_tpl_vars); extract($this->_tpl_vars);
array_unshift($this->_config, $this->_config[0]);
$this->_process_template($_smarty_include_tpl_file, $compile_path); $this->_process_template($_smarty_include_tpl_file, $compile_path);
include($compile_path); include($compile_path);
array_shift($this->_config);
} }
/*======================================================================*\ /*======================================================================*\

View File

@@ -347,20 +347,39 @@ class Smarty_Compiler extends Smarty {
$this->_syntax_error("missing 'file' attribute in config_load tag"); $this->_syntax_error("missing 'file' attribute in config_load tag");
} }
if (!empty($attrs['global']) && $attrs['global']) $scope = $this->_dequote($attrs['scope']);
$update_parent = true; if (!empty($scope)) {
else if ($scope != 'local' &&
$update_parent = false; $scope != 'parent' &&
$scope != 'global') {
var_dump($scope);
$this->_syntax_error("invalid 'scope' attribute value");
}
} else {
if (!empty($attrs['global']) && $attrs['global'])
$scope = 'parent';
else
$scope = 'local';
}
$output = "<?php\n" . $output = "<?php\n" .
"\$_smarty_config = array_merge((array)\$_smarty_config, \$this->_conf_obj->get(".$attrs['file']."));\n"; "\$this->_config[0] = array_merge(\$this->_config[0], \$this->_conf_obj->get(".$attrs['file']."));\n";
if ($update_parent) if ($scope == 'parent')
$output .= "\$_smarty_config_parent = array_merge((array)\$_smarty_config_parent, \$this->_conf_obj->get(".$attrs['file']."));\n"; $output .= "if (count(\$this->_config) > 0)\n" .
" \$this->_config[1] = array_merge(\$this->_config[1], \$this->_conf_obj->get(".$attrs['file']."));\n";
else if ($scope == 'global')
$output .= "for (\$this->_i = 1; \$this->_i < count(\$this->_config); \$this->_i++)\n" .
" \$this->_config[\$this->_i] = array_merge(\$this->_config[\$this->_i], \$this->_conf_obj->get(".$attrs['file']."));\n";
if (!empty($attrs['section'])) { $dq_section = $this->_dequote($attrs['section']);
$output .= "\$_smarty_config = array_merge((array)\$_smarty_config, \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n"; if (!empty($dq_section)) {
if ($update_parent) $output .= "\$this->_config[0] = array_merge(\$this->_config[0], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
$output .= "\$_smarty_config_parent = array_merge((array)\$_smarty_config_parent, \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n"; if ($scope == 'parent')
$output .= "if (count(\$this->_config) > 0)\n" .
" \$this->_config[1] = array_merge(\$this->_config[1], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
else if ($scope == 'global')
$output .= "for (\$this->_i = 1; \$this->_i < count(\$this->_config); \$this->_i++)\n" .
" \$this->_config[\$this->_i] = array_merge(\$this->_config[\$this->_i], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
} }
$output .= '?>'; $output .= '?>';
@@ -393,7 +412,7 @@ class Smarty_Compiler extends Smarty {
return "<?php " . return "<?php " .
"\$_smarty_tpl_vars = \$this->_tpl_vars;\n" . "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
"\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."), \$_smarty_config);\n" . "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" .
"\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
"unset(\$_smarty_tpl_vars); ?>"; "unset(\$_smarty_tpl_vars); ?>";
} }
@@ -793,7 +812,7 @@ class Smarty_Compiler extends Smarty {
$var_name = substr($var_ref, 1, -1); $var_name = substr($var_ref, 1, -1);
$output = "\$_smarty_config['$var_name']"; $output = "\$this->_config[0]['$var_name']";
$this->_parse_modifiers($output, $modifiers); $this->_parse_modifiers($output, $modifiers);
@@ -891,4 +910,6 @@ class Smarty_Compiler extends Smarty {
} }
} }
/* vim: set et: */
?> ?>

View File

@@ -1,4 +1,4 @@
{config_load file=test.conf section="setup"} {config_load file=test.conf section=""}
{include file=header.tpl title=foo} {include file=header.tpl title=foo}
<PRE> <PRE>

View File

@@ -162,6 +162,7 @@ class Smarty
var $_tpl_vars = array(); // where assigned template vars are kept var $_tpl_vars = array(); // where assigned template vars are kept
var $_sections = array(); // keeps track of sections var $_sections = array(); // keeps track of sections
var $_conf_obj = null; // configuration object var $_conf_obj = null; // configuration object
var $_config = array(); // loaded configuration settings
var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty'
var $_version = '1.4.2'; // Smarty version number var $_version = '1.4.2'; // Smarty version number
var $_extract = false; // flag for custom functions var $_extract = false; // flag for custom functions
@@ -519,6 +520,9 @@ class Smarty
extract($this->_tpl_vars); extract($this->_tpl_vars);
/* Initialize config array. */
$this->_config = array(array());
if ($this->show_info_header) { if ($this->show_info_header) {
$info_header = '<!-- Smarty '.$this->_version.' '.strftime("%Y-%m-%d %H:%M:%S %Z").' -->'."\n\n"; $info_header = '<!-- Smarty '.$this->_version.' '.strftime("%Y-%m-%d %H:%M:%S %Z").' -->'."\n\n";
} else { } else {
@@ -739,15 +743,17 @@ class Smarty
Function: _smarty_include() Function: _smarty_include()
Purpose: called for included templates Purpose: called for included templates
\*======================================================================*/ \*======================================================================*/
function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars, function _smarty_include($_smarty_include_tpl_file, $_smarty_include_vars)
&$_smarty_config_parent)
{ {
$_smarty_config = $_smarty_config_parent;
$this->_tpl_vars = array_merge($this->_tpl_vars, $_smarty_include_vars); $this->_tpl_vars = array_merge($this->_tpl_vars, $_smarty_include_vars);
extract($this->_tpl_vars); extract($this->_tpl_vars);
array_unshift($this->_config, $this->_config[0]);
$this->_process_template($_smarty_include_tpl_file, $compile_path); $this->_process_template($_smarty_include_tpl_file, $compile_path);
include($compile_path); include($compile_path);
array_shift($this->_config);
} }
/*======================================================================*\ /*======================================================================*\

View File

@@ -347,20 +347,39 @@ class Smarty_Compiler extends Smarty {
$this->_syntax_error("missing 'file' attribute in config_load tag"); $this->_syntax_error("missing 'file' attribute in config_load tag");
} }
if (!empty($attrs['global']) && $attrs['global']) $scope = $this->_dequote($attrs['scope']);
$update_parent = true; if (!empty($scope)) {
else if ($scope != 'local' &&
$update_parent = false; $scope != 'parent' &&
$scope != 'global') {
var_dump($scope);
$this->_syntax_error("invalid 'scope' attribute value");
}
} else {
if (!empty($attrs['global']) && $attrs['global'])
$scope = 'parent';
else
$scope = 'local';
}
$output = "<?php\n" . $output = "<?php\n" .
"\$_smarty_config = array_merge((array)\$_smarty_config, \$this->_conf_obj->get(".$attrs['file']."));\n"; "\$this->_config[0] = array_merge(\$this->_config[0], \$this->_conf_obj->get(".$attrs['file']."));\n";
if ($update_parent) if ($scope == 'parent')
$output .= "\$_smarty_config_parent = array_merge((array)\$_smarty_config_parent, \$this->_conf_obj->get(".$attrs['file']."));\n"; $output .= "if (count(\$this->_config) > 0)\n" .
" \$this->_config[1] = array_merge(\$this->_config[1], \$this->_conf_obj->get(".$attrs['file']."));\n";
else if ($scope == 'global')
$output .= "for (\$this->_i = 1; \$this->_i < count(\$this->_config); \$this->_i++)\n" .
" \$this->_config[\$this->_i] = array_merge(\$this->_config[\$this->_i], \$this->_conf_obj->get(".$attrs['file']."));\n";
if (!empty($attrs['section'])) { $dq_section = $this->_dequote($attrs['section']);
$output .= "\$_smarty_config = array_merge((array)\$_smarty_config, \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n"; if (!empty($dq_section)) {
if ($update_parent) $output .= "\$this->_config[0] = array_merge(\$this->_config[0], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
$output .= "\$_smarty_config_parent = array_merge((array)\$_smarty_config_parent, \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n"; if ($scope == 'parent')
$output .= "if (count(\$this->_config) > 0)\n" .
" \$this->_config[1] = array_merge(\$this->_config[1], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
else if ($scope == 'global')
$output .= "for (\$this->_i = 1; \$this->_i < count(\$this->_config); \$this->_i++)\n" .
" \$this->_config[\$this->_i] = array_merge(\$this->_config[\$this->_i], \$this->_conf_obj->get(".$attrs['file'].", ".$attrs['section']."));\n";
} }
$output .= '?>'; $output .= '?>';
@@ -393,7 +412,7 @@ class Smarty_Compiler extends Smarty {
return "<?php " . return "<?php " .
"\$_smarty_tpl_vars = \$this->_tpl_vars;\n" . "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
"\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."), \$_smarty_config);\n" . "\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" .
"\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
"unset(\$_smarty_tpl_vars); ?>"; "unset(\$_smarty_tpl_vars); ?>";
} }
@@ -793,7 +812,7 @@ class Smarty_Compiler extends Smarty {
$var_name = substr($var_ref, 1, -1); $var_name = substr($var_ref, 1, -1);
$output = "\$_smarty_config['$var_name']"; $output = "\$this->_config[0]['$var_name']";
$this->_parse_modifiers($output, $modifiers); $this->_parse_modifiers($output, $modifiers);
@@ -891,4 +910,6 @@ class Smarty_Compiler extends Smarty {
} }
} }
/* vim: set et: */
?> ?>

View File

@@ -1,4 +1,4 @@
{config_load file=test.conf section="setup"} {config_load file=test.conf section=""}
{include file=header.tpl title=foo} {include file=header.tpl title=foo}
<PRE> <PRE>