PageRenderTime 234ms CodeModel.GetById 197ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Tpblog/ThinkPHP/PlugIns/viewCache.php

http://tpblog.googlecode.com/
PHP | 247 lines | 170 code | 5 blank | 72 comment | 36 complexity | 55598b427dda7f4701d2f07a1dcc8727 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: viewCache
  4. Plugin URI: http://thinkphp.cn/
  5. Description: ????????????????????
  6. Author: ??
  7. Version: 1.0
  8. Author URI: http://blog.liu21st.com/
  9. */
  10. /**
  11. +----------------------------------------------------------
  12. * ?????????
  13. *
  14. +----------------------------------------------------------
  15. * @access public
  16. +----------------------------------------------------------
  17. * @return void
  18. +----------------------------------------------------------
  19. * @throws ThinkExecption
  20. +----------------------------------------------------------
  21. */
  22. function checkViewCache()
  23. {
  24. $cacheInfo = S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE');
  25. if($cacheType = $cacheInfo['type']) {
  26. // ????????
  27. $cacheTime = $cacheInfo['time'];
  28. if($cacheType=='ALL') {
  29. //??????
  30. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
  31. if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
  32. // ?????? ????Action
  33. readfile($cacheFile);
  34. exit();
  35. }
  36. }elseif($cacheType=='PART') {
  37. //??????
  38. // ????????
  39. $view_cache = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
  40. if($view_cache) {
  41. // ????????
  42. if($cacheInfo['default']=='NOCACHE') {
  43. foreach($view_cache as $key=>$val) {
  44. if(isset($val['expire'])) {
  45. // ????????
  46. $cacheTime = $val['expire'];
  47. }
  48. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'_'.$val['id'].'.html'; //??????
  49. if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
  50. // ?????? ?Action??
  51. Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],true);
  52. }else {
  53. Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],null);
  54. }
  55. }
  56. }
  57. elseif($cacheInfo['default']=='CACHE') {
  58. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html'; //??????
  59. if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
  60. // ?????? ?Action??
  61. Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',true);
  62. }else {
  63. Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',null);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. return ;
  70. }
  71. // ???????? ???????????
  72. function replaceCacheTag($content)
  73. {
  74. //?????ThinkCache?? ???????? <thinkcache type="" time="" />
  75. $find = preg_match('/<thinkcache\s(.+?)\s\/>\W/is',$content,$matches);
  76. if($find) {
  77. //??ThinkCache??
  78. $content = str_replace($matches[0],'',$content);
  79. //??ThinkCache??
  80. $tagLibs = $matches[1];
  81. $xml = '<tpl><tag '.$tagLibs.' /></tpl>';
  82. $xml = simplexml_load_string($xml);
  83. $xml = (array)($xml->tag->attributes());
  84. $array = array_change_key_case($xml['@attributes']);
  85. if(!isset($array['time'])) {
  86. $array['time'] = C('HTML_CACHE_TIME');
  87. }
  88. if(!isset($array['default'])) {
  89. $array['default'] = 'nocache';
  90. }
  91. $cacheInfo = array_map('strtoupper',$array);
  92. S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',$cacheInfo,-1);
  93. }else
  94. $cacheInfo = array('type'=>'', 'default'=>'');
  95. if($cacheInfo['type']=='PART') {
  96. // ?????? ?????
  97. $cacheData = array();
  98. if($cacheInfo['default'] == 'NOCACHE') {
  99. // ???????? ???????? ???? <cache id="" expire=""></cache>
  100. $find = preg_match_all('/<cache\s(.+?)>(.+?)<\/cache>/is',$content,$matches,PREG_SET_ORDER);
  101. if($find) {
  102. // ????????
  103. //??Cache??
  104. foreach($matches as $key=>$match) {
  105. // ???????
  106. $cacheAttr = $match[1];
  107. $cacheContent = $match[2];
  108. $xml = '<think><cache '.$cacheAttr.' /></think>';
  109. $xml = simplexml_load_string($xml);
  110. $xml = (array)($xml->cache->attributes());
  111. $array = array_change_key_case($xml['@attributes']);
  112. $cacheId = $array['id'];
  113. // ????????
  114. $parseStr = '<cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
  115. $parseStr .= $cacheContent;
  116. $parseStr .= '</cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
  117. $content = str_replace($match[0],$parseStr,$content);
  118. // ????????
  119. $cacheData[] = $array;
  120. }
  121. // ???????????
  122. S(MODULE_NAME.'_'.ACTION_NAME.'_DATA',$cacheData,-1);
  123. }
  124. }elseif($cacheInfo['default'] == 'CACHE') {
  125. //??????? ??????<nocache id=""></nocache> ??
  126. $find = preg_match_all('/<nocache\s(.+?)>(.+?)<\/nocache>/is',$content,$matches,PREG_SET_ORDER);
  127. if($find) {
  128. // ????????
  129. //??Cache??
  130. foreach($matches as $key=>$match) {
  131. // ???????
  132. $cacheAttr = $match[1];
  133. $cacheContent = $match[2];
  134. $xml = '<think><cache '.$cacheAttr.' /></think>';
  135. $xml = simplexml_load_string($xml);
  136. $xml = (array)($xml->cache->attributes());
  137. $array = array_change_key_case($xml['@attributes']);
  138. $cacheId = $array['id'];
  139. // ????????
  140. $parseStr = '<nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
  141. $parseStr .= $cacheContent;
  142. $parseStr .= '</nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
  143. $content = str_replace($match[0],$parseStr,$content);
  144. // ????????
  145. $cacheData[] = $array;
  146. }
  147. // ???????????
  148. S(MODULE_NAME.'_'.ACTION_NAME.'_DATA',$cacheData,-1);
  149. }
  150. }
  151. }
  152. return $content;
  153. }
  154. /**
  155. +----------------------------------------------------------
  156. * ??????
  157. *
  158. +----------------------------------------------------------
  159. * @access public
  160. +----------------------------------------------------------
  161. * @return string
  162. +----------------------------------------------------------
  163. * @throws ThinkExecption
  164. +----------------------------------------------------------
  165. */
  166. function writeViewCache($content)
  167. {
  168. // ????????
  169. $cacheInfo = S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE');
  170. if($cacheType = $cacheInfo['type']) {
  171. // ????????
  172. $cacheTime = $cacheInfo['time'];
  173. if($cacheType=='ALL') {
  174. // ??????????
  175. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
  176. if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
  177. // ??????????????
  178. if( false === file_put_contents($cacheFile,trim($content))) {
  179. throw_exception(L('_CACHE_WRITE_ERROR_'));
  180. }
  181. }
  182. }else {
  183. if($cacheInfo['default']=='NOCACHE') {
  184. // ??????????
  185. $cacheData = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
  186. foreach($cacheData as $key=>$val) {
  187. $cacheTime = isset($val['expire'])?$val['expire']:$cacheTime; // ??????
  188. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'_'.$val['id'].'.html';
  189. // ??????????
  190. $tag = 'cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'];
  191. $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$content,$matches);
  192. if($find) {
  193. $cacheContent = $matches[1];
  194. }
  195. if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
  196. // ??????????????
  197. if(trim($cacheContent)!='') {
  198. if( false === file_put_contents($cacheFile,trim($cacheContent))) {
  199. throw_exception(L('_CACHE_WRITE_ERROR_'));
  200. }
  201. }
  202. }else {
  203. $cacheContent = file_get_contents($cacheFile);
  204. $content = str_replace($matches[0],$cacheContent,$content);
  205. }
  206. }
  207. }elseif($cacheInfo['default']=='CACHE') {
  208. $cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
  209. if( !file_exists($cacheFile) || time()>filemtime($cacheFile)+$cacheTime) {
  210. // ??????????????
  211. if( false === file_put_contents($cacheFile,trim($content))) {
  212. throw_exception(L('_CACHE_WRITE_ERROR_'));
  213. }
  214. }else {
  215. // ????????
  216. $cacheContent = file_get_contents($cacheFile);
  217. // ????????????
  218. $cacheData = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
  219. foreach($cacheData as $key=>$val) {
  220. // ??????????
  221. $tag = 'nocache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'];
  222. $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$content,$matches);
  223. if($find) {
  224. $replaceContent = $matches[1];
  225. }
  226. $find = preg_match('/<'.$tag.'>(.+?)<\/'.$tag.'>/is',$cacheContent,$matches);
  227. if($find) {
  228. $nocacheContent = $matches[1];
  229. }
  230. // ?????????????
  231. $content = str_replace($nocacheContent,$replaceContent,$cacheContent);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. return $content;
  238. }
  239. add_filter('app_init','checkViewCache');
  240. add_filter('tmpl_replace','replaceCacheTag');
  241. add_filter('ob_content','writeViewCache');
  242. ?>