<?
$page = "admin_users.php";
include "admin_header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['p'])) { $p = (int) $_POST['p']; } elseif(isset($_GET['p'])) { $p = (int) $_GET['p']; } else { $p = 1; }
if(isset($_POST['s'])) { $s = $_POST['s']; } elseif(isset($_GET['s'])) { $s = $_GET['s']; } else { $s = "id"; }
if(isset($_POST['u'])) { $u = $_POST['u']; } elseif(isset($_GET['u'])) { $u = $_GET['u']; } else { $u = ""; }
if(isset($_POST['u_in'])) { $u_in = $_POST['u_in']; } elseif(isset($_GET['u_in'])) { $u_in = $_GET['u_in']; } else { $u_in = "ue"; }
$users_per_page = 50;

if($u == "") {
 $total_users = mysql_num_rows(mysql_query("SELECT * FROM bhost_users"));
} else {
 if($u_in == "u") {
 $total_users = mysql_num_rows(mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%'"));
 } elseif($u_in == "e") {
 $total_users = mysql_num_rows(mysql_query("SELECT * FROM bhost_users WHERE email LIKE '%$u%'"));
 } else {
 $total_users = mysql_num_rows(mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%' OR email LIKE '%$u%'"));
 }
}

if(($total_users % $users_per_page) != 0) {
$maxpage = ($total_users) / $users_per_page + 1;
} else {
$maxpage = ($total_users) / $users_per_page;
}

$maxpage = (int) $maxpage;
if($p > $maxpage) { $p = $maxpage; } elseif($p < 1) { $p = 1; }
$start = ($p - 1) * $users_per_page;

if($maxpage != 0 & $maxpage != 1) {
if($p - 1 > 0 & $maxpage != 1) {  
$prev = $p - 1;  
$pp = "<a href='admin_users.php?p=$prev&s=$s&u=$u&u_in=$u_in'>$admin_users1</a>";  
} else {
$pp = "<font color='#777777'>$admin_users1</font>";  
}
if($p + 1 <= $maxpage & $maxpage != 1) {  
$next = $p + 1;  
$np = "<a href='admin_users.php?p=$next&s=$s&u=$u&u_in=$u_in'>$admin_users2</a>";  
} else {
$np = "<font color='#777777'>$admin_users2</font>";  
}
}

//u_id
$i = "id";
//username
$a = "a";
//full name
$n = "n";
//signup date
$d = "dd";
//entries
$e = "e";
//email
$b = "b";
//verified
$v = "v";
//enabled
$en = "en";

if($s == "i") {
$sort = "u_id";
$i = "id";
} elseif($s == "id") {
$sort = "u_id DESC";
$i = "i";
} elseif($s == "a") {
$sort = "username";
$a = "ad";
} elseif($s == "ad") {
$sort = "username DESC";
$a = "a";
} elseif($s == "n") {
$sort = "lname, fname";
$n = "nd";
} elseif($s == "nd") {
$sort = "lname DESC, fname DESC";
$n = "n";
} elseif($s == "d") {
$sort = "signup_date";
$d = "dd";
} elseif($s == "dd") {
$sort = "signup_date DESC";
$d = "d";
} elseif($s == "e") {
$sort = "num_entries";
$e = "ed";
} elseif($s == "ed") {
$sort = "num_entries DESC";
$e = "e";
} elseif($s == "b") {
$sort = "email";
$b = "bd";
} elseif($s == "bd") {
$sort = "email DESC";
$b = "b";
} elseif($s == "v") {
$sort = "verified, signup_date DESC";
$v = "vd";
} elseif($s == "vd") {
$sort = "verified DESC, signup_date";
$v = "v";
} elseif($s == "en") {
$sort = "enabled, u_id DESC";
$en = "end";
} elseif($s == "end") {
$sort = "enabled DESC, u_id DESC";
$en = "en";
} else {
$sort = "u_id";
$i = "id";
$s = "i";
}



if($task == "dodelete") {
 if($u == "") {
  $users = mysql_query("SELECT * FROM bhost_users ORDER BY $sort LIMIT $start, $users_per_page");
 } else {
  if($u_in == "u") {
   $users = mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
  } elseif($u_in == "e") {
   $users = mysql_query("SELECT * FROM bhost_users WHERE email LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
  } else {
   $users = mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%' OR email LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
  }
 }
while($user = mysql_fetch_assoc($users)) {
$var = "delete_$user[u_id]";
  if(isset($_POST[$var])) {
    if($_POST[$var] == "yes") {
      mysql_query("DELETE FROM bhost_users WHERE u_id='$user[u_id]'");
      mysql_query("DELETE FROM bhost_profile WHERE u_id='$user[u_id]'");
      mysql_query("DELETE FROM bhost_friends WHERE u_id='$user[u_id]' OR friend_u_id='$user[u_id]'");
      mysql_query("DELETE FROM bhost_pms WHERE to_u_id='$user[u_id]'");
       $weblogs = mysql_query("SELECT w_id FROM bhost_weblogs WHERE u_id='$user[u_id]'");
       while($weblog = mysql_fetch_assoc($weblogs)) {
        mysql_query("DELETE FROM bhost_comments WHERE w_id='$weblog[w_id]'");
        mysql_query("DELETE FROM bhost_entries WHERE w_id='$weblog[w_id]'");
        mysql_query("DELETE FROM bhost_entrycategories WHERE w_id='$weblog[w_id]'");
        mysql_query("DELETE FROM bhost_trackbacks WHERE w_id='$weblog[w_id]'");
        mysql_query("DELETE FROM bhost_weblogs WHERE w_id='$weblog[w_id]'");
       }
       $multiauthor_weblogs = mysql_query("SELECT w_id, level1, level2, invited FROM bhost_weblogs WHERE level1 LIKE '%,$user[u_id],%' OR level2 LIKE '%,$user[u_id],%' OR invited LIKE '%,$user[u_id],%'");
       while($multiauthor_weblog = mysql_fetch_assoc($multiauthor_weblogs)) {
        $newlevel1 = str_replace(",$user[u_id],", ",", $multiauthor_weblog[level1]);
        $newlevel2 = str_replace(",$user[u_id],", ",", $multiauthor_weblog[level2]);
        $newinvited = str_replace(",$user[u_id],", ",", $multiauthor_weblog[invited]);
        mysql_query("UPDATE bhost_weblogs SET level1='$newlevel1', level2='$newlevel2', invited='$newinvited' WHERE w_id='$multiauthor_weblog[w_id]'");
       }

      // DELETE UPLOADED FILES
      $letter = strtolower(substr($user[username], 0, 1));
      $files = mysql_query("SELECT * FROM bhost_uploads WHERE u_id='$user[u_id]'");
      while($file = mysql_fetch_assoc($files)) {
       if($file[up_id] != ".." & $file[up_id] != ".") {
        unlink("../uploads/$letter/$user[username]/$file[up_id].$file[ext]");
	if(file_exists("../uploads/$letter/$user[username]/$file[up_id]_thumb.$file[ext]")) {
	 unlink("../uploads/$letter/$user[username]/$file[up_id]_thumb.$file[ext]");
	}
       }
      }
      $avatar = "../uploads/avatars/".basename($user[avatar]);
      @unlink($avatar);
      @unlink("../uploads/$letter/$user[username]/index.php");
      @rmdir("../uploads/$letter/$user[username]");
      mysql_query("DELETE FROM bhost_folders WHERE u_id='$user[u_id]'");
      mysql_query("DELETE FROM bhost_uploads WHERE u_id='$user[u_id]'");

      // DELETE AVATAR
      $avatar = "../uploads/avatars/".basename($user[avatar]);
      @unlink($avatar);
    }
  }
}
header("Location: admin_users.php?p=$p&s=$s&u=$u&u_in=$u_in");
exit();
}


if($task == "delete") {
echo $head;
echo "
<h2>$admin_users3</h2>
$admin_users4<br>$admin_users29
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='admin_users.php' method='POST'>
<tr><td>
<input type='submit' value='$admin_users5' class='button'>&nbsp;
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='u' value='$u'>
<input type='hidden' name='u_in' value='$u_in'>
";
if($u == "") {
 $users = mysql_query("SELECT * FROM bhost_users ORDER BY $sort LIMIT $start, $users_per_page");
} else {
 if($u_in == "u") {
  $users = mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
 } elseif($u_in == "e") {
  $users = mysql_query("SELECT * FROM bhost_users WHERE email LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
 } else {
  $users = mysql_query("SELECT * FROM bhost_users WHERE username LIKE '%$u%' OR email LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
 }
}
while($user = mysql_fetch_assoc($users)) {
$var = "delete_$user[u_id]";
  if(isset($_POST[$var])) {
    if($_POST[$var] == "yes") {
     echo "<input type='hidden' name='delete_$user[u_id]' value='yes'>";
    } else {
     echo "<input type='hidden' name='delete_$user[u_id]' value='no'>";
    }
  } else {
    echo "<input type='hidden' name='delete_$user[u_id]' value='no'>";
  }
}
echo "
<input type='hidden' name='task' value='dodelete'>
</td>
</form>
<form action='admin_users.php' method='POST'>
<td>
<input type='submit' value='$admin_users6' class='button'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='u' value='$u'>
<input type='hidden' name='u_in' value='$u_in'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}




echo $head;

echo "
<h2>$admin_users7</h2>
$admin_users8
<br><br>
<form action='admin_users.php' method='POST'>
<input type='text' name='u' value='$u'>
<select name='u_in'>
<option value='ue'"; if($u_in == "ue") { echo " SELECTED"; } echo ">$admin_users26</option>
<option value='u'"; if($u_in == "u") { echo " SELECTED"; } echo ">$admin_users27</option>
<option value='e'"; if($u_in == "e") { echo " SELECTED"; } echo ">$admin_users28</option>
</select>
&nbsp;<input class='button' type='submit' value='$admin_users9' style='margin-bottom: 2px;'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='task' value='search'>
</form>
<br>
";

if($total_users > $users_per_page) { 
echo "
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td NOWRAP>$admin_users10 $total_users</td>
<td align='right' NOWRAP style='padding-right: 5px;'>
  <table cellpadding='0' cellspacing='0'>
  <form action='admin_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='p' value='$p' size='1'>"; }
if($np != "") { echo " <font class='gray'>|</font> $np"; }

echo "
  </td>
  </tr>
  <input type='hidden' name='s' value='$s'>
  <input type='hidden' name='u' value='$u'>
  <input type='hidden' name='u_in' value='$u_in'>
  </form>
  </table>
</td>
</tr>
</table>
";
}

echo "
<script language='JavaScript'> 
<!--
var checkboxcount = 1;
function doCheckAll() {
if(checkboxcount == 0) {
with (document.usersform) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = false;
}}
checkboxcount = checkboxcount + 1;
window.document.usersform.check.alt='Check All';
}
} else
with (document.usersform) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = true;
}}
checkboxcount = checkboxcount - 1;
window.document.usersform.check.alt='Uncheck All';
}}
function changeAlt(imgObj,newAlt){
imgObj.alt = newAlt;
}
// -->
</SCRIPT>

