| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP API Manager |
| 4 |
* |
| 5 |
* This class handles MainWP Extensions API licensing. |
| 6 |
* |
| 7 |
* @package MainWP/MainWP API Manager |
| 8 |
* @author Todd Lahman LLC |
| 9 |
* @copyright Copyright (c) Todd Lahman LLC |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace MainWP\Dashboard; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class MainWP_Api_Manager |
| 17 |
* |
| 18 |
* @package MainWP\Dashboard |
| 19 |
*/ |
| 20 |
class MainWP_Api_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Extensions upgrade URL. |
| 24 |
* |
| 25 |
* @var string Default URL 'https://mainwp.com/' |
| 26 |
*/ |
| 27 |
private $upgrade_url = 'https://mainwp.com/'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Extensions license renewal URL. |
| 31 |
* |
| 32 |
* @var string Default 'https://mainwp.com/my-account' |
| 33 |
*/ |
| 34 |
private $renew_license_url = 'https://mainwp.com/my-account'; |
| 35 |
|
| 36 |
/** |
| 37 |
* MainWP installation domain name. |
| 38 |
* |
| 39 |
* @var string $domain Emtpy by default. |
| 40 |
*/ |
| 41 |
public $domain = ''; |
| 42 |
|
| 43 |
/** |
| 44 |
* Protected static variable to hold the single instance of the class. |
| 45 |
* |
| 46 |
* @var mixed Default null |
| 47 |
*/ |
| 48 |
protected static $instance = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Return the single instance of the class. |
| 52 |
* |
| 53 |
* @return mixed $instance The single instance of the class. |
| 54 |
*/ |
| 55 |
public static function instance() { |
| 56 |
|
| 57 |
if ( is_null( self::$instance ) ) { |
| 58 |
self::$instance = new self(); |
| 59 |
} |
| 60 |
|
| 61 |
return self::$instance; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Cloning is forbidden. |
| 66 |
* |
| 67 |
* @since 1.2 |
| 68 |
*/ |
| 69 |
private function __clone() { |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Unserializing instances of this class is forbidden. |
| 74 |
* |
| 75 |
* @since 1.2 |
| 76 |
*/ |
| 77 |
private function __wakeup() { |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* MainWP_Api_Manager constructor. |
| 82 |
* |
| 83 |
* Run each time the class is called. |
| 84 |
* |
| 85 |
* Replace HTTP procol to HTTPS. |
| 86 |
*/ |
| 87 |
public function __construct() { |
| 88 |
$this->domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get domain. |
| 93 |
* |
| 94 |
* @return string Current MainWP Dashboard URL. |
| 95 |
*/ |
| 96 |
public function get_domain() { |
| 97 |
return $this->domain; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get Upgrade URL. |
| 102 |
* |
| 103 |
* @return string Activation upgrade URL. |
| 104 |
*/ |
| 105 |
public function get_upgrade_url() { |
| 106 |
$url = apply_filters( 'mainwp_api_manager_upgrade_url', $this->upgrade_url ); |
| 107 |
return $url; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get activation info. |
| 112 |
* |
| 113 |
* @param mixed $ext_key extension key. |
| 114 |
* |
| 115 |
* @return mixed get_option() get activation information. |
| 116 |
*/ |
| 117 |
public function get_activation_info( $ext_key ) { |
| 118 |
if ( empty( $ext_key ) ) { |
| 119 |
return array(); |
| 120 |
} |
| 121 |
|
| 122 |
return get_option( $ext_key . '_APIManAdder' ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Store activation info. |
| 127 |
* |
| 128 |
* @param mixed $ext_key Extension key. |
| 129 |
* @param mixed $info Activation information. |
| 130 |
* |
| 131 |
* @return mixed Set activation info. |
| 132 |
*/ |
| 133 |
public function set_activation_info( $ext_key, $info ) { |
| 134 |
|
| 135 |
if ( empty( $ext_key ) ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
// Clear cached of all activations to reload for next loading. |
| 140 |
update_option( 'mainwp_extensions_all_activation_cached', '' ); |
| 141 |
|
| 142 |
return MainWP_Utility::update_option( $ext_key . '_APIManAdder', $info ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Check API Key & API Email again MainWP Servers. |
| 147 |
* |
| 148 |
* @param array $api Extension activation info. |
| 149 |
* @param string $api_key API license key. |
| 150 |
* @param string $api_email API email address. |
| 151 |
* |
| 152 |
* @return array Activation info. |
| 153 |
*/ |
| 154 |
public function license_key_activation( $api, $api_key, $api_email ) { |
| 155 |
|
| 156 |
$options = $this->get_activation_info( $api ); |
| 157 |
|
| 158 |
if ( ! is_array( $options ) ) { |
| 159 |
$options = array(); |
| 160 |
} |
| 161 |
$current_api_key = isset( $options['api_key'] ) ? $options['api_key'] : ''; |
| 162 |
$current_activation_email = isset( $options['activation_email'] ) ? $options['activation_email'] : ''; |
| 163 |
$activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; |
| 164 |
|
| 165 |
if ( 'Deactivated' == $activation_status || '' == $activation_status || '' == $api_key || '' == $api_email || $current_api_key != $api_key ) { |
| 166 |
if ( $current_api_key != $api_key ) { |
| 167 |
$reset = $this->replace_license_key( |
| 168 |
array( |
| 169 |
'email' => $current_activation_email, |
| 170 |
'licence_key' => $current_api_key, |
| 171 |
'product_id' => $options['product_id'], |
| 172 |
'instance' => $options['instance_id'], |
| 173 |
'platform' => $this->domain, |
| 174 |
) |
| 175 |
); |
| 176 |
if ( ! $reset ) { |
| 177 |
return array( 'error' => __( 'The license could not be deactivated.', 'mainwp' ) ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$return = array(); |
| 182 |
|
| 183 |
$args = array( |
| 184 |
'email' => $api_email, |
| 185 |
'licence_key' => $api_key, |
| 186 |
'product_id' => $options['product_id'], |
| 187 |
'instance' => $options['instance_id'], |
| 188 |
'software_version' => $options['software_version'], |
| 189 |
'platform' => $this->domain, |
| 190 |
); |
| 191 |
|
| 192 |
$activate_results = json_decode( MainWP_Api_Manager_Key::instance()->activate( $args ), true ); |
| 193 |
|
| 194 |
if ( true == $activate_results['activated'] ) { |
| 195 |
$return['result'] = 'SUCCESS'; |
| 196 |
$mess = isset( $activate_results['message'] ) ? $activate_results['message'] : ''; |
| 197 |
$return['message'] = __( 'The extension has been activated. ', 'mainwp' ) . $mess; |
| 198 |
$options['api_key'] = $api_key; |
| 199 |
$options['activation_email'] = $api_email; |
| 200 |
$options['activated_key'] = 'Activated'; |
| 201 |
$options['deactivate_checkbox'] = 'off'; |
| 202 |
} |
| 203 |
|
| 204 |
if ( false == $activate_results ) { |
| 205 |
$apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' ); |
| 206 |
if ( 1 == $apisslverify ) { |
| 207 |
MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 ); |
| 208 |
$return['retry_action'] = 1; |
| 209 |
} else { |
| 210 |
$return['error'] = __( 'Connection failed to the License Key API server. Try again later.', 'mainwp' ); |
| 211 |
} |
| 212 |
$options['api_key'] = ''; |
| 213 |
$options['activation_email'] = ''; |
| 214 |
$options['activated_key'] = 'Deactivated'; |
| 215 |
} |
| 216 |
|
| 217 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 218 |
if ( ! empty( $error ) ) { |
| 219 |
$return['error'] = $error; |
| 220 |
} |
| 221 |
|
| 222 |
$this->set_activation_info( $api, $options ); |
| 223 |
|
| 224 |
return $return; |
| 225 |
} else { |
| 226 |
return array( 'result' => 'SUCCESS' ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Deactivate the current license key before activating the new license key. |
| 232 |
* |
| 233 |
* @param array $args Request arguments. |
| 234 |
* |
| 235 |
* @return bool True on success, false on failure. |
| 236 |
*/ |
| 237 |
private function replace_license_key( $args ) { |
| 238 |
$reset = MainWP_Api_Manager_Key::instance()->deactivate( $args ); // reset license key activation. |
| 239 |
|
| 240 |
if ( true == $reset ) { |
| 241 |
return true; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Deactivate license Key. |
| 247 |
* |
| 248 |
* @param array $api Extension activation info. |
| 249 |
* |
| 250 |
* @return array Deactivation info. |
| 251 |
*/ |
| 252 |
public function license_key_deactivation( $api ) { |
| 253 |
|
| 254 |
$options = $this->get_activation_info( $api ); |
| 255 |
if ( ! is_array( $options ) ) { |
| 256 |
$options = array(); |
| 257 |
} |
| 258 |
|
| 259 |
$activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; |
| 260 |
$current_api_key = isset( $options['api_key'] ) ? $options['api_key'] : ''; |
| 261 |
$current_activation_email = isset( $options['activation_email'] ) ? $options['activation_email'] : ''; |
| 262 |
|
| 263 |
$return = array(); |
| 264 |
|
| 265 |
if ( 'Activated' == $activation_status && '' != $current_api_key && '' != $current_activation_email ) { |
| 266 |
$activate_results = MainWP_Api_Manager_Key::instance()->deactivate( |
| 267 |
array( |
| 268 |
'email' => $current_activation_email, |
| 269 |
'licence_key' => $current_api_key, |
| 270 |
'product_id' => $options['product_id'], |
| 271 |
'instance' => $options['instance_id'], |
| 272 |
'platform' => $this->domain, |
| 273 |
) |
| 274 |
); // reset license key activation. |
| 275 |
|
| 276 |
$activate_results = json_decode( $activate_results, true ); |
| 277 |
if ( true == $activate_results['deactivated'] || ( isset( $activate_results['activated'] ) && 'inactive' == $activate_results['activated'] ) ) { |
| 278 |
$options['api_key'] = ''; |
| 279 |
$options['activation_email'] = ''; |
| 280 |
$options['activated_key'] = 'Deactivated'; |
| 281 |
$options['deactivate_checkbox'] = 'on'; |
| 282 |
$return['result'] = 'SUCCESS'; |
| 283 |
$return['activations_remaining'] = $activate_results['activations_remaining']; |
| 284 |
} |
| 285 |
|
| 286 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 287 |
if ( ! empty( $error ) ) { |
| 288 |
$return['error'] = $error; |
| 289 |
} |
| 290 |
|
| 291 |
$this->set_activation_info( $api, $options ); |
| 292 |
|
| 293 |
return $return; |
| 294 |
} |
| 295 |
// to fix: clear cached of all activations to reload for next loading. |
| 296 |
update_option( 'mainwp_extensions_all_activation_cached', '' ); |
| 297 |
return array( 'result' => 'SUCCESS' ); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Test the users MainWP.com Login details against MainWP Server. |
| 302 |
* |
| 303 |
* @param string $username MainWP registered username. |
| 304 |
* @param string $password MainWP registered password. |
| 305 |
* |
| 306 |
* @return mixed test_login_api() login test result. |
| 307 |
*/ |
| 308 |
public function test_login_api( $username, $password ) { |
| 309 |
if ( empty( $username ) || empty( $password ) ) { |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
return MainWP_Api_Manager_Key::instance()->test_login_api( |
| 314 |
array( |
| 315 |
'username' => $username, |
| 316 |
'password' => $password, |
| 317 |
) |
| 318 |
); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/** |
| 323 |
* Check if the user purchased the software. |
| 324 |
* |
| 325 |
* @param string $username MainWP registered username. |
| 326 |
* @param string $password MainWP registered password. |
| 327 |
* @param string $productId extension (product) ID. |
| 328 |
* |
| 329 |
* @return mixed purchase_software() purchase extensions. |
| 330 |
*/ |
| 331 |
public function purchase_software( $username, $password, $productId ) { |
| 332 |
if ( empty( $username ) || empty( $password ) ) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
return MainWP_Api_Manager_Key::instance()->purchase_software( |
| 337 |
array( |
| 338 |
'username' => $username, |
| 339 |
'password' => $password, |
| 340 |
'product_id' => $productId, |
| 341 |
) |
| 342 |
); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Get users purchased extensions. |
| 347 |
* |
| 348 |
* @param string $username MainWP registered username. |
| 349 |
* @param string $password MainWP registered password. |
| 350 |
* @param string $productId extension (product) ID. |
| 351 |
* @param bool $no_register registration request. |
| 352 |
* |
| 353 |
* @return array Purchased extensions. |
| 354 |
*/ |
| 355 |
public function get_purchased_software( $username, $password, $productId = '', $no_register = false ) { |
| 356 |
if ( empty( $username ) || empty( $password ) ) { |
| 357 |
return false; |
| 358 |
} |
| 359 |
|
| 360 |
return MainWP_Api_Manager_Key::instance()->get_purchased_software( |
| 361 |
array( |
| 362 |
'username' => $username, |
| 363 |
'password' => $password, |
| 364 |
'product_id' => $productId, |
| 365 |
'noauth' => $no_register ? 1 : 0, |
| 366 |
) |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Grab users associate MainWP License key for the selected Extension. |
| 372 |
* |
| 373 |
* @param array $api Extension activation info. |
| 374 |
* @param string $username MainWP registered username. |
| 375 |
* @param string $password MainWP registered password. |
| 376 |
* |
| 377 |
* @return mixed Activation info. |
| 378 |
*/ |
| 379 |
public function grab_license_key( $api, $username, $password ) { |
| 380 |
|
| 381 |
$options = $this->get_activation_info( $api ); |
| 382 |
if ( ! is_array( $options ) ) { |
| 383 |
$options = array(); |
| 384 |
} |
| 385 |
$activation_status = isset( $options['activated_key'] ) ? $options['activated_key'] : ''; |
| 386 |
|
| 387 |
$api_key = isset( $options['api_key'] ) ? $options['api_key'] : ''; |
| 388 |
$api_email = isset( $options['activation_email'] ) ? $options['activation_email'] : ''; |
| 389 |
|
| 390 |
if ( 'Deactivated' == $activation_status || '' == $activation_status || '' == $api_key || '' == $api_email ) { |
| 391 |
$return = array(); |
| 392 |
if ( '' != $username && '' != $password ) { |
| 393 |
|
| 394 |
$args = array( |
| 395 |
'username' => $username, |
| 396 |
'password' => $password, |
| 397 |
'product_id' => isset( $options['product_id'] ) ? $options['product_id'] : '', |
| 398 |
'instance' => isset( $options['instance_id'] ) ? $options['instance_id'] : '', |
| 399 |
'software_version' => isset( $options['software_version'] ) ? $options['software_version'] : '', |
| 400 |
'platform' => $this->domain, |
| 401 |
); |
| 402 |
|
| 403 |
$activate_results = json_decode( MainWP_Api_Manager_Key::instance()->grab_api_key( $args ), true ); |
| 404 |
$options['api_key'] = ''; |
| 405 |
$options['activation_email'] = ''; |
| 406 |
$options['activated_key'] = 'Deactivated'; |
| 407 |
|
| 408 |
if ( is_array( $activate_results ) && isset( $activate_results['activated'] ) && ( true == $activate_results['activated'] ) && ! empty( $activate_results['api_key'] ) ) { |
| 409 |
$return['result'] = 'SUCCESS'; |
| 410 |
$mess = isset( $activate_results['message'] ) ? $activate_results['message'] : ''; |
| 411 |
$return['message'] = __( 'Extension activated. ', 'mainwp' ) . $mess; |
| 412 |
$options['api_key'] = $activate_results['api_key']; |
| 413 |
$return['api_key'] = $activate_results['api_key']; |
| 414 |
$options['activation_email'] = $activate_results['activation_email']; |
| 415 |
$return['activation_email'] = $activate_results['activation_email']; |
| 416 |
$options['activated_key'] = 'Activated'; |
| 417 |
$options['deactivate_checkbox'] = 'off'; |
| 418 |
} else { |
| 419 |
|
| 420 |
if ( false == $activate_results ) { |
| 421 |
$return['error'] = __( 'Connection with the API license server could not be established. Please, try again later.', 'mainwp' ); |
| 422 |
} elseif ( isset( $activate_results['error'] ) ) { |
| 423 |
$return['error'] = $activate_results['error']; |
| 424 |
} elseif ( empty( $activate_results['api_key'] ) ) { |
| 425 |
$return['error'] = __( 'License key could not be found.', 'mainwp' ); |
| 426 |
} else { |
| 427 |
$return['error'] = __( 'An undefined error occurred. Please try again later or contact MainWP Support.', 'mainwp' ); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 432 |
if ( ! empty( $error ) ) { |
| 433 |
$return['error'] = $error; |
| 434 |
} |
| 435 |
|
| 436 |
$this->set_activation_info( $api, $options ); |
| 437 |
|
| 438 |
return $return; |
| 439 |
} else { |
| 440 |
return array( 'error' => __( 'Username and Password are required in order to grab extensions API keys.', 'mainwp' ) ); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
return array( 'result' => 'SUCCESS' ); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Check if $response contains any api errors. |
| 449 |
* |
| 450 |
* @param array $response Response array. |
| 451 |
* |
| 452 |
* @return string $error Error message. |
| 453 |
*/ |
| 454 |
public function check_response_for_api_errors( $response ) { |
| 455 |
if ( ! is_array( $response ) || ! isset( $response['code'] ) ) { |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
$error = ''; |
| 460 |
switch ( $response['code'] ) { |
| 461 |
case '100': |
| 462 |
$error = __( 'Invalid request! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); |
| 463 |
break; |
| 464 |
case '102': |
| 465 |
$error = __( 'Activation error! Download permission for this product could not be found.', 'mainwp' ); |
| 466 |
break; |
| 467 |
case '101': |
| 468 |
$error = __( 'Activation error! Matching API key could not be found.', 'mainwp' ); |
| 469 |
break; |
| 470 |
case '103': |
| 471 |
case '104': |
| 472 |
$error = __( 'Invalid Instance ID! Please try to deactivate and re-activate the extension on the WP > Plugins page and try to activate API key again.', 'mainwp' ); |
| 473 |
break; |
| 474 |
case '105': |
| 475 |
case '106': |
| 476 |
$error = isset( $response['error'] ) ? $response['error'] : ''; |
| 477 |
$info = isset( $response['additional info'] ) ? ' ' . $response['additional info'] : ''; |
| 478 |
$error = $error . $info; |
| 479 |
break; |
| 480 |
case '900': |
| 481 |
$error = __( 'Your membership is on hold. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 482 |
break; |
| 483 |
case '901': |
| 484 |
$error = __( 'Your membership has been canceled. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 485 |
break; |
| 486 |
case '902': |
| 487 |
$error = __( 'Your membership has expired. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 488 |
break; |
| 489 |
} |
| 490 |
return $error; |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Check if $response contains any install errors. |
| 495 |
* |
| 496 |
* @param array $response Response array. |
| 497 |
* @param string $software_title Extension title. |
| 498 |
* |
| 499 |
* @return string $return Installation Error messages. |
| 500 |
*/ |
| 501 |
public function check_response_for_intall_errors( $response, $software_title = '' ) { |
| 502 |
if ( ! is_array( $response ) || ! isset( $response['error'] ) ) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
$return = false; |
| 507 |
switch ( $response['error'] ) { |
| 508 |
case 'subscription_on_hold': |
| 509 |
$return = __( 'Your membership is on hold. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 510 |
break; |
| 511 |
case 'subscription_cancelled': |
| 512 |
$return = __( 'Your membership has been canceled. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 513 |
break; |
| 514 |
case 'subscription_expired': |
| 515 |
$return = __( 'Your membership has expired. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 516 |
break; |
| 517 |
default: // download_revoked. |
| 518 |
$return = sprintf( __( 'Download permission for %1$s has been revoked possibly due to a license key or membership expiring. You can reactivate or purchase a license key from your account <a href="%2$s" target="_blank">dashboard</a>.', 'mainwp' ), $software_title, $this->renew_license_url ); |
| 519 |
break; |
| 520 |
} |
| 521 |
return $return; |
| 522 |
} |
| 523 |
|
| 524 |
/** |
| 525 |
* Check if Extensions have an update. |
| 526 |
* |
| 527 |
* @param array $args Request arguments. |
| 528 |
* |
| 529 |
* @return mixed update_check() plugin info. |
| 530 |
*/ |
| 531 |
public function update_check( $args ) { |
| 532 |
$args['domain'] = $this->domain; |
| 533 |
|
| 534 |
return MainWP_Api_Manager_Plugin_Update::instance()->update_check( $args ); |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Request plugin information |
| 539 |
* |
| 540 |
* @param array $args Request arguments. |
| 541 |
* |
| 542 |
* @return array Plugin info. |
| 543 |
*/ |
| 544 |
public function request_plugin_information( $args ) { |
| 545 |
$args['domain'] = $this->domain; |
| 546 |
|
| 547 |
return MainWP_Api_Manager_Plugin_Update::instance()->request( $args ); |
| 548 |
} |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
// End of class. |
| 553 |
|