<?php

include "config.php";

/* check the security code sent with the post to ensure that the post came from SafePay */

if ($moneybookers_email == $_REQUEST ['pay_to_email'] and $moneybookers_id == $_REQUEST ['merchant_id'] and $_REQUEST ['status'] == 2) {

	$userid = $_REQUEST ['userid'];

	$item = $_REQUEST ['itemname'];
	$amount = $_REQUEST ['amount'];
	$quantity = $_POST ['iquantity'];

	$query = "select * from members where userid='" . $userid . "'";

	$result = mysql_query ( $query ) or

	die ( "Query failed" );

	$numrows = @ mysql_num_rows ( $result );

	if ($numrows == 1) {

		$user = mysql_fetch_array ( $result );
		if ($item != $sitename . " Membership " . $userid and $item != $sitename . " One-Time Offer") {
			buycommission ( $user ['referid'], $amount );
			$freecommission = 0;
			$procommission = 0;
		}

		if ($item == $sitename . " One-Time Offer") {

			$projvcom = 0;
			$jvjvcom = 0;
			$superjvjvcom = 0;
			$prosupercom = 0;
			$jvsupercom = 0;
			$superjv2supercom = 0;

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - OTO','" . time () . "','$amount\$')" );

			$sql = mysql_query ( "SELECT * FROM oto LIMIT 1" );

			$oto = mysql_fetch_array ( $sql );

			otocommission ( $user ['referid'], $userid, $oto );

			if ($oto ['points'])
				mysql_query ( "UPDATE members SET points=points+" . $oto ['points'] . " WHERE userid='" . $userid . "'" );

			if ($oto ['banner_num'] and $oto ['banner_views']) {

				$count = $oto ['banner_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $oto ['banner_views'] . "', '0')" );

					$count --;

				}

			}
			if ($oto ['button_num'] and $oto ['button_views']) {

				$count = $oto ['button_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $oto ['button_views'] . "', '0')" );

					$count --;

				}

			}

			if ($oto ['hotlink_num'] and $oto ['hotlink_views']) {

				$count = $oto ['hotlink_num'];

				while ( $count > 0 ) {

					mysql_query ( "insert into hotlinks (userid,max) values('$userid','" . $oto ['hotlink_views'] . "')" );

					$count --;

				}

			}
			if ($oto ['hheadlinead_num'] and $oto ['hheadlinead_views']) {
				$count = $oto ['hheadlinead_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into hheadlineads (userid,max,purchase) values('$userid','" . $oto ['hheadlinead_views'] . "',NOW())" );
					$count --;
				}
			}
			if ($oto ['hheaderad_num'] and $oto ['hheaderad_views']) {
				$count = $oto ['hheaderad_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into hheaderads (userid,max,purchase) values('$userid','" . $oto ['hheaderad_views'] . "',NOW())" );
					$count --;
				}
			}
			if ($oto ['traffic_num'] and $oto ['traffic_views']) {

				$count = $oto ['traffic_num'];

				while ( $count > 0 ) {

					mysql_query ( "insert into tlinks (userid,paid) values('$userid','" . $oto ['traffic_views'] . "')" );

					$count --;

				}

			}

			if ($oto ['ptc_num'] and $oto ['ptc_views']) {

				$count = $oto ['ptc_num'];

				while ( $count > 0 ) {

					mysql_query ( "insert into ptcads (userid,paid) values('$userid','" . $oto ['ptc_views'] . "')" );

					$count --;

				}

			}

			if ($oto ['login_num'] and $oto ['login_views']) {

				$count = $oto ['login_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into loginads (userid,max) values('$userid','" . $oto ['login_views'] . "')" );
					$count --;
				}
			}

			if ($oto ['solo_num']) {

				$count = $oto ['solo_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

					$count --;

				}

			}

			if ($oto ['topnav_num']) {

				$count = $oto ['topnav_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO `topnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

					$count --;
				}
			}

			if ($oto ['botnav_num']) {

				$count = $oto ['botnav_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO `botnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

					$count --;
				}
			}

			if ($oto ['psolo_num']) {

				$count = $oto ['psolo_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `supersolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

					$count --;

				}

			}

			if ($oto ['upgrade_pro'] == "1") {
				upgrade_jv ( $userid );
			}

			if ($oto ['upgrade_pro'] == "2") {
				upgrade_superjv ( $userid );
			}

		} elseif ($item == $sitename . " Points " . $userid) {

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - 1000 points','" . time () . "','$amount\$')" );

			mysql_query ( "UPDATE members SET points=points+1000 WHERE userid = '" . $userid . "'" );

		} elseif ($item == $sitename . " Solo Ad " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Solo Ad','" . time () . "','$amount\$')" );

			mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

		} elseif ($item == $sitename . " Banner Impressions " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Banner Impressions','" . time () . "','$amount\$')" );

			mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '1000', '0')" );

		} elseif ($item == $sitename . " Button Ad " . $userid) {

			mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '1000', '0')" );

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookerspayment - Button Banner Ad','" . time () . "','$amount\$')" );

		} elseif ($item == $sitename . " Solo Footer Ad " . $userid) {

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - " . $quantity . " solo footer ad','" . time () . "','$payment_amount\$')" );

			while ( $quantity > 0 ) {
				mysql_query ( "insert into footerads (userid,paid) values('$userid','100')" );
				$quantity --;
			}

		} elseif ($item == $sitename . " Top Navigation Link " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Top Navigation link','" . time () . "','$amount\$')" );

			while ( $quantity > 0 ) {
				mysql_query ( "INSERT INTO `topnav` ( `userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );
				$quantity --;
			}

		} elseif ($item == $sitename . " Bottom Navigation Link " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Bottom Navigation link','" . time () . "','$amount\$')" );

			while ( $quantity > 0 ) {
				mysql_query ( "INSERT INTO `botnav` ( `userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );
				$quantity --;
			}

		} elseif ($item == $sitename . " Hot Headline Ad " . $userid) {
			$hheadlineadmaxhits = $_POST ['hheadlinead_num'];
			mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Moneybookers payment - Hot Headline Ad $hheadlineadmaxhits Impressions','" . time () . "','$amount\$')" );
			while ( $quantity > 0 ) {
				mysql_query ( "insert into hheadlineads (userid,max,purchase) values('$userid','$hheadlineadmaxhits',NOW())" );
				$quantity --;
			}
		} elseif ($item == $sitename . " Hot Header Ad " . $userid) {
			$hheaderadmaxhits = $_POST ['hheaderad_num'];
			mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Moneybookers payment - Hot Header Ad $hheaderadmaxhits Clicks','" . time () . "','$amount\$')" );
			while ( $quantity > 0 ) {
				mysql_query ( "insert into hheaderads (userid,max,purchase) values('$userid','$hheaderadmaxhits',NOW())" );
				$quantity --;
			}
		} elseif ($item == $sitename . " Login Ad " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Login ad','" . time () . "','$amount\$')" );

			mysql_query ( "insert into loginads (userid,max) values('$userid','1000')" );

		} elseif ($item == $sitename . " Traffic link 50 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Traffic link 50','" . time () . "','$amount\$')" );

			mysql_query ( "insert into tlinks (userid,paid) values('$userid','50')" );

		} elseif ($item == $sitename . " Traffic link 100 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Traffic link 100','" . time () . "','$amount\$')" );

			mysql_query ( "insert into tlinks (userid,paid) values('$userid','100')" );

		} elseif ($item == $sitename . " Traffic link 200 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment -  Traffic link 200','" . time () . "','$amount\$')" );

			mysql_query ( "insert into tlinks (userid,paid) values('$userid','200')" );

		} elseif ($item == $sitename . " Daily Bonus " . $userid) {

			$rented = $_REQUEST ['rented'];
			mysql_query ( "insert into dailybonus (userid,rented) values('$userid','$rented')" );

			mysql_query ( "INSERT INTO transactions VALUES ('" . $userid . "','Moneybookers payment - Daily Bonus $rented','" . time () . "','$amount\$')" );

			buycommission ( $info ['referid'], $amount );

		} elseif ($item == $sitename . " PTC 1 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - PTC 50','" . time () . "','$amount\$')" );

			mysql_query ( "insert into ptcads (userid,paid) values('$userid','50')" );

		} elseif ($item == $sitename . " PTC 2 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - PTC 100','" . time () . "','$amount\$')" );

			mysql_query ( "insert into ptcads (userid,paid) values('$userid','100')" );

		} elseif ($item == $sitename . " PTC 3 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment -  PTC 200','" . time () . "','$amount\$')" );

			mysql_query ( "insert into ptcads (userid,paid) values('$userid','200')" );

		} elseif ($item == $sitename . " Hot Link " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Moneybookers payment - Hot Link','" . time () . "','$amount\$')" );

			mysql_query ( "insert into hotlinks (userid,max) values('$userid','5000')" );

		} elseif ($item == $sitename . " Hot Link Pack 2 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Moneybookers payment - Hot Link 2','" . time () . "','$amount\$')" );

			mysql_query ( "insert into hotlinks (userid,max) values('$userid','10000')" );

		} elseif ($item == $sitename . " Hot Link Pack 3 " . $userid) {
			mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Moneybookers payment - Hot Link 3','" . time () . "','$amount\$')" );

			mysql_query ( "insert into hotlinks (userid,max) values('$userid','20000')" );

		} elseif ($item == $sitename . " Membership " . $userid) {

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - JV Member Upgrade','" . time () . "','$amount\$')" );

			upgrade_jv ( $userid );

		} elseif ($item == $sitename . " SUPER JV Membership " . $userid) {

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - SUPER JV  Upgrade','" . time () . "','$amount\$')" );

			upgrade_superjv ( $userid );

		} elseif ($item == $sitename . " Special Offer") {

			mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Special Offer','" . time () . "','$amount\$')" );

			$sql = mysql_query ( "SELECT * FROM offerpage LIMIT 1" );

			$offer = mysql_fetch_array ( $sql );

			if ($offer ['points'])
				mysql_query ( "UPDATE members SET points=points+" . $offer ['points'] . " WHERE userid='" . $userid . "'" );

			if ($offer ['banner_num'] and $offer ['banner_views']) {

				$count = $offer ['banner_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $offer ['banner_views'] . "', '0')" );

					$count --;

				}

			}

			if ($offer ['button_num'] and $offer ['button_views']) {

				$count = $offer ['button_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $offer ['button_views'] . "', '0')" );
					$count --;
				}
			}

			if ($offer ['traffic_num'] and $offer ['traffic_views']) {

				$count = $offer ['traffic_num'];

				while ( $count > 0 ) {

					mysql_query ( "insert into tlinks (userid,paid) values('$userid','" . $offer ['traffic_views'] . "')" );

					$count --;

				}

			}

			if ($offer ['login_num'] and $offer ['login_views']) {

				$count = $offer ['login_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into loginads (userid,max) values('$userid','" . $offer ['login_views'] . "')" );
					$count --;
				}
			}
			if ($offer ['hotlink_num'] and $offer ['hotlink_views']) {

				$count = $offer ['hotlink_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO hotlinks (userid,max,purchase) values('$userid','" . $offer ['hotlink_views'] . "','" . time () . "')" );
					$count --;
				}
			}
			if ($offer ['solo_num']) {

				$count = $offer ['solo_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

					$count --;

				}

			}

			if ($offer ['hheadlinead_num'] and $offer ['hheadlinead_views']) {
				$count = $offer ['hheadlinead_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into hheadlineads (userid,max,purchase) values('$userid','" . $offer ['hheadlinead_views'] . "',NOW())" );
					$count --;
				}
			}
			if ($offer ['hheaderad_num'] and $offer ['hheaderad_views']) {
				$count = $offer ['hheaderad_num'];
				while ( $count > 0 ) {
					mysql_query ( "insert into hheaderads (userid,max,purchase) values('$userid','" . $offer ['hheaderad_views'] . "',NOW())" );
					$count --;
				}
			}

			if ($offer ['ptc_num'] and $offer ['ptc_views']) {

				$count = $offer ['ptc_num'];

				while ( $count > 0 ) {

					mysql_query ( "insert into ptcads (userid,paid) values('$userid','" . $offer ['ptc_views'] . "')" );

					$count --;

				}

			}

			if ($offer ['topnav_num']) {

				$count = $offer ['topnav_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO `topnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

					$count --;
				}
			}

			if ($offer ['botnav_num']) {

				$count = $offer ['botnav_num'];
				while ( $count > 0 ) {
					mysql_query ( "INSERT INTO `botnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

					$count --;
				}
			}

			if ($offer ['psolo_num']) {

				$count = $offer ['psolo_num'];

				while ( $count > 0 ) {

					mysql_query ( "INSERT INTO `supersolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

					$count --;

				}

			}

			if ($offer ['upgrade_pro'] == "1") {
				upgrade_jv ( $userid );
			}

			if ($offer ['upgrade_pro'] == "2") {
				upgrade_superjv ( $userid );
			}

			elseif ($item == $sitename . " Special Offer Ad Package") {

				mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Special Offer','" . time () . "','$amount\$')" );

				$sql = mysql_query ( "SELECT * FROM offerpage LIMIT 1" );

				$offer = mysql_fetch_array ( $sql );

				if ($offer ['points'])
					mysql_query ( "UPDATE members SET points=points+" . $offer ['points'] . " WHERE userid='" . $userid . "'" );

				if ($offer ['banner_num'] and $offer ['banner_views']) {

					$count = $offer ['banner_num'];

					while ( $count > 0 ) {

						mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $offer ['banner_views'] . "', '0')" );

						$count --;

					}

				}

				if ($offer ['button_num'] and $offer ['button_views']) {

					$count = $offer ['button_num'];
					while ( $count > 0 ) {
						mysql_query ( "INSERT INTO `buttons` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '" . $offer ['button_views'] . "', '0')" );
						$count --;
					}
				}
				if ($offer ['traffic_num'] and $offer ['traffic_views']) {

					$count = $offer ['traffic_num'];

					while ( $count > 0 ) {

						mysql_query ( "insert into tlinks (userid,paid) values('$userid','" . $offer ['traffic_views'] . "')" );

						$count --;

					}

				}

				if ($offer ['login_num'] and $offer ['login_views']) {

					$count = $offer ['login_num'];
					while ( $count > 0 ) {
						mysql_query ( "insert into loginads (userid,max) values('$userid','" . $offer ['login_views'] . "')" );
						$count --;
					}
				}

				if ($offer ['hotlink_num'] and $offer ['hotlink_views']) {

					$count = $offer ['hotlink_num'];
					while ( $count > 0 ) {
						mysql_query ( "INSERT INTO hotlinks (userid,max,purchase) values('$userid','" . $offer ['hotlink_views'] . "','" . time () . "')" );
						$count --;
					}
				}
				if ($offer ['solo_num']) {

					$count = $offer ['solo_num'];

					while ( $count > 0 ) {

						mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

						$count --;

					}

				}

				if ($offer ['hheadlinead_num'] and $offer ['hheadlinead_views']) {
					$count = $offer ['hheadlinead_num'];
					while ( $count > 0 ) {
						mysql_query ( "insert into hheadlineads (userid,max,purchase) values('$userid','" . $offer ['hheadlinead_views'] . "',NOW())" );
						$count --;
					}
				}
				if ($offer ['hheaderad_num'] and $offer ['hheaderad_views']) {
					$count = $offer ['hheaderad_num'];
					while ( $count > 0 ) {
						mysql_query ( "insert into hheaderads (userid,max,purchase) values('$userid','" . $offer ['hheaderad_views'] . "',NOW())" );
						$count --;
					}
				}

				if ($offer ['ptc_num'] and $offer ['ptc_views']) {

					$count = $offer ['ptc_num'];

					while ( $count > 0 ) {

						mysql_query ( "insert into ptcads (userid,paid) values('$userid','" . $offer ['ptc_views'] . "')" );

						$count --;

					}

				}

				if ($offer ['topnav_num']) {

					$count = $offer ['topnav_num'];
					while ( $count > 0 ) {
						mysql_query ( "INSERT INTO `topnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

						$count --;
					}
				}

				if ($offer ['botnav_num']) {

					$count = $offer ['botnav_num'];
					while ( $count > 0 ) {
						mysql_query ( "INSERT INTO `botnav` (`userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );

						$count --;
					}
				}

				if ($offer ['psolo_num']) {

					$count = $offer ['psolo_num'];

					while ( $count > 0 ) {

						mysql_query ( "INSERT INTO `supersolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );

						$count --;

					}

				}

				if ($offer ['upgrade_pro'] == "1") {
					upgrade_jv ( $userid );
				}

				if ($offer ['upgrade_pro'] == "2") {
					upgrade_superjv ( $userid );
				}
				if ($item == $sitename . " Full Page Login Ad " . $userid) {

					$rented = $_REQUEST ['rented'];

					mysql_query ( "insert into fullloginads (userid,rented) values('$userid','$rented')" );

					mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Moneybookers payment - Full Page Login Ad','" . time () . "','$amount\$')" );

				}

			}

		}

	}

}

?>