PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/public/sspdir/app/cake/libs/cache/file.php

https://github.com/matejudo/prospekta
PHP | 279 lines | 126 code | 16 blank | 137 comment | 22 complexity | d0df1894ba6c91fedde6738d93930c30 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /* SVN FILE: $Id: file.php 5135 2007-05-20 06:50:08Z phpnut $ */
  3. /**
  4. * File Storage engine for cache
  5. *
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  10. * Copyright 2005-2007, Cake Software Foundation, Inc.
  11. * 1785 E. Sahara Avenue, Suite 490-204
  12. * Las Vegas, Nevada 89104
  13. *
  14. * Licensed under The MIT License
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @filesource
  18. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  19. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  20. * @package cake
  21. * @subpackage cake.cake.libs.cache
  22. * @since CakePHP(tm) v 1.2.0.4933
  23. * @version $Revision: 5135 $
  24. * @modifiedby $LastChangedBy: phpnut $
  25. * @lastmodified $Date: 2007-05-20 01:50:08 -0500 (Sun, 20 May 2007) $
  26. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  27. */
  28. /**
  29. * Included libraries.
  30. *
  31. */
  32. if (!class_exists('Folder')) {
  33. uses ('folder');
  34. }
  35. /**
  36. * File Storage engine for cache
  37. *
  38. * @todo use the File and Folder classes (if it's not a too big performance hit)
  39. * @package cake
  40. * @subpackage cake.cake.libs.cache
  41. */
  42. class FileEngine extends CacheEngine {
  43. /**
  44. * Cache directory
  45. *
  46. * @var string
  47. * @access private
  48. */
  49. var $_dir = '';
  50. /**
  51. * Cache filename prefix
  52. *
  53. * @var string
  54. * @access private
  55. */
  56. var $_prefix = '';
  57. /**
  58. * Use locking
  59. *
  60. * @var boolean
  61. * @access private
  62. */
  63. var $_lock = false;
  64. /**
  65. * Set up the cache engine
  66. *
  67. * Called automatically by the cache frontend
  68. *
  69. * @param array $params Associative array of parameters for the engine
  70. * @return boolean True if the engine has been succesfully initialized, false if not
  71. * @access public
  72. */
  73. function init($params) {
  74. $dir = CACHE;
  75. $prefix = 'cake_';
  76. $lock = false;
  77. extract($params);
  78. $dir = trim($dir);
  79. $folder =& new Folder();
  80. if (!empty($dir)) {
  81. $dir = $folder->slashTerm($dir);
  82. }
  83. if(empty($dir) || !$folder->isAbsolute($dir) || !is_writable($dir)) {
  84. return false;
  85. }
  86. $this->_dir = $dir;
  87. $this->_prefix = strval($prefix);
  88. $this->_lock = $lock;
  89. return true;
  90. }
  91. /**
  92. * Garbage collection
  93. * Permanently remove all expired and deleted data
  94. *
  95. * @return boolean True if garbage collection was succesful, false on failure
  96. * @access public
  97. */
  98. function gc() {
  99. return $this->clear(true);
  100. }
  101. /**
  102. * Write a value in the cache
  103. *
  104. * @param string $key Identifier for the data
  105. * @param mixed $value Data to be cached
  106. * @param mixed $duration How long to cache the data, in seconds
  107. * @return boolean True if the data was succesfully cached, false on failure
  108. * @access public
  109. */
  110. function write($key, &$value, $duration = CACHE_DEFAULT_DURATION) {
  111. $serialized = serialize($value);
  112. if(!$serialized) {
  113. return false;
  114. }
  115. $expires = time() + $duration;
  116. return $this->_writeCache($this->_getFilename($key), $serialized, $expires);
  117. }
  118. /**
  119. * Get absolute filename for a key
  120. *
  121. * @param string $key The key
  122. * @return string Absolute cache filename for the given key
  123. * @access private
  124. */
  125. function _getFilename($key) {
  126. return $this->_dir . $this->_prefix . $this->base64url_encode($key);
  127. }
  128. /**
  129. * write serialized data to a file
  130. *
  131. * @param string $filename
  132. * @param string $value
  133. * @param integer $expires
  134. * @return boolean True on success, false on failure
  135. * @access private
  136. */
  137. function _writeCache(&$filename, &$value, &$expires) {
  138. $contents = $expires."\n".$value."\n";
  139. return ife(file_put_contents($filename, $contents, ife($this->_lock, LOCK_EX, 0)), true, false);
  140. }
  141. /**
  142. * Read a value from the cache
  143. *
  144. * @param string $key Identifier for the data
  145. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  146. * @access public
  147. */
  148. function read($key) {
  149. $filename = $this->_getFilename($key);
  150. if(!is_file($filename) || !is_readable($filename)) {
  151. return false;
  152. }
  153. $fp = fopen($filename, 'r');
  154. if(!$fp) {
  155. return false;
  156. }
  157. if($this->_lock && !flock($fp, LOCK_SH)) {
  158. return false;
  159. }
  160. $expires = fgets($fp, 11);
  161. if(intval($expires) < time()) {
  162. fclose($fp);
  163. unlink($filename);
  164. return false;
  165. }
  166. $data = '';
  167. while(!feof($fp)) {
  168. $data .= fgets($fp, 4096);
  169. }
  170. $data = trim($data);
  171. return unserialize($data);
  172. }
  173. /**
  174. * Get the expiry time for a cache file
  175. *
  176. * @param string $filename
  177. * @return mixed Expiration timestamp, or false on failure
  178. * @access private
  179. */
  180. function _getExpiry($filename) {
  181. $fp = fopen($filename, 'r');
  182. if(!$fp) {
  183. return false;
  184. }
  185. if($this->_lock && !flock($fp, LOCK_SH)) {
  186. return false;
  187. }
  188. $expires = intval(fgets($fp, 11));
  189. fclose($fp);
  190. return $expires;
  191. }
  192. /**
  193. * Delete a value from the cache
  194. *
  195. * @param string $key Identifier for the data
  196. * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
  197. * @access public
  198. */
  199. function delete($key) {
  200. $filename = $this->_getFilename($key);
  201. return unlink($filename);
  202. }
  203. /**
  204. * Delete all values from the cache
  205. *
  206. * @param boolean $checkExpiry Optional - only delete expired cache items
  207. * @return boolean True if the cache was succesfully cleared, false otherwise
  208. * @access public
  209. */
  210. function clear($checkExpiry = false) {
  211. $dir = dir($this->_dir);
  212. if ($checkExpiry) {
  213. $now = time();
  214. $threshold = $now - 86400;
  215. }
  216. while(($entry = $dir->read()) !== false) {
  217. if(strpos($entry, $this->_prefix) !== 0) {
  218. continue;
  219. }
  220. $filename = $this->_dir.$entry;
  221. if($checkExpiry) {
  222. $mtime = filemtime($filename);
  223. if($mtime === false || $mtime > $threshold) {
  224. continue;
  225. }
  226. $expires = $this->_getExpiry($filename);
  227. if($expires > $now) {
  228. continue;
  229. }
  230. }
  231. unlink($filename);
  232. }
  233. $dir->close();
  234. return true;
  235. }
  236. /**
  237. * Return the settings for this cache engine
  238. *
  239. * @return array list of settings for this engine
  240. * @access public
  241. */
  242. function settings() {
  243. $lock = 'false';
  244. if($this->_lock) {
  245. $lock = 'true';
  246. }
  247. return array('class' => get_class($this),
  248. 'directory' => $this->_dir,
  249. 'prefix' => $this->_prefix,
  250. 'lock' => $lock);
  251. }
  252. /**
  253. * Get a filename-safe version of a string
  254. *
  255. * @param string $str String to encode
  256. * @return string Encoded version of the string
  257. * @access public
  258. */
  259. function base64url_encode($str) {
  260. return strtr(base64_encode($str), '+/', '-_');
  261. }
  262. }
  263. ?>