<?php
include ("../config.php");
if (((($_GET['load'] == "help") or ($_GET['load'] == "chat") or ($_GET['load'] == "login") or ($_GET['load'] == "logout")))) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_header() . " <?php ");
}
} else {
eval (" ?>" . admin_header() . " <?php ");
echo '<SCRIPT LANGUAGE="JavaScript">var checkflag = "false";function check(field) {if (checkflag == "false") {for (i = 0; i < field.length; i++) {field[i].checked = true;}checkflag = "true";return "Uncheck All"; }else {for (i = 0; i < field.length; i++) {field[i].checked = false; }checkflag = "false";return "Check All"; }}</script>';
}
if ($ipbancheck3 == "0") {if ($numv == "0"){
	if ($warn == $naum) {
	echo "You Do Not Have Access to the Admin Panel";
}
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$from = (($page * $setting[max_results]) - $setting[max_results]);

//if ($_GET['load'] == "users") {
$admin_page = "yes";
if ((($userlevel == "3") or ($userlevel == "4") or ($userlevel == "5"))) {
	echo "Sorry ".$_COOKIE[$pre."username"].", but you do not have permission to manage users. You are only a $level.";
} else {

if ($_GET['view'] == "search") {
	echo "<title>OneCMS Admins > Users > Search</title><form action='users.php?load=users&view=search' method='get'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\"><input type='hidden' name='view' value='search'><tr><td>Search for user</td><td><input type='text' name='search'></td><td><input type='submit' value='Search'></td></tr></table></form>";

	echo "<form action='users.php?load=users&view=manage' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\"><tr><td><b>Username</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr>";

	$query="SELECT * FROM ".$pre."users WHERE username LIKE '%" . $_GET['search'] . "%' ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
    	echo "<tr><td><a href='".$elite1.$row[username].$elite2."'>".$row[username]."</a></td><td><input type=\"checkbox\" name=\"id[]\" value=\"".$row[id]."\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"".$row[id]."\"></td></tr>";
    }

echo "<tr><td><div align='right'><input type='submit' name='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td><td><a href=\"users.php?load=users&view=add\">Add user</a></td><td><a href='users.php?load=users&view=manage&change=1'>Change Passwords</a></td></tr></form></table><br><br>";

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."users WHERE username LIKE '%" . $_GET['search'] . "%'"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=search&search=".$_GET['search']."&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=search&search=".$_GET['search']."&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=search&search=".$_GET['search']."&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if ($_GET['view'] == "") {
	echo "<title>OneCMS Admins > Manage Users</title><form action='users.php?load=users&view=search' method='get'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\"><input type='hidden' name='view' value='search'><tr><td>Search for user</td><td><input type='text' name='search'></td><td><input type='submit' value='Search'></td></tr></table></form>";

	echo "<form action='users.php?load=users&view=manage' name='form1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\"><tr><td><b>Username</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr>";

$query="SELECT * FROM ".$pre."users ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
    	echo "<tr><td><a href='".$elite1.$row[username].$elite2."'>".$row[username]."</a></td><td><input type=\"checkbox\" name=\"id[]\" value=\"".$row[id]."\"></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"".$row[id]."\"></td></tr>";
    }

echo "<tr><td><div align='right'><input type='submit' name='submit' value='Submit'></td><td><input type=button value='Check All' onClick='this.value=check(this.form)'></td><td><a href=\"users.php?load=users&view=add\">Add user</a></td><td><a href='users.php?load=users&view=manage&change=1'>Change Passwords</a></td></tr></form></table><br><br>";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."users"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&page=$i\">$i</a>&nbsp;";if (($i/25) == (int)($i/25)) {echo "<br>";}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "manage") && ($_POST['delete']) && ($_GET['confirm'] == "")) {
echo "<form action='users.php?load=users&view=manage&confirm=yes' method='post'>Are you sure you want to delete these user(s)?<br><input type='submit' name='de' value='Yes'>";
while (list(, $value) = each ($_POST['delete'])) {
	echo "<input type=\"hidden\" name=\"delete[]\" value=\"$value\">";
}
echo "</form>";
}

if ((($_GET['view'] == "manage") && ($_POST['id']) && ($_GET['update'] == ""))) {
	echo "<form action='users.php?load=users&view=manage&update=yes' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\">";

	while (list(, $val) = each ($_POST['id'])) {

	$query="SELECT * FROM ".$pre."users WHERE id = '$val'";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {

    echo "<input type=\"hidden\" name=\"id[]\" value=\"$val\"><tr><td><b><center>User #".$val."</b></center></td></tr><tr><td>Username</td><td><input type=\"text\" name=\"name_$val\" value=\"$row[username]\"><input type=\"hidden\" name=\"name2_$val\" value=\"$row[username]\"></td></tr><tr><td>Email</td><td><input type=\"text\" name=\"email_$val\" value=\"$row[email]\"></td></tr><tr><td>User Level</td><td><select name=\"level_$val\" multiple><option value=\"$row[level]\" selected>-- $row[level] --</option>";
	}

    $query2="SELECT * FROM ".$pre."userlevels";
	$result2=mysql_query($query2);
	while($row2 = mysql_fetch_array($result2)) {
	echo "<option value=\"$row2[name]\">$row2[name]</option>";
	}
	echo "</select></td></tr><tr><td>List this user on <a href='".$siteurl."/staff.php'>staff</a> page?</td><td><input type='checkbox' name='slist_".$val."' value='Yes'";

$fetch = mysql_fetch_row(mysql_query("SELECT slist FROM ".$pre."users WHERE id = '".$val."'"));

if ($fetch[0] == "Yes") {
echo "checked";
}
echo "></td></tr>";
	}
	echo "<tr><td><input type='submit' name='editcon' value='Submit'></td></tr></table></form>";
}

if (($_GET['view'] == "manage") && ($_GET['change'] == "1")) {
	echo "<form action='users.php?load=users&view=manage&change=2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"550\" align=\"center\"><tr><td>Username</td><td><select name=\"b\">";
	$query="SELECT * FROM ".$pre."users ORDER BY `username` ASC";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		echo "<option value=\"$row[username]\">$row[username]</option>";
	}

echo "</select></td></tr><tr><td>New Password</td><td><input type=\"password\" name=\"pass\"></td></tr><tr><td>Sent this user a PM with there new password?</td><td><input type='checkbox' name='send'></td></tr><tr><td><input type='submit' name='editcon' value='Submit'></td></tr></table></form>";
}

