Skip to content

Commit

Permalink
Merge branch 'feature/#397' into 000000-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
raamdev committed Jan 16, 2015
2 parents edd2d56 + 855455d commit 978a994
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 51 deletions.
5 changes: 5 additions & 0 deletions zencache/includes/advanced-cache.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,8 @@ public function output_buffer_callback_handler($buffer, $phase)

# Exclusion checks; there are MANY of these...

advanced_cache_back_compat::QUICK_CACHE_constants();

$cache = trim((string)$buffer);
if(!isset($cache[0])) // Allows a `0`.
return FALSE; // Don't cache an empty buffer.
Expand Down Expand Up @@ -1169,6 +1171,9 @@ public static function QUICK_CACHE_constants()
define('ZENCACHE_'.$_constant_sub_name, $_value);
}
unset($_constant, $_value); // Housekeeping.

if(isset($_SERVER['QUICK_CACHE_ALLOWED']) && !isset($_SERVER['ZENCACHE_ALLOWED']))
$_SERVER['ZENCACHE_ALLOWED'] = $_SERVER['QUICK_CACHE_ALLOWED'];
}
}

Expand Down
185 changes: 134 additions & 51 deletions zencache/includes/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -1541,32 +1541,63 @@ public function delete_files_from_cache_dir($regex, $check_max_age = FALSE)
if(!rename($cache_dir, $cache_dir_tmp)) // Work from tmp directory so deletions are atomic.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on directory: `%1$s`.', $this->text_domain), $cache_dir));

