PluginProbe
WP Reset / 2.05
WP Reset v2.05
trunk 1.0 1.1 1.11 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.77 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.90 All 42 releases
← All changes | wp-reset-licensing.php +112 -101 1.832.05 View file →
@@ -13,8 +13,9 @@
13 13 private $licensing_servers = array();
14 14 private $version = '';
15 15 private $slug = '';
16 16 private $basename = '';
17 + private $plugin_page = '';
17 18 private $plugin_file = '';
18 19 private $js_folder = '';
19 20 protected $api_ver = 'v1/';
20 21 protected $valid_forever = '2035-01-01';
@@ -19,8 +20,9 @@
19 20 protected $api_ver = 'v1/';
20 21 protected $valid_forever = '2035-01-01';
21 22 protected $unlimited_installs = 99999;
22 23 public $debug = false;
24 + public $filesystem_initialized = false;
23 25
24 26
25 27 /**
26 28 * Init licensing by setting up various params and hooking into actions.
@@ -36,8 +38,9 @@
36 38 $this->version = trim($params['version']);
37 39 $this->slug = dirname(plugin_basename(trim($params['plugin_file'])));
38 40 $this->basename = plugin_basename(trim($params['plugin_file']));
39 41 $this->plugin_file = $params['plugin_file'];
42 + $this->plugin_page = trim($params['plugin_page']);
40 43 $this->debug = !empty($params['debug']);
41 44
42 45 if ($params['js_folder']) {
43 46 $this->js_folder = trim($params['js_folder']);
@@ -45,11 +48,8 @@
45 48 $this->js_folder = plugin_dir_url($this->plugin_file) . 'js/';
46 49 }
47 50
48 51 if (empty($params['skip_hooks'])) {
49 - register_activation_hook($this->plugin_file, array($this, 'activate_plugin'));
50 - register_deactivation_hook($this->plugin_file, array($this, 'deactivate_plugin'));
51 -
52 52 add_action('init', array($this, 'init'));
53 53
54 54 add_action('wp_ajax_wf_licensing_' . $this->prefix . '_validate', array($this, 'validate_ajax'));
55 55 add_action('wp_ajax_wf_licensing_' . $this->prefix . '_save', array($this, 'save_ajax'));
@@ -67,31 +67,42 @@
67 67 */
68 68 function init()
69 69 {
70 70 if (is_admin()) {
71 - $vars = array(
72 - 'prefix' => $this->prefix,
73 - 'debug' => $this->debug,
74 - 'nonce' => wp_create_nonce('wf_licensing_' . $this->prefix),
75 - 'licensing_endpoint' => $this->licensing_servers[0] . $this->api_ver,
76 - 'request_data' => array(
77 - 'action' => 'validate_license',
78 - 'license_key' => '',
79 - 'rand' => rand(1000, 9999),
80 - 'version' => $this->version,
81 - 'wp_version' => get_bloginfo('version'),
82 - 'site_url' => get_home_url(),
83 - 'site_title' => get_bloginfo('name'),
84 - 'meta' => array()
85 - )
86 - );
71 + add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
72 + }
73 + } // init
87 74
88 - wp_enqueue_script('wf_licensing', $this->js_folder . 'wf-licensing.js', array(), 1.0, true);
89 - wp_localize_script('wf_licensing', 'wf_licensing_' . $this->prefix, $vars);
75 +
76 + function admin_enqueue_scripts() {
77 + $current_screen = get_current_screen();
78 +
79 + if (empty($current_screen->id) || $current_screen->id != $this->plugin_page) {
80 + return false;
90 81 }
91 - } // init
92 82
83 + $vars = array(
84 + 'prefix' => $this->prefix,
85 + 'debug' => $this->debug,
86 + 'nonce' => wp_create_nonce('wf_licensing_' . $this->prefix),
87 + 'licensing_endpoint' => $this->licensing_servers[0] . $this->api_ver,
88 + 'request_data' => array(
89 + 'action' => 'validate_license',
90 + 'license_key' => '',
91 + 'rand' => wp_rand(1000, 9999),
92 + 'version' => $this->version,
93 + 'wp_version' => get_bloginfo('version'),
94 + 'site_url' => get_home_url(),
95 + 'site_title' => get_bloginfo('name'),
96 + 'meta' => array()
97 + )
98 + );
93 99
100 + wp_enqueue_script('wf_licensing', $this->js_folder . 'wf-licensing.js', array(), 1.0, true);
101 + wp_localize_script('wf_licensing', 'wf_licensing_' . $this->prefix, $vars);
102 + } // admin_enqueue_scripts
103 +
104 +
94 105 /**
95 106 * Log message if debugging is enabled.
96 107 * Log file: /wp-content/wf-licensing.log
97 108 *
@@ -101,23 +112,33 @@
101 112 * @return void
102 113 */
103 114 function log($message, ...$data)
104 115 {
116 + global $wp_filesystem;
117 +
105 118 if (!$this->debug) {
106 119 return;
107 120 }
108 121
122 + global $wp_filesystem;
123 + $this->wp_init_filesystem();
124 +
109 125 $log_file = trailingslashit(WP_CONTENT_DIR) . 'wf-licensing.log';
110 - $fp = fopen($log_file, 'a+');
126 +
127 + if ( ! $wp_filesystem->exists( $log_file ) ) {
128 + $wp_filesystem->put_contents( $log_file, '' ); // Create an empty file if it doesn't exist.
129 + }
111 130
112 - fputs($fp, '[' . date('r') . '] ' . $this->prefix . ': ');
113 - fputs($fp, (string) $message . PHP_EOL);
131 + $log_content = $wp_filesystem->get_contents( $log_file ); // wp_filesystem can't append so we have to read existing content
132 +
133 + $log_content .= '[' . gmdate('r') . '] ' . $this->prefix . ': ';
134 + $log_content .= (string) $message . PHP_EOL;
114 135 foreach ($data as $tmp) {
115 - fputs($fp, var_export($tmp, true) . PHP_EOL);
136 + //only used for logging, no visible output
137 + $log_content .= var_export($tmp, true) . PHP_EOL; //phpcs:ignore
116 138 }
117 -
118 - fputs($fp, PHP_EOL);
119 - fclose($fp);
139 + $log_content .= PHP_EOL;
140 + $wp_filesystem->put_contents( $log_file, $log_content );
120 141 } // log
121 142
122 143
123 144 /**
@@ -191,16 +212,16 @@
191 212 if ($license['valid_until'] == $this->valid_forever) {
192 213 $out['valid_until'] = 'forever';
193 214 $out['recurring'] = false;
194 215 } else {
195 - $out['valid_until'] = 'until ' . date(get_option('date_format'), strtotime($license['valid_until']));
216 + $out['valid_until'] = 'until ' . gmdate(get_option('date_format'), strtotime($license['valid_until']));
196 217 $out['recurring'] = true;
197 218 }
198 219
199 - if (date('Y-m-d') == $license['valid_until']) {
220 + if (gmdate('Y-m-d') == $license['valid_until']) {
200 221 $out['expires'] = 'today';
201 - } elseif (date('Y-m-d', time() + 30 * DAY_IN_SECONDS) > $license['valid_until']) {
202 - $tmp = (strtotime($license['valid_until'] . date(' G:i:s')) - time()) / DAY_IN_SECONDS;
222 + } elseif (gmdate('Y-m-d', time() + 30 * DAY_IN_SECONDS) > $license['valid_until']) {
223 + $tmp = (strtotime($license['valid_until'] . gmdate(' G:i:s')) - time()) / DAY_IN_SECONDS;
203 224 $out['expires'] = 'in ' . round($tmp) . ' days';
204 225 } else {
205 226 $out['expires'] = 'in more than 30 days';
206 227 }
@@ -274,9 +295,9 @@
274 295 $license = $this->get_license();
275 296
276 297 if (
277 298 !empty($license['license_key']) && !empty($license['name']) &&
278 - !empty($license['valid_until']) && $license['valid_until'] >= date('Y-m-d')
299 + !empty($license['valid_until']) && $license['valid_until'] >= gmdate('Y-m-d')
279 300 ) {
280 301 if (!empty($feature)) {
281 302 if (!empty($license['meta'][$feature]) && filter_var($license['meta'][$feature], FILTER_VALIDATE_BOOLEAN) == true) {
282 303 return true;
@@ -292,65 +313,8 @@
292 313 } // is_active
293 314
294 315
295 316 /**
296 - * Hook to plugin activation action.
297 - * If there's a license key, try to activate & write response.
298 - *
299 - * @return void
300 - */
301 - function activate_plugin()
302 - {
303 - $license = $this->get_license();
304 - if ($this->is_active() || empty($license['license_key'])) {
305 - return false;
306 - }
307 -
308 - $tmp = $this->validate();
309 - if ($tmp) {
310 - $this->log('activating plugin, license activated');
311 - return true;
312 - } else {
313 - $this->log('activating plugin, unable to activate license');
314 - return false;
315 - }
316 - } // activate_plugin
317 -
318 -
319 - /**
320 - * Hook to plugin deactivation action.
321 - * If there's a license key, try to deactivate & write response.
322 - *
323 - * @return void
324 - */
325 - function deactivate_plugin()
326 - {
327 - if (!$this->is_active()) {
328 - return false;
329 - }
330 -
331 - $license = $this->get_license();
332 - $result = $this->query_licensing_server('deactivate_license');
333 -
334 - if (is_wp_error($result) || !is_array($result) || !isset($result['success']) || $result['success'] == false) {
335 - $this->log('unable to deactivate license');
336 -
337 - return false;
338 - } else {
339 - $license['error'] = '';
340 - $license['name'] = '';
341 - $license['valid_until'] = '';
342 - $license['meta'] = '';
343 - $license['last_check'] = 0;
344 - $this->update_license($license);
345 - $this->log('license deactivated');
346 -
347 - return true;
348 - }
349 - } // deactivate_plugin
350 -
351 -
352 - /**
353 317 * Use when uninstalling (deleting) the plugin to clean up.
354 318 *
355 319 * @param string $prefix Same prefix as used when initialising the class.
356 320 * @return bool
@@ -431,10 +395,19 @@
431 395
432 396 function validate_ajax()
433 397 {
434 398 check_ajax_referer('wf_licensing_' . $this->prefix);
399 +
400 + if (false === current_user_can('manage_options')) {
401 + wp_die('Sorry, you have to be an admin to run this action.');
402 + }
435 403
436 - $license_key = trim($_REQUEST['license_key']);
404 + if(!isset($_REQUEST['license_key'])){
405 + wp_send_json_error('Missing license key');
406 + }
407 +
408 + $license_key = sanitize_text_field(wp_unslash($_REQUEST['license_key']));
409 + $license_key = trim(substr($license_key, 0, 64));
437 410 if (empty($license_key)) {
438 411 $this->update_license(false);
439 412 do_action('wf_licensing_' . $this->prefix . '_validate_ajax', $license_key, false);
440 413
@@ -444,9 +417,8 @@
444 417 $license = $this->get_license();
445 418 do_action('wf_licensing_' . $this->prefix . '_validate_ajax', $license_key, $result);
446 419
447 420 if ($result == true) {
448 - set_site_transient('update_plugins', null);
449 421 wp_send_json_success($result);
450 422 } else {
451 423 wp_send_json_error($license);
452 424 }
@@ -457,8 +429,12 @@
457 429 function deactivate_ajax()
458 430 {
459 431 check_ajax_referer('wf_licensing_' . $this->prefix);
460 432
433 + if (false === current_user_can('manage_options')) {
434 + wp_die('Sorry, you have to be an admin to run this action.');
435 + }
436 +
461 437 $old_license = $this->get_license();
462 438 $result = $this->deactivate();
463 439 do_action('wf_licensing_' . $this->prefix . '_deactivate_ajax', $old_license, $result);
464 440 wp_send_json_success($result);
@@ -468,17 +444,33 @@
468 444 function save_ajax()
469 445 {
470 446 check_ajax_referer('wf_licensing_' . $this->prefix);
471 447
472 - $out['license_key'] = trim($_POST['license_key']);
448 + if (false === current_user_can('manage_options')) {
449 + wp_die('Sorry, you have to be an admin to run this action.');
450 + }
473 451
474 - if ($_POST['success'] == 'true') {
475 - $out['error'] = trim($_POST['data']['error']);
476 - $out['name'] = trim($_POST['data']['name']);
477 - $out['valid_until'] = trim($_POST['data']['valid_until']);
478 - $out['meta'] = $_POST['data']['meta'];
452 + if(!isset($_POST['license_key'])){
453 + wp_send_json_error('Missing license key');
454 + }
455 +
456 + $license_key = sanitize_text_field(wp_unslash($_POST['license_key']));
457 + $license_key = trim(substr($license_key, 0, 64));
458 + $out['license_key'] = $license_key;
459 +
460 + if(isset($_POST['data'])){
461 + $data = array_map('sanitize_text_field', wp_unslash($_POST['data']));
479 462 } else {
480 - $out['error'] = trim($_POST['data']);
463 + $data = array();
464 + }
465 +
466 + if (isset($_POST['success']) && sanitize_text_field(wp_unslash($_POST['success'])) == 'true') {
467 + $out['error'] = sanitize_text_field($data['error']);
468 + $out['name'] = sanitize_text_field($data['name']);
469 + $out['valid_until'] = sanitize_text_field($data['valid_until']);
470 + $out['meta'] = sanitize_text_field($data['meta']);
471 + } else {
472 + $out['error'] = sanitize_text_field($data);
481 473 $out['name'] = '';
482 474 $out['valid_until'] = '';
483 475 $out['meta'] = array();
484 476 }
@@ -506,9 +498,9 @@
506 498 $request_params = array('sslverify' => false, 'timeout' => 25, 'redirection' => 2);
507 499 $default_data = array(
508 500 'action' => '',
509 501 'license_key' => $license['license_key'],
510 - 'rand' => rand(1000, 9999),
502 + 'rand' => wp_rand(1000, 9999),
511 503 'version' => $this->version,
512 504 'wp_version' => get_bloginfo('version'),
513 505 'site_url' => get_home_url(),
514 506 'site_title' => get_bloginfo('name'),
@@ -539,6 +531,25 @@
539 531 } else {
540 532 return $result;
541 533 }
542 534 } // query_licensing_server
535 +
536 + /**
537 + * Initializes the WordPress filesystem.
538 + *
539 + * @return bool
540 + */
541 + function wp_init_filesystem()
542 + {
543 + if (! $this->filesystem_initialized) {
544 + if (! class_exists('WP_Filesystem')) {
545 + require_once ABSPATH . 'wp-admin/includes/file.php';
546 + }
547 +
548 + WP_Filesystem();
549 + $this->filesystem_initialized = true;
550 + }
551 +
552 + return true;
553 + }
543 554 } // WF_Licensing
544 555 } // if WF_Licensing