if (($_GET['view'] == "manage") && ($_GET['change'] == "2")) {
$pass = md5($salt.$_POST['pass']);
$r = mysql_query("UPDATE ".$pre."users SET password = '$pass' WHERE username = '".$_POST['b']."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
if ($_POST['send'] == "") {
} else {
$sent = mysql_query("INSERT INTO ".$pre."pm VALUES (null, '1', 'You have a new password, ".$_POST['b']."', '$username has changed your password. You can find the new password below:<br><br>".$_POST['pass']."<br><br>Please keep this password in your records. Thank you.', '$username', '".$_POST['b']."', '".time()."', 'inbox')") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$sent."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($r == TRUE) {
echo re_direct("1500", "users.php?load=users");
echo "The user <b>".$_POST['b']."</b> now has a new password. <a href=\"users.php?load=users\">Return to User Manager Home</a>";
}
}

if (($_GET['view'] == "manage") && ($_GET['update'] == "yes")) {
while (list(, $val) = each ($_POST['id'])) {
   $num = mysql_num_rows(mysql_query("SELECT * FROM ".$pre."users WHERE username = '".$_POST["name_$val"]."'"));

   if ($num > "1") {
   echo "Sorry, but the username <b>".$_POST["name_$val"]."</b> is already in use. Go back and choose another name.<br><br>";
   } else {

   $upd = "UPDATE ".$pre."users SET username = '".$_POST["name_$val"]."', email = '".$_POST["email_$val"]."', level = '".$_POST["level_$val"]."'";
   
   if ($_POST["slist_$val"]) {
   $upd .= ", slist = 'Yes'";
   } else {
   $upd .= ", slist = ''";
   }
   
   $upd .= "WHERE id = '$val'";
   $r = mysql_query($upd) or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$upd."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
   $s = mysql_query("UPDATE ".$pre."permissions SET username = '".$_POST["name_$val"]."' WHERE username = '".$_POST["name2_$val"]."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$s."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
   }
if (($s == TRUE) && ($r == TRUE)) {
echo re_direct("1500", "users.php?load=users");
echo "The users have been updated. <a href=\"users.php?load=users\">Return to User Manager Home</a>";
}
}
}

if (($_GET['view'] == "manage") && ($_GET['confirm'] == "yes")) {
while (list(, $val) = each ($_POST['delete'])) {

	$sql = @mysql_fetch_row(mysql_query("SELECT username FROM ".$pre."users WHERE id = '$val'"));

	$delete2 = mysql_query("DELETE FROM ".$pre."permissions WHERE username = '".$sql[0]."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete2."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

	$delete3 = mysql_query("DELETE FROM ".$pre."profile WHERE username = '".$sql[0]."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete3."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

	$delete = mysql_query("DELETE FROM ".$pre."users WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($delete == TRUE) {
echo re_direct("1500", "users.php?load=users");
echo "The user(s) have been deleted. <a href=\"users.php?load=users\">Return to Manage user Home</a>";
}
}

if (((($_GET['view'] == "levels") && ($_GET['add'] == "") && ($_GET['edit'] == "") && ($_GET['delete'] == "")))) {
	echo "<form action='users.php?load=users&view=levels&delete=1' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Name</b></td><td><b>Level</b></td><td><b><b>Edit</b></td><td><b>Delete</b></td></tr>";

	$query="SELECT * FROM ".$pre."userlevels ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[name]";
		$level = "$row[level]";
		$name = stripslashes($name2);
    	echo "<tr><td>$name</td><td>$level</td><td><a href='users.php?load=users&view=levels&edit=1&id=$id'>Edit</a></td><td><input type=\"checkbox\" name=\"del[]\" value=\"$id\"></td></tr>";
    }
		echo "<tr><td><input type=\"submit\" name=\"delete\" value=\"Delete Levels\"></td><td><a href='users.php?load=users&view=levels&add=1'>Add Levels</a></td></tr></form></table>";
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."userlevels"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=levels&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=levels&page=$i\">$i</a>&nbsp;";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=levels&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "levels") && ($_GET['delete'] == "1")) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';

while (list(, $val) = each ($_POST['del'])) {
$delete = mysql_query("DELETE FROM ".$pre."userlevels WHERE id = '$val'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
if ($delete == TRUE) {
echo re_direct("1500", "users.php?load=users&view=levels");
echo "The userlevel(s) have been deleted. <a href=\"users.php?load=users&view=levels\">Return to User Levels Home</a>";
}
}

if (($_GET['view'] == "levels") && ($_GET['edit'] == "1")) {
	echo "<form action='users.php?load=users&view=levels&edit=2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\">";

	$query="SELECT * FROM ".$pre."userlevels WHERE id = '".intval($_GET['id'])."'";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
	$title2 = "$row[name]";
	$level = "$row[level]";
	echo "<tr><td>Title</td><td><input type=\"text\" name=\"name\" value=\"$title2\"><input type=\"hidden\" name=\"name2\" value=\"$title2\"></td></tr><tr><td>Level</td><td><select name='level'><option value='$level'>-- $level --</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option></select><input type='hidden' name='id' value='".intval($_GET['id'])."'></td></tr>";
	}

	echo "<tr><td><input type=\"submit\" name=\"edit\" value=\"Submit Changes\"></td></tr></form></table>";
}

if (($_GET['view'] == "levels") && ($_GET['edit'] == "2")) {
	$r = mysql_query("UPDATE ".$pre."userlevels SET name = '".$_POST["name"]."', level = '".$_POST["level"]."' WHERE id = '".$_POST['id']."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

	$s = mysql_query("UPDATE ".$pre."users SET level = '".$_POST["name"]."' WHERE level = '".$_POST['name2']."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$s."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

	if (($r == TRUE) && ($s == TRUE)) {
	echo re_direct("1500", "users.php?load=users&view=levels");
	echo "User Levels have been update. <a href='users.php?load=users&view=levels'>Return back to User Levels Management</a>";
	}
}

if (($_GET['view'] == "levels") && ($_GET['add'] == "1")) {
		echo "<form action=\"users.php?load=users&view=levels&add=1\" method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td>How many levels to add?</td><td><input type='text' name='search'></td><td><input type='submit' name='addd' value='Submit'></td></tr></table></form>";

		echo "<form action='users.php?load=users&view=levels&add=2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

    if ($_POST['search']) {
    for($i = 0; $i < $_POST['search']; $i = $i+1)
	echo "<tr><td>Name</td><td><input type=\"text\" name='name_".$i."'></td><td>Level</td><td><select name='level_".$i."'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option></select></td></tr>";
	}
	echo "<input type=\"hidden\" name=\"s\" value='".$_POST['search']."'>";
			echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr></form></table>";
	}

if (($_GET['view'] == "levels") && ($_GET['add'] == "2")) {
   for($i = 0; $i < $_POST['s']; $i = $i+1) {
   $r = mysql_query("INSERT INTO ".$pre."userlevels VALUES (null, '".$_POST["name_$i"]."', '".$_POST["level_$i"]."')") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
   }
if ($r == TRUE) {
echo re_direct("1500", "users.php?load=users&view=levels");
echo "The user levels have been created. <a href=\"users.php?load=users&view=levels\">Return to User Levels Manage</a>";
}
}

if (((($_GET['view'] == "permissions") && ($_GET['add'] == "") && ($_GET['edit'] == "") && ($_GET['delete'] == "")))) {
	echo "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Username</b></td><td><b><b>Edit</b></td></tr>";

	$query="SELECT * FROM ".$pre."permissions ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id2 = mysql_fetch_row(mysql_query("SELECT id FROM ".$pre."users WHERE username = '".$row[username]."'"));
		$id = "$row[id]";
		$name2 = "$row[username]";
		$name = stripslashes($name2);
    	echo "<tr><td><a href='".$elite1.$id2[0].$elite2."' target='popup'>$name</a></td><td><input type=checkbox onclick=\"window.location='users.php?load=users&view=permissions&edit=1&id=$id'; return true;\"></td></tr>";
    }
		echo "</form></table>";
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."permissions"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=permissions&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=permissions&page=$i\">$i</a>&nbsp;";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=permissions&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "permissions") && ($_GET['edit'] == "1")) {
	echo "<form action='users.php?load=users&view=permissions&edit=2' method='post'><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";

	$query="SELECT * FROM ".$pre."permissions WHERE id = '".intval($_GET['id'])."'";
	$result=mysql_query($query);
	while($r = mysql_fetch_array($result)) {

	echo "<input type=\"hidden\" name=\"id\" value=\"".intval($_GET['id'])."\"><input type=\"hidden\" name=\"username\" value=\"$r[username]\"><tr><td><b>Username</b></td><td><a href='".$elite1.$r[username].$elite2."'>".$r[username]."</a></td></tr><tr><td><b>Force Validation?</b></td><td><input type='checkbox' name='ver' value='yes'";
		
		if ($r[ver] == "yes") {
		echo " checked";
		}
		echo "></td></tr><tr><td><b>Games</b></td><td><input type='checkbox' name='games' value='yes'";
		
		if ($r[games] == "yes") {
		echo " checked";
		}
		echo "></td></tr>";

	$query="SELECT * FROM ".$pre."cat";
	$result=mysql_query($query);
	while($z = mysql_fetch_array($result)) {
		$name = "$z[name]"; 

		echo "<tr><td><b>$z[name]</b></td><td><input type='checkbox' name='".$name."' value='yes'";
		
		if ($r["$name"] == "yes") {
		echo " checked";
		}
		echo "></td></tr>";
	}
	}
		echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr></form></table>";
}

if (($_GET['view'] == "permissions") && ($_GET['edit'] == "2")) {
   $upd = "UPDATE ".$pre."permissions SET ver = '".$_POST['ver']."', games = '".$_POST['games']."'";
   $result = mysql_query("SELECT * FROM `".$pre."cat`");
   $count = 0;
   while ($row = mysql_fetch_array($result)) {
   	$name = $row['name'];
   		$upd .= ", " . $name . " = '" . $_POST["$name"] . "'"; // Part 2
   	}
   $upd .= " WHERE id = '" . $_POST['id'] . "'";
   $r = mysql_query($upd) or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

if ($r == TRUE) {
echo re_direct("1500", "users.php?load=users&view=permissions");
echo "The user permissions have been updated. <a href=\"users.php?load=users&view=permissions\">Return to User Permissons Manage</a>";
}
}

if (($_GET['view'] == "ban") && ($_GET['edit'] == "")) {
	echo "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Username</b></td><td><b>Site Ban?</b></td><td><b>CP Ban?</b></td><td><b><b>Edit</b></td></tr>";

	$query="SELECT * FROM ".$pre."users ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[username]";
		$name = stripslashes($name2);
    	echo "<tr><td><a href='".$elite1.$row[username].$elite2."' target='popup'>$name</a></td><td>$row[bansite]</td><td>$row[banadmin]</td><td><a href='users.php?load=users&view=ban&edit=1&id=$id'>Edit</a></td></tr>";
    }
		echo "</form></table>";
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."users"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=ban&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=ban&page=$i\">$i</a>&nbsp;";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=ban&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "ban") && ($_GET['edit'] == "1")) {
	echo "<form action='users.php?load=users&view=ban&edit=2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

	$query="SELECT * FROM ".$pre."users WHERE id = '".intval($_GET['id'])."'";
	$result=mysql_query($query);
	while($row2 = mysql_fetch_array($result)) {
		$username = "$row2[username]";

	echo "<input type=\"hidden\" name=\"id\" value=\"".intval($_GET['id'])."\"><tr><td><b>Username</b></td><td><a href='".$elite1.$row[username].$elite2."'>$username</a></td></tr><tr><td><b>Ban From Site?</b></td><td><select name=\"bansite\" multiple><option value=\"".$row2["bansite"]."\" selected>-- ".$row2["bansite"]." --</option><option value='yes'>Yes</option><option value='no'>No</option></select></td></tr>";
	echo "<tr><td><b>Ban From AdminCP?</b></td><td><select name=\"banadmin\" multiple><option value=\"".$row2["banadmin"]."\" selected>-- ".$row2["banadmin"]." --</option><option value='yes'>Yes</option><option value='no'>No</option></select></td></tr>";
	}
			echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Submit Changes\"></td></tr></form></table>";
}

if (($_GET['view'] == "ban") && ($_GET['edit'] == "2")) {
$r = mysql_query("UPDATE ".$pre."users SET bansite = '".$_POST['bansite']."', banadmin = '".$_POST['banadmin']."' WHERE id = '" . $_POST['id'] . "'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
if ($r == TRUE) {
echo re_direct("1500", "users.php?load=users&view=ban");
echo "The user ban have been updated. <a href=\"users.php?load=users&view=ban\">Return to Ban Control Manage</a>";
}
}

if (($_GET['view'] == "warn") && ($_GET['edit'] == "")) {
    echo "<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Username</b></td><td><b>Warning Points</b></td><td><b><b>Edit</b></td></tr>";

	$query="SELECT * FROM ".$pre."users ORDER BY `id` DESC LIMIT $from, $setting[max_results]";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		$id = "$row[id]";
		$name2 = "$row[username]";
		$name = stripslashes($name2);
    	echo "<tr><td><a href='".$elite1.$row[username].$elite2."' target='popup'>$name</a></td><td>$row[warn]</td><td><a href='users.php?load=users&view=warn&edit=1&id=$id'>Edit</a></td></tr>";
    }
		echo "</form></table>";
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."users"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=warn&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=warn&page=$i\">$i</a>&nbsp;";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=warn&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "warn") && ($_GET['edit'] == "1")) {
	echo "<form action='users.php?load=users&view=warn&edit=2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

	$query="SELECT * FROM ".$pre."users WHERE id = '".intval($_GET['id'])."'";
	$result=mysql_query($query);
	while($row2 = mysql_fetch_array($result)) {
		$username = "$row2[username]";
        $barg = "$row2[warn]";

	echo "<input type=\"hidden\" name=\"id\" value=\"".intval($_GET['id'])."\"><tr><td><b>Username</b></td><td><a href='".$elite1.$row[username].$elite2."'>$username</a></td></tr><tr><td><b>Warn Points (limit is ".$warn.")</b></td><td><select name=\"warnp\" multiple><option value=\"$barg\" selected>-- ".$barg." --</option>";
	for ($i = 0; $i <= $warn; $i++) {
    echo "<option value=\"$i\">$i</option>";
	}
	echo "</select></td></tr>";
	}
			echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Submit Changes\"></td></tr></form></table>";
}