/** @var $_file_dir \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($cache_dir_tmp, $cache_dir_tmp_regex)) as $_file_dir)
/** @var $_resource \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($cache_dir_tmp, $cache_dir_tmp_regex)) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink()) // Files and/or symlinks only.
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

// Don't delete files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache files are nested. Files in the immediate directory are for other purposes.
&& (strpos($_file_dir->getSubpathname(), '/') !== FALSE)
if($_resource_type !== 'dir' && strpos($_sub_path_name, '/') === FALSE)
continue; // Don't delete links/files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache links/files are nested. Links/files in the immediate directory are for other purposes.

// If NOT checking max age; or if we ARE, and the file has expired now.
&& (!$check_max_age || (!empty($max_age) && $_file_dir->getMTime() < $max_age))

) // Throw an exception if a deletion failure occurs.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if(!$check_max_age && $regex === '/^.+/i' && $_file_dir->isDir()) // Directories too?
switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.
case 'link': // Symbolic links; i.e. 404 errors.

if($check_max_age && !empty($max_age) && is_file($_resource->getLinkTarget()))
if(($_lstat = lstat($_path_name)) && !empty($_lstat['mtime']))
if($_lstat['mtime'] >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if($check_max_age && !empty($max_age)) // Should check max age?
if($_resource->getMTime() >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if($regex !== '/^.+/i') // Deleting everything?
break; // Break switch handler. Not deleting everything.

if($check_max_age && !empty($max_age)) // Should check max age?
break; // Break switch handler. Not deleting everything in this case.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name, $_lstat); // Housekeeping.

if(!rename($cache_dir_tmp, $cache_dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $cache_dir_tmp));
Expand Down Expand Up @@ -1672,31 +1703,62 @@ public function delete_files_from_host_cache_dir($regex, $check_max_age = FALSE)
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $_host_cache_dir));

/** @var $_file_dir \RecursiveDirectoryIterator Regex iterator reference for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($_host_cache_dir_tmp, $_host_cache_dir_tmp_regex)) as $_file_dir)
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($_host_cache_dir_tmp, $_host_cache_dir_tmp_regex)) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink()) // Files and/or symlinks only.

// Don't delete files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache files are nested. Files in the immediate directory are for other purposes.
&& ($_host_cache_dir !== $cache_dir || strpos($_file_dir->getSubpathname(), '/') !== FALSE)
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

// If NOT checking max age; or if we ARE, and the file has expired now.
&& (!$check_max_age || (!empty($max_age) && $_file_dir->getMTime() < $max_age))
if($_host_cache_dir === $cache_dir && $_resource_type !== 'dir' && strpos($_sub_path_name, '/') === FALSE)
continue; // Don't delete links/files in the immediate directory; e.g. `zc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache links/files are nested. Links/files in the immediate directory are for other purposes.

) // Throw an exception if a deletion failure occurs.
switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if(!$check_max_age && $regex === '/^.+/i' && $_file_dir->isDir()) // Directories too?
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.
case 'link': // Symbolic links; i.e. 404 errors.

if($check_max_age && !empty($max_age) && is_file($_resource->getLinkTarget()))
if(($_lstat = lstat($_path_name)) && !empty($_lstat['mtime']))
if($_lstat['mtime'] >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if($check_max_age && !empty($max_age)) // Should check max age?
if($_resource->getMTime() >= $max_age) // Still valid?
break; // Break switch handler.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if($regex !== '/^.+/i') // Deleting everything?
break; // Break switch handler. Not deleting everything.

if($check_max_age && !empty($max_age)) // Should check max age?
break; // Break switch handler. Not deleting everything in this case.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
# $counter++; // Increment counter for each directory we delete. ~ NO don't do that here.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name, $_lstat); // Housekeeping.

if(!rename($_host_cache_dir_tmp, $_host_cache_dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete files. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $_host_cache_dir_tmp));
Expand Down Expand Up @@ -1756,22 +1818,43 @@ public function delete_all_files_dirs_in($dir, $delete_dir_too = FALSE)
throw new \exception(sprintf(__('Unable to delete all files/dirs. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $dir));

/** @var $_file_dir \RecursiveDirectoryIterator for IDEs. */
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($dir_temp, '/.+/')) as $_file_dir)
foreach(($_dir_regex_iteration = $this->dir_regex_iteration($dir_temp, '/.+/')) as $_resource)
{
if(($_file_dir->isFile() || $_file_dir->isLink())) // Files and/or symlinks.
{
if(!unlink($_file_dir->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each file we delete.
}
else if($_file_dir->isDir()) // Directories are last in the iteration; it should be empty now.
$_resource_type = $_resource->getType();
$_sub_path_name = $_resource->getSubpathname();
$_path_name = $_resource->getPathname();

switch($_resource_type) // Based on type; i.e. `link`, `file`, `dir`.
{
if(!rmdir($_file_dir->getPathname())) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_file_dir->getPathname()));
$counter++; // Increment counter for each directory we delete.
case 'link': // Symbolic links; i.e. 404 errors.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete symlink: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each link we delete.

break; // Break switch handler.

case 'file': // Regular files; i.e. not symlinks.

if(!unlink($_path_name)) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to delete file: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each file we delete.

break; // Break switch handler.

case 'dir': // A regular directory; i.e. not a symlink.

if(!rmdir($_path_name)) // Throw exception if unable to delete the directory itself.
throw new \exception(sprintf(__('Unable to delete dir: `%1$s`.', $this->text_domain), $_path_name));
$counter++; // Increment counter for each directory we delete.

break; // Break switch handler.

default: // Something else that is totally unexpected here.
throw new \exception(sprintf(__('Unexpected resource type: `%1$s`.', $this->text_domain), $_resource_type));
}
}
unset($_dir_regex_iteration, $_file_dir); // Housekeeping after this `foreach()` loop.
unset($_dir_regex_iteration, $_resource, $_resource_type, $_sub_path_name, $_path_name); // Housekeeping.

if(!rename($dir_temp, $dir)) // Deletions are atomic; restore original directory now.
throw new \exception(sprintf(__('Unable to delete all files/dirs. Rename failure on tmp directory: `%1$s`.', $this->text_domain), $dir_temp));
Expand Down

0 comments on commit 978a994

Please sign in to comment.