<?php
	$base_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); 
	$current_url = Mage::helper('core/url')->getCurrentUrl();

	$ipaddress     = "";

    if (getenv("HTTP_CLIENT_IP")) $ipaddress = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR")) $ipaddress = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("HTTP_X_FORWARDED")) $ipaddress = getenv("HTTP_X_FORWARDED");
    else if(getenv("HTTP_FORWARDED_FOR")) $ipaddress = getenv("HTTP_FORWARDED_FOR");
    else if(getenv("HTTP_FORWARDED")) $ipaddress = getenv("HTTP_FORWARDED");
    else if(getenv("REMOTE_ADDR")) $ipaddress = getenv("REMOTE_ADDR");
    else $ipaddress = "UNKNOWN";
    $affliate_cookie = (isset($_GET['af_id']) ? $_GET['af_id'] : (isset($_COOKIE["af_id"]) ? $_COOKIE["af_id"] : '') ); 

    $product = Mage::registry('current_product');
	$id = $product->getId();

	$affiliateData = array(
		"product_id"       => $id,
		"af_id"            => $affliate_cookie,
		"ip"               => $ipaddress,
		"base_url"         => base64_encode($base_url),
		"current_page_url" => base64_encode($current_url),
		"script_name"      => "magento",
	);

	$request_headers = array();
	$request_headers[] = 'User-Agent: '. (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
	$request_headers[] = 'Referer: '. $current_url;

	$request = http_build_query($affiliateData);
	$curl = curl_init('__baseurl__integration/addClick?'.$request);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	
	curl_exec($curl);