<?php
	
/**
 * class authsession is used for establishing sessions and all data in sessions
 * 
 * 
 * @copyright RMplus d.o.o. 2009
 * @author Bostjan Kodric <bostjan.kodric@rmplus.si>
 * 
 */



	class authsession
	{
		
		private sessionKey;
		
		/**
		 * class constructor starts session. Declaration of class must be before all header outputs.
		 *
		 */
		function __construct()
		{
			session_start();
			$this->sessionKey = session_id();
		}
		
		
		/**
		 * 
		 * class destructor stops session. You need to be carefully to end with all operations with session before end
		 *
		 */
		function __destruct()
		{
			session_destroy()
		}
		
		/**
		 * function set pointer to db class connect
		 *
		 * @param object $dbClass
		 */
		function SetDbConnection($dbClass) 
		{
			$this->db = $dbClass;
		}
		
		function CheckLogin($username,$password) 
		{
			$sql = "SELECT * FROM _users WHERE username = '".$username."' AND '".$password."'";
			$result = $this->db->SQLexecute($sql);
			if (!$this->db->NumRows($result)) return 0;
			$userInfo = $this->db->fetchAssoc($result);
			
			$sql = "INSERT INTO _sessions SET 
												session_key = '".$this->sessionKey."', 
												session_user = '".$userInfo[id]."',
												session_start = NOW()";
			$this->db->SQLexecute($sql);
			$sessionId = $this->db->GetLastInsertId();
			
			 	
		}
		
		
	}
?>