<?
$page = "account.php";
include "manager_header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }


// SERVER INFO
$server_array = explode("/", $_SERVER['PHP_SELF']);
$server_array_mod = array_pop($server_array);
$server_array_mod = array_pop($server_array);
$server_info = implode("/", $server_array);
$prefix = "http://".$_SERVER['HTTP_HOST'].$server_info;



if($task == "thanks") {
echo $head;
echo "
$account1
<br><br>
<form action='index.php' method='POST'>
<input type='submit' class='button' value='$account2'>
</form>
";
echo $foot;
exit;
}



if($task == "upgrade") {
  if($admin_info[allow_upgrades] != "1" & $admin_info[allow_downgrades] != "1") { header("Location: index.php"); exit(); }
 
  $upgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group > '$group_info[order_group]' AND order_group <> '0'");
  $downgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group <= '$group_info[order_group]' AND order_group <> '0'");
  
  $upgrades_total = mysql_num_rows($upgrades);
  $downgrades_total = mysql_num_rows($downgrades);
  $total_options = $upgrades_total + $downgrades_total;

  if($total_options <= 0) { header("Location: index.php"); exit(); }

  echo $head;
  echo "
  $account3
  <br><br>
  <table cellpadding='0' cellspacing='0'>
  ";


  // SHOW UPGRADES
  $c = 0;
  if($admin_info[allow_upgrades] == "1" & mysql_num_rows($upgrades) > 0) {
  while($upgrade_info = mysql_fetch_assoc($upgrades)) {
  $c++;
  if($upgrade_info[type] == "1") { $action = "https://www.paypal.com/cgi-bin/webscr"; } else { $action = "account.php"; }
  echo "
  <form action='$action' method='post'>
  <tr>
  <td class='form'>
  <font class='bold'>$upgrade_info[group_name]</font><br>$upgrade_info[group_description]
    <table cellpadding='0' cellspacing='0'>
    <tr><td>&nbsp;</td></tr>
    <tr><td><input type='submit' class='button' value='$account4'></td></tr>
      <input type='hidden' name='cmd' value='_xclick'>
      <input type='hidden' name='business' value='$admin_info[paypal_email]'>
      <input type='hidden' name='item_name' value='$upgrade_info[group_name]'>
      <input type='hidden' name='item_number' value='$upgrade_info[g_id]'>
      <input type='hidden' name='amount' value='$upgrade_info[price]'>
      <input type='hidden' name='no_note' value='1'>
      <input type='hidden' name='currency_code' value='$upgrade_info[currency]'>
      <input type='hidden' name='cancel_return' value='$prefix/manager/account.php'>
      <input type='hidden' name='return' value='$prefix/manager/account.php?task=thanks'>
      <input type='hidden' name='notify_url' value='$prefix/paypal.php'>
      <input type='hidden' name='custom' value='$user_info[u_id]'>
      <input type='hidden' name='task' value='change_account'>
      </form>
    </table>
  </td>
  </tr>
  ";

  if($c != $total_options) { echo "<tr><td colspan='2'>&nbsp;</td></tr>"; }

  }
  }


  // SHOW DOWNGRADES
  if($admin_info[allow_downgrades] == "1" & mysql_num_rows($downgrades) > 0) {
  while($downgrade_info = mysql_fetch_assoc($downgrades)) {
  $c++;
  if($downgrade_info[type] == "1") { $action = "https://www.paypal.com/cgi-bin/webscr"; } else { $action = "account.php"; }
  echo "
  <form action='$action' method='post'>
  <tr>
  <td class='form'>
  <font class='bold'>$downgrade_info[group_name]</font><br>$downgrade_info[group_description]
    <table cellpadding='0' cellspacing='0'>
    <tr><td>&nbsp;</td></tr>
    <tr><td><input type='submit' class='button' value='$account5'></td></tr>
      <input type='hidden' name='cmd' value='_xclick'>
      <input type='hidden' name='business' value='$admin_info[paypal_email]'>
      <input type='hidden' name='item_name' value='$downgrade_info[group_name]'>
      <input type='hidden' name='item_number' value='$downgrade_info[g_id]'>
      <input type='hidden' name='amount' value='$downgrade_info[price]'>
      <input type='hidden' name='no_note' value='1'>
      <input type='hidden' name='currency_code' value='$downgrade_info[currency]'>
      <input type='hidden' name='cancel_return' value='$prefix/manager/account.php'>
      <input type='hidden' name='return' value='$prefix/manager/account.php?task=thanks'>
      <input type='hidden' name='notify_url' value='$prefix/paypal.php'>
      <input type='hidden' name='custom' value='$user_info[u_id]'>
      <input type='hidden' name='task' value='change_account'>
      </form>
    </table>
  </td>
  </tr>
  ";
  if($c != $total_options) { echo "<tr><td colspan='2'>&nbsp;</td></tr>"; }
  }
  }

  echo "</table>";

  echo $foot;
  exit;
}







