<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	
class session 
{
	private $life;
	private $handler;
	private $error = 0;
	private $user_type;
	
	public function __construct()
	{
		global $Handler;
		
		$this->handler = $Handler;
		
		//Session lifetime
		$this->life = 60 * 60;
		
/* 		session_set_save_handler( 
	        array( &$this, "open" ), 
	        array( &$this, "close" ),
	        array( &$this, "read" ),
	        array( &$this, "write"),
	        array( &$this, "destroy"),
	        array( &$this, "clean" )
				); */
	}
	
	public function Start()
	{
		@session_start();
		
		if( isset( $_SESSION['EXPIRE'] ) && time() - $_SESSION['EXPIRE'] > $this->life )
		{
			@session_regenerate_id(true);
		}
		
		if( ! isset( $_SESSION['EXPIRE'] ) )
		{
			$_SESSION['EXPIRE'] = time();
		}
		
		if( ! isset( $_SESSION['LANGUAGE'] ) )
		{
			$_SESSION['LANGUAGE'] = "english";
		}
		
		if( isset( $_SESSION['IP'] ) ) 
		{
			if( $_SESSION['IP']  != $_SERVER['REMOTE_ADDR'] )
			{
				@session_destroy();
				header( 'Location:' . $_SERVER['REQUEST_URI'] );
			}
		}
		
		if( ! isset( $_SESSION['IP'] ) )
		{
			$_SESSION['IP'] = $_SERVER['REMOTE_ADDR'];
		}
		
		if( ! isset( $_SESSION['AccountLevel'] ) )
		{
			$_SESSION['AccountLevel'] = GUEST;
			header( 'Location:' . $_SERVER['REQUEST_URI'] );
		}
		
		if( ! isset( $_SESSION['LoggedIn'] ) )
		{
			$_SESSION['LoggedIn'] = false;
		}
	}
	
	public function open()
	{
	}
	
	public function close()
	{
		if( $this->error == 0 )
		{
			sqlsrv_close( $this->conn );
			return $this->handler->database->Free();
		}
	}
	
	public function read( $id )
	{
		$this->conn = $this->handler->database->Connect( SQL_HOST , SQL_USER , SQL_PASS );
		$id = $this->handler->database->escape( $id );
		
		$this->handler->database->prepare( "Select * from Account.dbo.tSessions where session_id = ?" , array( &$id ) ) or $this->error = 1;		
		
		$this->handler->database->execute();
		
		$k =  $this->handler->database->Fetch_Assoc();
		
		$this->handler->database->Free();
		
		if( $k['session_ip'] != $_SERVER['REMOTE_ADDR'] )
		{
			session_destroy();
			header( "refresh:0;url=". $_SERVER['REQUEST_URI'] . "" );
		}
		
		return $k;
	}
	
	public function write( $id , $data )
	{
		$this->conn = $this->handler->database->Connect( SQL_HOST , SQL_USER , SQL_PASS );
		$id = $this->handler->database->escape( $id );
		$data = $this->handler->database->escape( $data );
		$ip = $this->handler->database->escape( $_SERVER['REMOTE_ADDR'] );
		
		$params = array( &$id , &$data , &$id , &$data , &$ip );
		$sql = "if exists ( select * from Account.dbo.tSessions where session_id = ? )
					update Account.dbo.tSessions set session_data = ?
				else
				insert into Account.dbo.tSessions values ( ? , ? , CURRENT_TIMESTAMP , ?)
		";
		
		$do = $this->handler->database->prepare( $sql , $params );
						
		$this->handler->database->execute();
		
		$this->handler->database->Free();
	}
	
	public function destroy( $id )
	{
		$this->conn = $this->handler->database->Connect( SQL_HOST , SQL_USER , SQL_PASS );
		$id = $handler->database->escape( $id );
		
		$do = $this->handler->database->prepare( "Delete from Account.dbo.tSessions where session_id = ?" , array( &$id ) );
		
		$this->handler->database->execute();
		
		$this->handler->database->Free();
	}
	
	public function clean( $time )
	{
		$this->conn = $this->handler->database->Connect( SQL_HOST , SQL_USER , SQL_PASS );
		$time = time() - $time;
		
		$do = $this->handler->database->prepare( "Delete from Account.dbo.tSessions where session_expiration < ?" , array( &$time ) );
		
		$this->handler->database->execute();
		
		$this->handler->database->Free();		
	}
	
	public function __destruct()
	{
		session_write_close();
	}
}

?>