Location: PHPKode > projects > Modularized Information Environment > mie/includes/session.inc
<?php
/**
 * @file includes/session.inc
 * @brief Session Management Library
 * @author Kenneth Smith <hide@address.com>
 *
 * This file contains the code necessary to manage MIE sessions.
 *
 * Modularized Information Environment (MIE)
 * Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
 *
 * 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.
 */

defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('SESSION_INC') && exit;
define('SESSION_INC', true);

/**
 * @brief Session manager
 *
 * This class manages the current browser session.
 */
class SESSION {

	/// Current session
	private $current;

	/// Construct session
	function __construct() {
		session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'delete'), array($this, 'clean'));
		session_set_cookie_params(mie_conf('session_lifetime', 0), mie_base(), @$_SERVER['HTTP_HOST']);
		session_start();
	}

	function __get($_id) {
		return isset($this->current[$_id]) ? $this->current[$_id] : null;
	}

	function __set($_id, $_value) {
		return $this->current[$_id] = $_value;
	}

	/// Open callback
	function open($_path, $_name) {
		return true;
	}

	/// Close callback
	function close() {
		return true;
	}

	/// Read callback
	function read($_id) {
		global $db;
		$sql = $db->rewrite('SELECT * FROM t{sessions} WHERE k{id} = %c', $_id);
		$this->current = $db->result($sql);
		return $this->current == false ? '' : $this->current['data'];
	}

	/// Write callback
	function write($_id, $_data) {
		global $db;
		$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '???';
		if(isset($this->current['id'])) {
			$sql = $db->rewrite('UPDATE t{sessions} SET k{host} = %c, k{user_id} = %i, k{stamp} = %s, k{data} = %c WHERE k{id} = %c', $remote, $this->current['user_id'], time(), $_data, $_id);
		}
		else {
			$sql = $db->rewrite('INSERT INTO t{sessions} (k{id}, k{host}, k{user_id}, k{stamp}, k{data}) VALUES (%c, %c, %i, %s, %c)', $_id, $remote, $this->current['user_id'], time(), $_data);
		}
		$qry = $db->query($sql);
		return $qry != false;
	}

	/// Destroy callback
	function delete($_id) {
		global $db;
		$sql = $db->rewrite('DELETE FROM t{sessions} WHERE k{id} = %c', $_id);
		$qry = $db->query($sql);
		return $qry != false;
	}

	/// Garbage collection callback
	function clean($_maxlife) {
		global $db;
		$sql = $db->rewrite("DELETE FROM t{sessions} WHERE k{stamp} < %s", time() - $_maxlife);
		$qry = $db->query($sql);
		return $qry != false;
	}

	/**
	 * @brief All Sessions
	 * Return table of all sessions.
	 */
	function all() {
		global $db;
		$sql = $db->rewrite("SELECT * FROM t{sessions} ORDER BY k{stamp} DESC");
		return (array)$db->table($sql);
	}
}

?>
Return current item: Modularized Information Environment