<?
$page = "admin_paypal.php";
include "admin_header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }


// SET ERROR VARS
$error = "";
$is_error = "";








if($task == "due") {
$g_id = $_GET['g_id'];
if($g_id == "") { header("Location: admin_paypal.php"); exit; }

$groupquery = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$g_id'");
$group = mysql_fetch_assoc($groupquery);
if(mysql_num_rows($groupquery) == 0) { header("Location: admin_paypal.php"); exit; }
if($group[frequency] == "once" | $group[type] == "0") { header("Location: admin_paypal.php"); exit(); }
if(isset($_GET['p'])) { $p = $_GET['p']; } else { $p = 1; }

$today = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
switch($group[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;
}

$difference = ($today-$check_last_payment)+(24*60*60);
$users = mysql_query("SELECT *, CASE WHEN last_payment>'0' THEN (last_payment+$difference) WHEN last_payment='0' THEN signup_date ELSE NULL END AS due FROM bhost_users WHERE g_id='$group[g_id]' AND ((last_payment='0' AND signup_date<'$today') OR (last_payment<'$check_last_payment' AND last_payment>'0')) ORDER BY due,u_id");

$total_users = mysql_num_rows($users);

echo $head;
echo "
<h2>$admin_paypal3 ($total_users)</h2>
$admin_paypal4
<br><br>

<table cellspacing='0' cellpadding='0' width='100%'>
<tr>
<td class='header' width='1' NOWRAP>$admin_paypal5</td>
<td class='header' style='border-left: none;' NOWRAP>$admin_paypal6</td>
<td class='header' style='border-left: none;' NOWRAP>$admin_paypal7</td>
<td class='header' width='120' style='border-left: none;' NOWRAP>$admin_paypal8</td>
<td class='header' width='120' style='border-left: none;' NOWRAP>$admin_paypal9</td>
</tr>
";


while($user = mysql_fetch_assoc($users)) {

 $today = MakeTime("0", "0", "0", date("n", time()), date("j", time())+1, date("Y", time()))-1;
 switch($group[frequency]) {
  case "weekly":
   $due_date = $user[last_payment]+7*24*60*60;
   break;
  case "monthly":
    if(date("j", $user[last_payment]) == "31" & (date("n", $user[last_payment]) == "3" | date("n", $user[last_payment]) == "5" | date("n", $user[last_payment]) == "8" | date("n", $user[last_payment]) == "10")) {
     $due_date = MakeTime("0", "0", "0", date("n", $user[last_payment])+1, 30, date("Y", $user[last_payment]));
    } elseif(date("n", $user[last_payment]) == "1" & (date("j", $user[last_payment]) == "31" | date("j", $user[last_payment]) == "30" | (date("j", $user[last_payment]) == "29" & date("L", $user[last_payment]) != "1"))) {
     $due_date = MakeTime("0", "0", "0", date("n", $user[last_payment])+1, 28, date("Y", $user[last_payment]));
    } else {
     $due_date = MakeTime("0", "0", "0", date("n", $user[last_payment])+1, date("j", $user[last_payment]), date("Y", $user[last_payment]));
    }
   break;
  case "yearly":
   $lpayday = date("j", $user[last_payment]);
   $lpayday = date("n", $user[last_payment]);
   $lpayday = date("Y", $user[last_payment]);
   $due_date = MakeTime("0", "0", "0", date("n", $user[last_payment]), date("j", $user[last_payment]), date("Y", $user[last_payment])+1);
   break;
 }

 if($user[last_payment] == "0") { $due_date = $user[signup_date]; }
 $days_overdue = floor(($today-$due_date)/(24*60*60));



echo "
<tr>
<td class='list2'>$user[u_id]</td>
<td class='list2' style='border-left: none;'><a href='admin_useredit.php?u_id=$user[u_id]'>$user[username]</a></td>
<td class='list2' style='border-left: none;'><a href='mailto:$user[email]'>$user[email]</a></td>
<td class='list2' style='border-left: none;' NOWRAP>".cdate("F j, Y", $due_date)."</td>
<td class='list2' style='border-left: none;' NOWRAP>$days_overdue $admin_paypal10</td>
</tr>
";
}

echo "
</table>
<br>
<form action='admin_paypal.php' method='POST'>
<input type='submit' class='button' value='$admin_paypal11'>
</form>
";

echo $foot;
exit();
}










if($task == "doedit") {
$type = $_POST['type'];
$price = (int)(100*(float)preg_replace("/[^0-9,\.]/", "", $_POST['price']));
$currency = $_POST['currency'];
$frequency = $_POST['frequency'];
$warning = (int)$_POST['warning'];
$warning_message = str_replace("'", "&#39;", $_POST['warning_message']);
$g_id = $_POST['g_id'];

if($g_id == "") { header("Location: admin_paypal.php"); exit; }

$groupquery = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$g_id'");
$group = mysql_fetch_assoc($groupquery);
if(mysql_num_rows($groupquery) == 0) { header("Location: admin_paypal.php"); exit; }

if($type == "1" & ($price == 0 | $price == "" | $currency == "" | $frequency == "" | ($warning != "0" & $warning_message == ""))) { $is_error = "yes"; $error = "$admin_paypal12"; }

if($is_error == "yes") {
echo $head;
echo "
$admin_paypal43
$error
<br><br>
<form action='admin_paypal.php?task=edit&g_id=$g_id' method='POST'>
<input type='submit' class='button' value='$admin_paypal44'>
</form>
";
echo $foot;
exit();
} else {
$dollars = substr($price, 0, strlen($price)-2);
$cents = substr($price, -2);
 if(strlen($cents) == "1") { $cents = "0".$cents; }
$price = $dollars.".".$cents;
mysql_query("UPDATE bhost_groups SET type='$type', price='$price', currency='$currency', frequency='$frequency', warning='$warning', warning_message='$warning_message' WHERE g_id='$g_id'") or die("You have entered and invalid price. Please try again.");
echo $head;
echo "
<h2>$admin_paypal13</h2>
$admin_paypal14
<br><br>
<form action='admin_paypal.php' method='POST'>
<input type='submit' class='button' value='$admin_paypal11'>
</form>
";
echo $foot;
exit();
}}










if($task == "edit") {
$g_id = $_GET['g_id'];

if($g_id == "") { header("Location: admin_paypal.php"); exit; }

$groupquery = mysql_query("SELECT * FROM bhost_groups WHERE g_id='$g_id'");
$group = mysql_fetch_assoc($groupquery);
if(mysql_num_rows($groupquery) == 0) { header("Location: admin_paypal.php"); exit; }

echo $head;
echo "
<h2>$admin_paypal13</h2>
$admin_paypal15
<br><br>

<table width='100%' cellpadding='0' cellspacing='0'>
<form action='admin_paypal.php' method='POST'>
<tr><td class='header'>$admin_paypal16</td></tr>
<tr>
<td class='box'>

  <table cellpadding='0' cellspacing='0'>
  <tr>
  <td>$admin_paypal17 &nbsp;</td>
  <td>
  <select name='type'>
  <option value='0'"; if($group[type] == "0") { echo " SELECTED"; } echo ">$admin_paypal18</option>
  <option value='1'"; if($group[type] == "1") { echo " SELECTED"; } echo ">$admin_paypal19</option>
  </select>
  </td>
  </tr>
  </table>

<br>

$admin_paypal20
<br><br>
  <table cellpadding='0' cellspacing='0'>
  <tr>
  <td>
  <b>$admin_paypal21</b><br>
  <input type='text' class='text' size='5' name='price' value='$group[price]' maxlength='8'>&nbsp;&nbsp;
  </td>
  <td>
  <b>$admin_paypal22</b><br>
  <select name='currency'>
  <option></option>
  <option value='USD'"; if($group[currency] == "USD") { echo " SELECTED"; } echo ">U.S. Dollars (USD)</option>
  <option value='EUR'"; if($group[currency] == "EUR") { echo " SELECTED"; } echo ">Euros (EUR)</option>
  <option value='GBP'"; if($group[currency] == "GBP") { echo " SELECTED"; } echo ">Pounds Sterling (GBP)</option>
  <option value='CAD'"; if($group[currency] == "CAD") { echo " SELECTED"; } echo ">Canadian Dollars (CAD)</option>
  <option value='AUD'"; if($group[currency] == "AUD") { echo " SELECTED"; } echo ">Australian Dollars (AUD)</option>
  <option value='JPY'"; if($group[currency] == "JPY") { echo " SELECTED"; } echo ">Yen (JPY)</option>
  </select>&nbsp;&nbsp;
  </td>
  <td>
  <b>$admin_paypal23</b><br>
  <select name='frequency'>
  <option></option>
  <option value='once'"; if($group[frequency] == "once") { echo " SELECTED"; } echo ">$admin_paypal24</option>
  <option value='weekly'"; if($group[frequency] == "weekly") { echo " SELECTED"; } echo ">$admin_paypal25</option>
  <option value='monthly'"; if($group[frequency] == "monthly") { echo " SELECTED"; } echo ">$admin_paypal26</option>
  <option value='yearly'"; if($group[frequency] == "yearly") { echo " SELECTED"; } echo ">$admin_paypal27</option>
  </select>
  </td>
  </tr>
  </table>
</td>
</tr>
</table>

<br>

<table width='100%' cellpadding='0' cellspacing='0'>
<tr><td class='header'>$admin_paypal28</td></tr>
<tr>
<td class='box'>
$admin_paypal29
<br><br>
<table cellpadding='0' cellspacing='0'>
<tr><td>$admin_paypal30&nbsp;</td><td><input type='text' class='text' size='1' name='warning' value='$group[warning]' maxlength='2'></td><td>&nbsp;$admin_paypal31</td></tr>
</table>
<br>
$admin_paypal32<br>
<textarea name='warning_message' rows='6' cols='50' style='width: 100%; margin-top: 2px;'>$group[warning_message]</textarea>
<br>
$admin_paypal33
</td>
</tr>
</table>

<br>

<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='submit' name='submit' class='button' value='$admin_paypal34'>&nbsp;</td>
<input type='hidden' name='g_id' value='$g_id'>
<input type='hidden' name='task' value='doedit'>
</form>
<form action='admin_paypal.php' method='POST'>
<td><input type='submit' name='submit' class='button' value='$admin_paypal35'></td>
<input type='hidden' name='task' value='main'>
</form>
</tr>
</table>
";

echo $foot;
exit;
}
















if($task == "dosave") {
  $paypal_email = htmlspecialchars($_POST['paypal_email'], ENT_QUOTES);
  $allow_upgrades = $_POST['allow_upgrades'];
  $allow_downgrades = $_POST['allow_downgrades'];
  mysql_query("UPDATE bhost_admin SET paypal_email='$paypal_email', allow_upgrades='$allow_upgrades', allow_downgrades='$allow_downgrades'");

  $groups = mysql_query("SELECT g_id FROM bhost_groups");
  while($group = mysql_fetch_assoc($groups)) {
    $var = "order_".$group[g_id];
    $order = $_POST[$var];
    if(!is_numeric($order) | $order != (int)$order) { $order = "0"; }
    mysql_query("UPDATE bhost_groups SET order_group='$order' WHERE g_id='$group[g_id]'") or die(mysql_error());
  }

echo $head;
echo "
<h2>$admin_paypal13</h2>
$admin_paypal14
<br><br>
<form action='admin_paypal.php' method='POST'>
<input type='submit' class='button' value='$admin_paypal11'>
</form>
";
echo $foot;
exit();
}















if($task == "main") {
echo $head;

echo "
<h2>$admin_paypal1</h2>
$admin_paypal2
<br><br>
";

if(str_replace(" ", "", $admin_info[paypal_email]) == "" & mysql_num_rows(mysql_query("SELECT g_id FROM bhost_groups WHERE type='1'")) > 0) {
 echo "$admin_paypal36";
}

echo "
<form action='admin_paypal.php' method='POST'>

<table cellpadding='0' cellspacing='0' width='100%'>
<tr><td class='header'>$admin_paypal37</td></tr>
<tr>
<td class='box'>
$admin_paypal38
<br><br>
<input type='text' name='paypal_email' class='text' value='$admin_info[paypal_email]' size='30' style='margin-top: 2px;'>
</td>
</tr>
</table>
<br>

<table cellpadding='0' cellspacing='0' width='100%'>
<tr><td class='header'>$admin_paypal39</td></tr>
<tr>
<td class='box'>
$admin_paypal40
<br><br>
  <table cellpadding='0' cellspacing='0'>
  <tr>
    <td><input type='radio' name='allow_upgrades' id='allow_upgrades1' value='1'"; if($admin_info[allow_upgrades] == "1") { echo " CHECKED"; } echo "></td>
    <td NOWRAP><label for='allow_upgrades1'>$admin_paypal41</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td><input type='radio' name='allow_downgrades' id='allow_downgrades1' value='1'"; if($admin_info[allow_downgrades] == "1") { echo " CHECKED"; } echo "></td>
    <td NOWRAP><label for='allow_downgrades1'>$admin_paypal45</label></td>
  </tr>
  <tr>
    <td><input type='radio' name='allow_upgrades' id='allow_upgrades0' value='0'"; if($admin_info[allow_upgrades] == "0") { echo " CHECKED"; } echo "></td>
    <td NOWRAP><label for='allow_upgrades0'>$admin_paypal42</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td><input type='radio' name='allow_downgrades' id='allow_downgrades0' value='0'"; if($admin_info[allow_downgrades] == "0") { echo " CHECKED"; } echo "></td>
    <td NOWRAP><label for='allow_downgrades0'>$admin_paypal46</label></td>
  </tr>
  </table>
</td>
</tr>
</table>
<br>
";

$groups = mysql_query("SELECT * FROM bhost_groups ORDER BY group_name");

if(mysql_num_rows($groups) == 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td class='box2'>
$admin_paypal47
</td>
</tr>
</table>
";
}

echo "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td class='header' align='center' width='1%' NOWRAP>$admin_paypal48</td>
<td class='header' style='border-left: none;' NOWRAP>$admin_paypal49</td>
<td class='header' width='5%' style='border-left: none;' NOWRAP>$admin_paypal50</td>
<td class='header' width='5%' style='border-left: none;' NOWRAP>$admin_paypal51</td>
<td class='header' width='5%' style='border-left: none;' align='center' NOWRAP>$admin_paypal52</td>
</tr>
";

$count = 0;

while($group = mysql_fetch_assoc($groups)) {
$price = "";
if($group[type] == "1") { 
 switch($group[currency]) {
  case "USD":
   $price .= "\$$group[price]";
   break;
  case "EUR":
   $price .= "&#128;$group[price]";
   break;
  case "GBP":
   $price .= "£$group[price]";
   break;
  case "CAD":
   $price .= "\$$group[price] (CAD) ";
   break;
  case "AUD":
   $price .= "\$$group[price] (AUD) ";
   break;
  case "JPY":
   $price .= "¥$group[price]";
   break;
 }
 $today = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time()));
 switch($group[frequency]) {
  case "once":
   $price .= " $admin_paypal53";
   $payments_due = "0";
   break;
  case "weekly":
   $price .= "$admin_paypal54";
   $this_week = $today-(7*24*60*60);
   $payments_due = mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE ((last_payment='0' AND signup_date<'$today') OR (last_payment<'$this_week' AND last_payment>'0')) AND g_id='$group[g_id]'"));
   break;
  case "monthly":
   $price .= "$admin_paypal55";
    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; }
     $this_month = 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")) {
     $this_month = MakeTime("0", "0", "0", date("n", time())-1, 30, date("Y", time()));
    } else {
     $this_month = MakeTime("0", "0", "0", date("n", time())-1, date("j", time()), date("Y", time()));
    }
   $payments_due = mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE ((last_payment='0' AND signup_date<'$today') OR (last_payment<'$this_month' AND last_payment>'0')) AND g_id='$group[g_id]'"));
   break;
  case "yearly":
   $price .= "$admin_paypal56";
   $this_year = MakeTime("0", "0", "0", date("n", time()), date("j", time()), date("Y", time())-1);
   $payments_due = mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE ((last_payment='0' AND signup_date<'$today') OR (last_payment<'$this_year' AND last_payment>'0')) AND g_id='$group[g_id]'"));
   break;
 }
} else { 
 $price = "$admin_paypal57"; 
 $payments_due = "0";
}

echo "
<tr>
<td class='list2' align='center'>$group[g_id]</td>
<td class='list2' style='border-left: none;'><a href='admin_paypal.php?task=edit&g_id=$group[g_id]'>$group[group_name]</a></td>
<td class='list2' style='border-left: none;' NOWRAP>$price</td>
<td class='list2' style='border-left: none;' NOWRAP>"; if($payments_due != "0") { echo "<a href='admin_paypal.php?task=due&g_id=$group[g_id]'>$payments_due $admin_paypal58</a>"; } else { echo "0 $admin_paypal58"; } echo "</td>
<td class='list2' style='border-left: none;' align='center' NOWRAP><input type='text' name='order_$group[g_id]' value='$group[order_group]' size='1' maxlength='2' tabindex='$count'></td>
</tr>
";
$count++;
}

echo "
</table>

<br>

<input type='submit' class='button' value='$admin_paypal34'>
<input type='hidden' name='task' value='dosave'>
</form>
";

echo $foot;
}
?>