function GetAllFilesInSubfolders($dirname) { $AllFiles = array(); $dirname = rtrim(realpath($dirname), '/\\'); if ($dirhandle = @opendir($dirname)) { while (($file = readdir($dirhandle)) !== false) { $fullfilename = $dirname . DIRECTORY_SEPARATOR . $file; if (is_file($fullfilename)) { $AllFiles[] = $fullfilename; } elseif (is_dir($fullfilename)) { switch ($file) { case '.': case '..': break; default: $AllFiles[] = $fullfilename; $subfiles = phpthumb_functions::GetAllFilesInSubfolders($fullfilename); foreach ($subfiles as $filename) { $AllFiles[] = $filename; } break; } } else { // ignore? } } closedir($dirhandle); } sort($AllFiles); return array_unique($AllFiles); }
function CleanUpCacheDirectory() { $this->DebugMessage('CleanUpCacheDirectory() set to purge (' . (is_null($this->config_cache_maxage) ? 'NULL' : number_format($this->config_cache_maxage / 86400, 1)) . ' days; ' . (is_null($this->config_cache_maxsize) ? 'NULL' : number_format($this->config_cache_maxsize / 1048576, 2)) . ' MB; ' . (is_null($this->config_cache_maxfiles) ? 'NULL' : number_format($this->config_cache_maxfiles)) . ' files)', __FILE__, __LINE__); if (!is_writable($this->config_cache_directory)) { $this->DebugMessage('CleanUpCacheDirectory() skipped because "' . $this->config_cache_directory . '" is not writable', __FILE__, __LINE__); return true; } // cache status of cache directory for 1 hour to avoid hammering the filesystem functions $phpThumbCacheStats_filename = $this->config_cache_directory . DIRECTORY_SEPARATOR . 'phpThumbCacheStats.txt'; if (file_exists($phpThumbCacheStats_filename) && is_readable($phpThumbCacheStats_filename) && filemtime($phpThumbCacheStats_filename) >= time() - 3600) { $this->DebugMessage('CleanUpCacheDirectory() skipped because "' . $phpThumbCacheStats_filename . '" is recently modified', __FILE__, __LINE__); return true; } touch($phpThumbCacheStats_filename); $DeletedKeys = array(); $AllFilesInCacheDirectory = array(); if ($this->config_cache_maxage > 0 || $this->config_cache_maxsize > 0 || $this->config_cache_maxfiles > 0) { $CacheDirOldFilesAge = array(); $CacheDirOldFilesSize = array(); $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory); foreach ($AllFilesInCacheDirectory as $fullfilename) { if (preg_match('#' . preg_quote($this->config_cache_prefix) . '#i', $fullfilename) && file_exists($fullfilename)) { $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename); if ($CacheDirOldFilesAge[$fullfilename] == 0) { $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename); } $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename); } } if (empty($CacheDirOldFilesSize)) { return true; } $DeletedKeys['zerobyte'] = array(); foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) { // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files) $cutofftime = time() - 3600; if ($filesize == 0 && $CacheDirOldFilesAge[$fullfilename] < $cutofftime) { $this->DebugMessage('deleting "' . $fullfilename . '"', __FILE__, __LINE__); if (@unlink($fullfilename)) { $DeletedKeys['zerobyte'][] = $fullfilename; unset($CacheDirOldFilesSize[$fullfilename]); unset($CacheDirOldFilesAge[$fullfilename]); } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['zerobyte']) . ' zero-byte files', __FILE__, __LINE__); asort($CacheDirOldFilesAge); if ($this->config_cache_maxfiles > 0) { $TotalCachedFiles = count($CacheDirOldFilesAge); $DeletedKeys['maxfiles'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFiles > $this->config_cache_maxfiles) { $this->DebugMessage('deleting "' . $fullfilename . '"', __FILE__, __LINE__); if (@unlink($fullfilename)) { $TotalCachedFiles--; $DeletedKeys['maxfiles'][] = $fullfilename; } } else { // there are few enough files to keep the rest break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxfiles']) . ' files based on (config_cache_maxfiles=' . $this->config_cache_maxfiles . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxfiles'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxage > 0) { $mindate = time() - $this->config_cache_maxage; $DeletedKeys['maxage'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($filedate > 0) { if ($filedate < $mindate) { $this->DebugMessage('deleting "' . $fullfilename . '"', __FILE__, __LINE__); if (@unlink($fullfilename)) { $DeletedKeys['maxage'][] = $fullfilename; } } else { // the rest of the files are new enough to keep break; } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxage']) . ' files based on (config_cache_maxage=' . $this->config_cache_maxage . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxage'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxsize > 0) { $TotalCachedFileSize = array_sum($CacheDirOldFilesSize); $DeletedKeys['maxsize'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFileSize > $this->config_cache_maxsize) { $this->DebugMessage('deleting "' . $fullfilename . '"', __FILE__, __LINE__); if (@unlink($fullfilename)) { $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename]; $DeletedKeys['maxsize'][] = $fullfilename; } } else { // the total filesizes are small enough to keep the rest of the files break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxsize']) . ' files based on (config_cache_maxsize=' . $this->config_cache_maxsize . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxsize'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } } else { $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__); } $totalpurged = 0; foreach ($DeletedKeys as $key => $value) { $totalpurged += count($value); } $this->DebugMessage('CleanUpCacheDirectory() purged ' . $totalpurged . ' files (from ' . count($AllFilesInCacheDirectory) . ') based on config settings', __FILE__, __LINE__); if ($totalpurged > 0) { $empty_dirs = array(); foreach ($AllFilesInCacheDirectory as $fullfilename) { if (is_dir($fullfilename)) { $empty_dirs[realpath($fullfilename)] = 1; } else { unset($empty_dirs[realpath(dirname($fullfilename))]); } } krsort($empty_dirs); $totalpurgeddirs = 0; foreach ($empty_dirs as $empty_dir => $dummy) { if ($empty_dir == $this->config_cache_directory) { // shouldn't happen, but just in case, don't let it delete actual cache directory continue; } elseif (@rmdir($empty_dir)) { $totalpurgeddirs++; } else { $this->DebugMessage('failed to rmdir(' . $empty_dir . ')', __FILE__, __LINE__); } } $this->DebugMessage('purged ' . $totalpurgeddirs . ' empty directories', __FILE__, __LINE__); } return true; }
function GetAllFilesInSubfolders($dirname) { $AllFiles = array(); $dirname = rtrim(realpath($dirname), '/\\'); if ($dirhandle = @opendir($dirname)) { while ($file = readdir($dirhandle)) { $fullfilename = $dirname . DIRECTORY_SEPARATOR . $file; if (is_file($fullfilename)) { $AllFiles[] = $fullfilename; } elseif (is_dir($fullfilename)) { if ($file == '.' || $file == '..') { continue; } $subfiles = phpthumb_functions::GetAllFilesInSubfolders($fullfilename); foreach ($subfiles as $filename) { $AllFiles[] = $filename; } } else { // ignore? } } closedir($dirhandle); } sort($AllFiles); return array_unique($AllFiles); }
function CleanUpCacheDirectory() { $this->DebugMessage('skipping CleanUpCacheDirectory() set to purge (' . number_format($this->config_cache_maxage / 86400, 1) . ' days; ' . number_format($this->config_cache_maxsize / 1048576, 2) . 'MB; ' . number_format($this->config_cache_maxfiles) . ' files)', __FILE__, __LINE__); $DeletedKeys = array(); $AllFilesInCacheDirectory = array(); if ($this->config_cache_maxage > 0 || $this->config_cache_maxsize > 0 || $this->config_cache_maxfiles > 0) { $CacheDirOldFilesAge = array(); $CacheDirOldFilesSize = array(); $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory); foreach ($AllFilesInCacheDirectory as $fullfilename) { if (preg_match('/^phpThumb\\_cache\\_/i', basename($fullfilename)) && file_exists($fullfilename)) { $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename); if ($CacheDirOldFilesAge[$fullfilename] == 0) { $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename); } $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename); } } if (empty($CacheDirOldFilesSize)) { return true; } $DeletedKeys['zerobyte'] = array(); foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) { // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files) $cutofftime = time() - 3600; if ($filesize == 0 && $CacheDirOldFilesAge[$fullfilename] < $cutofftime) { if (@unlink($fullfilename)) { $DeletedKeys['zerobyte'][] = $fullfilename; unset($CacheDirOldFilesSize[$fullfilename]); unset($CacheDirOldFilesAge[$fullfilename]); } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['zerobyte']) . ' zero-byte files', __FILE__, __LINE__); asort($CacheDirOldFilesAge); if ($this->config_cache_maxfiles > 0) { $TotalCachedFiles = count($CacheDirOldFilesAge); $DeletedKeys['maxfiles'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFiles > $this->config_cache_maxfiles) { if (@unlink($fullfilename)) { $TotalCachedFiles--; $DeletedKeys['maxfiles'][] = $fullfilename; } } else { // there are few enough files to keep the rest break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxfiles']) . ' files based on (config_cache_maxfiles=' . $this->config_cache_maxfiles . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxfiles'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxage > 0) { $mindate = time() - $this->config_cache_maxage; $DeletedKeys['maxage'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($filedate > 0) { if ($filedate < $mindate) { if (@unlink($fullfilename)) { $DeletedKeys['maxage'][] = $fullfilename; } } else { // the rest of the files are new enough to keep break; } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxage']) . ' files based on (config_cache_maxage=' . $this->config_cache_maxage . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxage'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxsize > 0) { $TotalCachedFileSize = array_sum($CacheDirOldFilesSize); $DeletedKeys['maxsize'] = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFileSize > $this->config_cache_maxsize) { if (@unlink($fullfilename)) { $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename]; $DeletedKeys['maxsize'][] = $fullfilename; } } else { // the total filesizes are small enough to keep the rest of the files break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys['maxsize']) . ' files based on (config_cache_maxsize=' . $this->config_cache_maxsize . ')', __FILE__, __LINE__); foreach ($DeletedKeys['maxsize'] as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } } else { $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__); } $totalpurged = 0; foreach ($DeletedKeys as $key => $value) { $totalpurged += count($value); } $this->DebugMessage('CleanUpCacheDirectory() purged ' . $totalpurged . ' files (from ' . count($AllFilesInCacheDirectory) . ') based on config settings', __FILE__, __LINE__); if ($totalpurged > 0) { $empty_dirs = array(); foreach ($AllFilesInCacheDirectory as $fullfilename) { if (is_dir($fullfilename)) { $empty_dirs[realpath($fullfilename)] = 1; } else { unset($empty_dirs[realpath(dirname($fullfilename))]); } } krsort($empty_dirs); $totalpurgeddirs = 0; foreach ($empty_dirs as $empty_dir => $dummy) { if ($empty_dir == $this->config_cache_directory) { // shouldn't happen, but just in case, don't let it delete actual cache directory continue; } elseif (@rmdir($empty_dir)) { $totalpurgeddirs++; } else { $this->DebugMessage('failed to rmdir(' . $empty_dir . ')', __FILE__, __LINE__); } } $this->DebugMessage('purged ' . $totalpurgeddirs . ' empty directories', __FILE__, __LINE__); } return true; }
function CleanUpCacheDirectory() { if ($this->config_cache_maxage > 0 || $this->config_cache_maxsize > 0 || $this->config_cache_maxfiles > 0) { $CacheDirOldFilesAge = array(); $CacheDirOldFilesSize = array(); $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory); foreach ($AllFilesInCacheDirectory as $fullfilename) { if (eregi('^phpThumb_cache_', $fullfilename) && file_exists($fullfilename)) { $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename); if ($CacheDirOldFilesAge[$fullfilename] == 0) { $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename); } $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename); } } if (empty($CacheDirOldFilesSize)) { return true; } $DeletedKeys = array(); foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) { // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files) $cutofftime = time() - 3600; if ($filesize == 0 && $CacheDirOldFilesAge[$fullfilename] < $cutofftime) { if (@unlink($fullfilename)) { $DeletedKeys[] = $fullfilename; unset($CacheDirOldFilesSize[$fullfilename]); unset($CacheDirOldFilesAge[$fullfilename]); } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys) . ' zero-byte files', __FILE__, __LINE__); asort($CacheDirOldFilesAge); if ($this->config_cache_maxfiles > 0) { $TotalCachedFiles = count($CacheDirOldFilesAge); $DeletedKeys = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFiles > $this->config_cache_maxfiles) { if (@unlink($fullfilename)) { $TotalCachedFiles--; $DeletedKeys[] = $fullfilename; } } else { // there are few enough files to keep the rest break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys) . ' files based on (config_cache_maxfiles=' . $this->config_cache_maxfiles . ')', __FILE__, __LINE__); foreach ($DeletedKeys as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxage > 0) { $mindate = time() - $this->config_cache_maxage; $DeletedKeys = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($filedate > 0) { if ($filedate < $mindate) { if (@unlink($fullfilename)) { $DeletedKeys[] = $fullfilename; } } else { // the rest of the files are new enough to keep break; } } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys) . ' files based on (config_cache_maxage=' . $this->config_cache_maxage . ')', __FILE__, __LINE__); foreach ($DeletedKeys as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } if ($this->config_cache_maxsize > 0) { $TotalCachedFileSize = array_sum($CacheDirOldFilesSize); $DeletedKeys = array(); foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { if ($TotalCachedFileSize > $this->config_cache_maxsize) { if (@unlink($fullfilename)) { $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename]; $DeletedKeys[] = $fullfilename; } } else { // the total filesizes are small enough to keep the rest of the files break; } } $this->DebugMessage('CleanUpCacheDirectory() purged ' . count($DeletedKeys) . ' files based on (config_cache_maxsize=' . $this->config_cache_maxsize . ')', __FILE__, __LINE__); foreach ($DeletedKeys as $fullfilename) { unset($CacheDirOldFilesAge[$fullfilename]); unset($CacheDirOldFilesSize[$fullfilename]); } } } else { $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__); } return true; }