-
Notifications
You must be signed in to change notification settings - Fork 2
/
Client.php
333 lines (286 loc) · 7.85 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace IdeoLogix\DigitalLicenseManagerUpdaterWP\Http;
use IdeoLogix\DigitalLicenseManagerUpdaterWP\Models\Model;
class Client {
/**
* The consumer key
* @var string
*/
protected $consumerKey;
/**
* The consumer secret
* @var string
*/
protected $consumerSecret;
/**
* The base url
* @var string
*/
protected $baseUrl;
/**
* The prefix
* @var string
*/
protected $prefix;
/**
* The cache TTL
* @var float[]|int[]
*/
protected $cacheTTL = [
'info' => MINUTE_IN_SECONDS * 15,
'validateLicense' => HOUR_IN_SECONDS * 1,
];
/**
* Client constructor.
*
* @param $args
*/
public function __construct( $args ) {
$this->consumerKey = isset( $args['consumer_key'] ) ? $args['consumer_key'] : '';
$this->consumerSecret = isset( $args['consumer_secret'] ) ? $args['consumer_secret'] : '';
$this->baseUrl = isset( $args['api_url'] ) ? $args['api_url'] : '';
$this->prefix = isset( $args['prefix'] ) ? $args['prefix'] : 'dlm';
foreach ( $this->cacheTTL as $key => $value ) {
$this->cacheTTL[ $key ] = isset( $args[ 'cache_ttl_' . $key ] ) && is_numeric( $args[ 'cache_ttl_' . $key ] ) ? intval( $args[ 'cache_ttl_' . $key ] ) : $this->cacheTTL[ $key ];
}
}
/**
* Returns the license cache key
*
* @param string $token
*
* @return string
*/
public function getLicenseCacheKey( $token ) {
return sprintf( '%s_license_%s', $this->prefix, substr( md5( $token ), 0, 12 ) );
}
/**
* Returns the update cache key
*
* @param Model $entity
*
* @return string
*/
public function getUpdateCacheKey( $entity ) {
return sprintf( '%s_update_%s_%s', $this->prefix, $entity->getId(), $entity->getVersion() );
}
/**
* Returns license
*
* @param $licenseKey
* @param bool $decode
*
* @return array|\WP_Error
*/
public function getLicense( $licenseKey, $decode = true ) {
$result = $this->_get( 'licenses/' . $licenseKey );
return $this->_result( $result, $decode );
}
/**
* Activate license
*
* @param $licenseKey
* @param array $params
* @param bool $decode
*
* @return array|\WP_Error
*/
public function activateLicense( $licenseKey, $params = array(), $decode = true ) {
$result = $this->_get( 'licenses/activate/' . $licenseKey, $params );
return $this->_result( $result, $decode );
}
/**
* Deactivate license
*
* @param $activationToken
* @param bool $decode
*
* @return array|\WP_Error
*/
public function deactivateLicense( $activationToken, $params = array(), $decode = true ) {
$result = $this->_get( 'licenses/deactivate/' . $activationToken, $params );
return $this->_result( $result, $decode );
}
/**
* Deactivate license
*
* @param $activationToken
* @param bool $decode
*
* @return array|\WP_Error
*/
public function validateLicense( $activationToken, $decode = true ) {
$result = $this->_get( 'licenses/validate/' . $activationToken );
return $this->_result( $result, $decode );
}
/**
* Find remote info
*
* @param $entity
* @param string $type
* @param bool $decode
*
* @return array|Response|\WP_Error
*/
private function info( $entity, $type = 'wp', $decode = true ) {
$softwareId = $entity->getId();
$acitvationToken = $entity->getActivationToken();
$params = array( 'type' => $type );
if ( ! empty( $acitvationToken ) ) {
$params['activation_token'] = $acitvationToken;
}
$result = $this->_get( 'software/' . $softwareId, $params );
return $this->_result( $result, $decode );
}
/**
* Find remote info (Cached)
*
* @param Model $entity
* @param string $type
* @param bool $decode
*
* @return array
*/
public function prepareInfo( $entity, $type = 'wp', $decode = true, $force = false ) {
$cacheResp = false;
$cacheTTL = isset( $this->cacheTTL['info'] ) ? $this->cacheTTL['info'] : 0;
$transient = $this->getUpdateCacheKey( $entity );
$response = $this->info( $entity, $type, $decode );
if ( $force || $cacheTTL <= 0 ) {
if ( $force && $cacheTTL > 0 ) {
$cacheResp = true;
}
} else {
$cacheResp = true;
}
if ( $cacheResp && $cacheTTL > 0 ) {
if ( ! $response->isError() ) {
set_transient( $transient, $response, $cacheTTL );
}
}
return is_a( $response, Response::class ) ? $response->getData() : array();
}
/**
* Retrieve the license
*
* @param $token
* @param bool $decode
* @param bool $force
*
* @return array
*/
public function prepareValidateLicense( $token, $decode = true, $force = false ) {
$cacheResp = false;
$cacheTTL = isset( $this->cacheTTL['validateLicense'] ) ? $this->cacheTTL['validateLicense'] : 0;
$transient = $this->getLicenseCacheKey( $token );
$response = $this->validateLicense( $token, $decode );
if ( $force || $cacheTTL <= 0 ) {
if ( $force && $cacheTTL > 0 ) {
$cacheResp = true;
}
} else {
$cacheResp = true;
}
if ( $cacheResp && $cacheTTL > 0 ) {
if ( ! $response->isError() ) {
set_transient( $transient, $response, $cacheTTL );
}
}
return is_a( $response, Response::class ) ? $response->getData() : array();
}
/**
* The result
*
* @param array|\WP_Error $response
* @param $decode
*
* @return Response|array|\WP_Error
*/
protected function _result( $response, $decode ) {
if ( $decode ) {
$newResponse = new Response();
if ( is_wp_error( $response ) ) {
$newResponse->setCode( 'http_error' );
$newResponse->setError( $response->get_error_message() );
} else {
$responseData = @json_decode( $response['body'], true );
if ( ! is_array( $responseData ) ) {
$newResponse->setError( 'Unable to decode response. (1)' );
} else if ( isset( $responseData['message'] ) && isset( $responseData['data']['status'] ) ) {
$newResponse->setError( $responseData['message'] );
$newResponse->setCode( $responseData['code'] );
} else {
if ( ! isset( $responseData['data'] ) ) {
$newResponse->setError( 'Unable to decode response. (2)' );
} else {
$newResponse->setData( $responseData['data'] );
}
}
}
$newResponse->setRaw( $response );
$response = $newResponse;
}
return $response;
}
/**
* Performs HTTP GET request
*
* @param $path
* @param array $params
* @param array $headers
*
* @return array|\WP_Error
*/
protected function _get( $path, $params = array(), $headers = array() ) {
$url = add_query_arg( $params, trailingslashit( $this->baseUrl ) . ltrim( $path, '/' ) );
$headers = array_merge( $this->authHeaders(), $headers );
$params = apply_filters( 'wlm_http_get_params', array( 'timeout' => 5, 'headers' => $headers ), $this );
return wp_remote_get( $url, $params );
}
/**
* Performs HTTP POST request
*
* @param $path
* @param array $params
* @param array $headers
*
* @return array|\WP_Error
*/
protected function _post( $path, $params = array(), $headers = array() ) {
$url = trailingslashit( $this->baseUrl ) . ltrim( $path, '/' );
$headers = array_merge( $this->authHeaders(), $headers );
$params = apply_filters( 'wlm_http_post_params', array(
'timeout' => 5,
'headers' => $headers,
'body' => $params
), $this );
return wp_remote_post( $url, $params );
}
/**
* Clear the client cache
*
* @param Model $entity
*/
public function clearCache( $entity ) {
// Clear license cache
$token = $entity->getActivationToken();
if ( $token ) {
$licenseCacheKey = $this->getLicenseCacheKey( $token );
delete_transient( $licenseCacheKey );
}
// Clear update cache
delete_transient( $this->getUpdateCacheKey( $entity ) );
// Force update check
delete_site_transient( 'update_plugins' );
}
/**
* Get authorization headers
* @return mixed|void
*/
protected function authHeaders() {
$headers = array(
'Authorization' => 'Basic ' . base64_encode( $this->consumerKey . ':' . $this->consumerSecret ),
);
return apply_filters( 'dlm_http_auth', $headers );
}
}