PluginProbe
The WP Remote WordPress Plugin / 4.79
The WP Remote WordPress Plugin v4.79
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.79, at protect/wp/protect.php

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