<?php

/*			Session handling routines for MySQL.
 *
 *	Include this before any 'session_start()'.
 *
 *
 *	$Linux: ownsess.php,v 0.2 2000/01/21 13:12:03 slagge Exp $
 *
 *
 * Copyright (c) 2000 Kenneth Mutka <slagge@c64.org>
 * All rights reserved.
 *
 * Redistribution and use in source form, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALLTHE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * The table:
 *
 * # Table structure for table 'session'
 *
 * CREATE TABLE session (
 *   ID int(10) DEFAULT '0' NOT NULL auto_increment,
 *   sessid text,
 *   sessval text,
 *   created varchar(14) DEFAULT '00000000000000' NOT NULL,
 *   modified varchar(14) DEFAULT '00000000000000' NOT NULL,
 *   PRIMARY KEY (ID)
 * );
 *
 */


// Take care of redefinitions.
if (!$session_functions)
{
	$session_functions= 1;

// ------------------------
	function conn () {
		if (!$sessconn) {
			// Make a persistent connection to the database. Seems to be a bit faster.
			$sessconn= mysql_pconnect("host", "user", "password"); // We don't want to know: die("SlgError: Failed to open connection to database in ownsess.".mysql_error());
			if (!$sessconn) return false;
		}
		return true;
	}
// ------------------------
	function open ($save_path, $session_name) {
		conn();
		return true;
	}
// ------------------------
	function close () {
		// This won't actually affect the above mysql_pconnect. Clean it out if you want to.
		if ($sessconn) mysql_close($sessconn);
		return true;
	}
// ------------------------
	function read ($key) {
		if (conn())
		{
			$sessresult= mysql_db_query("database", "SELECT * FROM session WHERE sessid='".$key."'");
			if ($sessresult)
			{
				$sessrow= mysql_fetch_object($sessresult);
				if (strlen($sessrow->sessval)>0)
				{
					return $sessrow->sessval;
				}
			}
		}
		return false;
	}
// ------------------------
	function write ($key, $val) {
		if (conn())
		{
			$sessresult= mysql_db_query("database", "SELECT * FROM session WHERE sessid='".$key."'");
			$sessrow= mysql_fetch_object($sessresult);
			if ($sessrow)
			{
				session_decode($val);
				$sessresult= mysql_db_query("database", "UPDATE session SET sessval='".serialize(session_encode($val))."', modified=".time()." WHERE sessid='".$key."'");
			} else {
				session_decode($val);
				$sessresult= mysql_db_query("database", "INSERT INTO session VALUES (0, '".$key."', '".serialize(session_encode($val))."', '".time()."', '".time()."')");
			}
		}
		return true;
	}
// ------------------------
	function destroy ($key) {
		if (conn())
		{
			$sessresult= mysql_db_query("database", "DELETE FROM session WHERE sessid='".$key."'");
		}
		return true;
	}
// ------------------------
	function gc ($maxlifetime) {
		// Dunno exactly what this is supposed to do.
		// Garbage Collection, yes, but what's it supposed to do? Clear out unused variables?
		// Nevermind. Returning 'true' in any case.
		return true;
	}

	session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");
}
?>
