| 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 |
$request_params['bvctag'] = MCRecover::connectionTag($this->settings); |
| 26 |
$url = $this->bvinfo->appUrl()."/api/v3/accounts/".$params['account_id']."/sites"; |
| 27 |
foreach (preg_grep('#site_id|email|password|wp_cli_command|is_staging_env#i', array_keys($params)) as $key ) { |
| 28 |
$request_params[$key] = $params[$key]; |
| 29 |
} |
| 30 |
$headers = array( |
| 31 |
'Authorization' => "BVAPI-HMAC {$params['account_public']}:{$params['sig']}:{$params['timestamp']}" |
| 32 |
); |
| 33 |
|
| 34 |
$resp = $this->request($url, $request_params, $headers); |
| 35 |
|
| 36 |
$this->updateAccountInfo($resp); |
| 37 |
|
| 38 |
$this->handle_response($resp); |
| 39 |
} |
| 40 |
|
| 41 |
public function setkeys($args, $params) { |
| 42 |
if (!isset($params['public']) || !isset($params['secret'])) { |
| 43 |
WP_CLI::error('Please enter valid public and secret keys.'); |
| 44 |
} |
| 45 |
$secret = $params['secret']; |
| 46 |
$pubkey = $params['public']; |
| 47 |
if (strlen($pubkey) < 32 || strlen($secret) < 32) { |
| 48 |
WP_CLI::error('Public key and secret key should be 32 characters long.'); |
| 49 |
} |
| 50 |
MCAccount::addAccount($this->settings, $pubkey, $secret); |
| 51 |
MCAccount::updateApiPublicKey($this->settings, $pubkey); |
| 52 |
if (MCAccount::exists($this->settings, $pubkey)) { |
| 53 |
WP_CLI::success('Keys Setup Successfully.'); |
| 54 |
} else { |
| 55 |
WP_CLI::error('Keys Setup Failed.'); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function disable_fw($args, $params) { |
| 60 |
$account = MCAccount::apiPublicAccount($this->settings); |
| 61 |
if (!$account) { |
| 62 |
WP_CLI::error('Account not found'); |
| 63 |
} |
| 64 |
$resp = $this->request($account->authenticatedUrl('/bvapi/disable_fw')); |
| 65 |
$this->handle_response($resp); |
| 66 |
} |
| 67 |
|
| 68 |
public function enable_fw($args, $params) { |
| 69 |
$account = MCAccount::apiPublicAccount($this->settings); |
| 70 |
if (!$account) { |
| 71 |
WP_CLI::error('Account not found.'); |
| 72 |
} |
| 73 |
$resp = $this->request($account->authenticatedUrl('/bvapi/enable_fw')); |
| 74 |
$this->handle_response($resp); |
| 75 |
} |
| 76 |
|
| 77 |
public function disconnect($args, $params) { |
| 78 |
$status = false; |
| 79 |
if (isset($params['public_key'])) { |
| 80 |
$status = MCAccount::remove($this->settings, $params['public_key']); |
| 81 |
} else if(isset($params['account_type'])) { |
| 82 |
$status = MCAccount::removeByAccountType($this->settings, $params['account_type']); |
| 83 |
} else if(isset($params['account_gid'])) { |
| 84 |
$status = MCAccount::removeByAccountGid($this->settings, $params['account_gid']); |
| 85 |
} else { |
| 86 |
WP_CLI::error('Input Params are incorrect. Please validate the params.'); |
| 87 |
} |
| 88 |
|
| 89 |
if ($status) { |
| 90 |
WP_CLI::success('Account removed successfully.'); |
| 91 |
} else { |
| 92 |
WP_CLI::error('No Account exists.'); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private function request($url, $request_params = array(), $headers = array()) { |
| 97 |
$resp = $this->bvapi->http_request($url, $request_params, $headers); |
| 98 |
return $resp; |
| 99 |
} |
| 100 |
|
| 101 |
private function updateAccountInfo($resp) { |
| 102 |
if(isset($resp["response"]) && isset($resp["response"]["code"]) && ($resp["response"]["code"] == 200)) { |
| 103 |
if (isset($resp["body"])) { |
| 104 |
$body = json_decode($resp["body"], true); |
| 105 |
if (isset($body["account_info"])) { |
| 106 |
$info = $body["account_info"]; |
| 107 |
MCAccount::addAccount($this->settings, $info['pubkey'], $info['secret']); |
| 108 |
$account = MCAccount::find($this->settings, $info['pubkey']); |
| 109 |
$account->updateInfo($info); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
private function handle_response($resp) { |
| 116 |
if (empty($resp)) { |
| 117 |
WP_CLI::error("Error in connecting to MalCare Server. Please retry after some time."); |
| 118 |
} else if (is_wp_error($resp)) { |
| 119 |
$error_message = ""; |
| 120 |
if (isset($resp->errors["http_request_failed"][0])) { |
| 121 |
$error_message = $resp->errors["http_request_failed"][0]; |
| 122 |
} else { |
| 123 |
$error_message = "WPError request params empty"; |
| 124 |
} |
| 125 |
WP_CLI::error("{$error_message} . Please retry after sometime or contact us."); |
| 126 |
} else { |
| 127 |
if (isset($resp["response"])) { |
| 128 |
if (isset($resp["response"]["code"])) { |
| 129 |
$resp_code = $resp["response"]["code"]; |
| 130 |
if ($resp_code == 200) { |
| 131 |
if (isset($resp["body"])) { |
| 132 |
$body = json_decode($resp["body"], true); |
| 133 |
if (isset($body["error"])) { |
| 134 |
WP_CLI::error("code: {$resp_code} -- message: {$body["error"]} . Please retry or contact us"); |
| 135 |
} else if (isset($body["message"])) { |
| 136 |
WP_CLI::success("code: {$resp_code} -- message: {$body["message"]}"); |
| 137 |
} else { |
| 138 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 139 |
} |
| 140 |
} else { |
| 141 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 142 |
} |
| 143 |
} else if (MCHelper::safePregMatch("/^4[0-9][0-9]$/", strval($resp_code))) { |
| 144 |
if (isset($resp["body"])) { |
| 145 |
WP_CLI::error("code: {$resp_code} -- message: {$resp["body"]}"); |
| 146 |
} else { |
| 147 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 148 |
} |
| 149 |
} else { |
| 150 |
if (isset($resp["response"]["message"])) { |
| 151 |
WP_CLI::error("code: {$resp_code} -- message: {$resp["response"]["message"]} . Please retry or contact us"); |
| 152 |
} else { |
| 153 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 154 |
} |
| 155 |
} |
| 156 |
} else { |
| 157 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 158 |
} |
| 159 |
} else { |
| 160 |
WP_CLI::error("Invalid Response. Please retry or contact us."); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
public function setkey($args, $params) { |
| 165 |
// Support for encoded key |
| 166 |
if (isset($params['key'])) { |
| 167 |
$decoded = base64_decode($params['key']); |
| 168 |
$parts = explode(':', $decoded, 3); |
| 169 |
if (count($parts) === 3) { |
| 170 |
if ($parts[0] !== 'v1') { |
| 171 |
WP_CLI::error('Key version incompatible or invalid key format.'); |
| 172 |
} |
| 173 |
if ($parts[1] !== '' && $parts[2] !== '') { |
| 174 |
$pubkey = $parts[1]; |
| 175 |
$secret = $parts[2]; |
| 176 |
|
| 177 |
if (strlen($pubkey) < 32 || strlen($secret) < 32) { |
| 178 |
WP_CLI::error('Please enter valid key.'); |
| 179 |
} |
| 180 |
MCAccount::addAccount($this->settings, $pubkey, $secret); |
| 181 |
MCAccount::updateApiPublicKey($this->settings, $pubkey); |
| 182 |
if (MCAccount::exists($this->settings, $pubkey)) { |
| 183 |
WP_CLI::success('Key Setup Successfully.'); |
| 184 |
} else { |
| 185 |
WP_CLI::error('Key Setup Failed.'); |
| 186 |
} |
| 187 |
} else { |
| 188 |
WP_CLI::error('Invalid key format.'); |
| 189 |
} |
| 190 |
} else { |
| 191 |
WP_CLI::error('Invalid key format.'); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public function removekey($args, $params) { |
| 197 |
// Support for encoded key (same format as setkey: base64(v1:pubkey:secret)) |
| 198 |
if (isset($params['key'])) { |
| 199 |
$decoded = base64_decode($params['key']); |
| 200 |
$parts = explode(':', $decoded, 3); |
| 201 |
if (count($parts) === 3) { |
| 202 |
if ($parts[0] !== 'v1') { |
| 203 |
WP_CLI::error('Key version incompatible or invalid key format.'); |
| 204 |
} |
| 205 |
if ($parts[1] !== '') { |
| 206 |
$pubkey = $parts[1]; |
| 207 |
if (strlen($pubkey) < 32) { |
| 208 |
WP_CLI::error('Please enter valid key.'); |
| 209 |
} |
| 210 |
|
| 211 |
MCAccount::remove($this->settings, $pubkey); |
| 212 |
|
| 213 |
if (!MCAccount::exists($this->settings, $pubkey)) { |
| 214 |
WP_CLI::success('Key Removed Successfully.'); |
| 215 |
} else { |
| 216 |
WP_CLI::error('Key Removal Failed.'); |
| 217 |
} |
| 218 |
} else { |
| 219 |
WP_CLI::error('Invalid key format.'); |
| 220 |
} |
| 221 |
} else { |
| 222 |
WP_CLI::error('Invalid key format.'); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
endif; |
| 228 |
|