PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/cache/storage/cachelite.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 351 lines | 194 code | 45 blank | 112 comment | 30 complexity | e597d46b56103961c3abaf5be3f23d04 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Cache lite storage handler
  12. *
  13. * @see http://pear.php.net/package/Cache_Lite/
  14. * @since 11.1
  15. */
  16. class JCacheStorageCachelite extends JCacheStorage
  17. {
  18. /**
  19. * Static cache of the Cache_Lite instance
  20. *
  21. * @var object
  22. * @since 11.1
  23. */
  24. protected static $CacheLiteInstance = null;
  25. /**
  26. * Root path
  27. *
  28. * @var string
  29. * @since 11.1
  30. */
  31. protected $_root;
  32. /**
  33. * Constructor
  34. *
  35. * @param array $options Optional parameters.
  36. *
  37. * @since 11.1
  38. */
  39. public function __construct($options = array())
  40. {
  41. parent::__construct($options);
  42. $this->_root = $options['cachebase'];
  43. $cloptions = array(
  44. 'cacheDir' => $this->_root . '/',
  45. 'lifeTime' => $this->_lifetime,
  46. 'fileLocking' => $this->_locking,
  47. 'automaticCleaningFactor' => isset($options['autoclean']) ? $options['autoclean'] : 200,
  48. 'fileNameProtection' => false,
  49. 'hashedDirectoryLevel' => 0,
  50. 'caching' => $options['caching']);
  51. if (self::$CacheLiteInstance === null)
  52. {
  53. $this->initCache($cloptions);
  54. }
  55. }
  56. /**
  57. * Instantiates the appropriate CacheLite object.
  58. * Only initializes the engine if it does not already exist.
  59. * Note this is a protected method
  60. *
  61. * @param array $cloptions optional parameters
  62. *
  63. * @return object
  64. *
  65. * @since 11.1
  66. */
  67. protected function initCache($cloptions)
  68. {
  69. if (!class_exists('Cache_Lite'))
  70. {
  71. require_once 'Cache/Lite.php';
  72. }
  73. self::$CacheLiteInstance = new Cache_Lite($cloptions);
  74. return self::$CacheLiteInstance;
  75. }
  76. /**
  77. * Get cached data from a file by id and group
  78. *
  79. * @param string $id The cache data id.
  80. * @param string $group The cache data group.
  81. * @param boolean $checkTime True to verify cache time expiration threshold.
  82. *
  83. * @return mixed Boolean false on failure or a cached data string.
  84. *
  85. * @since 11.1
  86. */
  87. public function get($id, $group, $checkTime = true)
  88. {
  89. self::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
  90. $this->_getCacheId($id, $group);
  91. $data = self::$CacheLiteInstance->get($this->rawname, $group);
  92. return $data;
  93. }
  94. /**
  95. * Get all cached data
  96. *
  97. * @return array
  98. *
  99. * @since 11.1
  100. */
  101. public function getAll()
  102. {
  103. parent::getAll();
  104. $path = $this->_root;
  105. $folders = new DirectoryIterator($path);
  106. $data = array();
  107. foreach ($folders as $folder)
  108. {
  109. if (!$folder->isDir() || $folder->isDot())
  110. {
  111. continue;
  112. }
  113. $foldername = $folder->getFilename();
  114. $files = new DirectoryIterator($path . '/' . $foldername);
  115. $item = new JCacheStorageHelper($foldername);
  116. foreach ($files as $file)
  117. {
  118. if (!$file->isFile())
  119. {
  120. continue;
  121. }
  122. $filename = $file->getFilename();
  123. $item->updateSize(filesize($path . '/' . $foldername . '/' . $filename) / 1024);
  124. }
  125. $data[$foldername] = $item;
  126. }
  127. return $data;
  128. }
  129. /**
  130. * Store the data to a file by id and group
  131. *
  132. * @param string $id The cache data id.
  133. * @param string $group The cache data group.
  134. * @param string $data The data to store in cache.
  135. *
  136. * @return boolean True on success, false otherwise
  137. *
  138. * @since 11.1
  139. */
  140. public function store($id, $group, $data)
  141. {
  142. $dir = $this->_root . '/' . $group;
  143. // If the folder doesn't exist try to create it
  144. if (!is_dir($dir))
  145. {
  146. // Make sure the index file is there
  147. $indexFile = $dir . '/index.html';
  148. @mkdir($dir) && file_put_contents($indexFile, '<!DOCTYPE html><title></title>');
  149. }
  150. // Make sure the folder exists
  151. if (!is_dir($dir))
  152. {
  153. return false;
  154. }
  155. self::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
  156. $this->_getCacheId($id, $group);
  157. $success = self::$CacheLiteInstance->save($data, $this->rawname, $group);
  158. if ($success == true)
  159. {
  160. return $success;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. /**
  168. * Remove a cached data file by id and group
  169. *
  170. * @param string $id The cache data id
  171. * @param string $group The cache data group
  172. *
  173. * @return boolean True on success, false otherwise
  174. *
  175. * @since 11.1
  176. */
  177. public function remove($id, $group)
  178. {
  179. self::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
  180. $this->_getCacheId($id, $group);
  181. $success = self::$CacheLiteInstance->remove($this->rawname, $group);
  182. if ($success == true)
  183. {
  184. return $success;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. }
  191. /**
  192. * Clean cache for a group given a mode.
  193. *
  194. * @param string $group The cache data group.
  195. * @param string $mode The mode for cleaning cache [group|notgroup].
  196. * group mode : cleans all cache in the group
  197. * notgroup mode : cleans all cache not in the group
  198. *
  199. * @return boolean True on success, false otherwise.
  200. *
  201. * @since 11.1
  202. */
  203. public function clean($group, $mode = null)
  204. {
  205. jimport('joomla.filesystem.folder');
  206. jimport('joomla.filesystem.file');
  207. switch ($mode)
  208. {
  209. case 'notgroup':
  210. $clmode = 'notingroup';
  211. $success = self::$CacheLiteInstance->clean($group, $clmode);
  212. break;
  213. case 'group':
  214. if (is_dir($this->_root . '/' . $group))
  215. {
  216. $clmode = $group;
  217. self::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
  218. $success = self::$CacheLiteInstance->clean($group, $clmode);
  219. // Remove sub-folders of folder; disable all filtering
  220. $folders = JFolder::folders($this->_root . '/' . $group, '.', false, true, array(), array());
  221. foreach ($folders as $folder)
  222. {
  223. if (is_link($folder))
  224. {
  225. if (JFile::delete($folder) !== true)
  226. {
  227. return false;
  228. }
  229. }
  230. elseif (JFolder::delete($folder) !== true)
  231. {
  232. return false;
  233. }
  234. }
  235. }
  236. else
  237. {
  238. $success = true;
  239. }
  240. break;
  241. default:
  242. if (is_dir($this->_root . '/' . $group))
  243. {
  244. $clmode = $group;
  245. self::$CacheLiteInstance->setOption('cacheDir', $this->_root . '/' . $group . '/');
  246. $success = self::$CacheLiteInstance->clean($group, $clmode);
  247. }
  248. else
  249. {
  250. $success = true;
  251. }
  252. break;
  253. }
  254. if ($success == true)
  255. {
  256. return $success;
  257. }
  258. else
  259. {
  260. return false;
  261. }
  262. }
  263. /**
  264. * Garbage collect expired cache data
  265. *
  266. * @return boolean True on success, false otherwise.
  267. *
  268. * @since 11.1
  269. */
  270. public function gc()
  271. {
  272. $result = true;
  273. self::$CacheLiteInstance->setOption('automaticCleaningFactor', 1);
  274. self::$CacheLiteInstance->setOption('hashedDirectoryLevel', 1);
  275. $success1 = self::$CacheLiteInstance->_cleanDir($this->_root . '/', false, 'old');
  276. if (!($dh = opendir($this->_root . '/')))
  277. {
  278. return false;
  279. }
  280. while ($file = readdir($dh))
  281. {
  282. if (($file != '.') && ($file != '..') && ($file != '.svn'))
  283. {
  284. $file2 = $this->_root . '/' . $file;
  285. if (is_dir($file2))
  286. {
  287. $result = ($result && (self::$CacheLiteInstance->_cleanDir($file2 . '/', false, 'old')));
  288. }
  289. }
  290. }
  291. $success = ($success1 && $result);
  292. return $success;
  293. }
  294. /**
  295. * Test to see if the cache storage is available.
  296. *
  297. * @return boolean True on success, false otherwise.
  298. *
  299. * @since 12.1
  300. */
  301. public static function isSupported()
  302. {
  303. @include_once 'Cache/Lite.php';
  304. return class_exists('Cache_Lite');
  305. }
  306. }