<?php
function pml_login($todo = "",$action = "") {
ob_start();
include("lang.php");
include("pml_config.inc.php");
if(!isset($_SESSION)) { exit($lang['sessionproblem']); }
pml_connect();
// Check if user is logged in
if(!isset($_SESSION['pml_userid'])) {
if(isset($_COOKIE['pml_userid_cookie'])) {
// Check cookie data with data in database
$sql = "SELECT id,username,password,cookie_pass,actcode,rank FROM `".$settings['db_table']."` WHERE id = '".$_COOKIE['pml_userid_cookie']."' LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 1) {
// User exists
$row = mysql_fetch_array($query);
$id = htmlspecialchars($row['id']);
$username = htmlspecialchars($row['username']);
$password_db = htmlspecialchars($row['password']);
$cookie_pass = htmlspecialchars($row['cookie_pass']);
$actcode = htmlspecialchars($row['actcode']);
$rank = htmlspecialchars($row['rank']);
if($actcode == "") {
// Useraccount is activated
if($cookie_pass == $_COOKIE['pml_usercode_cookie']) {
// Everything ok, create sessions
$_SESSION['pml_userid'] = $id;
$_SESSION['pml_userrank'] = $rank;
$sql_updateonline = "UPDATE `".$settings['db_table']."` SET lastactive = NOW() AND lastlogin = NOW() WHERE id = '".$id."' LIMIT 1";
mysql_query($sql_updateonline);
header("Location: ".$_SERVER['REQUEST_URI']);
}else{
// Incorrect password
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}else{
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}else{
// User doesn't exists
setcookie("pml_userid_cookie", "", time() - 3600);
setcookie("pml_usercode_cookie", "", time() - 3600);
header("Location: ".$_SERVER['REQUEST_URI']);
}
}
if(isset($_POST['submit'])) {
if($_POST['username'] != "" AND $_POST['password'] != "") {
// Check submitted data with data in database
$sql = "SELECT id,username,password,cookie_pass,actcode,rank FROM `".$settings['db_table']."` WHERE username = '".$_POST['username']."' LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) == 1) {
// User exists
$row = mysql_fetch_array($query);
$id = htmlspecialchars($row['id']);
$username = htmlspecialchars($row['username']);
$password_db = htmlspecialchars($row['password']);
$cookie_pass = htmlspecialchars($row['cookie_pass']);
$actcode = htmlspecialchars($row['actcode']);
$rank = htmlspecialchars($row['rank']);
if($actcode == "") {
// Useraccount is activated
if($password_db == sha1(md5($_POST['password']))) {
// Everything ok, create sessions
$_SESSION['pml_userid'] = $id;
$_SESSION['pml_userrank'] = $rank;
if(isset($_POST['cookie'])) {
// Also create cookie
setcookie("pml_userid_cookie", $id, time() + 365 * 86400);
if($cookie_pass == "") {
// Create cookie code
mt_srand((double)microtime()*1000000);
$pass = 1;
while(strlen($pass) <= 10) {
$i = chr(mt_rand(0,255));
if(eregi("^[a-z0-9]$",$i)) {
$pass = $pass.$i;
}
}
$cookie_pass = md5($pass);
$sql_cookiepass = "UPDATE `".$settings['db_table']."` SET cookie_pass = '".$cookie_pass."' WHERE id = ".$id." LIMIT 1";
mysql_query($sql_cookiepass);
}
setcookie("pml_usercode_cookie", $cookie_pass, time() + 365 * 86400);
}
$sql_updateonline = "UPDATE `".$settings['db_table']."` SET lastactive = NOW(),lastlogin = NOW() WHERE id = '".$id."' LIMIT 1";
mysql_query($sql_updateonline) or trigger_error(mysql_error());
header("Location: ".$_SERVER['REQUEST_URI']);
}else{
// Incorrect password
echo $lang['login-incorrect']."<br />";
}
}else{
echo $lang['login-notactive']."<br />";
}
}else{
// User doesn't exists
echo $lang['login-incorrect']."<br />";
}
}else{
echo $lang['login-forgotfield']."<br />";
}
}
// Login form
echo "\n";
?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<?php echo $lang['login-username']; ?>
<br>
<input class="input" type="text" id="username" name="username" <?php if(isset($_POST['username'])) { echo 'value="'.$_POST['username'].'"'; } ?> />
<br>
<label for="cookie"><?php echo $lang['login-password']; ?></label>
<br>
<input class="input" type="password" id="password" name="password"/>
<br>
<input type="checkbox" id="cookie" name="cookie" value="true" <?php if(isset($_POST['cookie'])) { echo "checked"; } ?> />
<?php echo $lang['login-cookie']; ?>
<br>
<input type="submit" name="submit" value="<?php echo $lang['login-submitbutton']; ?>" />
</form>
<a href="?page=registreren">Registreren</a><br>
<a href="?page=wachtwoordvergeten">Wachtwoord vergeten?</a>
<?php
}else{
// User is logged on, redirect to page $goto; if no $goto just view msg that user is logged in
if($todo != "") {
if($todo == "include") {
include($action);
}elseif($todo == "redirect") {
header("Location: ".$action);
}else{
echo $lang['functionproblem'];
}
}else{
echo $lang['login-already'];
}
}
ob_end_flush();
}
?>