<?php
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$headers = getallheaders();
$data = json_decode(file_get_contents('php://input'), true);
$message = http_build_query($data);
//$_req_key = "0703dcf6d434f48bdaceff9af5bd0a6d";
$_req_key = $_ENV['SECRET'];
foreach($headers as $_key=>$val){
 if(strtolower($_key) == strtolower("X-CF-Auth-Bridge-Access") && $val){   $key = $val; }
 if(strtolower($_key) == strtolower("X-CF-Auth-Bridge-Key") && $val == $_req_key){ $reqKey = $val;}
}

if(!isset($key) || !isset($reqKey)){header("HTTP/1.1 401 Unauthorized");
    exit;echo json_encode(["request_id" => bin2hex(openssl_random_pseudo_bytes(16)), "error" => true]); die();}

try {
        $cipher = "AES-128-CBC";
        $iv = "lksdasfsaskllopl";
        $ciphertext_raw = openssl_encrypt($message, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv);
        $hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
        $value = base64_encode($iv . $hmac . $ciphertext_raw);

        echo $value;
    }catch (Exception $e){
        if(!isset($key)){echo json_encode(["request_id" => bin2hex(openssl_random_pseudo_bytes(32)), "error" => true]);}
    }
