sfFileCache.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Cache class that stores content in files.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfFileCache.class.php 16899 2009-04-02 06:47:33Z fabien $
  16. */
  17. class sfFileCache extends sfCache
  18. {
  19. const READ_DATA = 1;
  20. const READ_TIMEOUT = 2;
  21. const READ_LAST_MODIFIED = 4;
  22. const EXTENSION = '.cache';
  23. /**
  24. * Initializes this sfCache instance.
  25. *
  26. * Available options:
  27. *
  28. * * cache_dir: The directory where to put cache files
  29. *
  30. * * see sfCache for options available for all drivers
  31. *
  32. * @see sfCache
  33. */
  34. public function initialize($options = array())
  35. {
  36. parent::initialize($options);
  37. if (!$this->getOption('cache_dir'))
  38. {
  39. throw new sfInitializationException('You must pass a "cache_dir" option to initialize a sfFileCache object.');
  40. }
  41. $this->setcache_dir($this->getOption('cache_dir'));
  42. }
  43. /**
  44. * @see sfCache
  45. */
  46. public function get($key, $default = null)
  47. {
  48. if (!$this->has($key))
  49. {
  50. return $default;
  51. }
  52. return $this->read($this->getFilePath($key));
  53. }
  54. /**
  55. * @see sfCache
  56. */
  57. public function has($key)
  58. {
  59. return file_exists($this->getFilePath($key)) && time() < $this->read($this->getFilePath($key), self::READ_TIMEOUT);
  60. }
  61. /**
  62. * @see sfCache
  63. */
  64. public function set($key, $data, $lifetime = null)
  65. {
  66. if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1)
  67. {
  68. $this->clean(sfCache::OLD);
  69. }
  70. return $this->write($this->getFilePath($key), $data, time() + $this->getLifetime($lifetime));
  71. }
  72. /**
  73. * @see sfCache
  74. */
  75. public function remove($key)
  76. {
  77. return @unlink($this->getFilePath($key));
  78. }
  79. /**
  80. * @see sfCache
  81. */
  82. public function removePattern($pattern)
  83. {
  84. if (false !== strpos($pattern, '**'))
  85. {
  86. $pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
  87. $regexp = self::patternToRegexp($pattern);
  88. $paths = array();
  89. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
  90. {
  91. if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
  92. {
  93. $paths[] = $path;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. $paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
  100. }
  101. foreach ($paths as $path)
  102. {
  103. if (is_dir($path))
  104. {
  105. sfToolkit::clearDirectory($path);
  106. }
  107. else
  108. {
  109. @unlink($path);
  110. }
  111. }
  112. }
  113. /**
  114. * @see sfCache
  115. */
  116. public function clean($mode = sfCache::ALL)
  117. {
  118. if (!is_dir($this->getOption('cache_dir')))
  119. {
  120. return true;
  121. }
  122. $result = true;
  123. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $file)
  124. {
  125. if (sfCache::ALL == $mode || time() > $this->read($file, self::READ_TIMEOUT))
  126. {
  127. $result = @unlink($file) && $result;
  128. }
  129. }
  130. return $result;
  131. }
  132. /**
  133. * @see sfCache
  134. */
  135. public function getTimeout($key)
  136. {
  137. $path = $this->getFilePath($key);
  138. if (!file_exists($path))
  139. {
  140. return 0;
  141. }
  142. $timeout = $this->read($path, self::READ_TIMEOUT);
  143. return $timeout < time() ? 0 : $timeout;
  144. }
  145. /**
  146. * @see sfCache
  147. */
  148. public function getLastModified($key)
  149. {
  150. $path = $this->getFilePath($key);
  151. if (!file_exists($path) || $this->read($path, self::READ_TIMEOUT) < time())
  152. {
  153. return 0;
  154. }
  155. return $this->read($path, self::READ_LAST_MODIFIED);
  156. }
  157. /**
  158. * Converts a cache key to a full path.
  159. *
  160. * @param string $key The cache key
  161. *
  162. * @return string The full path to the cache file
  163. */
  164. protected function getFilePath($key)
  165. {
  166. return $this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $key).self::EXTENSION;
  167. }
  168. /**
  169. * Reads the cache file and returns the content.
  170. *
  171. * @param string $path The file path
  172. * @param mixed $type The type of data you want to be returned
  173. * sfFileCache::READ_DATA: The cache content
  174. * sfFileCache::READ_TIMEOUT: The timeout
  175. * sfFileCache::READ_LAST_MODIFIED: The last modification timestamp
  176. *
  177. * @return string The content of the cache file.
  178. *
  179. * @throws sfCacheException
  180. */
  181. protected function read($path, $type = self::READ_DATA)
  182. {
  183. if (!$fp = @fopen($path, 'rb'))
  184. {
  185. throw new sfCacheException(sprintf('Unable to read cache file "%s".', $path));
  186. }
  187. @flock($fp, LOCK_SH);
  188. clearstatcache(); // because the filesize can be cached by PHP itself...
  189. $length = @filesize($path);
  190. $mqr = get_magic_quotes_runtime();
  191. //set_magic_quotes_runtime(0); DEPRECATED
  192. ini_set("magic_quotes_runtime", 0);
  193. switch ($type)
  194. {
  195. case self::READ_TIMEOUT:
  196. $data = $length ? intval(@fread($fp, 12)) : 0;
  197. break;
  198. case self::READ_LAST_MODIFIED:
  199. @fseek($fp, 12);
  200. $data = $length ? intval(@fread($fp, 12)) : 0;
  201. break;
  202. case self::READ_DATA:
  203. if ($length)
  204. {
  205. @fseek($fp, 24);
  206. $data = @fread($fp, $length - 24);
  207. }
  208. else
  209. {
  210. $data = '';
  211. }
  212. break;
  213. default:
  214. throw new sfConfigurationException(sprintf('Unknown type "%s".', $type));
  215. }
  216. //set_magic_quotes_runtime($mqr); DEPRECATED
  217. ini_set("magic_quotes_runtime", 0);
  218. @flock($fp, LOCK_UN);
  219. @fclose($fp);
  220. return $data;
  221. }
  222. /**
  223. * Writes the given data in the cache file.
  224. *
  225. * @param string $path The file path
  226. * @param string $data The data to put in cache
  227. * @param integer $timeout The timeout timestamp
  228. *
  229. * @return boolean true if ok, otherwise false
  230. *
  231. * @throws sfCacheException
  232. */
  233. protected function write($path, $data, $timeout)
  234. {
  235. $current_umask = umask();
  236. umask(0000);
  237. if (!is_dir(dirname($path)))
  238. {
  239. // create directory structure if needed
  240. mkdir(dirname($path), 0777, true);
  241. }
  242. $tmpFile = tempnam(dirname($path), basename($path));
  243. if (!$fp = @fopen($tmpFile, 'wb'))
  244. {
  245. throw new sfCacheException(sprintf('Unable to write cache file "%s".', $tmpFile));
  246. }
  247. @fwrite($fp, str_pad($timeout, 12, 0, STR_PAD_LEFT));
  248. @fwrite($fp, str_pad(time(), 12, 0, STR_PAD_LEFT));
  249. @fwrite($fp, $data);
  250. @fclose($fp);
  251. // Hack from Agavi (http://trac.agavi.org/changeset/3979)
  252. // With php < 5.2.6 on win32, renaming to an already existing file doesn't work, but copy does,
  253. // so we simply assume that when rename() fails that we are on win32 and try to use copy()
  254. if (!@rename($tmpFile, $path))
  255. {
  256. if (copy($tmpFile, $path))
  257. {
  258. unlink($tmpFile);
  259. }
  260. }
  261. chmod($path, 0666);
  262. umask($current_umask);
  263. return true;
  264. }
  265. /**
  266. * Sets the cache root directory.
  267. *
  268. * @param string $cache_dir The directory where to put the cache files
  269. */
  270. protected function setcache_dir($cache_dir)
  271. {
  272. // remove last DIRECTORY_SEPARATOR
  273. if (DIRECTORY_SEPARATOR == substr($cache_dir, -1))
  274. {
  275. $cache_dir = substr($cache_dir, 0, -1);
  276. }
  277. // create cache dir if needed
  278. if (!is_dir($cache_dir))
  279. {
  280. $current_umask = umask(0000);
  281. @mkdir($cache_dir, 0777, true);
  282. umask($current_umask);
  283. }
  284. }
  285. }