| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
if (!class_exists('BVProtect')) : |
| 4 |
|
| 5 |
require_once dirname( __FILE__ ) . '/../base.php'; |
| 6 |
require_once dirname( __FILE__ ) . '/logger.php'; |
| 7 |
require_once dirname( __FILE__ ) . '/ipstore.php'; |
| 8 |
require_once dirname( __FILE__ ) . '/../fw/fw.php'; |
| 9 |
require_once dirname( __FILE__ ) . '/../fw/config.php'; |
| 10 |
require_once dirname( __FILE__ ) . '/../fw/request.php'; |
| 11 |
require_once dirname( __FILE__ ) . '/lp/lp.php'; |
| 12 |
require_once dirname( __FILE__ ) . '/lp/config.php'; |
| 13 |
|
| 14 |
class BVProtect { |
| 15 |
public $db; |
| 16 |
public $settings; |
| 17 |
|
| 18 |
function __construct($db, $settings) { |
| 19 |
$this->settings = $settings; |
| 20 |
$this->db = $db; |
| 21 |
} |
| 22 |
|
| 23 |
public function init() { |
| 24 |
add_action('clear_pt_config', array($this, 'uninstall')); |
| 25 |
} |
| 26 |
|
| 27 |
public function run() { |
| 28 |
$bvipstore = new BVIPStore($this->db); |
| 29 |
$bvipstore->init(); |
| 30 |
$bvinfo = new WPRInfo($this->settings); |
| 31 |
|
| 32 |
$config = $this->settings->getOption($bvinfo->services_option_name); |
| 33 |
if (array_key_exists('protect', $config)) { |
| 34 |
$config = $config['protect']; |
| 35 |
} else { |
| 36 |
$config = array(); |
| 37 |
} |
| 38 |
|
| 39 |
$ipHeader = array_key_exists('ipheader', $config) ? $config['ipheader'] : false; |
| 40 |
$ip = BVProtectBase::getIP($ipHeader); |
| 41 |
|
| 42 |
$fwLogger = new BVLogger($this->db, BVFWConfig::$requests_table); |
| 43 |
|
| 44 |
$fwConfHash = array_key_exists('fw', $config) ? $config['fw'] : array(); |
| 45 |
$ruleSet = $this->getRuleSet(); |
| 46 |
$fw = new BVFW($fwLogger, $fwConfHash, $ip, $bvinfo, $bvipstore, $ruleSet); |
| 47 |
|
| 48 |
if ($fw->isActive()) { |
| 49 |
|
| 50 |
if ($fw->canSetAdminCookie()) { |
| 51 |
add_action('init', array($fw, 'setBypassCookie')); |
| 52 |
} |
| 53 |
|
| 54 |
if (!defined('MCFWLOADED') && $fw->canSetIPCookie()) { |
| 55 |
$fw->setIPCookie(); |
| 56 |
} |
| 57 |
|
| 58 |
if (!defined('MCFWLOADED')) { |
| 59 |
register_shutdown_function(array($fw, 'log')); |
| 60 |
|
| 61 |
$fw->execute(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
$lpConfHash = array_key_exists('lp', $config) ? $config['lp'] : array(); |
| 66 |
$lp = new BVWPLP($this->db, $this->settings, $ip, $bvipstore, $lpConfHash); |
| 67 |
if ($lp->isActive()) { |
| 68 |
$lp->init(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function uninstall() { |
| 73 |
$this->settings->deleteOption('bvptconf'); |
| 74 |
$this->db->dropBVTable(BVFWConfig::$requests_table); |
| 75 |
$this->db->dropBVTable(BVWPLPConfig::$requests_table); |
| 76 |
$this->settings->deleteOption('bvptplug'); |
| 77 |
$this->remove_wp_prepend(); |
| 78 |
$this->remove_php_prepend(); |
| 79 |
$this->remove_mcdata(); |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
private function remove_wp_prepend() { |
| 84 |
$wp_conf_paths = array(ABSPATH . "wp-config.php", ABSPATH . "../wp-config.php"); |
| 85 |
if (file_exists($wp_conf_paths[0])) { |
| 86 |
$fname = $wp_conf_paths[0]; |
| 87 |
} elseif (file_exists($wp_conf_paths[1])) { |
| 88 |
$fname = $wp_conf_paths[1]; |
| 89 |
} else { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$content = file_get_contents($fname); |
| 94 |
if ($content) { |
| 95 |
$pattern = "@include '" . ABSPATH . "malcare-waf.php" . "';"; |
| 96 |
$modified_content = str_replace($pattern, "", $content); |
| 97 |
if ($content !== $modified_content) { |
| 98 |
file_put_contents($fname, $modified_content); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private function remove_php_prepend() { |
| 104 |
$this->remove_htaccess_prepend(); |
| 105 |
$this->remove_userini_prepend(); |
| 106 |
} |
| 107 |
|
| 108 |
private function remove_prepend($fname, $pattern) { |
| 109 |
if (!file_exists($fname)) return; |
| 110 |
|
| 111 |
$content = file_get_contents($fname); |
| 112 |
if ($content) { |
| 113 |
$modified_content = preg_replace($pattern, "", $content); |
| 114 |
if ($content !== $modified_content) { |
| 115 |
file_put_contents($fname, $modified_content); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
private function remove_htaccess_prepend() { |
| 121 |
$pattern = "/# MalCare WAF(.|\n)*# END MalCare WAF/i"; |
| 122 |
$this->remove_prepend(ABSPATH . ".htaccess", $pattern); |
| 123 |
} |
| 124 |
|
| 125 |
private function remove_userini_prepend() { |
| 126 |
$pattern = "/; MalCare WAF(.|\n)*; END MalCare WAF/i"; |
| 127 |
$this->remove_prepend(ABSPATH . ".user.ini", $pattern); |
| 128 |
} |
| 129 |
|
| 130 |
private function remove_mcdata() { |
| 131 |
$this->rrmdir($this->get_contdir() . "mc_data"); |
| 132 |
} |
| 133 |
|
| 134 |
private function rrmdir($dir) { |
| 135 |
if (is_dir($dir)) { |
| 136 |
$objects = scandir($dir); |
| 137 |
foreach ($objects as $object) { |
| 138 |
if ($object != "." && $object != "..") { |
| 139 |
if (is_dir($dir . "/" . $object) && !is_link($dir . "/" . $object)) { |
| 140 |
rrmdir($dir . "/" . $object); |
| 141 |
} else { |
| 142 |
unlink($dir . "/" . $object); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
rmdir($dir); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public function get_contdir() { |
| 151 |
return defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR . "/" : ABSPATH . "wp-content/"; |
| 152 |
} |
| 153 |
|
| 154 |
public function getRuleSet() { |
| 155 |
$ruleSet = $this->settings->getOption('bvruleset'); |
| 156 |
if ($ruleSet) { |
| 157 |
return $ruleSet; |
| 158 |
} |
| 159 |
return array(); |
| 160 |
} |
| 161 |
} |
| 162 |
endif; |