/trunk/Tpblog/ThinkPHP/PlugIns/viewCache.php
PHP | 247 lines | 170 code | 5 blank | 72 comment | 36 complexity | 55598b427dda7f4701d2f07a1dcc8727 MD5 | raw file
- <?php
- /*
- Plugin Name: viewCache
- Plugin URI: http://thinkphp.cn/
- Description: ????????????????????
- Author: ??
- Version: 1.0
- Author URI: http://blog.liu21st.com/
- */
-
- /**
- +----------------------------------------------------------
- * ?????????
- *
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- function checkViewCache()
- {
- $cacheInfo = S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE');
- if($cacheType = $cacheInfo['type']) {
- // ????????
- $cacheTime = $cacheInfo['time'];
- if($cacheType=='ALL') {
- //??????
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
- if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
- // ?????? ????Action
- readfile($cacheFile);
- exit();
- }
- }elseif($cacheType=='PART') {
- //??????
- // ????????
- $view_cache = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
- if($view_cache) {
- // ????????
- if($cacheInfo['default']=='NOCACHE') {
- foreach($view_cache as $key=>$val) {
- if(isset($val['expire'])) {
- // ????????
- $cacheTime = $val['expire'];
- }
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'_'.$val['id'].'.html'; //??????
- if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
- // ?????? ?Action??
- Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],true);
- }else {
- Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],null);
- }
- }
- }
- elseif($cacheInfo['default']=='CACHE') {
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html'; //??????
- if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
- // ?????? ?Action??
- Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',true);
- }else {
- Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',null);
- }
- }
- }
- }
- }
- return ;
- }
-
- // ???????? ???????????
- function replaceCacheTag($content)
- {
- //?????ThinkCache?? ???????? <thinkcache type="" time="" />
- $find = preg_match('/<thinkcache\s(.+?)\s\/>\W/is',$content,$matches);
- if($find) {
- //??ThinkCache??
- $content = str_replace($matches[0],'',$content);
- //??ThinkCache??
- $tagLibs = $matches[1];
- $xml = '<tpl><tag '.$tagLibs.' /></tpl>';
- $xml = simplexml_load_string($xml);
- $xml = (array)($xml->tag->attributes());
- $array = array_change_key_case($xml['@attributes']);
- if(!isset($array['time'])) {
- $array['time'] = C('HTML_CACHE_TIME');
- }
- if(!isset($array['default'])) {
- $array['default'] = 'nocache';
- }
- $cacheInfo = array_map('strtoupper',$array);
- S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',$cacheInfo,-1);
- }else
- $cacheInfo = array('type'=>'', 'default'=>'');
-
- if($cacheInfo['type']=='PART') {
- // ?????? ?????
- $cacheData = array();
- if($cacheInfo['default'] == 'NOCACHE') {
- // ???????? ???????? ???? <cache id="" expire=""></cache>
- $find = preg_match_all('/<cache\s(.+?)>(.+?)<\/cache>/is',$content,$matches,PREG_SET_ORDER);
- if($find) {
- // ????????
- //??Cache??
- foreach($matches as $key=>$match) {
- // ???????
- $cacheAttr = $match[1];
- $cacheContent = $match[2];
- $xml = '<think><cache '.$cacheAttr.' /></think>';
- $xml = simplexml_load_string($xml);
- $xml = (array)($xml->cache->attributes());
- $array = array_change_key_case($xml['@attributes']);
- $cacheId = $array['id'];
- // ????????
- $parseStr = '<cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
- $parseStr .= $cacheContent;
- $parseStr .= '</cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
- $content = str_replace($match[0],$parseStr,$content);
- // ????????
- $cacheData[] = $array;
- }
- // ???????????
- S(MODULE_NAME.'_'.ACTION_NAME.'_DATA',$cacheData,-1);
- }
- }elseif($cacheInfo['default'] == 'CACHE') {
- //??????? ??????<nocache id=""></nocache> ??
- $find = preg_match_all('/<nocache\s(.+?)>(.+?)<\/nocache>/is',$content,$matches,PREG_SET_ORDER);
- if($find) {
- // ????????
- //??Cache??
- foreach($matches as $key=>$match) {
- // ???????
- $cacheAttr = $match[1];
- $cacheContent = $match[2];
- $xml = '<think><cache '.$cacheAttr.' /></think>';
- $xml = simplexml_load_string($xml);
- $xml = (array)($xml->cache->attributes());
- $array = array_change_key_case($xml['@attributes']);
- $cacheId = $array['id'];
- // ????????
- $parseStr = '<nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
- $parseStr .= $cacheContent;
- $parseStr .= '</nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
- $content = str_replace($match[0],$parseStr,$content);
- // ????????
- $cacheData[] = $array;
- }
- // ???????????
- S(MODULE_NAME.'_'.ACTION_NAME.'_DATA',$cacheData,-1);
- }
- }
- }
- return $content;
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- *
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- function writeViewCache($content)
- {
- // ????????
- $cacheInfo = S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE');
- if($cacheType = $cacheInfo['type']) {
- // ????????
- $cacheTime = $cacheInfo['time'];
- if($cacheType=='ALL') {
- // ??????????
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
- if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
- // ??????????????
- if( false === file_put_contents($cacheFile,trim($content))) {
- throw_exception(L('_CACHE_WRITE_ERROR_'));
- }
- }
- }else {
- if($cacheInfo['default']=='NOCACHE') {
- // ??????????
- $cacheData = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
- foreach($cacheData as $key=>$val) {
- $cacheTime = isset($val['expire'])?$val['expire']:$cacheTime; // ??????
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'_'.$val['id'].'.html';
- // ??????????
- $tag = 'cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'];
- $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$content,$matches);
- if($find) {
- $cacheContent = $matches[1];
- }
- if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
- // ??????????????
- if(trim($cacheContent)!='') {
- if( false === file_put_contents($cacheFile,trim($cacheContent))) {
- throw_exception(L('_CACHE_WRITE_ERROR_'));
- }
- }
- }else {
- $cacheContent = file_get_contents($cacheFile);
- $content = str_replace($matches[0],$cacheContent,$content);
- }
- }
- }elseif($cacheInfo['default']=='CACHE') {
- $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
- if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
- // ??????????????
- if( false === file_put_contents($cacheFile,trim($content))) {
- throw_exception(L('_CACHE_WRITE_ERROR_'));
- }
- }else {
- // ????????
- $cacheContent = file_get_contents($cacheFile);
- // ????????????
- $cacheData = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
- foreach($cacheData as $key=>$val) {
- // ??????????
- $tag = 'nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'];
- $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$content,$matches);
- if($find) {
- $replaceContent = $matches[1];
- }
- $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$cacheContent,$matches);
- if($find) {
- $nocacheContent = $matches[1];
- }
- // ?????????????
- $content = str_replace($nocacheContent,$replaceContent,$cacheContent);
- }
- }
- }
- }
- }
- return $content;
- }
-
- add_filter('app_init','checkViewCache');
- add_filter('tmpl_replace','replaceCacheTag');
- add_filter('ob_content','writeViewCache');
- ?>