<?
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['s'])) { $s = stripslashes($_POST['s']); } elseif(isset($_GET['s'])) { $s = $_GET['s']; } else { $s = ""; }
if(isset($_POST['o'])) { $o = $_POST['o']; } elseif(isset($_GET['o'])) { $o = $_GET['o']; } else { $o = "u"; }
if(isset($_POST['t'])) { $t = $_POST['t']; } elseif(isset($_GET['t'])) { $t = $_GET['t']; } else { $t = "e"; }
include "header.php";


if($task == "delete") {
  if(isset($_POST['delete_type'])) {
  $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($_POST['delete_type'] == "with") {
      $subject = stripslashes($_POST['subject']);
      $message = stripslashes($_POST['message']);
        if(str_replace(" ", "", $subject) == "" | str_replace(" ", "", $message) == "") {
        echo $header;
        echo "
        <b>An Error Has Occurred:</b><br>
        You must provide both a subject and message for your notification emails.<br><br>
        <b><font face='verdana'>&#171;</font> <a href=\"javascript:history.go(-1);\">Click here to go back and try again.</a></b>
        ";
        echo $footer;
        exit();
        }
      mail($user_info[email], $subject, $message, "From: $admin_info[email]");
      }
      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'");
      }
    }
  } else {
  $incoming_fields = array_keys($_POST);
  $incoming_values = array_values($_POST);
  $deletion = "";
  $users = "";
  $userslist = "";
  $last_user = "yes";
  $counter = 0;
    for($i=0;$i<count($incoming_fields);$i++) {
      if((substr($incoming_fields[$i], 0, 4) == "u_id") & ($incoming_values[$i] == "yes")) {
      $counter = $counter + 1;
      $u_id = substr($incoming_fields[$i], 4);
      $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$u_id'"));
        if($last_user == "no") { $users .= ", "; }
      $users .= "$user_info[email]";
        if($user_info[price] > 0) {
          if($user_info[payer_paypal_email] == "") { $payer_email = "None"; } else { $anypaidusers = "yes"; $payer_email = "<font color='#FF0000'>$user_info[payer_paypal_email]</font>"; }
        $paypalemail = "(PayPal Email: $payer_email)";
        } else {
        $paypalemail = "(Free Account)";
        }
      $userslist .= "<br>$counter. <b>$user_info[email]</b> $paypalemail";
      $last_user = "no";
      $deletion .= "<input type='hidden' name='$incoming_fields[$i]' value='yes'>";
      }
    }
  if($counter == 0) { header("Location: users.php?task=main&page=$page&s=$s&o=$o&t=$t"); exit; }
  echo $header;
  echo "
<table cellpadding='0' cellspacing='0' width='510'>
<tr><td>
<b>Delete Users?</b><br>
You are about to delete one or more users and send email notifications. 
Please enter the email subject and message below. (Note: This will be sent to all deleted users.) 
If you do not want to send this email notification, click \"Delete Users Without Notifications\".
<br><br>
";

if($anypaidusers == "yes") {
echo "
<font color='#FF0000'><b>WARNING:</b></font>
One or more of the users you are about to delete has a PayPal subscription to your IMS.<br>
Deleting their accounts will not delete their PayPal subscription: You must do this manually
via your PayPal account. To do this, login to <a href='http://www.paypal.com'>PayPal</a>, 
click on your \"History\" tab, and find the subscriptions you wish to delete. You must
delete any subscriptions with the emails marked in <font color='#FF0000'>red</font> before deleting these users.
<br><br>
";
}

echo "
<b>The following users will be deleted:</b>
$userslist
<br><br>
</td></tr></table>

<table cellpadding='0' cellspacing='0'>
<form action='users.php' method='POST'>
<tr><td width='60' align='right'>Subject&nbsp;</td><td><input type='text' name='subject' maxlength='250' size='45'></td></tr>
<tr><td align='right' valign='top'>Message&nbsp;</td><td><textarea cols='60' rows='10' name='message'></textarea></td></tr>
</td></tr></table>

<br>

<table cellpadding='0' cellspacing='0'>
<tr><td>
  $deletion
  <input type='hidden' name='task' value='delete'>
  <input type='hidden' name='page' value='$page'>
  <input type='hidden' name='s' value='$s'>
  <input type='hidden' name='o' value='$o'>
  <input type='hidden' name='t' value='$t'>
  <input type='hidden' name='delete_type' value='with'>
  <input class='button' type='submit' value='Delete Users'>&nbsp;
</td>
  </form>
  <form action='users.php' method='POST'>
<td>
  $deletion
  <input type='hidden' name='task' value='delete'>
  <input type='hidden' name='page' value='$page'>
  <input type='hidden' name='s' value='$s'>
  <input type='hidden' name='o' value='$o'>
  <input type='hidden' name='t' value='$t'>
  <input type='hidden' name='delete_type' value='without'>
  <input class='button' type='submit' value='Delete Users Without Notifications'>&nbsp;
</td>
  </form>
  <form action='users.php' method='POST'>
<td>
  <input type='hidden' name='task' value='main'>
  <input type='hidden' name='page' value='$page'>
  <input type='hidden' name='s' value='$s'>
  <input type='hidden' name='o' value='$o'>
  <input type='hidden' name='t' value='$t'>
  <input class='button' type='submit' value='Cancel'>
</td></tr></form></table>
  ";
  echo $footer;
  exit;
  }
}



