Current Path : /home/church/www/offwins.com/wp-content/plugins/adunblocker/includes/ |
Current File : /home/church/www/offwins.com/wp-content/plugins/adunblocker/includes/class-adunblocker-license.php |
<?php class Adunblocker_License { private $plugin_name; private $version; private $license_email; private $license_key; private $license; private $status; private $token; private $product_id; public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->license = get_option( $this->plugin_name . '-license-options' ); $this->token = get_option( $this->plugin_name . '-auth-token' ); $this->setValid( false ); $this->product_id = "daau"; } public function get_token( $redirect = true ) { $this->license = get_option( $this->plugin_name . '-license-options' ); $response = ''; if( ! empty( $this->license[ $this->plugin_name . '-license-email' ] ) && ! empty( $this->license[ $this->plugin_name . '-license-key' ] ) ) { $request = array( 'name' => 'validateLicense', 'param' => array( 'email' => $this->license[ $this->plugin_name . '-license-email' ], 'key' => $this->license[ $this->plugin_name . '-license-key' ], 'host' => $_SERVER['HTTP_HOST'], 'product_id' => $this->product_id ) ); $curl = curl_init(); curl_setopt( $curl, CURLOPT_URL, $this->getApiURL() ); curl_setopt( $curl, CURLOPT_POST, true ); curl_setopt( $curl, CURLOPT_HTTPHEADER, [ 'content-type: application/json' ] ); curl_setopt( $curl, CURLOPT_POSTFIELDS, json_encode( $request ) ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); $result = curl_exec( $curl ); $err = curl_error( $curl ); curl_close( $curl ); if( $err ) { return array( 'status' => 'error', 'message' => "cURL error:" . json_encode( $err ) ); } else { $response = json_decode( $result, true ); if( ! $this->isJson( $result ) ) { return array( 'status' => 'error', 'message' => 'API Error: ' . json_encode($result ) ); } else { if( array_key_exists( 'error', $response) && $response['error'] ) { // $this->show_notice( $response['error']['message'] ); $this->clear_license_settings(); return array( 'status' => 'error', 'message' => "API Response: " . $response['error']['message'] ); } else { if( $response !== null ) { $token = $response['response']['result']['token']; $this->manage_token( $token ); if( $redirect ) { return array( 'status' => 'success', 'message' => 'License activated. Thank you.', 'redirect' => 'true' ); } else { return array( 'status' => 'success', 'message' => 'License activated. Thank you.' ); } } } } } } return array( 'status' => 'error', 'message' => 'Empty login and password.' . $response['error']['message'] ); } function isJson($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } public function getApiURL() { if( DAAU_DEBUG ) { return 'http://digitalapps.site/api/'; } else { return 'https://api.digitalapps.com/v1.1/'; } } public function isValid() { $status = $this->verify_token(); if( $status['status'] == 'success' ) { $this->setValid( true ); } return $this->status; } public function setValid( $status ) { $this->status = $status; } public function verify_token() { if( empty( $this->token ) && empty( $this->license[ $this->plugin_name . '-license-email' ] ) && empty( $this->license[ $this->plugin_name . '-license-key' ] ) ) { return array( 'status' => 'error', 'message' => 'Empty auth and login details.' ); } if( ! empty( $this->token ) ) { $tparts = explode( '.', $this->token ); $license = array(); if( is_array( $tparts ) ) { $base = base64_decode( $tparts[1] ); $license = json_decode( $base, true ); } if( $license[ 'license' ][ 'email' ] != $this->license[ $this->plugin_name . '-license-email' ] || $license[ 'license' ][ 'key' ] != $this->license[ $this->plugin_name . '-license-key' ] ) { // $this->show_notice( 'Invalid license details.' ); // $this->clear_license_settings(); $this->setValid( false ); return array( 'status' => 'error', 'message' => 'Invalid license details.' ); } if( $license[ 'exp' ] < time() ) { return $this->get_token( false ); } if( $license[ 'iss' ] != $_SERVER['HTTP_HOST'] ) { // $this->show_notice( 'License locked to a different domain. Please purchase a new license.' ); // $this->clear_license_settings(); $this->setValid( false ); return array( 'status' => 'error', 'message' => 'License locked to a different domain. Please purchase a new license.' ); } return array( 'status' => 'success', 'message' => 'Token is valid.' ); } else { return array( 'status' => 'error', 'message' => 'Token is not valid.' ); } } public function verify_license() { if( ! empty( $this->license[ $this->plugin_name . '-license-key' ] ) || ! empty( $this->license[ $this->plugin_name . '-license-key' ] ) ) { $token = get_option( $this->plugin_name . '-auth-token' ); if( ! empty( $token ) ) { $tparts = explode( '.', $token ); $license = array(); if( is_array( $tparts ) ) { $base = base64_decode( $tparts[1] ); $license = json_decode( $base, true ); } if( $license[ 'license' ][ 'email' ] != $this->license[ $this->plugin_name . '-license-email' ] || $license[ 'license' ][ 'key' ] != $this->license[ $this->plugin_name . '-license-key' ] ) { // $this->show_notice( 'Invalid license details.' ); $this->clear_license_settings(); $this->setValid( false ); return array( 'status' => 'error', 'message' => 'Invalid license details.' ); } if( strtotime( $license[ 'exp' ] ) >= strtotime( time() ) ) { $this->get_token(); } if( $license[ 'iss' ] != $_SERVER['HTTP_HOST'] ) { // $this->show_notice( 'License locked to a different domain. Please purchase a new license.' ); $this->clear_license_settings(); $this->setValid( false ); return array( 'status' => 'error', 'message' => 'License locked to a different domain. Please purchase a new license.' ); } return array( 'status' => 'success', 'message' => 'Token is valid.' ); } else { return $this->get_token(); } } else { $this->clear_license_settings(); $this->setValid( false ); return array( 'status' => 'error', 'message' => 'License details are not set.' ); } } public function manage_token( $token ) { if( $token ) { update_option( $this->plugin_name . '-auth-token', $token ); $this->setValid( true ); } } public function clear_license_settings() { $options = array( $this->plugin_name . '-license-key' => '', $this->plugin_name . '-license-email' => '' ); update_option( $this->plugin_name . '-license-options', $options ); update_option( $this->plugin_name . '-auth-token', '' ); } public function show_notice( $message ) { ?> <div class="notice notice-error is-dismissible"> <p><?php _e( $message ); ?></p> </div> <?php } }