Skip to content

Commit

Permalink
- Debug logger class fixed.
Browse files Browse the repository at this point in the history
- Fixed a few other things.
  • Loading branch information
amin0_000 committed Sep 23, 2014
1 parent 8860d4b commit 46ffa1b
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 47 deletions.
File renamed without changes.
48 changes: 31 additions & 17 deletions software-license-manager/includes/slm-api-listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ function __construct() {
function creation_api_listener() {
if (isset($_REQUEST['slm_action']) && trim($_REQUEST['slm_action']) == 'slm_create_new') {
//Handle the licene creation API query
global $slm_debug_logger;

if (isset($_REQUEST['secret_key'])) {

$options = get_option('slm_plugin_options');
$lic_key_prefix = $options['lic_prefix'];

SLM_API_Utility::verify_secret_key();//Verify the secret key first.

$slm_debug_logger->log_debug("API - license creation request received.");

$fields = array();
$fields['license_key'] = uniqid($lic_key_prefix);
$fields['lic_status'] = 'active';
$fields['first_name'] = $_REQUEST['first_name'];
$fields['last_name'] = $_REQUEST['last_name'];
$fields['email'] = $_REQUEST['email'];
$fields['company_name'] = $_REQUEST['company_name'];
$fields['txn_id'] = $_REQUEST['txn_id'];
$fields['lic_status'] = 'pending';
$fields['first_name'] = strip_tags($_REQUEST['first_name']);
$fields['last_name'] = strip_tags($_REQUEST['last_name']);
$fields['email'] = strip_tags($_REQUEST['email']);
$fields['company_name'] = 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']);
}

global $wpdb;
Expand All @@ -54,8 +61,6 @@ function creation_api_listener() {
$args = (array('result' => 'success', 'message' => 'License successfully created'));
SLM_API_Utility::output_api_response($args);
}


}

}
Expand All @@ -72,13 +77,18 @@ function creation_api_listener() {
function activation_api_listener() {
if (isset($_REQUEST['slm_action']) && trim($_REQUEST['slm_action']) == 'slm_activate') {
//Handle the license activation API query
global $slm_debug_logger;

if (isset($_REQUEST['secret_key'])) {

SLM_API_Utility::verify_secret_key();//Verify the secret key first.

$slm_debug_logger->log_debug("API - license activation request received.");

$fields = array();
$fields['lic_key'] = trim($_REQUEST['license_key']);
$fields['registered_domain'] = trim($_REQUEST['registered_domain']); //gethostbyaddr($_SERVER['REMOTE_ADDR']);
$fields['lic_key'] = trim(strip_tags($_REQUEST['license_key']));
$fields['registered_domain'] = trim(strip_tags($_REQUEST['registered_domain'])); //gethostbyaddr($_SERVER['REMOTE_ADDR']);
$slm_debug_logger->log_debug("License key: ".$fields['lic_key']. " Domain: ".$fields['registered_domain']);

global $wpdb;
$tbl_name = SLM_TBL_LICENSE_KEYS;
Expand All @@ -94,17 +104,15 @@ function activation_api_listener() {
if ($retLic->lic_status == 'blocked') {
$args = (array('result' => 'error', 'message' => 'Your License key is blocked'));
SLM_API_Utility::output_api_response($args);
}elseif ($retLic->lic_status == 'pending') {
$args = (array('result' => 'error', 'message' => 'Your License key is pending activation'));
SLM_API_Utility::output_api_response($args);
}elseif ($retLic->lic_status == 'expired') {
$args = (array('result' => 'error', 'message' => 'Your License key has expired'));
SLM_API_Utility::output_api_response($args);
}

if (floor($retLic->max_allowed_domains) > count($reg_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($_REQUEST['migrate_from'])));
$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);
}
Expand Down Expand Up @@ -133,22 +141,28 @@ function activation_api_listener() {
function deactivation_api_listener() {
if (isset($_REQUEST['slm_action']) && trim($_REQUEST['slm_action']) == 'slm_deactivate') {
//Handle the license deactivation API query
global $slm_debug_logger;

if (isset($_REQUEST['secret_key'])) {

SLM_API_Utility::verify_secret_key();//Verify the secret key first.

$slm_debug_logger->log_debug("API - license deactivation request received.");

if (empty($_REQUEST['registered_domain'])) {
$args = (array('result' => 'error', 'message' => 'Registered domain information is missing'));
SLM_API_Utility::output_api_response($args);
}
$registered_domain = trim($_REQUEST['registered_domain']);
$license_key = trim($_REQUEST['license_key']);
$registered_domain = trim(strip_tags($_REQUEST['registered_domain']));
$license_key = trim(strip_tags($_REQUEST['license_key']));
$slm_debug_logger->log_debug("License key: ".$license_key. " Domain: ".$registered_domain);

global $wpdb;
$registered_dom_table = SLM_TBL_LIC_DOMAIN;
$sql_prep = $wpdb->prepare("DELETE FROM $registered_dom_table WHERE lic_key=%s AND registered_domain=%s", $license_key, $registered_domain);
$delete = $wpdb->query($sql_prep);
if ($delete === false) {
//TODO - log the error
$slm_debug_logger->log_debug("Error - failed to delete the registered domain from the database.");
} else if ($delete == 0) {
$args = (array('result' => 'error', 'message' => 'The license key on this domain is already inactive'));
SLM_API_Utility::output_api_response($args);
Expand Down
33 changes: 5 additions & 28 deletions software-license-manager/includes/slm-api-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class SLM_API_Utility {
*/

static function output_api_response($args) {
//Log to debug file (if enabled)
global $slm_debug_logger;
$slm_debug_logger->log_debug('API Response - Result: ' . $args['result'] . ' Message: ' . $args['message']);

//Send response
echo json_encode($args);
exit(0);
}
Expand All @@ -21,33 +26,5 @@ static function verify_secret_key() {
SLM_API_Utility::output_api_response($args);
}
}

/**
* If debug is enabled and a log file exists in the root of the plugin directory, log the $data
*/
public static function log($data) {
//TODO - add option setting for debug
//$debug = get_option('slm_enable_debug');
$debug = TRUE; //harcoding for now
if($debug) {
$tz = '- Server time zone ' . date('T');
$date = date('m/d/Y g:i:s a', self::localTs());
$header = strpos($_SERVER['REQUEST_URI'], 'wp-admin') ? "\n\n======= ADMIN REQUEST =======\n[LOG DATE: $date $tz]\n" : "\n\n[LOG DATE: $date $tz]\n";
$filename = WP_LICENSE_MANAGER_PATH . "/slm_log.txt";
if(file_exists($filename) && is_writable($filename)) {
file_put_contents($filename, $header . $data, FILE_APPEND);
}
}
}

public static function localTs($timestamp=null) {
$timestamp = isset($timestamp) ? $timestamp : time();
if(date('T') == 'UTC') {
$timestamp += (get_option( 'gmt_offset' ) * 3600 );
}
return $timestamp;
}



}
112 changes: 112 additions & 0 deletions software-license-manager/includes/slm-debug-logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/*
* Logs debug data to a debug file in the "logs" folder. Example usage below:
*
* global $slm_debug_logger;
* $slm_debug_logger->log_debug("Some debug message");
*
* OR
*
* SLM_Debug_Logger::log_debug_st("Some debug message");
*/

class SLM_Debug_Logger
{
var $log_folder_path;
var $default_log_file = 'log.txt';
var $default_log_file_cron = 'log-cron-job.txt';
var $debug_enabled = false;
var $debug_status = array('SUCCESS','STATUS','NOTICE','WARNING','FAILURE','CRITICAL');
var $section_break_marker = "\n----------------------------------------------------------\n\n";
var $log_reset_marker = "-------- Log File Reset --------\n";

function __construct()
{
$this->log_folder_path = WP_LICENSE_MANAGER_PATH . '/logs';
//Check config and if debug is enabled then set the enabled flag to true
$options = get_option('slm_plugin_options');
if(!empty($options['enable_debug'])){//Debugging is enabled
$this->debug_enabled = true;
}
}

function get_debug_timestamp()
{
return '['.date('m/d/Y g:i A').'] - ';
}

function get_debug_status($level)
{
$size = count($this->debug_status);
if($level >= $size){
return 'UNKNOWN';
}
else{
return $this->debug_status[$level];
}
}

function get_section_break($section_break)
{
if ($section_break) {
return $this->section_break_marker;
}
return "";
}

function append_to_file($content,$file_name)
{
if(empty($file_name))$file_name = $this->default_log_file;
$debug_log_file = $this->log_folder_path.'/'.$file_name;
$fp=fopen($debug_log_file,'a');
fwrite($fp, $content);
fclose($fp);
}

function reset_log_file($file_name='')
{
if(empty($file_name))$file_name = $this->default_log_file;
$debug_log_file = $this->log_folder_path.'/'.$file_name;
$content = $this->get_debug_timestamp().$this->log_reset_marker;
$fp=fopen($debug_log_file,'w');
fwrite($fp, $content);
fclose($fp);
}

function log_debug($message,$level=0,$section_break=false,$file_name='')
{
if (!$this->debug_enabled) return;
$content = $this->get_debug_timestamp();//Timestamp
$content .= $this->get_debug_status($level);//Debug status
$content .= ' : ';
$content .= $message . "\n";
$content .= $this->get_section_break($section_break);
$this->append_to_file($content, $file_name);
}

function log_debug_cron($message,$level=0,$section_break=false)
{
if (!$this->debug_enabled) return;
$content = $this->get_debug_timestamp();//Timestamp
$content .= $this->get_debug_status($level);//Debug status
$content .= ' : ';
$content .= $message . "\n";
$content .= $this->get_section_break($section_break);
//$file_name = $this->default_log_file_cron;
$this->append_to_file($content, $this->default_log_file_cron);
}

static function log_debug_st($message,$level=0,$section_break=false,$file_name='')
{
$options = get_option('slm_plugin_options');
if(empty($options['enable_debug'])){//Debugging is disabled
return;
}
$content = '['.date('m/d/Y g:i A').'] - STATUS : '. $message . "\n";
$debug_log_file = WP_LICENSE_MANAGER_PATH . '/logs/log.txt';
$fp=fopen($debug_log_file,'a');
fwrite($fp, $content);
fclose($fp);
}
}
File renamed without changes.
Empty file.
Empty file.
Empty file.
13 changes: 12 additions & 1 deletion software-license-manager/menu/lic_add_licenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function wp_lic_mgr_add_licenses_menu() {
$created_date = '';
$renewed_date = '';
$expiry_date = '';
$current_date = (date ("Y-m-d"));

$slm_options = get_option('slm_plugin_options');

Expand Down Expand Up @@ -61,6 +62,15 @@ function wp_lic_mgr_add_licenses_menu() {
$renewed_date = $_POST['date_renewed'];
$expiry_date = $_POST['date_expiry'];

if(empty($created_date)){
$created_date = $current_date;
}
if(empty($renewed_date)){
$renewed_date = $current_date;
}
if(empty($expiry_date)){
$expiry_date = $current_date;
}

//Save the entry to the database
$fields = array();
Expand All @@ -81,6 +91,7 @@ function wp_lic_mgr_add_licenses_menu() {
$lk_table = SLM_TBL_LICENSE_KEYS;
if (empty($id)) {//Insert into database
$result = $wpdb->insert( $lk_table, $fields);
$id = $wpdb->insert_id;
if($result === false){
$errors .= __('Record could not be inserted into the database!', 'slm');
}
Expand Down Expand Up @@ -252,7 +263,7 @@ function wp_lic_mgr_add_licenses_menu() {
</div>
</form>
</div></div>
<a href="admin.php?page='.SLM_MAIN_MENU_SLUG.'" class="button">Manage Licenses</a><br /><br />
<a href="admin.php?page=<?php echo SLM_MAIN_MENU_SLUG; ?>" class="button">Manage Licenses</a><br /><br />
</div></div>
</div>

Expand Down
20 changes: 20 additions & 0 deletions software-license-manager/menu/lic_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ function wp_lic_mgr_general_settings() {
'lic_prefix' => trim($_POST["lic_prefix"]),
'default_max_domains' => trim($_POST["default_max_domains"]),
'lic_verification_secret' => trim($_POST["lic_verification_secret"]),
'enable_debug' => isset($_POST['enable_debug']) ? '1':'',
);
update_option('slm_plugin_options', $options);

echo '<div id="message" class="updated fade"><p>';
echo 'Options Updated!';
echo '</p></div>';
}

$options = get_option('slm_plugin_options');
Expand Down Expand Up @@ -85,6 +90,21 @@ function wp_lic_mgr_general_settings() {
</table>
</div></div>

<div class="postbox">
<h3><label for="title">Debugging and Testing Settings</label></h3>
<div class="inside">
<table class="form-table">

<tr valign="top">
<th scope="row">Enable Debug Logging</th>
<td><input name="enable_debug" type="checkbox"<?php if ($options['enable_debug'] != '') echo ' checked="checked"'; ?> value="1"/>
<p class="description">If checked, debug output will be written to log files (keep it disabled unless you are troubleshooting).</p>
</td>
</tr>

</table>
</div></div>

<div class="submit">
<input type="submit" class="button-primary" name="slm_save_settings" value=" <?php _e('Update Options', 'slm'); ?>" />
</div>
Expand Down
7 changes: 6 additions & 1 deletion software-license-manager/slm_plugin_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
define('SLM_MENU_ICON', 'dashicons-lock');

//Includes
include_once('includes/slm-debug-logger.php');
include_once('includes/slm-init-time-tasks.php');
include_once('includes/slm-api-utility.php');
include_once('includes/slm-api-listener.php');
include_once('includes/slm-third-party-integration.php');
include_once('includes/slm_db_access.php');
//Include admin side only files
if (is_admin()) {
include_once('menu/slm-admin-init.php');
Expand All @@ -24,6 +24,10 @@
add_action('init', 'slm_init_handler');
add_action('plugins_loaded', 'slm_plugins_loaded_handler');

//Initialize debug logger
global $slm_debug_logger;
$slm_debug_logger = new SLM_Debug_Logger();

//Do init time tasks
function slm_init_handler() {
$init_task = new SLM_Init_Time_Tasks();
Expand All @@ -39,6 +43,7 @@ function slm_plugins_loaded_handler() {
require_once(dirname(__FILE__) . '/slm_installer.php');
}
}

}

//TODO - need to move this to an ajax handler file
Expand Down

0 comments on commit 46ffa1b

Please sign in to comment.