PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.0.7
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.0.7
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / data / controllers / class-key-controller.php
superb-blocks / src / data / controllers Last commit date
class-cache-controller.php 2 years ago class-domainshift-controller.php 2 years ago class-key-controller.php 2 years ago class-log-controller.php 2 years ago class-option-controller.php 2 years ago class-rest-controller.php 2 years ago
class-key-controller.php
175 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.", 'superbaddons'));
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.", 'superbaddons'), true, $response_code);
32 } else {
33 throw new KeyException(__("Unable to validate license key. Please contact support for assistance.", 'superbaddons'), 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.", 'superbaddons'));
40 }
41
42 if ($data->verification->exceeded) {
43 throw new KeyException(__("You have already used up all your domain activations for this license key.", 'superbaddons'));
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.", 'superbaddons'));
48 }
49
50 if ($data->expired) {
51 throw new KeyException(__("License key has expired. Please renew your subscription to re-activate your license.", 'superbaddons'));
52 }
53
54 try {
55 $option_controller->UpdateKey($key, $data->verification->stamp);
56 self::UpdateKeyType($data->level, $data->active, $data->expired);
57 return array("type" => $data->level, "active" => $data->active, "expired" => $data->expired, "verified" => $data->verification->verified);
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.", 'superbaddons'));
61 }
62 } catch (KeyException $k_ex) {
63 throw $k_ex;
64 } catch (Exception $ex) {
65 LogController::HandleException($ex);
66 throw new KeyException(__("Internal Error Occured During License Key Registration", 'superbaddons'));
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.", 'superbaddons'), $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);
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 HasPremiumKey()
111 {
112 $option_controller = new OptionController();
113 $has_premium = $option_controller->HasPremiumKey();
114 $is_expired = $option_controller->KeyIsExpired();
115 $is_active = $option_controller->KeyIsActive();
116 $is_verified = $option_controller->KeyIsVerified();
117 return $has_premium && !$is_expired && $is_active && $is_verified;
118 }
119
120 public static function GetKeyTypeLabel($keytype)
121 {
122 switch ($keytype) {
123 case KeyType::PREMIUM:
124 return __("Premium License", 'superbaddons');
125 case KeyType::FREE_PLUS:
126 return __("Free+ License", 'superbaddons');
127 case KeyType::FREE:
128 default:
129 return __("Free License", 'superbaddons');
130 }
131 }
132
133 public static function GetCurrentKeyTypeLabel()
134 {
135 $option_controller = new OptionController();
136
137 if (!self::HasRegisteredKey()) {
138 return self::GetKeyTypeLabel(KeyType::FREE);
139 }
140
141 if (!self::HasPremiumKey()) {
142 return self::GetKeyTypeLabel(KeyType::FREE_PLUS);
143 }
144
145 return self::GetKeyTypeLabel($option_controller->GetKeyType());
146 }
147
148 public static function VerificationFailed()
149 {
150 $option_controller = new OptionController();
151 return $option_controller->SetKeyVerificationFailed();
152 }
153
154 public static function GetKeyStatus()
155 {
156 $option_controller = new OptionController();
157 return array("active" => $option_controller->KeyIsActive(), "expired" => $option_controller->KeyIsExpired(), "verified" => $option_controller->KeyIsVerified());
158 }
159
160 public static function UpdateKeyType($keytype, $active, $expired)
161 {
162 $option_controller = new OptionController();
163 switch ($keytype) {
164 case KeyType::PREMIUM:
165 case KeyType::FREE_PLUS:
166 // Accepted key types
167 break;
168 default:
169 $keytype = KeyType::FREE;
170 break;
171 }
172 return $option_controller->UpdateKeyType($keytype, $active, $expired);
173 }
174 }
175