<?



include "config.php";



$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {

$value = urlencode(stripslashes($value));

$req .= "&$key=$value";

//Remove this line after you have debugged

//print $req;

}



// post back to PayPal system to validate

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);



// assign posted variables to local variables

$payment_status = $_POST['payment_status'];

$payment_amount = $_POST['mc_gross'];

$payment_currency = $_POST['mc_currency'];

$quantity = $_POST['quantity'];

$txn_id = $_POST['txn_id'];

$receiver_email = $_POST['receiver_email'];

$payer_email = $_POST['payer_email'];

$userid = $_POST['option_selection1'];

$item = $_POST['item_name'];


// email header

$em_headers  = "From: $receiver_email\n";		

$em_headers .= "Reply-To: $receiver_email\n";

$em_headers .= "Return-Path: $receiver_email\n";



if (!$fp) {

// HTTP ERROR

} else {

fputs ($fp, $header . $req);

while (!feof($fp)) {

$res = fgets ($fp, 1024);

if (strcmp ($res, "VERIFIED") == 0) {

print "VERIFIED";

// check the payment_status is Completed

// check that txn_id has not been previously processed

// check that receiver_email is your Primary PayPal email

// check that payment_amount/payment_currency are correct

// process payment



	if ($receiver_email == $paypal AND $payment_status == "Completed") {



		$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);
			buycommission($user['referid'], $payment_amount);
			$freecommission=0;
			$procommission=0;


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

			$offer = mysql_fetch_array($sql);
			$oto = $offer;
			
	$item2 = str_replace($sitename." Special Offer #1", "", $item);
	$item2 = substr($item2,0,-(strlen($userid)+3));
	
	
			mysql_query("INSERT INTO transactions VALUES ('id','".$userid."','Paypal payment - Special Offer #1 ".$item2."','".time()."','$payment_amount\$')");	
	
	if(strlen($item2)>1) {
		$item2 = substr($item2,3);
		
		$item3 = explode("/ ", $item2);
		foreach($item3 as $each) {
			$upgrade = explode(" ", $each);
			
			if($upgrade[2]==1) {
				mysql_query("UPDATE members SET points=points+'".$upgrade[0]*$oto['upgrade1_points']."' WHERE userid='$userid'");
			} elseif($upgrade[2]==2) {
			
				$count = $upgrade[0]*$oto['upgrade2_solos'];

				while($count > 0) {

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

					$count--;

				}
				
			} elseif($upgrade[2]==3) {
			
				$count = $upgrade[0]*$oto['upgrade3_banners'];

				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['upgrade3_views']."', '0')");

					$count--;

				}	

			} elseif($upgrade[2]==4) {
			
				$count = $upgrade[0]*$oto['upgrade4_traffic'];

				while($count > 0) {

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

					$count--;

				}	
				
			} elseif($upgrade[2]==5) {
			
				$count = $upgrade[0]*$oto['upgrade5_login'];

				while($count > 0) {

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

					$count--;

				}					
			}
			
		}
		
		
	}
			

			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['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['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($oto['ssolo_num']) {

			

				$count = $oto['ssolo_num'];

				while($count > 0) {

					mysql_query("INSERT INTO `supersolos` (userid, site) VALUES ('$userid','paidflirts')");

					$count--;

				}

			}

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

		

		} else {

		//Error

		

		$message = "User can't be found\n\nAmount: $payment_amount\nPayer email: $payer_email\nUser ID: $userid";

		mail($paypal_email, "Invalid order", $message, $em_headers);

		

		}

	

	}





}

else if (strcmp ($res, "INVALID") == 0) {

// log for manual investigation

print "NOT VERIFIED";



}

}

fclose ($fp);

}



mysql_close();







?>