options = new AmzFulfillment_Options(); } /** * activate */ public function activate() { $this->options->load(); if($this->hasPro()) { AmzFulfillment_Core::instance()->addAdminMessage(AmzFulfillment_Core::ERROR, 'License is allready activaed'); return; } elseif(!isset($_REQUEST['licenseKey']) || empty($_REQUEST['licenseKey'])) { AmzFulfillment_Core::instance()->addAdminMessage(AmzFulfillment_Core::ERROR, 'No license entered'); return; } $newLicenseKey = $_REQUEST['licenseKey']; try { $licenseData = $this->query(self::LICENSE_ACTIVATE_ACTION, $newLicenseKey); if($licenseData->result === 'success') { $expireTime = strtotime($licenseData->date_expiry); $expireDate = date('d.m.Y', $expireTime); $this->options->setLicense($newLicenseKey, $expireTime); $this->options->setLicenseOldKey(false); $this->options->save(); $message = 'Activation succeeded'; $level = AmzFulfillment_Core::SUCCESS; } else { $message = sprintf('Failed to activate license: (%d) %s', intval($licenseData->error_code) , $licenseData->message); $level = AmzFulfillment_Core::ERROR; } } catch(Exception $e) { $message = sprintf('Failed to activate license: %s', $e->getMessage()); $level = AmzFulfillment_Core::ERROR; } AmzFulfillment_Core::instance()->addAdminMessage($level, $message); AmzFulfillment_Core::instance()->getLog()->add($message); AmzFulfillment_Core::debug($message); } /** * deactivate */ public function deactivate() { $this->options->load(); if(!$this->hasPro()) { AmzFulfillment_Core::instance()->addAdminMessage(AmzFulfillment_Core::ERROR, 'No license to deactivate'); return; } try { $licenseKey = $this->options->getLicenseKey(); $licenseData = $this->query(self::LICENSE_DEACTIVATE_ACTION, $licenseKey); if($licenseData->result === 'success' || $licenseData->error_code === self::ALREADY_INACTIVE_CODE) { $this->options->setLicense(false, false); $this->options->setLicenseOldKey(false); $this->options->save(); $message = 'Deactivation succeeded'; $level = AmzFulfillment_Core::SUCCESS; } else { $message = sprintf('Failed to deactivate license: (%d) %s', intval($licenseData->error_code) , $licenseData->message); $level = AmzFulfillment_Core::ERROR; } } catch(Exception $e) { $message = sprintf('Failed to deactivate license: %s', $e->getMessage()); $level = AmzFulfillment_Core::ERROR; } AmzFulfillment_Core::instance()->addAdminMessage($level, $message); AmzFulfillment_Core::instance()->getLog()->add($message); AmzFulfillment_Core::debug($message); } /** * update */ public function update() { $this->options->load(); if(!$this->hasPro()) { return; } try { $licenseData = $this->query(self::LICENSE_CHECK_ACTION, $this->options->getLicenseKey()); if($licenseData->result !== 'success') { AmzFulfillment_Core(sprintf('License check failed: %s', json_encode($licenseData))); return; } if($licenseData->status ==='blocked') { $this->options->load(); $this->options->setLicenseOldKey($this->options->getLicenseKey()); $this->options->setLicense(false, false); $this->options->save(); AmzFulfillment_Core::instance()->getLog()->add('License was revoked'); AmzFulfillment_Core::debug('License was revoked'); } } catch(Exception $e) { AmzFulfillment_Core::debug('License query failed: ' . $e->getMessage()); } } /** * hasPro * * @return boolean */ public function hasPro() { $this->options->load(); if($this->options->getLicenseKey() === false) { return false; } elseif(time() > $this->options->getLicenseExpireDate()) { $this->options->setLicenseOldKey($this->options->getLicenseKey()); $this->options->setLicense(false, false); $this->options->save(); AmzFulfillment_Core::instance()->getLog()->add("License expired"); return false; } else { return true; } } /** * query * * @param string $action * @param string $licenseKey * @throws Exception * @return object */ private function query($action, $licenseKey) { $requestArgs = array( 'slm_action' => $action, 'secret_key' => base64_decode('NThkMTc2YTZhM2UzNDYuMDIwODY5MTI='), 'license_key' => $licenseKey, 'registered_domain' => $_SERVER['SERVER_NAME'], 'item_reference' => urlencode(self::ITEM_REFERENCE) ); $requestOptions = array( 'timeout' => self::LICENSE_CHECK_TIMEOUT, 'sslverify' => self::LICENSE_CHECK_SSL ); $request = esc_url_raw(add_query_arg($requestArgs, self::LICENSE_CHECK_SERVER)); $response = wp_remote_get($request, $requestOptions); if (is_wp_error($response)) { throw new Exception('Request failed: ' . json_encode($response)); } $licenseData = json_decode(wp_remote_retrieve_body($response)); if($licenseData=== NULL || !is_object($licenseData)) { throw new Exception('Request failed: No valid json response'); } return $licenseData; } }