<?
$page = "admin_userdelete.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 = "i"; }
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 = ""; }
if(isset($_POST['u_id'])) { $u_id = $_POST['u_id']; } elseif(isset($_GET['u_id'])) { $u_id = $_GET['u_id']; } else { header("Location: admin_users.php?p=$p&s=$s&u=$u"); exit(); }

$query = mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'");
if(mysql_num_rows($query) == 0) { exit(); }
$thisuser = mysql_fetch_assoc($query);

if($task == "main") {
echo $head;
echo "
<h2>$admin_userdelete1</h2>
$admin_userdelete2 \"<b>$thisuser[username]</b>\"?<br>$admin_userdelete5
<br><br>

<table cellpadding='0' cellspacing='0'>
<form action='admin_userdelete.php' method='POST'>
<tr><td>
<input type='submit' value='$admin_userdelete3' 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'>
<input type='hidden' name='u_id' value='$u_id'>
<input type='hidden' name='task' value='delete_do'>
</td>
</form>
<form action='admin_users.php' method='POST'>
<td>
<input type='submit' value='$admin_userdelete4' 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='u_id' value='$u_id'>
<input type='hidden' name='task' value='main'>
</td>
</tr>
</form>
</table>
";
echo $foot;
exit();
}


if($task == "delete_do") {
mysql_query("DELETE FROM bhost_users WHERE u_id='$u_id'");
mysql_query("DELETE FROM bhost_profile WHERE u_id='$u_id'");
mysql_query("DELETE FROM bhost_friends WHERE u_id='$u_id' OR friend_u_id='$u_id'");
mysql_query("DELETE FROM bhost_pms WHERE to_u_id='$u_id'");
 $weblogs = mysql_query("SELECT w_id FROM bhost_weblogs WHERE u_id='$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 '%,$u_id,%' OR level2 LIKE '%,$u_id,%' OR invited LIKE '%,$u_id,%'");
 while($multiauthor_weblog = mysql_fetch_assoc($multiauthor_weblogs)) {
  $newlevel1 = str_replace(",$u_id,", ",", $multiauthor_weblog[level1]);
  $newlevel2 = str_replace(",$u_id,", ",", $multiauthor_weblog[level2]);
  $newinvited = str_replace(",$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($thisuser[username], 0, 1));
$files = mysql_query("SELECT * FROM bhost_uploads WHERE u_id='$u_id'");
while($file = mysql_fetch_assoc($files)) {
 if($file[up_id] != ".." & $file[up_id] != ".") {
  @unlink("../uploads/$letter/$thisuser[username]/$file[up_id].$file[ext]");
  if(file_exists("../uploads/$letter/$thisuser[username]/$file[up_id]_thumb.$file[ext]")) {
   unlink("../uploads/$letter/$thisuser[username]/$file[up_id]_thumb.$file[ext]");
  }
 }
}
$avatar = "../uploads/avatars/".basename($thisuser[avatar]);
@unlink($avatar);
@unlink("../uploads/$letter/$thisuser[username]/index.php");
@rmdir("../uploads/$letter/$thisuser[username]");

mysql_query("DELETE FROM bhost_uploads WHERE u_id='$u_id'");
mysql_query("DELETE FROM bhost_folders WHERE u_id='$u_id'");

header("Location: admin_users.php?p=$p&s=$s&u=$u&u_in=$u_in");
exit();
}

?>