<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 -  2.4): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# session.php: last changed 20/08/2006  , version: 2.4.10
#                  by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: session.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002 - 2005 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}

/* Create new object of class */
$ses_class = new session();
$ses_class->ses_table = $dbprefix . "sessions";

/* Change the save_handler to use the class functions */
session_set_save_handler(array (
	& $ses_class,
	'_open'
), array (
	& $ses_class,
	'_close'
), array (
	& $ses_class,
	'_read'
), array (
	& $ses_class,
	'_write'
), array (
	& $ses_class,
	'_destroy'
), array (
	& $ses_class,
	'_gc'
));

/* Start the session */
session_start();

class session
{
	/* Define the mysql table you wish to use with
	   this class, this table MUST exist. */
	var $ses_table;

	/* Open session */
	function _open($path, $name)
	{
		/* just define this function. */
		return TRUE;
	}

	/* Close session */
	function _close()
	{
		/* This is used for a manual call of the
		   session gc function */
		$this->_gc(0);
		return TRUE;
	}

	/* Read session data from database */
	function _read($ses_id)
	{
		$session_sql = "SELECT * FROM " . $this->ses_table . " WHERE ses_id = '$ses_id'";
		$session_res = @ mysql_query($session_sql);
		if (!$session_res)
		{
			return '';
		}

		$session_num = @ mysql_num_rows($session_res);
		if ($session_num > 0)
		{
			$session_row = mysql_fetch_assoc($session_res);
			$ses_data = $session_row["ses_value"];
			return $ses_data;
		}
		else
		{
			return '';
		}
	}

	/* Write new data to database */
	function _write($ses_id, $data)
	{
		$session_sql = "UPDATE " . $this->ses_table . " SET ses_time='" . time() . "', ses_value='$data' WHERE ses_id='$ses_id'";
		$session_res = @ mysql_query($session_sql);
		if (!$session_res)
		{
			return FALSE;
		}
		if (mysql_affected_rows())
		{
			return TRUE;
		}

		$session_sql = "INSERT INTO " . $this->ses_table . " (ses_id, ses_time, ses_start, ses_value)" . " VALUES ('$ses_id', '" . time() . "', '" . time() . "', '$data')";
		$session_res = @ mysql_query($session_sql);
		if (!$session_res)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	/* Destroy session record in database */
	function _destroy($ses_id)
	{
		$session_sql = "DELETE FROM " . $this->ses_table . " WHERE ses_id = '$ses_id'";
		$session_res = @ mysql_query($session_sql);
		if (!$session_res)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	/* Garbage collection, deletes old sessions */
	function _gc($life)
	{
		$ses_life = strtotime("-60 minutes");

		$session_sql = "DELETE FROM " . $this->ses_table . " WHERE ses_time < $ses_life";
		$session_res = @ mysql_query($session_sql);

		if (!$session_res)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
}
?>
