<?php
class outright_security{

	var $api_url;
var $illegal_use;

					function __construct($prod_id=false){

						$this->api_url ="https://apistore.outrightcrm.com/service/v4_1/rest.php";						
						 $sql ="select * from outr_outr_licence where deleted =0 and name ='".$prod_id."' ";
						$cc =outright_run_sql_one_row($sql);
						$rr = json_encode($cc);
						$this->prod_bean = json_decode($rr);
						$this->prod_bean->lic_key = $this->prod_bean->licence_key;

							if( $this->prod_bean->verification_status =='verified' && $this->outright_license_to_verify() ){
								$this->do_validate();
							}
					}
                function do_validate(){
					global $sugar_config;
		
				$verify_params =array();
				$verify_params['session'] = $this->prod_bean->licence_key;
				$verify_params['domain_name'] = $sugar_config['site_url'];
				$verify_params['lic_key'] = $this->prod_bean->licence_key;
				$verify_params['old_key'] = $this->prod_bean->licence_key;
				$verify_params['last_verify_date'] = $this->prod_bean->licence_key;
				$verify_params['last_verify_status'] = $this->prod_bean->licence_key;
				$verify_params['action'] = 'verify';



				$final_result = outright_rest_call($this->api_url,'outright_license_verify',$verify_params);
				if($final_result->status =='success'){
							$this->prod_bean->verification_status='verified';
							$this->prod_bean->signature=$final_result->signature;
							}
							else{
							$this->prod_bean->verification_status='failed';
							}
							$this->prod_bean->description=$final_result->msg;
						 $sql ="update outr_outr_licence set verification_status ='".$this->prod_bean->verification_status."',status='active' where id ='".$this->prod_bean->id."' ";

						$res = outright_run_sql_one_row($sql);

				}
				 function do_cancel(){
					global $sugar_config;
					$verify_params =array();
					$verify_params['session'] = $this->prod_bean->licence_key;
					$verify_params['domain_name'] = $sugar_config['site_url'];
					$verify_params['lic_key'] = $this->prod_bean->licence_key;
					$verify_params['old_key'] = $this->prod_bean->licence_key;
					$verify_params['last_verify_date'] = $this->prod_bean->licence_key;
					$verify_params['last_verify_status'] = $this->prod_bean->licence_key;
					$verify_params['action'] = 'cancel';			
	
					return outright_rest_call($this->api_url,'outright_license_verify',$verify_params);
				}

				function outright_break_lic_key($lic_key){
				$lic_key_prefix =explode('-',$lic_key);
				return $lic_key_prefix[0];
				}


				function replace_final_msg($msg){
				$rep_arr =array(
				'uu' =>'You',
				'rr' =>'Are',
				'vtm' =>'Victim',
					'pls' =>'Please',
					'vrfy' =>'Verify',
					'frst' =>'First',
				);
						foreach($rep_arr as $target_words=>$target){
						$msg =str_replace($target_words,$target,$msg);
						}
				return $msg;
				}

				function outright_license_to_verify(){
					outright_error_log('verify_license::Verification started','outr_product_logger');

					$time_now = strtotime('now');
				$sql =" select date_modified from outr_outr_licence where id='".$this->prod_bean->id."' order by date_entered desc limit 1";
				$row =outright_run_sql_one_row($sql);
				if($time_now -strtotime($row['date_modified']) > 86400){
				return true;
				}
				return false;
				}
}

function outright_lic( $key ){
			$ors_security = new outright_security(false,'outright_email_to_lead');
		if($ors_security->prod_bean->verification_status !='verified'){
			echo '<h1><a href="index.php?module=outr_outr_licence&action=index">Hey,Please Validate your License before enjoying this extension!!!</a></h1>';
			die;
		}
}

function outright_product_license_check( $product_key, $do_die = false){
		$ors_security = new outright_security($product_key);
		if($ors_security->prod_bean->verification_status !='verified'){
			if($do_die){
				echo '<h1><a href="index.php?module=outr_outr_licence&action=index">Hey,Please Validate your License before enjoying this extension!!!</a></h1>';
			die;
			}
		return false;			
		}
		return true;
}

function outright_product_step2_check( $product_configuration_table){
	$sql ="select * from ".$product_configuration_table." where deleted = 0";
	$result =outright_run_sql($sql);
	return $result;
}

function outright_activate_license($product_key){
	global $db;
	$sql ="select * from outr_outr_licence where deleted =0 and name ='".$product_key."' ";
			$row = $db->query($sql);
	$res = $db->fetchByAssoc( $row );
			if( !$res ){
			$id = create_guid();
			$aa =	"INSERT INTO outr_outr_licence (id, name) VALUES ('".$id."' ,'".$product_key."')";
			$db->query( $aa );
			}
	$ors_security = new outright_security( $product_key );
	$ors_security->do_validate();
	return $ors_security;
}