| 1 |
<?php |
| 2 |
|
| 3 |
class MainWP_Api_Manager { |
| 4 |
|
| 5 |
private $upgrade_url = 'https://mainwp.com/'; |
| 6 |
private $renew_license_url = 'https://mainwp.com/my-account'; |
| 7 |
public $domain = ''; |
| 8 |
|
| 9 |
/** |
| 10 |
* @var The single instance of the class |
| 11 |
*/ |
| 12 |
protected static $_instance = null; |
| 13 |
|
| 14 |
public static function instance() { |
| 15 |
|
| 16 |
if ( is_null( self::$_instance ) ) { |
| 17 |
self::$_instance = new self(); |
| 18 |
} |
| 19 |
|
| 20 |
return self::$_instance; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Cloning is forbidden. |
| 25 |
* |
| 26 |
* @since 1.2 |
| 27 |
*/ |
| 28 |
private function __clone() { |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Unserializing instances of this class is forbidden. |
| 34 |
* |
| 35 |
* @since 1.2 |
| 36 |
*/ |
| 37 |
private function __wakeup() { |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
public function __construct() { |
| 42 |
$this->domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); |
| 43 |
} |
| 44 |
|
| 45 |
public function get_domain() { |
| 46 |
return $this->domain; |
| 47 |
} |
| 48 |
|
| 49 |
public function getUpgradeUrl() { |
| 50 |
$url = apply_filters( 'mainwp_api_manager_upgrade_url', $this->upgrade_url ); |
| 51 |
return $url; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public function get_activation_info( $ext_key ) { |
| 56 |
if (empty($ext_key)) |
| 57 |
return array(); |
| 58 |
|
| 59 |
return get_option( $ext_key . '_APIManAdder' ); |
| 60 |
} |
| 61 |
|
| 62 |
public function set_activation_info( $ext_key, $info ) { |
| 63 |
|
| 64 |
if (empty($ext_key)) |
| 65 |
return false; |
| 66 |
|
| 67 |
update_option('mainwp_extensions_all_activation_cached', ''); // clear cached of all activations to reload for next loading |
| 68 |
|
| 69 |
return MainWP_Utility::update_option( $ext_key . '_APIManAdder', $info ); |
| 70 |
} |
| 71 |
|
| 72 |
public function license_key_activation( $api, $api_key, $api_email ) { |
| 73 |
|
| 74 |
$options = $this->get_activation_info( $api ); |
| 75 |
|
| 76 |
if ( !is_array( $options ) ) { |
| 77 |
$options = array(); |
| 78 |
} |
| 79 |
$current_api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; |
| 80 |
$current_activation_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; |
| 81 |
$activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; |
| 82 |
|
| 83 |
if ( $activation_status == 'Deactivated' || $activation_status == '' || $api_key == '' || $api_email == '' || $current_api_key != $api_key ) { |
| 84 |
if ( $current_api_key != $api_key ) { |
| 85 |
$reset = $this->replace_license_key( array( |
| 86 |
'email' => $current_activation_email, |
| 87 |
'licence_key' => $current_api_key, |
| 88 |
'product_id' => $options[ 'product_id' ], |
| 89 |
'instance' => $options[ 'instance_id' ], |
| 90 |
'platform' => $this->domain, |
| 91 |
) ); |
| 92 |
if ( !$reset ) { |
| 93 |
return array( 'error' => __( 'The license could not be deactivated.', 'mainwp' ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$return = array(); |
| 98 |
|
| 99 |
$args = array( |
| 100 |
'email' => $api_email, |
| 101 |
'licence_key' => $api_key, |
| 102 |
'product_id' => $options[ 'product_id' ], |
| 103 |
'instance' => $options[ 'instance_id' ], |
| 104 |
'software_version' => $options[ 'software_version' ], |
| 105 |
'platform' => $this->domain, |
| 106 |
); |
| 107 |
|
| 108 |
$activate_results = json_decode( MainWP_Api_Manager_Key::instance()->activate( $args ), true ); |
| 109 |
|
| 110 |
if ( $activate_results[ 'activated' ] == true ) { |
| 111 |
$return[ 'result' ] = 'SUCCESS'; |
| 112 |
$mess = isset( $activate_results[ 'message' ] ) ? $activate_results[ 'message' ] : ''; |
| 113 |
$return[ 'message' ] = __( 'The extension has been activated. ', 'mainwp' ) . $mess; |
| 114 |
$options[ 'api_key' ] = $api_key; |
| 115 |
$options[ 'activation_email' ] = $api_email; |
| 116 |
$options[ 'activated_key' ] = 'Activated'; |
| 117 |
$options[ 'deactivate_checkbox' ] = 'off'; |
| 118 |
} |
| 119 |
|
| 120 |
if ( $activate_results == false ) { |
| 121 |
$apisslverify = get_option( 'mainwp_api_sslVerifyCertificate' ); |
| 122 |
if ( $apisslverify == 1 ) { |
| 123 |
MainWP_Utility::update_option( 'mainwp_api_sslVerifyCertificate', 0 ); |
| 124 |
$return[ 'retry_action' ] = 1; |
| 125 |
} else { |
| 126 |
$return[ 'error' ] = __( 'Connection failed to the License Key API server. Try again later.', 'mainwp' ); |
| 127 |
} |
| 128 |
$options[ 'api_key' ] = ''; |
| 129 |
$options[ 'activation_email' ] = ''; |
| 130 |
$options[ 'activated_key' ] = 'Deactivated'; |
| 131 |
} |
| 132 |
|
| 133 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 134 |
if ( !empty( $error ) ) |
| 135 |
$return[ 'error' ] = $error; |
| 136 |
|
| 137 |
$this->set_activation_info( $api, $options ); |
| 138 |
|
| 139 |
return $return; |
| 140 |
} else { |
| 141 |
return array( 'result' => 'SUCCESS' ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Deactivate the current license key before activating the new license key |
| 146 |
private function replace_license_key( $args ) { |
| 147 |
$reset = MainWP_Api_Manager_Key::instance()->deactivate( $args ); // reset license key activation |
| 148 |
|
| 149 |
if ( $reset == true ) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public function license_key_deactivation( $api ) { |
| 155 |
|
| 156 |
$options = $this->get_activation_info( $api ); |
| 157 |
if ( !is_array( $options ) ) { |
| 158 |
$options = array(); |
| 159 |
} |
| 160 |
|
| 161 |
$activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; |
| 162 |
$current_api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; |
| 163 |
$current_activation_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; |
| 164 |
|
| 165 |
$return = array(); |
| 166 |
|
| 167 |
if ( $activation_status == 'Activated' && $current_api_key != '' && $current_activation_email != '' ) { |
| 168 |
$activate_results = MainWP_Api_Manager_Key::instance()->deactivate( 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 |
) ); // reset license key activation |
| 175 |
|
| 176 |
$activate_results = json_decode( $activate_results, true ); |
| 177 |
if ( $activate_results['deactivated'] == true || (isset( $activate_results['activated'] ) && $activate_results['activated'] == 'inactive')) { |
| 178 |
$options[ 'api_key' ] = ''; |
| 179 |
$options[ 'activation_email' ] = ''; |
| 180 |
$options[ 'activated_key' ] = 'Deactivated'; |
| 181 |
$options[ 'deactivate_checkbox' ] = 'on'; |
| 182 |
$return[ 'result' ] = 'SUCCESS'; |
| 183 |
$return[ 'activations_remaining' ] = $activate_results[ 'activations_remaining' ]; |
| 184 |
} |
| 185 |
|
| 186 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 187 |
if ( !empty( $error ) ) |
| 188 |
$return[ 'error' ] = $error; |
| 189 |
|
| 190 |
$this->set_activation_info( $api, $options ); |
| 191 |
|
| 192 |
return $return; |
| 193 |
} |
| 194 |
update_option('mainwp_extensions_all_activation_cached', ''); // to fix: clear cached of all activations to reload for next loading |
| 195 |
return array( 'result' => 'SUCCESS' ); |
| 196 |
} |
| 197 |
|
| 198 |
public function test_login_api( $username, $password ) { |
| 199 |
if ( empty( $username ) || empty( $password ) ) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
return MainWP_Api_Manager_Key::instance()->testloginapi( array( |
| 204 |
'username' => $username, |
| 205 |
'password' => $password, |
| 206 |
) ); |
| 207 |
} |
| 208 |
|
| 209 |
public function purchase_software( $username, $password, $productId ) { |
| 210 |
if ( empty( $username ) || empty( $password ) ) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
return MainWP_Api_Manager_Key::instance()->purchasesoftware( array( |
| 215 |
'username' => $username, |
| 216 |
'password' => $password, |
| 217 |
'product_id' => $productId |
| 218 |
) ); |
| 219 |
} |
| 220 |
|
| 221 |
public function get_purchased_software( $username, $password, $productId = "", $no_register = false ) { |
| 222 |
if ( empty( $username ) || empty( $password ) ) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
return MainWP_Api_Manager_Key::instance()->getpurchasedsoftware( array( |
| 227 |
'username' => $username, |
| 228 |
'password' => $password, |
| 229 |
'product_id' => $productId, |
| 230 |
'noauth' => $no_register ? 1 : 0 |
| 231 |
) ); |
| 232 |
} |
| 233 |
|
| 234 |
public function grab_license_key( $api, $username, $password ) { |
| 235 |
|
| 236 |
$options = $this->get_activation_info( $api ); |
| 237 |
if ( !is_array( $options ) ) { |
| 238 |
$options = array(); |
| 239 |
} |
| 240 |
$activation_status = isset( $options[ 'activated_key' ] ) ? $options[ 'activated_key' ] : ''; |
| 241 |
|
| 242 |
$api_key = isset( $options[ 'api_key' ] ) ? $options[ 'api_key' ] : ''; |
| 243 |
$api_email = isset( $options[ 'activation_email' ] ) ? $options[ 'activation_email' ] : ''; |
| 244 |
|
| 245 |
if ( $activation_status == 'Deactivated' || $activation_status == '' || $api_key == '' || $api_email == '' ) { |
| 246 |
$return = array(); |
| 247 |
if ( $username != '' && $password != '' ) { |
| 248 |
|
| 249 |
$args = array( |
| 250 |
'username' => $username, |
| 251 |
'password' => $password, |
| 252 |
'product_id' => isset( $options[ 'product_id' ] ) ? $options[ 'product_id' ] : '', |
| 253 |
'instance' => isset( $options[ 'instance_id' ] ) ? $options[ 'instance_id' ] : '', |
| 254 |
'software_version' => isset( $options[ 'software_version' ] ) ? $options[ 'software_version' ] : '', |
| 255 |
'platform' => $this->domain, |
| 256 |
); |
| 257 |
|
| 258 |
$activate_results = json_decode( MainWP_Api_Manager_Key::instance()->grabapikey( $args ), true ); |
| 259 |
$options[ 'api_key' ] = ''; |
| 260 |
$options[ 'activation_email' ] = ''; |
| 261 |
$options[ 'activated_key' ] = 'Deactivated'; |
| 262 |
|
| 263 |
if ( is_array( $activate_results ) && isset( $activate_results[ 'activated' ] ) && ( $activate_results[ 'activated' ] == true ) && !empty( $activate_results[ 'api_key' ] ) ) { |
| 264 |
$return[ 'result' ] = 'SUCCESS'; |
| 265 |
$mess = isset( $activate_results[ 'message' ] ) ? $activate_results[ 'message' ] : ''; |
| 266 |
$return[ 'message' ] = __( 'Extension activated. ', 'mainwp' ) . $mess; |
| 267 |
$options[ 'api_key' ] = $return[ 'api_key' ] = $activate_results[ 'api_key' ]; |
| 268 |
$options[ 'activation_email' ] = $return[ 'activation_email' ] = $activate_results[ 'activation_email' ]; |
| 269 |
$options[ 'activated_key' ] = 'Activated'; |
| 270 |
$options[ 'deactivate_checkbox' ] = 'off'; |
| 271 |
} else { |
| 272 |
|
| 273 |
if ( $activate_results == false ) { |
| 274 |
$return[ 'error' ] = __( 'Connection with the API license server could not be established. Please, try again later.', "mainwp" ); |
| 275 |
} else if ( isset( $activate_results[ 'error' ] ) ) { |
| 276 |
$return[ 'error' ] = $activate_results[ 'error' ]; |
| 277 |
} else if ( empty( $activate_results[ 'api_key' ] ) ) { |
| 278 |
$return[ 'error' ] = __( 'License key could not be found.', 'mainwp' ); |
| 279 |
} else { |
| 280 |
$return[ 'error' ] = __( 'An undefined error occurred. Please try again later or contact MainWP Support.', 'mainwp' ); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
$error = $this->check_response_for_api_errors( $activate_results ); |
| 285 |
if ( !empty( $error ) ) |
| 286 |
$return[ 'error' ] = $error; |
| 287 |
|
| 288 |
$this->set_activation_info( $api, $options ); |
| 289 |
|
| 290 |
return $return; |
| 291 |
} else { |
| 292 |
return array( 'error' => __( 'Username and Password are required in order to grab extensions API keys.', 'mainwp' ) ); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return array( 'result' => 'SUCCESS' ); |
| 297 |
} |
| 298 |
|
| 299 |
public function check_response_for_api_errors( $response ) { |
| 300 |
if ( !is_array( $response ) || !isset( $response[ 'code' ] ) ) |
| 301 |
return false; |
| 302 |
|
| 303 |
$error = ''; |
| 304 |
switch ( $response[ 'code' ] ) { |
| 305 |
case '100': |
| 306 |
$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' ); |
| 307 |
break; |
| 308 |
case '102': |
| 309 |
$error = __( 'Activation error! Download permission for this product could not be found.', 'mainwp' ); |
| 310 |
break; |
| 311 |
case '101': |
| 312 |
$error = __( 'Activation error! Matching API key could not be found.', 'mainwp' ); |
| 313 |
break; |
| 314 |
case '103': |
| 315 |
case '104': |
| 316 |
$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' ); |
| 317 |
break; |
| 318 |
case '105': |
| 319 |
case '106': |
| 320 |
$error = isset( $response[ 'error' ] ) ? $response[ 'error' ] : ''; |
| 321 |
$info = isset( $response[ 'additional info' ] ) ? ' ' . $response[ 'additional info' ] : ''; |
| 322 |
$error = $error . $info; |
| 323 |
break; |
| 324 |
case '900' : |
| 325 |
$error = __( 'Your membership is on hold. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 326 |
break; |
| 327 |
case '901' : |
| 328 |
$error = __( 'Your membership has been canceled. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 329 |
break; |
| 330 |
case '902' : |
| 331 |
$error = __( 'Your membership has expired. Reactivate your membership to activate MainWP extensions', 'mainwp' ); |
| 332 |
break; |
| 333 |
} |
| 334 |
return $error; |
| 335 |
} |
| 336 |
|
| 337 |
public function check_response_for_intall_errors( $response, $software_title = "" ) { |
| 338 |
if ( !is_array( $response ) || !isset( $response[ 'error' ] ) ) |
| 339 |
return false; |
| 340 |
|
| 341 |
switch ( $response[ 'error' ] ) { |
| 342 |
case 'subscription_on_hold': |
| 343 |
return __( 'Your membership is on hold. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 344 |
break; |
| 345 |
case 'subscription_cancelled': |
| 346 |
return __( 'Your membership has been canceled. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 347 |
break; |
| 348 |
case 'subscription_expired': |
| 349 |
return __( 'Your membership has expired. Reactivate your membership to install MainWP extensions.', 'mainwp' ); |
| 350 |
break; |
| 351 |
default : //download_revoked |
| 352 |
return sprintf( __( 'Download permission for %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="%s" target="_blank">dashboard</a>.', 'mainwp' ), $software_title, $this->renew_license_url ); |
| 353 |
break; |
| 354 |
} |
| 355 |
return false; |
| 356 |
} |
| 357 |
|
| 358 |
public function update_check( $args ) { |
| 359 |
$args[ 'domain' ] = $this->domain; |
| 360 |
|
| 361 |
return MainWP_Api_Manager_Plugin_Update::instance()->update_check( $args ); |
| 362 |
} |
| 363 |
|
| 364 |
public function request_plugin_information( $args ) { |
| 365 |
$args[ 'domain' ] = $this->domain; |
| 366 |
|
| 367 |
return MainWP_Api_Manager_Plugin_Update::instance()->request( $args ); |
| 368 |
} |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
// End of class |
| 373 |
|