<?php
/*
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
*/

/*
    const CODE_OK = 0;
    const CODE_INVALID_USER_NAME_OR_PASSWORD = -1;
    const CODE_USER_EXISTS = -2;
    const CODE_SYSTEM_ERROR = -8;
    const ERROR_LIMIT_BY_IP = 'Same user Exist in the last 20 min with the same IP. duplicate LEAD';
*/
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');

$url = 'https://track.fintechgurus.org/api/signup/procform';
$api = '2643889w34df345676ssdas323tgc738';
$user = 'GodzMedia';
$password = 'h22Wf>0KZA';
$ai = '2958472';
$gi = '474';
$ci = '1';
$mpc_2 = '29572';
//$arr_req_uri = explode("/", $_SERVER['REQUEST_URI']);


if ($_POST) {
	
	$arr_uri_query = array();
	if(!empty($_POST['uri_query']) and $_POST['uri_query']){
		parse_str($_POST['uri_query'], $arr_uri_query);
	}

    $mpc_1 = '';

    if (!empty($_POST['subid']) and $_POST['subid']) {
        $mpc_1 = $_POST['subid'];
    } else if (array_key_exists("subid", $arr_uri_query)) {
        $mpc_1 = $arr_uri_query['subid'];
    }
    
    $mpc_3 = array_key_exists("sub1", $arr_uri_query) ? $arr_uri_query['sub1'] : '';
    $mpc_4 = array_key_exists("sub2", $arr_uri_query) ? $arr_uri_query['sub2'] : '';
    $mpc_5 = array_key_exists("sub3", $arr_uri_query) ? $arr_uri_query['sub3'] : '';
    $funnel = !empty($_POST['funnel']) ? $_POST['funnel'] : "BitKros";
    
    $data = [
        'firstname' => $_POST['fname'],
        'lastname'  => $_POST['lname'],
        'email'     => $_POST['email'],
        'password'  => "ahdSk63F",
        'phone'     => $_POST['phone'],
        'userip'    => $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? null,
        'useragent' => ($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "",
        'so'        => $funnel,
        'ai'        => $ai,
        'ci'        => $ci,
        'gi'        => $gi,
		'MPC_1'		=> $mpc_1,
		'MPC_2'		=> $mpc_2,
		'MPC_3'		=> $mpc_3,
		'MPC_4'		=> $mpc_4,
		'MPC_5'		=> $mpc_5,
    ];

    $headers = [
        'x-api-key: ' . $api,
        'x-trackbox-username: ' . $user,
        'x-trackbox-password: ' . $password,
        'Content-Type: application/json'
    ];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    $response = curl_exec($ch);

    curl_close($ch);

    $response = json_decode($response, true);

    if ($response['status'] == true) {
		
		if(!isset($response['addonData']['data']['loginURL'])){
			$response['addonData']['data']['loginURL'] = $response['data'];
		}
		
        $data = [
            'status' => 'success',
            'data'   => $response['addonData'] ?? []
        ];

        echo json_encode($data);
    } else {
        header('HTTP/1.0 403 Forbidden');
        echo json_encode($response['data']);
    }

    die();
}