<?

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.1\r\n";
$header .= "Host: www.paypal.com\r\n";
//$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);



// assign posted variables to local variables
$item = $_POST['item_name'];
//$item_name = $_POST['item_name'];
//$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$txn_type = $_POST['txn_type'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$userid = $_POST['option_selection1'];
$pdoto = $_POST['option_selection2'];
$option_name1 = $_POST['option_name1'];
$option_name2 = $_POST['option_name2'];
$retry_at = $_POST['retry_at'];
$subscr_id = $_POST['subscr_id'];
$payment_type = $_POST['payment_type'];
$quantity = $_POST['quantity'];


// email header

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



if ($fp) {
fputs ($fp, $header . $req);
$res = stream_get_contents($fp, 6144);
$res2 = strip_tags($res);
if (strpos($res2, "VERIFIED") !== false) {
// 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 ($payment_status == "Completed" AND $receiver_email == $paypal) {

$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 ($mpool == "1") {
ppbuypool ( $userid, $payment_amount );
}
buycommission ( $user ['referid'], $payment_amount );

if ($item == $sitename . " 1000 Points " . $userid) {
mysql_query ( "UPDATE members SET points=points+" . (1000 * $quantity) . " WHERE userid = '" . $userid . "'" );
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . (1000 * $quantity) . " points','" . time () . "','$$payment_amount')" );

} elseif ($item == $sitename . " Solo Ad " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " solos','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `solos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );
$quantity --;
}

} elseif ($item == $sitename . " Full Page Login Ad ". $userid) {
$rented = $_POST ['option_selection2'];
mysql_query ( "insert into fullloginads (userid,rented) values('$userid','$rented')" );
mysql_query ( "INSERT INTO transactions VALUES ('id', '". $userid ."','Paypal payment - Full Page Login Ad $rented', '". time () ."','$$payment_amount')" );


} elseif ($item == $sitename . " Hot Headline Ad " . $userid) {
$hheadlineadmaxhits = $_POST ['option_selection2'];
mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Paypal payment - Hot Headline Ad $hheadlineadmaxhits Clicks','" . time () . "','$$payment_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 ['option_selection2'];
mysql_query ( "INSERT INTO transactions VALUES ('id','" . $userid . "','Paypal payment - Hot Header Ad $hheaderadmaxhits Clicks','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into hheaderads (userid,max,purchase) values('$userid','$hheaderadmaxhits',NOW())" );
$quantity --;
}

} elseif ($item == $sitename . " High Priority Solo Ad " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " HP Solos','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `hpsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );
$quantity --;
}

} elseif ($item == $sitename . " 1000 Banner Impressions " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Banner Ad','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `banners` ( `id` , `name` , `bannerurl` , `targeturl` , `userid` , `status` , `shown` , `clicks` , `max` , `added` )VALUES (NULL , '', '', '', '" . $userid . "', '0', '0', '0', '1000', '0')" );
$quantity --;
}

} 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 . "','Paypal payment - Button Ad','" . time () . "','$$payment_amount')" );

} elseif ($item == $sitename . " Top Navigation Link " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Top Nav Link','" . time () . "','$$payment_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 . "','Paypal payment - " . $quantity . " Bottom Nav Link','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `botnav` ( `userid` , `date`) VALUES ('" . $userid . "', '" . time () . "')" );
$quantity --;
}

} elseif ($item == $sitename . " Login Ad " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Login Ad','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into loginads (userid,max) values('$userid','1000')" );
$quantity --;
}

} elseif ($item == $sitename . " Traffic Link 50 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " T-Link 50','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into tlinks (userid,paid) values('$userid','50')" );
$quantity --;
}

} elseif ($item == $sitename . " Traffic Link 100 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " T-Link 100','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into tlinks (userid,paid) values('$userid','100')" );
$quantity --;
}

} elseif ($item == $sitename . " Traffic Link 200 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " T-Link 200','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into tlinks (userid,paid) values('$userid','200')" );
$quantity --;
}

} elseif ($item == $sitename . " Hotlink 5000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Hotlink 5000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into hotlinks (userid,max) values('$userid','5000')" );
$quantity --;
}

} elseif ($item == $sitename . " Hotlink 10000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Hotlink 10000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into hotlinks (userid,max) values('$userid','10000')" );
$quantity --;
}

} elseif ($item == $sitename . " Hotlink 20000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Hotlink 20000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into hotlinks (userid,max) values('$userid','20000')" );
$quantity --;
}

} elseif ($item == $sitename . " PTC Link 50 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " PTC 50','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into ptcads (userid,paid) values('$userid','50')" );
$quantity --;
}

} elseif ($item == $sitename . " PTC Link 100 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " PTC 100','" . time () . "','$$payment_amount')" );

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

} elseif ($item == $sitename . " PTC Link 200 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " PTC 200','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into ptcads (userid,paid) values('$userid','200')" );
$quantity --;
}

} elseif ($item == $sitename . " Set N Forget Solo " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - SNF Solos','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into snfsolos (userid,type,solos_left) values('$userid','" . $_POST ['option_selection2'] . "','" . $_POST ['custom'] . "')" );
$quantity --;
}

} elseif ($item == $sitename . " Set N Forget Cash Solo " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - SNF Cash Solos','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into snfcsolos (userid,type,cashsolos_left) values('$userid','" . $_POST ['option_selection2'] . "','" . $_POST ['custom'] . "')" );
$quantity --;
}

} elseif ($item == $sitename . " Cash Solo " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Cash Solos','" . time () . "','$$payment_amount')" );

while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `cashsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );
$quantity --;
}

} elseif ($item == $sitename . " High Priority Cash Solo " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " HP Cash Solo','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "INSERT INTO `hpcsolos` (`id` ,`userid` ,`approved` ,`subject` ,`adbody` ,`sent` ,`added`) VALUES (NULL , '" . $userid . "', '0', '', '', '0', '0')" );
$quantity --;
}

} elseif ($item == $sitename . " Solo Footer Ad " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal 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 . " Solo Header Ad " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Solo Header Ad','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into headerads (userid,paid) values('$userid','50')" );
$quantity --;
}


} elseif ($item == $sitename . " Daily Bonus " . $userid) {
$rented = $_POST ['option_selection2'];
mysql_query ( "insert into dailybonus (userid,rented) values('$userid','$rented')" );
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - Daily Bonus $rented','" . time () . "','$$payment_amount')" );


} elseif ($item == $sitename . " Headline Ad 5000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Headline Ad 5000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into headlineads (userid,max) values('$userid','5000')" );
$quantity --;
}

} elseif ($item == $sitename . " Headline Ad 10000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Headline Ad 10000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into headlineads (userid,max) values('$userid','10000')" );
$quantity --;
}

} elseif ($item == $sitename . " Headline Ad 20000 " . $userid) {
mysql_query ( "INSERT INTO transactions VALUES ('id', '" . $userid . "','Paypal payment - " . $quantity . " Headline Ad 20000','" . time () . "','$$payment_amount')" );
while ( $quantity > 0 ) {
mysql_query ( "insert into headlineads (userid,max) values('$userid','20000')" );
$quantity --;
}

}

}

}


} else {
print "PayPal response is not VERIFIED";
}
} else {
print "PayPal - SOCKET ERROR!";
}
mysql_close();
?>