class-cache-controller.php
1 year ago
class-css-controller.php
1 year ago
class-domainshift-controller.php
1 year ago
class-key-controller.php
1 year ago
class-log-controller.php
1 year ago
class-option-controller.php
1 year ago
class-rest-controller.php
1 year ago
class-key-controller.php
199 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use Exception; |
| 8 | use SuperbAddons\Data\Controllers\OptionController; |
| 9 | use SuperbAddons\Data\Utils\KeyException; |
| 10 | use SuperbAddons\Data\Utils\KeyType; |
| 11 | use SuperbAddons\Data\Utils\OptionException; |
| 12 | |
| 13 | class KeyController |
| 14 | { |
| 15 | const ENDPOINT_BASE = 'addons-status/'; |
| 16 | |
| 17 | public static function RegisterKey($key, $is_registration = false) |
| 18 | { |
| 19 | try { |
| 20 | $is_valid = strlen($key) === 23 && preg_match('/^[A-Z0-9]{5}(-[A-Z0-9]{5}){3}$/', $key); |
| 21 | if (!$is_valid) { |
| 22 | throw new KeyException(__("Invalid License Key. Please check that the license key was entered correctly.", "superb-blocks")); |
| 23 | } |
| 24 | |
| 25 | $option_controller = new OptionController(); |
| 26 | $stamp = $option_controller->GetStamp(); |
| 27 | $response = DomainShiftController::RemoteGet(self::ENDPOINT_BASE . 'keys?' . ($is_registration ? "registration=true" : "revalidate=true") . '&key=' . $key . '&dm=' . urlencode(home_url()) . '&stamp=' . absint($stamp)); |
| 28 | $response_code = wp_remote_retrieve_response_code($response); |
| 29 | if (!is_array($response) || is_wp_error($response) || $response_code !== 200) { |
| 30 | if ($response_code === 404) { |
| 31 | throw new KeyException(__("License key could not be validated. Please check that the license key was entered correctly.", "superb-blocks"), true, $response_code); |
| 32 | } else { |
| 33 | throw new KeyException(__("Unable to validate license key. Please contact support for assistance.", "superb-blocks"), false, $response_code); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | $data = json_decode($response['body']); |
| 38 | if (!isset($data->level) || !isset($data->active) || !$data->active || !isset($data->expired) || !isset($data->verification) || !$data->verification || !isset($data->verification->exceeded) || !isset($data->verification->verified) || !isset($data->verification->stamp)) { |
| 39 | throw new KeyException(__("License key not currently active. Please contact support for assistance.", "superb-blocks")); |
| 40 | } |
| 41 | |
| 42 | if ($data->verification->exceeded) { |
| 43 | throw new KeyException(__("You have already used up all your domain activations for this license key.", "superb-blocks")); |
| 44 | } |
| 45 | |
| 46 | if ((!$data->verification->verified || !$data->verification->stamp) && $data->active && !$data->expired) { |
| 47 | throw new KeyException(__("License key verification could not complete. Please contact support for assistance.", "superb-blocks")); |
| 48 | } |
| 49 | |
| 50 | if ($data->expired && $data->level !== KeyType::STANDARD) { |
| 51 | throw new KeyException(__("License key has expired. Please renew your subscription to re-activate your license.", "superb-blocks")); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | $option_controller->UpdateKey($key, $data->verification->stamp); |
| 56 | self::UpdateKeyType($data->level, $data->active, $data->expired, $data->exceeded); |
| 57 | return array("type" => $data->level, "active" => $data->active, "expired" => $data->expired, "verified" => $data->verification->verified, "exceeded" => $data->exceeded); |
| 58 | } catch (OptionException $o_ex) { |
| 59 | self::RemoveKey($key, $data->verification->stamp); |
| 60 | throw new KeyException(__("Unable to store license in WordPress. If the problem persists, please contact support.", "superb-blocks")); |
| 61 | } |
| 62 | } catch (KeyException $k_ex) { |
| 63 | throw $k_ex; |
| 64 | } catch (Exception $ex) { |
| 65 | LogController::HandleException($ex); |
| 66 | throw new KeyException(__("Internal Error Occurred During License Key Registration", "superb-blocks")); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public static function RemoveKey($request_key = false, $request_stamp = false) |
| 71 | { |
| 72 | $option_controller = new OptionController(); |
| 73 | try { |
| 74 | $stamp = $request_stamp ? $request_stamp : $option_controller->GetStamp(); |
| 75 | $key = $request_key ? $request_key : $option_controller->GetKey(); |
| 76 | $response = DomainShiftController::RemoteGet(self::ENDPOINT_BASE . 'keys/remove?key=' . $key . '&dm=' . urlencode(home_url()) . '&stamp=' . absint($stamp)); |
| 77 | $response_code = wp_remote_retrieve_response_code($response); |
| 78 | if (!is_array($response) || is_wp_error($response) || $response_code !== 200) { |
| 79 | throw new Exception(__("License key removal record not received.", "superb-blocks"), $response_code); |
| 80 | } |
| 81 | } catch (Exception $ex) { |
| 82 | LogController::HandleException($ex); |
| 83 | } |
| 84 | return $option_controller->RemoveKey(); |
| 85 | } |
| 86 | |
| 87 | public static function GetUpdatedLicenseKeyInformation() |
| 88 | { |
| 89 | try { |
| 90 | $option_controller = new OptionController(); |
| 91 | if ($option_controller->HasRegisteredKey()) { |
| 92 | $key = $option_controller->GetKey(); |
| 93 | return KeyController::RegisterKey($key); |
| 94 | } |
| 95 | return array("type" => KeyType::FREE, "active" => true, "expired" => false, "verified" => true, "exceeded" => false); |
| 96 | } catch (KeyException $k_ex) { |
| 97 | throw $k_ex; |
| 98 | } catch (Exception $ex) { |
| 99 | LogController::HandleException($ex); |
| 100 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public static function HasRegisteredKey() |
| 105 | { |
| 106 | $option_controller = new OptionController(); |
| 107 | return $option_controller->HasRegisteredKey(); |
| 108 | } |
| 109 | |
| 110 | public static function HasValidStandardKey() |
| 111 | { |
| 112 | $option_controller = new OptionController(); |
| 113 | $has_standard = $option_controller->HasStandardKey(); |
| 114 | $is_active = $option_controller->KeyIsActive(); |
| 115 | $is_verified = $option_controller->KeyIsVerified(); |
| 116 | $is_exceeded = $option_controller->KeyIsExceeded(); |
| 117 | return $has_standard && $is_active && $is_verified && !$is_exceeded; |
| 118 | } |
| 119 | |
| 120 | public static function HasValidPremiumKey() |
| 121 | { |
| 122 | $option_controller = new OptionController(); |
| 123 | $has_premium = $option_controller->HasPremiumKey(); |
| 124 | $is_expired = $option_controller->KeyIsExpired(); |
| 125 | $is_active = $option_controller->KeyIsActive(); |
| 126 | $is_verified = $option_controller->KeyIsVerified(); |
| 127 | $is_exceeded = $option_controller->KeyIsExceeded(); |
| 128 | return $has_premium && !$is_expired && $is_active && $is_verified && !$is_exceeded; |
| 129 | } |
| 130 | |
| 131 | public static function HasValidKey() |
| 132 | { |
| 133 | $option_controller = new OptionController(); |
| 134 | $has_key = $option_controller->HasRegisteredKey(); |
| 135 | if (!$has_key) { |
| 136 | return false; |
| 137 | } |
| 138 | $is_expired = !$option_controller->HasStandardKey() && $option_controller->KeyIsExpired(); |
| 139 | $is_active = $option_controller->KeyIsActive(); |
| 140 | $is_verified = $option_controller->KeyIsVerified(); |
| 141 | $is_exceeded = $option_controller->KeyIsExceeded(); |
| 142 | return !$is_expired && $is_active && $is_verified && !$is_exceeded; |
| 143 | } |
| 144 | |
| 145 | public static function GetKeyTypeLabel($keytype) |
| 146 | { |
| 147 | switch ($keytype) { |
| 148 | case KeyType::PREMIUM: |
| 149 | return __("Premium License", "superb-blocks"); |
| 150 | case KeyType::STANDARD: |
| 151 | return __("Theme License", "superb-blocks"); |
| 152 | case KeyType::FREE_PLUS: |
| 153 | return __("Free+ License", "superb-blocks"); |
| 154 | case KeyType::FREE: |
| 155 | default: |
| 156 | return __("Free License", "superb-blocks"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public static function GetCurrentKeyTypeLabel() |
| 161 | { |
| 162 | $option_controller = new OptionController(); |
| 163 | |
| 164 | if (!self::HasValidKey()) { |
| 165 | return self::GetKeyTypeLabel(KeyType::FREE); |
| 166 | } |
| 167 | |
| 168 | return self::GetKeyTypeLabel($option_controller->GetKeyType()); |
| 169 | } |
| 170 | |
| 171 | public static function VerificationFailed() |
| 172 | { |
| 173 | $option_controller = new OptionController(); |
| 174 | return $option_controller->SetKeyVerificationFailed(); |
| 175 | } |
| 176 | |
| 177 | public static function GetKeyStatus() |
| 178 | { |
| 179 | $option_controller = new OptionController(); |
| 180 | return array("type" => $option_controller->GetKeyType(), "active" => $option_controller->KeyIsActive(), "expired" => $option_controller->KeyIsExpired(), "verified" => $option_controller->KeyIsVerified(), "exceeded" => $option_controller->KeyIsExceeded()); |
| 181 | } |
| 182 | |
| 183 | public static function UpdateKeyType($keytype, $active, $expired, $exceeded) |
| 184 | { |
| 185 | $option_controller = new OptionController(); |
| 186 | switch ($keytype) { |
| 187 | case KeyType::PREMIUM: |
| 188 | case KeyType::STANDARD: |
| 189 | case KeyType::FREE_PLUS: |
| 190 | // Accepted key types |
| 191 | break; |
| 192 | default: |
| 193 | $keytype = KeyType::FREE; |
| 194 | break; |
| 195 | } |
| 196 | return $option_controller->UpdateKeyType($keytype, $active, $expired, $exceeded); |
| 197 | } |
| 198 | } |
| 199 |