PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/joomla/cache/storage/file.php

http://kak.googlecode.com/
PHP | 264 lines | 135 code | 23 blank | 106 comment | 24 complexity | fee35adcf53ba93fa479466fe129e0cb MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * @version $Id: file.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * File cache storage handler
  18. *
  19. * @package Joomla.Framework
  20. * @subpackage Cache
  21. * @since 1.5
  22. */
  23. class JCacheStorageFile extends JCacheStorage
  24. {
  25. /**
  26. * Constructor
  27. *
  28. * @access protected
  29. * @param array $options optional parameters
  30. */
  31. function __construct( $options = array() )
  32. {
  33. parent::__construct($options);
  34. $config =& JFactory::getConfig();
  35. $this->_root = $options['cachebase'];
  36. $this->_hash = $config->getValue('config.secret');
  37. }
  38. /**
  39. * Get cached data from a file by id and group
  40. *
  41. * @access public
  42. * @param string $id The cache data id
  43. * @param string $group The cache data group
  44. * @param boolean $checkTime True to verify cache time expiration threshold
  45. * @return mixed Boolean false on failure or a cached data string
  46. * @since 1.5
  47. */
  48. function get($id, $group, $checkTime)
  49. {
  50. $data = false;
  51. $path = $this->_getFilePath($id, $group);
  52. $this->_setExpire($id, $group);
  53. if (file_exists($path)) {
  54. $data = file_get_contents($path);
  55. if($data) {
  56. // Remove the initial die() statement
  57. $data = preg_replace('/^.*\n/', '', $data);
  58. }
  59. }
  60. return unserialize($data);
  61. }
  62. /**
  63. * Store the data to a file by id and group
  64. *
  65. * @access public
  66. * @param string $id The cache data id
  67. * @param string $group The cache data group
  68. * @param string $data The data to store in cache
  69. * @return boolean True on success, false otherwise
  70. * @since 1.5
  71. */
  72. function store($id, $group, $data)
  73. {
  74. $written = false;
  75. $path = $this->_getFilePath($id, $group);
  76. $expirePath = $path . '_expire';
  77. $die = '<?php die("Access Denied"); ?>'."\n";
  78. // Prepend a die string
  79. $data = $die.serialize($data);
  80. $fp = @fopen($path, "wb");
  81. if ($fp) {
  82. if ($this->_locking) {
  83. @flock($fp, LOCK_EX);
  84. }
  85. $len = strlen($data);
  86. @fwrite($fp, $data, $len);
  87. if ($this->_locking) {
  88. @flock($fp, LOCK_UN);
  89. }
  90. @fclose($fp);
  91. $written = true;
  92. }
  93. // Data integrity check
  94. if ($written && ($data == file_get_contents($path))) {
  95. @file_put_contents($expirePath, ($this->_now + $this->_lifetime));
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Remove a cached data file by id and group
  103. *
  104. * @access public
  105. * @param string $id The cache data id
  106. * @param string $group The cache data group
  107. * @return boolean True on success, false otherwise
  108. * @since 1.5
  109. */
  110. function remove($id, $group)
  111. {
  112. $path = $this->_getFilePath($id, $group);
  113. @unlink($path.'_expire');
  114. if (!@unlink($path)) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Clean cache for a group given a mode.
  121. *
  122. * group mode : cleans all cache in the group
  123. * notgroup mode : cleans all cache not in the group
  124. *
  125. * @access public
  126. * @param string $group The cache data group
  127. * @param string $mode The mode for cleaning cache [group|notgroup]
  128. * @return boolean True on success, false otherwise
  129. * @since 1.5
  130. */
  131. function clean($group, $mode)
  132. {
  133. jimport('joomla.filesystem.folder');
  134. $return = true;
  135. $folder = $group;
  136. if(trim($folder) == '') {
  137. $mode = 'notgroup';
  138. }
  139. switch ($mode)
  140. {
  141. case 'notgroup':
  142. $folders = JFolder::folders($this->_root);
  143. for ($i=0,$n=count($folders);$i<$n;$i++)
  144. {
  145. if ($folders[$i] != $folder) {
  146. $return |= JFolder::delete($this->_root.DS.$folders[$i]);
  147. }
  148. }
  149. break;
  150. case 'group':
  151. default:
  152. if (is_dir($this->_root.DS.$folder)) {
  153. $return = JFolder::delete($this->_root.DS.$folder);
  154. }
  155. break;
  156. }
  157. return $return;
  158. }
  159. /**
  160. * Garbage collect expired cache data
  161. *
  162. * @access public
  163. * @return boolean True on success, false otherwise.
  164. */
  165. function gc()
  166. {
  167. jimport('joomla.filesystem.file');
  168. $result = true;
  169. // files older than lifeTime get deleted from cache
  170. $files = JFolder::files($this->_root, '_expire', true, true);
  171. foreach($files As $file) {
  172. $time = @file_get_contents($file);
  173. if ($time < $this->_now) {
  174. $result |= JFile::delete($file);
  175. $result |= JFile::delete(str_replace('_expire', '', $file));
  176. }
  177. }
  178. return $result;
  179. }
  180. /**
  181. * Test to see if the cache storage is available.
  182. *
  183. * @static
  184. * @access public
  185. * @return boolean True on success, false otherwise.
  186. */
  187. function test()
  188. {
  189. $config =& JFactory::getConfig();
  190. $root = $config->getValue('config.cache_path', JPATH_ROOT.DS.'cache');
  191. return is_writable($root);
  192. }
  193. /**
  194. * Check to make sure cache is still valid, if not, delete it.
  195. *
  196. * @access private
  197. *
  198. * @param string $id Cache key to expire.
  199. * @param string $group The cache data group.
  200. */
  201. function _setExpire($id, $group)
  202. {
  203. $path = $this->_getFilePath($id, $group);
  204. // set prune period
  205. if(file_exists($path.'_expire')) {
  206. $time = @file_get_contents($path.'_expire');
  207. if ($time < $this->_now || empty($time)) {
  208. $this->remove($id, $group);
  209. }
  210. } elseif(file_exists($path)) {
  211. //This means that for some reason there's no expire file, remove it
  212. $this->remove($id, $group);
  213. }
  214. }
  215. /**
  216. * Get a cache file path from an id/group pair
  217. *
  218. * @access private
  219. * @param string $id The cache data id
  220. * @param string $group The cache data group
  221. * @return string The cache file path
  222. * @since 1.5
  223. */
  224. function _getFilePath($id, $group)
  225. {
  226. $folder = $group;
  227. $name = md5($this->_application.'-'.$id.'-'.$this->_hash.'-'.$this->_language).'.php';
  228. $dir = $this->_root.DS.$folder;
  229. // If the folder doesn't exist try to create it
  230. if (!is_dir($dir)) {
  231. // Make sure the index file is there
  232. $indexFile = $dir . DS . 'index.html';
  233. @ mkdir($dir) && file_put_contents($indexFile, '<html><body bgcolor="#FFFFFF"></body></html>');
  234. }
  235. // Make sure the folder exists
  236. if (!is_dir($dir)) {
  237. return false;
  238. }
  239. return $dir.DS.$name;
  240. }
  241. }