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

if($admin_info[approve_deny] != "yes") {
echo $header;
echo "
<b>Approval/Rejection is Disabled</b><br>
This page is not available because the signup approval/rejection feature is disabled.<br>
You must enable signup approval/rejection from the <a href='settings_users.php'>user settings page</a>.
";
echo $footer;
exit;
}




if($task == "Reject Users") {
  $incoming_fields = array_keys($_POST);
  $incoming_values = array_values($_POST);
    for($i=0;$i<count($incoming_fields);$i++) {
      if((substr($incoming_fields[$i], 0, 4) == "u_id") & ($incoming_values[$i] == "yes") & (substr(str_replace(" ", "", $incoming_fields[$i]), 4) != "")) {
      $u_id = substr($incoming_fields[$i], 4);
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$u_id'"));
      if(str_replace(" ", "", $u_id) != "") { @deleteuse("../u/$u_id"); }
      mysql_query("DELETE FROM ims WHERE u_id='$u_id'");
      mysql_query("DELETE FROM ims_image WHERE u_id='$u_id'");
      mysql_query("DELETE FROM ims_folders WHERE u_id='$u_id'");
      if($admin_info[den_email] == "yes") {
      $den_sub = str_replace("&#39;", "'", stripslashes($admin_info[den_sub]));
      $den_message = str_replace("&#39;", "'", stripslashes($admin_info[den_message]));
      $den_message = str_replace("[email]", "$user_info[email]", $den_message);
      $den_sub = str_replace("[email]", "$user_info[email]", $den_sub);
      $den_message = str_replace("[password]", "$user_info[password]", $den_message);
      $den_sub = str_replace("[password]", "$user_info[password]", $den_sub);
      $fname = str_replace("&#39;", "'", $user_info[fname]);
      $lname = str_replace("&#39;", "'", $user_info[lname]);
      $den_message = str_replace("[name]", "$fname $lname", $den_message);
      $den_sub = str_replace("[name]", "$fname $lname", $den_sub);
      mail($user_info[email], $den_sub, $den_message, "From: $admin_info[email]");
      }}
    }
}



if($task == "Confirm Users") {
  $incoming_fields = array_keys($_POST);
  $incoming_values = array_values($_POST);
  $newurl = "http".($_SERVER['HTTPS']=="on"?"s":"")."://".$_SERVER['SERVER_NAME'].strrev(substr(strstr(strrev("$_SERVER[PHP_SELF]"),"/"), 6));
  $login_url = $newurl."index.php";
    for($i=0;$i<count($incoming_fields);$i++) {
      if((substr($incoming_fields[$i], 0, 4) == "u_id") & ($incoming_values[$i] == "yes") & (substr(str_replace(" ", "", $incoming_fields[$i]), 4) != "")) {
      $u_id = substr($incoming_fields[$i], 4);
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$u_id'"));
      mysql_query("UPDATE ims SET is_approved='yes' WHERE u_id='$u_id'");
      if($admin_info[appr_email] == "yes") {
      $appr_sub = str_replace("&#39;", "'", stripslashes($admin_info[appr_sub]));
      $appr_message = str_replace("&#39;", "'", stripslashes($admin_info[appr_message]));
      $appr_message = str_replace("[email]", "$user_info[email]", $appr_message);
      $appr_sub = str_replace("[email]", "$user_info[email]", $appr_sub);
      $appr_message = str_replace("[password]", "$user_info[password]", $appr_message);
      $appr_sub = str_replace("[password]", "$user_info[password]", $appr_sub);
      $fname = str_replace("&#39;", "'", $user_info[fname]);
      $lname = str_replace("&#39;", "'", $user_info[lname]);
      $appr_message = str_replace("[name]", "$fname $lname", $appr_message);
      $appr_sub = str_replace("[name]", "$fname $lname", $appr_sub);
      $appr_message = str_replace("[url]", "$login_url", $appr_message);
      $appr_sub = str_replace("[url]", "$login_url", $appr_sub);
      mail($user_info[email], $appr_sub, $appr_message, "From: $admin_info[email]");
      }}
    }

}



if($o == "e") { $sortby = "email"; } elseif($o == "ed") { $sortby = "email DESC"; }
elseif($o == "u") { $sortby = "u_id"; } elseif($o == "ud") { $sortby = "u_id DESC"; }
elseif($o == "n") { $sortby = "lname, fname"; } elseif($o == "nd") { $sortby = "lname DESC, fname DESC"; }
elseif($o == "s") { $sortby = "signup_date"; } elseif($o == "sd") { $sortby = "signup_date DESC"; }
else { $o = "u"; $sortby = "u_id"; }

$total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE is_approved<>'yes'"));

