| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRProtect_V572')) : |
| 5 |
require_once dirname( __FILE__ ) . '/logger.php'; |
| 6 |
require_once dirname( __FILE__ ) . '/ipstore.php'; |
| 7 |
require_once dirname( __FILE__ ) . '/request.php'; |
| 8 |
require_once dirname( __FILE__ ) . '/wp_user.php'; |
| 9 |
require_once dirname( __FILE__ ) . '/lib.php'; |
| 10 |
require_once dirname( __FILE__ ) . '/fw.php'; |
| 11 |
require_once dirname( __FILE__ ) . '/lp.php'; |
| 12 |
require_once dirname( __FILE__ ) . '/../helper.php'; |
| 13 |
|
| 14 |
class WPRProtect_V572 { |
| 15 |
public static $settings; |
| 16 |
public static $db; |
| 17 |
public static $info; |
| 18 |
|
| 19 |
const MODE_PREPEND = 0; |
| 20 |
const MODE_WP = 1; |
| 21 |
|
| 22 |
const CONF_VERSION = '2'; |
| 23 |
|
| 24 |
public static function init($mode) { |
| 25 |
if (defined('WP_CLI') && WP_CLI) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
if ($mode == WPRProtect_V572::MODE_PREPEND) { |
| 30 |
$config_file = MCDATAPATH . MCCONFKEY . '-' . 'mc.conf'; |
| 31 |
$config = WPRProtectUtils_V572::parseFile($config_file); |
| 32 |
|
| 33 |
if (empty($config['time']) || !($config['time'] > time() - (48*3600)) || |
| 34 |
!isset($config['mc_conf_version']) || |
| 35 |
(WPRProtect_V572::CONF_VERSION !== $config['mc_conf_version'])) { |
| 36 |
return false; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
$brand_name = array_key_exists('brandname', $config) ? $config['brandname'] : 'Protect'; |
| 41 |
$request_ip_header = array_key_exists('ipheader', $config) ? $config['ipheader'] : null; |
| 42 |
$req_config = array_key_exists('reqconfig', $config) ? $config['reqconfig'] : array(); |
| 43 |
$request = new WPRProtectRequest_V572($request_ip_header, $req_config); |
| 44 |
$fw_config = array_key_exists('fw', $config) ? $config['fw'] : array(); |
| 45 |
|
| 46 |
WPRProtectFW_V572::getInstance($mode, $request, $fw_config, $brand_name)->init(); |
| 47 |
} else { |
| 48 |
$plug_config = self::$settings->getOption(self::$info->services_option_name); |
| 49 |
$config = array_key_exists('protect', $plug_config) ? $plug_config['protect'] : array(); |
| 50 |
if (!is_array($config) || !array_key_exists('mc_conf_version', $config) || |
| 51 |
(WPRProtect_V572::CONF_VERSION !== $config['mc_conf_version'])) { |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$brand_name = self::$info->getBrandName(); |
| 57 |
$request_ip_header = array_key_exists('ipheader', $config) ? $config['ipheader'] : null; |
| 58 |
$req_config = array_key_exists('reqconfig', $config) ? $config['reqconfig'] : array(); |
| 59 |
$request = new WPRProtectRequest_V572($request_ip_header, $req_config); |
| 60 |
$fw_config = array_key_exists('fw', $config) ? $config['fw'] : array(); |
| 61 |
$lp_config = array_key_exists('lp', $config) ? $config['lp'] : array(); |
| 62 |
|
| 63 |
WPRProtectFW_V572::getInstance($mode, $request, $fw_config, $brand_name)->init(); |
| 64 |
WPRProtectLP_V572::getInstance($request, $lp_config, $brand_name)->init(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public static function uninstall() { |
| 69 |
self::$settings->deleteOption('bvptconf'); |
| 70 |
self::$settings->deleteOption('bvptplug'); |
| 71 |
WPRProtectIpstore_V572::uninstall(); |
| 72 |
WPRProtectFW_V572::uninstall(); |
| 73 |
WPRProtectLP_V572::uninstall(); |
| 74 |
|
| 75 |
WPRProtect_V572::removeWPPrepend(); |
| 76 |
WPRProtect_V572::removePHPPrepend(); |
| 77 |
WPRProtect_V572::removeMCData(); |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
private static function removeWPPrepend() { |
| 83 |
$wp_conf_paths = array( |
| 84 |
rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/wp-config.php", |
| 85 |
rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/../wp-config.php" |
| 86 |
); |
| 87 |
|
| 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 |
$pattern = "@include '" . rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/malcare-waf.php" . "';"; |
| 97 |
|
| 98 |
WPRProtectUtils_V572::fileRemovePattern($fname, $pattern); |
| 99 |
} |
| 100 |
|
| 101 |
private static function removePHPPrepend() { |
| 102 |
WPRProtect_V572::removeHtaccessPrepend(); |
| 103 |
WPRProtect_V572::removeUseriniPrepend(); |
| 104 |
} |
| 105 |
|
| 106 |
private static function removeHtaccessPrepend() { |
| 107 |
$pattern = "/# MalCare WAF(.|\n)*# END MalCare WAF/i"; |
| 108 |
|
| 109 |
WPRProtectUtils_V572::fileRemovePattern(rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/.htaccess", $pattern, true); |
| 110 |
} |
| 111 |
|
| 112 |
private static function removeUseriniPrepend() { |
| 113 |
$pattern = "/; MalCare WAF(.|\n)*; END MalCare WAF/i"; |
| 114 |
|
| 115 |
WPRProtectUtils_V572::fileRemovePattern(rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/.user.ini", $pattern, true); |
| 116 |
} |
| 117 |
|
| 118 |
private static function removeMCData() { |
| 119 |
$content_dir = defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : rtrim(ABSPATH, DIRECTORY_SEPARATOR) . "/wp-content"; |
| 120 |
$mc_data_dir = $content_dir . "/mc_data"; |
| 121 |
|
| 122 |
WPRProtectUtils_V572::rrmdir($mc_data_dir); |
| 123 |
} |
| 124 |
} |
| 125 |
endif; |