abstract more private functions to plugin directory

This commit is contained in:
mohrt
2003-05-08 20:21:16 +00:00
parent 8d99b837ec
commit 5b67277699
26 changed files with 1502 additions and 1123 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -339,14 +339,14 @@ class Smarty_Compiler extends Smarty {
/* Emit code to load needed plugins. */ /* Emit code to load needed plugins. */
if (count($this->_plugin_info)) { if (count($this->_plugin_info)) {
$plugins_code = '<?php $this->_load_plugins(array('; $plugins_code = "<?php \$_params = array('plugins' => array(";
foreach ($this->_plugin_info as $plugin_type => $plugins) { foreach ($this->_plugin_info as $plugin_type => $plugins) {
foreach ($plugins as $plugin_name => $plugin_info) { foreach ($plugins as $plugin_name => $plugin_info) {
$plugins_code .= "\narray('$plugin_type', '$plugin_name', '$plugin_info[0]', $plugin_info[1], "; $plugins_code .= "\narray('$plugin_type', '$plugin_name', '$plugin_info[0]', $plugin_info[1], ";
$plugins_code .= $plugin_info[2] ? 'true),' : 'false),'; $plugins_code .= $plugin_info[2] ? 'true),' : 'false),';
} }
} }
$plugins_code .= ")); ?>"; $plugins_code .= "));\n\$this->_execute_core_function('load_plugins', \$_params); ?>\n";
$template_header .= $plugins_code; $template_header .= $plugins_code;
$this->_plugin_info = array(); $this->_plugin_info = array();
} }
@@ -765,7 +765,7 @@ class Smarty_Compiler extends Smarty {
$this->_add_plugin('insert', $name, $delayed_loading); $this->_add_plugin('insert', $name, $delayed_loading);
return "<?php echo \$this->_run_insert_handler(array(".implode(', ', (array)$arg_list).")); ?>\n"; return "<?php \$_params = array('args' => array(".implode(', ', (array)$arg_list).")); echo \$this->_execute_core_function('run_insert_handler', \$_params); ?>\n";
} }
/** /**
@@ -804,7 +804,8 @@ class Smarty_Compiler extends Smarty {
$output .= $output .=
"\$_smarty_tpl_vars = \$this->_tpl_vars;\n" . "\$_smarty_tpl_vars = \$this->_tpl_vars;\n" .
"\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\n" . "\$_params = array('smarty_include_tpl_file' => '" . $this->_dequote($include_file) . "', 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."));\n" .
"\$this->_execute_core_function('smarty_include', \$_params);\n" .
"\$this->_tpl_vars = \$_smarty_tpl_vars;\n" . "\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
"unset(\$_smarty_tpl_vars);\n"; "unset(\$_smarty_tpl_vars);\n";
@@ -844,8 +845,7 @@ class Smarty_Compiler extends Smarty {
} }
$output = $output =
"<?php \$this->_smarty_include_php($attrs[file], '$assign_var', $once_var, " . "<?php \$_params = array('smarty_file' => '" . $this->_dequote($attrs['file']) . "', 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', (array)$arg_list).")); \$this->_execute_core_function('smarty_include_php', \$_params); ?>";
"array(".implode(',', (array)$arg_list).")); ?>";
return $output; return $output;
} }
@@ -1797,7 +1797,8 @@ class Smarty_Compiler extends Smarty {
foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
if ($prefilter === false) { if ($prefilter === false) {
unset($this->_plugins['prefilter'][$filter_name]); unset($this->_plugins['prefilter'][$filter_name]);
$this->_load_plugins(array(array('prefilter', $filter_name, null, null, false))); $_params = array('plugins' => array(array('prefilter', $filter_name, null, null, false)));
$this->_execute_core_function('load_plugins', $_params);
} }
} }
} }
@@ -1805,7 +1806,8 @@ class Smarty_Compiler extends Smarty {
foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
if ($postfilter === false) { if ($postfilter === false) {
unset($this->_plugins['postfilter'][$filter_name]); unset($this->_plugins['postfilter'][$filter_name]);
$this->_load_plugins(array(array('postfilter', $filter_name, null, null, false))); $_params = array('plugins' => array(array('postfilter', $filter_name, null, null, false)));
$this->_execute_core_function('load_plugins', $_params);
} }
} }
} }

View File

@@ -0,0 +1,62 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* called to compile the templates
*
* sets $template_compiled to the compiled template
* @param string $tpl_file
* @param string $template_source
* @param string $template_compiled
* @return boolean
*/
// $tpl_file, $template_source, &$template_compiled
function smarty_core_compile_template(&$params, &$this)
{
if(file_exists(SMARTY_DIR . $this->compiler_file)) {
require_once SMARTY_DIR . $this->compiler_file;
} else {
// use include_path
require_once $this->compiler_file;
}
$smarty_compiler = new $this->compiler_class;
$smarty_compiler->template_dir = $this->template_dir;
$smarty_compiler->compile_dir = $this->compile_dir;
$smarty_compiler->plugins_dir = $this->plugins_dir;
$smarty_compiler->config_dir = $this->config_dir;
$smarty_compiler->force_compile = $this->force_compile;
$smarty_compiler->caching = $this->caching;
$smarty_compiler->php_handling = $this->php_handling;
$smarty_compiler->left_delimiter = $this->left_delimiter;
$smarty_compiler->right_delimiter = $this->right_delimiter;
$smarty_compiler->_version = $this->_version;
$smarty_compiler->security = $this->security;
$smarty_compiler->secure_dir = $this->secure_dir;
$smarty_compiler->security_settings = $this->security_settings;
$smarty_compiler->trusted_dir = $this->trusted_dir;
$smarty_compiler->_reg_objects = &$this->_reg_objects;
$smarty_compiler->_plugins = &$this->_plugins;
$smarty_compiler->_tpl_vars = &$this->_tpl_vars;
$smarty_compiler->default_modifiers = $this->default_modifiers;
$smarty_compiler->compile_id = $this->_compile_id;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled)) {
return true;
} else {
$this->trigger_error($smarty_compiler->_error_msg);
return false;
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,59 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* create full directory structure
*
* @param string $dir
*/
// $dir
function smarty_core_create_dir_structure($params, &$this)
{
if (!file_exists($params['dir'])) {
$_new_dir = (preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $params['dir']))
? DIRECTORY_SEPARATOR : getcwd().DIRECTORY_SEPARATOR;
$_dir_parts = preg_split('!\\' . DIRECTORY_SEPARATOR . '+!', $params['dir'], -1, PREG_SPLIT_NO_EMPTY);
// do not attempt to test or make directories outside of open_basedir
$_open_basedir_ini = ini_get('open_basedir');
if(!empty($_open_basedir_ini)) {
$_use_open_basedir = true;
$_open_basedir_sep = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') ? ';' : ':';
$_open_basedirs = explode($_open_basedir_sep, $_open_basedir_ini);
} else {
$_use_open_basedir = false;
}
foreach ($_dir_parts as $_dir_part) {
$_new_dir .= $_dir_part;
if ($_use_open_basedir) {
$_make_new_dir = false;
foreach ($_open_basedirs as $_open_basedir) {
if (substr($_new_dir.'/', 0, strlen($_open_basedir)) == $_open_basedir) {
$_make_new_dir = true;
break;
}
}
} else {
$_make_new_dir = true;
}
if ($_make_new_dir && !file_exists($_new_dir) && !@mkdir($_new_dir, $this->_dir_perms)) {
$this->trigger_error("problem creating directory '" . $params['dir'] . "'");
return false;
}
$_new_dir .= DIRECTORY_SEPARATOR;
}
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,93 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* fetch the template info. Gets timestamp, and source
* if get_source is true
*
* sets $template_source to the source of the template, and
* $template_timestamp to its time stamp
* @param string $tpl_path
* @param string $template_source
* @param integer $template_timestamp
* @param boolean $get_source
* @param boolean $quiet
* @return boolean
*/
// $tpl_path, &$template_source, &$template_timestamp, $get_source = true, $quiet = false
function smarty_core_fetch_template_info(&$params, &$this)
{
if(!isset($params['get_source'])) { $params['get_source'] = true; }
if(!isset($params['quiet'])) { $params['quiet'] = false; }
$_return = false;
$_params = array('file_base_path' => $this->template_dir,
'file_path' => $params['tpl_path']) ;
if ($this->_execute_core_function('parse_file_path', $_params)) {
$_resource_type = $_params['resource_type'];
$_resource_name = $_params['resource_name'];
switch ($_resource_type) {
case 'file':
if ($params['get_source']) {
$params['template_source'] = $this->_read_file($_resource_name);
}
$params['template_timestamp'] = filemtime($_resource_name);
$_return = true;
break;
default:
// call resource functions to fetch the template source and timestamp
if ($params['get_source']) {
$_source_return = isset($this->_plugins['resource'][$_resource_type]) &&
call_user_func_array($this->_plugins['resource'][$_resource_type][0][0],
array($_resource_name, &$params['template_source'], &$this));
} else {
$_source_return = true;
}
$_timestamp_return = isset($this->_plugins['resource'][$_resource_type]) &&
call_user_func_array($this->_plugins['resource'][$_resource_type][0][1],
array($_resource_name, &$params['template_timestamp'], &$this));
$_return = $_source_return && $_timestamp_return;
break;
}
}
if (!$_return) {
// see if we can get a template with the default template handler
if (!empty($this->default_template_handler_func)) {
if (!$this->_plugin_implementation_exists($this->default_template_handler_func)) {
$this->trigger_error("default template handler function \"$this->default_template_handler_func\" doesn't exist.");
} else {
$_return = call_user_func_array(
$this->default_template_handler_func,
array($_resource_type, $_resource_name, &$params['template_source'], &$params['template_timestamp'], &$this));
}
}
}
if (!$_return) {
if (!$params['quiet']) {
$this->trigger_error('unable to read template resource: "' . $params['tpl_path'] . '"');
}
} else if ($_return && $this->security && !$this->_execute_core_function('is_secure', $_params)) {
if (!$params['quiet'])
$this->trigger_error('(secure mode) accessing "' . $params['tpl_path'] . '" is not allowed');
$params['template_source'] = null;
$params['template_timestamp'] = null;
return false;
}
return $_return;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,44 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Get path to file from include_path
*
* @param string $file_path
* @param string $new_file_path
* @return boolean
* @staticvar array|null
*/
// $file_path, &$new_file_path
function smarty_core_get_include_path(&$params, &$this)
{
static $_path_array = null;
if(!isset($_path_array)) {
$_ini_include_path = ini_get('include_path');
if(strstr($_ini_include_path,';')) {
// windows pathnames
$_path_array = explode(';',$_ini_include_path);
} else {
$_path_array = explode(':',$_ini_include_path);
}
}
foreach ($_path_array as $_include_path) {
if (file_exists($_include_path . DIRECTORY_SEPARATOR . $params['file_path'])) {
$params['new_file_path'] = $_include_path . DIRECTORY_SEPARATOR . $params['file_path'];
return true;
}
}
return false;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,23 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Get seconds and microseconds
* @return double
*/
function smarty_core_get_microtime($params, &$this)
{
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = (double)($mtime[1]) + (double)($mtime[0]);
return ($mtime);
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,78 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Retrieves PHP script resource
*
* sets $php_resource to the returned resource
* @param string $resource
* @param string $resource_type
* @param $php_resource
* @return boolean
*/
function smarty_core_get_php_resource(&$params, &$this)
{
$params['file_base_path'] = $this->trusted_dir;
$this->_execute_core_function('parse_file_path', $params);
/*
* Find out if the resource exists.
*/
if ($params['resource_type'] == 'file') {
$_readable = false;
if(file_exists($params['resource_name']) && is_readable($params['resource_name'])) {
$_readable = true;
} else {
// test for file in include_path
$_params = array('file_path' => $params['resource_name']);
if($this->_execute_core_function('get_include_path', $_params)) {
$_include_path = $_params['new_file_path'];
$_readable = true;
}
}
} else if ($params['resource_type'] != 'file') {
$_template_source = null;
$_readable = $this->_plugin_implementation_exists($this->_plugins['resource'][$params['resource_type']][0][0])
&& call_user_func_array($this->_plugins['resource'][$params['resource_type']][0][0],
array($params['resource_name'], &$_template_source, &$this));
}
/*
* Set the error function, depending on which class calls us.
*/
if (method_exists($this, '_syntax_error')) {
$_error_funcc = '_syntax_error';
} else {
$_error_funcc = 'trigger_error';
}
if ($_readable) {
if ($this->security) {
if (!$this->_execute_core_function('is_trusted',$params)) {
$this->$_error_funcc('(secure mode) ' . $params['resource_type'] . ':' . $params['resource_name'] . ' is not trusted');
return false;
}
}
} else {
$this->$_error_funcc($params['resource_type'] . ':' . $params['resource_name'] . ' is not readable');
return false;
}
if ($params['resource_type'] == 'file') {
$params['php_resource'] = $params['resource_name'];
} else {
$params['php_resource'] = $_template_source;
}
return true;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,48 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* determines if a resource is secure or not.
*
* @param string $resource_type
* @param string $resource_name
* @return boolean
*/
// $resource_type, $resource_name
function smarty_core_is_secure($params, &$this)
{
if (!$this->security || $this->security_settings['INCLUDE_ANY']) {
return true;
}
$_smarty_secure = false;
if ($params['resource_type'] == 'file') {
if (!empty($this->secure_dir)) {
foreach ((array)$this->secure_dir as $curr_dir) {
if ( !empty($curr_dir) && is_readable ($curr_dir)) {
if (substr(realpath($params['resource_name']),0, strlen(realpath($curr_dir))) == realpath($curr_dir)) {
$_smarty_secure = true;
break;
}
}
}
}
} else {
// resource is not on local file system
$_smarty_secure = call_user_func_array(
$this->_plugins['resource'][$params['resource_type']][0][2],
array($params['resource_name'], &$_smarty_secure, &$this));
}
return $_smarty_secure;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,51 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**#@+
* @access private
*/
/**
* determines if a resource is trusted or not
*
* @param string $resource_type
* @param string $resource_name
* @return boolean
*/
// $resource_type, $resource_name
function smarty_core_is_trusted($params, &$this)
{
$_smarty_trusted = false;
if ($params['resource_type'] == 'file') {
if (!empty($this->trusted_dir)) {
// see if template file is within a trusted directory. If so,
// disable security during the execution of the template.
if (!empty($this->trusted_dir)) {
foreach ((array)$this->trusted_dir as $curr_dir) {
if (!empty($curr_dir) && is_readable ($curr_dir)) {
if (substr(realpath($params['resource_name']),0, strlen(realpath($curr_dir))) == realpath($curr_dir)) {
$_smarty_trusted = true;
break;
}
}
}
}
}
} else {
// resource is not on local file system
$_smarty_trusted = call_user_func_array($this->_plugins['resource'][$params['resource_type']][0][3],
array($params['resource_name'], $this));
}
return $_smarty_trusted;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,124 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Load requested plugins
*
* @param array $plugins
*/
// $plugins
function smarty_core_load_plugins($params, &$this)
{
foreach ($params['plugins'] as $_plugin_info) {
list($_type, $_name, $_tpl_file, $_tpl_line, $_delayed_loading) = $_plugin_info;
$_plugin = &$this->_plugins[$_type][$_name];
/*
* We do not load plugin more than once for each instance of Smarty.
* The following code checks for that. The plugin can also be
* registered dynamically at runtime, in which case template file
* and line number will be unknown, so we fill them in.
*
* The final element of the info array is a flag that indicates
* whether the dynamically registered plugin function has been
* checked for existence yet or not.
*/
if (isset($_plugin)) {
if (empty($_plugin[3])) {
if (!$this->_plugin_implementation_exists($_plugin[0])) {
$this->_trigger_fatal_error("[plugin] $_type '$_name' is not implemented", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
} else {
$_plugin[1] = $_tpl_file;
$_plugin[2] = $_tpl_line;
$_plugin[3] = true;
}
}
continue;
} else if ($_type == 'insert') {
/*
* For backwards compatibility, we check for insert functions in
* the symbol table before trying to load them as a plugin.
*/
$_plugin_func = 'insert_' . $_name;
if (function_exists($_plugin_func)) {
$_plugin = array($_plugin_func, $_tpl_file, $_tpl_line, true);
continue;
}
}
$_plugin_file = $this->_get_plugin_filepath($_type, $_name);
if (! $_found = ($_plugin_file != false)) {
$_message = "could not load plugin file '$_type.$_name.php'\n";
}
/*
* If plugin file is found, it -must- provide the properly named
* plugin function. In case it doesn't, simply output the error and
* do not fall back on any other method.
*/
if ($_found) {
include_once $_plugin_file;
$_plugin_func = 'smarty_' . $_type . '_' . $_name;
if (!$this->_plugin_implementation_exists($_plugin_func)) {
$this->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", $_tpl_file, $_tpl_line, __FILE__, __LINE__);
continue;
}
}
/*
* In case of insert plugins, their code may be loaded later via
* 'script' attribute.
*/
else if ($_type == 'insert' && $_delayed_loading) {
$_plugin_func = 'smarty_' . $_type . '_' . $_name;
$_found = true;
}
/*
* Plugin specific processing and error checking.
*/
if (!$_found) {
if ($_type == 'modifier') {
/*
* In case modifier falls back on using PHP functions
* directly, we only allow those specified in the security
* context.
*/
if ($this->security && !in_array($_name, $this->security_settings['MODIFIER_FUNCS'])) {
$_message = "(secure mode) modifier '$_name' is not allowed";
} else {
if (!function_exists($_name)) {
$_message = "modifier '$_name' is not implemented";
} else {
$_plugin_func = $_name;
$_found = true;
}
}
} else if ($_type == 'function') {
/*
* This is a catch-all situation.
*/
$_message = "unknown tag - '$_name'";
}
}
if ($_found) {
$this->_plugins[$_type][$_name] = array($_plugin_func, $_tpl_file, $_tpl_line, true);
} else {
// output error
$this->_trigger_fatal_error('[plugin] ' . $_message, $_tpl_file, $_tpl_line, __FILE__, __LINE__);
}
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,74 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* load a resource plugin
*
* @param string $type
*/
// $type
function smarty_core_load_resource_plugin($params, &$this)
{
/*
* Resource plugins are not quite like the other ones, so they are
* handled differently. The first element of plugin info is the array of
* functions provided by the plugin, the second one indicates whether
* all of them exist or not.
*/
$_plugin = &$this->_plugins['resource'][$params['type']];
if (isset($_plugin)) {
if (!$_plugin[1] && count($_plugin[0])) {
$_plugin[1] = true;
foreach ($_plugin[0] as $_plugin_func) {
if (!$this->_plugin_implementation_exists($_plugin_func)) {
$_plugin[1] = false;
break;
}
}
}
if (!$_plugin[1]) {
$this->_trigger_fatal_error("[plugin] resource '" . $params['type'] . "' is not implemented", null, null, __FILE__, __LINE__);
}
return;
}
$_plugin_file = $this->_get_plugin_filepath('resource', $params['type']);
$_found = ($_plugin_file != false);
if ($_found) { /*
* If the plugin file is found, it -must- provide the properly named
* plugin functions.
*/
include_once $_plugin_file;
/*
* Locate functions that we require the plugin to provide.
*/
$_resource_ops = array('source', 'timestamp', 'secure', 'trusted');
$_resource_funcs = array();
foreach ($_resource_ops as $_op) {
$_plugin_func = 'smarty_resource_' . $params['type'] . '_' . $_op;
if (!function_exists($_plugin_func)) {
$this->_trigger_fatal_error("[plugin] function $_plugin_func() not found in $_plugin_file", null, null, __FILE__, __LINE__);
return;
} else {
$_resource_funcs[] = $_plugin_func;
}
}
$this->_plugins['resource'][$params['type']] = array($_resource_funcs, true);
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,68 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* parse out the type and name from the template resource
*
* @param string $file_base_path
* @param string $file_path
* @param string $resource_type
* @param string $resource_name
* @return boolean
*/
// $file_base_path, $file_path, &$resource_type, &$resource_name
function smarty_core_parse_file_path(&$params, &$this)
{
// split tpl_path by the first colon
$_file_path_parts = explode(':', $params['file_path'], 2);
if (count($_file_path_parts) == 1) {
// no resource type, treat as type "file"
$params['resource_type'] = 'file';
$params['resource_name'] = $_file_path_parts[0];
} else {
$params['resource_type'] = $_file_path_parts[0];
$params['resource_name'] = $_file_path_parts[1];
if ($params['resource_type'] != 'file') {
$_params = array('type' => $params['resource_type']);
$this->_execute_core_function('load_resource_plugin', $_params);
}
}
if ($params['resource_type'] == 'file') {
if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $params['resource_name'])) {
// relative pathname to $params['file_base_path']
// use the first directory where the file is found
$_file_base_path = $params['file_base_path'];
settype($_file_base_path, 'array');
foreach ($_file_base_path as $_curr_path) {
$_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params['resource_name'];
if (file_exists($_fullpath) && is_file($_fullpath)) {
$params['resource_name'] = $_fullpath;
return true;
}
// didn't find the file, try include_path
$_params = array('file_path' => $_fullpath);
if($this->_execute_core_function('get_include_path', $_params)) {
$params['resource_name'] = $_params['new_file_path'];
return true;
}
}
return false;
}
}
// resource type != file
return true;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Replace cached inserts with the actual results
*
* @param string $results
* @return string
*/
function smarty_core_process_cached_inserts($params, &$this)
{
preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis',
$params['results'], $match);
list($cached_inserts, $insert_args) = $match;
for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) {
if ($this->debugging) {
$_params = array();
$debug_start_time = $this->_execute_core_function('get_microtime', $_params);
}
$args = unserialize($insert_args[$i]);
$name = $args['name'];
if (isset($args['script'])) {
$_params = array('file_path' => $this->_dequote($args['script']));
if(!$this->_execute_core_function('get_php_resource', $_params)) {
return false;
}
$resource_type = $_params['resource_type'];
$php_resource = $_params['php_resource'];
if ($resource_type == 'file') {
include_once($php_resource);
} else {
eval($php_resource);
}
}
$function_name = $this->_plugins['insert'][$name][0];
$replace = $function_name($args, $this);
$params['results'] = str_replace($cached_inserts[$i], $replace, $params['results']);
if ($this->debugging) {
$_params = array();
$this->_smarty_debug_info[] = array('type' => 'insert',
'filename' => 'insert_'.$name,
'depth' => $this->_inclusion_depth,
'exec_time' => $this->_execute_core_function('get_microtime', $_params) - $debug_start_time);
}
}
return $params['results'];
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,98 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* read a cache file, determine if it needs to be
* regenerated or not
*
* @param string $tpl_file
* @param string $cache_id
* @param string $compile_id
* @param string $results
* @return boolean
*/
// $tpl_file, $cache_id, $compile_id, &$results
function smarty_core_read_cache_file(&$params, &$this)
{
static $content_cache = array();
if ($this->force_compile) {
// force compile enabled, always regenerate
return false;
}
if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) {
list($params['results'], $this->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']];
return true;
}
if (!empty($this->cache_handler_func)) {
// use cache_handler function
call_user_func_array($this->cache_handler_func,
array('read', &$this, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id']));
} else {
// use local cache file
$_auto_id = $this->_get_auto_id($params['cache_id'], $params['compile_id']);
$_cache_file = $this->_get_auto_filename($this->cache_dir, $params['tpl_file'], $_auto_id);
$params['results'] = $this->_read_file($_cache_file);
}
if (empty($params['results'])) {
// nothing to parse (error?), regenerate cache
return false;
}
$cache_split = explode("\n", $params['results'], 2);
$cache_header = $cache_split[0];
$this->_cache_info = unserialize($cache_header);
if ($this->caching == 2 && isset ($this->_cache_info['expires'])){
// caching by expiration time
if ($this->_cache_info['expires'] > -1 && (time() > $this->_cache_info['expires'])) {
// cache expired, regenerate
return false;
}
} else {
// caching by lifetime
if ($this->cache_lifetime > -1 && (time() - $this->_cache_info['timestamp'] > $this->cache_lifetime)) {
// cache expired, regenerate
return false;
}
}
if ($this->compile_check) {
foreach ($this->_cache_info['template'] as $_template_dep) {
$_params = array('tpl_path' => $_template_dep);
$this->_execute_core_function('fetch_template_info', $_params);
if ($this->_cache_info['timestamp'] < $_params['template_timestamp']) {
// template file has changed, regenerate cache
return false;
}
}
if (isset($this->_cache_info['config'])) {
foreach ($this->_cache_info['config'] as $config_dep) {
if ($this->_cache_info['timestamp'] < filemtime($this->config_dir . DIRECTORY_SEPARATOR . $config_dep)) {
// config file has changed, regenerate cache
return false;
}
}
}
}
$params['results'] = $cache_split[1];
$content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $this->_cache_info);
return true;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,63 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* delete an automagically created file by name and id
*
* @param string $auto_base
* @param string $auto_source
* @param string $auto_id
* @param integer $exp_time
* @return boolean
*/
// $auto_base, $auto_source = null, $auto_id = null, $exp_time = null
function smarty_core_rm_auto($params, &$this)
{
if (!@is_dir($params['auto_base']))
return false;
if(!isset($params['auto_id']) && !isset($params['auto_source'])) {
$_params = array(
'dirname' => $params['auto_base'],
'level' => 0,
'exp_time' => $params['exp_time']
);
$_res = $this->_execute_core_function('rmdir', $_params);
} else {
$_tname = $this->_get_auto_filename($params['auto_base'], $params['auto_source'], $params['auto_id']);
if(isset($params['auto_source'])) {
$_res = $this->_unlink($_tname);
} elseif ($this->use_sub_dirs) {
$_params = array(
'dirname' => $_tname,
'level' => 1,
'exp_time' => $params['exp_time']
);
$_res = $this->_execute_core_function('rmdir', $_params);
} else {
// remove matching file names
$_handle = opendir($params['auto_base']);
$_res = true;
while (false !== ($_filename = readdir($_handle))) {
if($_filename == '.' || $_filename == '..') {
continue;
} elseif (substr($params['auto_base'] . DIRECTORY_SEPARATOR . $_filename, 0, strlen($_tname)) == $_tname) {
$_res &= (bool)$this->_unlink($params['auto_base'] . DIRECTORY_SEPARATOR . $_filename, $params['exp_time']);
}
}
}
}
return $_res;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,58 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* delete a dir recursively (level=0 -> keep root)
* WARNING: no tests, it will try to remove what you tell it!
*
* @param string $dirname
* @param integer $level
* @param integer $exp_time
* @return boolean
*/
// $dirname, $level = 1, $exp_time = null
function smarty_core_rmdir($params, &$this)
{
if(!isset($params['level'])) { $params['level'] = 1; }
if(!isset($params['exp_time'])) { $params['exp_time'] = null; }
if($_handle = @opendir($params['dirname'])) {
while (false !== ($_entry = readdir($_handle))) {
if ($_entry != '.' && $_entry != '..') {
if (@is_dir($params['dirname'] . DIRECTORY_SEPARATOR . $_entry)) {
$_params = array(
'dirname' => $params['dirname'] . DIRECTORY_SEPARATOR . $_entry,
'level' => $params['level'] + 1,
'exp_time' => $params['exp_time']
);
$this->_execute_core_function('rmdir', $_params);
}
else {
$this->_unlink($params['dirname'] . DIRECTORY_SEPARATOR . $_entry, $params['exp_time']);
}
}
}
closedir($_handle);
if ($params['level']) {
@rmdir($params['dirname']);
}
return true;
} else {
return false;
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,68 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Handle insert tags
*
* @param array $args
* @return string
*/
function smarty_core_run_insert_handler($params, &$this)
{
if ($this->debugging) {
$_params = array();
$_debug_start_time = $this->_execute_core_function('get_microtime', $_params);
}
if ($this->caching) {
$_arg_string = serialize($params['args']);
$_name = $params['args']['name'];
if (!isset($this->_cache_info['insert_tags'][$_name])) {
$this->_cache_info['insert_tags'][$_name] = array('insert',
$_name,
$this->_plugins['insert'][$_name][1],
$this->_plugins['insert'][$_name][2],
!empty($params['args']['script']) ? true : false);
}
return $this->_smarty_md5."{insert_cache $_arg_string}".$this->_smarty_md5;
} else {
if (isset($params['args']['script'])) {
$_params = array('file_path' => $this->_dequote($params['args']['script']));
if(!$this->_execute_core_function('get_php_resource', $_params)) {
return false;
}
if ($_params['resource_type'] == 'file') {
include_once($_params['php_resource']);
} else {
eval($_params['php_resource']);
}
unset($params['args']['script']);
}
$_funcname = $this->_plugins['insert'][$params['args']['name']][0];
$_content = $_funcname($params['args'], $this);
if ($this->debugging) {
$_params = array();
$this->_smarty_debug_info[] = array('type' => 'insert',
'filename' => 'insert_'.$params['args']['name'],
'depth' => $this->_inclusion_depth,
'exec_time' => $this->_execute_core_function('get_microtime', $_params) - $_debug_start_time);
}
if (!empty($params['args']["assign"])) {
$this->assign($params['args']["assign"], $_content);
} else {
return $_content;
}
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,58 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* called for included templates
*
* @param string $_smarty_include_tpl_file
* @param string $_smarty_include_vars
*/
// $_smarty_include_tpl_file, $_smarty_include_vars
function smarty_core_smarty_include($params, &$this)
{
if ($this->debugging) {
$_params = array();
$debug_start_time = $this->_execute_core_function('get_microtime', $_params);
$this->_smarty_debug_info[] = array('type' => 'template',
'filename' => $params['smarty_include_tpl_file'],
'depth' => ++$this->_inclusion_depth);
$included_tpls_idx = count($this->_smarty_debug_info) - 1;
}
$this->_tpl_vars = array_merge($this->_tpl_vars, $params['smarty_include_vars']);
// config vars are treated as local, so push a copy of the
// current ones onto the front of the stack
array_unshift($this->_config, $this->_config[0]);
$_smarty_compile_path = $this->_get_compile_path($params['smarty_include_tpl_file']);
if ($this->_process_template($params['smarty_include_tpl_file'], $_smarty_compile_path)) {
include($_smarty_compile_path);
}
// pop the local vars off the front of the stack
array_shift($this->_config);
$this->_inclusion_depth--;
if ($this->debugging) {
// capture time for debugging info
$_params = array();
$this->_smarty_debug_info[$included_tpls_idx]['exec_time'] = $this->_execute_core_function('get_microtime', $_params) - $debug_start_time;
}
if ($this->caching) {
$this->_cache_info['template'][] = $params['smarty_include_tpl_file'];
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,59 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* called for included php files within templates
*
* @param string $_smarty_include_php_file
* @param string $_smarty_assign variable to assign the included template's
* output into
* @param boolean $_smarty_once uses include_once if this is true
* @param array $_smarty_include_vars associative array of vars from
* {include file="blah" var=$var}
*/
// $file, $assign, $once, $_smarty_include_vars
function smarty_core_smarty_include_php($params, &$this)
{
$_params = array('file_path' => $params['smarty_file']);
$this->_execute_core_function('get_php_resource', $_params);
$_smarty_resource_type = $_params['resource_type'];
$_smarty_php_resource = $_params['php_resource'];
extract($params['smarty_include_vars'], EXTR_PREFIX_SAME, 'include_php_');
if (!empty($params['smarty_assign'])) {
ob_start();
if ($_smarty_resource_type == 'file') {
if($params['smarty_once']) {
include_once($_smarty_php_resource);
} else {
include($_smarty_php_resource);
}
} else {
eval($_smarty_php_resource);
}
$this->assign($params['smarty_assign'], ob_get_contents());
ob_end_clean();
} else {
if ($_smarty_resource_type == 'file') {
if($params['smarty_once']) {
include_once($_smarty_php_resource);
} else {
include($_smarty_php_resource);
}
} else {
eval($_smarty_php_resource);
}
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,52 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Prepend the cache information to the cache file
* and write it
*
* @param string $tpl_file
* @param string $cache_id
* @param string $compile_id
* @param string $results
* @return true|null
*/
// $tpl_file, $cache_id, $compile_id, $results
function smarty_core_write_cache_file($params, &$this)
{
// put timestamp in cache header
$this->_cache_info['timestamp'] = time();
if ($this->cache_lifetime > -1){
// expiration set
$this->_cache_info['expires'] = $this->_cache_info['timestamp'] + $this->cache_lifetime;
} else {
// cache will never expire
$this->_cache_info['expires'] = -1;
}
// prepend the cache header info into cache file
$params['results'] = serialize($this->_cache_info)."\n".$params['results'];
if (!empty($this->cache_handler_func)) {
// use cache_handler function
call_user_func_array($this->cache_handler_func,
array('write', &$this, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id']));
} else {
// use local cache file
$_auto_id = $this->_get_auto_id($params['cache_id'], $params['compile_id']);
$_cache_file = $this->_get_auto_filename($this->cache_dir, $params['tpl_file'], $_auto_id);
$_params = array('filename' => $_cache_file, 'contents' => $params['results'], 'create_dirs' => true);
$this->_execute_core_function('write_file', $_params);
return true;
}
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,26 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* write the compiled template
*
* @param string $compile_path
* @param string $template_compiled
* @param integer $template_timestamp
* @return true
*/
function smarty_core_write_compiled_template($params, &$this)
{
$_params = array('filename' => $params['compile_path'], 'contents' => $params['template_compiled'], 'create_dirs' => true);
$this->_execute_core_function('write_file', $_params);
touch($params['compile_path'], $params['template_timestamp']);
return true;
}
/* vim: set expandtab: */
?>

View File

@@ -0,0 +1,47 @@
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* write out a file to disk
*
* @param string $filename
* @param string $contents
* @param boolean $create_dirs
* @return boolean
*/
function smarty_core_write_file($params, &$this)
{
$_dirname = dirname($params['filename']);
if ($params['create_dirs']) {
$_params = array('dir' => $_dirname);
$this->_execute_core_function('create_dir_structure', $_params);
}
// write to tmp file, then rename it to avoid
// file locking race condition
$_tmp_file = $_dirname . '/' . uniqid('');
if (!($fd = @fopen($_tmp_file, 'w'))) {
$this->trigger_error("problem writing temporary file '$_tmp_file'");
return false;
}
fwrite($fd, $params['contents']);
fclose($fd);
if(file_exists($params['filename'])) {
@unlink($params['filename']);
}
@rename($_tmp_file, $params['filename']);
chmod($params['filename'], $this->_file_perms);
return true;
}
/* vim: set expandtab: */
?>

View File

@@ -25,7 +25,8 @@
function smarty_function_config_load($params, &$smarty) function smarty_function_config_load($params, &$smarty)
{ {
if ($smarty->debugging) { if ($smarty->debugging) {
$_debug_start_time = $smarty->_get_microtime(); $_params = array();
$_debug_start_time = $smarty->_execute_core_function('get_microtime', $_params);
} }
$_file = isset($params['file']) ? $params['file'] : null; $_file = isset($params['file']) ? $params['file'] : null;
@@ -55,7 +56,9 @@ function smarty_function_config_load($params, &$smarty)
$_config_dir = $smarty->config_dir; $_config_dir = $smarty->config_dir;
} else { } else {
// config_dir not found, try include_path // config_dir not found, try include_path
$smarty->_get_include_path($smarty->config_dir, $_config_dir); $_params = array('file_path' => $smarty->config_dir);
$smarty->_execute_core_function('get_include_path', $_params);
$_config_dir = $_params['new_file_path'];
} }
$_file_path = str_replace('//', '/' ,$_config_dir . '/' . $_file); $_file_path = str_replace('//', '/' ,$_config_dir . '/' . $_file);
@@ -95,7 +98,8 @@ function smarty_function_config_load($params, &$smarty)
} else { } else {
$_compile_data = '<?php $_config_vars = unserialize(\'' . str_replace('\'','\\\'', serialize($_config_vars)) . '\'); return true; ?>'; $_compile_data = '<?php $_config_vars = unserialize(\'' . str_replace('\'','\\\'', serialize($_config_vars)) . '\'); return true; ?>';
} }
$smarty->_write_file($_compile_file, $_compile_data, true); $_params = array('filename' => $_compile_file, 'contents' => $_compile_data, 'create_dirs' => true);
$smarty->_execute_core_function('write_file', $_params);
touch($_compile_file,filemtime($_file_path)); touch($_compile_file,filemtime($_file_path));
} }
} }
@@ -118,10 +122,11 @@ function smarty_function_config_load($params, &$smarty)
} }
if ($smarty->debugging) { if ($smarty->debugging) {
$_params = array();
$smarty->_smarty_debug_info[] = array('type' => 'config', $smarty->_smarty_debug_info[] = array('type' => 'config',
'filename' => $_file.' ['.$_section.'] '.$_scope, 'filename' => $_file.' ['.$_section.'] '.$_scope,
'depth' => $smarty->_inclusion_depth, 'depth' => $smarty->_inclusion_depth,
'exec_time' => $smarty->_get_microtime() - $_debug_start_time); 'exec_time' => $smarty->_execute_core_function('get_microtime', $_params) - $_debug_start_time);
} }
} }

View File

@@ -21,40 +21,33 @@
*/ */
function smarty_function_fetch($params, &$smarty) function smarty_function_fetch($params, &$smarty)
{ {
$file = $params['file']; if (empty($params['file'])) {
if (empty($file)) {
$smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty"); $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
return; return;
} }
if ($smarty->security && !preg_match('!^(http|ftp)://!i', $file)) { if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
// fetching file, make sure it comes from secure directory $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
foreach ($smarty->secure_dir as $curr_dir) { if(!$smarty->_execute_core_function('is_secure', $_params)) {
if (substr(realpath($file), 0, strlen(realpath($curr_dir))) == realpath($curr_dir)) { $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
$resource_is_secure = true; return;
break; }
}
}
if (!$resource_is_secure) {
$smarty->_trigger_fatal_error("[plugin] (secure mode) fetch '$file' is not allowed");
return;
}
// fetch the file // fetch the file
if($fp = @fopen($file,'r')) { if($fp = @fopen($params['file'],'r')) {
while(!feof($fp)) { while(!feof($fp)) {
$content .= fgets ($fp,4096); $content .= fgets ($fp,4096);
} }
fclose($fp); fclose($fp);
} else { } else {
$smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'"); $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
return; return;
} }
} else { } else {
// not a local file // not a local file
if(preg_match('!^http://!i',$file)) { if(preg_match('!^http://!i',$params['file'])) {
// http fetch // http fetch
if($uri_parts = parse_url($file)) { if($uri_parts = parse_url($params['file'])) {
// set defaults // set defaults
$host = $server_name = $uri_parts['host']; $host = $server_name = $uri_parts['host'];
$timeout = 30; $timeout = 30;
@@ -152,7 +145,7 @@ function smarty_function_fetch($params, &$smarty)
return; return;
} else { } else {
if($_is_proxy) { if($_is_proxy) {
fputs($fp, "GET $file HTTP/1.0\r\n"); fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
} else { } else {
fputs($fp, "GET $uri HTTP/1.0\r\n"); fputs($fp, "GET $uri HTTP/1.0\r\n");
} }
@@ -197,13 +190,13 @@ function smarty_function_fetch($params, &$smarty)
} }
} else { } else {
// ftp fetch // ftp fetch
if($fp = @fopen($file,'r')) { if($fp = @fopen($params['file'],'r')) {
while(!feof($fp)) { while(!feof($fp)) {
$content .= fgets ($fp,4096); $content .= fgets ($fp,4096);
} }
fclose($fp); fclose($fp);
} else { } else {
$smarty->_trigger_fatal_error("[plugin] fetch cannot read file '$file'"); $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] .'\'');
return; return;
} }
} }

View File

@@ -112,7 +112,8 @@ function smarty_function_html_image($params, &$smarty)
return; return;
} }
} }
if(!$smarty->security && !$smarty->_is_secure('file', $_image_path)) { $_params = array('resource_type' => 'file', 'resource_name' => $_image_path);
if(!$smarty->security && !$this->_execute_core_function('is_secure', $_params)) {
$smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE); $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);
return; return;
} }