<?php 
// Revised send.php 
 
ini_set('display_errors', 1); 
error_reporting(E_ALL); 
session_start(); 
include("conf.php"); 
 
// Sanitize function 
function check($str = '') { 
    $str = trim($str); 
    $str = stripslashes($str); 
    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 
} 
 
// Collect data 
$name = (!empty($_POST['name'])) ? check($_POST['name']) : ''; 
$phone = (!empty($_POST['phone'])) ? check($_POST['phone']) : ''; 
$pixel = (!empty($_POST['pixel'])) ? check($_POST['pixel']) : ''; 
$custom1 = (!empty($_POST['custom1'])) ? check($_POST['custom1']) : ''; 
 
// ðŸŸ¢ Get clickID from POST first, fallback to GET (from index.html URL) 
$aff_click_id = (!empty($_POST['aff_click_id'])) ? check($_POST['aff_click_id']) : 
                (!empty($_GET['clickID']) ? check($_GET['clickID']) : bin2hex(random_bytes(12))); 
 
$site = $_SERVER['HTTP_HOST'] ?? null; 
$ip = $_SERVER['REMOTE_ADDR'] ?? null; 
 
try { 
    if (!empty($phone)) { 
 
        // Set fallback dynamic values 
        $aff_param1 = $site; 
        $aff_param2 = $ip; 
        $aff_param3 = $pixel; 
 
        $params = array( 
            'goal_id' => $pp_affscale_offerid, 
            'firstname' => $name, 
            'phone' => $phone, 
            'aff_click_id' => $aff_click_id, 
            'custom1' => $custom1, 
            'sub_id2' => $name, 
            'sub_id3' => $phone, 
            'sub_id4' => '', 
            'sub_id5' => '', 
            'aff_param1' => $aff_param1, 
            'aff_param2' => $aff_param2, 
            'aff_param3' => $aff_param3, 
            'aff_param4' => '', 
            'aff_param5' => '' 
        ); 
 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, 'https://tracking.affscalecpa.com/api/v2/affiliate/leads?api-key=' . $pp_affscale_apikey); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        $res = curl_exec($ch); 
 
        if ($res === false) { 
            throw new Exception('Curl error: ' . curl_error($ch)); 
        } 
 
        $response = json_decode($res); 
 
        if (!is_object($response) || !isset($response->code)) { 
            throw new Exception('Broken JSON response: ' . $res); 
        } 
 
        if ($response->code !== 200) { 
            throw new Exception('API Error: ' . $res); 
        } 
 
        file_put_contents(__DIR__ . '/order.success.log', date('Y.m.d H:i:s') . ' ' . $res . PHP_EOL, FILE_APPEND); 
        curl_close($ch); 
 
        // Redirect to success.html with name and phone 
        header('Location: success.html?pixel=' . urlencode($pixel) . '&name=' . urlencode($name) . '&phone=' . urlencode($phone)); 
        exit; 
    } else { 
        // Missing phone 
        header('Location: index.php?pixel=' . urlencode($pixel) . '&name=' . urlencode($name) . '&phone=' . urlencode($phone)); 
        exit; 
    } 
 
} catch (Exception $e) { 
    file_put_contents( 
        DIR . '/order.error.log', 
        date('Y.m.d H:i:s') . ' ' . $e->getMessage() . PHP_EOL . ($res ?? '') . PHP_EOL, 
        FILE_APPEND 
    ); 
    echo "Something went wrong: " . $e->getMessage(); // optional debug 
}