<?
include "mysqlcon.php";
include "functions.php";
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_admin"));


$postvars = array();
$restrict = array('receiver_email','business','item_name','item_name_','item_number','item_number_','quantity','invoice','custom','option_name','option_selection','option_name_','option_selection_','num_cart_items','payment_status','pending_reason','payment_date','settle_amount','settle_currency','exchange_rate','payment_gross','payment_fee','mc_gross','mc_fee','mc_currency','txn_id','tax','txn_type','reason_code','for_auction','auction_buyer_id','auction_close_date','auction_multi_item','c','memo','first_name','last_name','address_street','address_city','address_state','address_zip','address_country','address_status','payer_email','payer_id','payer_status','payment_type','notify_version','verify_sign','subscr_date','subscr_effective','period','amount','mc_amount','recurring','reattempt','retry_at','recur_times','username','password','subscr_id');

$postipn = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$postipn .= "&$key=$value";
}

$socket = fsockopen("www.paypal.com", 80, $errno, $errstr, 30);
$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($postipn)."\r\n\r\n";

if(!$socket & !$error) {
$problem = "Problem: Error Number: " . $errno . " Error String: " . $errstr;
exit;
}

fputs($socket, $header.$postipn);
  while(!feof($socket)) {
  $reply = fgets($socket, 1024);
  $reply = trim($reply);
  }

$receiver_email = $_POST['receiver_email'];
$business = $_POST['business'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$quantity = $_POST['quantity'];
$invoice = $_POST['invoice'];
$custom = $_POST['custom'];
$option_name1 = $_POST['option_name1'];
$option_selection1 = $_POST['option_selection1'];
$option_name2 = $_POST['option_name2'];
$option_selection2 = $_POST['option_selection2'];
$num_cart_items = $_POST['num_cart_items'];
$payment_status = $_POST['payment_status'];
$pending_reason = $_POST['pending_reason'];
$payment_date = $_POST['payment_date'];
$settle_amount = $_POST['settle_amount'];
$settle_currency = $_POST['settle_currency'];
$exchange_rate = $_POST['exchange_rate'];
$payment_gross = $_POST['payment_gross'];
$payment_fee = $_POST['payment_fee'];
$mc_gross = $_POST['mc_gross'];
$mc_fee = $_POST['mc_fee'];
$mc_currency = $_POST['mc_currency'];
$tax = $_POST['tax'];
$txn_id = $_POST['txn_id'];
$txn_type = $_POST['txn_type'];
$reason_code = $_POST['reason_code'];
$for_auction = $_POST['for_auction'];
$auction_buyer_id = $_POST['auction_buyer_id'];
$auction_close_date = $_POST['auction_close_date'];
$auction_multi_item = $_POST['auction_multi_item'];
$memo = $_POST['memo'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$address_status = $_POST['address_status'];
$payer_email = $_POST['payer_email'];
$payer_id = $_POST['payer_id'];
$payer_status = $_POST['payer_status'];
$payment_type = $_POST['payment_type'];
$notify_version = $_POST['notify_version'];
$verify_sign = $_POST['verify_sign'];
$subscr_date = $_POST['subscr_date'];
$subscr_effective = $_POST['subscr_effective'];
$period1 = $_POST['period1'];
$period2 = $_POST['period2'];
$period3 = $_POST['period3'];
$amount1 = $_POST['amount1'];
$amount2 = $_POST['amount2'];
$amount3 = $_POST['amount3'];
$mc_amount1 = $_POST['mc_amount1'];
$mc_amount2 = $_POST['mc_amount2'];
$mc_amount3 = $_POST['mc_amount3'];
$recurring = $_POST['recurring'];
$reattempt = $_POST['reattempt'];
$retry_at = $_POST['retry_at'];
$recur_times = $_POST['recur_times'];
$username = $_POST['username'];
$password = $_POST['password'];
$subscr_id = $_POST['subscr_id'];

if(!strcmp($reply,"VERIFIED")) {
 if($txn_type == "web_accept") {
  if($business == "$admin_info[paypal_email]") {
   $new_group = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$item_number'");
   $user = mysql_query("SELECT * FROM bhost_users WHERE u_id='$custom'");
   if(mysql_num_rows($group) != 1 | mysql_num_rows($user) != 1) {
    $new_group_info = mysql_fetch_assoc($new_group);
    $user_info = mysql_fetch_assoc($user);
    $group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$user_info[g_id]'"));
    if($payment_gross == "$new_group_info[price]" & $mc_currency == "$new_group_info[currency]") {
     if($user_info[last_payment] == "0") {
      // USER JUST SIGNED UP
      $payment_date = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
     } else {
      // CHECK WHETHER IT'S BEFORE OR AFTER PAYMENT DUE DATE
      $today = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
      switch($group_info[frequency]) {
       case "weekly": $check_last_payment = $today-(7*24*60*60); break;
       case "monthly": if(date("n", $today) == "3" & (date("j", $today) == "31" | date("j", $today) == "30" | date("j", $today) == "29")) { if(date("L", $today) == "1") { $day = 29; } else { $day = 28; } $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, $day, date("Y", time())); } elseif(date("j", $today) == 31 & (date("n", $today) == "5" | date("n", $today) == "7" | date("n", $today) == "10" | date("n", $today) == "12")) { $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, 30, date("Y", time())); } else { $check_last_payment = MakeTime("0", "0", "0", date("n", time())-1, date("j", time()), date("Y", time())); } break;
       case "yearly": $check_last_payment = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time())-1); break;
      }
      
      if($user_info[last_payment] <= $check_last_payment) {
       // AFTER PAYMENT DUE DATE
       $payment_date = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
      } else {
       // BEFORE PAYMENT DUE DATE
       $last_payment = MakeTime("0", "0", "0", date("n", $user_info[last_payment]), date("j", $user_info[last_payment]), date("Y", $user_info[last_payment]));
       switch($group_info[frequency]) {
        case "weekly": $payment_date = $last_payment+(7*24*60*60); break;
        case "monthly": if(date("j", $last_payment) == "31" & (date("n", $last_payment) == "3" | date("n", $last_payment) == "5" | date("n", $last_payment) == "8" | date("n", $last_payment) == "10")) { $payment_date = MakeTime("0", "0", "0", date("n", $last_payment)+1, 30, date("Y", $last_payment)); } elseif(date("n", $last_payment) == "1" & (date("j", $last_payment) == "31" | date("j", $last_payment) == "30" | (date("j", $last_payment) == "29" & date("L", $last_payment) != "1"))) { $payment_date = MakeTime("0", "0", "0", date("n", $last_payment)+1, 28, date("Y", $last_payment)); } else { $payment_date = MakeTime("0", "0", "0", date("n", $last_payment)+1, date("j", $last_payment), date("Y", $last_payment)); } break;
        case "yearly": $lpayday = date("j", $last_payment); $lpayday = date("n", $last_payment); $lpayday = date("Y", $last_payment); $payment_date = MakeTime("0", "0", "0", date("n", $last_payment), date("j", $last_payment), date("Y", $last_payment)+1); break;
       }
      }
     }
     mysql_query("UPDATE bhost_users SET g_id='$new_group_info[g_id]', last_payment='$payment_date' WHERE u_id='$user_info[u_id]'");
    }
   }
  }
 }
} elseif(!strcmp($reply, "INVALID")) {
// Check your code for any Post back Validation problems
// Investigate the Fact that this Could be a spoofed IPN
// If updating your DB, Ensure this "txn_id" is Not a Duplicate
}

fclose ($socket);
exit;


?>