<table width='100%' cellpadding='0' cellspacing='0'>
<form action='admin_users.php' name='usersform' method='POST'>
<tr>
";

if($u == "") {
 $users = mysql_query("SELECT u_id, username, fname, lname, enabled, signup_date, last_login, email, verified, num_entries AS entries FROM bhost_users ORDER BY $sort LIMIT $start, $users_per_page");
} else {
 if($u_in == "u") {
 $users = mysql_query("SELECT u_id, username, fname, lname, enabled, signup_date, last_login, email, verified, num_entries AS entries FROM bhost_users WHERE bhost_users.username LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
 } elseif($u_in == "e") {
 $users = mysql_query("SELECT u_id, username, fname, lname, enabled, signup_date, last_login, email, verified, num_entries AS entries FROM bhost_users WHERE bhost_users.email LIKE '%$u%' ORDER BY $sort LIMIT $start, $users_per_page");
 } else {
 $users = mysql_query("SELECT u_id, username, fname, lname, enabled, signup_date, last_login, email, verified, num_entries AS entries FROM bhost_users WHERE (bhost_users.username LIKE '%$u%' OR bhost_users.email LIKE '%$u%') ORDER BY $sort LIMIT $start, $users_per_page");
 }
}

if(mysql_num_rows($users) != 0) {
echo "<td width='1%' class='header' style='padding-right: 0px; padding-bottom: 7px;'><a href='javascript:doCheckAll()'><img src='../images/check.gif' id='check' border='0' alt='Check All'></a></td>";
} else {
echo "<td width='1%' class='header' style='padding-right: 0px; padding-bottom: 7px;'><img src='../images/check_disabled.gif' border='0' style='margin-right: 3px;'></td>";
}

echo "
<td class='header' width='1%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$i&u=$u&u_in=$u_in'><b>$admin_users11</b></a></td>
<td class='header' width='1%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$a&u=$u&u_in=$u_in'><b>$admin_users12</b></a></td>
<td class='header' width='70%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$n&u=$u&u_in=$u_in'><b>$admin_users13</b></a></td>

<td class='header' width='23%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$b&u=$u&u_in=$u_in'><b>$admin_users22</b></a></td>
"; if($admin_info[signup_emailverify] == "1") { echo "
<td class='header' width='1%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$v&u=$u&u_in=$u_in'><b>$admin_users23</b></a></td>
"; } echo "
<td class='header' width='1%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$en&u=$u&u_in=$u_in'><b>$admin_users30</b></a></td>
<td class='header' width='1%' style='border-left: none;'><a href='admin_users.php?p=$p&s=$e&u=$u&u_in=$u_in'><b>$admin_users14</b></a></td>
<td class='header' width='1%' style='border-left: none;' NOWRAP><a href='admin_users.php?p=$p&s=$d&u=$u&u_in=$u_in'><b>$admin_users15</b></a></td>
<td class='header' width='1%' style='border-left: none;' NOWRAP>$admin_users16</td>
</tr>
";

if(mysql_num_rows($users) == 0) {
echo "
<tr>
<td class='box' colspan='9'>$admin_users17</td>
</tr>
";
}

$count = 0;
while($user = mysql_fetch_assoc($users)) {
$count++;
if($count % 2) { $class = "list2"; } else { $class = "list2"; }

if($user[last_login] == 0 OR $user[last_login] == "") {
$timedifference = time() - $user[signup_date];
} else {
$timedifference = time() - $user[last_login];
}

if($timedifference > 2629743) {
$fontstart = "<font color='#FF0000'>";
$fontend = "</font>";
} else {
$fontstart = "";
$fontend = "";
}

echo "
<tr>
<td class='$class' style='border-left: 1px solid #AAAAAA; padding: 3px;' NOWRAP><input type='checkbox' name='delete_$user[u_id]' value='yes'></td>
<td class='$class' style='border-left: none;' NOWRAP>$user[u_id]&nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP>$fontstart$user[username]$fontend&nbsp;</td>
<td class='$class' style='border-left: none;'>$user[lname]"; if(str_replace(" ", "", $user[fname]) != "") { echo ", $user[fname]"; } echo "&nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP><a href='mailto:$user[email]'>$user[email]</a></td>
"; if($admin_info[signup_emailverify] == "1") { 
if($user[verified] == "1") { $verified = "$admin_users24"; } else { $verified = "$admin_users25"; }
echo "
<td class='$class' style='border-left: none;' NOWRAP>$verified</td>
"; }
if($user[enabled] == "1") { $enabled = "$admin_users24"; } else { $enabled = "$admin_users25"; }
echo "
<td class='$class' style='border-left: none;' NOWRAP>$enabled</td>
<td class='$class' style='border-left: none;' NOWRAP>$user[entries]&nbsp;</td>
<td class='$class' style='border-left: none;' NOWRAP>".cdate("M. j, Y", $user[signup_date])."&nbsp;</td>
<td class='$class' style='border-right: 1px solid #AAAAAA; border-left: none;' NOWRAP><a href='admin_useredit.php?p=$p&s=$s&u=$u&u_in=$u_in&u_id=$user[u_id]'>$admin_users18</a> <font class='gray'>|</font> <a href='admin_userdelete.php?p=$p&s=$s&u=$u&u_in=$u_in&u_id=$user[u_id]'>$admin_users19</a> <font class='gray'>|</font> <a href='user_cookies.php?u_id=$user[u_id]' target='_blank'>$admin_users20</a></td>
</tr>
";
}

echo "
</table>
<br>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='u' value='$u'>
<input type='hidden' name='u_in' value='$u_in'>
<input type='hidden' name='task' value='delete'>
<input class='button' type='submit' value='$admin_users21'>
</form>
";

echo $foot;
?>