<?php


function CreateTasaCambio($directory,$monedas)
{
   
   require_once ($directory.'/configOPFGlobal.php');

   $OPFGlobal = new OPFGlobal();
   $OPFGlobal->initOPF();

   $dataWS = array();

   foreach ($monedas as $key => $value) {
      $dataWS[$key] = $value;
   }
   
   ob_start();
   $OPFGlobal->crearByWS('Tasascambio', $dataWS,'TasasCambioWS.log',false,true);
   $output = ob_get_clean(); // Captura la salida y limpia el búfer

   if ($output <> "") {
      throw new Exception($output);   
   }

}

function checkAuth()
{

   $privateKey = "9f6e63406f65667f";

   $headers = getallheaders();

   if (array_key_exists('Authorization', $headers)) {
      $token = $headers['Authorization'];
   }else{
      http_response_code(401);
      throw new Exception("Unauthorized");
   }

   $publicKey = hash("sha256", $privateKey);

   $message = date('Y-m-d');

   $signature = hash_hmac("sha256", $message, $privateKey); 

    if ($token <> "Bearer ".$signature) {
       http_response_code(401);
       throw new Exception("Unauthorized");
    }

}


$Response = new stdClass();
$Response->success = 0;


try {

   checkAuth();

   CreateTasaCambio($_POST['path'],json_decode($_POST['tasas']));
   $Response->success = 1;
   
} catch (Exception $e) {
    $Response->message = $e->getMessage();
}

echo json_encode($Response);

?>
