<?php
	function fo_exit( ) {
		FNTH_db_firephp();
		FNTH_db_set_debugging( false );
		exit();
	}

	function fo_session_open( $save_path , $session_name ) {
		$GLOBALS["fo_session_save_path"]=$save_path;
		return true;
	}

	function fo_session_close( ) {
		return true;
	}

	function fo_session_read( $id ) {
		switch ($GLOBALS["model_version"]) {
		case 2.0:
			$query = sprintf( "select fo_session_data as session_data from fo_sessions where fo_session_id='%s'",
				FNTH_db_addslashes( $id ) );
			break;
		case 3.0:
			$query = sprintf( "select session_data from sessions where session_id='%s'",
				FNTH_db_addslashes( $id ) );
			break;
		}
		$row = FNTH_db_get_row( $query );
		if (!$row) return false;
		return $row["session_data"];
	}

	function fo_session_write( $id , $data ) {
		if (isset( $GLOBALS["fo3_readonly_session"] )) return;
		switch ($GLOBALS["model_version"]) {
		case 2.0:
			$query = sprintf( "select fo_session_id as session_id from fo_sessions where fo_session_id='%s'",
				FNTH_db_addslashes($id));
			break;
		case 3.0:
			$query = sprintf( "select session_id from sessions where session_id='%s'",
				FNTH_db_addslashes($id));
			break;
		}
		$row = FNTH_db_get_row( $query );

		$d = array();
		switch ($GLOBALS["model_version"]) {
		case 2.0:
			$d["fo_session_data"]=sprintf( "'%s'" , FNTH_db_addslashes( $data ) );
			$d["fo_session_expires"]=time();
			break;
		case 3.0:
			$d["session_data"]=sprintf( "'%s'" , FNTH_db_addslashes( $data ) );
			$d["session_expires"]=time();
			if (isset( $_SERVER["REMOTE_ADDR"] )) $d["server_remote_ip"]=sprintf( "'%s'" , FNTH_db_addslashes( substr( $_SERVER["REMOTE_ADDR"] , 0 , 16 ) ) );
			if (isset( $_SERVER["SERVER_NAME"] )) $d["server_name"]=sprintf( "'%s'" , FNTH_db_addslashes( substr( $_SERVER["SERVER_NAME"] , 0 , 255 ) ) );
			if (isset( $_SERVER["REQUEST_URI"] )) $d["server_uri"]=sprintf( "'%s'" , FNTH_db_addslashes( substr( $_SERVER["REQUEST_URI"] , 0 , 255 ) ) );
			if (isset( $_SERVER["HTTP_USER_AGENT"] )) $d["server_agent"]=sprintf( "'%s'" , FNTH_db_addslashes( substr( $_SERVER["HTTP_USER_AGENT"] , 0 , 255 ) ) );
			if (isset( $_SERVER["HTTP_REFERER"] )) $d["server_referer"]=sprintf( "'%s'" , FNTH_db_addslashes( substr( $_SERVER["HTTP_REFERER"] , 0 , 255 ) ) );
			break;
		}

		if ($row) {
			$d["user_id"]=(isset( $_SESSION["user_id"] )) ? $_SESSION["user_id"] : "null";
			switch ($GLOBALS["model_version"]) {
			case 2.0:
				$q = FNTH_db_generate_update_query( "fo_sessions" , $d , sprintf( "fo_session_id='%s'" , FNTH_db_addslashes( $id ) ) );
				break;
			case 3.0:
				$q = FNTH_db_generate_update_query( "sessions" , $d , sprintf( "session_id='%s'" , FNTH_db_addslashes( $id ) ) );
				break;
			}
		} else {
			switch ($GLOBALS["model_version"]) {
			case 2.0:
				$d["fo_session_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $id ) );
				$q = FNTH_db_generate_insert_query( "fo_sessions" , $d );
				break;
			case 3.0:
				$d["session_id"]=sprintf( "'%s'" , FNTH_db_addslashes( $id ) );
				$q = FNTH_db_generate_insert_query( "sessions" , $d );
				break;
			}
		}
		FNTH_db_free_result( FNTH_db_query( $q ) );
	}

	function fo_session_destroy( $id ) {
		switch ($GLOBALS["model_version"]) {
		case 2.0:
			$query = sprintf( "delete from fo_sessions where fo_session_id='%s'", FNTH_db_addslashes( $id ) );
			break;
		case 3.0:
			$query = sprintf( "delete from sessions where session_id='%s'", FNTH_db_addslashes( $id ) );
			break;
		}
		FNTH_db_free_result( FNTH_db_query( $query ) );
		return true;
	}

	function fo_session_gc( $maxtime ) {
		switch ($GLOBALS["model_version"]) {
		case 2.0:
			$query = sprintf( "delete from fo_sessions where fo_session_expires<%d",time()-$maxtime);
			break;
		case 3.0:
			$query = sprintf( "delete from sessions where session_expires<%d",time()-$maxtime);
			break;
		}
		FNTH_db_free_result( FNTH_db_query( $query ) );
		return true;
	}
	
	function fo3_set_session_readonly() {
		$GLOBALS["fo3_readonly_session"]=true;
	}


	session_set_save_handler(
		"fo_session_open",
		"fo_session_close",
		"fo_session_read",
		"fo_session_write",
		"fo_session_destroy",
		"fo_session_gc" );

	ini_set( "session.gc_probability" , 100 );
	ini_set( "session.gc_divisor" , 100 );
	ini_set( "session.gc_maxlifetime" , $GLOBALS["session_timeout"] );
	session_cache_expire( $GLOBALS["session_timeout"] );

	session_start();

?>
