<?php

if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}


if( trim($_POST['txtusername']) == '' ){

	$err = USERNAME_BLANK; // change to a constant later

} elseif( trim($_POST['txtpassword']) == '' ){

	$err = PASSWORD_BLANK; // change to a constant later

} else {
	$row =& $osDB->getRow( 'SELECT *  FROM ! where username = ? and password = ?' , array( ADMIN_TABLE, $_POST['txtusername'], md5( trim( $_POST['txtpassword'] ) ) ) );

	if( $row['id'] > 0 ) {

		$_SESSION['AdminId'] = $row['id'];

		$_SESSION['LastVisit'] = $row['lastvisit'];

		$_SESSION['AdminName'] = $row['fullname'];

		$_SESSION['UserName'] = $_POST['txtusername'];

		$_SESSION['UserId'] = '';

		$_SESSION['whatIneed'] = base64_encode( $_POST['txtpassword'] );

		$time = time();

		$osDB->query( 'UPDATE ! SET lastvisit = ? WHERE id = ?', array( ADMIN_TABLE, $time, $row['id'] ) );

		$_SESSION['Permissions'] = $osDB->getRow( 'SELECT * FROM ! WHERE adminid = ?', array( ADMIN_RIGHTS_TABLE, $row['id'] ) );

		unset($row);

		$_SESSION['opt_lang'] = $config['admin_lang'];

		header( 'location: panel.php' );

		exit();
	} else {

		$err = INVALID_LOGIN; // change to a constant later
	}
}

header( 'location: index.php?errid=' . $err );
exit();

?>