<?
session_start();
class login{
	var $username;
	var $password;
	var $language;
	
	function login($username,$password,$language){
		global $db_pms;
		
		$today = getdate();
		$this->username=strtolower($username);
		$this->password=md5(strtolower($password));
		$this->language=$language;

		$myquery="Select * from general_users where user_status=1"; 
		$myresult =& $db_pms->query($myquery);
		$mycount=$myresult->numRows();
		$userfound=0;
		if ($mycount!=0) {
			while($myrow=$myresult->fetchRow()) {
				if (($myrow['user_name']==$this->username)&&($myrow['user_password']==$this->password)) {
					$userfound++;
					$myuserpkey=$myrow['user_pkey'];
				}
			}
		} else {
			$e=error_n_goto("-1","No such User or Invalid Password!");
			session_destroy();
			echo $e;
		}

		if ($userfound!=0) {

			$gd1=get_data("general_users","user_pkey,user_grouppkey,user_name,user_role","where user_name='$this->username' and user_password='$this->password'");
			$gd2=get_data("staff_profiles","staff_pkey,staff_no,staff_nricno,staff_title,staff_name,staff_role,staff_designation,staff_divpkey,staff_deptpkey","where staff_userpkey='".$myuserpkey."'");
			$gd3=get_data("general_usergroupaccess","*","where access_grouppkey='".$gd1['user_grouppkey']."'");
			$gd4=get_data("system_settings","system_settingvalue","where system_settingname='system_gui_cssfile'");
			$gd5=get_data("general_usergroups","*","where group_pkey='".$gd1['user_grouppkey']."'");

			// TODO: get access rights, menu status

			$_SESSION['Xuserpkey']=$gd1['user_pkey'];
			$_SESSION['Xusergrouppkey']=$gd1['user_grouppkey'];
			$_SESSION['Xusername']=$gd1['user_name'];
			$_SESSION['Xuserrole']=$gd1['user_role'];
			$_SESSION['Xlogintime']=mktime();
			$_SESSION['Xstaffpkey']=$gd2['staff_pkey'];
			$_SESSION['Xstaffno']=$gd2['staff_no'];
			$_SESSION['Xstaffnricno']=$gd2['staff_nricno'];
			$_SESSION['Xstaffrank']=$gd2['staff_rank'];
			$_SESSION['Xstaffname']=$gd2['staff_name'];
			$_SESSION['Xstaffrole']=$gd2['staff_role'];
			$_SESSION['Xstaffdesignation']=$gd2['staff_designation'];
			$_SESSION['Xstaffdivpkey']=$gd2['staff_divpkey'];
			$_SESSION['Xstaffdeptpkey']=$gd2['staff_deptpkey'];
			$_SESSION['Xuseraccess']=$gd3;
			$_SESSION['Xcssfile']=$gd4['system_settingvalue'];
			$_SESSION['Xgroupname']=$gd5['group_name'];
			$_SESSION['Xprojectpkey']="";
			$_SESSION['Xprojectid']="";
			$_SESSION['Xprojectname']="";
			$_SESSION['Xprojectdesc']="";
			$_SESSION['Xerrormessage']="";
			$_SESSION['Xactivetab']="";
			$_SESSION['Xprevmodule']="";
			
			$module = "Login";
			$sql = "Login to PMS";
			audittrail_add($module, $sql, 'Login');

			// new 19/10/2009
			//$purger = new purgatory();
			//$purger->purge_audit();
	
			$g=goto('index.php');
			echo $g;
		} else {
			$e=error_n_goto("-1","No such User or Invalid Password!");
			session_destroy();
			echo $e;
		}
	}
}
?>