<?php

/*

This is a system file and will be used to track user actions

like how many people opened their email, when, their georaphical location,

how many click the detail link, unsubscribe link, cannot see link etc.

Developed By: Steve Curry

May 28, 2009

*/

session_start();

require 'configuration.php';

require_once ('classes/database.php');

require_once ('classes/functions.php');

$sqlObj = new mysqlClass;

$funcObj = new funcClass;

$table = "##report";

$curFile = basename(__FILE__);



$userEmail 		= urldecode($_GET['email']); // USER EMAIL ADDRESS

$campaignID		= intval($_GET['camid']);

$friend			= intval($_GET['friend']);

$action			= urldecode($_GET['action']);



$queryString	= $_SERVER['QUERY_STRING'];

$queryStrArr	= explode("redirect=", $queryString);

$redirect		= urldecode($queryStrArr[1]);

function getIpAddress() 

{

	return (empty($_SERVER['HTTP_CLIENT_IP'])?(empty($_SERVER['HTTP_X_FORWARDED_FOR'])?

$_SERVER['REMOTE_ADDR']:$_SERVER['HTTP_X_FORWARDED_FOR']):$_SERVER['HTTP_CLIENT_IP']);

}

$userIP		= getIpAddress();

$OSList = array

(

        // Match user agent string with operating systems

        'Windows 3.11' => 'Win16',

        'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',

        'Windows 98' => '(Windows 98)|(Win98)',

        'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',

        'Windows XP' => '(Windows NT 5.1)|(Windows XP)',

        'Windows Server 2003' => '(Windows NT 5.2)',

        'Windows Vista' => '(Windows NT 6.0)',

        'Windows 7' => '(Windows NT 7.0)',

        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',

        'Windows ME' => 'Windows ME',

        'Open BSD' => 'OpenBSD',

        'Sun OS' => 'SunOS',

        'Linux' => '(Linux)|(X11)',

        'Mac OS' => '(Mac_PowerPC)|(Macintosh)',

        'QNX' => 'QNX',

        'BeOS' => 'BeOS',

        'OS/2' => 'OS/2',

        'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'

);



foreach($OSList as $CurrOS=>$Match)

{

        if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))

        {

                break;

        }

}



// You are using Windows Vista

//echo "You are using ".$CurrOS . "<br />";



require_once('classes/class.browser.php');

$br = new Browser;

$platform	= $br->Platform;

$browser	= $br->Name . ' ' . $br->Version;





$query = "INSERT INTO $table SET CAMPAIGN_ID='$campaignID', EMAIL='$userEmail', USER_IP='$userIP', BROWSER='$browser', 

OS='$CurrOS', PLATFORM='$platform', ACTION='$action', REDIRECT_URL='$redirect', IS_FRIEND = '$friend', DATE_TIME=NOW()";

$insertId = $funcObj->insertQuery($query);





switch ($action)

{

	case 'view':

		require 'visitormap.php';

		

		$ip = $userIP;

		//$ip = '190.141.47.32';

		//$ip = '113.21.32.40';

		//$ip = 'thedropzone.co.nz'; //for testing

		$userinfo = IPtoCoords($ip);

		$user = mysql_query("SELECT `location` FROM `smlr_visitor_map` WHERE `location` = '".$userinfo['location']."' AND campaign_id = '".$campaignID."'");

		if(!mysql_fetch_row($user) && $userinfo)

		{

			

			mysql_query('INSERT INTO `smlr_visitor_map` (`campaign_id`, `ip`, `location`, `longitude`, `latitude`) VALUES (' . $campaignID . ', 

			\'' . mysql_real_escape_string($ip) . '\', \'' . $userinfo['location'] . '\', ' . $userinfo['longitude'] . ', ' . $userinfo['latitude'] . ')') or die(mysql_error());

		}

		header('Content-Type: image/gif');

		readfile('images/blank.gif');

		break;

	

	case 'cantread': // it is basically view in browser option

	

		if (empty($_GET['camid']) || empty($_GET['code']))

			die ("Required parameter is missing");

		else if (sha1($_GET['camid']) != $_GET['code'])

			die ("Some of the parameters are missing. Sorry for the inconvenience.");

			

			

		$ID = intval($_GET['camid']);

		

		$query = "SELECT CONTENTS FROM ##campaigns WHERE ID = '$ID'";

		$resultSet = array();

		$resultSet = $funcObj->CustomQuery($query);

		echo $resultSet[0]->CONTENTS;

		break;

	

	case 'unsubscribe':

		

		$message = "Some required parameters are missing";

		if (!empty($userEmail) && (!empty($campaignID)) && (!empty($_GET['hash'])))

		{

			if (sha1($userEmail) == $_GET['hash'])

			{

				/*

				Remove the user from the database

				*/

				$query = "SELECT DISTINCT(ADDR_ID) AS ADDR_ID FROM ##campaign_send_stats WHERE CAMPAIGN_ID = " . intval($campaignID);

				$resultSet = array();

				$resultSet = $funcObj->CustomQueryArray($query);

				$campIds = array();

				if (count($resultSet))

				{

					foreach ($resultSet as $row)

					{

						$campIds[] = $row['ADDR_ID'];

					}

					$campIdsStr = implode(",", $campIds);

					

					/*

					Search email address only in the address book(s) used in

					this campaign.

					*/

					$query = "UPDATE smlr_contacts SET IS_DELETED = 1 WHERE ADDR_ID IN ($campIdsStr) AND EMAIL = '$userEmail' AND IS_DELETED = 0";

					$updateId = $funcObj->updateQuery($query);

					if ($updateId > 0)

					{

						$message = "Your email address has been successfully removed from our mailing list.";

					}

					else

					{

						$message = "Sorry we couldn'd find your email address in our mailing list.";

					}

					

				}

			}

		}

		if (!empty($redirect)) // if there is a redirect URL then redirect the user to that page

		{

			if (!headers_sent()) 

			{

				header("location: $redirect");

				exit;

			}

			else

			{

				echo "

				<script type='text/javascript'>

				window.location.href='$redirect';

				</script>

				";

			}

		}

		else //if there is no direct url then display a message to the user.

		{	

			echo "

			<center>

			<table cellpadding='10' style='border: 2px solid #cccccc; background-color: #f3f3f3; font-size: 14px; font-family: arial; margin: 10px;'>

			<tr>

			<td>$message</td>

			</tr>					

			</table></center>";

		}

		

		break;						

		

	default:

		if (!empty($redirect))

		{

			if (!headers_sent()) 

			{

				header("location: $redirect");

				exit;

			}

			else

			{

				echo "

				<script type='text/javascript'>

				window.location.href='$redirect';

				</script>

				";

			}

		}	

		break;

}

?>