if(($total_users % $admin_info[users_per_page]) != 0) {
$maxpage = ($total_users) / $admin_info[users_per_page] + 1;
} else {
$maxpage = ($total_users) / $admin_info[users_per_page];
}
$maxpage = (int) $maxpage;
if($page > $maxpage) { $page = $maxpage; } elseif($page < 1) { $page = 1; }
$start = ($page - 1) * $admin_info[users_per_page];

$users = mysql_query("SELECT * FROM ims WHERE is_approved<>'yes' ORDER BY $sortby LIMIT $start,$admin_info[users_per_page]");

if($maxpage != 0 & $maxpage != 1) {
if($page - 1 > 0 & $maxpage != 1 & $maxpage != 0) {  
$prev = $page - 1;  
$pp = "<a href='users_pending.php?page=$prev&task=$task&o=$o'>Previous Page</a>";  
} else {
$pp = "Previous Page";  
}
if($page + 1 <= $maxpage & $maxpage != 1) {  
$next = $page + 1;  
$np = "<a href='users_pending.php?page=$next&task=$task&o=$o'>Next Page</a>";  
} else {
$np = "Next Page";  
}
}

echo $header;
echo "

<b>Pending Signups Manager</b><br>
Use this page to approve or reject pending user signups. <br>
Click on a user's email address
to view/edit their account particulars.
<br><br>
";

if($total_users != 0) {
echo "
<table cellpadding='0' cellspacing='0' width='515'>
<tr>
<td NOWRAP>Total Pending Users Found: $total_users</td>
<td align='right' NOWRAP>
  <table cellpadding='0' cellspacing='0'>
  <form action='users_pending.php' method='POST'>
  <tr>
  <td style='padding-bottom: 3px;'>";

if($pp != "") { echo "$pp <font class='gray'>|</font> "; }
if($maxpage != 1 AND $maxpage != 0) { echo "<input type='text' class='text' name='page' value='$page' size='1'>"; }
if($np != "") { echo " <font class='gray'>|</font> $np"; }

echo "
  </td>
  </tr>
  </form>
  </table>
</td>
</tr>
</table>
";
}

if($total_users != 0) {
echo "
<table cellspacing='0' cellpadding='0' style='border: 1px solid #AAAAAA; background: #FFFFFF;'>
<tr><form action='users_pending.php' method='POST'>
<td width='10' style='padding: 5px; padding-bottom: 3px;' NOWRAP>&nbsp;</td>
<td width='20' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users_pending.php?page=$page&task=$task&o=u"; if($o == "u") { echo "d"; } echo "'>ID</a></b></td>
<td width='150' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users_pending.php?page=$page&task=$task&o=e"; if($o == "e") { echo "d"; } echo "'>Email</a></b></td>
<td width='150' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users_pending.php?page=$page&task=$task&o=n"; if($o == "n") { echo "d"; } echo "'>Name</a></b></td>
<td width='123' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users_pending.php?page=$page&task=$task&o=s"; if($o == "s") { echo "d"; } echo "'>Signup Date</a></b></td>
</tr>
";
}

if($total_users == 0) { echo "
<table cellspacing='0' cellpadding='0'>
<tr><td><b>There are no users waiting for approval/rejection.</b></td></tr>
"; 
}

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

echo "
<tr>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'><input type='checkbox' name='u_id$user_info[u_id]' value='yes'></td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'>$user_info[u_id]</td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'><a href='#' onclick='getuserpending($user_info[u_id])'>$user_info[email]</a></td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'>$user_info[lname], $user_info[fname]</td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'>".date("M d, Y h:ia", timezone($user_info[signup_date]))."</td>
</tr>
";
}

echo "</table>";

if($maxpage != 0) {
echo "
<table cellpadding='0' cellspacing='0' width='515'>
<tr>
<td NOWRAP valign='bottom' style='padding-top: 7px;'>
<input type='hidden' name='page' value='$page'>
<input type='hidden' name='o' value='$o'>
<input class='button' type='submit' name='task' value='Confirm Users'>
<input class='button' type='submit' name='task' value='Reject Users'>
</td>
</form>
<td align='right' NOWRAP>
  <table cellpadding='0' cellspacing='0'>
  <form action='users_pending.php' method='POST'>
  <tr>
  <td>";
}
if($pp != "") { echo "$pp <font class='gray'>|</font> "; }
if($maxpage != 1 AND $maxpage != 0) { echo "<input type='text' class='text' name='page' value='$page' size='1' style='margin-top: 5px;'>"; }
if($np != "") { echo " <font class='gray'>|</font> $np"; }
if($maxpage != 0) {
echo "
  </td>
  </tr>
  </form>
  </table>
</td>
</tr>
</table>
";
}

echo $footer;


?>