Diff
6 years ago
dashboard
7 years ago
rest-api
6 years ago
.htaccess
7 years ago
Diff.php
6 years ago
GeoLite2-Country.mmdb
6 years ago
IPTraf.php
6 years ago
IPTrafList.php
7 years ago
WFLSPHP52Compatability.php
6 years ago
compat.php
8 years ago
conntest.php
7 years ago
cronview.php
8 years ago
dbview.php
8 years ago
diffResult.php
8 years ago
email_genericAlert.php
7 years ago
email_newIssues.php
6 years ago
email_unlockRequest.php
8 years ago
email_unsubscribeRequest.php
7 years ago
flags.php
7 years ago
live_activity.php
8 years ago
menu_dashboard.php
7 years ago
menu_dashboard_options.php
7 years ago
menu_firewall.php
6 years ago
menu_firewall_blocking.php
7 years ago
menu_firewall_blocking_options.php
8 years ago
menu_firewall_waf.php
7 years ago
menu_firewall_waf_options.php
7 years ago
menu_options.php
6 years ago
menu_scanner.php
6 years ago
menu_scanner_credentials.php
8 years ago
menu_scanner_options.php
6 years ago
menu_support.php
7 years ago
menu_tools.php
7 years ago
menu_tools_diagnostic.php
6 years ago
menu_tools_importExport.php
7 years ago
menu_tools_livetraffic.php
6 years ago
menu_tools_twoFactor.php
7 years ago
menu_tools_whois.php
8 years ago
menu_wordfence_central.php
7 years ago
noc1.key
7 years ago
sysinfo.php
8 years ago
unknownFiles.php
8 years ago
viewFullActivityLog.php
8 years ago
wf503.php
7 years ago
wfAPI.php
6 years ago
wfActivityReport.php
6 years ago
wfAdminNoticeQueue.php
8 years ago
wfAlerts.php
6 years ago
wfArray.php
7 years ago
wfBrowscap.php
6 years ago
wfBrowscapCache.php
7 years ago
wfBulkCountries.php
7 years ago
wfCache.php
6 years ago
wfCentralAPI.php
6 years ago
wfConfig.php
6 years ago
wfCrawl.php
6 years ago
wfCredentialsController.php
7 years ago
wfCrypt.php
6 years ago
wfDB.php
7 years ago
wfDashboard.php
7 years ago
wfDateLocalization.php
8 years ago
wfDiagnostic.php
6 years ago
wfDict.php
8 years ago
wfDirectoryIterator.php
7 years ago
wfHelperBin.php
11 years ago
wfHelperString.php
11 years ago
wfIPWhitelist.php
7 years ago
wfImportExportController.php
7 years ago
wfIssues.php
6 years ago
wfJWT.php
7 years ago
wfLockedOut.php
7 years ago
wfLog.php
6 years ago
wfMD5BloomFilter.php
8 years ago
wfModuleController.php
7 years ago
wfNotification.php
8 years ago
wfOnboardingController.php
7 years ago
wfPersistenceController.php
8 years ago
wfRESTAPI.php
7 years ago
wfScan.php
6 years ago
wfScanEngine.php
6 years ago
wfSchema.php
6 years ago
wfStyle.php
7 years ago
wfSupportController.php
6 years ago
wfUnlockMsg.php
6 years ago
wfUpdateCheck.php
6 years ago
wfUtils.php
6 years ago
wfVersionCheckController.php
8 years ago
wfView.php
10 years ago
wfViewResult.php
8 years ago
wordfenceClass.php
6 years ago
wordfenceConstants.php
6 years ago
wordfenceHash.php
6 years ago
wordfenceScanner.php
6 years ago
wordfenceURLHoover.php
6 years ago
wfCache.php
204 lines
| 1 | <?php |
| 2 | class wfCache { |
| 3 | private static $cacheStats = array(); |
| 4 | private static $cacheClearedThisRequest = false; |
| 5 | private static $lastRecursiveDeleteError = false; |
| 6 | |
| 7 | public static function removeCaching() { |
| 8 | $cacheType = wfConfig::get('cacheType', false); |
| 9 | if ($cacheType === 'disabled') { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | if ($cacheType == 'falcon') { |
| 14 | self::addHtaccessCode('remove'); |
| 15 | self::updateBlockedIPs('remove'); |
| 16 | } |
| 17 | |
| 18 | wfConfig::set('cacheType', 'disabled'); |
| 19 | |
| 20 | $cacheDir = WP_CONTENT_DIR . '/wfcache/'; |
| 21 | if (file_exists($cacheDir . '.htaccess')) { |
| 22 | unlink($cacheDir . '.htaccess'); |
| 23 | } |
| 24 | |
| 25 | self::clearPageCacheSafe(); |
| 26 | } |
| 27 | public static function clearPageCacheSafe(){ |
| 28 | if(self::$cacheClearedThisRequest){ return; } |
| 29 | self::$cacheClearedThisRequest = true; |
| 30 | self::clearPageCache(); |
| 31 | } |
| 32 | public static function clearPageCache(){ //If a clear is in progress this does nothing. |
| 33 | self::$cacheStats = array( |
| 34 | 'dirsDeleted' => 0, |
| 35 | 'filesDeleted' => 0, |
| 36 | 'totalData' => 0, |
| 37 | 'totalErrors' => 0, |
| 38 | 'error' => '', |
| 39 | ); |
| 40 | |
| 41 | $cacheDir = WP_CONTENT_DIR . '/wfcache/'; |
| 42 | if (!file_exists($cacheDir)) { |
| 43 | return self::$cacheStats; |
| 44 | } |
| 45 | |
| 46 | $cacheClearLock = WP_CONTENT_DIR . '/wfcache/clear.lock'; |
| 47 | if(! is_file($cacheClearLock)){ |
| 48 | if(! touch($cacheClearLock)){ |
| 49 | self::$cacheStats['error'] = "Could not create a lock file $cacheClearLock to clear the cache."; |
| 50 | self::$cacheStats['totalErrors']++; |
| 51 | return self::$cacheStats; |
| 52 | } |
| 53 | } |
| 54 | $fp = fopen($cacheClearLock, 'w'); |
| 55 | if(! $fp){ |
| 56 | self::$cacheStats['error'] = "Could not open the lock file $cacheClearLock to clear the cache. Please make sure the directory is writable by your web server."; |
| 57 | self::$cacheStats['totalErrors']++; |
| 58 | return self::$cacheStats; |
| 59 | } |
| 60 | if(flock($fp, LOCK_EX | LOCK_NB)){ //non blocking exclusive flock attempt. If we get a lock then it continues and returns true. If we don't lock, then return false, don't block and don't clear the cache. |
| 61 | // This logic means that if a cache clear is currently in progress we don't try to clear the cache. |
| 62 | // This prevents web server children from being queued up waiting to be able to also clear the cache. |
| 63 | self::$lastRecursiveDeleteError = false; |
| 64 | self::recursiveDelete(WP_CONTENT_DIR . '/wfcache/'); |
| 65 | if(self::$lastRecursiveDeleteError){ |
| 66 | self::$cacheStats['error'] = self::$lastRecursiveDeleteError; |
| 67 | self::$cacheStats['totalErrors']++; |
| 68 | } |
| 69 | flock($fp, LOCK_UN); |
| 70 | @unlink($cacheClearLock); |
| 71 | @rmdir($cacheDir); |
| 72 | } |
| 73 | fclose($fp); |
| 74 | |
| 75 | return self::$cacheStats; |
| 76 | } |
| 77 | private static function recursiveDelete($dir) { |
| 78 | $files = array_diff(scandir($dir), array('.','..')); |
| 79 | foreach ($files as $file) { |
| 80 | if(is_dir($dir . '/' . $file)){ |
| 81 | if(! self::recursiveDelete($dir . '/' . $file)){ |
| 82 | return false; |
| 83 | } |
| 84 | } else { |
| 85 | if($file == 'clear.lock'){ continue; } //Don't delete our lock file |
| 86 | $size = filesize($dir . '/' . $file); |
| 87 | if($size){ |
| 88 | self::$cacheStats['totalData'] += round($size / 1024); |
| 89 | } |
| 90 | if(strpos($dir, 'wfcache/') === false){ |
| 91 | self::$lastRecursiveDeleteError = "Not deleting file in directory $dir because it appears to be in the wrong path."; |
| 92 | self::$cacheStats['totalErrors']++; |
| 93 | return false; //Safety check that we're in a subdir of the cache |
| 94 | } |
| 95 | if(@unlink($dir . '/' . $file)){ |
| 96 | self::$cacheStats['filesDeleted']++; |
| 97 | } else { |
| 98 | self::$lastRecursiveDeleteError = "Could not delete file " . $dir . "/" . $file . " : " . wfUtils::getLastError(); |
| 99 | self::$cacheStats['totalErrors']++; |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if($dir != WP_CONTENT_DIR . '/wfcache/'){ |
| 105 | if(strpos($dir, 'wfcache/') === false){ |
| 106 | self::$lastRecursiveDeleteError = "Not deleting directory $dir because it appears to be in the wrong path."; |
| 107 | self::$cacheStats['totalErrors']++; |
| 108 | return false; //Safety check that we're in a subdir of the cache |
| 109 | } |
| 110 | if(@rmdir($dir)){ |
| 111 | self::$cacheStats['dirsDeleted']++; |
| 112 | } else { |
| 113 | self::$lastRecursiveDeleteError = "Could not delete directory $dir : " . wfUtils::getLastError(); |
| 114 | self::$cacheStats['totalErrors']++; |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } else { |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | public static function addHtaccessCode($action){ |
| 123 | if($action != 'remove'){ |
| 124 | die("Error: addHtaccessCode must be called with 'remove' as param"); |
| 125 | } |
| 126 | $htaccessPath = self::getHtaccessPath(); |
| 127 | if(! $htaccessPath){ |
| 128 | return "Wordfence could not find your .htaccess file."; |
| 129 | } |
| 130 | $fh = @fopen($htaccessPath, 'r+'); |
| 131 | if(! $fh){ |
| 132 | $err = error_get_last(); |
| 133 | return $err['message']; |
| 134 | } |
| 135 | flock($fh, LOCK_EX); |
| 136 | fseek($fh, 0, SEEK_SET); //start of file |
| 137 | clearstatcache(); |
| 138 | $contents = fread($fh, filesize($htaccessPath)); |
| 139 | if(! $contents){ |
| 140 | fclose($fh); |
| 141 | return "Could not read from $htaccessPath"; |
| 142 | } |
| 143 | $contents = preg_replace('/#WFCACHECODE.*WFCACHECODE[\r\s\n\t]*/s', '', $contents); |
| 144 | ftruncate($fh, 0); |
| 145 | fflush($fh); |
| 146 | fseek($fh, 0, SEEK_SET); |
| 147 | fwrite($fh, $contents); |
| 148 | flock($fh, LOCK_UN); |
| 149 | fclose($fh); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * @param $action |
| 155 | * @return bool|string|void |
| 156 | */ |
| 157 | public static function updateBlockedIPs($action){ //'add' or 'remove' |
| 158 | $htaccessPath = self::getHtaccessPath(); |
| 159 | if(! $htaccessPath){ |
| 160 | return "Wordfence could not find your .htaccess file."; |
| 161 | } |
| 162 | if($action == 'remove'){ |
| 163 | $fh = @fopen($htaccessPath, 'r+'); |
| 164 | if(! $fh){ |
| 165 | $err = error_get_last(); |
| 166 | return $err['message']; |
| 167 | } |
| 168 | flock($fh, LOCK_EX); |
| 169 | fseek($fh, 0, SEEK_SET); //start of file |
| 170 | clearstatcache(); |
| 171 | $contents = @fread($fh, filesize($htaccessPath)); |
| 172 | if(! $contents){ |
| 173 | fclose($fh); |
| 174 | return "Could not read from $htaccessPath"; |
| 175 | } |
| 176 | |
| 177 | $contents = preg_replace('/#WFIPBLOCKS.*WFIPBLOCKS[\r\s\n\t]*/s', '', $contents); |
| 178 | |
| 179 | ftruncate($fh, 0); |
| 180 | fflush($fh); |
| 181 | fseek($fh, 0, SEEK_SET); |
| 182 | @fwrite($fh, $contents); |
| 183 | flock($fh, LOCK_UN); |
| 184 | fclose($fh); |
| 185 | return false; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | public static function getHtaccessPath(){ |
| 190 | if (!function_exists('get_home_path')) { |
| 191 | include_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 192 | } |
| 193 | |
| 194 | $homePath = get_home_path(); |
| 195 | $htaccessFile = $homePath.'.htaccess'; |
| 196 | return $htaccessFile; |
| 197 | } |
| 198 | public static function doNotCache(){ |
| 199 | if(! defined('WFDONOTCACHE')){ |
| 200 | define('WFDONOTCACHE', true); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 |