if (($_GET['view'] == "warn") && ($_GET['edit'] == "2")) {
if ($_POST['warnp'] == $warn) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("You do realize setting his warn level to this will ban him, correct?\n Press ok to continue.");
if (agree)
document.write("");
else
history.go(-1);
</SCRIPT>';
} else {
$r = mysql_query("UPDATE ".$pre."users SET warn = '".$_POST['warnp']."' WHERE id = '".$_POST['id']."'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$r."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

if ($r == TRUE) {
echo re_direct("1500", "users.php?load=users&view=warn");
echo "The user warning points have been updated. <a href=\"users.php?load=users&view=warn\">Return to Warn Control Manager</a>";
}
}
}

if ($_GET['view'] == "add") {
	echo "<form action='users.php?load=users&view=add2' method='post'><table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" align=\"left\">";

	echo "<tr><td>Username</td><td><input type=\"text\" name='name'></td></tr><tr><td>Password</td><td><input type=\"password\" name='password1'></td></tr><tr><td>E-Mail</td><td><input type=\"text\" name='email'></td></tr><tr><td>User Level</td><td><select name='level' multiple size='5'>";
	
	$query="SELECT * FROM ".$pre."userlevels";
	$result=mysql_query($query);
	while($row = mysql_fetch_array($result)) {
		echo "<option value=\"$row[name]\">$row[name]</option>";
	}
		echo "</td></tr><tr><td>List this user on <a href='staff.php'>staff</a> page?</td><td><input type='checkbox' name='slist' value='Yes' checked></td></tr><tr><td><input type=\"submit\" name=\"Add\" value=\"Add User\"></td></tr></form></table>";
	}

