smarty定时自动清空缓存

本文介绍如何通过设置Smarty缓存机制,在指定时间自动清空缓存,以解决缓存积累导致的硬盘负载问题。通过添加变量、方法和调用自动清理任务,实现了每两天早上10点自动清除所有过期缓存。

smarty的缓存机制不是太完善,只会判断当前的缓存文件是否过期,如果过期就写入新的缓存,这样缓存只会越来越多,硬盘也总有hold不住的那天。那么,我们就来改进下smarty使之能够定时自动清空缓存。

1、打开Smarty.class.php在smarty这个类中添加一个变量:

 /**
  * @每2天 早上10点清空缓存
  */ 
 var $clear_cache_time = ’2 10′;

2、在smarty类中添加两个方法:一个执行自动清空缓存的任务,一个判断是否需要清空

 private function autoClearCache()
 {
  if($this->checkClearTime()){
   $this->clear_all_cache(); //删除所有已过期的缓存
  }
 }
 
 private function checkClearTime()
 {
  $CacheParam = explode(” “,$this->clear_cache_time);

  if(!$this->clear_cache_time || count($CacheParam) !== 2)
  {
   return false;
  }

  if(date(‘H’) != $CacheParam[1])
//当前的 小时 不等 设定的需要清空的 小时,返回false
  {
   return false;
  }

  $cachetag = $this->compile_dir.”/autoclear.tag”;
//设定一个文件,用于记录上次自动清空的时间

        if (file_exists($cachetag))
  {
            $filetime = date(‘U’, filemtime($cachetag));
//返回文件内容上次修改的时间

   if(date(‘d’)-date(“d”,$filetime) == $CacheParam[0])
//如果现在距离上次文件修改时间的天数 为 设定的自动清空缓存的天数
   {
    return true ;
   } else {
    return false ;
   }
  }

  file_put_contents($cachetag,date(“Y-m-d H:i:s”));
//如果不存在autoclear.tag文件,则创建并写入当前时间

  return true;
 }

3、在smarty本来的fetch方法的头部加上一句

$this->autoClearCache();
//也就是每次执行smarty的过程中,都进行自动清空缓存的操作

ok,这样简单的一个通过设定每几天 某个时间段内自动清空缓存的操作就完成了。

