Skip to content

Instantly share code, notes, and snippets.

@indikatordesign
Last active December 30, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indikatordesign/bb14c77bb4af0142ac20cd49dff1731e to your computer and use it in GitHub Desktop.
Save indikatordesign/bb14c77bb4af0142ac20cd49dff1731e to your computer and use it in GitHub Desktop.
[Indikator Design - EMP License Key Registration] Needs to run in WordPress
<?php
/**
* Do not allow direct access
*
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) die( 'Don\'t try to load this file directly!' );
/**
* Indikator Design - EMP License Key Registration
*
* @since 1.0.0
*/
if ( ! class_exists( 'IdLicenseKeyRegistration' ) )
{
class IdLicenseKeyRegistration
{
/**
* Define properties
*
* @since 1.0.0
*/
private $id;
private $key;
private $email;
private $secret;
private $vector;
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct( $id, $email, $key )
{
$this->id = 'id';
$this->key = 'key';
$this->email = 'email';
$this->secret = 'your-key'; // Get your key from your EMP Profile Page
$this->vector = 'your-vector'; // Get your vector from your EMP Profile Page
/**
* Let the magic happens
*
* @since 1.0.0
*/
$this->initialize();
} // end constructor
/**********************************************************************************
Check the license object and create a user
**********************************************************************************/
/**
* Initialize
*
* @since 1.0.0
*/
public function initialize()
{
$obj = $this->getLicenseObj( $this->key, $this->id, $this->email );
if ( is_object( $obj ) )
$id = $this->create( $obj );
if ( is_int( $id ) )
{
// return do something
} // end if
// return do something else
} // end initialize
/**********************************************************************************
Methods related to the license
**********************************************************************************/
/**
* Get the license information
*
* @since 1.0.0
*/
public function getLicenseObj( $key, $id, $email )
{
$obj = json_decode( $this->processKey( $this->secret, $this->vector, $key, true ) );
if ( is_object( $obj ) )
{
$obj = (object) $this->sanitizeArr( (array) $obj );
return $this->verifyLicense( $obj, $id, $email );
} // end if
return false;
} // end getLicenseObj
/**
* Encrypt/Decrypt the customer secret credential key
*
* @since 1.0.0
*/
private function processKey( $key, $vec, $data, $decrypt = false )
{
$met = 'AES-256-CBC';
$key = hash( 'sha256', $key );
$vec = substr( hash( 'sha256', $vec ), 0, 16 );
if ( ! $decrypt )
return base64_encode( openssl_encrypt( $data, $met, $key, 0, $vec ) );
else
return openssl_decrypt( base64_decode( $data ), $met, $key, 0, $vec );
} // end processKey
/**
* Check the license information
*
* @since 1.0.0
*/
private function verifyLicense( $obj, $id, $email )
{
if ( (int) $obj->payID != (int) $id || $obj->email != $email )
return false;
return $obj;
} // end verifyLicense
/**
* Sanitize multidimensional Arrays
*
* @since 1.0.0
*/
public function sanitizeArr( $arr )
{
foreach( $arr as $key => $val )
{
if ( false !== strpos( $key, 'key' ) || false !== strpos( $key, 'pass' ) )
{
$arr[$key] = $val;
continue;
} // end if
if ( false !== strpos( $key, 'url' ) || false !== strpos( $val, '//:' ) )
{
$arr[$key] = esc_url_raw( $val );
continue;
} // end if
$arr[$key] = esc_attr( $val );
} // end foreach
return $arr;
} // end sanitizeArr
/**********************************************************************************
Methods related to the user creation
**********************************************************************************/
/**
* Create User
*
* @since 1.0.0
*/
public function create( $obj )
{
$id = wp_insert_user(
[
'user_pass' => $obj->pass,
'user_login' => isset( $obj->user ) ? $obj->user : $obj->email,
'user_email' => $obj->email,
'first_name' => isset( $obj->first ) ? $obj->first : '',
'last_name' => isset( $obj->last ) ? $obj->last : '',
'nickname' => isset( $obj->first, $obj->last ) ? $obj->first . ' ' . $obj->last : '',
'display_name' => isset( $obj->first, $obj->last ) ? $obj->first . ' ' . $obj->last : '',
'role' => 'subscriber', // Set your desired user role
]); // end $user
if ( is_int( $id ) )
{
$this->setLicense( $id, $obj );
} // end if
return $id;
} // end create
/**
* Set the license key
*
* @since 1.0.0
*/
public function setLicense( $id, $obj )
{
// Add your desired Meta Key here
$key = '_license_' . str_replace( ' ', '_', strtolower( $obj->market ) ) . '_' . $obj->itemID;
$meta = get_user_meta( $id, $key, true );
$date = strtotime( $obj->date );
$expr = $this->expirationDate( $obj->expire, $date, $obj );
if ( time() >= $expr )
return false; // Do something if the license is expired
if ( $meta && ( (int) $meta->expire === $expr ) )
return false; // Do something if the license is already added
if ( $meta && ( (int) $meta->expire > $expr ) )
return false; // Do something if a newer license is already added
return update_user_meta( $id, $key, (object)
[
'expire' => $expr,
'payID' => (int) $obj->payID,
'itemID' => (int) $obj->itemID,
'market' => $obj->market,
'author' => $obj->author,
'title' => $obj->title,
'email' => $obj->email,
'date' => $date,
]); // end update_user_meta
} // end setLicense
/**
* Determining the expiration date
*
* @since 1.0.0
*/
public function expirationDate( $expire, $date, $obj )
{
return ( 'lifetime' == $expire || '' == $expire ) ? (int) $date + ( 100 * YEAR_IN_SECONDS ) : (int) $expire;
} // end expirationDate
} // end class
} // end if
// Instantiate the class
new IdLicenseKeyRegistration( $id, $email, $key );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment