📄 smarty.class.php
字号:
* @param string $source_content
* @param integer $resource_timestamp
* @param boolean $get_source
* @param boolean $quiet
* @return boolean
*/
function _fetch_resource_info(&$params)
{
if(!isset($params['get_source'])) { $params['get_source'] = true; }
if(!isset($params['quiet'])) { $params['quiet'] = false; }
$_return = false;
$_params = array('resource_name' => $params['resource_name']) ;
if (isset($params['resource_base_path']))
$_params['resource_base_path'] = $params['resource_base_path'];
if ($this->_parse_resource_name($_params)) {
$_resource_type = $_params['resource_type'];
$_resource_name = $_params['resource_name'];
switch ($_resource_type) {
case 'file':
if ($params['get_source']) {
$params['source_content'] = $this->_read_file($_resource_name);
}
$params['resource_timestamp'] = filemtime($_resource_name);
$_return = is_file($_resource_name);
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['source_content'], &$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['resource_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 (!is_callable($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($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
}
}
}
if (!$_return) {
if (!$params['quiet']) {
$this->trigger_error('unable to read resource: "' . $params['resource_name'] . '"');
}
} else if ($_return && $this->security) {
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php');
if (!smarty_core_is_secure($_params, $this)) {
if (!$params['quiet'])
$this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
$params['source_content'] = null;
$params['resource_timestamp'] = null;
return false;
}
}
return $_return;
}
/**
* parse out the type and name from the resource
*
* @param string $resource_base_path
* @param string $resource_name
* @param string $resource_type
* @param string $resource_name
* @return boolean
*/
function _parse_resource_name(&$params)
{
// split tpl_path by the first colon
$_resource_name_parts = explode(':', $params['resource_name'], 2);
if (count($_resource_name_parts) == 1) {
// no resource type given
$params['resource_type'] = $this->default_resource_type;
$params['resource_name'] = $_resource_name_parts[0];
} else {
if(strlen($_resource_name_parts[0]) == 1) {
// 1 char is not resource type, but part of filepath
$params['resource_type'] = $this->default_resource_type;
$params['resource_name'] = $params['resource_name'];
} else {
$params['resource_type'] = $_resource_name_parts[0];
$params['resource_name'] = $_resource_name_parts[1];
}
}
if ($params['resource_type'] == 'file') {
if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $params['resource_name'])) {
// relative pathname to $params['resource_base_path']
// use the first directory where the file is found
if (isset($params['resource_base_path'])) {
$_resource_base_path = (array)$params['resource_base_path'];
} else {
$_resource_base_path = (array)$this->template_dir;
$_resource_base_path[] = '.';
}
foreach ($_resource_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);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_include_path.php');
if(smarty_core_get_include_path($_params, $this)) {
$params['resource_name'] = $_params['new_file_path'];
return true;
}
}
return false;
}
} elseif (empty($this->_plugins['resource'][$params['resource_type']])) {
$_params = array('type' => $params['resource_type']);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_resource_plugin.php');
smarty_core_load_resource_plugin($_params, $this);
}
return true;
}
/**
* Handle modifiers
*
* @param string|null $modifier_name
* @param array|null $map_array
* @return string result of modifiers
*/
function _run_mod_handler()
{
$_args = func_get_args();
list($_modifier_name, $_map_array) = array_splice($_args, 0, 2);
list($_func_name, $_tpl_file, $_tpl_line) =
$this->_plugins['modifier'][$_modifier_name];
$_var = $_args[0];
foreach ($_var as $_key => $_val) {
$_args[0] = $_val;
$_var[$_key] = call_user_func_array($_func_name, $_args);
}
return $_var;
}
/**
* Remove starting and ending quotes from the string
*
* @param string $string
* @return string
*/
function _dequote($string)
{
if (($string{0} == "'" || $string{0} == '"') &&
$string{strlen($string)-1} == $string{0})
return substr($string, 1, -1);
else
return $string;
}
/**
* read in a file from line $start for $lines.
* read the entire file if $start and $lines are null.
*
* @param string $filename
* @param integer $start
* @param integer $lines
* @return string
*/
function _read_file($filename, $start=null, $lines=null)
{
if (!($fd = @fopen($filename, 'r'))) {
return false;
}
flock($fd, LOCK_SH);
if ($start == null && $lines == null) {
// read the entire file
$contents = fread($fd, filesize($filename));
} else {
if ( $start > 1 ) {
// skip the first lines before $start
for ($loop=1; $loop < $start; $loop++) {
fgets($fd, 65536);
}
}
if ( $lines == null ) {
// read the rest of the file
while (!feof($fd)) {
$contents .= fgets($fd, 65536);
}
} else {
// read up to $lines lines
for ($loop=0; $loop < $lines; $loop++) {
$contents .= fgets($fd, 65536);
if (feof($fd)) {
break;
}
}
}
}
fclose($fd);
return $contents;
}
/**
* get a concrete filename for automagically created content
*
* @param string $auto_base
* @param string $auto_source
* @param string $auto_id
* @return string
* @staticvar string|null
* @staticvar string|null
*/
function _get_auto_filename($auto_base, $auto_source = null, $auto_id = null)
{
$_compile_dir_sep = $this->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
if(@is_dir($auto_base)) {
$_return = $auto_base . DIRECTORY_SEPARATOR;
} else {
// auto_base not found, try include_path
$_params = array('file_path' => $auto_base);
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_include_path.php');
smarty_core_get_include_path($_params, $this);
$_return = isset($_params['new_file_path']) ? $_params['new_file_path'] . DIRECTORY_SEPARATOR : null;
}
if(isset($auto_id)) {
// make auto_id safe for directory names
$auto_id = str_replace('%7C',$_compile_dir_sep,(urlencode($auto_id)));
// split into separate directories
$_return .= $auto_id . $_compile_dir_sep;
}
if(isset($auto_source)) {
// make source name safe for filename
$_filename = urlencode(basename($auto_source));
$_crc32 = crc32($auto_source) . $_compile_dir_sep;
// prepend %% to avoid name conflicts with
// with $params['auto_id'] names
$_crc32 = '%%' . substr($_crc32,0,3) . $_compile_dir_sep . '%%' . $_crc32;
$_return .= $_crc32 . $_filename;
}
return $_return;
}
/**
* unlink a file, possibly using expiration time
*
* @param string $resource
* @param integer $exp_time
*/
function _unlink($resource, $exp_time = null)
{
if(isset($exp_time)) {
if(time() - @filemtime($resource) >= $exp_time) {
return @unlink($resource);
}
} else {
return @unlink($resource);
}
}
/**
* returns an auto_id for auto-file-functions
*
* @param string $cache_id
* @param string $compile_id
* @return string|null
*/
function _get_auto_id($cache_id=null, $compile_id=null) {
if (isset($cache_id))
return (isset($compile_id)) ? $cache_id . '|' . $compile_id : $cache_id;
elseif(isset($compile_id))
return $compile_id;
else
return null;
}
/**
* trigger Smarty plugin error
*
* @param string $error_msg
* @param string $tpl_file
* @param integer $tpl_line
* @param string $file
* @param integer $line
* @param integer $error_type
*/
function _trigger_fatal_error($error_msg, $tpl_file = null, $tpl_line = null,
$file = null, $line = null, $error_type = E_USER_ERROR)
{
if(isset($file) && isset($line)) {
$info = ' ('.basename($file).", line $line)";
} else {
$info = '';
}
if (isset($tpl_line) && isset($tpl_file)) {
$this->trigger_error('[in ' . $tpl_file . ' line ' . $tpl_line . "]: $error_msg$info", $error_type);
} else {
$this->trigger_error($error_msg . $info, $error_type);
}
}
/**
* callback function for preg_replace, to call a non-cacheable block
* @return string
*/
function _process_compiled_include_callback($match) {
$_func = '_smarty_tplfunc_'.$match[2].'_'.$match[3];
ob_start();
$_func($this);
$_ret = ob_get_contents();
ob_end_clean();
return $_ret;
}
/**
* called for included templates
*
* @param string $_smarty_include_tpl_file
* @param string $_smarty_include_vars
*/
// $_smarty_include_tpl_file, $_smarty_include_vars
function _smarty_include($params)
{
if ($this->debugging) {
$_params = array();
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.get_microtime.php');
$debug_start_time = smarty_core_get_microtime($_params, $this);
$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->_is_compiled($params['smarty_include_tpl_file'], $_smarty_compile_path)
|| $this->_compile_resource($params['smarty_include_tpl_file'], $_smarty_compile_path))
{
include($_smarty_co
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -