<?php
session_start();

if ($_SESSION["valid"] == "true") {
	$valid_count = $_SESSION["validcount"];    
	$_SESSION["validcount"] = $valid_count + 1;  
	} else {
	$_SESSION["valid"] = "true";
	$_SESSION["validcount"] = "1";
	$_SESSION["timeout"] = time() + 36000;
}

$api_key = "adcb3f3210";
$publisher_id = "2596";
$offer_id = "1337";

$publisher_sub_id = $_SESSION["pub_sub_id"];
$publisher_order_id = $_SESSION["publisher_order_id"] ;
$click_id = $_SESSION["click_id"];
$extra_id_1 = $_SESSION["extra_id_1"];
$extra_id_2 = $_SESSION["extra_id_2"];
$stream_id = $_SESSION["stream_id"];

//// for keitaro:
// $click_id = $_POST["click_id"];
$api_url = "https://api.ezaff.com/send";

function onlyaz($value) {
$result = preg_replace("/[^\s\w_:,-.]+/u", "", $value);
return $result;
}

function getIPAddress() 
	{
		foreach (array("HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "REMOTE_ADDR","HTTP_CF_CONNECTING_IP") as $key) {
			if (array_key_exists($key, $_SERVER) === true) {
				foreach (explode(",", $_SERVER[$key]) as $ip) {
					if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
						return $ip;
					}
				}
			}
		}
	}
	
$post = array(
		"offer_id" => $offer_id,
		"publisher_id" => $publisher_id,
		"api_key" => $api_key,
		"name" => onlyaz($_POST["name"]),
		"phone" =>onlyaz($_POST["phone"]),	
		"email" => "",
		"click_id" => $click_id,
		"publisher_sub_id" => $publisher_sub_id ,
		"publisher_order_id" => $publisher_order_id,
		"extra_id_1" => $extra_id_1,
		"extra_id_2" => $extra_id_2,
		"stream_id" => $stream_id,
		"extra_note" => "",
		"city" => "",
		"post_code" => "",
		"country" => "AZ",
		"address" => "",
		"state" => "",
		"district" => "",
		"quantity" => "1",
		"price" => "39",
		"price_currency" => "AZN",
		"client_ip" => getIPAddress(),
		"order_note" => "",
		"bundle_option" => "",
		"payment_method" => "",
		"browser_agent" => "",
		"ref_url" => $_SERVER["HTTP_HOST"],
		"product_id" => ""
);

if ( !empty($_POST["name"]) && !empty($_POST["phone"]) && ($_SESSION["validcount"] < "4")  ) {
	$ch = curl_init($api_url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	$response = curl_exec($ch);
	curl_close($ch);
	$obj = json_decode($response, true);
		if ( $obj["status"] == "success" ) {
			header("location: success.php");
		} else {
			echo "<body onload='history.go(-1);'>";    	
		}	
} else {
	echo "<body onload='history.go(-1);'>";
}	
?>
			