<?php
include 'dbFunctions.php';
//include 'generate_refid.php';

//////////======= BELOW INCLUDE IS TO CHECK THE LOGIN Authentication ======= ////////
include 'mob_check_api_login.php';

//RECEIVE DATA ////////
$headers2 = getRequestLoginHeaders();
$devId = $headers2['Developerid'];
$devPass = $headers2['Developerpassword'];
$login = $headers2['X-Method-Override'];
//echo "<br>Header:" .json_decode($headers2,true);
//echo "<br>DevId:" . $devId;
//echo "<br>DeveloperPassword:" . $devPass;
//echo "<br>login:" . $login;

//CHECK THE LOGIN Authentication from database  ////////
$auth_result = checklogin($devId,$devPass, $content);

/////////// ======== LOGIN Authentication = END ======== /////////////////////////////

//$auth_result ="SUCCESS";

if ($auth_result =="FAIL") {
///// LOGIN FAIL

}
else
{
///// LOGIN SUCESS & PROCEED


//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';

//if(strcasecmp($contentType, 'application/json') != 0){
//   throw new Exception('Content type must be: application/json');
//}

////////////////////////////////////////// 
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));

//echo "<br>Content:" . $content;
//Attempt to decode the incoming RAW post data from JSON.
$decoded_resp = json_decode($content, true);

//$sourceTxnId = $externalReferenceId;
$api_name ='pdf_encrypt.php';


//$merchant_account = $decoded_resp['merchant_account']; 
$recipient_ac = $decoded_resp['recipient_ac']; 
$sender_merchant_passphrase = $decoded_resp['sender_merchant_passphrase']; 
$data_to_encrypt = $decoded_resp['data_to_encrypt']; 



          
//////////////////// MOBILENO - EMPTY
//  if ($idnum ==="" or is_null($idnum)) {
//if ($merchant_account ==="" or is_null($merchant_account)) {
if ($recipient_ac ==="") {

              $response_result = array(   
              "errorDetails" =>  array(
              "errorCode" => "3001",
              "errorDescription" => "Recipient Account - cannot be empty."
              ),
              "recipient_ac" => "$recipient_ac",
              "sender_merchant_passphrase" => "$sender_merchant_passphrase"
              );
        
         $endcode_error= json_encode($response_result);
               
       
        
        $sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac, api_name,data_received, sqlerror,data_response_mob) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$api_name','$content', '$sqlError2','$endcode_error')";
        //echo "<br>memberid:" . $sql_insert;
         
       
       $insert_data = mysql_query($sql_insert);
       
       //echo "<br>SQL:" . $sql_insert . "<br>" ;
       echo $endcode_error;
       
       exit();

}

if ($sender_merchant_passphrase ==="") {

              $response_result = array(   
              "errorDetails" =>  array(
              "errorCode" => "3002",
              "errorDescription" => "Merchant Passphrase- cannot be empty."
              ),
              "recipient_ac" => "$recipient_ac",
              "sender_merchant_passphrase" => "$sender_merchant_passphrase"
              
              );
        
         $endcode_error= json_encode($response_result);
               
       
        
        $sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac, api_name,data_received, sqlerror,data_response_mob) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$api_name','$content', '$sqlError2','$endcode_error')";
        //echo "<br>memberid:" . $sql_insert;
         
       
       $insert_data = mysql_query($sql_insert);
       
       //echo "<br>SQL:" . $sql_insert . "<br>" ;
       echo $endcode_error;
       
       exit();

}


/////////////CHECK ACCOUNT ID WITH PASSPHRASE //////////////////////


//B_URL + "?requestType=getAccountId&secretPhrase="
//{"errorDescription":"At least one of [secretPhrase, publicKey] must be specified","errorCode":3}

//include 'include_writetext.php';
//$writelogResult = writelog_encrypt($data_to_encrypt);


///////////////////////////////////////////////