if ($_GET['view'] == "add2") {
   if (($_POST['name']) && ($_POST['password1'])) {
   $sql = mysql_query("SELECT * FROM ".$pre."users WHERE username = '".$_POST["name"]."'");
   $num = mysql_num_rows($sql);

   $user = strip_tags(stripcslashes($_POST["name"]));

   if ($num > "0") {
	   echo "Sorry, but the username <b>".$user."</b> is already in use. Go back and choose another name.<br><br>";
   } else {

   $register3b = "INSERT INTO ".$pre."profile VALUES (null, '".$user."', '', '', '', '', '', '', '', '', '', '', ''";
   $resulte = mysql_query("SELECT * FROM ".$pre."fields WHERE cat = 'users' ORDER BY `id` DESC");
   while ($rowe = mysql_fetch_array($resulte)) {
	   $register3b .= ", ''";
   }
   
   $register3b .= ")";
   $register3 = mysql_query($register3b) or die(mysql_error());

   $upd4 = mysql_query("INSERT INTO `".$pre."pm` VALUES (null, '1', 'Welcome ".$user."!', 'Welcome to OneCMS ".$user.". Below you can find your user information, please keep record of this.<br><br>Username - ".$user."<br>Password - ".$_POST["password1"]."<br><br>Thanks!', '".$_COOKIE[$pre."username"]."', '".$user."', '".time()."', 'inbox')") or die(mysql_error());

   $upd = "INSERT INTO `".$pre."permissions` VALUES (null, '".$user."', 'no', 'yes'";

   $result = mysql_query("SELECT * FROM ".$pre."cat");
   while ($row = mysql_fetch_array($result)) {

   		$upd .= ", 'yes'";
   	}

	$upd .= ")";

	$r = mysql_query($upd) or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$upd."<br><b>Error:</b> (".mysql_errno().")".mysql_error());

   $rank = mysql_fetch_row(mysql_query("SELECT name FROM ".$pre."ranks WHERE points = '0' ORDER BY `points` DESC"));

   $password2 = md5($salt.$_POST["password1"]);
   $s = mysql_query("INSERT INTO ".$pre."users VALUES (null, '".$user."', '".$password2."', '".$_POST["email"]."', '".$_POST["level"]."', '".$rank[0]."', '0', 'no', 'no', '0', '8', '3', '".$_POST['slist']."')") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$s."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
}
if (($r == TRUE) && ($s == TRUE)) {
echo re_direct("1500", "users.php?load=users");
echo "The user has been created. <a href=\"users.php?load=users\">Return to Manage users Home</a><br>";
}
}
}

