<?php
/* FXH SIGNAL RECEIVER — FINAL V3 */

require_once __DIR__."/../config.php";

function fxh_post(){
    if(!empty($_POST)) return $_POST;
    $raw = file_get_contents("php://input");
    parse_str($raw,$out);
    return $out;
}
$d = fxh_post();
if (empty($d)) $d = $_GET;
if (empty($d)) { echo "NO_DATA"; exit; }

// ----------------------
// Extract
// ----------------------
$enc  = $conn->real_escape_string($d["license_key"] ?? "");
$raw  = $conn->real_escape_string($d["raw_signal"] ?? "");
$symbol = $conn->real_escape_string($d["symbol"] ?? "");
$action = $conn->real_escape_string($d["action"] ?? "");
$tf     = $conn->real_escape_string($d["timeframe"] ?? "");

// ----------------------
// DECRYPT LICENSE KEY
// ----------------------
$plain = $enc;
$q = $conn->query("SELECT decrypted_key FROM fxh_license_map WHERE encrypted_key='$enc' LIMIT 1");
if ($q && $q->num_rows > 0) {
    $plain = $q->fetch_assoc()['decrypted_key'];
}

// ----------------------
$sql = "
INSERT INTO fxh_signal_logs
(license_key, raw_signal, symbol, action, timeframe)
VALUES
('$plain','$raw','$symbol','$action','$tf')
";

file_put_contents(__DIR__."/debug_post_signal_sql.txt",$sql."\n\n",FILE_APPEND);

if($conn->query($sql)) echo "OK";
else echo "SQL_ERROR";
?>
