PluginProbe
ezCache / 1.2
ezCache v1.2
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / LicenseApi.php

LicenseApi.php in ezCache 1.2, at includes/LicenseApi.php

241 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Upress\EzCache;
3
4 use ErrorException;
5 use Upress\EzCache\Utilities\Encrypter;
6 use WP_Error;
7
8 class LicenseApi {
9 protected $domain;
10 protected $encrypter;
11 protected $pid = 'EZCSLPRO';
12 protected $api_url = 'https://ezcache.app';
13 protected $opt_key = 'ezcache_license';
14 protected $opt_key_data = 'ezcache_license_data';
15
16 protected static $_key;
17 protected static $_license_data;
18
19 function __construct() {
20 $this->domain = str_replace( [ 'https://', 'http://' ], '', get_bloginfo( 'wpurl' ) );
21 $this->encrypter = new Encrypter();
22 }
23
24 /*
25 Array
26 (
27 [status] => success
28 [status_code] => s205
29 [message] => הרשיון פעיל ותקף בעבור דומיין זה
30 [licence_status] => active
31 [licence_start] => 2019-07-21
32 [licence_expire] => 2020-07-21
33 [key] => 6C597A11-B502131F-8902E4CC
34 )
35 */
36 public function get_expiration_date() {
37 $data = $this->get_license_data();
38 return isset( $data['licence_expire'] ) ? strtotime( $data['licence_expire'] ) : 0;
39 }
40
41 public function is_license_valid() {
42 $data = $this->get_license_data();
43 return isset( $data['license_status'] ) && $data['license_status'] == 'active';
44 }
45
46 public function get_masked_license_key() {
47 $key = $this->get_license_key();
48 if ( empty( $key ) ) return '';
49
50 $maskedKey = substr( $key, - 4 );
51 return "•••• •••• •••• {$maskedKey}";
52 }
53
54 public function get_license_key() {
55 if ( self::$_key ) return self::$_key;
56
57 try {
58 self::$_key = $this->encrypter->decrypt( get_option( $this->opt_key ) );
59 } catch( ErrorException $ex ) {
60 self::$_key = '';
61 }
62
63 return self::$_key;
64 }
65
66 public function get_license_data( $cached=true ) {
67 if ( self::$_license_data ) return self::$_license_data;
68
69 try {
70 self::$_license_data = $this->encrypter->decrypt( get_transient( $this->opt_key_data ) );
71 } catch( ErrorException $ex ) {
72 self::$_license_data = [];
73 }
74
75 if ( ! $cached || empty( self::$_license_data ) ) {
76 $key = $this->get_license_key();
77 // get the current status and cache it
78 self::$_license_data = $this->status_check( $key );
79 self::$_license_data = array_merge( (array) self::$_license_data, [ 'key' => $key ] );
80 set_transient( $this->opt_key_data, $this->encrypter->encrypt( self::$_license_data ), DAY_IN_SECONDS );
81 }
82
83 return self::$_license_data;
84 }
85
86 public function update_license( $key ) {
87 if ( empty( $key ) ) {
88 return new WP_Error( 'empty_license_key', __( 'Can\'t set empty license key', 'ezcache' ) );
89 }
90
91 // try to activate the license
92 $response = $this->activate( $key );
93 if ( isset( $response->status ) && 'error' == $response->status ) {
94 return new WP_Error( $response->status_code, $response->message );
95 }
96
97 // get the current status and cache it
98 self::$_key = $key;
99 self::$_license_data = $this->status_check( $key );
100 self::$_license_data = array_merge( (array) self::$_license_data, [ 'key' => $key ] );
101 update_option( $this->opt_key, $this->encrypter->encrypt( $key ) );
102 set_transient( $this->opt_key_data, $this->encrypter->encrypt( self::$_license_data ), DAY_IN_SECONDS );
103
104 return self::$_license_data;
105 }
106
107 public function clear_license() {
108 $key = $this->get_license_key();
109
110 $response = [];
111 if ( ! empty( $key ) ) {
112 // deactivate the license on the server
113 if ( ! empty( $key ) ) {
114 $response = $this->deactivate( $key );
115
116 if ( isset( $response->status ) && 'error' == $response->status && 'e111' != $response->status_code ) {
117 return new WP_Error( $response->status_code, $response->message );
118 }
119 }
120 }
121
122 // and clear any cached data
123 self::$_key = null;
124 self::$_license_data = null;
125 delete_option( $this->opt_key );
126 delete_transient( $this->opt_key_data );
127
128 return $this->get_license_data( false );
129 }
130
131
132
133 /**
134 * Check the license status for a key
135 *
136 * @param string $key License key
137 *
138 * @return array|mixed|object|WP_Error
139 */
140 protected function status_check( $key ) {
141 return $this->api_function( 'status-check', $key );
142 }
143
144 /**
145 * Activate the current website for the provided key
146 *
147 * @param string $key License key
148 *
149 * @return array|mixed|object|WP_Error
150 */
151 protected function activate( $key ) {
152 return $this->api_function( 'activate', $key );
153 }
154
155 /**
156 * Deactivate the current website from the provided key
157 *
158 * @param string $key License key
159 *
160 * @return array|mixed|object|WP_Error
161 */
162 protected function deactivate( $key ) {
163 return $this->api_function( 'deactivate', $key );
164 }
165
166
167 /**
168 * Run a API function
169 *
170 * @param string $action API action
171 * @param string $key License key
172 *
173 * @return array|mixed|object|WP_Error
174 */
175 protected function api_function( $action, $key ) {
176 $response = wp_safe_remote_get( $this->api_url . '?' . http_build_query( [
177 'woo_sl_action' => $action,
178 'licence_key' => $key,
179 'product_unique_id' => $this->pid,
180 'domain' => $this->domain,
181 ] ) );
182
183 if ( is_wp_error( $response ) ) {
184 /** @param WP_Error $response */
185 return (object)[
186 'status' => 'error',
187 'status_code' => 'error_checking_status',
188 'message' => $response->get_error_message(),
189 ];
190 }
191
192 $response = json_decode( wp_remote_retrieve_body( $response ) );
193 if ( is_array( $response ) ) {
194 $response = array_shift( $response );
195 }
196
197 if ( isset( $response->status_code ) ) {
198 $response->message = $this->get_status_message( $response->status_code );
199 }
200
201 return $response;
202 }
203
204 /**
205 * Get a message text for a specific status code
206 *
207 * @param string $status
208 *
209 * @return mixed|string|void
210 */
211 protected function get_status_message( $status ) {
212 $map = [
213 // success responses
214 's100' => _x( 'Licence key successfully activated for the current domain', 'license API response message', 'ezcache' ),
215 's101' => _x( 'Licence key successfully activated for the current domain', 'license API response message', 'ezcache' ),
216 's201' => _x( 'Licence key successfully unassigned', 'license API response message', 'ezcache' ),
217 's203' => _x( 'Licence key is not valid for this website', 'license API response message', 'ezcache' ),
218 's205' => _x( 'Licence key is active and valid for this website', 'license API response message', 'ezcache' ),
219 's401' => '', // a full response with code metadata on calling plugin_update or theme_update methods
220 's402' => '', // a full response with code metadata on calling plugin_information method
221 's403' => '', // a full response with code metadata on calling code_information method
222 's610' => _x( 'Licence key successfully deleted', 'license API response message', 'ezcache' ),
223
224 // error responses
225 'e001' => _x( 'Invalid provided data', 'license API response message', 'ezcache' ),
226 'e002' => _x( 'Invalid licence key', 'license API response message', 'ezcache' ),
227 'e003' => _x( 'Order does not exists anymore', 'license API response message', 'ezcache' ),
228 'e004' => _x( 'Order status not allowed', 'license API response message', 'ezcache' ),
229 'e110' => _x( 'Invalid licence key or licence not active for domain', 'license API response message', 'ezcache' ),
230 'e111' => _x( 'Invalid data', 'license API response message', 'ezcache' ),
231 'e112' => _x( 'You had reached the maximum number of domains for this key', 'license API response message', 'ezcache' ),
232 'e204' => _x( 'Licence key not active for current domain', 'license API response message', 'ezcache' ),
233 'e301' => _x( 'Licence key does not match this product', 'license API response message', 'ezcache' ),
234 'e312' => _x( 'Licence is not active', 'license API response message', 'ezcache' ),
235 'e419' => _x( 'Invalid product unique ID', 'license API response message', 'ezcache' ),
236 ];
237
238 return isset( $map[ $status ] ) ? $map[ $status ] : __( 'Unknown error', 'ezcache' );
239 }
240 }
241