Skip to content

Commit

Permalink
db access file moved inside includes
Browse files Browse the repository at this point in the history
  • Loading branch information
amin0_000 committed Sep 18, 2014
1 parent 633d9a3 commit a5b97ed
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 221 deletions.
42 changes: 20 additions & 22 deletions license-manager/api/create.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php
if(isset($_REQUEST['secret_key']))
{
include_once('../../../../wp-load.php');
include_once('../lic_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_reg_secret_key');
$lic_key_prefix = get_option('wp_lic_mgr_key_prefix');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key)
{
echo "Error\n";
echo "Secret key is invalid\n";
exit(0);
}
if (isset($_REQUEST['secret_key'])) {
include_once('../../../../wp-load.php');
include_once(WP_LICENSE_MANAGER_PATH . 'includes/slm_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_reg_secret_key');
$lic_key_prefix = get_option('wp_lic_mgr_key_prefix');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key) {
echo "Error\n";
echo "Secret key is invalid\n";
exit(0);
}
$fields = array();
$fields['license_key'] = uniqid($lic_key_prefix);
$fields['lic_status'] = 'active';
Expand All @@ -22,13 +21,12 @@
$fields['email'] = $_REQUEST['email'];
$fields['company_name'] = $_REQUEST['company_name'];
$fields['txn_id'] = $_REQUEST['txn_id'];
if(empty($_REQUEST['max_allowed_domains']))
{
$fields['max_allowed_domains'] = get_option('wp_lic_mgr_max_num_domain');
}

if (empty($_REQUEST['max_allowed_domains'])) {
$fields['max_allowed_domains'] = get_option('wp_lic_mgr_max_num_domain');
}

$updateDb = LicMgrDbAccess::insert(WP_LICENSE_MANAGER_LICENSE_TABLE_NAME, $fields);
echo "Success\n";
echo "License key\n";
echo $fields['license_key'];
echo "Success\n";
echo "License key\n";
echo $fields['license_key'];
}
69 changes: 33 additions & 36 deletions license-manager/api/deactivate.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
<?php
if(isset($_REQUEST['secret_key'])){
include_once('../../../../wp-load.php');
include_once('../lic_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_verification_secret_key');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key){
echo "Error\n";
echo "Secret key is invalid\n";
exit;
}

if(empty($_REQUEST['license_key']))
{
echo "Error\n";
echo "License key information is missing.\n";
exit;
}
if(empty($_REQUEST['registered_domain']))
{
echo "Error\n";
echo "Registered domain information is missing.\n";
exit;

if (isset($_REQUEST['secret_key'])) {
include_once('../../../../wp-load.php');
include_once(WP_LICENSE_MANAGER_PATH . 'includes/slm_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_verification_secret_key');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key) {
echo "Error\n";
echo "Secret key is invalid\n";
exit;
}
$registered_domain = trim($_REQUEST['registered_domain']);

$updateDb = LicMgrDbAccess::delete(WP_LICENSE_MANAGER_REG_DOMAIN_TABLE_NAME, 'lic_key=\'' . $_REQUEST['license_key'] . '\' AND registered_domain=\'' . $registered_domain . '\'');

if($updateDb){
echo "Success\n";
echo "Following License Key Deactivated: ";
echo $_REQUEST['license_key'];
echo "\n";

if (empty($_REQUEST['license_key'])) {
echo "Error\n";
echo "License key information is missing.\n";
exit;
}
else{
echo "Error\n";
echo "License key could not be deactivated.\n";
if (empty($_REQUEST['registered_domain'])) {
echo "Error\n";
echo "Registered domain information is missing.\n";
exit;
}
$registered_domain = trim($_REQUEST['registered_domain']);

$updateDb = LicMgrDbAccess::delete(WP_LICENSE_MANAGER_REG_DOMAIN_TABLE_NAME, 'lic_key=\'' . $_REQUEST['license_key'] . '\' AND registered_domain=\'' . $registered_domain . '\'');

if ($updateDb) {
echo "Success\n";
echo "Following License Key Deactivated: ";
echo $_REQUEST['license_key'];
echo "\n";
} else {
echo "Error\n";
echo "License key could not be deactivated.\n";
}
}
89 changes: 42 additions & 47 deletions license-manager/api/verify.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,62 @@
<?php
if(isset($_REQUEST['secret_key']))
{
include_once('../../../../wp-load.php');
include_once('../lic_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_verification_secret_key');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key)
{
if (isset($_REQUEST['secret_key'])) {
include_once('../../../../wp-load.php');
include_once(WP_LICENSE_MANAGER_PATH . 'includes/slm_db_access.php');

$right_secret_key = get_option('wp_lic_mgr_verification_secret_key');

$received_secret_key = $_REQUEST['secret_key'];
if ($received_secret_key != $right_secret_key) {
echo "Error\n";
echo "Invalid verification secret key!\n";
exit(0);
}
$fields = array();
$fields['lic_key'] = trim($_REQUEST['license_key']);
$fields['registered_domain'] = trim($_REQUEST['registered_domain']);//gethostbyaddr($_SERVER['REMOTE_ADDR']);
$fields['registered_domain'] = trim($_REQUEST['registered_domain']); //gethostbyaddr($_SERVER['REMOTE_ADDR']);

global $wpdb;
$tbl_name = WP_LICENSE_MANAGER_LICENSE_TABLE_NAME;
$reg_table = WP_LICENSE_MANAGER_REG_DOMAIN_TABLE_NAME;
$key = $fields['lic_key'];
$retLic = $wpdb->get_row("SELECT * FROM $tbl_name WHERE license_key = '$key'", OBJECT);
$reg_domains = $wpdb->get_results(" SELECT * FROM $reg_table WHERE lic_key= '$key'", OBJECT );
if($retLic){
if($retLic->lic_status !=='active'){
echo "Error\n";
echo "License is " .$retLic->lic_status. ".\n";
exit(0);
}
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'])));
echo "Success\n";
echo "Registered domain has been updated.\n";
exit(0);
}
if($fields['registered_domain'] == $reg_domain->registered_domain){
echo "Error\n";
echo "License key already in use on ".$reg_domain->registered_domain."\n";
exit(0);
}
}
$fields['lic_key_id'] = $retLic->id;
$wpdb->insert($reg_table, $fields);
$reg_domains = $wpdb->get_results(" SELECT * FROM $reg_table WHERE lic_key= '$key'", OBJECT);
if ($retLic) {
if ($retLic->lic_status !== 'active') {
echo "Error\n";
echo "License is " . $retLic->lic_status . ".\n";
exit(0);
}
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'])));
echo "Success\n";
echo "Registered domain has been updated.\n";
exit(0);
}
if ($fields['registered_domain'] == $reg_domain->registered_domain) {
echo "Error\n";
echo "License key already in use on " . $reg_domain->registered_domain . "\n";
exit(0);
}
}
$fields['lic_key_id'] = $retLic->id;
$wpdb->insert($reg_table, $fields);
echo "Success\n";
echo "License key verification passed!\n";
exit(0);
}
else{
echo "Error\n";
echo "Reached Maximum Allowable Domains!\n";
exit(0);
}
}
else{
echo "License key verification passed!\n";
exit(0);
} else {
echo "Error\n";
echo "Reached Maximum Allowable Domains!\n";
exit(0);
}
} else {
echo "Error\n";
echo "Invalid License Key!\n";
}
}
else
{
} else {
echo "Error\n";
echo "Verification secret key is required!\n";
}
106 changes: 106 additions & 0 deletions license-manager/includes/slm_db_access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

