PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/Cache/File.php

https://github.com/EthanBlast/Glam-Star-Life
PHP | 237 lines | 117 code | 41 blank | 79 comment | 19 complexity | f4e0a3c54a20befced547d435a082b2b MD5 | raw file
  1. <?php
  2. if (!defined('W3_CACHE_FILE_EXPIRE_MAX')) {
  3. define('W3_CACHE_FILE_EXPIRE_MAX', 2592000);
  4. }
  5. /**
  6. * File class
  7. */
  8. require_once W3TC_LIB_W3_DIR . '/Cache/Base.php';
  9. /**
  10. * Class W3_Cache_File
  11. */
  12. class W3_Cache_File extends W3_Cache_Base
  13. {
  14. /**
  15. * Path to cache dir
  16. *
  17. * @var string
  18. */
  19. var $_cache_dir = '';
  20. /**
  21. * File locking
  22. *
  23. * @var boolean
  24. */
  25. var $_locking = false;
  26. /**
  27. * PHP5 constructor
  28. *
  29. * @param array $config
  30. */
  31. function __construct($config = array())
  32. {
  33. $this->_cache_dir = isset($config['cache_dir']) ? trim($config['cache_dir']) : 'cache';
  34. $this->_locking = isset($config['locking']) ? (boolean) $config['locking'] : false;
  35. }
  36. /**
  37. * PHP4 constructor
  38. *
  39. * @paran array $config
  40. * @return W3_Cache_File
  41. */
  42. function W3_Cache_File($config = array())
  43. {
  44. $this->__construct($config);
  45. }
  46. /**
  47. * Adds data
  48. *
  49. * @param string $key
  50. * @param mixed $var
  51. * @param integer $expire
  52. * @return boolean
  53. */
  54. function add($key, &$var, $expire = 0)
  55. {
  56. if ($this->get($key) === false) {
  57. return $this->set($key, $var, $expire);
  58. }
  59. return false;
  60. }
  61. /**
  62. * Sets data
  63. *
  64. * @param string $key
  65. * @param mixed $var
  66. * @param integer $expire
  67. * @return boolean
  68. */
  69. function set($key, &$var, $expire = 0)
  70. {
  71. $sub_path = $this->_get_path($key);
  72. $path = $this->_cache_dir . '/' . $sub_path;
  73. $sub_dir = dirname($sub_path);
  74. $dir = dirname($path);
  75. if ((@is_dir($dir) || w3_mkdir($sub_dir, 0755, $this->_cache_dir))) {
  76. $fp = @fopen($path, 'wb');
  77. if ($fp) {
  78. if ($this->_locking) {
  79. @flock($fp, LOCK_EX);
  80. }
  81. @fputs($fp, pack('L', $expire));
  82. @fputs($fp, @serialize($var));
  83. @fclose($fp);
  84. if ($this->_locking) {
  85. @flock($fp, LOCK_UN);
  86. }
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * Returns data
  94. *
  95. * @param string $key
  96. * @return mixed
  97. */
  98. function get($key)
  99. {
  100. $var = false;
  101. $path = $this->_cache_dir . '/' . $this->_get_path($key);
  102. if (is_readable($path)) {
  103. $ftime = @filemtime($path);
  104. if ($ftime) {
  105. $fp = @fopen($path, 'rb');
  106. if ($fp) {
  107. if ($this->_locking) {
  108. @flock($fp, LOCK_SH);
  109. }
  110. $expires = @fread($fp, 4);
  111. if ($expires !== false) {
  112. list(, $expire) = @unpack('L', $expires);
  113. $expire = ($expire && $expire <= W3_CACHE_FILE_EXPIRE_MAX ? $expire : W3_CACHE_FILE_EXPIRE_MAX);
  114. if ($ftime > time() - $expire) {
  115. $data = '';
  116. while (!@feof($fp)) {
  117. $data .= @fread($fp, 4096);
  118. }
  119. $var = @unserialize($data);
  120. }
  121. }
  122. if ($this->_locking) {
  123. @flock($fp, LOCK_UN);
  124. }
  125. @fclose($fp);
  126. }
  127. }
  128. }
  129. return $var;
  130. }
  131. /**
  132. * Replaces data
  133. *
  134. * @param string $key
  135. * @param mixed $var
  136. * @param integer $expire
  137. * @return boolean
  138. */
  139. function replace($key, &$var, $expire = 0)
  140. {
  141. if ($this->get($key) !== false) {
  142. return $this->set($key, $var, $expire);
  143. }
  144. return false;
  145. }
  146. /**
  147. * Deletes data
  148. *
  149. * @param string $key
  150. * @return boolean
  151. */
  152. function delete($key)
  153. {
  154. $path = $this->_cache_dir . '/' . $this->_get_path($key);
  155. if (file_exists($path)) {
  156. return @unlink($path);
  157. }
  158. return false;
  159. }
  160. /**
  161. * Flushes all data
  162. *
  163. * @return boolean
  164. */
  165. function flush()
  166. {
  167. @set_time_limit(180);
  168. w3_emptydir($this->_cache_dir);
  169. return true;
  170. }
  171. /**
  172. * Returns modification time of cache file
  173. *
  174. * @param integer $key
  175. */
  176. function mtime($key)
  177. {
  178. $path = $this->_cache_dir . '/' . $this->_get_path($key);
  179. if (file_exists($path)) {
  180. return @filemtime($path);
  181. }
  182. return false;
  183. }
  184. /**
  185. * Returns file path for key
  186. *
  187. * @param string $key
  188. * @return string
  189. */
  190. function _get_path($key)
  191. {
  192. $hash = md5($key);
  193. $path = sprintf('%s/%s/%s/%s', substr($hash, 0, 1), substr($hash, 1, 1), substr($hash, 2, 1), $hash);
  194. return $path;
  195. }
  196. }