Source for file file.php
Documentation is available at file.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* File cache storage handler
* @package Joomla.Platform
* @param array $options Optional parameters
$this->_root =
$options['cachebase'];
// NOTE: raw php calls are up to 100 times faster than JFile or JFolder
* Get cached data from a file by id and group
* @param string $id The cache data id
* @param string $group The cache data group
* @param boolean $checkTime True to verify cache time expiration threshold
* @return mixed Boolean false on failure or a cached data string
public function get($id, $group, $checkTime =
true)
if ($checkTime ==
false ||
($checkTime ==
true &&
$this->_checkExpire($id, $group) ===
true))
// Remove the initial die() statement
$data =
str_replace('<?php die("Access Denied"); ?>#x#', '', $data);
* @return array The cached data
foreach ($folders as $folder)
foreach ($files as $file)
$item->updateSize(filesize($path .
'/' .
$folder .
'/' .
$file) /
1024);
* Store the data to a file by id and group
* @param string $id The cache data id
* @param string $group The cache data group
* @param string $data The data to store in cache
* @return boolean True on success, false otherwise
public function store($id, $group, $data)
$die =
'<?php die("Access Denied"); ?>#x#';
$_fileopen =
@fopen($path, "wb");
@fwrite($_fileopen, $data, $len);
* Remove a cached data file by id and group
* @param string $id The cache data id
* @param string $group The cache data group
* @return boolean True on success, false otherwise
public function remove($id, $group)
* Clean cache for a group given a mode.
* @param string $group The cache data group
* @param string $mode The mode for cleaning cache [group|notgroup]
* group mode : cleans all cache in the group
* notgroup mode : cleans all cache not in the group
* @return boolean True on success, false otherwise
public function clean($group, $mode =
null)
for ($i =
0, $n =
count($folders); $i <
$n; $i++
)
if ($folders[$i] !=
$folder)
* Garbage collect expired cache data
* @return boolean True on success, false otherwise.
// Files older than lifeTime get deleted from cache
$files =
$this->_filesInFolder($this->_root, '', true, true, array('.svn', 'CVS', '.DS_Store', '__MACOSX', 'index.html'));
foreach ($files as $file)
* Test to see if the cache storage is available.
* @return boolean True on success, false otherwise.
* @param string $id The cache data id
* @param string $group The cache data group
* @param integer $locktime Cached item max lock time
* @return boolean True on success, false otherwise.
public function lock($id, $group, $locktime)
$returning =
new stdClass;
$returning->locklooped =
false;
$looptime =
$locktime *
10;
$_fileopen =
@fopen($path, "r+b");
$data_lock =
@flock($_fileopen, LOCK_EX);
if ($data_lock ===
false)
// Loop until you find that the lock has been released.
// That implies that data get from other thread has finished
while ($data_lock ===
false)
if ($lock_counter >
$looptime)
$returning->locked =
false;
$returning->locklooped =
true;
$data_lock =
@flock($_fileopen, LOCK_EX);
$returning->locked =
$data_lock;
* @param string $id The cache data id
* @param string $group The cache data group
* @return boolean True on success, false otherwise.
public function unlock($id, $group =
null)
$_fileopen =
@fopen($path, "r+b");
$ret =
@flock($_fileopen, LOCK_UN);
* Check to make sure cache is still valid, if not, delete it.
* @param string $id Cache key to expire.
* @param string $group The cache data group.
* @return boolean False if not valid
* Get a cache file path from an id/group pair
* @param string $id The cache data id
* @param string $group The cache data group
* @return string The cache file path
$dir =
$this->_root .
'/' .
$group;
// If the folder doesn't exist try to create it
// Make sure the index file is there
$indexFile =
$dir .
'/index.html';
// Make sure the folder exists
return $dir .
'/' .
$name .
'.php';
* Quickly delete a folder of files
* @param string $path The path to the folder to delete.
* @return boolean True on success.
// Bad programmer! Bad Bad programmer!
JLog::add('JCacheStorageFile::_deleteFolder ' .
JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), JLog::WARNING, 'jerror');
// Check to make sure path is inside cache folder, we do not want to delete Joomla root!
if ($pos ===
false ||
$pos >
0)
JLog::add('JCacheStorageFile::_deleteFolder' .
JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path), JLog::WARNING, 'jerror');
// Remove all the files in folder if they exist; disable all filtering
$files =
$this->_filesInFolder($path, '.', false, true, array(), array());
if (!empty($files) &&
!is_array($files))
elseif (!empty($files) &&
is_array($files))
foreach ($files as $file)
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
JLog::add('JCacheStorageFile::_deleteFolder' .
JText::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', $filename), JLog::WARNING, 'jerror');
// Remove sub-folders of folder; disable all filtering
$folders =
$this->_folders($path, '.', false, true, array(), array());
foreach ($folders as $folder)
// Don't descend into linked directories, just delete the link.
if (@unlink($folder) !==
true)
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
JLog::add('JCacheStorageFile::_deleteFolder' .
JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path), JLog::WARNING, 'jerror');
* Function to strip additional / or \ in a path name
* @param string $path The path to clean
* @param string $ds Directory separator (optional)
* @return string The cleaned path
protected function _cleanPath($path, $ds =
DIRECTORY_SEPARATOR)
// Remove double slashes and backslahses and convert all slashes and backslashes to DIRECTORY_SEPARATOR
* Utility function to quickly read the files in a folder.
* @param string $path The path of the folder to read.
* @param string $filter A filter for file names.
* @param mixed $recurse True to recursively search into sub-folders, or an
* integer to specify the maximum depth.
* @param boolean $fullpath True to return the full path to the file.
* @param array $exclude Array with names of files which should not be shown in
* @param array $excludefilter Array of folder names to exclude
* @return array Files in the given folder.
protected function _filesInFolder($path, $filter =
'.', $recurse =
false, $fullpath =
false
, $exclude =
array('.svn', 'CVS', '.DS_Store', '__MACOSX'), $excludefilter =
array('^\..*', '.*~'))
// Check to make sure the path valid and clean
JLog::add('JCacheStorageFile::_filesInFolder' .
JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path), JLog::WARNING, 'jerror');
// Read the source directory.
if (count($excludefilter))
$excludefilter =
'/(' .
implode('|', $excludefilter) .
')/';
while (($file =
readdir($handle)) !==
false)
if (($file !=
'.') &&
($file !=
'..') &&
(!in_array($file, $exclude)) &&
(!$excludefilter ||
!preg_match($excludefilter, $file)))
$dir =
$path .
'/' .
$file;
$arr2 =
$this->_filesInFolder($dir, $filter, $recurse -
1, $fullpath);
$arr[] =
$path .
'/' .
$file;
* Utility function to read the folders in a folder.
* @param string $path The path of the folder to read.
* @param string $filter A filter for folder names.
* @param mixed $recurse True to recursively search into sub-folders, or an integer to specify the maximum depth.
* @param boolean $fullpath True to return the full path to the folders.
* @param array $exclude Array with names of folders which should not be shown in the result.
* @param array $excludefilter Array with regular expressions matching folders which should not be shown in the result.
* @return array Folders in the given folder.
protected function _folders($path, $filter =
'.', $recurse =
false, $fullpath =
false
, $exclude =
array('.svn', 'CVS', '.DS_Store', '__MACOSX'), $excludefilter =
array('^\..*'))
// Check to make sure the path valid and clean
JLog::add('JCacheStorageFile::_folders' .
JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path), JLog::WARNING, 'jerror');
// Read the source directory
if (count($excludefilter))
$excludefilter_string =
'/(' .
implode('|', $excludefilter) .
')/';
$excludefilter_string =
'';
while (($file =
readdir($handle)) !==
false)
if (($file !=
'.') &&
($file !=
'..')
&&
(empty($excludefilter_string) ||
!preg_match($excludefilter_string, $file)))
$dir =
$path .
'/' .
$file;
// Removes filtered directories
$arr2 =
$this->_folders($dir, $filter, $recurse -
1, $fullpath, $exclude, $excludefilter);
$arr2 =
$this->_folders($dir, $filter, $recurse, $fullpath, $exclude, $excludefilter);
Documentation generated on Tue, 19 Nov 2013 15:03:10 +0100 by phpDocumentor 1.4.3