PageRenderTime 926ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/Lib/Action/CacheAction.class.php

https://github.com/alin40404/FanweShare
PHP | 287 lines | 237 code | 36 blank | 14 comment | 39 complexity | 36e9590335ae936784e7f8e63d2a49ec MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 方维购物分享网站系统 (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: awfigq <awfigq@qq.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. +------------------------------------------------------------------------------
  13. * 缓存管理
  14. +------------------------------------------------------------------------------
  15. */
  16. class CacheAction extends CommonAction
  17. {
  18. public function system()
  19. {
  20. $this->display();
  21. }
  22. public function custom()
  23. {
  24. $root_dir = FANWE_ROOT.'public/data/caches/custom/';
  25. $dir = trim($_REQUEST['dir']);
  26. $prev = '';
  27. if(empty($dir) || !file_exists($dir))
  28. $dir = $root_dir;
  29. else
  30. {
  31. $paths = pathinfo($dir);
  32. $prev = $paths['dirname'].'/';
  33. $dir = $prev.$paths['basename'].'/';
  34. if($root_dir == $dir || strpos($dir, $root_dir) === false)
  35. {
  36. $dir = $root_dir;
  37. $prev = '';
  38. }
  39. }
  40. $file_list = $this->getFileList($dir);
  41. $this->assign("dir",$dir);
  42. $this->assign("prev",$prev);
  43. $this->assign("file_list",$file_list);
  44. $this->display();
  45. }
  46. public function clear()
  47. {
  48. $type = trim($_REQUEST['type']);
  49. switch($type)
  50. {
  51. case 'system':
  52. $is_admin = isset($_REQUEST['is_admin']) ? intval($_REQUEST['is_admin']) : 0;
  53. $is_data = isset($_REQUEST['is_data']) ? intval($_REQUEST['is_data']) : 0;
  54. $is_tpl = isset($_REQUEST['is_tpl']) ? intval($_REQUEST['is_tpl']) : 0;
  55. $bln = false;
  56. unset($_SESSION['cache_system_clear']);
  57. if($is_admin == 1)
  58. {
  59. $bln = true;
  60. $_SESSION['cache_system_clear']['is_admin'] = 1;
  61. }
  62. if($is_data == 1)
  63. {
  64. $bln = true;
  65. $_SESSION['cache_system_clear']['is_data'] = 1;
  66. }
  67. if($is_tpl == 1)
  68. {
  69. $bln = true;
  70. $_SESSION['cache_system_clear']['is_tpl'] = 1;
  71. }
  72. if(!$bln)
  73. $this->redirect('Cache/system');
  74. else
  75. $this->redirect('Cache/systemClear');
  76. break;
  77. case 'custom':
  78. $file = isset($_REQUEST['file']) ? trim($_REQUEST['file']) : '';
  79. unset($_SESSION['cache_custom_clear']);
  80. if(empty($file) || !file_exists($file))
  81. $this->redirect('Cache/custom');
  82. else
  83. {
  84. if(!is_dir($file))
  85. {
  86. @unlink($file);
  87. $this->redirect('Cache/custom');
  88. }
  89. else
  90. {
  91. $_SESSION['cache_custom_clear'] = array('root'=>$file,'current'=>$file);
  92. $this->redirect('Cache/customClear');
  93. }
  94. }
  95. break;
  96. default:
  97. $this->redirect('Cache/system');
  98. break;
  99. }
  100. }
  101. public function systemClear()
  102. {
  103. $system_clear = $_SESSION['cache_system_clear'];
  104. if(empty($system_clear))
  105. $this->redirect('Cache/system');
  106. else
  107. $_SESSION['cache_system_clear'] = $system_clear;
  108. @set_time_limit(3600);
  109. if(function_exists('ini_set'))
  110. {
  111. ini_set('max_execution_time',3600);
  112. ini_set("memory_limit","256M");
  113. }
  114. $this->display();
  115. ob_start();
  116. ob_end_flush();
  117. ob_implicit_flush(1);
  118. if(isset($system_clear['is_admin']))
  119. {
  120. clearCache();
  121. echoFlush('<script type="text/javascript">showmessage(\''.L('SYSTEM_TIPS6').'\',1);</script>');
  122. }
  123. Vendor('common');
  124. if(isset($system_clear['is_data']))
  125. {
  126. usleep(100);
  127. include_once fimport('class/cache');
  128. Cache::getInstance()->updateCache();
  129. echoFlush('<script type="text/javascript">showmessage(\''.L('SYSTEM_TIPS7').'\',1);</script>');
  130. }
  131. if(isset($system_clear['is_tpl']))
  132. {
  133. usleep(50);
  134. clearDir(FANWE_ROOT.'./public/data/tpl/caches/static');
  135. usleep(50);
  136. clearDir(FANWE_ROOT.'./public/data/tpl/caches/dynamic');
  137. usleep(50);
  138. clearDir(FANWE_ROOT.'./public/data/tpl/compiled');
  139. usleep(50);
  140. clearDir(FANWE_ROOT.'./public/data/tpl/caches/adv_position');
  141. @file_put_contents(FANWE_ROOT.'./public/data/tpl/caches/page/is_clear.lock',TIMESTAMP);
  142. echoFlush('<script type="text/javascript">showmessage(\''.L('SYSTEM_TIPS8').'\',1);</script>');
  143. }
  144. usleep(100);
  145. echoFlush('<script type="text/javascript">showmessage(\''.L('SYSTEM_TIPS9').'\',3);</script>');
  146. exit;
  147. }
  148. public function customClear()
  149. {
  150. $custom_dir = FANWE_ROOT.'public/data/caches/custom/';
  151. $custom_clear = $_SESSION['cache_custom_clear'];
  152. if(empty($custom_clear))
  153. $this->redirect('Cache/custom');
  154. else
  155. $_SESSION['cache_custom_clear'] = $custom_clear;
  156. $root_dir = $custom_clear['root'];
  157. $current_dir = $custom_clear['current'];
  158. if(!file_exists($root_dir) || !is_dir($root_dir) || strpos($root_dir, $custom_dir) === false)
  159. $root_dir = $custom_dir;
  160. if(!file_exists($current_dir) || !is_dir($current_dir))
  161. $current_dir = $root_dir;
  162. $paths = pathinfo($root_dir);
  163. $root_dir = $paths['dirname'].'/'.$paths['basename'].'/';
  164. $paths = pathinfo($current_dir);
  165. $current_dir = $paths['dirname'].'/'.$paths['basename'].'/';
  166. if(strpos($current_dir, $root_dir) === false)
  167. $current_dir = $root_dir;
  168. @set_time_limit(3600);
  169. if(function_exists('ini_set'))
  170. {
  171. ini_set('max_execution_time',3600);
  172. ini_set("memory_limit","256M");
  173. }
  174. $this->display();
  175. ob_start();
  176. ob_end_flush();
  177. ob_implicit_flush(1);
  178. echoFlush('<script type="text/javascript">showmessage(\''.sprintf(L('CUSTOM_TIPS2'),$current_dir).'\',1);</script>');
  179. $files = array();
  180. $dirhandle=opendir($current_dir);
  181. while(($file = readdir($dirhandle)) !== false)
  182. {
  183. if(($file!=".") && ($file!=".."))
  184. {
  185. $file = $current_dir.$file;
  186. if(is_dir($file))
  187. {
  188. @closedir($dirhandle);
  189. $current_dir = $file.'/';
  190. $_SESSION['cache_custom_clear'] = array('root'=>$root_dir,'current'=>$current_dir);
  191. echoFlush('<script type="text/javascript">showmessage(\''.U('Cache/customClear').'\',2);</script>');
  192. exit;
  193. }
  194. else
  195. {
  196. $files[] = $file;
  197. if(count($files) > 5000)
  198. break;
  199. }
  200. }
  201. }
  202. @closedir($dirhandle);
  203. foreach($files as $file)
  204. {
  205. @unlink($file);
  206. echoFlush('<script type="text/javascript">showmessage(\''.sprintf(L('CUSTOM_TIPS3'),$file).'\',1);</script>');
  207. usleep(20);
  208. }
  209. if(count($files) > 5000)
  210. {
  211. $_SESSION['cache_custom_clear'] = array('root'=>$root_dir,'current'=>$current_dir);
  212. echoFlush('<script type="text/javascript">showmessage(\''.U('Cache/customClear').'\',2);</script>');
  213. exit;
  214. }
  215. if($root_dir == $current_dir)
  216. {
  217. if($custom_dir != $root_dir)
  218. @rmdir($root_dir);
  219. echoFlush('<script type="text/javascript">showmessage(\''.L('CUSTOM_TIPS5').'\',3);</script>');
  220. exit;
  221. }
  222. else
  223. {
  224. $paths = pathinfo($current_dir);
  225. @rmdir($current_dir);
  226. $current_dir = $paths['dirname'].'/';
  227. $_SESSION['cache_custom_clear'] = array('root'=>$root_dir,'current'=>$current_dir);
  228. echoFlush('<script type="text/javascript">showmessage(\''.U('Cache/customClear').'\',2);</script>');
  229. exit;
  230. }
  231. }
  232. private function getFileList($dir)
  233. {
  234. $dirhandle=opendir($dir);
  235. $list=array();
  236. while(($file = readdir($dirhandle)) !== false)
  237. {
  238. if(($file!=".") && ($file!=".."))
  239. {
  240. $list[]=array(
  241. 'name'=>$file,
  242. 'path'=>$dir.$file.(is_dir($dir.$file) ? '/' : ''),
  243. 'is_dir'=>is_dir($dir.$file) ? 1 : 0
  244. );
  245. }
  246. }
  247. @closedir($dirhandle);
  248. return $list;
  249. }
  250. }
  251. ?>