diff --git a/software-license-manager/includes/slm-api-listener.php b/software-license-manager/includes/slm-api-listener.php index 065e5f3..0cb20c5 100644 --- a/software-license-manager/includes/slm-api-listener.php +++ b/software-license-manager/includes/slm-api-listener.php @@ -33,9 +33,9 @@ function creation_api_listener() { SLM_API_Utility::verify_secret_key_for_creation(); //Verify the secret key first. $slm_debug_logger->log_debug("API - license creation (slm_create_new) request received."); - + //Action hook - do_action('slm_api_listener_slm_create_new'); + do_action('slm_api_listener_slm_create_new'); $fields = array(); if (isset($_REQUEST['license_key']) && !empty($_REQUEST['license_key'])){ @@ -49,11 +49,17 @@ function creation_api_listener() { $fields['email'] = strip_tags($_REQUEST['email']); $fields['company_name'] = wp_unslash(strip_tags($_REQUEST['company_name'])); $fields['txn_id'] = strip_tags($_REQUEST['txn_id']); + if (empty($_REQUEST['max_allowed_domains'])) { $fields['max_allowed_domains'] = $options['default_max_domains']; } else { $fields['max_allowed_domains'] = strip_tags($_REQUEST['max_allowed_domains']); } + if (empty($_REQUEST['max_allowed_devices'])) { + $fields['max_allowed_devices'] = $options['default_max_devices']; + } else { + $fields['max_allowed_devices'] = strip_tags($_REQUEST['max_allowed_devices']); + } $fields['date_created'] = isset($_REQUEST['date_created'])?strip_tags($_REQUEST['date_created']):date("Y-m-d"); $fields['date_expiry'] = isset($_REQUEST['date_expiry'])?strip_tags($_REQUEST['date_expiry']):''; @@ -87,9 +93,9 @@ function activation_api_listener() { SLM_API_Utility::verify_secret_key(); //Verify the secret key first. $slm_debug_logger->log_debug("API - license activation (slm_activate) request received."); - + //Action hook - do_action('slm_api_listener_slm_activate'); + do_action('slm_api_listener_slm_activate'); $fields = array(); $fields['lic_key'] = trim(strip_tags($_REQUEST['license_key'])); @@ -106,6 +112,7 @@ function activation_api_listener() { $sql_prep2 = $wpdb->prepare("SELECT * FROM $reg_table WHERE lic_key = %s", $key); $reg_domains = $wpdb->get_results($sql_prep2, OBJECT); + if ($retLic) { if ($retLic->lic_status == 'blocked') { $args = (array('result' => 'error', 'message' => 'Your License key is blocked', 'error_code' => SLM_Error_Codes::LICENSE_BLOCKED)); @@ -127,20 +134,52 @@ function activation_api_listener() { SLM_API_Utility::output_api_response($args); } } + $fields['lic_key_id'] = $retLic->id; $wpdb->insert($reg_table, $fields); - + $slm_debug_logger->log_debug("Updating license key status to active."); $data = array('lic_status' => 'active'); $where = array('id' => $retLic->id); $updated = $wpdb->update($tbl_name, $data, $where); - + $args = (array('result' => 'success', 'message' => 'License key activated')); SLM_API_Utility::output_api_response($args); - } else { + } + else { $args = (array('result' => 'error', 'message' => 'Reached maximum allowable domains', 'error_code' => SLM_Error_Codes::REACHED_MAX_DOMAINS)); SLM_API_Utility::output_api_response($args); } + + if (count($reg_domains) < floor($retLic->max_allowed_domains)) { + foreach ($reg_domains as $reg_domain) { + if (isset($_REQUEST['migrate_from']) && (trim($_REQUEST['migrate_from']) == $reg_domain->registered_domain)) { + $wpdb->update($reg_table, array('registered_domain' => $fields['registered_domain']), array('registered_domain' => trim(strip_tags($_REQUEST['migrate_from'])))); + $args = (array('result' => 'success', 'message' => 'Registered domain has been updated')); + SLM_API_Utility::output_api_response($args); + } + if ($fields['registered_domain'] == $reg_domain->registered_domain) { + $args = (array('result' => 'error', 'message' => 'License key already in use on ' . $reg_domain->registered_domain, 'error_code' => SLM_Error_Codes::LICENSE_IN_USE)); + SLM_API_Utility::output_api_response($args); + } + } + + $fields['lic_key_id'] = $retLic->id; + $wpdb->insert($reg_table, $fields); + + $slm_debug_logger->log_debug("Updating license key status to active."); + $data = array('lic_status' => 'active'); + $where = array('id' => $retLic->id); + $updated = $wpdb->update($tbl_name, $data, $where); + + $args = (array('result' => 'success', 'message' => 'License key activated')); + SLM_API_Utility::output_api_response($args); + } + else { + $args = (array('result' => 'error', 'message' => 'Reached maximum allowable devices', 'error_code' => SLM_Error_Codes::REACHED_MAX_DOMAINS)); + SLM_API_Utility::output_api_response($args); + } + } else { $args = (array('result' => 'error', 'message' => 'Invalid license key', 'error_code' => SLM_Error_Codes::LICENSE_INVALID)); SLM_API_Utility::output_api_response($args); @@ -156,9 +195,9 @@ function deactivation_api_listener() { SLM_API_Utility::verify_secret_key(); //Verify the secret key first. $slm_debug_logger->log_debug("API - license deactivation (slm_deactivate) request received."); - + //Action hook - do_action('slm_api_listener_slm_deactivate'); + do_action('slm_api_listener_slm_deactivate'); if (empty($_REQUEST['registered_domain'])) { $args = (array('result' => 'error', 'message' => 'Registered domain information is missing', 'error_code' => SLM_Error_Codes::DOMAIN_MISSING)); @@ -192,14 +231,14 @@ function check_api_listener() { SLM_API_Utility::verify_secret_key(); //Verify the secret key first. $slm_debug_logger->log_debug("API - license check (slm_check) request received."); - + $fields = array(); $fields['lic_key'] = trim(strip_tags($_REQUEST['license_key'])); $slm_debug_logger->log_debug("License key: " . $fields['lic_key']); //Action hook do_action('slm_api_listener_slm_check'); - + global $wpdb; $tbl_name = SLM_TBL_LICENSE_KEYS; $reg_table = SLM_TBL_LIC_DOMAIN; @@ -211,12 +250,18 @@ function check_api_listener() { $reg_domains = $wpdb->get_results($sql_prep2, OBJECT); if ($retLic) {//A license key exists $args = (array( - 'result' => 'success', - 'message' => 'License key details retrieved.', - 'status' => $retLic->lic_status, + 'result' => 'success', + 'code' => '200', + 'message' => 'License key details retrieved.', + 'status' => $retLic->lic_status, 'max_allowed_domains' => $retLic->max_allowed_domains, + 'max_allowed_devices' => $retLic->max_allowed_devices, 'email' => $retLic->email, + 'first_name' => $retLic->first_name, + 'last_name' => $retLic->last_name, + // 'until' => $retLic->until, //until what version license is supported 'registered_domains' => $reg_domains, + 'license_key' => $retLic->license_key, 'date_created' => $retLic->date_created, 'date_renewed' => $retLic->date_renewed, 'date_expiry' => $retLic->date_expiry, @@ -226,7 +271,7 @@ function check_api_listener() { } else { $args = (array('result' => 'error', 'message' => 'Invalid license key', 'error_code' => SLM_Error_Codes::LICENSE_INVALID)); SLM_API_Utility::output_api_response($args); - } + } } } diff --git a/software-license-manager/includes/slm-api-utility.php b/software-license-manager/includes/slm-api-utility.php index 61b3e2f..8fd3222 100644 --- a/software-license-manager/includes/slm-api-utility.php +++ b/software-license-manager/includes/slm-api-utility.php @@ -26,7 +26,7 @@ static function verify_secret_key() { SLM_API_Utility::output_api_response($args); } } - + static function verify_secret_key_for_creation() { $slm_options = get_option('slm_plugin_options'); $right_secret_key = $slm_options['lic_creation_secret']; @@ -35,7 +35,7 @@ static function verify_secret_key_for_creation() { $args = (array('result' => 'error', 'message' => 'License Creation API secret key is invalid', 'error_code' => SLM_Error_Codes::CREATE_KEY_INVALID)); SLM_API_Utility::output_api_response($args); } - } + } static function insert_license_data_internal($fields) { /* The fields array should have values for the following keys diff --git a/software-license-manager/includes/slm-meta-boxes.php b/software-license-manager/includes/slm-meta-boxes.php new file mode 100644 index 0000000..00ed4e1 --- /dev/null +++ b/software-license-manager/includes/slm-meta-boxes.php @@ -0,0 +1,64 @@ +'; + + // License Field + woocommerce_wp_text_input( + array( + 'id' => 'amount_of_licenses['. $loop .']', + 'label' => __( 'Number of Licenses (domain)', 'woocommerce' ), + 'placeholder' => '1-20', + 'desc_tip' => true, + 'wrapper_class' => 'form-row form-row-first', + 'description' => __( 'Ideal for themes, plugins, and websites', 'woocommerce' ), + 'value' => get_post_meta($variation->ID, 'amount_of_licenses', true) + ) + ); + + echo ""; + + echo '
Number of domains/installs in which this license can be used. Leave blank if you wish to use the default value set in the license manager plugin settings.
'; $product_config_html .= ''; + $product_config_html .= 'Number of devices in which this license can be used. Leave blank if you wish to use the default value set in the license manager plugin settings.
'; + $product_config_html .= 'Number of days before expiry. The expiry date of the license will be set based on this value. For example, if you want the key to expire in 6 months then enter a value of 180.
'; @@ -192,11 +222,13 @@ function slm_estore_new_product_added($prod_dat_array, $prod_id) { $fields['prod_id'] = $prod_id; $fields['meta_key'] = 'slm_max_allowed_domains'; $fields['meta_value'] = $prod_dat_array['slm_max_allowed_domains']; + $fields['meta_key'] = 'slm_max_allowed_devices'; + $fields['meta_value'] = $prod_dat_array['slm_max_allowed_devices']; $result = $wpdb->insert($product_meta_table_name, $fields); if (!$result) { //insert query failed } - + //Save expiry date value $fields = array(); $fields['prod_id'] = $prod_id; @@ -206,7 +238,7 @@ function slm_estore_new_product_added($prod_dat_array, $prod_id) { if (!$result) { //insert query failed } - + } function slm_estore_product_updated($prod_dat_array, $prod_id) { @@ -219,18 +251,22 @@ function slm_estore_product_updated($prod_dat_array, $prod_id) { //Found existing value so lets update it $fields = array(); $fields['meta_key'] = 'slm_max_allowed_domains'; + $fields['meta_key'] = 'slm_max_allowed_devices'; $fields['meta_value'] = $prod_dat_array['slm_max_allowed_domains']; - $result = $wpdb->update($product_meta_table_name, $fields, array('prod_id' => $prod_id)); - + $fields['meta_value'] = $prod_dat_array['slm_max_allowed_devices']; + $result = $wpdb->update($product_meta_table_name, $fields, array('prod_id' => $prod_id)); + } else { //No value for this field was there so lets insert one. $fields = array(); $fields['prod_id'] = $prod_id; $fields['meta_key'] = 'slm_max_allowed_domains'; + $fields['meta_key'] = 'slm_max_allowed_devices'; $fields['meta_value'] = $prod_dat_array['slm_max_allowed_domains']; - $result = $wpdb->insert($product_meta_table_name, $fields); + $fields['meta_value'] = $prod_dat_array['slm_max_allowed_devices']; + $result = $wpdb->insert($product_meta_table_name, $fields); } - + //Find the existing value for the expiry date field (for the given product) $product_meta = $wpdb->get_row("SELECT * FROM $product_meta_table_name WHERE prod_id = '$prod_id' AND meta_key='slm_date_of_expiry'", OBJECT); if ($product_meta) { @@ -238,24 +274,25 @@ function slm_estore_product_updated($prod_dat_array, $prod_id) { $fields = array(); $fields['meta_key'] = 'slm_date_of_expiry'; $fields['meta_value'] = $prod_dat_array['slm_date_of_expiry']; - $result = $wpdb->update($product_meta_table_name, $fields, array('prod_id' => $prod_id)); - + $result = $wpdb->update($product_meta_table_name, $fields, array('prod_id' => $prod_id)); + } else { //No value for this field was there so lets insert one. $fields = array(); $fields['prod_id'] = $prod_id; $fields['meta_key'] = 'slm_date_of_expiry'; $fields['meta_value'] = $prod_dat_array['slm_date_of_expiry']; - $result = $wpdb->insert($product_meta_table_name, $fields); + $result = $wpdb->insert($product_meta_table_name, $fields); } - + } function slm_estore_product_deleted($prod_id) { global $wpdb; $product_meta_table_name = WP_ESTORE_PRODUCTS_META_TABLE_NAME; - + $result = $wpdb->delete($product_meta_table_name, array('prod_id' => $prod_id, 'meta_key' => 'slm_max_allowed_domains')); + $result = $wpdb->delete($product_meta_table_name, array('prod_id' => $prod_id, 'meta_key' => 'slm_max_allowed_devices')); $result = $wpdb->delete($product_meta_table_name, array('prod_id' => $prod_id, 'meta_key' => 'slm_date_of_expiry')); } diff --git a/software-license-manager/logs/log.txt b/software-license-manager/logs/log.txt index e69de29..28c2021 100644 --- a/software-license-manager/logs/log.txt +++ b/software-license-manager/logs/log.txt @@ -0,0 +1,33 @@ +[07/04/2017 7:02 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/05/2017 7:21 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/06/2017 6:44 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/10/2017 4:49 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/10/2017 8:57 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/14/2017 6:34 PM] - STATUS : SLM daily cronjob - auto expiry of license key is enabled. +[07/18/2017 11:23 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:23 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:23 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:23 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:23 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:23 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:24 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:24 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:24 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:25 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:25 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:25 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:25 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:25 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:25 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:25 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:25 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:25 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:26 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:26 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:26 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:28 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:28 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:28 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. +[07/18/2017 11:30 PM] - SUCCESS : API - license check (slm_check) request received. +[07/18/2017 11:30 PM] - SUCCESS : License key: PK-15-LN-0-596e93655b246 +[07/18/2017 11:30 PM] - SUCCESS : API Response - Result: success Message: License key details retrieved. diff --git a/software-license-manager/menu/slm-add-licenses.php b/software-license-manager/menu/slm-add-licenses.php index 53648fa..421523d 100644 --- a/software-license-manager/menu/slm-add-licenses.php +++ b/software-license-manager/menu/slm-add-licenses.php @@ -6,6 +6,7 @@ function wp_lic_mgr_add_licenses_menu() { $id = ''; $license_key = ''; $max_domains = 1; + $max_devices = 1; $license_status = ''; $first_name = ''; $last_name = ''; @@ -20,7 +21,7 @@ function wp_lic_mgr_add_licenses_menu() { $current_date_plus_1year = date('Y-m-d', strtotime('+1 year')); $slm_options = get_option('slm_plugin_options'); - + echo 'When enabled, it will automatically set the status of a license key to "Expired" when the expiry date value of the key is reached. +
When enabled, it will automatically set the status of a license key to "Expired" when the expiry date value of the key is reached. It doesn't remotely deactivate a key. It simply changes the status of the key in your database to expired.
If checked, debug output will be written to log files (keep it disabled unless you are troubleshooting).
+If checked, debug output will be written to log files (keep it disabled unless you are troubleshooting).