if (((($_GET['view'] == "log") && ($_GET['search'] == "") && ($_GET['del'] == "") && ($_GET['log'] == "")))) {
echo "<form name=\"search\" method=\"get\" action=\"users.php?load=users&view=log\"><input type='hidden' name='view' value='log'><table cellspacing=\"0\" cellpading=\"2\" border=\"0\" align=\"center\"><tr><td>Search by user  </td><td><select name=\"type\"><option value=\"username\">Username</option><option value=\"ip\">IP</option><option value=\"url\">URL</option><option value=\"date\">Date</option></select></td><td><input type=\"text\" name=\"search\"></td><td></td><td><input type=\"submit\" value=\"Search\"></td></tr></table></form><center><a href=\"#bottom\">Bottom of Page</a></center><br><br><form action='users.php?load=users&view=log&del=yes' method='post' name='checkboxform'><table cellspacing=\"1\" cellpadding=\"2\" border=\"0\" align=\"center\"><tr><td>Delete all Logs?</td><td><input type='checkbox' name='all' value='yes'></td></tr><tr><td><b><center>Username</center></b></td><td><b><center>IP</center></b></td><td><b><center>View Info</center></b></td><td><b><center>Delete?</center></b></td></tr>";

$sql = mysql_query("SELECT * FROM ".$pre."stats WHERE type = 'users.php' ORDER BY `id` DESC LIMIT $from, $setting[max_results]");
while($r = mysql_fetch_array($sql)) {

		echo "<tr><td>";
		if ($r[username]) {
		echo "<a href='".$elite1.$r[username].$elite2."'>".$r[username]."</a>";
		} else {
		echo "Guest";
		}
		echo "</td><td><center>".$r[ip]."</center></td><td><center><a href='users.php?load=users&view=log&log=".$r[id]."'>View</a></center></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"".$r[id]."\"></td></center></td></tr>";
}

		echo "<tr><td><input type='submit' name='deletebiatch' value='Delete'></td><td><input type=\"button\" value=\"Check All\" onClick=\"checkAll()\"></td><td><input type=\"button\" value=\"Uncheck All\" onClick=\"uncheckAll()\"></td></tr></form></table><br><br>";

	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats WHERE type = 'users.php'"),0);

$total_pages = ceil($total_results / $setting[max_results]);
if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=log&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=log&page=$i\">$i</a>&nbsp;";
if (($i/25) == (int)($i/25)) {
  echo "<br>";
}
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=log&page=$next\">Next>></a>";
}
echo "</center>";
}
echo "<a name=\"bottom\">";
}

