PluginProbe
The WP Remote WordPress Plugin / 4.86
The WP Remote WordPress Plugin v4.86
6.72 6.69 6.65 6.62 6.48 6.47 4.87 4.97 5.05 5.09 5.16 5.22 5.24 5.25 5.38 5.41 5.42 5.45 5.47 5.53 5.56 5.65 5.68 5.72 5.73 All 53 releases
wpremote / protect / wp / protect.php

protect.php in The WP Remote WordPress Plugin 4.86, at protect/wp/protect.php

168 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = BVFW::getInstance($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('MCWAFLOADED') && $fw->canSetIPCookie()) {
55 $fw->setIPCookie();
56 }
57
58 define('BVWPLOADED', true);
59
60 if (!defined('MCWAFLOADED')) {
61 register_shutdown_function(array($fw, 'log'));
62
63 $fw->execute();
64 } elseif ($fw->isGeoBlocking()) {
65 $fw->blockIfBlacklisted();
66 }
67
68 $fw->executeRules();
69 }
70
71 $lpConfHash = array_key_exists('lp', $config) ? $config['lp'] : array();
72 $lp = new BVWPLP($this->db, $this->settings, $ip, $bvipstore, $lpConfHash);
73 if ($lp->isActive()) {
74 $lp->init();
75 }
76 }
77
78 public function uninstall() {
79 $this->settings->deleteOption('bvptconf');
80 $this->db->dropBVTable(BVFWConfig::$requests_table);
81 $this->db->dropBVTable(BVWPLPConfig::$requests_table);
82 $this->settings->deleteOption('bvptplug');
83 $this->remove_wp_prepend();
84 $this->remove_php_prepend();
85 $this->remove_mcdata();
86 return true;
87 }
88
89 private function remove_wp_prepend() {
90 $wp_conf_paths = array(ABSPATH . "wp-config.php", ABSPATH . "../wp-config.php");
91 if (file_exists($wp_conf_paths[0])) {
92 $fname = $wp_conf_paths[0];
93 } elseif (file_exists($wp_conf_paths[1])) {
94 $fname = $wp_conf_paths[1];
95 } else {
96 return;
97 }
98
99 $content = file_get_contents($fname);
100 if ($content) {
101 $pattern = "@include '" . ABSPATH . "malcare-waf.php" . "';";
102 $modified_content = str_replace($pattern, "", $content);
103 if ($content !== $modified_content) {
104 file_put_contents($fname, $modified_content);
105 }
106 }
107 }
108
109 private function remove_php_prepend() {
110 $this->remove_htaccess_prepend();
111 $this->remove_userini_prepend();
112 }
113
114 private function remove_prepend($fname, $pattern) {
115 if (!file_exists($fname)) return;
116
117 $content = file_get_contents($fname);
118 if ($content) {
119 $modified_content = preg_replace($pattern, "", $content);
120 if ($content !== $modified_content) {
121 file_put_contents($fname, $modified_content);
122 }
123 }
124 }
125
126 private function remove_htaccess_prepend() {
127 $pattern = "/# MalCare WAF(.|\n)*# END MalCare WAF/i";
128 $this->remove_prepend(ABSPATH . ".htaccess", $pattern);
129 }
130
131 private function remove_userini_prepend() {
132 $pattern = "/; MalCare WAF(.|\n)*; END MalCare WAF/i";
133 $this->remove_prepend(ABSPATH . ".user.ini", $pattern);
134 }
135
136 private function remove_mcdata() {
137 $this->rrmdir($this->get_contdir() . "mc_data");
138 }
139
140 private function rrmdir($dir) {
141 if (is_dir($dir)) {
142 $objects = scandir($dir);
143 foreach ($objects as $object) {
144 if ($object != "." && $object != "..") {
145 if (is_dir($dir . "/" . $object) && !is_link($dir . "/" . $object)) {
146 rrmdir($dir . "/" . $object);
147 } else {
148 unlink($dir . "/" . $object);
149 }
150 }
151 }
152 rmdir($dir);
153 }
154 }
155
156 public function get_contdir() {
157 return defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR . "/" : ABSPATH . "wp-content/";
158 }
159
160 public function getRuleSet() {
161 $ruleSet = $this->settings->getOption('bvruleset');
162 if ($ruleSet) {
163 return $ruleSet;
164 }
165 return array();
166 }
167 }
168 endif;