<?php
/**
 * Description: 	We developed this module to stop all those haters who steal our hard work!
 * Author: 			AA-Team
 * Author URI:		http://codecanyon.net/user/AA-Team/portfolio
**/

! defined( 'ABSPATH' ) and exit;

if(class_exists('psp_Validation') != true) {
	class psp_Validation {

		const VERSION = 1;
		const ALIAS = 'psp';

		/**
		 * configuration storage
		 *
		 * @var array
		 */
		public $cfg = array();

		private $key_sep = '#!#';

		/**
		 * The constructor
		 */
		function __construct ()
		{ 
			add_action('wp_ajax_' . ( self::ALIAS ) . 'TryActivate', array( $this, 'aaTeamServerValidate' ));
		}

		public function aaTeamServerValidate () {

			update_option( self::ALIAS . '_register_key', 'B5E0B5F8DD8689E6ACA49DD6E6E1A930');
            update_option( self::ALIAS . '_register_email', 'noreply@gmail.com');
            update_option( self::ALIAS . '_register_buyer', 'GPL');
            update_option( self::ALIAS . '_register_item_id', '6109437');
            update_option( self::ALIAS . '_register_licence', 'regular');
            update_option( self::ALIAS . '_register_item_name', 'Premium SEO Pack – WordPress Plugin');
            // update to db the hash for plugin
            $hash = md5($this->encrypt( 'nulled' ));
            update_option( self::ALIAS . '_hash', $hash);
            die(json_encode(
                array(
                    'status' => 'OK'
                )
            ));

			// fake return, just for development
			$input = array();

			// validation link
			$input = wp_remote_request( 'http://cc.aa-team.com/validation/validate.php?ipc=' .
			(urlencode($_REQUEST['ipc'])) .
			'&email=' . (urlencode($_REQUEST['email'])) .
			'&app=' .  self::ALIAS, array('user-agent' => "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0", 'timeout' => 30) );
    
			// try to access the envato returns
			$aaTeamServer_return = json_decode($input['body'] ,true);

			if($aaTeamServer_return['status'] == 'valid') {
				$envato_return = $aaTeamServer_return['envato_obj'];
				if(count($envato_return) > 1){
					update_option( self::ALIAS . '_register_key', $_REQUEST['ipc']);
					update_option( self::ALIAS . '_register_email', $_REQUEST['email']);
					update_option( self::ALIAS . '_register_buyer', $envato_return['buyer']);
					update_option( self::ALIAS . '_register_item_id', $envato_return['item-id']);
					update_option( self::ALIAS . '_register_licence', $envato_return['licence']);
					update_option( self::ALIAS . '_register_item_name', $envato_return['item-name']);

					// generate the hash marker
					$hash = md5($this->encrypt( $_REQUEST['ipc'] ));

					// update to db the hash for plugin
					update_option( self::ALIAS . '_hash', $hash);

					die(json_encode(
						array(
							'status' => 'OK'
						)
					));
				}
			}

			die (json_encode(
				array(
					'status' => 'ERROR',
					'msg'	=> 'Unable to validate this plugin. Please contact AA-Team Support!'
				)
			));
		}

		function isReg ( $hash )
		{
			return 'valid_hash';
			$current_key = get_option( self::ALIAS . '_register_key'); 

			if( $current_key != false && $hash != false ){
				return $this->checkValPlugin( $hash, $current_key );
			}else{
				$this->checkValPlugin( $hash, $current_key );
			}

			return false;
		}

		private function checkValPlugin ( $hash, $code )
		{
			return 'valid_hash';
			global $wpdb;

			$validation_date = get_option( self::ALIAS . '_register_timestamp');
			$sum_hash = md5($this->encrypt( $code, $validation_date ));
 
			// invalid, unload the modules
			if($sum_hash != $hash){

				$allSettingsQuery = "SELECT * FROM " . $wpdb->prefix . "options where 1=1 and option_name LIKE '" . ( self::ALIAS . '_module' ) . "_%'";
				$results = $wpdb->get_results( $allSettingsQuery, ARRAY_A);
				// prepare the return
				$return = array();
				if( count($results) > 0 ){
					foreach ($results as $key => $value){
						//update_option( $value['option_name'], 'false' );
					}
				}
			}else{
				return 'valid_hash';
			}
		}

		private function encrypt ( $code, $sendTime=null )
		{
			// add some extra data to hash
			$register_email = get_option( self::ALIAS . '_register_email');
			$buyer = get_option( self::ALIAS . '_register_buyer');
			$item_id = get_option( self::ALIAS . '_register_item_id');
			$validation_date = !isset($sendTime) ? time() : $sendTime;

			if(!isset($sendTime)) {
				// store the date into DB, use for decrypt
				update_option( self::ALIAS . '_register_timestamp', $validation_date);
			}

			return  $validation_date . $this->key_sep .
					$register_email . $this->key_sep .
					//$this->getHost(get_option('siteurl')) . $this->key_sep .
					$buyer . $this->key_sep .
					$item_id . $this->key_sep .
					$code . $this->key_sep;
		}

		private function decrypt ( $code )
		{

		}

		private function getHost ( $url )
		{
			$__ = parse_url( $url );
			return $__['host'];
		}
	}
}