PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-widget-cache/inc/wcache.class.php

https://bitbucket.org/openfarmtech/weblog-content
PHP | 238 lines | 204 code | 34 blank | 0 comment | 43 complexity | 6ed01d703c1f7d3605adb90ecc725c8c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. class WCache
  3. {
  4. var $defmod=0755;
  5. function WCache($path, $disable=false, $disable_output=false)
  6. {
  7. if(!is_dir($path))
  8. {
  9. @mkdir($path, $this->defmod);
  10. $disable=!is_dir($path);
  11. }
  12. if(!$disable && !is_writable($path))
  13. {
  14. @chmod($path, $this->defmod);
  15. $disable=!is_writable($path);
  16. }
  17. if(!in_array(substr($path,-1),array("\\","/"))) $path.="/";
  18. $this->path = $path;
  19. $this->disable = $disable;
  20. $this->disable_output = $disable_output;
  21. $this->stack = array();
  22. $this->output = null;
  23. }
  24. function _output($output)
  25. {
  26. $this->output = $output;
  27. if(!$this->disable_output) echo $output;
  28. }
  29. function _load($file, $time)
  30. {
  31. if($this->disable) return false;
  32. $filename = $this->path.$file;
  33. if(!file_exists($filename)) return false;
  34. if(time()-filemtime($filename)>$time) return false;
  35. return @file_get_contents($filename);
  36. }
  37. function _start($file, $time)
  38. {
  39. $data = $this->_load($file,$time);
  40. if($data===false)
  41. {
  42. $this->stack[count($this->stack)] = $file;
  43. if(!$this->disable_output)ob_start();
  44. return count($this->stack);
  45. }
  46. $data = $this->_unpack($data);
  47. $this->_output($data['__output__']);
  48. return $data;
  49. }
  50. function _unpack($data)
  51. {
  52. return unserialize($data);
  53. }
  54. function _pack($data)
  55. {
  56. return serialize($data);
  57. }
  58. function _getfsname($name)
  59. {
  60. return md5($name);
  61. }
  62. function mkdir_recursive($pathname, $mode)
  63. {
  64. is_dir(dirname($pathname)) || mkdir_recursive(dirname($pathname), $mode);
  65. return is_dir($pathname) || @mkdir($pathname, $mode);
  66. }
  67. function _formatfile($file, $group=false)
  68. {
  69. if(!is_string($file))$file=serialize($file);
  70. $file=$this->_getfsname($file);
  71. if($group)
  72. {
  73. if(!is_string($group))$group=serialize($group);
  74. $subdir=$this->_getfsname($group);
  75. if(!is_dir($this->path.$subdir))
  76. {
  77. $surephp5=false;
  78. if(function_exists("version_compare"))
  79. {
  80. $surephp5=version_compare(PHP_VERSION, '5.0.0', '>=');
  81. }
  82. if($surephp5)@mkdir($this->path.$subdir, $this->defmod, true);
  83. else $this->mkdir_recursive($this->path.$subdir, $this->defmod);
  84. }
  85. $file=$subdir."/".$file;
  86. }
  87. return $file;
  88. }
  89. function save($file, $time, $data=array(), $group=false)
  90. {
  91. if($this->disable) return false;
  92. $file=$this->_formatfile($file, $group);
  93. $time = intval($time);
  94. if($time < 3) $time=3;
  95. if(count($this->stack) && $file == $this->stack[count($this->stack)-1])
  96. {
  97. $filename = $this->path.$file;
  98. if(!$this->disable_output)
  99. {
  100. $data['__output__'] = ob_get_contents();
  101. ob_end_clean();
  102. }
  103. if(file_exists($filename) && !is_writable($filename))
  104. {
  105. trigger_error("Cache file not writeable!", E_USER_ERROR);
  106. return false;
  107. }
  108. $f = fopen($filename, 'w');
  109. if (flock($f, LOCK_EX))
  110. {
  111. fwrite($f, $this->_pack($data));
  112. flock($f, LOCK_UN);
  113. }
  114. fclose($f);
  115. $this->_output($data['__output__']);
  116. unset($this->stack[count($this->stack)-1]);
  117. return false;
  118. }
  119. elseif( count($this->stack) && in_array($file,$this->stack) )
  120. {
  121. trigger_error("Cache stack problem: ".$this->stack[count($this->stack)-1]." not properly finished!", E_USER_ERROR);
  122. return false;
  123. }
  124. else
  125. {
  126. $r = $this->_start($file,$time);
  127. if(is_int($r))
  128. {
  129. return $r;
  130. }
  131. else
  132. {
  133. for($i = 0;$i<count($data); $i++)
  134. {
  135. $data[$i] = $r[$i];
  136. }
  137. return false;
  138. }
  139. }
  140. }
  141. function _cleardir($dir, $exp=0)
  142. {
  143. $n=0;
  144. $dirstack=array();
  145. array_push($dirstack, $dir);
  146. do
  147. {
  148. $dir=array_pop($dirstack);
  149. if(!in_array(substr($dir,-1),array("\\","/"))) $dir.="/";
  150. $fs = @scandir($dir);
  151. foreach($fs as $f)
  152. {
  153. if(in_array($f, array(".","..")))continue;
  154. $fn=$dir.$f;
  155. if(!is_readable($fn))continue;
  156. if(is_file($fn))
  157. {
  158. if($exp > 0)
  159. {
  160. $ts=time()-filemtime($fn);
  161. if($ts < $exp)continue;
  162. }
  163. if($exp>=0)
  164. {
  165. @unlink($fn);
  166. }
  167. $n++;
  168. }
  169. elseif(is_dir($fn))
  170. {
  171. array_push($dirstack, $fn);
  172. }
  173. }
  174. if($exp==0)@rmdir($dir);
  175. }while(sizeof($dirstack)>0);
  176. return $n;
  177. }
  178. function clear($exp=0)
  179. {
  180. return $this->_cleardir($this->path, $exp);
  181. }
  182. function remove($file, $group=false)
  183. {
  184. if(!$file)return;
  185. $file=$this->_formatfile($file, $group);
  186. $filename = $this->path.$file;
  187. if (is_file($filename))
  188. {
  189. @unlink($filename);
  190. }
  191. }
  192. function remove_group($group)
  193. {
  194. if(!$group)return;
  195. $subdir=$this->_getfsname($group);
  196. if(!is_dir($this->path.$subdir))return;
  197. $this->_cleardir($this->path.$subdir);
  198. }
  199. function cachecount()
  200. {
  201. return $this->_cleardir($this->path, -1);
  202. }
  203. }
  204. ?>