| 1 |
<?php |
| 2 |
if (!defined('ABSPATH') && !defined('MCDATAPATH')) exit; |
| 3 |
|
| 4 |
if (!class_exists('WPRProtect_V553')) : |
| 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_V553 { |
| 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 ($mode == WPRProtect_V553::MODE_PREPEND) { |
| 26 |
$config_file = MCDATAPATH . MCCONFKEY . '-' . 'mc.conf'; |
| 27 |
$config = WPRProtectUtils_V553::parseFile($config_file); |
| 28 |
|
| 29 |
if (empty($config['time']) || !($config['time'] > time() - (48*3600)) || |
| 30 |
!isset($config['mc_conf_version']) || |
| 31 |
(WPRProtect_V553::CONF_VERSION !== $config['mc_conf_version'])) { |
| 32 |
return false; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
$brand_name = array_key_exists('brandname', $config) ? $config['brandname'] : 'Protect'; |
| 37 |
$request_ip_header = array_key_exists('ipheader', $config) ? $config['ipheader'] : null; |
| 38 |
$req_config = array_key_exists('reqconfig', $config) ? $config['reqconfig'] : array(); |
| 39 |
$request = new WPRProtectRequest_V553($request_ip_header, $req_config); |
| 40 |
$fw_config = array_key_exists('fw', $config) ? $config['fw'] : array(); |
| 41 |
|
| 42 |
WPRProtectFW_V553::getInstance($mode, $request, $fw_config, $brand_name)->init(); |
| 43 |
} else { |
| 44 |
//For backward compatibility. |
| 45 |
self::$settings = new WPRWPSettings(); |
| 46 |
self::$db = new WPRWPDb(); |
| 47 |
self::$info = new WPRInfo(self::$settings); |
| 48 |
|
| 49 |
$plug_config = self::$settings->getOption(self::$info->services_option_name); |
| 50 |
$config = array_key_exists('protect', $plug_config) ? $plug_config['protect'] : array(); |
| 51 |
if (!is_array($config) || !array_key_exists('mc_conf_version', $config) || |
| 52 |
(WPRProtect_V553::CONF_VERSION !== $config['mc_conf_version'])) { |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
$brand_name = self::$info->getBrandName(); |
| 58 |
$request_ip_header = array_key_exists('ipheader', $config) ? $config['ipheader'] : null; |
| 59 |
$req_config = array_key_exists('reqconfig', $config) ? $config['reqconfig'] : array(); |
| 60 |
$request = new WPRProtectRequest_V553($request_ip_header, $req_config); |
| 61 |
$fw_config = array_key_exists('fw', $config) ? $config['fw'] : array(); |
| 62 |
$lp_config = array_key_exists('lp', $config) ? $config['lp'] : array(); |
| 63 |
|
| 64 |
WPRProtectFW_V553::getInstance($mode, $request, $fw_config, $brand_name)->init(); |
| 65 |
WPRProtectLP_V553::getInstance($request, $lp_config, $brand_name)->init(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public static function uninstall() { |
| 70 |
self::$settings->deleteOption('bvptconf'); |
| 71 |
self::$settings->deleteOption('bvptplug'); |
| 72 |
WPRProtectIpstore_V553::uninstall(); |
| 73 |
WPRProtectFW_V553::uninstall(); |
| 74 |
WPRProtectLP_V553::uninstall(); |
| 75 |
|
| 76 |
WPRProtect_V553::removeWPPrepend(); |
| 77 |
WPRProtect_V553::removePHPPrepend(); |
| 78 |
WPRProtect_V553::removeMCData(); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
private static function removeWPPrepend() { |
| 84 |
$wp_conf_paths = array( |
| 85 |
dirname(ABSPATH) . "/wp-config.php", |
| 86 |
dirname(ABSPATH) . "/../wp-config.php" |
| 87 |
); |
| 88 |
|
| 89 |
if (file_exists($wp_conf_paths[0])) { |
| 90 |
$fname = $wp_conf_paths[0]; |
| 91 |
} elseif (file_exists($wp_conf_paths[1])) { |
| 92 |
$fname = $wp_conf_paths[1]; |
| 93 |
} else { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$pattern = "@include '" . dirname(ABSPATH) . "/malcare-waf.php" . "';"; |
| 98 |
|
| 99 |
WPRProtectUtils_V553::fileRemovePattern($fname, $pattern); |
| 100 |
} |
| 101 |
|
| 102 |
private static function removePHPPrepend() { |
| 103 |
WPRProtect_V553::removeHtaccessPrepend(); |
| 104 |
WPRProtect_V553::removeUseriniPrepend(); |
| 105 |
} |
| 106 |
|
| 107 |
private static function removeHtaccessPrepend() { |
| 108 |
$pattern = "/# MalCare WAF(.|\n)*# END MalCare WAF/i"; |
| 109 |
|
| 110 |
WPRProtectUtils_V553::fileRemovePattern(dirname(ABSPATH) . "/.htaccess", $pattern, true); |
| 111 |
} |
| 112 |
|
| 113 |
private static function removeUseriniPrepend() { |
| 114 |
$pattern = "/; MalCare WAF(.|\n)*; END MalCare WAF/i"; |
| 115 |
|
| 116 |
WPRProtectUtils_V553::fileRemovePattern(dirname(ABSPATH) . "/.user.ini", $pattern, true); |
| 117 |
} |
| 118 |
|
| 119 |
private static function removeMCData() { |
| 120 |
$content_dir = defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : dirname(ABSPATH) . "/wp-content"; |
| 121 |
$mc_data_dir = $content_dir . "/mc_data"; |
| 122 |
|
| 123 |
WPRProtectUtils_V553::rrmdir($mc_data_dir); |
| 124 |
} |
| 125 |
} |
| 126 |
endif; |