<?
include "header.php";

$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) {
echo "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 == "subscr_signup") {
      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$custom'")) == 1) {
      //NEW SUBSCRIPTIONS, OLD USER
      $plan_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_plans WHERE p_id='$item_number'"));
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$custom'"));
      if($receiver_email != "") { $paypal_email = $receiver_email; } else { $paypal_email = $business; }
      $signup_date = time();
      mysql_query("UPDATE ims SET 
      subscribed='yes',
      payer_paypal_email='$payer_email',
      reset='$signup_date',
      plan='$plan_info[p_id]', 
      price='$plan_info[price]',
      ext_list='$plan_info[ext_list]',
      space_avail='$plan_info[allocated_space]',
      folders_avail='$plan_info[allowed_folders]',
      max_filesize='$plan_info[max_filesize]',
      bandwidth_allotted='$plan_info[bandwidth_allotted]',
      photo_album='$plan_info[photo_album]',
      paypal_email='$paypal_email'
      WHERE u_id='$custom'
      ");

      } else {
      //NEW SIGNUPS, NEW USER
      $custom = trim($_POST['custom']);
      $custom_array = explode(",", $custom);
      $email = $custom_array[0];
      $password = $custom_array[1];
      $fname = str_replace("'", "&#39;", stripslashes($custom_array[2]));
      $lname = str_replace("'", "&#39;", stripslashes($custom_array[3]));
      $IP = $custom_array[4];
      if($receiver_email != "") { $paypal_email = $receiver_email; } else { $paypal_email = $business; }
        $plan_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_plans WHERE p_id='$item_number'"));

      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email='$email'")) == 0) {
        $is_approved = "yes";
        $signup_date = time();
        $code = randomcode();
        mysql_query("INSERT INTO ims (email,password,code,fname,lname,subscribed,payer_paypal_email,account_active,reset,plan,price,signup_date,IP,last_login,ext_list,space_avail,folders_avail,max_filesize,bandwidth_allotted,photo_album,paypal_email,is_approved,prefs_newwin,prefs_listby,prefs_images_per_page,albumprefs_on,albumprefs_privacy,albumprefs_password,albumprefs_logo,albumprefs_thumbs,albumprefs_align,albumprefs_width) VALUES ('$email','$password','$code','$fname','$lname','yes','$payer_email','yes','$signup_date','$item_number','$plan_info[price]','$signup_date','$IP','Never','$plan_info[ext_list]','$plan_info[allocated_space]','$plan_info[allowed_folders]','$plan_info[max_filesize]','$plan_info[bandwidth_allotted]','$plan_info[photo_album]','$paypal_email','$is_approved','yes','oldest','10','no','public','','','yes','left','500')");
        $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password' AND signup_date='$signup_date' AND IP='$IP'"));
        mkdir("./u/$user_info[u_id]", 0700);
        chmod("./u/$user_info[u_id]", 0777);
        $newurl = "http".($_SERVER['HTTPS']=="on"?"s":"")."://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev("$_SERVER[PHP_SELF]"),"/"));
        $login_url = $newurl."index.php";

        if($admin_info[welcome] == "yes") {
        $fname = str_replace("&#39;", "'", $fname);
        $lname = str_replace("&#39;", "'", $lname);
        $welcome_note = str_replace("&#39;", "'", stripslashes($admin_info[welcome_note]));
        $welcome_sub = str_replace("&#39;", "'", stripslashes($admin_info[welcome_sub]));
        $welcome_note = str_replace("[email]", "$email", $welcome_note);
        $welcome_sub = str_replace("[email]", "$email", $welcome_sub);
        $welcome_note = str_replace("[password]", "$password", $welcome_note);
        $welcome_sub = str_replace("[password]", "$password", $welcome_sub);
        $welcome_note = str_replace("[name]", "$fname $lname", $welcome_note);
        $welcome_sub = str_replace("[name]", "$fname $lname", $welcome_sub);
        $welcome_note = str_replace("[url]", "$login_url", $welcome_note);
        $welcome_sub = str_replace("[url]", "$login_url", $welcome_sub);
        mail($email, $welcome_sub, $welcome_note, "From: $admin_info[email]");
        }
        if($admin_info[notify] == "yes") {
        $fname = str_replace("&#39;", "'", $fname);
        $lname = str_replace("&#39;", "'", $lname);
        $subject = "A new IMS account has been created.";
        $message = "A new user has signed up to your IMS.\n\nFull Name: $fname $lname\nUsername: $email\nPassword: $password";
        mail($admin_info[email], $subject, $message, "From: $admin_info[email]");
        }
      }
      }

    } elseif($txn_type == "subscr_modify") {
      //PLAN UPGRADES
      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$custom'")) == 1) {
      $u_id = $custom;
      $signup_date = time();
      $plan_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_plans WHERE p_id='$item_number'"));
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$u_id'"));
      mysql_query("UPDATE ims SET 
      subscribed='yes',
      payer_paypal_email='$payer_email',
      reset='$signup_date',
      plan='$plan_info[p_id]', 
      price='$plan_info[price]',
      ext_list='$plan_info[ext_list]',
      space_avail='$plan_info[allocated_space]',
      folders_avail='$plan_info[allowed_folders]',
      max_filesize='$plan_info[max_filesize]',
      bandwidth_allotted='$plan_info[bandwidth_allotted]',
      photo_album='$plan_info[photo_album]'
      WHERE u_id='$user_info[u_id]'
      ");
      } else {
      $custom = trim($_POST['custom']);
      $custom_array = explode(",", $custom);
      $email = $custom_array[0];
      $password = $custom_array[1];
      $fname = $custom_array[2];
      $lname = $custom_array[3];
      $IP = $custom_array[4];
      $signup_date = time();
      $plan_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_plans WHERE p_id='$item_number'"));
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password' AND IP='$IP'"));
      mysql_query("UPDATE ims SET 
      subscribed='yes',
      payer_paypal_email='$payer_email',
      reset='$signup_date',
      plan='$plan_info[p_id]', 
      price='$plan_info[price]',
      ext_list='$plan_info[ext_list]',
      space_avail='$plan_info[allocated_space]',
      folders_avail='$plan_info[allowed_folders]',
      max_filesize='$plan_info[max_filesize]',
      bandwidth_allotted='$plan_info[bandwidth_allotted]',
      photo_album='$plan_info[photo_album]'
      WHERE u_id='$user_info[u_id]'
      ");
      }

    } elseif($txn_type == "subscr_cancel") {
    //PLAN CANCELLATION
      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$custom'")) == 1) {
      mysql_query("DELETE FROM ims WHERE u_id='$custom'");
      mysql_query("DELETE FROM ims_image WHERE u_id='$custom'");
      mysql_query("DELETE FROM ims_folders WHERE u_id='$custom'");
      if(str_replace(" ", "", $custom) != "") { @deleteuse("./u/$custom/"); }
      } else {
      $custom = trim($_POST['custom']);
      $custom_array = explode(",", $custom);
      $email = $custom_array[0];
      $password = $custom_array[1];
      $fname = $custom_array[2];
      $lname = $custom_array[3];
      $IP = $custom_array[4];
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password' AND IP='$IP'"));
      mysql_query("DELETE FROM ims WHERE u_id='$user_info[u_id]'");
      mysql_query("DELETE FROM ims_image WHERE u_id='$user_info[u_id]'");
      mysql_query("DELETE FROM ims_folders WHERE u_id='$user_info[u_id]'");
      if(str_replace(" ", "", $user_info[u_id]) != "") { @deleteuse("./u/$user_info[u_id]/"); }
      }

    } elseif($txn_type == "subscr_failed") {
      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$custom'")) == 1) {
      $user_info = mysql_fetch_assoc(mysql_query("SELECT email FROM ims WHERE u_id='$custom'"));
      mail($admin_info[email], "Subscription Failed", "The user $user_info[email] has missed a payment.", "From: $admin_info[email]");
      } else {
      $custom = trim($_POST['custom']);
      $custom_array = explode(",", $custom);
      $email = $custom_array[0];
      $password = $custom_array[1];
      $fname = $custom_array[2];
      $lname = $custom_array[3];
      $IP = $custom_array[4];
      mail($admin_info[email], "Subscription Failed", "The user $email has missed a payment.", "From: $admin_info[email]");
      }

    } elseif($txn_type == "subscr_eot") {
      if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE u_id='$custom'")) == 1) {
      mysql_query("DELETE FROM ims WHERE u_id='$custom'");
      mysql_query("DELETE FROM ims_image WHERE u_id='$custom'");
      mysql_query("DELETE FROM ims_folders WHERE u_id='$custom'");
      if(str_replace(" ", "", $custom) != "") { @deleteuse("./u/$custom/"); }
      } else {
      $custom = trim($_POST['custom']);
      $custom_array = explode(",", $custom);
      $email = $custom_array[0];
      $password = $custom_array[1];
      $fname = $custom_array[2];
      $lname = $custom_array[3];
      $IP = $custom_array[4];
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password' AND IP='$IP'"));
      mysql_query("DELETE FROM ims WHERE u_id='$user_info[u_id]'");
      mysql_query("DELETE FROM ims_image WHERE u_id='$user_info[u_id]'");
      mysql_query("DELETE FROM ims_folders WHERE u_id='$user_info[u_id]'");
      if(str_replace(" ", "", $user_info[u_id]) != "") { @deleteuse("./u/$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;


?>