From 37775357c1dbc15bee0e68c662762d4841186b68 Mon Sep 17 00:00:00 2001 From: jaswsinc Date: Thu, 15 Jan 2015 23:43:03 -0900 Subject: [PATCH 1/3] Improving clear/purge/wipe utilities in the `share` class. See: websharks/quick-cache#397 --- zencache-pro/includes/share.php | 189 +++++++++++++++++++++++--------- 1 file changed, 136 insertions(+), 53 deletions(-) diff --git a/zencache-pro/includes/share.php b/zencache-pro/includes/share.php index eb830d3..1f8f517 100644 --- a/zencache-pro/includes/share.php +++ b/zencache-pro/includes/share.php @@ -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)); @@ -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)); @@ -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)); @@ -1811,7 +1894,7 @@ public function delete_all_files_dirs_in($dir, $delete_dir_too = FALSE) public function cache_lock() { if($this->apply_wp_filters(__CLASS__.'_disable_cache_locking', FALSE)) - return false; + return FALSE; if(!($wp_config_file = $this->find_wp_config_file())) throw new \exception(__('Unable to find the wp-config.php file.', $this->text_domain)); @@ -1833,7 +1916,7 @@ public function cache_lock() throw new \exception(__('No writable tmp directory.', $this->text_domain)); $inode_key = fileinode($wp_config_file); - $mutex = $tmp_dir.'/'.$this->slug.'-'.$inode_key.'.lock'; + $mutex = $tmp_dir.'/'.$this->slug.'-'.$inode_key.'.lock'; if(!($resource = fopen($mutex, 'w')) || !flock($resource, LOCK_EX)) throw new \exception(__('Unable to obtain an exclusive lock.', $this->text_domain)); From 42a0dc8902424e2810b46415b6da31665764c9ec Mon Sep 17 00:00:00 2001 From: jaswsinc Date: Fri, 16 Jan 2015 00:14:29 -0900 Subject: [PATCH 2/3] Improving back compat. w/ Quick Cache constants. --- zencache-pro/includes/advanced-cache.tpl.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zencache-pro/includes/advanced-cache.tpl.php b/zencache-pro/includes/advanced-cache.tpl.php index 84df194..cddc4f1 100644 --- a/zencache-pro/includes/advanced-cache.tpl.php +++ b/zencache-pro/includes/advanced-cache.tpl.php @@ -1234,6 +1234,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. From 88474b93dddbd89db750adb0b196dd61d684863e Mon Sep 17 00:00:00 2001 From: jaswsinc Date: Fri, 16 Jan 2015 00:23:24 -0900 Subject: [PATCH 3/3] Improving back compat. w/ Quick Cache constants. --- zencache-pro/includes/advanced-cache.tpl.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zencache-pro/includes/advanced-cache.tpl.php b/zencache-pro/includes/advanced-cache.tpl.php index cddc4f1..e4261fd 100644 --- a/zencache-pro/includes/advanced-cache.tpl.php +++ b/zencache-pro/includes/advanced-cache.tpl.php @@ -1621,6 +1621,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']; } }