<?php
/*
* Error Codes
* 0: Success
* 1: User does not exist in DB
* 2: User is already logged in
* 3: Hash in form is not equal to server side created hash
* 4: An error occured while creating the session in the DB
*/
session_start();
srand(time());
if(!isset($_SESSION['RND'])) {
$_SESSION['RND'] = sha1(rand()%1000001);
}
if(!isset($_SESSION['authINF1'], $_SESSION['authINF2'], $_SESSION['authINF3'])) {
$_SESSION['authINF1'] = sha1($_SERVER['HTTP_USER_AGENT']);
$_SESSION['authINF2'] = sha1($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$_SESSION['authINF3'] = sha1($_SERVER['REMOTE_ADDR']);
}
function authenticate() {
if(!isset($_SESSION['conSession'], $_SESSION['chSession'])) {
// Get information from form
$username = htmlentities($_POST['username'], ENT_QUOTES);
$hash = htmlentities($_POST['hash'], ENT_QUOTES);
// Create salt for hash
$salt = htmlentities($_SESSION['RND'], ENT_QUOTES);
$_SESSION['RND'] = sha1(rand()%1000001);
$salt .= htmlentities($_SERVER['REMOTE_ADDR'], ENT_QUOTES);
$salt .= htmlentities($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES);
$qGetUser = @mysql_query("SELECT * FROM users WHERE gebruikersnaam='".$username."'");
if(@mysql_num_rows($qGetUser) == 1) {
// The user exists in the DB
$aGetUser = @mysql_fetch_assoc($qGetUser);
$qGetSession = @mysql_query("SELECT * FROM sessions WHERE gebruikersnaam='".$username."'");
if(@mysql_num_rows($qGetSession) == 0) {
// The user is not logged in yet
$serverSideHash = sha1($aGetUser['wachtwoord'].$salt);
if($serverSideHash == $hash) {
// The submitted hash and the server side created one are equal
$chSession = sha1(rand()%1000001);
if(@mysql_query("INSERT INTO sessions(gebruikersnaam, conSessie, chSessie, sessieTijd) VALUES('".$username."', '".$serverSideHash."', '".$chSession."', ".time().")")) {
// The session has been created
$_SESSION['conSession'] = $serverSideHash;
$_SESSION['chSession'] = $chSession;
$err = 0;
}
else {
$err = 4;
}
}
else {
$err = 3;
}
}
else {
$err = 2;
}
}
else {
$err = 1;
}
}
return $err;
}
function renew() {
deleteOldSessions();
session_regenerate_id(TRUE);
$conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
$chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
$qGetSession = @mysql_query("SELECT * FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."'");
if(@mysql_num_rows($qGetSession) == 1) {
$aGetSession = @mysql_fetch_assoc($qGetSession);
if($chSession == $aGetSession['chSessie']) {
$chSession = sha1((rand()%1000001).$chSession);
$_SESSION['chSession'] = htmlentities($chSession, ENT_QUOTES);
@mysql_query("UPDATE sessions SET chSessie='".$chSession."', sessieTijd=".time()."");
}
}
}
function destroy() {
$conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
$chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
$qGetSession = @mysql_query("DELETE FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."'");
session_unset();
session_destroy();
}
function deleteOldSessions() {
$inactivityTime = 60*5;
$expirationTime = time() - $inactivityTime;
if(isset($_SESSION['conSession']) && isset($_SESSION['chSession'])) {
$conSession = htmlentities($_SESSION['conSession'], ENT_QUOTES);
$chSession = htmlentities($_SESSION['chSession'], ENT_QUOTES);
$qGetSession = @mysql_query("SELECT * FROM sessions WHERE conSessie='".$conSession."' AND chSessie='".$chSession."' AND sessieTijd<".$expirationTime."");
$aGetSession = @mysql_fetch_assoc($qGetSession);
if(@mysql_num_rows($qGetSession) == 1) {
destroy();
}
}
@mysql_query("DELETE FROM sessions WHERE sessieTijd<".$expirationTime."");
}
?>