<?
$page = "login.php";
include "header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
// FOR BACKWARDS COMPATIBILITY:
if(isset($_POST['u'])) { $_POST['w'] = $_POST['u']; } elseif(isset($_GET['u'])) { $_GET['w'] = $_GET['u']; }
if(isset($_POST['w'])) { $w = $_POST['w']; } elseif(isset($_GET['w'])) { $w = $_GET['w']; } else { $w = ""; }
if(isset($_POST['e_id'])) { $e_id = $_POST['e_id']; } elseif(isset($_GET['e_id'])) { $e_id = $_GET['e_id']; } else { $e_id = ""; }
if(isset($_POST['c_id'])) { $c_id = $_POST['c_id']; } elseif(isset($_GET['c_id'])) { $c_id = $_GET['c_id']; } else { $c_id = ""; }
if(isset($_POST['ref'])) { $ref = $_POST['ref']; } elseif(isset($_GET['ref'])) { $ref = $_GET['ref']; } else { $ref = ""; }





if($task == "forgot") {
echo $head;
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' class='box'>
<h2>$login1</h2>
$login2
<br>

<form action='login.php' method='POST'>
<b>$login3</b><br>

<table cellspacing='0' cellpadding='0'>
<tr>
<td><input type='text' class='text' name='username' maxlength='200'></td>
<td>&nbsp;<input type='submit' class='sbutton' name='submit' value='$login4' style='margin-bottom: 2px;'></td>
<input type='hidden' name='task' value='sendpass'>
</form>
<form action='login.php' method='POST'>
<td>&nbsp;<input type='submit' class='sbutton' name='submit' value='$login5' style='margin-bottom: 2px;'></td>
<input type='hidden' name='task' value='cancel'>
</form>
</tr>
</table>

</td>
</tr>
</table>
";
echo $foot;
}





if($task == "cancel") {
header("Location: login.php");
exit();
}





if($task == "sendpass") {
$username = $_POST['username'];

$user = mysql_query("SELECT * FROM bhost_users WHERE username='$username'");

if(mysql_num_rows($user) != 1) {
echo $head;
echo "
<h2>$login22</h2>
$login23
";
echo $foot;
exit();
}

$user_info = mysql_fetch_assoc($user);
$password = randomcode();
$password_db = crypt($password, $user_info[code]);
mysql_query("UPDATE bhost_users SET password='$password_db' WHERE u_id='$user_info[u_id]'");
$subject = str_replace("<fname>", "$user_info[fname]", $admin_info[forgot_subject]);
$subject = str_replace("<lname>", "$user_info[lname]", $subject);
$subject = str_replace("<username>", $user_info[username], $subject);
$subject = str_replace("<password>", $password, $subject);
$message = str_replace("<fname>", "$user_info[fname]", $admin_info[forgot_message]);
$message = str_replace("<lname>", "$user_info[lname]", $message);
$message = str_replace("<username>", $user_info[username], $message);
$message = str_replace("<password>", $password, $message);

$forgot_headers = "From: $admin_info[fname] $admin_info[lname] <$admin_info[email]>";
mail($user_info[email], $subject, $message, $forgot_headers);


echo $head;
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' class='box'>
<h2>$login1</h2>
$login11
<br>
<form action='login.php' method='POST'>
<input type='submit' class='sbutton' value='$login12'>
</form>
</td>
</tr>
</table>
";
echo $foot;
}







