-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSoftwareLicenseManager.class.php
464 lines (420 loc) · 18.2 KB
/
SoftwareLicenseManager.class.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* Software License Manager.
*
* @since 1.1.1
* @package Software License Manager
* @subpackage WP Connector Class
* @author George Lewe <george@lewe.com>
* @source https://github.com/glewe/software-license-manager-class
* @license https://www.gnu.org/licenses/gpl-3.0.txt
*
* You can use this class in any PHP application to communicate with a WordPress
* based license server using the Software License Manager plugin:
* https://www.tipsandtricks-hq.com/software-license-manager-plugin-for-wordpress
*
*/
class SoftwareLicenseManager
{
/**
* The secret key.
*
* This variable specifies the value which is set in the license manager
* plugin settings page as: "Secret Key for License Verification Requests".
*
* @since 1.0.0
* @var SECRET_KEY The host REST URI
*/
const SECRET_KEY = '5421048138b321.90068894';
/**
* The license server URL.
*
* This variable specifies the URL of your server where the license manager
* plugin is installed on. Your plugin from a customer’s site will be
* communicating with this server to activate or deactivate license keys.
*
* @since 1.0.0
* @var LICENSE_SERVER The host REST URI
*/
const LICENSE_SERVER = 'https://mylicenseserver.com';
/**
* This variable provides a reference label for the licenses which will be
* issued. Therefore you should enter something specific to describe what
* the licenses issued are pertaining to.
*
* @since 1.0.0
* @var ITEM_REFERENCE The licensed product or item
*/
const ITEM_REFERENCE = 'My Licensed Item';
/**
* The license key.
* Private variable holding the linces key itself.
* Set with setKey(), read with getKey() method.
*
* @since 1.0.0
* @access private
* @var string $key The license key
*/
private $key = '';
/**
* Language array for thje details display.
*
* @since 1.0.0
* @access private
* @var array $lang The license key
*/
private $lang = array();
/**
* JSON reponse from the license server.
*
* @since 1.0.0
* @access public
* @var JSON $details JSON reposnse
*/
public $details;
// ---------------------------------------------------------------------
/**
* Constructor.
*/
public function __construct()
{
//
// Fill language array
//
$this->lang['lic_active'] = 'Active License';
$this->lang['lic_active_subject'] = 'This is an active license for this domain. Awesome!';
$this->lang['lic_active_unregistered_subject'] = 'This is an active license but not registered for this domain.';
$this->lang['lic_alert_activation_fail'] = 'The following error occurred while trying to activate your license:';
$this->lang['lic_alert_activation_success'] = 'Your license was successfully activated for this domain.';
$this->lang['lic_alert_registration_fail'] = 'The following error occurred while trying to register your domain to your license:';
$this->lang['lic_alert_registration_success'] = 'Your domain was successfully registered to your license.';
$this->lang['lic_alert_deregistration_fail'] = 'The following error occurred while trying to deregister your domain from your license:';
$this->lang['lic_alert_deregistration_success'] = 'Your domain was successfully deregistered from your license.';
$this->lang['lic_blocked'] = 'Blocked License';
$this->lang['lic_blocked_subject'] = 'This license is blocked.';
$this->lang['lic_blocked_help'] = 'Please contact your administrator to unblock this license.';
$this->lang['close_this_message'] = 'Close this message';
$this->lang['lic_company'] = 'Company';
$this->lang['lic_date_created'] = 'Date Created';
$this->lang['lic_date_expiry'] = 'Date Expiry';
$this->lang['lic_date_renewed'] = 'Date Renewed';
$this->lang['lic_daysleft'] = 'days left';
$this->lang['lic_details'] = 'License Details';
$this->lang['lic_email'] = 'E-mail';
$this->lang['lic_expired'] = 'Expired License';
$this->lang['lic_expired_subject'] = 'This license has expired.';
$this->lang['lic_expired_help'] = 'Please contact your administrator to renew this license.';
$this->lang['lic_expiringsoon'] = 'License Expiry Warning';
$this->lang['lic_expiringsoon_subject'] = 'Your license will expire in %d days.';
$this->lang['lic_expiringsoon_help'] = 'Please contact your administrator to renew this license in time.';
$this->lang['lic_invalid'] = 'Invalid License';
$this->lang['lic_invalid_subject'] = 'No license key was found or it is invalid.';
$this->lang['lic_invalid_text'] = 'This instance is unregistered or a ucfirst license key was not entered and activated yet.';
$this->lang['lic_invalid_help'] = 'Please contact the administrator to obtain a valid license.';
$this->lang['lic_key'] = 'License Key';
$this->lang['lic_name'] = 'Licensee';
$this->lang['lic_max_allowed_domains'] = 'Maximum Allowed Domains';
$this->lang['lic_pending'] = 'Pending License';
$this->lang['lic_pending_subject'] = 'This license is registered but not activated yet.';
$this->lang['lic_pending_help'] = 'Please contact your administrator to activate this license.';
$this->lang['lic_registered_domains'] = 'Registered Domains';
$this->lang['lic_status'] = 'Status';
$this->lang['lic_product'] = 'Product';
$this->lang['lic_unregistered'] = 'Unregistered License';
$this->lang['lic_unregistered_subject'] = 'The license key of this instance is not registered for this domain.';
$this->lang['lic_unregistered_help'] = 'Please contact the administrator to register this domain or obtain a valid license.';
}
// ---------------------------------------------------------------------------
/**
* Activates a license (and registers the domain the request is coming from).
*
* @return JSON
*/
function activate()
{
$parms = array(
'slm_action' => 'slm_activate',
'secret_key' => self::SECRET_KEY,
'license_key' => $this->key,
'registered_domain' => $_SERVER['SERVER_NAME'],
'item_reference' => urlencode(self::ITEM_REFERENCE),
);
$response = $this->callAPI('GET', self::LICENSE_SERVER, $parms);
$response = json_decode((string)$response);
if (!$response) {
$response = (object) array('result' => 'error', 'message' => 'Unexpected Error! The activation request returned with an error.');
}
return $response;
}
// ---------------------------------------------------------------------------
/**
* API Call.
*
* @param string $method POST, PUT, GET, ...
* @param string $url API host URL
* @param array $data URL paramater: array("param" => "value") ==> index.php?param=value
* @return JSON
*/
function callAPI($method, $url, $data = false)
{
$curl = curl_init();
switch (strtoupper($method)) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($curl, CURLOPT_USERPWD, "username:password");
// Optional Debugging:
// $query = LICENSE_SERVER . '?slm_action=' . $data['slm_action'] . '&secret_key=' . $data['secret_key'] . '&license_key=' . $data['license_key'] . '&registered_domain=' . $data['registered_domain'] . '&item_reference=' . $data['item_reference'];
// die($query);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// ---------------------------------------------------------------------------
/**
* Deactivates a license (deregisters the domain the request is coming from).
*
* @return JSON
*/
function deactivate()
{
$parms = array(
'slm_action' => 'slm_deactivate',
'secret_key' => self::SECRET_KEY,
'license_key' => $this->key,
'registered_domain' => $_SERVER['SERVER_NAME'],
'item_reference' => urlencode(self::ITEM_REFERENCE),
);
$response = $this->callAPI('GET', self::LICENSE_SERVER, $parms);
$response = json_decode((string)$response);
if (!$response) {
$response = (object) array('result' => 'error', 'message' => 'Unexpected Error! The deactivation request returned with an error.');
}
return $response;
}
// ---------------------------------------------------------------------------
/**
* Checks whether the current domain is registered.
*
* @return boolean
*/
function domainRegistered()
{
// if (!$this->readKey()) return false; // Enable if using the readKey() method
if (count($this->details->registered_domains)) {
foreach ($this->details->registered_domains as $domain) {
if ($domain->registered_domain == $_SERVER['SERVER_NAME']) return true;
}
return false;
} else {
return false;
}
}
// ---------------------------------------------------------------------------
/**
* Returns the days until expiry.
*
* @return integer
*/
function daysToExpiry()
{
$todayDate = new DateTime('now');
$expiryDate = new DateTime($this->details->date_expiry);
$daysToExpiry = $todayDate->diff($expiryDate);
return intval($daysToExpiry->format('%R%a'));
}
// ---------------------------------------------------------------------------
/**
* Loads the license information from license server.
*
* @return JSON Saved in $this->details
*/
function load()
{
$parms = array(
'slm_action' => 'slm_check',
'secret_key' => self::SECRET_KEY,
'license_key' => $this->key,
);
$response = $this->callAPI('GET', self::LICENSE_SERVER, $parms);
$response = json_decode((string)$response);
$this->details = $response;
}
// ---------------------------------------------------------------------------
/**
* Reads the class license key.
*
* @return string
*/
function getKey()
{
return $this->key;
}
// ---------------------------------------------------------------------
/**
* Reads the license key from the database.
*/
function readKey()
{
//
// You may want to use this method to read the license key from elsewhere
// e.g. from a database with this pseudo code
// $this->key = read_key_from_db();
//
}
// ---------------------------------------------------------------------
/**
* Saves the license key to the database.
*/
function saveKey($value)
{
//
// You may want to use this method to save the license key elsewhere
// e.g. to a database with this pseudo code
// save_key_to_db($this->key);
//
}
// ---------------------------------------------------------------------------
/**
* Sets the class license key.
*
* @param string $key The license key
*/
function setKey($key)
{
$this->key = $key;
}
// ---------------------------------------------------------------------------
/**
* Creates a table with license details and displays it inside a Bootstrap
* alert box. This method assumes that your application uses Bootstrap 4.
*
* @param object $data License information array
* @return string HTML
*/
function show($data, $showDetails = false)
{
if (isset($data->result) && $data->result == "error") {
$alert['type'] = 'danger';
$alert['title'] = $this->lang['lic_invalid'];
$alert['subject'] = $this->lang['lic_invalid_subject'];
$alert['text'] = $this->lang['lic_invalid_text'];
$alert['help'] = $this->lang['lic_invalid_help'];
$details = "";
} else
{
$domains = "";
if (count($data->registered_domains)) {
foreach ($data->registered_domains as $domain) {
$domains .= $domain->registered_domain . ', ';
}
$domains = substr($domains, 0, -2); // Remove last comma and blank
}
$daysleft = "";
if ($daysToExpiry = $this->daysToExpiry()) {
$daysleft = " (" . $daysToExpiry . " " . $this->lang['lic_daysleft'] . ")";
}
$details = "<div style=\"height:20px;\"></div>";
$details .= "<table class=\"table table-hover\">
<tr><th>" . $this->lang['lic_product'] . ":</th><td>" . $data->product_ref . "</td></tr>
<tr><th>" . $this->lang['lic_key'] . ":</th><td>" . $data->license_key . "</td></tr>
<tr><th>" . $this->lang['lic_name'] . ":</th><td>" . $data->first_name . " " . $data->last_name . "</td></tr>
<tr><th>" . $this->lang['lic_email'] . ":</th><td>" . $data->email . "</td></tr>
<tr><th>" . $this->lang['lic_company'] . ":</th><td>" . $data->company_name . "</td></tr>
<tr><th>" . $this->lang['lic_date_created'] . ":</th><td>" . $data->date_created . "</td></tr>
<tr><th>" . $this->lang['lic_date_renewed'] . ":</th><td>" . $data->date_renewed . "</td></tr>
<tr><th>" . $this->lang['lic_date_expiry'] . ":</th><td>" . $data->date_expiry . $daysleft . "</td></tr>
<tr><th>" . $this->lang['lic_registered_domains'] . ":</th><td>" . $domains . "</td></tr>
</table>";
switch ($this->status()) {
case "active":
$title = $this->lang['lic_active'];
$alert['type'] = 'success';
$alert['title'] = $title . '<span class="btn btn-' . $alert['type'] . ' btn-sm" style="margin-left:16px;">' . ucfirst($data->status) . '</span>';
$alert['subject'] = $this->lang['lic_active_subject'];
$alert['text'] = '';
$alert['help'] = '';
break;
case "expired":
$title = $this->lang['lic_expired'];
$alert['type'] = 'warning';
$alert['title'] = $title . '<span class="btn btn-' . $alert['type'] . ' btn-sm" style="margin-left:16px;">' . ucfirst($data->status) . '</span>';
$alert['subject'] = $this->lang['lic_expired_subject'];
$alert['help'] = $this->lang['lic_expired_help'];
break;
case "blocked":
$alert['type'] = 'warning';
$title = $this->lang['lic_blocked'];
$alert['title'] = $title . '<span class="btn btn-' . $alert['type'] . ' btn-sm" style="margin-left:16px;">' . ucfirst($data->status) . '</span>';
$alert['subject'] = $this->lang['lic_blocked_subject'];
$alert['text'] = '';
$alert['help'] = $this->lang['lic_blocked_help'];
break;
case "pending":
$alert['type'] = 'warning';
$title = $this->lang['lic_pending'];
$alert['title'] = $title . '<span class="btn btn-' . $alert['type'] . ' btn-sm" style="margin-left:16px;">' . ucfirst($data->status) . '</span>';
$alert['subject'] = $this->lang['lic_pending_subject'];
$alert['text'] = '';
$alert['help'] = $this->lang['lic_pending_help'];
break;
case "unregistered":
$title = $this->lang['lic_active'];
$alert['type'] = 'warning';
$alert['title'] = $title . '<span class="btn btn-' . $alert['type'] . ' btn-sm" style="margin-left:16px;">' . ucfirst($data->status) . '</span>';
$alert['subject'] = $this->lang['lic_active_unregistered_subject'];
$alert['text'] = '';
$alert['help'] = '';
break;
}
}
$alertBox = '
<div class="alert alert-dismissable alert-' . $alert['type'] . '">
<button type="button" class="close" data-dismiss="alert" title="' . $this->lang['close_this_message'] . '"><i class="far fa-times-circle"></i></button>
<h4><strong>' . $alert['title'] . '</strong></h4>
<hr>
<p><strong>' . $alert['subject'] . '</strong></p>
<p>' . $alert['text'] . '</p>
' . (strlen($alert['help']) ? "<p><i>" . $alert['help'] . "</i></p>" : "") . (($showDetails) ? $details : '') . '
</div>';
return $alertBox;
}
// ---------------------------------------------------------------------------
/**
* Get license status.
*
* @return string active/blocked/invalid/expired/pending/unregistered
*/
function status()
{
if ($this->details->result == 'error') return "invalid";
switch ($this->details->status) {
case "active":
if (!$this->domainRegistered()) return 'unregistered';
return 'active';
break;
case "expired":
return 'expired';
break;
case "blocked":
return 'blocked';
break;
case "pending":
return 'pending';
break;
}
}
}