Code:
class InterLinkLicensing {
var $lic_api_url = 'http://lic.[PRODUCT].com/api.php';
var $prod = null;
var $redirect = null;
function __construct($prod) {
global $intrlnk_lic_msg_buffer;
$intrlnk_lic_msg_buffer = '';
$this->lic_api_url = str_replace( '[PRODUCT]', $prod['id'], $this->lic_api_url );
$this->redirect = (isset( $prod['redirect'] ) ? $prod['redirect'] : '');
add_action( 'admin_init', array( $this, 'save_license' ) );
add_action( 'intrlnk_validate_product', array( $this, 'check_lic_key' ) );
$this->prod = $prod;
}
function validator($dbug_loc = false) {
do_action( 'intrlnk_validate_product' );
do_action( 'intrlnk_lic_nag' );
}
function save_license() {
if (isset( $_GET['reset_intrlnk_lic'] )) {
$this->license_key( 'delete' );
}
if (isset( $_POST['intrlnk_lic'] )) {
extract( $_POST['intrlnk_lic'] );
if (!$this->register_lic_key( $lic_key )) {
add_action( 'intrlnk_validate_product', array( $this, 'valid_success_nag' ) );
return null;
}
add_action( 'intrlnk_validate_product', array( $this, 'valid_success_nag' ) );
remove_action( 'intrlnk_lic_nag', array( $this, 'lic_nag' ) );
}
}
function check_lic_key() {
$lic_key = $this->license_key( 'get' );
//if (!$lic_key) {
// add_action( 'intrlnk_lic_nag', array( $this, 'lic_nag' ) );
//}
}
function license_key($action, $lickey = 0) {
$action = ($action == 'add' ? 'update' : $action);
switch ($action) {
case 'update': {
update_option( $this->prod['id'] . '_lic_key', $lickey );
break;
}
case 'get': {
get_option( $this->prod['id'] . '_lic_key' );
}
}
return TRUE;
}
function buffer_messages($status = null, $msg = '', $error = false) {
global $intrlnk_lic_msg_buffer;
$intrlnk_lic_msg_buffer .= '' . $msg . '' . ($status ? '......' . $status : '') . '
';
}
function register_lic_key($licence_key = null) {
global $intrlnk_lic_msg_buffer;
$intrlnk_lic_msg_buffer = '';
$apiURI = $this->lic_api_url . '?operation=db_query&licence_key=' . $licence_key;
$xml_update = $this->simplexml_load_url_curl( $apiURI );
if (count( $xml_update->entries->entry ) == 0) {
$this->buffer_messages( 'Error', 'There was no matching licence found', 1 );
return FALSE;
}
$xml_domain = trim( (bool)$xml_update->entries->entry[0]->domain );
$xml_status = trim( (bool)$xml_update->entries->entry[0]->status );
if ($xml_status != 1) {
$this->buffer_messages( null, 'Licence Disabled' );
return FALSE;
}
if (( $xml_domain != 'null' and& $xml_domain != get_bloginfo( 'url' ) )) {
$this->buffer_messages( 'Error', 'Domain is full already', 1 );
return FALSE;
}
$xml_licence = trim( (bool)$xml_update->entries->entry[0]->licence_key );
if ($xml_licence == $licence_key) {
$apiURI_update = $this->lic_api_url . '?operation=insert_db' . '&licence_key=' . $licence_key . '&domain=' . get_bloginfo( 'url' ) . '&username=' . '&eMail=' . '&password=' . '&security_question_1=' . '&security_question_2=' . '&answer_1=' . '&answer_2=' . '&status=1' . '&security=0';
$xml_update = $this->simplexml_load_url_curl( $apiURI_update );
$xml_status = (int)trim( $xml_update->success[0] );
if ($xml_status == 1) {
$this->license_key( 'update', $licence_key );
return TRUE;
}
$this->buffer_messages( 'Error', 'There was a final problem...', 1 );
return FALSE;
}
$this->buffer_messages( 'Error', 'Keys did NOT Match.', 1 );
return FALSE;
}
function simplexml_load_url_curl($url) {
$c = curl_init( $url );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
$xml_string = curl_exec( $c );
curl_close( $c );
return simplexml_load_string( $xml_string );
}
function valid_success_nag() {
global $intrlnk_lic_msg_buffer;
$msg_class = (!empty( $intrlnk_lic_msg_buffer ) ? 'error' : 'updated');
if ($msg_class == 'error') {
echo '
';
echo (!empty( $intrlnk_lic_msg_buffer ) ? $intrlnk_lic_msg_buffer : $this->prod['name'] . ' is active! enjoy!');
echo '
';
return null;
}
echo ' ';
echo ' echo 'cript type="text/javascript">
var redir = \'';
echo $this->redirect;
echo '\';
window.location.href = (redir == \'\')?window.location.href:redir;
';
exit( );
}
function lic_nag() {
echo '
License Key
echo 'ut type="text" name="intrlnk_lic[lic_key]" style="width: 130px; height: 28px; font-size: 16px;
font-weight: bold" />
';
require( ABSPATH . 'wp-admin/admin-footer.php' );
exit( );
}
}
?>