/** * Check if we need to download a file and check validity */ public function download_api_product() { global $wpdb; if (isset($_GET['download_api_product']) && isset($_GET['licence_key'])) { $download_api_product = absint($_GET['download_api_product']); $licence_key = sanitize_text_field($_GET['licence_key']); $activation_email = sanitize_text_field($_GET['activation_email']); $licence = wppl_get_licence_from_key($licence_key); // Validation if (!$licence) { wp_die(__('Invalid or expired licence key.', 'wp-plugin-licencing')); } if (is_user_logged_in() && $licence->user_id && $licence->user_id != get_current_user_id()) { wp_die(__('This licence does not appear to be yours.', 'wp-plugin-licencing')); } if (!is_email($activation_email) || $activation_email != $licence->activation_email) { wp_die(__('Invalid activation email address.', 'wp-plugin-licencing')); } if (!in_array($download_api_product, wppl_get_licence_api_product_permissions($licence->product_id))) { wp_die(__('This licence does not allow access to the requested product.', 'wp-plugin-licencing')); } // Get the download URL $file_path = wppl_get_package_file_path($download_api_product); // Log this download $wpdb->insert($wpdb->prefix . 'wp_plugin_licencing_download_log', array('licence_key' => $licence_key, 'activation_email' => $activation_email, 'api_product_id' => $download_api_product, 'date_downloaded' => current_time('mysql'), 'user_ip_address' => sanitize_text_field(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']))); // Download it! $this->download($file_path); } }
/** * Activate a licence key */ public function activate() { global $wpdb; $this->check_required(array('email', 'licence_key', 'api_product_id', 'instance')); $licence = wppl_get_licence_from_key($this->request['licence_key']); $api_product_post_id = wppl_get_api_product_post_id($this->request['api_product_id']); if (!$licence) { $this->trigger_error('101', __('Activation error: The provided licence is invalid or has expired.', 'wp-plugin-licencing')); } if (!$api_product_post_id) { $this->trigger_error('102', __('Activation error: Invalid API Product ID.', 'wp-plugin-licencing')); } if (!is_email($this->request['email']) || strtolower($this->request['email']) != strtolower($licence->activation_email)) { $this->trigger_error('103', sprintf(__('Activation error: The email provided (%s) is invalid.', 'wp-plugin-licencing'), $this->request['email'])); } if (!in_array($api_product_post_id, wppl_get_licence_api_product_permissions($licence->product_id))) { $this->trigger_error('104', __('Activation error: Licence is not for this product.', 'wp-plugin-licencing')); } $active_instances = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id'], 1); // Check if activation limit is reached if ($licence->activation_limit && sizeof($active_instances) >= $licence->activation_limit) { // lets allow reactvation for guests, but registered users need to de-activate first if (!$licence->user_id) { foreach ($active_instances as $activation) { if ($activation->instance == $this->request['instance']) { // Reactivate the key $activation_result = $wpdb->update("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql')), array('instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key'])); if (!$activation_result) { $this->trigger_error('106', __('Activation error: Could not reactivate licence key.', 'wp-plugin-licencing')); } else { $response = array('activated' => true); $activations_remaining = absint($licence->activation_limit - sizeof($active_instances)); $response['remaining'] = sprintf(__('%s out of %s activations remaining', 'wp-plugin-licencing'), $activations_remaining, $licence->activation_limit); $this->send_response($response); } } } } $this->trigger_error('105', __('Activation error: Licence key activation limit reached. Deactivate an install first.', 'wp-plugin-licencing')); } $instance_exists = false; $instances = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id']); // Check for reactivation if ($instances) { foreach ($instances as $activation) { if ($activation->instance == $this->request['instance']) { $instance_exists = true; } } } if ($instance_exists) { $activation_result = $wpdb->update("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql')), array('instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key'])); } else { $activation_result = $wpdb->insert("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql'), 'instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key'])); } if (!$activation_result) { $this->trigger_error('107', __('Activation error: Could not activate licence key.', 'wp-plugin-licencing')); } $activations = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id']); $response = array('activated' => true); if ($licence->activation_limit) { $activations_remaining = absint($licence->activation_limit - sizeof($activations)); $response['remaining'] = sprintf(__('%s out of %s activations remaining', 'wp-plugin-licencing'), $activations_remaining, $licence->activation_limit); } $this->send_response($response); }
<?php /** * New licence email */ if (!defined('ABSPATH')) { exit; } // Exit if accessed directly if ($user_first_name) { echo sprintf(__("Hello %s,", 'wp-plugin-licencing'), $user_first_name) . "\n\n"; } else { echo __("Hi there,", 'wp-plugin-licencing') . "\n\n"; } _e("A licence key has just been generated for you. The details are as follows:", 'wp-plugin-licencing'); echo "\n"; if ($api_product_permissions = wppl_get_licence_api_product_permissions($key->product_id)) { foreach ($api_product_permissions as $api_product_permission) { echo "\n====================\n"; echo esc_html(get_the_title($api_product_permission)) . ': ' . wppl_get_package_download_url($api_product_permission, $key->licence_key, $key->activation_email) . "\n"; echo $key->licence_key . ""; echo "\n====================\n\n"; } } _e("You can input this licence on the plugins page within your WordPress dashboard.", 'wp-plugin-licencing'); echo "\n"; echo "\n"; // Footer echo '--' . "\n"; echo apply_filters('woocommerce_email_footer_text', get_option('woocommerce_email_footer_text'));
/** * Check access to plugin update API */ public function check_access() { // Check data if (empty($this->request['licence_key'])) { $this->trigger_error('no_key', 'no_key'); } if (empty($this->request['email']) || empty($this->request['api_product_id']) || empty($this->request['instance']) || empty($this->request['version']) || empty($this->request['plugin_name'])) { $this->trigger_error('invalid_request', 'invalid_request'); } // Check licence $licence = wppl_get_licence_from_key($this->request['licence_key']); $api_product_post_id = wppl_get_api_product_post_id($this->request['api_product_id']); if (!$api_product_post_id) { $this->trigger_error('invalid_request', 'invalid_request'); } if (!$licence || !is_email($this->request['email']) || strtolower($this->request['email']) != strtolower($licence->activation_email) || !in_array($api_product_post_id, wppl_get_licence_api_product_permissions($licence->product_id))) { $this->trigger_error('invalid_key', sprintf(__('The licence for <code>%s</code> is invalid or has expired. To continue to receive support and updates you must obtain an updated licence key. If you have an account, expired keys can be renewed via your <a href="%s" target="_blank">account dashboard</a>.', 'wp-plugin-licencing'), $this->request['api_product_id'], get_permalink(wc_get_page_id('myaccount')))); } if (!wppl_is_licence_activated($this->request['licence_key'], $this->request['api_product_id'], $this->request['instance'])) { $this->trigger_error('no_activation', sprintf(__('The licence is no longer activated on this site. Reactivate the licence to receive updates for <code>%s</code>.', 'wp-plugin-licencing'), $this->request['api_product_id'])); } }