if($task == "change_account_do") {
$g_id = $_POST['g_id'];
$group = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$g_id'");
if(mysql_num_rows($group) != 1) { header("Location: account.php?task=upgrade"); exit(); }
$new_group_info = mysql_fetch_assoc($group);

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]'");


header("Location: index.php");
exit();
}





if($task == "change_account") {
$g_id = $_POST['item_number'];
$group = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$g_id'");
if(mysql_num_rows($group) != 1) { header("Location: account.php?task=upgrade"); exit(); }
$new_group_info = mysql_fetch_assoc($group);

echo $head;
echo "
$account6
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='account.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$account7'>&nbsp;
</td>
<input type='hidden' name='task' value='change_account_do'>
<input type='hidden' name='g_id' value='$g_id'>
</form>
<form action='account.php' method='POST'>
<td>
<input type='submit' class='button' value='$account8'>
</td>
<input type='hidden' name='task' value='upgrade'>
<input type='hidden' name='g_id' value='$g_id'>
</tr>
</form>
</table>
";
echo $foot;
exit();
}









if($task == "main") {

 if($group_info[type] != 1) { header("Location: index.php"); exit(); }

 switch($group_info[currency]) {
  case "USD": $sign .= "\$"; break;
  case "EUR": $sign .= "&#128;"; break;
  case "GBP": $sign .= "£"; break;
  case "CAD": $sign .= "\$(CAD)"; break;
  case "AUD": $sign .= "\$(AUD)"; break;
  case "JPY": $sign .= "¥"; break;
 }
 switch($group_info[frequency]) {
  case "once": $amount_due = $sign.$group_info[price]; break;
  case "weekly": $freq1 = "week"; $freq2 = "Week"; break;
  case "monthly": $freq1 = "month"; $freq2 = "Month"; break;
  case "yearly": $freq1 = "year"; $freq2 = "Year"; break;
 }

 $billing_cycle = "1 $freq1";
 $price = $sign.$group_info[price]."/$freq2";
 $decimals = explode(".", $group_info[price]);
 if(strlen($decimals[1]) == "1") { $decimals[1] .= "0"; } elseif(strlen($decimals[1]) == "0") { $decimals[1] .= "00"; }
 $full_price = implode(".", $decimals);
 $total = $sign.$full_price;

 if($group_info[frequency] == "once") {
  $amount_due = "$account9 <font class='bold'>$amount_due</font>";
 } else {
  $amount_due = "$billing_cycle x $price = <font class='bold'>$total</font>";
 }






echo $head;
echo "
$account10
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>
<tr><td>
 <table cellpadding='0' cellspacing='0' class='form'>
 <tr>
 <td class='form1'>$account11</td>
 <td class='form2'>$amount_due</td>
 </tr>
 </table>
</td>
<td>&nbsp;&nbsp;<input type='submit' class='button' value='$account2'></td>
</tr>
<input type='hidden' name='cmd' value='_xclick'>
<input type='hidden' name='business' value='$admin_info[paypal_email]'>
<input type='hidden' name='item_name' value='$group_info[group_name]'>
<input type='hidden' name='item_number' value='$group_info[g_id]'>
<input type='hidden' name='amount' value='$group_info[price]'>
<input type='hidden' name='no_note' value='1'>
<input type='hidden' name='currency_code' value='$group_info[currency]'>
<input type='hidden' name='cancel_return' value='$prefix/manager/account.php'>
<input type='hidden' name='return' value='$prefix/manager/account.php?task=thanks'>
<input type='hidden' name='notify_url' value='$prefix/paypal.php'>
<input type='hidden' name='custom' value='$user_info[u_id]'>
</form>
</table>
<br>
";

$upgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group >= '$group_info[order_group]' AND order_group <> '0'");
$downgrades = mysql_query("SELECT * FROM bhost_groups WHERE g_id <> '$group_info[g_id]' AND order_group <= '$group_info[order_group]' AND order_group <> '0'");
if($admin_info[allow_downgrades] == "1" & mysql_num_rows($downgrades) > 0) {
 $text = "$account12";
} elseif($admin_info[allow_upgrades] == "1" & mysql_num_rows($upgrades) > 0) {
 $text = "$account13";
}
echo "$text";
echo $foot;
exit;
}