if($task == "dologin") {
$date = time();
$username = $_POST['username'];
$password_unencrypted = $_POST['password'];
$rememberme = $_POST['rememberme'];
$ipaddress = $_SERVER['REMOTE_ADDR'];

$check = mysql_query("SELECT username, password, code, verified FROM bhost_users WHERE username='$username'");
$check_info = @mysql_fetch_assoc($check);

$password = crypt($password_unencrypted, $check_info[code]);

// NO JAVASCRIPT
if(isset($_POST['javascript']) & $_POST['javascript'] == "no") {
$result = "<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'><tr><td>$login13</td></tr></table>";
$task = "main";

// LOGIN FAIL
} elseif(mysql_num_rows($check) == 0) {
mysql_query("INSERT INTO bhost_log (date, username, password, ipaddress, result) VALUES ('$date', '$username', '$password', '$ipaddress', 'Failure')");
$result = "<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'><tr><td>$login14</td></tr></table>";
$task = "main";
} elseif($username != $check_info[username] | $password != $check_info[password]) {
mysql_query("INSERT INTO bhost_log (date, username, password, ipaddress, result) VALUES ('$date', '$username', '$password', '$ipaddress', 'Failure')");
$result = "<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'><tr><td>$login14</td></tr></table>";
$task = "main";
} elseif($check_info[verified] == 0) {
mysql_query("INSERT INTO bhost_log (date, username, password, ipaddress, result) VALUES ('$date', '$username', '$password', '$ipaddress', '0')");
$result = "<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'><tr><td>$login15</td></tr></table>";
$task = "main";
} else {
// LOGIN SUCCESS
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_users WHERE username='$username' AND password='$password'"));
$date = time();
mysql_query("UPDATE bhost_users SET last_login='$date' WHERE u_id='$user_info[u_id]'");
mysql_query("INSERT INTO bhost_log (date, username, password, ipaddress, result) VALUES ('$date', '$username', '$password', '$ipaddress', '1')");
bumplog();
$user = crypt($user_info[username], $user_info[code]);
$pass = $user_info[password];
$u_id = $user_info[u_id];


setcookie("admin_username", "");
setcookie("admin_password", "");

// REMEMBER ME
if(isset($rememberme) AND $rememberme == "1") {
setcookie("username", "$user", time()+60*999999, "/");
setcookie("password", "$pass", time()+60*999999, "/");
setcookie("u_id", "$u_id", time()+60*999999, "/");
} else {
// DONT REMEMBER ME
setcookie("username", "$user", 0, "/");
setcookie("password", "$pass", 0, "/");
setcookie("u_id", "$u_id", 0, "/");
}

// SET LOCATION
if($ref == "1") {
$location = url("entry", "$w", "", "$e_id");
} elseif($ref == "2") {
$location = "post_comment.php?w=$w&e_id=$e_id";
} elseif($ref == "3") {
$location = "edit_comment.php?w=$w&e_id=$e_id&c_id=$c_id";
} elseif($ref == "4") {
$location = "delete_comment.php?w=$w&e_id=$e_id&c_id=$c_id";
} else {
$location = "./manager/index.php";
}

cheader($location);
exit();
}
}













if($task == "main") {
echo $head;
echo "

<h2>$login16</h2>
$login17
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='login.php' name='login' method='POST'>
<tr>
<td><font class='bold'>$login18</font><br>
<input type='text' class='text' name='username' maxlength='30'>&nbsp;
</td>
<td><font class='bold'>$login19</font><br>
<input type='password' class='text' name='password' maxlength='30'>&nbsp;
</td>
<td valign='bottom'>
<input type='submit' class='sbutton' value='$login20' style='margin-bottom: 3px;'>
</td>
<td><br>&nbsp;<input type='checkbox' name='rememberme' id='rememberme' value='1'></td>
<td><br><label for='rememberme'>$login21</label></td>
<input type='hidden' name='task' value='dologin'>
<input type='hidden' name='w' value='$w'>
<input type='hidden' name='e_id' value='$e_id'>
<input type='hidden' name='c_id' value='$c_id'>
<input type='hidden' name='ref' value='$ref'>
<NOSCRIPT><input type='hidden' name='javascript' value='no'></NOSCRIPT>
</tr></table>
$result
</form>


<script language='JavaScript'>
<!-- 
appendEvent = function(el, evname, func) {
 if (el.attachEvent) { // IE
   el.attachEvent('on' + evname, func);
 } else if (el.addEventListener) { // Gecko / W3C
   el.addEventListener(evname, func, true);
 } else {
   el['on' + evname] = func;
 }
};
appendEvent(window, 'load', windowonload);

function windowonload() { document.login.username.focus(); } 
// -->
</script>
";

echo $foot;
}


?>