getLicenseNotifyDate()) { if($settings->getLicenseNotifyDate() > (time()-self::NOTIFY_ERROR_INTERVAL)) return; Alert::add("License check failed", "There was a failed license check. Error: $error. Please visit the following link for more information https://docs.jetbackup.com/licensing_issue_notification.html", Alert::LEVEL_CRITICAL); } $settings->setLicenseNotifyDate(time()); $settings->save(); } private static function _fetchLicense($product_id, $licenseKey, $domain) { $public_key = openssl_get_publickey(static::PUBLIC_KEY); if(!openssl_public_encrypt($licenseKey, $key_encrypted, $public_key)) throw new LicenseException("Unable to prepare license key for validation"); try { $response = JetHttp::request() ->setMethod(JetHttp::METHOD_POST) ->setTimeout(30) ->setReturnTransfer() ->setBody(http_build_query([ 'output' => 'json', 'license_key' => base64_encode($key_encrypted), 'product_id' => $product_id, 'domain' => $domain, ])) ->exec(self::LICENSE_CHECK_URL); } catch(HttpRequestException $e) { throw new LicenseException( "Failed checking license (" . self::LICENSE_CHECK_URL . "). Error: " . $e->getMessage()); } if($response->getHeaders()->getCode() !== 200 || !($data = $response->getBody())) throw new LicenseException( "Could not resolve host (" . self::LICENSE_CHECK_URL . ")"); $result = json_decode($data, true); if($result === false) throw new LicenseException("No valid response received from the license server"); if(!$result['success'] || !$result['data']['localkey']) throw new LicenseException("Invalid response from licensing server: {$result['message']}"); $localKey = $result['data']['localkey']; $newLocalKeyDetails = new LicenseLocalKey($localKey); $signed = $newLocalKeyDetails->getSigned() ? base64_decode($newLocalKeyDetails->getSigned()) : ''; $signed_key = sha1(intval(time() / self::LOCALKEY_DAYS), true); if(!openssl_verify($signed_key, $signed, $public_key, OPENSSL_ALGO_SHA256)) throw new LicenseException( "Failed validating license. " . ($newLocalKeyDetails->getDescription() ? " The error returned from our licensing server: \"" . $newLocalKeyDetails->getDescription() . "\"" : '') . "
Please contact your JetApps license provider if you need to reissue your license"); return $localKey; } /** * @param string|null $licenseKey * * @return void * @throws IOException * @throws LicenseException */ public static function retrieveLocalKey(?string $licenseKey=null) { $config = Factory::getConfig(); $localKeyDetails = new LicenseLocalKey(); if(!$licenseKey) $licenseKey = $config->getLicenseKey(); if (!$licenseKey) { $config->setLicenseLocalKey(''); $config->setLicenseLastCheck(time()); $config->setLicenseNextCheck(time() + self::LOCALKEY_CHECK_INTERVAL); $config->save(); return; } try { $domain = Wordpress::getSiteDomain(); if (!$domain) throw new LicenseException('Cannot find domain'); $domain = preg_replace('/^www\./', '', $domain); $exception = null; foreach(self::LIST_PRODUCT_ID as $product_id) { try { $localKey = self::_fetchLicense($product_id, $licenseKey, $domain); $exception = null; break; } catch(LicenseException $e) { $exception = $e; } } if($exception) throw $exception; $config->setLicenseNotifyDate(); $config->setLicenseLocalKey($localKey); $config->setLicenseLastCheck(time()); $config->setLicenseNextCheck(time() + self::LOCALKEY_CHECK_INTERVAL); $config->save(); } catch(LicenseException $e) { self::_addAlert($e->getMessage()); $config->setLocalKeyInvalid($e->getMessage(), $localKeyDetails); $config->save(); throw $e; } } /** * @throws LicenseException * @throws Exception */ public static function checkLocalKey() { $settings = Factory::getConfig(); if(!$settings->getLicenseKey()) throw new LicenseException("No license key found"); $localKey = new LicenseLocalKey(); if(!$localKey->getLocalKey()) throw new LicenseException("LocalKey is empty"); $status = $localKey->getStatus() ?: self::STATUS_INVALID; $description = $localKey->getDescription() ?? ''; $public_key = openssl_get_publickey(static::PUBLIC_KEY); $modulo = $settings->getLicenseLastCheck() % self::LOCALKEY_DAYS; $signed = $localKey->getSignedStatus() ? base64_decode($localKey->getSignedStatus()) : ''; $signed_key = sha1(intval((time() - $modulo) / self::LOCALKEY_DAYS) . self::STATUS_ACTIVE, true); if(!openssl_verify($signed_key, $signed, $public_key, OPENSSL_ALGO_SHA256)) { if($status == self::STATUS_ACTIVE || ($status == self::STATUS_INVALID && !$description)) { $description = "Cannot find valid license. " . ($description ? " Error: $description." : '') . " Please visit the following link for more information https://docs.jetbackup.com/licensing_issue_notification.html"; $settings->setLocalKeyInvalid($description, $localKey); $settings->setLicenseNextCheck(); $settings->save(); } throw new LicenseException($description, $status); } } /** * @return string * @throws Exception */ public static function getLicenseStatus():string { try { self::checkLocalKey(); } catch(LicenseException $e) { return $e->getStatus(); } return self::STATUS_ACTIVE; } /** * @return bool */ public static function isValid():bool { try { self::checkLocalKey(); } catch(Exception $e) { return false; } return true; } }