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

https://gitlab.com/endomorphosis/greenrenaissancejoomla · PHP · 230 lines · 119 code · 17 blank · 94 comment · 24 complexity · 97d1f9ea8868fea4aa8d3c437a3f69f3 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: file.php 10071 2008-02-27 19:17:32Z ian $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2008 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. * @author Louis Landry <louis.landry@joomla.org>
  20. * @package Joomla.Framework
  21. * @subpackage Cache
  22. * @since 1.5
  23. */
  24. class JCacheStorageFile extends JCacheStorage
  25. {
  26. /**
  27. * Constructor
  28. *
  29. * @access protected
  30. * @param array $options optional parameters
  31. */
  32. function __construct( $options = array() )
  33. {
  34. parent::__construct($options);
  35. $config =& JFactory::getConfig();
  36. $this->_root = $options['cachebase'];
  37. $this->_hash = $config->getValue('config.secret');
  38. }
  39. /**
  40. * Get cached data from a file by id and group
  41. *
  42. * @access public
  43. * @param string $id The cache data id
  44. * @param string $group The cache data group
  45. * @param boolean $checkTime True to verify cache time expiration threshold
  46. * @return mixed Boolean false on failure or a cached data string
  47. * @since 1.5
  48. */
  49. function get($id, $group, $checkTime)
  50. {
  51. $data = false;
  52. $path = $this->_getFilePath($id, $group);
  53. clearstatcache();
  54. if (is_file($path)) {
  55. if ($checkTime) {
  56. if (@ filemtime($path) > $this->_threshold) {
  57. $data = file_get_contents($path);
  58. }
  59. } else {
  60. $data = file_get_contents($path);
  61. }
  62. }
  63. return $data;
  64. }
  65. /**
  66. * Store the data to a file by id and group
  67. *
  68. * @access public
  69. * @param string $id The cache data id
  70. * @param string $group The cache data group
  71. * @param string $data The data to store in cache
  72. * @return boolean True on success, false otherwise
  73. * @since 1.5
  74. */
  75. function store($id, $group, $data)
  76. {
  77. $written = false;
  78. $path = $this->_getFilePath($id, $group);
  79. $fp = @fopen($path, "wb");
  80. if ($fp) {
  81. if ($this->_locking) {
  82. @flock($fp, LOCK_EX);
  83. }
  84. $len = strlen($data);
  85. @fwrite($fp, $data, $len);
  86. if ($this->_locking) {
  87. @flock($fp, LOCK_UN);
  88. }
  89. @fclose($fp);
  90. $written = true;
  91. }
  92. // Data integrity check
  93. if ($written && ($data == file_get_contents($path))) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. /**
  100. * Remove a cached data file by id and group
  101. *
  102. * @access public
  103. * @param string $id The cache data id
  104. * @param string $group The cache data group
  105. * @return boolean True on success, false otherwise
  106. * @since 1.5
  107. */
  108. function remove($id, $group)
  109. {
  110. $path = $this->_getFilePath($id, $group);
  111. if (!@unlink($path)) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. /**
  117. * Clean cache for a group given a mode.
  118. *
  119. * group mode : cleans all cache in the group
  120. * notgroup mode : cleans all cache not in the group
  121. *
  122. * @access public
  123. * @param string $group The cache data group
  124. * @param string $mode The mode for cleaning cache [group|notgroup]
  125. * @return boolean True on success, false otherwise
  126. * @since 1.5
  127. */
  128. function clean($group, $mode)
  129. {
  130. jimport('joomla.filesystem.folder');
  131. $return = true;
  132. $folder = $group;
  133. if(trim($folder) == '') {
  134. $mode = 'notgroup';
  135. }
  136. switch ($mode)
  137. {
  138. case 'notgroup':
  139. $folders = JFolder::folders($this->_root);
  140. for ($i=0,$n=count($folders);$i<$n;$i++)
  141. {
  142. if ($folders[$i] != $folder) {
  143. $return |= JFolder::delete($this->_root.DS.$folders[$i]);
  144. }
  145. }
  146. break;
  147. case 'group':
  148. default:
  149. if (is_dir($this->_root.DS.$folder)) {
  150. $return = JFolder::delete($this->_root.DS.$folder);
  151. }
  152. break;
  153. }
  154. return $return;
  155. }
  156. /**
  157. * Garbage collect expired cache data
  158. *
  159. * @access public
  160. * @return boolean True on success, false otherwise.
  161. */
  162. function gc()
  163. {
  164. $result = true;
  165. // files older than lifeTime get deleted from cache
  166. if (!is_null($this->_lifeTime)) {
  167. $files = JFolder::files($this->_root, '.', true, true);
  168. for ($i=0,$n=count($files);$i<$n;$i++)
  169. {
  170. if (@ filemtime($files[$i]) < $this->_threshold) {
  171. $result |= JFile::delete($files[$i]);
  172. }
  173. }
  174. }
  175. return $result;
  176. }
  177. /**
  178. * Test to see if the cache storage is available.
  179. *
  180. * @static
  181. * @access public
  182. * @return boolean True on success, false otherwise.
  183. */
  184. function test()
  185. {
  186. $config =& JFactory::getConfig();
  187. $root = $config->getValue('config.cache_path', JPATH_ROOT.DS.'cache');
  188. return is_writable($root);
  189. }
  190. /**
  191. * Get a cache file path from an id/group pair
  192. *
  193. * @access private
  194. * @param string $id The cache data id
  195. * @param string $group The cache data group
  196. * @return string The cache file path
  197. * @since 1.5
  198. */
  199. function _getFilePath($id, $group)
  200. {
  201. $folder = $group;
  202. $name = md5($this->_application.'-'.$id.'-'.$this->_hash.'-'.$this->_language).'.cache';
  203. // If the folder doesn't exist try to create it
  204. if (!is_dir($this->_root.DS.$folder)) {
  205. @mkdir($this->_root.DS.$folder);
  206. }
  207. // Make sure the folder exists
  208. if (!is_dir($this->_root.DS.$folder)) {
  209. return false;
  210. }
  211. return $this->_root.DS.$folder.DS.$name;
  212. }
  213. }