if (((($_GET['view'] == "log") && ($_GET['search']) && ($_GET['del'] == "") && ($_GET['log'] == "")))) {
echo "<center><a href=\"#bottom\">Bottom of Page</a></center><br><br><form action='users.php?load=users&view=log&del=yes' method='post' name='checkboxform'><table cellspacing=\"0\" cellpadding=\"1\" border=\"0\" align=\"center\"><tr><td>Delete all Logs?</td><td><input type='checkbox' name='all' value='yes'></td></tr><tr><td><b><center>Username</center></b></td><td><b><center>IP</center></b></td><td><b><center>View Info</center></b></td><td><b><center>Delete?</center></b></td></tr>";

$type = $_GET['type'];
$keyword = $_GET['search'];

$query2 = mysql_query("SELECT * FROM ".$pre."log WHERE $type LIKE '%" . $keyword . "%'") or die(mysql_error());
$num = mysql_num_rows($query2);

echo "<center>Number of Results: <b>$num</b></center><br><br>";

echo "<title>$num Results for '$keyword'</title>";

$query = mysql_query("SELECT * FROM ".$pre."stats WHERE ptype = 'users.php' AND ".$type." LIKE '%".$keyword."%' ORDER BY `id` DESC LIMIT $from, $setting[max_results]") or die(mysql_error());
while($r = mysql_fetch_array($query)) {
                
        $patterns[0] = $_POST['keyword'];
        $replacements[0] = "<b>".$_POST['keyword']."</b>";
        $ip = str_replace($patterns, $replacements, $r[ip]);
		$url = str_replace($patterns, $replacements, $r[page]);
		$user = str_replace($patterns, $replacements, $r[username]);
        $date = str_replace($patterns, $replacements, $r[date]);

		if ($user == "") {
		$user = "Unknown";
		}

echo "<tr><td><center><a href='".$elite1.$user.$elite2."'>".$user."</a></center></td><td><center>".$ip."</center></td><td><center><a href=\"users.php?load=users&view=log&log=".$id."\">View</a></center></td><td><input type=\"checkbox\" name=\"delete[]\" value=\"".$id."\"></td></center></td></tr>";
}

if ($id == "") {
echo "<tr><td>Sorry, but there are no results for <b>".$keyword."</b></td></tr>";
}

echo "<tr><td><input type='submit' name='deletebiatch' value='Submit'></td><td><input type=\"button\" value=\"Check All\" onClick=\"checkAll()\"></td><td><input type=\"button\" value=\"Uncheck All\" onClick=\"uncheckAll()\"></td></tr></form></table><br><br>";

echo "<a name=\"bottom\">";
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ".$pre."stats WHERE ptype = 'users.php' AND $type LIKE '%" . $keyword . "%'"),0);
$total_pages = ceil($total_results / $setting[max_results]);

if ($total_pages > 1) {
echo "<center>";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"users.php?load=users&view=log&type=$type&search=$keyword&page=$prev\"><<Previous</a>&nbsp;";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i&nbsp;";
        } else {
            echo "<a href=\"users.php?load=users&view=log&type=$type&search=$keyword&page=$i\">$i</a>&nbsp;";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"users.php?load=users&view=log&type=$type&search=$keyword&page=$next\">Next>></a>";
}
echo "</center>";
}
}

