Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordfenc.../lib
File: wfCache.php
<?php
[0] Fix | Delete
class wfCache {
[1] Fix | Delete
private static $cacheStats = array();
[2] Fix | Delete
private static $cacheClearedThisRequest = false;
[3] Fix | Delete
private static $lastRecursiveDeleteError = false;
[4] Fix | Delete
[5] Fix | Delete
public static function removeCaching() {
[6] Fix | Delete
$cacheType = wfConfig::get('cacheType', false);
[7] Fix | Delete
if ($cacheType === 'disabled') {
[8] Fix | Delete
return;
[9] Fix | Delete
}
[10] Fix | Delete
[11] Fix | Delete
if ($cacheType == 'falcon') {
[12] Fix | Delete
self::addHtaccessCode('remove');
[13] Fix | Delete
self::updateBlockedIPs('remove');
[14] Fix | Delete
}
[15] Fix | Delete
[16] Fix | Delete
wfConfig::set('cacheType', 'disabled');
[17] Fix | Delete
[18] Fix | Delete
$cacheDir = WP_CONTENT_DIR . '/wfcache/';
[19] Fix | Delete
if (file_exists($cacheDir . '.htaccess')) {
[20] Fix | Delete
unlink($cacheDir . '.htaccess');
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
self::clearPageCacheSafe();
[24] Fix | Delete
}
[25] Fix | Delete
public static function clearPageCacheSafe(){
[26] Fix | Delete
if(self::$cacheClearedThisRequest){ return; }
[27] Fix | Delete
self::$cacheClearedThisRequest = true;
[28] Fix | Delete
self::clearPageCache();
[29] Fix | Delete
}
[30] Fix | Delete
public static function clearPageCache(){ //If a clear is in progress this does nothing.
[31] Fix | Delete
self::$cacheStats = array(
[32] Fix | Delete
'dirsDeleted' => 0,
[33] Fix | Delete
'filesDeleted' => 0,
[34] Fix | Delete
'totalData' => 0,
[35] Fix | Delete
'totalErrors' => 0,
[36] Fix | Delete
'error' => '',
[37] Fix | Delete
);
[38] Fix | Delete
[39] Fix | Delete
$cacheDir = WP_CONTENT_DIR . '/wfcache/';
[40] Fix | Delete
if (!file_exists($cacheDir)) {
[41] Fix | Delete
return self::$cacheStats;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
$cacheClearLock = WP_CONTENT_DIR . '/wfcache/clear.lock';
[45] Fix | Delete
if(! is_file($cacheClearLock)){
[46] Fix | Delete
if(! touch($cacheClearLock)){
[47] Fix | Delete
self::$cacheStats['error'] = "Could not create a lock file $cacheClearLock to clear the cache.";
[48] Fix | Delete
self::$cacheStats['totalErrors']++;
[49] Fix | Delete
return self::$cacheStats;
[50] Fix | Delete
}
[51] Fix | Delete
}
[52] Fix | Delete
$fp = fopen($cacheClearLock, 'w');
[53] Fix | Delete
if(! $fp){
[54] Fix | Delete
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.";
[55] Fix | Delete
self::$cacheStats['totalErrors']++;
[56] Fix | Delete
return self::$cacheStats;
[57] Fix | Delete
}
[58] Fix | Delete
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.
[59] Fix | Delete
// This logic means that if a cache clear is currently in progress we don't try to clear the cache.
[60] Fix | Delete
// This prevents web server children from being queued up waiting to be able to also clear the cache.
[61] Fix | Delete
self::$lastRecursiveDeleteError = false;
[62] Fix | Delete
self::recursiveDelete(WP_CONTENT_DIR . '/wfcache/');
[63] Fix | Delete
if(self::$lastRecursiveDeleteError){
[64] Fix | Delete
self::$cacheStats['error'] = self::$lastRecursiveDeleteError;
[65] Fix | Delete
self::$cacheStats['totalErrors']++;
[66] Fix | Delete
}
[67] Fix | Delete
flock($fp, LOCK_UN);
[68] Fix | Delete
@unlink($cacheClearLock);
[69] Fix | Delete
@rmdir($cacheDir);
[70] Fix | Delete
}
[71] Fix | Delete
fclose($fp);
[72] Fix | Delete
[73] Fix | Delete
return self::$cacheStats;
[74] Fix | Delete
}
[75] Fix | Delete
private static function recursiveDelete($dir) {
[76] Fix | Delete
$files = array_diff(scandir($dir), array('.','..'));
[77] Fix | Delete
foreach ($files as $file) {
[78] Fix | Delete
if(is_dir($dir . '/' . $file)){
[79] Fix | Delete
if(! self::recursiveDelete($dir . '/' . $file)){
[80] Fix | Delete
return false;
[81] Fix | Delete
}
[82] Fix | Delete
} else {
[83] Fix | Delete
if($file == 'clear.lock'){ continue; } //Don't delete our lock file
[84] Fix | Delete
$size = filesize($dir . '/' . $file);
[85] Fix | Delete
if($size){
[86] Fix | Delete
self::$cacheStats['totalData'] += round($size / 1024);
[87] Fix | Delete
}
[88] Fix | Delete
if(strpos($dir, 'wfcache/') === false){
[89] Fix | Delete
self::$lastRecursiveDeleteError = "Not deleting file in directory $dir because it appears to be in the wrong path.";
[90] Fix | Delete
self::$cacheStats['totalErrors']++;
[91] Fix | Delete
return false; //Safety check that we're in a subdir of the cache
[92] Fix | Delete
}
[93] Fix | Delete
if(@unlink($dir . '/' . $file)){
[94] Fix | Delete
self::$cacheStats['filesDeleted']++;
[95] Fix | Delete
} else {
[96] Fix | Delete
self::$lastRecursiveDeleteError = "Could not delete file " . $dir . "/" . $file . " : " . wfUtils::getLastError();
[97] Fix | Delete
self::$cacheStats['totalErrors']++;
[98] Fix | Delete
return false;
[99] Fix | Delete
}
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
if($dir != WP_CONTENT_DIR . '/wfcache/'){
[103] Fix | Delete
if(strpos($dir, 'wfcache/') === false){
[104] Fix | Delete
self::$lastRecursiveDeleteError = "Not deleting directory $dir because it appears to be in the wrong path.";
[105] Fix | Delete
self::$cacheStats['totalErrors']++;
[106] Fix | Delete
return false; //Safety check that we're in a subdir of the cache
[107] Fix | Delete
}
[108] Fix | Delete
if(@rmdir($dir)){
[109] Fix | Delete
self::$cacheStats['dirsDeleted']++;
[110] Fix | Delete
} else {
[111] Fix | Delete
self::$lastRecursiveDeleteError = "Could not delete directory $dir : " . wfUtils::getLastError();
[112] Fix | Delete
self::$cacheStats['totalErrors']++;
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
return true;
[116] Fix | Delete
} else {
[117] Fix | Delete
return true;
[118] Fix | Delete
}
[119] Fix | Delete
}
[120] Fix | Delete
public static function addHtaccessCode($action){
[121] Fix | Delete
if($action != 'remove'){
[122] Fix | Delete
die("Error: addHtaccessCode must be called with 'remove' as param");
[123] Fix | Delete
}
[124] Fix | Delete
$htaccessPath = self::getHtaccessPath();
[125] Fix | Delete
if(! $htaccessPath){
[126] Fix | Delete
return "Wordfence could not find your .htaccess file.";
[127] Fix | Delete
}
[128] Fix | Delete
$fh = @fopen($htaccessPath, 'r+');
[129] Fix | Delete
if(! $fh){
[130] Fix | Delete
$err = error_get_last();
[131] Fix | Delete
return $err['message'];
[132] Fix | Delete
}
[133] Fix | Delete
flock($fh, LOCK_EX);
[134] Fix | Delete
fseek($fh, 0, SEEK_SET); //start of file
[135] Fix | Delete
clearstatcache();
[136] Fix | Delete
$contents = fread($fh, filesize($htaccessPath));
[137] Fix | Delete
if(! $contents){
[138] Fix | Delete
fclose($fh);
[139] Fix | Delete
return "Could not read from $htaccessPath";
[140] Fix | Delete
}
[141] Fix | Delete
$contents = preg_replace('/#WFCACHECODE.*WFCACHECODE[\r\s\n\t]*/s', '', $contents);
[142] Fix | Delete
ftruncate($fh, 0);
[143] Fix | Delete
fflush($fh);
[144] Fix | Delete
fseek($fh, 0, SEEK_SET);
[145] Fix | Delete
fwrite($fh, $contents);
[146] Fix | Delete
flock($fh, LOCK_UN);
[147] Fix | Delete
fclose($fh);
[148] Fix | Delete
return false;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* @param $action
[153] Fix | Delete
* @return bool|string|void
[154] Fix | Delete
*/
[155] Fix | Delete
public static function updateBlockedIPs($action){ //'add' or 'remove'
[156] Fix | Delete
$htaccessPath = self::getHtaccessPath();
[157] Fix | Delete
if(! $htaccessPath){
[158] Fix | Delete
return "Wordfence could not find your .htaccess file.";
[159] Fix | Delete
}
[160] Fix | Delete
if($action == 'remove'){
[161] Fix | Delete
$fh = @fopen($htaccessPath, 'r+');
[162] Fix | Delete
if(! $fh){
[163] Fix | Delete
$err = error_get_last();
[164] Fix | Delete
return $err['message'];
[165] Fix | Delete
}
[166] Fix | Delete
flock($fh, LOCK_EX);
[167] Fix | Delete
fseek($fh, 0, SEEK_SET); //start of file
[168] Fix | Delete
clearstatcache();
[169] Fix | Delete
$contents = @fread($fh, filesize($htaccessPath));
[170] Fix | Delete
if(! $contents){
[171] Fix | Delete
fclose($fh);
[172] Fix | Delete
return "Could not read from $htaccessPath";
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
$contents = preg_replace('/#WFIPBLOCKS.*WFIPBLOCKS[\r\s\n\t]*/s', '', $contents);
[176] Fix | Delete
[177] Fix | Delete
ftruncate($fh, 0);
[178] Fix | Delete
fflush($fh);
[179] Fix | Delete
fseek($fh, 0, SEEK_SET);
[180] Fix | Delete
@fwrite($fh, $contents);
[181] Fix | Delete
flock($fh, LOCK_UN);
[182] Fix | Delete
fclose($fh);
[183] Fix | Delete
return false;
[184] Fix | Delete
}
[185] Fix | Delete
return false;
[186] Fix | Delete
}
[187] Fix | Delete
public static function getHtaccessPath(){
[188] Fix | Delete
[189] Fix | Delete
$homePath = wfUtils::getHomePath();
[190] Fix | Delete
$htaccessFile = $homePath.'.htaccess';
[191] Fix | Delete
return $htaccessFile;
[192] Fix | Delete
}
[193] Fix | Delete
public static function doNotCache(){
[194] Fix | Delete
if(! defined('WFDONOTCACHE')){
[195] Fix | Delete
define('WFDONOTCACHE', true);
[196] Fix | Delete
}
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function