PluginProbe
MalCare WordPress Security Plugin – Malware Scanner, Cleaner, Security Firewall / 5.77
MalCare WordPress Security Plugin – Malware Scanner, Cleaner, Security Firewall v5.77
6.72 6.69 6.65 6.62 6.48 6.47 5.41 5.42 5.45 5.47 5.53 5.54 5.55 5.56 5.65 5.67 5.68 5.72 5.73 5.77 5.81 5.85 5.88 5.91 5.92 All 88 releases
malcare-security / wp_cli.php

wp_cli.php in MalCare WordPress Security Plugin – Malware Scanner, Cleaner, Security Firewall 5.77, at wp_cli.php

164 lines 5.6 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('MCWPCli')) :
4
5 require_once dirname( __FILE__ ) . '/recover.php';
6
7 class MCWPCli {
8 public $settings;
9 public $siteinfo;
10 public $bvinfo;
11 public $bvapi;
12
13 public function __construct($settings, $bvinfo, $bvsiteinfo, $bvapi) {
14 $this->settings = $settings;
15 $this->siteinfo = $bvsiteinfo;
16 $this->bvinfo = $bvinfo;
17 $this->bvapi = $bvapi;
18 }
19
20 public function create($args, $params) {
21 $this->settings->updateOption($this->bvinfo->plug_redirect, 'no');
22 $request_params = array_merge($this->siteinfo->info(), $this->bvinfo->info());
23 $request_params['bvpublic'] = MCAccount::getApiPublicKey($this->settings);
24 $request_params['bvsecret'] = MCRecover::defaultSecret($this->settings);
25 $url = $this->bvinfo->appUrl()."/api/v3/accounts/".$params['account_id']."/sites";
26 foreach (preg_grep('#site_id|email|password|wp_cli_command|is_staging_env#i', array_keys($params)) as $key ) {
27 $request_params[$key] = $params[$key];
28 }
29 $headers = array(
30 'Authorization' => "BVAPI-HMAC {$params['account_public']}:{$params['sig']}:{$params['timestamp']}"
31 );
32
33 $resp = $this->request($url, $request_params, $headers);
34
35 $this->updateAccountInfo($resp);
36
37 $this->handle_response($resp);
38 }
39
40 public function setkeys($args, $params) {
41 if (!isset($params['public']) || !isset($params['secret'])) {
42 WP_CLI::error('Please enter valid public and secret keys.');
43 }
44 $secret = $params['secret'];
45 $pubkey = $params['public'];
46 if (strlen($pubkey) < 32 || strlen($secret) < 32) {
47 WP_CLI::error('Public key and secret key should be 32 characters long.');
48 }
49 MCAccount::addAccount($this->settings, $pubkey, $secret);
50 MCAccount::updateApiPublicKey($this->settings, $pubkey);
51 if (MCAccount::exists($this->settings, $pubkey)) {
52 WP_CLI::success('Keys Setup Successfully.');
53 } else {
54 WP_CLI::error('Keys Setup Failed.');
55 }
56 }
57
58 public function disable_fw($args, $params) {
59 $account = MCAccount::apiPublicAccount($this->settings);
60 if (!$account) {
61 WP_CLI::error('Account not found');
62 }
63 $resp = $this->request($account->authenticatedUrl('/bvapi/disable_fw'));
64 $this->handle_response($resp);
65 }
66
67 public function enable_fw($args, $params) {
68 $account = MCAccount::apiPublicAccount($this->settings);
69 if (!$account) {
70 WP_CLI::error('Account not found.');
71 }
72 $resp = $this->request($account->authenticatedUrl('/bvapi/enable_fw'));
73 $this->handle_response($resp);
74 }
75
76 public function disconnect($args, $params) {
77 $status = false;
78 if (isset($params['public_key'])) {
79 $status = MCAccount::remove($this->settings, $params['public_key']);
80 } else if(isset($params['account_type'])) {
81 $status = MCAccount::removeByAccountType($this->settings, $params['account_type']);
82 } else if(isset($params['account_gid'])) {
83 $status = MCAccount::removeByAccountGid($this->settings, $params['account_gid']);
84 } else {
85 WP_CLI::error('Input Params are incorrect. Please validate the params.');
86 }
87
88 if ($status) {
89 WP_CLI::success('Account removed successfully.');
90 } else {
91 WP_CLI::error('No Account exists.');
92 }
93 }
94
95 private function request($url, $request_params = array(), $headers = array()) {
96 $resp = $this->bvapi->http_request($url, $request_params, $headers);
97 return $resp;
98 }
99
100 private function updateAccountInfo($resp) {
101 if(isset($resp["response"]) && isset($resp["response"]["code"]) && ($resp["response"]["code"] == 200)) {
102 if (isset($resp["body"])) {
103 $body = json_decode($resp["body"], true);
104 if (isset($body["account_info"])) {
105 $info = $body["account_info"];
106 MCAccount::addAccount($this->settings, $info['pubkey'], $info['secret']);
107 $account = MCAccount::find($this->settings, $info['pubkey']);
108 $account->updateInfo($info);
109 }
110 }
111 }
112 }
113
114 private function handle_response($resp) {
115 if (empty($resp)) {
116 WP_CLI::error("Error in connecting to MalCare Server. Please retry after some time.");
117 } else if (is_wp_error($resp)) {
118 $error_message = "";
119 if (isset($resp->errors["http_request_failed"][0])) {
120 $error_message = $resp->errors["http_request_failed"][0];
121 } else {
122 $error_message = "WPError request params empty";
123 }
124 WP_CLI::error("{$error_message} . Please retry after sometime or contact us.");
125 } else {
126 if (isset($resp["response"])) {
127 if (isset($resp["response"]["code"])) {
128 $resp_code = $resp["response"]["code"];
129 if ($resp_code == 200) {
130 if (isset($resp["body"])) {
131 $body = json_decode($resp["body"], true);
132 if (isset($body["error"])) {
133 WP_CLI::error("code: {$resp_code} -- message: {$body["error"]} . Please retry or contact us");
134 } else if (isset($body["message"])) {
135 WP_CLI::success("code: {$resp_code} -- message: {$body["message"]}");
136 } else {
137 WP_CLI::error("Invalid Response. Please retry or contact us.");
138 }
139 } else {
140 WP_CLI::error("Invalid Response. Please retry or contact us.");
141 }
142 } else if (MCHelper::safePregMatch("/^4[0-9][0-9]$/", strval($resp_code))) {
143 if (isset($resp["body"])) {
144 WP_CLI::error("code: {$resp_code} -- message: {$resp["body"]}");
145 } else {
146 WP_CLI::error("Invalid Response. Please retry or contact us.");
147 }
148 } else {
149 if (isset($resp["response"]["message"])) {
150 WP_CLI::error("code: {$resp_code} -- message: {$resp["response"]["message"]} . Please retry or contact us");
151 } else {
152 WP_CLI::error("Invalid Response. Please retry or contact us.");
153 }
154 }
155 } else {
156 WP_CLI::error("Invalid Response. Please retry or contact us.");
157 }
158 } else {
159 WP_CLI::error("Invalid Response. Please retry or contact us.");
160 }
161 }
162 }
163 }
164 endif;