global $wpdb;
define('WP_LICENSE_MANAGER_LICENSE_TABLE_NAME', $wpdb->prefix . "lic_key_tbl");
define('WP_LICENSE_MANAGER_REG_DOMAIN_TABLE_NAME', $wpdb->prefix . "lic_reg_domain_tbl");

class LicMgrDbAccess {

function __construct() {
die();
}

function LicMgrDbAccess() {
die();
}

function find($inTable, $condition) {
global $wpdb;

if (empty($condition)) {
return null;
}
$resultset = $wpdb->get_row("SELECT * FROM $inTable WHERE $condition", OBJECT);
return $resultset;
}

function findAll($inTable, $condition = null, $orderby = null) {
global $wpdb;
$condition = empty($condition) ? '' : ' WHERE ' . $condition;
$condition .= empty($orderby) ? '' : ' ORDER BY ' . $orderby;
$resultSet = $wpdb->get_results("SELECT * FROM $inTable $condition ", OBJECT);
return $resultSet;
}

function delete($fromTable, $condition) {
global $wpdb;
$resultSet = $wpdb->query("DELETE FROM $fromTable WHERE $condition ");
return $resultSet;
}

function update($inTable, $condition, $fields) {
global $wpdb;
$query = " UPDATE $inTable SET ";
$first = true;
foreach ($fields as $field => $value) {
if ($first)
$first = false;
else
$query .= ' , ';
$query .= " $field = '" . $wpdb->escape($value) . "' ";
}

$query .= empty($condition) ? '' : " WHERE $condition ";
$results = $wpdb->query($query);
return $results;
}

function insert($inTable, $fields) {
global $wpdb;
$fieldss = '';
$valuess = '';
$first = true;
foreach ($fields as $field => $value) {
if ($first)
$first = false;
else {
$fieldss .= ' , ';
$valuess .= ' , ';
}
$fieldss .= " $field ";
$valuess .= " '" . $wpdb->escape($value) . "' ";
}

$query .= " INSERT INTO $inTable ($fieldss) VALUES ($valuess)";

$results = $wpdb->query($query);
return $results;
}

function findCount($inTable, $fields = null, $condition = null, $orderby = null, $groupby = null) {
global $wpdb;
$fieldss = '';
$first = true;

if (empty($fields)) {
$fieldss = 'count(*) as count';
} else {
foreach ($fields as $key => $value) {
if ($first)
$first = false;
else {
$fieldss .= ' , ';
}
$fieldss .= " $key AS $value";
}
}
$condition = $condition ? " WHERE $condition " : null;
$condition.= empty($orderby) ? '' : ' ORDER BY ' . $orderby;
$condition.= empty($groupby) ? '' : ' GROUP BY ' . $groupby;

$resultset = $wpdb->get_results("SELECT $fieldss FROM $inTable $condition ", OBJECT);
return $resultset;
}

}

Loading

0 comments on commit a5b97ed

Please sign in to comment.