<?
$page = "admin_useredit.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(); }
if(mysql_num_rows(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'")) == 1) { $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE u_id='$u_id'")); } else { header("Location: admin_users.php?p=$p&s=$s&u=$u"); exit(); }


if($task == "edit") {
$enabled = $_POST['enabled'];
$password = $_POST['password'];
$fname = str_replace("'", "&#39;", $_POST['fname']);
$lname = str_replace("'", "&#39;", $_POST['lname']);
$email = $_POST['email'];
$g_id = $_POST['g_id'];
$notes = $_POST['notes'];
$is_error = "no";
$error = "";

// FIELD ERRORS
if($password == "" OR strlen($password) < 4) { $is_error = "yes"; $error = $admin_useredit48; }
if(preg_match("/[^a-z,A-Z,0-9]/", $password) & $password != "********") { $is_error = "yes"; $error = $admin_useredit50; }
if(!preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email)) { $is_error = "yes"; $error = $admin_useredit51; }


if($is_error == "yes") {
echo $head;
echo "
<h2>$admin_useredit52</h2>
$error<br><br><input type='button' class='button' value='$admin_useredit53' onClick='history.go(-1);'>
";
echo $foot;
exit();
}

if($password == "********") {
$password = $user_info[password];
} else {
$password = crypt($password, $user_info[code]);
}
mysql_query("UPDATE bhost_users SET g_id='$g_id', enabled='$enabled', password='$password', fname='$fname', lname='$lname', email='$email', notes='$notes' WHERE u_id='$u_id'");
header("Location: admin_users.php?p=$p&s=$s&u=$u&u_in=$u_in");
exit();
}




if($task == "resend") {
$server_array = explode("/", $_SERVER['PHP_SELF']);
$server_array_mod = array_pop($server_array);
$server_array_mod = array_pop($server_array);
$server_info = implode("/", $server_array);
$server = "http://".$_SERVER['HTTP_HOST'].$server_info;
$verify_subject = $admin_info[verify_subject];
$verify_subject = str_replace("<fname>", $user_info[fname], $verify_subject);
$verify_subject = str_replace("<lname>", $user_info[lname], $verify_subject);
$verify_subject = str_replace("<link>", "$server/verify.php?u=$user_info[u_id]", $verify_subject);
$verify_message = $admin_info[verify_message];
$verify_message = str_replace("<fname>", $user_info[fname], $verify_message);
$verify_message = str_replace("<lname>", $user_info[lname], $verify_message);
$verify_message = str_replace("<link>", "$server/verify.php?u=$user_info[u_id]&c=$user_info[code]", $verify_message);
$verify_headers = "From: $admin_info[fname] $admin_info[lname] <$admin_info[email]>";
mail($user_info[email], $verify_subject, $verify_message, $verify_headers);

echo $head;
echo "$admin_useredit37
<form action='admin_useredit.php' method='POST'>
<input type='submit' class='button' value='$admin_useredit38'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='u_id' value='$u_id'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='u' value='$u'>
<input type='hidden' name='u_in' value='$u_in'>
</form>
";
echo $foot;
exit();
}



$total_friends = mysql_num_rows(mysql_query("SELECT bhost_friends.u_id, bhost_friends.friend_u_id, bhost_users.username FROM bhost_friends LEFT JOIN bhost_users ON bhost_friends.friend_u_id=bhost_users.u_id WHERE bhost_friends.u_id='$user_info[u_id]'"));
$friend_of = mysql_num_rows(mysql_query("SELECT bhost_friends.u_id, bhost_friends.friend_u_id, bhost_users.username FROM bhost_friends LEFT JOIN bhost_users ON bhost_friends.u_id=bhost_users.u_id WHERE bhost_friends.friend_u_id='$user_info[u_id]' ORDER BY bhost_users.username"));
$total_logins = mysql_num_rows(mysql_query("SELECT l_id FROM bhost_log WHERE username='$user_info[username]' AND result='1'"));
$last_login = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_log WHERE username='$user_info[username]' AND result='1' ORDER BY l_id DESC LIMIT 1"));
$group_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_groups WHERE g_id='$user_info[g_id]'"));
$total_entries = mysql_num_rows(mysql_query("SELECT e_id FROM bhost_entries WHERE author_u_id='$user_info[u_id]'"));
$total_comments = mysql_num_rows(mysql_query("SELECT c_id FROM bhost_comments WHERE author_u_id='$user_info[u_id]'"));

if($group_info[allow_uploads] == 1) {
$files = mysql_query("SELECT * FROM bhost_uploads WHERE u_id='$user_info[u_id]'");
$totalfiles = mysql_num_rows($files);
$totalsize = mysql_fetch_assoc(mysql_query("SELECT SUM(filesize) AS filesize FROM bhost_uploads WHERE u_id='$user_info[u_id]'"));
$totalsize = $totalsize[filesize];
$totalsize = ($totalsize / 1024) / 1024;
$totalsize = round($totalsize, 2);
$totalsize .= " MB";
$filestext = "$totalfiles $admin_useredit26 ($totalsize)";
} else {
$filestext = "$admin_useredit27";
}

if($user_info[last_login] == 0 OR $user_info[last_login] == "") {
$last_login_time = ""; 
} else {
$last_login_time = cdate("h:i:s A m/d/y", $user_info[last_login]);
}
echo $head;

echo "
<h2>$admin_useredit1</h2>
<table width='450' cellpadding='0' cellspacing='0' style='margin-top: 5px; border-top: 1px solid #AAAAAA;'>

<tr><td class='stats1' width='250'>$admin_useredit2</td><td class='stats2'><b>$user_info[username]</b> <font class='gray'>[</font> <a href='".url("profile", "", "$user_info[username]")."' target='_blank'>$admin_useredit29</a> <font class='gray'>]</font> &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit22</td><td class='stats2' NOWRAP><a href='mailto:$user_info[email]'>$user_info[email]</a> &nbsp;</td></tr>
"; if($admin_info[signup_emailverify] == "1") { 
if($user_info[verified] == "1") { $verified = "$admin_useredit34"; } else { $verified = "$admin_useredit35"; }
echo "
<tr><td class='stats1'>$admin_useredit33</td><td class='stats2' NOWRAP>$verified"; if($user_info[verified] != "1") { echo " <font class='gray'>[</font> <a href='admin_useredit.php?p=$p&s=$s&u=$u&u_in=$u_in&u_id=$user_info[u_id]&task=resend'>$admin_useredit36</a> <font class='gray'>]</font>"; } echo "&nbsp;</td></tr>
"; } echo "
<tr><td class='stats1'>$admin_useredit3</td><td class='stats2'>".cdate("h:i:s A m/d/y", $user_info[signup_date])." &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit4</td><td class='stats2'>$last_login_time &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit23</td><td class='stats2'>$total_logins $admin_useredit24 &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit25</td><td class='stats2'>$filestext &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit5</td><td class='stats2'>$total_entries $admin_useredit10 &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit6</td><td class='stats2'>$total_comments $admin_useredit11 &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit8</td><td class='stats2'>$total_friends $admin_useredit12 &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit9</td><td class='stats2'>$friend_of $admin_useredit12 &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit30</td><td class='stats2'>$user_info[signup_ip] &nbsp;</td></tr>
<tr><td class='stats1'>$admin_useredit31</td><td class='stats2'>$last_login[ipaddress] &nbsp;</td></tr>
</table>
<br>

<h2>$admin_useredit7</h2>
$admin_useredit28
<br><br>

<table width='450' cellpadding='0' cellspacing='0'>
<tr>
<td class='header' NOWRAP' align='center'><b>$admin_useredit42</b></td>
<td class='header' style='border-left: none;' width='350'><b>$admin_useredit43</b></a></td>
<td class='header' NOWRAP style='border-left: none;'><b>$admin_useredit44</b></td>
</tr>
";
$weblogs_own = mysql_query("SELECT * FROM bhost_weblogs WHERE u_id='$user_info[u_id]'");
$weblogs_admin = mysql_query("SELECT * FROM bhost_weblogs WHERE level1 LIKE '%,$user_info[u_id],%'");
$weblogs_member = mysql_query("SELECT * FROM bhost_weblogs WHERE level2 LIKE '%,$user_info[u_id],%'");
while($weblogs_own_info = mysql_fetch_assoc($weblogs_own)) {
echo "
<tr>
<td class='list2' NOWRAP align='center'>$weblogs_own_info[w_id]</td>
<td class='list2' style='border-left: none;' NOWRAP><a href='".url("index", "$weblogs_own_info[weblog]")."' target='_blank'>$weblogs_own_info[weblog]</a></td>
<td class='list2' style='border-left: none;' NOWRAP>$admin_useredit45</td>
</tr>
";
}
while($weblogs_admin_info = mysql_fetch_assoc($weblogs_admin)) {
echo "
<tr>
<td class='list2' NOWRAP align='center'>$weblogs_admin_info[w_id]</td>
<td class='list2' style='border-left: none;' NOWRAP><a href='".url("index", "$weblogs_admin_info[weblog]")."' target='_blank'>$weblogs_admin_info[weblog]</a></td>
<td class='list2' style='border-left: none;' NOWRAP>$admin_useredit46</td>
</tr>
";
}
while($weblogs_member_info = mysql_fetch_assoc($weblogs_member)) {
echo "
<tr>
<td class='list2' NOWRAP align='center'>$weblogs_member_info[w_id]</td>
<td class='list2' style='border-left: none;' NOWRAP><a href='".url("index", "$weblogs_member_info[weblog]")."' target='_blank'>$weblogs_member_info[weblog]</a></td>
<td class='list2' style='border-left: none;' NOWRAP>$admin_useredit47</td>
</tr>
";
}

echo "
</table>


<form action='admin_useredit.php' method='POST'>

<h2>$admin_useredit13</h2>
$admin_useredit14
<br><br>

$admin_useredit39*<br>
<select name='enabled'>
<option value='1'"; if($user_info[enabled] == 1) { echo " SELECTED"; } echo ">$admin_useredit40</option>
<option value='0'"; if($user_info[enabled] == 0) { echo " SELECTED"; } echo ">$admin_useredit41</option>
</select>
<br><br>

$admin_useredit15*<br>
<input type='password' name='password' class='text' value='********'>
<br><br>

$admin_useredit16<br>
<input type='text' name='fname' class='text' value='$user_info[fname]'>
<br><br>

$admin_useredit17<br>
<input type='text' name='lname' class='text' value='$user_info[lname]'>
<br><br>

$admin_useredit18*<br>
<input type='text' name='email' class='text' value='$user_info[email]'>
<br><br>

$admin_useredit32*<br>
<select name='g_id'>
";

$groups = mysql_query("SELECT g_id, group_name FROM bhost_groups ORDER BY group_name");
while($group_info = mysql_fetch_assoc($groups)) {
echo "<option value='$group_info[g_id]'"; if($user_info[g_id] == $group_info[g_id]) { echo " SELECTED"; } echo ">$group_info[group_name]</option>";
}

echo "
</select>
<br><br>

$admin_useredit19<br>
<textarea rows='3' cols='60' name='notes'>$user_info[notes]</textarea>
<br><br>

<table cellpadding='0' cellspacing='0'>
<tr>
<td>
<input type='hidden' name='task' value='edit'>
<input type='hidden' name='p' value='$p'>
<input type='hidden' name='u_id' value='$u_id'>
<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='submit' value='$admin_useredit20' class='button'>&nbsp;
</td>
</form>
<form action='admin_users.php' method='POST'>
<td>
<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='submit' value='$admin_useredit21' class='button'>
</td>
</tr>
</form>
</table>
";

echo $foot;
?>