mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Refactoring FileEngine cache class
Refactoring Folder class git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5339 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
a08f18c4a1
commit
4a9748e7f5
2 changed files with 88 additions and 17 deletions
53
cake/libs/cache/file.php
vendored
53
cake/libs/cache/file.php
vendored
|
@ -29,6 +29,9 @@
|
||||||
* Included libraries.
|
* Included libraries.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
if (!class_exists('File')) {
|
||||||
|
uses ('file');
|
||||||
|
}
|
||||||
if (!class_exists('Folder')) {
|
if (!class_exists('Folder')) {
|
||||||
uses ('folder');
|
uses ('folder');
|
||||||
}
|
}
|
||||||
|
@ -40,6 +43,14 @@ if (!class_exists('Folder')) {
|
||||||
* @subpackage cake.cake.libs.cache
|
* @subpackage cake.cake.libs.cache
|
||||||
*/
|
*/
|
||||||
class FileEngine extends CacheEngine {
|
class FileEngine extends CacheEngine {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does the cache engine handle prefixes on it's own?
|
||||||
|
*
|
||||||
|
* @var boolean
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $_usesPrefixes = true;
|
||||||
/**
|
/**
|
||||||
* Cache directory
|
* Cache directory
|
||||||
*
|
*
|
||||||
|
@ -117,17 +128,31 @@ class FileEngine extends CacheEngine {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$expires = time() + $duration;
|
$expires = time() + $duration;
|
||||||
return $this->_writeCache($this->_getFilename($key), $serialized, $expires);
|
|
||||||
|
$fileName = $this->_getFilename($key);
|
||||||
|
if ($fileName === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $this->_writeCache($fileName, $serialized, $expires);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Get absolute filename for a key
|
* Get absolute filename for a key
|
||||||
*
|
*
|
||||||
* @param string $key The key
|
* @param string $key The key
|
||||||
* @return string Absolute cache filename for the given key
|
* @return mixed Absolute cache filename for the given key or false if erroneous
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _getFilename($key) {
|
function _getFilename($key) {
|
||||||
return $this->_dir . $this->_prefix . $this->base64url_encode($key);
|
$fullpath = $this->_dir . $key;
|
||||||
|
$directoryName = dirname($fullpath);
|
||||||
|
$fileName = $this->_prefix.basename($fullpath);
|
||||||
|
$fullpath = Folder::realpath($directoryName . DS . $fileName);
|
||||||
|
|
||||||
|
$folder = new Folder($this->_dir);
|
||||||
|
if (!$folder->inPath($fullpath, true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $fullpath;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* write serialized data to a file
|
* write serialized data to a file
|
||||||
|
@ -139,6 +164,13 @@ class FileEngine extends CacheEngine {
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
function _writeCache(&$filename, &$value, &$expires) {
|
function _writeCache(&$filename, &$value, &$expires) {
|
||||||
|
$directoryName = dirname($filename);
|
||||||
|
if (!is_writable($directoryName)) {
|
||||||
|
$folder = new Folder($directoryName);
|
||||||
|
if (!$folder->create($directoryName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
$contents = $expires."\n".$value."\n";
|
$contents = $expires."\n".$value."\n";
|
||||||
return ife(file_put_contents($filename, $contents, ife($this->_lock, LOCK_EX, 0)), true, false);
|
return ife(file_put_contents($filename, $contents, ife($this->_lock, LOCK_EX, 0)), true, false);
|
||||||
}
|
}
|
||||||
|
@ -152,7 +184,7 @@ class FileEngine extends CacheEngine {
|
||||||
function read($key) {
|
function read($key) {
|
||||||
$filename = $this->_getFilename($key);
|
$filename = $this->_getFilename($key);
|
||||||
|
|
||||||
if (!is_file($filename) || !is_readable($filename)) {
|
if ($filename === false || !is_file($filename) || !is_readable($filename)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$fp = fopen($filename, 'r');
|
$fp = fopen($filename, 'r');
|
||||||
|
@ -209,6 +241,9 @@ class FileEngine extends CacheEngine {
|
||||||
*/
|
*/
|
||||||
function delete($key) {
|
function delete($key) {
|
||||||
$filename = $this->_getFilename($key);
|
$filename = $this->_getFilename($key);
|
||||||
|
if ($filename === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return unlink($filename);
|
return unlink($filename);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -265,15 +300,5 @@ class FileEngine extends CacheEngine {
|
||||||
'prefix' => $this->_prefix,
|
'prefix' => $this->_prefix,
|
||||||
'lock' => $lock);
|
'lock' => $lock);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Get a filename-safe version of a string
|
|
||||||
*
|
|
||||||
* @param string $str String to encode
|
|
||||||
* @return string Encoded version of the string
|
|
||||||
* @access public
|
|
||||||
*/
|
|
||||||
function base64url_encode($str) {
|
|
||||||
return strtr(base64_encode($str), '+/', '-_');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
|
@ -114,7 +114,7 @@ class Folder extends Object{
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function cd($path) {
|
function cd($path) {
|
||||||
$path = realpath($path);
|
$path = $this->realpath($path);
|
||||||
if (!$this->isAbsolute($path)) {
|
if (!$this->isAbsolute($path)) {
|
||||||
$path = $this->addPathElement($this->path, $path);
|
$path = $this->addPathElement($this->path, $path);
|
||||||
}
|
}
|
||||||
|
@ -334,9 +334,19 @@ class Folder extends Object{
|
||||||
* @return boolean
|
* @return boolean
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function inPath($path = '') {
|
function inPath($path = '', $reverse = false) {
|
||||||
|
if (!$this->isAbsolute($path)) {
|
||||||
|
$path = $this->addPathElement($this->path, $path);
|
||||||
|
}
|
||||||
|
$path = $this->realpath($path);
|
||||||
$dir = substr($this->slashTerm($path), 0, -1);
|
$dir = substr($this->slashTerm($path), 0, -1);
|
||||||
$return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
|
|
||||||
|
if (!$reverse) {
|
||||||
|
$return = preg_match('/^' . preg_quote($this->slashTerm($dir), '/') . '(.*)/', $this->slashTerm($this->pwd()));
|
||||||
|
} else {
|
||||||
|
$return = preg_match('/^' . preg_quote($this->slashTerm($this->pwd()), '/') . '(.*)/', $this->slashTerm($dir));
|
||||||
|
}
|
||||||
|
|
||||||
if ($return == 1) {
|
if ($return == 1) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -671,6 +681,42 @@ class Folder extends Object{
|
||||||
function rm($path) {
|
function rm($path) {
|
||||||
return $this->delete($path);
|
return $this->delete($path);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Get the real path (taking ".." and such into account)
|
||||||
|
*
|
||||||
|
* @param string $path Path to resolve
|
||||||
|
* @return string The resolved path
|
||||||
|
*/
|
||||||
|
function realpath($path) {
|
||||||
|
$path = trim($path);
|
||||||
|
if (strpos($path, '..') === false) {
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
$parts = explode(DS, $path);
|
||||||
|
$newparts = array();
|
||||||
|
$newpath = ife($path{0} == DS, DS, '');
|
||||||
|
|
||||||
|
while (($part = array_shift($parts)) !== NULL) {
|
||||||
|
if ($part == '.' || $part == '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($part == '..') {
|
||||||
|
if (count($newparts) > 0) {
|
||||||
|
array_pop($newparts);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$newparts[] = $part;
|
||||||
|
}
|
||||||
|
$newpath .= implode(DS, $newparts);
|
||||||
|
|
||||||
|
if (strlen($path > 1) && $path{strlen($path)-1} == DS) {
|
||||||
|
$newpath .= DS;
|
||||||
|
}
|
||||||
|
return $newpath;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @deprecated
|
* @deprecated
|
||||||
|
|
Loading…
Add table
Reference in a new issue