<?php 
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
date_default_timezone_set('Asia/Calcutta'); 
ob_start();
include '../config.php';
include '../encode.php';

$imsisdn            = $_GET["msisdn"];
$iuserip            = $_GET["userip"];
$sub_id             = $_GET["sub_id"]; // Added by riccha on 8th april 2020
$ipackage_id        = '1686';//$_GET["package_id"]; 


$user               = aes128Encrypt("DHDUFYlinsGDDSSs", "TEFFMedia");
$password           = aes128Encrypt("DHDUFYlinsGDDSSs", "hw82640sj");
$msisdn             = aes128Encrypt("DHDUFYlinsGDDSSs", $imsisdn);
$packageid          = aes128Encrypt("DHDUFYlinsGDDSSs", $ipackage_id);
$userip             = aes128Encrypt("DHDUFYlinsGDDSSs", $iuserip);

$itoken 				= substr(md5(uniqid('', 1)), 0, 16);
$token                  = aes128Encrypt("DHDUFYlinsGDDSSs", $itoken);
        
$data   = json_encode(array(
    "msisdn"=>$msisdn,
    "password"=>$password,
    "txnid"=>"test",
    "user"=>$user,
    "packageId"=>$packageid,
    "channel"=>"web",
    "sourceIP"=>$userip,
    "adPartnerName"=>"test",
    "pubId"=>"test",
    
));
	
		

            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => "https://pt5.etisalat.ae/Moneta/pushPOSTPin.htm",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => "",
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => "POST",
              CURLOPT_POSTFIELDS => $data,
              CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
            ));

            $result = curl_exec($curl);
            curl_close($curl);
          //echo '<pre>';print_r($result);die;
		
		$msg 		= '';
		$status 	= False;
        $token      = '';
        $pinReqStatus = 0;
		
		writeResponse($result,"https://pt5.etisalat.ae/Moneta/pushPOSTPin.htm",$data);
		
        //$result = 'pin_sent|EYP1+PvJQ2Z7/O/g4DVwhSLyzndzn1UpYLUo+jW955M=';// Testing
        
        if(strpos($result,'|')){
            $resultArr  = explode('|',$result);
            $token  =  $resultArr[1];
            $result =  $resultArr[0];           
        }
        
        //echo $result."\n".$token;die;

        if($result == "Blank_Username_or_Password"){
            $msg = 'usr and pwd are blank or empty';
            
        }else if($result == "Invalid_MSISDN"){
            $msg = 'msisdn is less than or more than 12 or MSISDN is empty';
            
        }else if($result == "Blank_Packageid"){
            $msg = 'packageid id blank';
            
        }else if($result == "Authantication_Failed"){
            $msg = 'Authantication_Failed';
            
        }else if($result == "Invalid_Package"){
            $msg = 'Package id is blank';
            
        }else if($result == "pin_sent"){
            $msg    = 'Pin sent';
            $status = True;
            $pinReqStatus = 1;
            
        }else if($result == "Pin_Generation_Error_Try_Again"){
            $msg = 'Pin_Generation_Error_Try_Again';
            
        }else if($result == "UnAutharized_Package"){
            $msg = 'package id id unauthorized';
            
        }else if($result == "Invalid_MSISDN"){
            $msg = 'user is not of Etisalat telecom';
            
        }else if($result == "pin_limit_exceeded"){
            $msg = 'Number of PIN per day exceeded . Currently 3 PIN per MSISDN';
            
        }else if($result == "missing_promo_param"){
            $msg = 'promotiona param ip,pubid,ad partner name is not passed in URL';
        }
        
       // Store details by riccha at 05th april 2021
       echo $insert_qry     = "INSERT INTO `msisdn_pin_status`(`subid`,`msisdn`, `pin_req_status`, `pin_req_msg`, `created_at`, `updated_at`) VALUES ('$sub_id','$imsisdn','$pinReqStatus','$msg',NOW(),NOW())";
       die;
       $insert_qry_rs  = mysqli_query($dbLink, $insert_qry);
       // Ends
        
        
        
        if($status){
            echo json_encode(array('errorMessage'=>$msg,"token"=>$token,'response'=>'SUCCESS'));

        }else{
            echo json_encode(array('errorMessage'=>$msg,'response'=>'Fail'));
        }
            
	
	
	
	function writeResponse($response,$pinRequestApi,$data){
        $log  = "Api: ".$pinRequestApi."  data: ".$data." Response :".$response.' - '.date("F j, Y, g:i a")."\n";

		file_put_contents('log.txt', $log, FILE_APPEND);
		return;
	}
				

?>