$data = array(
                'requestType' => 'encryptTo',   
                'recipient' => $recipient_ac, 
                //'secretPhrase' => 'wrote perfection climb bid ode prove kid suffocate happy spend escape sort',
                'secretPhrase' => $sender_merchant_passphrase, 
                'messageToEncrypt'=> $data_to_encrypt
             
          );
          
          $postdata = json_encode($data);
          # Create a connection
           
           ///echo "<br>postdata:" . $postdata;
           ///echo "<br>data:" . $data;
           
          //$url = 'http://13.250.156.74:27876/nxt';
          
          // ARDOR SERVER
          //$url = 'http://18.140.161.52:27876/nxt';
          
          // COAL TESTNET SERVER
          $url = 'https://testcoal.coalculus.com/nxt';
          
          //$url = 'http://18.136.11.63:26876/nxt'; - 20250915 comment
          
           //$url = 'http://3.0.26.144:26876/nxt';
            
           
          // WMP SERVER
          //$url = 'http://13.250.20.111:8789/wmp';
          
          
          $ch = curl_init($url);
          # Form data string
          $postString = http_build_query($data, '', '&');
          # Setting our options
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          # Get the response
          $response = curl_exec($ch);
          $result = json_decode($response,true);
          
          curl_close($ch);
          
          
          //$bc_transactionid = $result['transactionJSON']['transaction'];
          $errDesc = $result['errorDescription'];
          $errCode = $result['errorCode'];
          
          //echo "<br>response:" . $response;
          
          //echo "<br><br>";
          
          //////////////////
          
           
       
       //requestProcessingTime
       //nonce
       //data
       
       $requestProcessingTime = $result['requestProcessingTime'];
       $nonce = $result['nonce'];
       $data = $result['data'];
         
        
       

//include 'include_writetext.php';
//$writelogResult = writelog_encrypt($sql_insert_trace);

       //echo "<br>SQL:" . $sql_insert;        
       
      //{"errorDescription":"Incorrect \"account\"","errorCode":4}
      
      //$errCode= $result->{'errorCode'};
      //echo "<br>Erro:" . $erro;
      
     /////----------------------------------


$postdata_save = substr($postdata, 0, 2000);

switch ($errCode) {
     
  case "4":
       
   $response_result = array(   
              "errorDetails" =>  array(
              "errorCode" => "5001",
              "errorDescription" => "Invalid Recipent Account"
              ),
              
               'recipient_ac' => $recipient_ac,
               "sender_merchant_passphrase" => "$sender_merchant_passphrase"
              );
        
         $en_response_result= json_encode($response_result);
               
         $sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac,data_response_mob, api_name,data_received,data_response_server,data_request_server) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$en_response_result','$api_name','$content','$response','$postdata_save')";
         
         // $sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac,data_response_mob, api_name,data_received,data_response_server) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$en_response_result','$api_name','','$response')";
          
           //echo "<br>SQL:" . $sql_insert;        
         
       $insert_data = mysql_query($sql_insert);
     
       echo $en_response_result;
      
          exit();
       break;
       
  

default:

        
}   
      
             
          
      $response_result = array(   
                                  "errorDetails" =>  array(
                                  "errorCode" => "0",
                                  "errorDescription" => "Success"
                                  ),
                                    "recipient_ac" => "$recipient_ac",
                                    "sender_merchant_passphrase" => "$sender_merchant_passphrase",
                                    "encrypted_data" => "$data",
                                    "nonce" => "$nonce"
                                    
                                    
                                  );
                  
                   
                                  
                 $en_response_result= json_encode($response_result);
                 
                 //////////////////////////////////
                 $en_response_result_save = substr($en_response_result, 0, 2000);
                 $response_save = substr($response, 0, 2000);
                 $content_save = substr($content, 0, 2000);
                 
                 
                  //$sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac, data_response_mob, api_name,data_received,data_response_server,data_request_server,rcp_ibma_ac) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$en_response_result','$api_name','','$response','$postdata','$recipient_ac')";
                  
                   $sql_insert = "INSERT INTO mob_api_received (develop_id, develop_pass, merchant_ibma_ac, data_response_mob, api_name,data_received,data_response_server,data_request_server,rcp_ibma_ac) VALUES ('$devId','$devPass', '$merchant_ibma_ac','$en_response_result_save','$api_name','$content_save','$response_save','$postdata_save','$recipient_ac')";
                  
                  
                   
                   //echo "<br>sql_insert123:" . $sql_insert;
                   
                   $insert_data = mysql_query($sql_insert);
               
                   echo $en_response_result;     

   

    
}

?>