<?php

include("inc.all.php");
$q2 = new CDb();
$q3 = new CDb();

$member_id = $_GET['member_id'];
$q->query("SELECT * FROM members WHERE id='$member_id'");
$q->next_record();
$member_email = get_setting('emailing_from_email');
$member_name = get_setting('emailing_from_name');    	  

$q2->query("SELECT id, name, email, date_click, aff FROM track WHERE crypted_email='".$_GET['email']."' and camp_id='".$_GET['campaign']."'");
$q2->next_record();
$friend_name = $q2->f('name');


$q->query("UPDATE track SET date_click=NOW(), ip='".getenv('REMOTE_ADDR')."' WHERE crypted_email='".$_GET['email']."' AND camp_id='".$_GET['campaign']."'");
$q->query("SELECT click_email_body, send_click_notification, click_email_subject ,link, enable_link_tracking, website_name FROM campaigns WHERE id='".$_GET['campaign']."'");
$q->next_record();

if ($q2->f('date_click') == '0000-00-00 00:00:00'){
	if ($q->f("enable_link_tracking") && $q->f("send_click_notification")){
		$q2->query("SELECT name, email, date_click, aff FROM track WHERE crypted_email='".$_GET['email']."' and camp_id='".$_GET['campaign']."'");
		$q2->next_record();
		$q3->query("SELECT name, email FROM track WHERE id='".$q2->f("aff")."'");
		$q3->next_record();

		$message = $q->f("click_email_body");
		$message = str_replace("[[sitename]]", $q->f("website_name"),$message);
		$message = str_replace("[[your_name]]", $q3->f("name"),$message);
		$message = str_replace("[[friend_name]]", $friend_name,$message);
		$message = str_replace("[[link]]", $q->f("link"),$message);
		
		$subject = $q->f("click_email_subject");
		$subject = str_replace("[[your_name]]", $q3->f("name"),$subject);
		$message = str_replace("[[admin_profile_name]]", $member_name,$message);
		$headers .= "From: ".$member_name." <".$member_email.">";
		mail($q3->f("name")." <".$q3->f("email").">",$subject,$message,$headers);
	}
}

	preg_match("'^http.*'",$q->f("link"),$match);
	$match[0] ? header ("Location: ".$q->f("link")) : header ("Location: http://".$q->f("link"));
//header ("Location: ".$q->f("link"));
?>