File: /home/kashmira/.trash/stylo-core/inc/License.php
<?php
	
	namespace LMFW\SDK;
	
	use ErrorException;
	
	if ( ! class_exists( 'LMFW\SDK\License' ) ) {
		
		/**
		 * License Manager for WooCommerce SDK to let communication with the API
		 *
		 * Defines basic functionality to connect with the API
		 *
		 * @since      1.0.0
		 * @package    Pahp/SDK
		 * @subpackage License
		 * @author     Pablo Hernández (OtakuPahp) <pablo@otakupahp.com>
		 */
		class License {
			
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string
			 */
			private $plugin_name;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string $api_url
			 */
			private $api_url;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string $customer_key
			 */
			private $customer_key;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string $customer_secret
			 */
			private $customer_secret;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var array $valid_status
			 */
			private $valid_status;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var array
			 */
			private $product_ids;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string
			 */
			private $stored_license;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var string
			 */
			private $valid_object;
			
			/**
			 * @since 1.0.0
			 * @access private
			 * @var int
			 */
			private $ttl;
			
			/**
			 * License constructor
			 *
			 * @param string $plugin_name
			 * @param string $server_url
			 * @param string $customer_key
			 * @param string $customer_secret
			 * @param mixed $product_ids
			 * @param array $license_options
			 * @param string $valid_object
			 * @param int $ttl
			 * @since 1.0.0
			 *
			 */
			public function __construct(
				$plugin_name,
				$server_url,
				$customer_key,
				$customer_secret,
				$product_ids,
				$license_options,
				$valid_object,
				$ttl
			) {
				// Set plugin name for internationalization
				$this->plugin_name = $plugin_name;
				
				// Connection variables
				$this->api_url         = "{$server_url}/wp-json/lmfwc/v2/";
				$this->customer_key    = $customer_key;
				$this->customer_secret = $customer_secret;
				
				// Product IDs
				$this->product_ids = is_array( $product_ids ) ? $product_ids : array( $product_ids );
				
				// License variables
				$this->license_options = $license_options;
				$this->valid_object = $valid_object;
				$this->ttl          = $ttl;
				$this->valid_status = get_option( $valid_object, array() );
				
			}
			
			/**
			 * HTTP Request call
			 *
			 * @param string $endpoint
			 * @param string $method
			 * @param string $args
			 *
			 * @return array
			 * @throws ErrorException
			 * @since 1.0.0
			 *
			 */
			private function call( $endpoint, $method = 'GET', $args = '' ) {
				// Populate the correct endpoint for the API request
				$url = "{$this->api_url}{$endpoint}?consumer_key={$this->customer_key}&consumer_secret={$this->customer_secret}";
				// Create header
				$headers = array(
					'Accept'       => 'application/json',
					'Content-Type' => 'application/json; charset=UTF-8',
				);
				
				// Initialize wp_args
				$wp_args = array(
					'headers' => $headers,
					'method'  => $method,
					'timeout' => 10,
				);
				
				// Populate the args for use in the wp_remote_request call
				if ( ! empty( $args ) ) {
					$wp_args['body'] = $args;
				}
				
				// Make the call and store the response in $res
				$res = wp_remote_request( $url, $wp_args );
				
				// Check for success
				return json_decode( $res['body'], true );
			}
			/**
			 * HTTP POST call
			 *
			 * @param string $endpoint
			 * @param string $method
			 * @param string $args
			 *
			 * @return array
			 * @throws ErrorException
			 * @since 1.0.0
			 *
			 */
			private function post( $endpoint, $method = 'GET', $args = '' ) {
				// Populate the correct endpoint for the API request
				$url = "{$this->api_url}{$endpoint}?consumer_key={$this->customer_key}&consumer_secret={$this->customer_secret}";
				// Create header
				$headers = array(
					'Accept'       => 'application/json',
					'Content-Type' => 'application/json; charset=UTF-8',
				);
				
				// Initialize wp_args
				$wp_args = array(
					'headers' => $headers,
					'method'  => $method,
					'timeout' => 10,
				);
				
				// Populate the args for use in the wp_remote_request call
				if ( ! empty( $args ) ) {
					$wp_args['body'] = json_encode($args);
				}
				
				// Make the call and store the response in $res
				$res = wp_remote_request( $url, $wp_args );
				
				// Check for success
				return json_decode( $res['body'], true );
			}
			/**
			 * Validate license
			 *
			 * @param $license_key
			 * @throws ErrorException
			 * @since 1.0.0
			 */
			public function retrieve( $license_key ) {
				if ( ! empty( $license_key ) ) {
					$response = $this->call( "licenses/validate/{$license_key}" );
					if ( isset( $response['success'] ) && (bool) $response['success'] ) {
						return $response['data'];
					} elseif($response['code'] == 'lmfwc_rest_data_error') {
						return $response['message'];
					} else {
						return $response;
					}
				}
			}
			/**
			 * Retrieve license information
			 *
			 * @param $license_key
			 * @throws ErrorException
			 * @since 1.0.0
			 */
			public function license_info( $license_key ) {
				if ( ! empty( $license_key ) ) {
					$response = $this->call( "licenses/{$license_key}" );
					return $response;
				}
			}
			/**
			 * Ping Product
			 *
			 * @param $license_key, $product_name, $host
			 * @throws ErrorException
			 * @since 1.0.0
			 */
			public function ping( $license_key, $product_name, $host ) {
				if ( ! empty( $license_key ) ) {
					$response = $this->post( "products/ping", "POST", array("license_key" => $license_key, "product_name" => $product_name, "host" => $host) );
					return $response;
				}
			}
			/**
			 * Activate license
			 *
			 * @param $license_key, $order_id, $customer_email
			 * @return array|null
			 * @throws ErrorException
			 *
			 * @since 1.0.0
			 *
			 */
			public function activate( $customer_email, $order_id, $license_key ) {
				$response = null;
				$site_url = site_url();
				if ( ! empty( $customer_email ) && ! empty( $order_id ) && ! empty( $license_key ) ) {
					$license_check = $this->retrieve($license_key);
					if(!empty($license_check)) {
						if(($license_check['orderId'] == $order_id) && ($license_check['licenseKey'] == $license_key)) {
							$activation_data = $license_check['activationData'];
							if($activation_data){
								$data_count = count($activation_data);
								$tokens_array = array();
								if($data_count > 0) {
									for($i=0; $i<$data_count; $i++) {
										$tokens_array[] = $activation_data[$i]['token'];
									}
								}
		
								$existing_activation_object = get_option('stylo_license_status');
								$existing_activation_data = $existing_activation_object['activation_data'];
								$activation_token = $existing_activation_data['token'];
		
		
								if(in_array($activation_token, $tokens_array)) {
									$response = $this->call( "licenses/activate/{$license_key}", 'GET', array("token" => $activation_token, "label" => $site_url) );
								} else {
									$response = $this->call( "licenses/activate/{$license_key}", 'GET', array("label" => $site_url) );
								}
							} else {
								$response = $this->call( "licenses/activate/{$license_key}", 'GET', array("label" => $site_url) ); 
							}
						} else {
							$response = 'Invalid License Key or Order ID';
						}
					}
					if ( isset( $response['success'] ) && (bool) $response['success'] ) {
						$license = $response['data'];
						if($license['activationData'] != '' && $license['errors'] == '') {
							$this->valid_status['is_valid']       = 1;
							$this->valid_status['activation_data']    = $license['activationData'];
							$this->valid_status['nextValidation'] = time();
							$this->valid_status['error'] = '';
							update_option( $this->valid_object, $this->valid_status );
							$response = 'License key Activated';
						} else {
							$response = $license['errors']['lmfwc_rest_data_error'][0];
						}
					} else {
						$this->valid_status['is_valid']       = false;
						$this->valid_status['error']          = $response['message'];
						$this->valid_status['nextValidation'] = time();
						update_option( $this->valid_object, $this->valid_status );
						
					}
				} else {
					$response = 'Missing required information';
				}
				
				return $response;
			}
			
			/**
			 * Deactivate license
			 *
			 * @param $license_key
			 * @throws ErrorException
			 * @since 1.0.0
			 */
			public function deactivate( $license_key ) {
				if ( ! empty( $license_key ) ) {
    				$existing_activation_object = get_option('stylo_license_status');
					$existing_activation_data = $existing_activation_object['activation_data'];
					$activation_token = $existing_activation_data['token'];
					
					$response = $this->call( "licenses/deactivate/{$license_key}", 'GET', array("token" => $activation_token) );
					if ( isset( $response['success'] ) && (bool) $response['success'] ) {
						$this->valid_status['is_valid']       = false;
						$this->valid_status['error']          = 'License deactivated';
						$this->valid_status['nextValidation'] = time();
						update_option($this->valid_object, $this->valid_status);
						$response = 'License Key Deactivated';
					}
				
				}  else {
					$response = 'License not found';
				}
				return $response;
			}
						
			/**
			 * Returns time of license validity
			 *
			 * @return int|null
			 * @since 1.0.0
			 */
			public function valid_until() {
				return isset( $this->valid_status['valid_until'] ) ? $this->valid_status['valid_until'] : null;
			}
			
		}
		
	}