if (($_GET['view'] == "log") && ($_GET['del'] == "yes")) {
echo '<SCRIPT LANGUAGE="JavaScript">
var agree=confirm("Confirm Deletion?");
if (agree)
document.write("");
else
history.go(-1);
// End -->
</SCRIPT>';

if ($_POST['all']) {
$delete = mysql_query("DELETE FROM ".$pre."stats WHERE ptype = 'users.php'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
} else {
while (list(, $val) = each ($_POST['delete'])) {
$delete = mysql_query("DELETE FROM ".$pre."stats WHERE id = '".$val."' AND ptype = 'users.php'") or die("<font color='red'><b>A fatal MySQL error occured</b></font>.<br><br><b>Query:</b> ".$delete."<br><b>Error:</b> (".mysql_errno().")".mysql_error());
}
}
if ($delete == TRUE) {
echo re_direct("1500", "users.php?load=users&view=log");
echo "The log(s) have been deleted. <a href=\"users.php?load=users&view=log\">Return to IP Logger</a>";
}
echo "</form>";
}

if (($_GET['view'] == "log") && (is_numeric($_GET['log']))) {
$sql = mysql_query("SELECT * FROM ".$pre."stats WHERE id = '".$_GET['log']."'");
while($r = mysql_fetch_array($sql)) {
echo "<table cellspacing=\"2\" cellpadding=\"3\" border=\"0\" align=\"center\"><tr><td><b>Username:</b></td><td>";
if ($r[username]) {
echo "<a href='".$elite1.$r[username].$elite2."'>".$r[username]."</a>";
} else {
echo "Guest";
}
echo "</td></tr><tr><td><b>IP Address:</b></td><td><a href='http://www.dnsstuff.com/tools/whois.ch?ip=".$r[ip]."'>".$r[ip]."</a></td></tr><tr><td><b>Date:</b></td><td>".date("M d, y h:i a", $r[date])."</td></tr><tr><td><b>URL Accessed:</b></td><td><a href='".$siteurl.$r[page]."'>".$r[page]."</a></td></tr><tr><td><b>Log Number:</b></td><td>".$_GET['log']."</td></tr></table>";
}
}
//}
}
}
if (($_GET['load'] == "help") or ($_GET['load'] == "chat")) {
if (($_GET['load'] == "help") && ($_GET['id'] == "list3")) {
eval (" ?>" . admin_footer() . " <?php ");
}
} else {
eval (" ?>" . admin_footer() . " <?php ");
}
?>
