10 defined(
'JPATH_PLATFORM') or die;
36 public function __construct($options = array())
38 parent::__construct($options);
39 $this->_root = $options[
'cachebase'];
55 public function get($id, $group, $checkTime =
true)
59 $path = $this->_getFilePath($id, $group);
61 if ($checkTime ==
false || ($checkTime ==
true && $this->_checkExpire($id, $group) ===
true))
63 if (file_exists($path))
65 $data = file_get_contents($path);
69 $data = str_replace(
'<?php die("Access Denied"); ?>#x#',
'', $data);
88 public function getAll()
93 $folders = $this->_folders($path);
96 foreach ($folders as $folder)
98 $files = $this->_filesInFolder($path .
'/' . $folder);
101 foreach ($files as $file)
103 $item->updateSize(filesize($path .
'/' . $folder .
'/' . $file) / 1024);
105 $data[$folder] = $item;
122 public function store($id, $group, $data)
125 $path = $this->_getFilePath($id, $group);
126 $die =
'<?php die("Access Denied"); ?>#x#';
129 $data = $die . $data;
131 $_fileopen = @fopen($path,
"wb");
135 $len = strlen($data);
136 @fwrite($_fileopen, $data, $len);
141 if ($written && ($data == file_get_contents($path)))
161 public function remove($id, $group)
163 $path = $this->_getFilePath($id, $group);
183 public function clean($group, $mode = null)
188 if (trim($folder) ==
'')
196 $folders = $this->_folders($this->_root);
197 for ($i = 0, $n = count($folders); $i < $n; $i++)
199 if ($folders[$i] != $folder)
201 $return |= $this->_deleteFolder($this->_root .
'/' . $folders[$i]);
207 if (is_dir($this->_root .
'/' . $folder))
209 $return = $this->_deleteFolder($this->_root .
'/' . $folder);
228 $files = $this->_filesInFolder($this->_root,
'',
true,
true, array(
'.svn',
'CVS',
'.DS_Store',
'__MACOSX',
'index.html'));
229 foreach ($files as $file)
231 $time = @filemtime($file);
232 if (($time + $this->_lifetime) < $this->_now || empty($time))
234 $result |= @unlink($file);
247 public static function isSupported()
250 return is_writable($conf->get(
'cache_path', JPATH_CACHE));
264 public function lock($id, $group, $locktime)
266 $returning =
new stdClass;
267 $returning->locklooped =
false;
269 $looptime = $locktime * 10;
270 $path = $this->_getFilePath($id, $group);
272 $_fileopen = @fopen($path,
"r+b");
276 $data_lock = @flock($_fileopen, LOCK_EX);
283 if ($data_lock ===
false)
290 while ($data_lock ===
false)
293 if ($lock_counter > $looptime)
295 $returning->locked =
false;
296 $returning->locklooped =
true;
301 $data_lock = @flock($_fileopen, LOCK_EX);
306 $returning->locked = $data_lock;
321 public function unlock($id, $group = null)
323 $path = $this->_getFilePath($id, $group);
325 $_fileopen = @fopen($path,
"r+b");
329 $ret = @flock($_fileopen, LOCK_UN);
346 protected function _checkExpire($id, $group)
348 $path = $this->_getFilePath($id, $group);
351 if (file_exists($path))
353 $time = @filemtime($path);
354 if (($time + $this->_lifetime) < $this->_now || empty($time))
374 protected function _getFilePath($id, $group)
376 $name = $this->_getCacheId($id, $group);
377 $dir = $this->_root .
'/' . $group;
384 $indexFile = $dir .
'/index.html';
385 @ mkdir($dir) && file_put_contents($indexFile,
'<!DOCTYPE html><title></title>');
393 return $dir .
'/' . $name .
'.php';
405 protected function _deleteFolder($path)
408 if (!$path || !is_dir($path) || empty($this->_root))
415 $path = $this->_cleanPath($path);
418 $pos = strpos($path, $this->_cleanPath($this->_root));
420 if ($pos ===
false || $pos > 0)
427 $files = $this->_filesInFolder($path,
'.',
false,
true, array(), array());
429 if (!empty($files) && !is_array($files))
431 if (@unlink($files) !==
true)
436 elseif (!empty($files) && is_array($files))
439 foreach ($files as $file)
441 $file = $this->_cleanPath($file);
451 $filename = basename($file);
459 $folders = $this->_folders($path,
'.',
false,
true, array(), array());
461 foreach ($folders as $folder)
463 if (is_link($folder))
466 if (@unlink($folder) !==
true)
471 elseif ($this->_deleteFolder($folder) !==
true)
501 protected function _cleanPath($path, $ds = DIRECTORY_SEPARATOR)
507 $path = $this->_root;
512 $path = preg_replace(
'#[/\\\\]+#', $ds, $path);
534 protected function _filesInFolder($path, $filter =
'.', $recurse =
false, $fullpath =
false
535 , $exclude = array(
'.svn',
'CVS',
'.DS_Store',
'__MACOSX'), $excludefilter = array(
'^\..*',
'.*~'))
540 $path = $this->_cleanPath($path);
550 if (!($handle = @opendir($path)))
555 if (count($excludefilter))
557 $excludefilter =
'/(' . implode(
'|', $excludefilter) .
')/';
563 while (($file = readdir($handle)) !==
false)
565 if (($file !=
'.') && ($file !=
'..') && (!in_array($file, $exclude)) && (!$excludefilter || !preg_match($excludefilter, $file)))
567 $dir = $path .
'/' . $file;
568 $isDir = is_dir($dir);
573 if (is_int($recurse))
575 $arr2 = $this->_filesInFolder($dir, $filter, $recurse - 1, $fullpath);
579 $arr2 = $this->_filesInFolder($dir, $filter, $recurse, $fullpath);
582 $arr = array_merge($arr, $arr2);
587 if (preg_match(
"/$filter/", $file))
591 $arr[] = $path .
'/' . $file;
620 protected function _folders($path, $filter =
'.', $recurse =
false, $fullpath =
false
621 , $exclude = array(
'.svn',
'CVS',
'.DS_Store',
'__MACOSX'), $excludefilter = array(
'^\..*'))
626 $path = $this->_cleanPath($path);
636 if (!($handle = @opendir($path)))
641 if (count($excludefilter))
643 $excludefilter_string =
'/(' . implode(
'|', $excludefilter) .
')/';
647 $excludefilter_string =
'';
649 while (($file = readdir($handle)) !==
false)
651 if (($file !=
'.') && ($file !=
'..')
652 && (!in_array($file, $exclude))
653 && (empty($excludefilter_string) || !preg_match($excludefilter_string, $file)))
655 $dir = $path .
'/' . $file;
656 $isDir = is_dir($dir);
660 if (preg_match(
"/$filter/", $file))
673 if (is_int($recurse))
675 $arr2 = $this->_folders($dir, $filter, $recurse - 1, $fullpath, $exclude, $excludefilter);
679 $arr2 = $this->_folders($dir, $filter, $recurse, $fullpath, $exclude, $excludefilter);
682 $arr = array_merge($arr, $arr2);