<?php /** * 鸿宇多用户商城 模版类 * ============================================================================ * 版权所有 2005-2010 鸿宇多用户商城科技有限公司,并保留所有权利。 * 网站地址: http://bbs.hongyuvip.com; * ---------------------------------------------------------------------------- * 仅供学习交流使用,如需商用请购买正版版权。鸿宇不承担任何法律责任。 * 踏踏实实做事,堂堂正正做人。 * ============================================================================ * $Author: liuhui $ * $Id: cls_template.php 17063 2010-03-25 06:35:46Z liuhui $ */ class cls_template { var $template_dir = &#39;&#39;; var $cache_dir = &#39;&#39;; var $compile_dir = &#39;&#39;; var $cache_lifetime = 3600; // 缓存更新时间, 默认 3600 秒 var $direct_output = false; var $caching = false; var $template = array(); var $force_compile = false; var $_var = array(); var $_echash = &#39;554fcae493e564ee0dc75bdf2ebf94ca&#39;; var $_foreach = array(); var $_current_file = &#39;&#39;; var $_expires = 0; var $_errorlevel = 0; var $_nowtime = null; var $_checkfile = true; var $_foreachmark = &#39;&#39;; var $_seterror = 0; var $_temp_key = array(); // 临时存放 foreach 里 key 的数组 var $_temp_val = array(); // 临时存放 foreach 里 item 的数组 function __construct() { $this->cls_template(); } function cls_template() { $this->_errorlevel = error_reporting(); $this->_nowtime = time(); if (defined(&#39;EC_CHARSET&#39;)) { $charset = EC_CHARSET; } else { $charset = &#39;utf-8&#39;; } header(&#39;Content-type: text/html; charset=&#39;.$charset); } /** * 注册变量 * * @access public * @param mix $tpl_var * @param mix $value * * @return void */ function assign($tpl_var, $value = &#39;&#39;) { if (is_array($tpl_var)) { foreach ($tpl_var AS $key => $val) { if ($key != &#39;&#39;) { $this->_var[$key] = $val; } } } else { if ($tpl_var != &#39;&#39;) { $this->_var[$tpl_var] = $value; } } } /** * 显示页面函数 * * @access public * @param string $filename * @param sting $cache_id * * @return void */ function display($filename, $cache_id = &#39;&#39;) { $this->_seterror++; error_reporting(E_ALL ^ E_NOTICE); $this->_checkfile = false; $out = $this->fetch($filename, $cache_id); if (strpos($out, $this->_echash) !== false) { $k = explode($this->_echash, $out); foreach ($k AS $key => $val) { if (($key % 2) == 1) { $k[$key] = $this->insert_mod($val); } } $out = implode(&#39;&#39;, $k); } error_reporting($this->_errorlevel); $this->_seterror--; echo $out; } /** * 处理模板文件 * * @access public * @param string $filename * @param sting $cache_id * * @return sring */ function fetch($filename, $cache_id = &#39;&#39;) { if (!$this->_seterror) { error_reporting(E_ALL ^ E_NOTICE); } $this->_seterror++; if (strncmp($filename,&#39;str:&#39;, 4) == 0) { $out = $this->_eval($this->fetch_str(substr($filename, 4))); } else { if ($this->_checkfile) { if (!file_exists($filename)) { $filename = $this->template_dir . &#39;/&#39; . $filename; } } else { $filename = $this->template_dir . &#39;/&#39; . $filename; } if ($this->direct_output) { $this->_current_file = $filename; $out = $this->_eval($this->fetch_str(file_get_contents($filename))); } else { if ($cache_id && $this->caching) { $out = $this->template_out; } else { if (!in_array($filename, $this->template)) { $this->template[] = $filename; } $out = $this->make_compiled($filename); if ($cache_id) { $cachename = basename($filename, strrchr($filename, &#39;.&#39;)) . &#39;_&#39; . $cache_id; $data = serialize(array(&#39;template&#39; => $this->template, &#39;expires&#39; => $this->_nowtime + $this->cache_lifetime, &#39;maketime&#39; => $this->_nowtime)); $out = str_replace("\r", &#39;&#39;, $out); while (strpos($out, "\n\n") !== false) { $out = str_replace("\n\n", "\n", $out); } $hash_dir = $this->cache_dir . &#39;/&#39; . substr(md5($cachename), 0, 1); if (!is_dir($hash_dir)) { mkdir($hash_dir); } if (file_put_contents($hash_dir . &#39;/&#39; . $cachename . &#39;.php&#39;, &#39;<?php exit;?>&#39; . $data . $out, LOCK_EX) === false) { trigger_error(&#39;can\&#39;t write:&#39; . $hash_dir . &#39;/&#39; . $cachename . &#39;.php&#39;); } $this->template = array(); } } } } $this->_seterror--; if (!$this->_seterror) { error_reporting($this->_errorlevel); } return $out; // 返回html数据 } /** * 编译模板函数 * * @access public * @param string $filename * * @return sring 编译后文件地址 */ function make_compiled($filename) { $name = $this->compile_dir . &#39;/&#39; . basename($filename) . &#39;.php&#39;; if ($this->_expires) { $expires = $this->_expires - $this->cache_lifetime; } else { $filestat = @stat($name); $expires = $filestat[&#39;mtime&#39;]; } $filestat = @stat($filename); if ($filestat[&#39;mtime&#39;] <= $expires && !$this->force_compile) { if (file_exists($name)) { $source = $this->_require($name); if ($source == &#39;&#39;) { $expires = 0; } } else { $source = &#39;&#39;; $expires = 0; } } if ($this->force_compile || $filestat[&#39;mtime&#39;] > $expires) { $this->_current_file = $filename; $source = $this->fetch_str(file_get_contents($filename)); if (file_put_contents($name, $source, LOCK_EX) === false) { trigger_error(&#39;can\&#39;t write:&#39; . $name); } $source = $this->_eval($source); } return $source; } /** * 处理字符串函数 * * @access public * @param string $source * * @return sring */ function fetch_str($source) { if (!defined(&#39;ECS_ADMIN&#39;)) { $source = $this->smarty_prefilter_preCompile($source); } $source = preg_replace("/<\?[^><]+\?>|<\%[^><]+\%>|<script[^>]+language[^>]*=[^>]*php[^>]*>[^><]*<\/script\s*>/iU", "", $source); return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select(&#39;\\1&#39;);", $source); } /** * 判断是否缓存 * * @access public * @param string $filename * @param sting $cache_id * * @return bool */ function is_cached($filename, $cache_id = &#39;&#39;) { $cachename = basename($filename, strrchr($filename, &#39;.&#39;)) . &#39;_&#39; . $cache_id; if ($this->caching == true && $this->direct_output == false) { $hash_dir = $this->cache_dir . &#39;/&#39; . substr(md5($cachename), 0, 1); if ($data = @file_get_contents($hash_dir . &#39;/&#39; . $cachename . &#39;.php&#39;)) { $data = substr($data, 13); $pos = strpos($data, &#39;<&#39;); $paradata = substr($data, 0, $pos); $para = @unserialize($paradata); if ($para === false || $this->_nowtime > $para[&#39;expires&#39;]) { $this->caching = false; return false; } $this->_expires = $para[&#39;expires&#39;]; $this->template_out = substr($data, $pos); foreach ($para[&#39;template&#39;] AS $val) { $stat = @stat($val); if ($para[&#39;maketime&#39;] < $stat[&#39;mtime&#39;]) { $this->caching = false; return false; } } } else { $this->caching = false; return false; } return true; } else { return false; } } /** * 处理{}标签 * * @access public * @param string $tag * * @return sring */ function select($tag) { $tag = stripslashes(trim($tag)); if (empty($tag)) { return &#39;{}&#39;; } elseif ($tag{0} == &#39;*&#39; && substr($tag, -1) == &#39;*&#39;) // 注释部分 { return &#39;&#39;; } elseif ($tag{0} == &#39;$&#39;) // 变量 { return &#39;<?php echo &#39; . $this->get_val(substr($tag, 1)) . &#39;; ?>&#39;; } elseif ($tag{0} == &#39;/&#39;) // 结束 tag { switch (substr($tag, 1)) { case &#39;if&#39;: return &#39;<?php endif; ?>&#39;; break; case &#39;foreach&#39;: if ($this->_foreachmark == &#39;foreachelse&#39;) { $output = &#39;<?php endif; unset($_from); ?>&#39;; } else { array_pop($this->_patchstack); $output = &#39;<?php endforeach; endif; unset($_from); ?>&#39;; } $output .= "<?php \$this->pop_vars();; ?>"; return $output; break; case &#39;literal&#39;: return &#39;&#39;; break; default: return &#39;{&#39;. $tag .&#39;}&#39;; break; } } else { /* 代码修改_start By bbs.hongyuvip.com */ //$tag_sel = array_shift(explode(&#39; &#39;, $tag)); $tag_arr_www_ecshop68_com = explode(&#39; &#39;, $tag); $tag_sel = array_shift($tag_arr_www_ecshop68_com); /* 代码修改_end By bbs.hongyuvip.com */ switch ($tag_sel) { case &#39;if&#39;: return $this->_compile_if_tag(substr($tag, 3)); break; case &#39;else&#39;: return &#39;<?php else: ?>&#39;; break; case &#39;elseif&#39;: return $this->_compile_if_tag(substr($tag, 7), true); break; case &#39;foreachelse&#39;: $this->_foreachmark = &#39;foreachelse&#39;; return &#39;<?php endforeach; else: ?>&#39;; break; case &#39;foreach&#39;: $this->_foreachmark = &#39;foreach&#39;; if(!isset($this->_patchstack)) { $this->_patchstack = array(); } return $this->_compile_foreach_start(substr($tag, 8)); break; case &#39;assign&#39;: $t = $this->get_para(substr($tag, 7),0); if ($t[&#39;value&#39;]{0} == &#39;$&#39;) { /* 如果传进来的值是变量,就不用用引号 */ $tmp = &#39;$this->assign(\&#39;&#39; . $t[&#39;var&#39;] . &#39;\&#39;,&#39; . $t[&#39;value&#39;] . &#39;);&#39;; } else { $tmp = &#39;$this->assign(\&#39;&#39; . $t[&#39;var&#39;] . &#39;\&#39;,\&#39;&#39; . addcslashes($t[&#39;value&#39;], "&#39;") . &#39;\&#39;);&#39;; } // $tmp = $this->assign($t[&#39;var&#39;], $t[&#39;value&#39;]); return &#39;<?php &#39; . $tmp . &#39; ?>&#39;; break; case &#39;include&#39;: $t = $this->get_para(substr($tag, 8), 0); return &#39;<?php echo $this->fetch(&#39; . "&#39;$t[file]&#39;" . &#39;); ?>&#39;; break; case &#39;insert_scripts&#39;: $t = $this->get_para(substr($tag, 15), 0); return &#39;<?php echo $this->smarty_insert_scripts(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;create_pages&#39;: $t = $this->get_para(substr($tag, 13), 0); return &#39;<?php echo $this->smarty_create_pages(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;insert&#39; : $t = $this->get_para(substr($tag, 7), false); $out = "<?php \n" . &#39;$k = &#39; . preg_replace("/(\&#39;\\$[^,]+)/e" , "stripslashes(trim(&#39;\\1&#39;,&#39;\&#39;&#39;));", var_export($t, true)) . ";\n"; $out .= &#39;echo $this->_echash . $k[\&#39;name\&#39;] . \&#39;|\&#39; . serialize($k) . $this->_echash;&#39; . "\n?>"; return $out; break; case &#39;literal&#39;: return &#39;&#39;; break; case &#39;cycle&#39; : $t = $this->get_para(substr($tag, 6), 0); return &#39;<?php echo $this->cycle(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;html_options&#39;: $t = $this->get_para(substr($tag, 13), 0); return &#39;<?php echo $this->html_options(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;html_select_date&#39;: $t = $this->get_para(substr($tag, 17), 0); return &#39;<?php echo $this->html_select_date(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;html_radios&#39;: $t = $this->get_para(substr($tag, 12), 0); return &#39;<?php echo $this->html_radios(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; case &#39;html_select_time&#39;: $t = $this->get_para(substr($tag, 12), 0); return &#39;<?php echo $this->html_select_time(&#39; . $this->make_array($t) . &#39;); ?>&#39;; break; default: return &#39;{&#39; . $tag . &#39;}&#39;; break; } } } /** * 处理smarty标签中的变量标签 * * @access public * @param string $val * * @return bool */ function get_val($val) { if (strrpos($val, &#39;[&#39;) !== false) { $val = preg_replace("/\[([^\[\]]*)\]/eis", "&#39;.&#39;.str_replace(&#39;$&#39;,&#39;\$&#39;,&#39;\\1&#39;)", $val); } if (strrpos($val, &#39;|&#39;) !== false) { $moddb = explode(&#39;|&#39;, $val); $val = array_shift($moddb); } if (empty($val)) { return &#39;&#39;; } if (strpos($val, &#39;.$&#39;) !== false) { $all = explode(&#39;.$&#39;, $val); foreach ($all AS $key => $val) { $all[$key] = $key == 0 ? $this->make_var($val) : &#39;[&#39;. $this->make_var($val) . &#39;]&#39;; } $p = implode(&#39;&#39;, $all); } else { $p = $this->make_var($val); } if (!empty($moddb)) { foreach ($moddb AS $key => $mod) { $s = explode(&#39;:&#39;, $mod); switch ($s[0]) { case &#39;escape&#39;: $s[1] = trim($s[1], &#39;"&#39;); if ($s[1] == &#39;html&#39;) { $p = &#39;htmlspecialchars(&#39; . $p . &#39;)&#39;; } elseif ($s[1] == &#39;url&#39;) { $p = &#39;urlencode(&#39; . $p . &#39;)&#39;; } elseif ($s[1] == &#39;decode_url&#39;) { $p = &#39;urldecode(&#39; . $p . &#39;)&#39;; } elseif ($s[1] == &#39;quotes&#39;) { $p = &#39;addslashes(&#39; . $p . &#39;)&#39;; } elseif ($s[1] == &#39;u8_url&#39;) { if (EC_CHARSET != &#39;utf-8&#39;) { $p = &#39;urlencode(ecs_iconv("&#39; . EC_CHARSET . &#39;", "utf-8",&#39; . $p . &#39;))&#39;; } else { $p = &#39;urlencode(&#39; . $p . &#39;)&#39;; } } else { $p = &#39;htmlspecialchars(&#39; . $p . &#39;)&#39;; } break; case &#39;nl2br&#39;: $p = &#39;nl2br(&#39; . $p . &#39;)&#39;; break; case &#39;default&#39;: $s[1] = $s[1]{0} == &#39;$&#39; ? $this->get_val(substr($s[1], 1)) : "&#39;$s[1]&#39;"; $p = &#39;empty(&#39; . $p . &#39;) ? &#39; . $s[1] . &#39; : &#39; . $p; break; case &#39;truncate&#39;: $p = &#39;sub_str(&#39; . $p . ",$s[1])"; break; case &#39;strip_tags&#39;: $p = &#39;strip_tags(&#39; . $p . &#39;)&#39;; break; default: # code... break; } } } return $p; } /** * 处理去掉$的字符串 * * @access public * @param string $val * * @return bool */ function make_var($val) { if (strrpos($val, &#39;.&#39;) === false) { if (isset($this->_var[$val]) && isset($this->_patchstack[$val])) { $val = $this->_patchstack[$val]; } $p = &#39;$this->_var[\&#39;&#39; . $val . &#39;\&#39;]&#39;; } else { $t = explode(&#39;.&#39;, $val); $_var_name = array_shift($t); if (isset($this->_var[$_var_name]) && isset($this->_patchstack[$_var_name])) { $_var_name = $this->_patchstack[$_var_name]; } if ($_var_name == &#39;smarty&#39;) { $p = $this->_compile_smarty_ref($t); } else { $p = &#39;$this->_var[\&#39;&#39; . $_var_name . &#39;\&#39;]&#39;; } foreach ($t AS $val) { $p.= &#39;[\&#39;&#39; . $val . &#39;\&#39;]&#39;; } } return $p; } /** * 处理insert外部函数/需要include运行的函数的调用数据 * * @access public * @param string $val * @param int $type * * @return array */ function get_para($val, $type = 1) // 处理insert外部函数/需要include运行的函数的调用数据 { $pa = $this->str_trim($val); foreach ($pa AS $value) { if (strrpos($value, &#39;=&#39;)) { list($a, $b) = explode(&#39;=&#39;, str_replace(array(&#39; &#39;, &#39;"&#39;, "&#39;", &#39;"&#39;), &#39;&#39;, $value)); if ($b{0} == &#39;$&#39;) { if ($type) { eval(&#39;$para[\&#39;&#39; . $a . &#39;\&#39;]=&#39; . $this->get_val(substr($b, 1)) . &#39;;&#39;); } else { $para[$a] = $this->get_val(substr($b, 1)); } } else { $para[$a] = $b; } } } return $para; } /** * 判断变量是否被注册并返回值 * * @access public * @param string $name * * @return mix */ function &get_template_vars($name = null) { if (empty($name)) { return $this->_var; } elseif (!empty($this->_var[$name])) { return $this->_var[$name]; } else { $_tmp = null; return $_tmp; } } /** * 处理if标签 * * @access public * @param string $tag_args * @param bool $elseif * * @return string */ function _compile_if_tag($tag_args, $elseif = false) { preg_match_all(&#39;/\-?\d+[\.\d]+|\&#39;[^\&#39;|\s]*\&#39;|"[^"|\s]*"|[\$\w\.]+|!==|===|==|!=|<>|<<|>>|<=|>=|&&|\|\||\(|\)|,|\!|\^|=|&|<|>|~|\||\%|\+|\-|\/|\*|\@|\S/&#39;, $tag_args, $match); $tokens = $match[0]; // make sure we have balanced parenthesis $token_count = array_count_values($tokens); if (!empty($token_count[&#39;(&#39;]) && $token_count[&#39;(&#39;] != $token_count[&#39;)&#39;]) { // $this->_syntax_error(&#39;unbalanced parenthesis in if statement&#39;, E_USER_ERROR, __FILE__, __LINE__); } for ($i = 0, $count = count($tokens); $i < $count; $i++) { $token = &$tokens[$i]; switch (strtolower($token)) { case &#39;eq&#39;: $token = &#39;==&#39;; break; case &#39;ne&#39;: case &#39;neq&#39;: $token = &#39;!=&#39;; break; case &#39;lt&#39;: $token = &#39;<&#39;; break; case &#39;le&#39;: case &#39;lte&#39;: $token = &#39;<=&#39;; break; case &#39;gt&#39;: $token = &#39;>&#39;; break; case &#39;ge&#39;: case &#39;gte&#39;: $token = &#39;>=&#39;; break; case &#39;and&#39;: $token = &#39;&&&#39;; break; case &#39;or&#39;: $token = &#39;||&#39;; break; case &#39;not&#39;: $token = &#39;!&#39;; break; case &#39;mod&#39;: $token = &#39;%&#39;; break; default: if ($token[0] == &#39;$&#39;) { $token = $this->get_val(substr($token, 1)); } break; } } if ($elseif) { return &#39;<?php elseif (&#39; . implode(&#39; &#39;, $tokens) . &#39;): ?>&#39;; } else { return &#39;<?php if (&#39; . implode(&#39; &#39;, $tokens) . &#39;): ?>&#39;; } } /** * 处理foreach标签 * * @access public * @param string $tag_args * * @return string */ function _compile_foreach_start($tag_args) { $attrs = $this->get_para($tag_args, 0); $arg_list = array(); $from = $attrs[&#39;from&#39;]; if(isset($this->_var[$attrs[&#39;item&#39;]]) && !isset($this->_patchstack[$attrs[&#39;item&#39;]])) { $this->_patchstack[$attrs[&#39;item&#39;]] = $attrs[&#39;item&#39;] . &#39;_&#39; . str_replace(array(&#39; &#39;, &#39;.&#39;), &#39;_&#39;, microtime()); $attrs[&#39;item&#39;] = $this->_patchstack[$attrs[&#39;item&#39;]]; } else { $this->_patchstack[$attrs[&#39;item&#39;]] = $attrs[&#39;item&#39;]; } $item = $this->get_val($attrs[&#39;item&#39;]); if (!empty($attrs[&#39;key&#39;])) { $key = $attrs[&#39;key&#39;]; $key_part = $this->get_val($key).&#39; => &#39;; } else { $key = null; $key_part = &#39;&#39;; } if (!empty($attrs[&#39;name&#39;])) { $name = $attrs[&#39;name&#39;]; } else { $name = null; } $output = &#39;<?php &#39;; $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, &#39;array&#39;); }; \$this->push_vars(&#39;$attrs[key]&#39;, &#39;$attrs[item]&#39;);"; if (!empty($name)) { $foreach_props = "\$this->_foreach[&#39;$name&#39;]"; $output .= "{$foreach_props} = array(&#39;total&#39; => count(\$_from), &#39;iteration&#39; => 0);\n"; $output .= "if ({$foreach_props}[&#39;total&#39;] > 0):\n"; $output .= " foreach (\$_from AS $key_part$item):\n"; $output .= " {$foreach_props}[&#39;iteration&#39;]++;\n"; } else { $output .= "if (count(\$_from)):\n"; $output .= " foreach (\$_from AS $key_part$item):\n"; } return $output . &#39;?>&#39;; } /** * 将 foreach 的 key, item 放入临时数组 * * @param mixed $key * @param mixed $val * * @return void */ function push_vars($key, $val) { if (!empty($key)) { array_push($this->_temp_key, "\$this->_vars[&#39;$key&#39;]=&#39;" .$this->_vars[$key] . "&#39;;"); } if (!empty($val)) { array_push($this->_temp_val, "\$this->_vars[&#39;$val&#39;]=&#39;" .$this->_vars[$val] ."&#39;;"); } } /** * 弹出临时数组的最后一个 * * @return void */ function pop_vars() { $key = array_pop($this->_temp_key); $val = array_pop($this->_temp_val); if (!empty($key)) { eval($key); } } /** * 处理smarty开头的预定义变量 * * @access public * @param array $indexes * * @return string */ function _compile_smarty_ref(&$indexes) { /* Extract the reference name. */ $_ref = $indexes[0]; switch ($_ref) { case &#39;now&#39;: $compiled_ref = &#39;time()&#39;; break; case &#39;foreach&#39;: array_shift($indexes); $_var = $indexes[0]; $_propname = $indexes[1]; switch ($_propname) { case &#39;index&#39;: array_shift($indexes); $compiled_ref = "(\$this->_foreach[&#39;$_var&#39;][&#39;iteration&#39;] - 1)"; break; case &#39;first&#39;: array_shift($indexes); $compiled_ref = "(\$this->_foreach[&#39;$_var&#39;][&#39;iteration&#39;] <= 1)"; break; case &#39;last&#39;: array_shift($indexes); $compiled_ref = "(\$this->_foreach[&#39;$_var&#39;][&#39;iteration&#39;] == \$this->_foreach[&#39;$_var&#39;][&#39;total&#39;])"; break; case &#39;show&#39;: array_shift($indexes); $compiled_ref = "(\$this->_foreach[&#39;$_var&#39;][&#39;total&#39;] > 0)"; break; default: $compiled_ref = "\$this->_foreach[&#39;$_var&#39;]"; break; } break; case &#39;get&#39;: $compiled_ref = &#39;$_GET&#39;; break; case &#39;post&#39;: $compiled_ref = &#39;$_POST&#39;; break; case &#39;cookies&#39;: $compiled_ref = &#39;$_COOKIE&#39;; break; case &#39;env&#39;: $compiled_ref = &#39;$_ENV&#39;; break; case &#39;server&#39;: $compiled_ref = &#39;$_SERVER&#39;; break; case &#39;request&#39;: $compiled_ref = &#39;$_REQUEST&#39;; break; case &#39;session&#39;: $compiled_ref = &#39;$_SESSION&#39;; break; default: // $this->_syntax_error(&#39;$smarty.&#39; . $_ref . &#39; is an unknown reference&#39;, E_USER_ERROR, __FILE__, __LINE__); break; } array_shift($indexes); return $compiled_ref; } function smarty_insert_scripts($args) { static $scripts = array(); $arr = explode(&#39;,&#39;, str_replace(&#39; &#39;, &#39;&#39;, $args[&#39;files&#39;])); $str = &#39;&#39;; foreach ($arr AS $val) { if (in_array($val, $scripts) == false) { $scripts[] = $val; if ($val{0} == &#39;.&#39;) { $str .= &#39;<script type="text/javascript" src="&#39; . $val . &#39;"></script>&#39;; } else { $str .= &#39;<script type="text/javascript" src="js/&#39; . $val . &#39;"></script>&#39;; } } } return $str; } function smarty_prefilter_preCompile($source) { $file_type = strtolower(strrchr($this->_current_file, &#39;.&#39;)); $tmp_dir = &#39;themes/&#39; . $GLOBALS[&#39;_CFG&#39;][&#39;template&#39;] . &#39;/&#39;; // 模板所在路径 /** * 处理模板文件 */ if ($file_type == &#39;.dwt&#39;) { /* 将模板中所有library替换为链接 */ $pattern = &#39;/<!--\s#BeginLibraryItem\s\"\/(.*?)\"\s-->.*?<!--\s#EndLibraryItem\s-->/se&#39;; $replacement = "&#39;{include file=&#39;.strtolower(&#39;\\1&#39;). &#39;}&#39;"; $source = preg_replace($pattern, $replacement, $source); /* 检查有无动态库文件,如果有为其赋值 */ $dyna_libs = get_dyna_libs($GLOBALS[&#39;_CFG&#39;][&#39;template&#39;], $this->_current_file); if ($dyna_libs) { foreach ($dyna_libs AS $region => $libs) { $pattern = &#39;/<!--\\s*TemplateBeginEditable\\sname="&#39;. $region .&#39;"\\s*-->(.*?)<!--\\s*TemplateEndEditable\\s*-->/s&#39;; if (preg_match($pattern, $source, $reg_match)) { $reg_content = $reg_match[1]; /* 生成匹配字串 */ $keys = array_keys($libs); $lib_pattern = &#39;&#39;; foreach ($keys AS $lib) { $lib_pattern .= &#39;|&#39; . str_replace(&#39;/&#39;, &#39;\/&#39;, substr($lib, 1)); } $lib_pattern = &#39;/{include\sfile=(&#39; . substr($lib_pattern, 1) . &#39;)}/&#39;; /* 修改$reg_content中的内容 */ $GLOBALS[&#39;libs&#39;] = $libs; $reg_content = preg_replace_callback($lib_pattern, &#39;dyna_libs_replace&#39;, $reg_content); /* 用修改过的内容替换原来当前区域中内容 */ $source = preg_replace($pattern, $reg_content, $source); } } } /* 在头部加入版本信息 */ $source = preg_replace(&#39;/<head>/i&#39;, "<head>\r\n<meta name=\"Generator\" content=\"" . APPNAME .&#39; &#39; . VERSION . "\" />", $source); /* By bbs.hongyuvip.com 代码增加_start */ $source = preg_replace(&#39;/<head>/i&#39;, "<head>\r\n<base href=\"". $GLOBALS[&#39;ecs&#39;]->url() ."\" />", $source); /* By bbs.hongyuvip.com 代码增加_end */ /* 修正css路径 */ $source = preg_replace(&#39;/(<link\shref=["|\&#39;])(?:\.\/|\.\.\/)?(css\/)?([a-z0-9A-Z_]+\.css["|\&#39;]\srel=["|\&#39;]stylesheet["|\&#39;]\stype=["|\&#39;]text\/css["|\&#39;])/i&#39;,&#39;\1&#39; . $tmp_dir . &#39;\2\3&#39;, $source); /* 修正js目录下js的路径 */ $source = preg_replace(&#39;/(<script\s(?:type|language)=["|\&#39;]text\/javascript["|\&#39;]\ssrc=["|\&#39;])(?:\.\/|\.\.\/)?(js\/[a-z0-9A-Z_\-\.]+\.(?:js|vbs)["|\&#39;]><\/script>)/&#39;, &#39;\1&#39; . $tmp_dir . &#39;\2&#39;, $source); /* 更换编译模板的编码类型 */ $source = preg_replace(&#39;/<meta\shttp-equiv=["|\&#39;]Content-Type["|\&#39;]\scontent=["|\&#39;]text\/html;\scharset=(?:.*?)["|\&#39;][^>]*?>\r?\n?/i&#39;, &#39;<meta http-equiv="Content-Type" content="text/html; charset=&#39; . EC_CHARSET . &#39;" />&#39; . "\n", $source); } /** * 处理库文件 */ elseif ($file_type == &#39;.lbi&#39;) { /* 去除meta */ $source = preg_replace(&#39;/<meta\shttp-equiv=["|\&#39;]Content-Type["|\&#39;]\scontent=["|\&#39;]text\/html;\scharset=(?:.*?)["|\&#39;]>\r?\n?/i&#39;, &#39;&#39;, $source); } /* 替换文件编码头部 */ if (strpos($source, "\xEF\xBB\xBF") !== FALSE) { $source = str_replace("\xEF\xBB\xBF", &#39;&#39;, $source); } $pattern = array( &#39;/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/&#39;, // 替换smarty注释 &#39;/<!--[^<|>|{|\n]*?-->/&#39;, // 替换不换行的html注释 &#39;/(href=["|\&#39;])\.\.\/(.*?)(["|\&#39;])/i&#39;, // 替换相对链接 &#39;/((?:background|src)\s*=\s*["|\&#39;])(?:\.\/|\.\.\/)?(images\/.*?["|\&#39;])/is&#39;, // 在images前加上 $tmp_dir &#39;/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is&#39;, // 在images前加上 $tmp_dir &#39;/([\&#39;|"])\.\.\//is&#39;, // 以../开头的路径全部修正为空 ); $replace = array( &#39;\1&#39;, &#39;&#39;, &#39;\1\2\3&#39;, &#39;\1&#39; . $tmp_dir . &#39;\2&#39;, &#39;\1&#39; . $tmp_dir . &#39;\2&#39;, &#39;\1&#39; ); return preg_replace($pattern, $replace, $source); } function insert_mod($name) // 处理动态内容 { list($fun, $para) = explode(&#39;|&#39;, $name); $para = unserialize($para); $fun = &#39;insert_&#39; . $fun; return $fun($para); } function str_trim($str) { /* 处理&#39;a=b c=d k = f &#39;类字符串,返回数组 */ while (strpos($str, &#39;= &#39;) != 0) { $str = str_replace(&#39;= &#39;, &#39;=&#39;, $str); } while (strpos($str, &#39; =&#39;) != 0) { $str = str_replace(&#39; =&#39;, &#39;=&#39;, $str); } return explode(&#39; &#39;, trim($str)); } function _eval($content) { ob_start(); eval(&#39;?&#39; . &#39;>&#39; . trim($content)); $content = ob_get_contents(); ob_end_clean(); return $content; } function _require($filename) { ob_start(); include $filename; $content = ob_get_contents(); ob_end_clean(); return $content; } function html_options($arr) { $selected = $arr[&#39;selected&#39;]; if ($arr[&#39;options&#39;]) { $options = (array)$arr[&#39;options&#39;]; } elseif ($arr[&#39;output&#39;]) { if ($arr[&#39;values&#39;]) { foreach ($arr[&#39;output&#39;] AS $key => $val) { $options["{$arr[values][$key]}"] = $val; } } else { $options = array_values((array)$arr[&#39;output&#39;]); } } if ($options) { foreach ($options AS $key => $val) { $out .= $key == $selected ? "<option value=\"$key\" selected>$val</option>" : "<option value=\"$key\">$val</option>"; } } return $out; } function html_select_date($arr) { $pre = $arr[&#39;prefix&#39;]; if (isset($arr[&#39;time&#39;])) { if (intval($arr[&#39;time&#39;]) > 10000) { $arr[&#39;time&#39;] = gmdate(&#39;Y-m-d&#39;, $arr[&#39;time&#39;] + 8*3600); } $t = explode(&#39;-&#39;, $arr[&#39;time&#39;]); $year = strval($t[0]); $month = strval($t[1]); $day = strval($t[2]); } $now = gmdate(&#39;Y&#39;, $this->_nowtime); if (isset($arr[&#39;start_year&#39;])) { if (abs($arr[&#39;start_year&#39;]) == $arr[&#39;start_year&#39;]) { $startyear = $arr[&#39;start_year&#39;]; } else { $startyear = $arr[&#39;start_year&#39;] + $now; } } else { $startyear = $now - 3; } if (isset($arr[&#39;end_year&#39;])) { if (strlen(abs($arr[&#39;end_year&#39;])) == strlen($arr[&#39;end_year&#39;])) { $endyear = $arr[&#39;end_year&#39;]; } else { $endyear = $arr[&#39;end_year&#39;] + $now; } } else { $endyear = $now + 3; } $out = "<select name=\"{$pre}Year\">"; for ($i = $startyear; $i <= $endyear; $i++) { $out .= $i == $year ? "<option value=\"$i\" selected>$i</option>" : "<option value=\"$i\">$i</option>"; } if ($arr[&#39;display_months&#39;] != &#39;false&#39;) { $out .= "</select> <select name=\"{$pre}Month\">"; for ($i = 1; $i <= 12; $i++) { $out .= $i == $month ? "<option value=\"$i\" selected>" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>" : "<option value=\"$i\">" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>"; } } if ($arr[&#39;display_days&#39;] != &#39;false&#39;) { $out .= "</select> <select name=\"{$pre}Day\">"; for ($i = 1; $i <= 31; $i++) { $out .= $i == $day ? "<option value=\"$i\" selected>" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>" : "<option value=\"$i\">" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>"; } } return $out . &#39;</select>&#39;; } function html_radios($arr) { $name = $arr[&#39;name&#39;]; $checked = $arr[&#39;checked&#39;]; $options = $arr[&#39;options&#39;]; $out = &#39;&#39;; foreach ($options AS $key => $val) { $out .= $key == $checked ? "<input type=\"radio\" name=\"$name\" value=\"$key\" checked> {$val} " : "<input type=\"radio\" name=\"$name\" value=\"$key\"> {$val} "; } return $out; } function html_select_time($arr) { $pre = $arr[&#39;prefix&#39;]; if (isset($arr[&#39;time&#39;])) { $arr[&#39;time&#39;] = gmdate(&#39;H-i-s&#39;, $arr[&#39;time&#39;] + 8*3600); $t = explode(&#39;-&#39;, $arr[&#39;time&#39;]); $hour = strval($t[0]); $minute = strval($t[1]); $second = strval($t[2]); } $out = &#39;&#39;; if (!isset($arr[&#39;display_hours&#39;])) { $out .= "<select name=\"{$pre}Hour\">"; for ($i = 0; $i <= 23; $i++) { $out .= $i == $hour ? "<option value=\"$i\" selected>" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>" : "<option value=\"$i\">" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>"; } $out .= "</select> "; } if (!isset($arr[&#39;display_minutes&#39;])) { $out .= "<select name=\"{$pre}Minute\">"; for ($i = 0; $i <= 59; $i++) { $out .= $i == $minute ? "<option value=\"$i\" selected>" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>" : "<option value=\"$i\">" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>"; } $out .= "</select> "; } if (!isset($arr[&#39;display_seconds&#39;])) { $out .= "<select name=\"{$pre}Second\">"; for ($i = 0; $i <= 59; $i++) { $out .= $i == $second ? "<option value=\"$i\" selected>" . str_pad($i, 2, &#39;0&#39;, STR_PAD_LEFT) . "</option>" : "<option value=\"$i\">$i</option>"; } $out .= "</select> "; } return $out; } function cycle($arr) { static $k, $old; $value = explode(&#39;,&#39;, $arr[&#39;values&#39;]); if ($old != $value) { $old = $value; $k = 0; } else { $k++; if (!isset($old[$k])) { $k = 0; } } echo $old[$k]; } function make_array($arr) { $out = &#39;&#39;; foreach ($arr AS $key => $val) { if ($val{0} == &#39;$&#39;) { $out .= $out ? ",&#39;$key&#39;=>$val" : "array(&#39;$key&#39;=>$val"; } else { $out .= $out ? ",&#39;$key&#39;=>&#39;$val&#39;" : "array(&#39;$key&#39;=>&#39;$val&#39;"; } } return $out . &#39;)&#39;; } function smarty_create_pages($params) { extract($params); if (empty($page)) { $page = 1; } if (!empty($count)) { $str = "<option value=&#39;1&#39;>1</option>"; $min = min($count - 1, $page + 3); for ($i = $page - 3 ; $i <= $min ; $i++) { if ($i < 2) { continue; } $str .= "<option value=&#39;$i&#39;"; $str .= $page == $i ? " selected=&#39;true&#39;" : &#39;&#39;; $str .= ">$i</option>"; } if ($count > 1) { $str .= "<option value=&#39;$count&#39;"; $str .= $page == $count ? " selected=&#39;true&#39;" : &#39;&#39;; $str .= ">$count</option>"; } } else { $str = &#39;&#39;; } return $str; } } ?>
最新发布
11-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值