<?php
	require_once 'init.php';
	
	$errorFail =  "<div align=center><font color=red>Login Failed </font></div>
	<script language=javascript>
	setTimeout('location.href = \"logout.php\"', 1000);
	</script>";
	
	$errorFail2 =  "<div align=center><font color=red>Login Failed<br>-<br>Adminseite wird benutzt!</font></div>
	<script language=javascript>
	setTimeout('location.href = \"logout.php\"', 2000);
	</script>";

	if (isset($_SESSION['userid'])) {
		$rs = $adodb->Execute("SELECT * FROM qb_user".
		" WHERE userid = '".$_SESSION['userid']."'");
		if (!$rs->EOF) {
			if ( file_exists("templates/sem") && $_SESSION['userid'] != implode(file("templates/sem")) ) {
				die($errorFail2);
			} else {
 				if (ereg('login2\.php', @$_SERVER['HTTP_REFERER'])) {
					header('Location: nola.php');
				}
				$passkey = md5($_SESSION['userid'].($rs->fields['passwd']).'qbuilder');
				if ($_SESSION['passkey'] !== $passkey) {
					die($errorFail);
				}
				return true;
			}
		}
	}

	$userid = $_REQUEST['auth_user'];
	$pass = $_REQUEST['password_md5'];
	
	//Semaphor-Check
	if ( file_exists("templates/sem") && $userid != implode(file("templates/sem")) ) {
		die($errorFail2);
	} else {	
	$rs = $adodb->Execute("SELECT *, u.userid as userid FROM qb_user u LEFT JOIN qb_user_group ug ON ug.userid = u.userid WHERE u.userid = '".$userid."'") or die($adodb->ErrorMsg());
//	$rs = $adodb->Execute("SELECT * FROM qb_user WHERE userid = '".$userid."'") or die($adodb->ErrorMsg());
	if ($rs && strlen($pass) > 0 && $rs->fields['passwd'] === $pass) {
		$record = array();
		$record['updated'] = date('Y-m-d H:i:s');
		$record['user'] = $userid;
		$record['host'] = $_SERVER['REMOTE_ADDR'];
		$record['operation'] = 'login';
		$record['newval'] = session_id();
		$rs2 = $adodb->SelectLimit("SELECT * FROM qb_changelog", 1);
		$adodb->Execute($adodb->GetInsertSQL($rs2, $record));

		$_SESSION['userid'] = $rs->fields['userid'];
		$_SESSION['passkey'] = md5($userid.$pass.'qbuilder');
        $_SESSION['group_name'] = $rs->fields['group_name'];
		#header('Location: phplayersmenu-3.2.0/index3.php');
		header('Location: nola.php');
	} else {
		die($errorFail);
	}
	}


?>