if($t == "u") { $type = "u_id"; } elseif($t == "e") { $type = "email"; } else { $t = "e"; $type = "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 == "s") { $sortby = "account_active DESC, last_login DESC"; } elseif($o == "sd") { $sortby = "account_active, last_login"; }
else { $o = "u"; $sortby = "u_id"; }


$s = addslashes($s);

if($admin_info[demo_feature] == "on") {
  if($task == "search") { 
  $total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE $type LIKE '$s%' AND is_approved='yes' AND email <> '$admin_info[demo_email]'"));
  } else {
  $total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE is_approved='yes' AND email <> '$admin_info[demo_email]'"));
  }
} else {
  if($task == "search") { 
  $total_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE $type LIKE '$s%' AND is_approved='yes'"));
  } else {
  $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];

if($admin_info[demo_feature] == "on") {
  if($task == "search") { 
  $users = mysql_query("SELECT * FROM ims WHERE $type LIKE '$s%' AND is_approved='yes' AND email <> '$admin_info[demo_email]' ORDER BY $sortby LIMIT $start,$admin_info[users_per_page]");
  } else {
  $users = mysql_query("SELECT * FROM ims WHERE is_approved='yes' AND email <> '$admin_info[demo_email]' ORDER BY $sortby LIMIT $start,$admin_info[users_per_page]");
  }
} else {
  if($task == "search") { 
  $users = mysql_query("SELECT * FROM ims WHERE $type LIKE '$s%' AND is_approved='yes' ORDER BY $sortby LIMIT $start,$admin_info[users_per_page]");
  } else {
  $users = mysql_query("SELECT * FROM ims WHERE is_approved='yes' ORDER BY $sortby LIMIT $start,$admin_info[users_per_page]");
  }
}

$s = stripslashes($s);

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

echo $header;
echo "

<b>User Manager</b><br>
Use this page to find users and manage their accounts. <br>
Click on a user's email address
to view/edit their account particulars.
<br><br>

<table cellpadding='0' cellspacing='0'>
<tr>
<td valign='top'>
<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF;'>
<form action='users.php' method='POST'>
<tr>
<td style='padding: 5px;'>
<input type='hidden' name='task' value='search'>
<b>Search 
<select class='text' name='t'>
<option value='e'"; if($t == "e") { echo " SELECTED"; } echo ">Emails</option>
<option value='u'"; if($t == "u") { echo " SELECTED"; } echo ">User IDs</option>
</select> for 
<input class='text' size='15' type='text' name='s' value='".str_replace("'", "&#39;", $s)."' maxlength='250'>
<input type='submit' value='Search' class='button' style='margin-bottom: 1px;'>
</td>
</tr>
</form>
</table>
</td>
<td width='150' style='padding-left: 10px;' align='center'>
<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF;'>
<tr>
<td style='padding: 6px; padding-bottom: 7px;'>
<input type='button' class='button' value='Add New User' onclick='adduser()'>
</tr>
</tr>
</table>
</td>
</tr>
</table>
";

if($total_users != 0) { 
echo "
<br>
<table cellpadding='0' cellspacing='0' width='515'>
<tr>
<td NOWRAP>Total Users Found: $total_users</td>
<td align='right' NOWRAP>
  <table cellpadding='0' cellspacing='0'>
  <form action='users.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 width='515' cellspacing='0' cellpadding='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-top: 1px; margin-bottom: 10px;'>
<tr><form action='users.php' method='POST'>
<td width='10' style='padding: 5px; padding-bottom: 3px;' NOWRAP>&nbsp;</td>
<td width='10' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users.php?page=$page&task=$task&s=$s&t=$t&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.php?page=$page&task=$task&s=$s&t=$t&o=e"; if($o == "e") { echo "d"; } echo "'>Email</a></b></td>
<td width='40' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b><a href='users.php?page=$page&task=$task&s=$s&t=$t&o=s"; if($o == "s") { echo "d"; } echo "'>Status</a></b></td>
<td width='100' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b>Space Used</b></td>
<td colspan='2' width='100' style='padding: 5px; padding-bottom: 3px;' NOWRAP><b>Bandwidth Used</b></td>
</tr>
";
}

if($task == "search") {
if($type == "u_id") {
$errormsg = "No user with user ID \"$s\" was found in the database.";
} elseif($type == "email") {
$errormsg = "No users have an email address that contains the string: \"$s\"";
}
} else {
$errormsg = "No users have been found in your IMS database.";
}

if($total_users == 0) { echo "
<table cellspacing='0' cellpadding='0'>
<tr><td style='padding-left: 5px; padding-top: 5px;'><b>$errormsg</b></td></tr>
"; }

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

if($user_info[last_login] == "Never") {
$day_difference = time() - $user_info[signup_date];
} else {
$day_difference = time() - $user_info[last_login];
}
$banned_users_email = explode(",", $admin_info[ban_email]);
$banned_users_ip = explode(",", $admin_info[ban_ip]);
if(in_array($user_info[email], $banned_users_email) | (in_array($user_info[IP], $banned_users_ip) & $user_info[IP] != "")) {
$status = "<font color='#FF0000'>Banned</font>";
} elseif($user_info[account_active] != "yes") {
$status = "<font color='#FF0000'>Disabled</font>";
} elseif($day_difference >= ($admin_info[inactivity] * 86400) & $admin_info[inactivity] != 0) {
$status = "<font color='#FF0000'>Inactive</font>";
} else {
$status = "Active";
}
$space_used = mysql_fetch_assoc(mysql_query("SELECT sum(size) AS size FROM ims_image WHERE u_id='$user_info[u_id]'"));
if($space_used[size] == NULL) { $space_used[size] = 0; }
$space_used = ($space_used[size]) / (1024 * 1024);
$space_used = rounder($space_used);

if($user_info[bandwidth_used] == "") { $user_info[bandwidth_used] = 0; }
$bandwidth_used = ($user_info[bandwidth_used]) / (1024 * 1024);
$bandwidth_used = rounder($bandwidth_used);

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='getuser($user_info[u_id])'>$user_info[email]</a></td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'>$status</td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'><b>".$space_used." MB</b> / ".$user_info[space_avail]." MB</td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'><b>".$bandwidth_used." MB</b> / ".$user_info[bandwidth_allotted]." MB</td>
<td style='padding: 5px;' style='border-top: 1px solid #AAAAAA;'><a href='user_cookies.php?u_id=$user_info[u_id]' title=\"Login to this user's account.\" target='_blank'><img src='../icons/camera.gif' border='0'></a></td>
</tr>
";
}

echo "</table>";

if($maxpage != 0) {
echo "
<table cellpadding='0' cellspacing='0' width='515'>
<tr>
<td NOWRAP valign='bottom'>
<input class='button' type='submit' value='Delete Selected Users'>
<input type='hidden' name='page' value='$page'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='o' value='$o'>
<input type='hidden' name='t' value='$t'>
<input type='hidden' name='task' value='delete'>
</td>
</form>
<td align='right' NOWRAP>
  <table cellpadding='0' cellspacing='0'>
  <form action='users.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;
?>