Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/sessions/sess.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	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 at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class sess {
	
	private $sessid;
	private $CACHE;
	protected $SESSRef;
	private static $cookname;
	
	public function __construct($SESSRef){
		$this->SESSRef = $SESSRef;
		if ( FALSE === configs::get('sessions', $this->SESSRef, 'timeout') ){
			errors::raise("Session : $this->SESSRef, Missing configuration 'timeout', setting to 30mins", CORE_LOG_WARNING, 'SESS');
			configs::set('sessions', $this->SESSRef, 60*30, 'timeout');
		}
		if ( FALSE === configs::get('sessions', $this->SESSRef, 'lifetime') ){
			errors::raise("Session : $this->SESSRef, Missing configuration 'lifetime', setting to 24hours", CORE_LOG_WARNING, 'SESS');
			configs::set('sessions', $this->SESSRef, 60*60*24, 'lifetime');
		}
		//adding session cooky channel if not already done;
		if ( !isset(self::$cookname) ){
			self::$cookname = TRUE;
			$expire = configs::get('sessions', $this->SESSRef, 'lifetime');
			if ( $expire == 0 ){ $expire = 60*60*24*365; }
			configs::set('cookies', sessions::cookref(), Array('cipher'=>FALSE, 'expire'=>$expire));
			errors::raise("Core Session is using Cookies channel : ".sessions::cookref(), CORE_LOG_NOTICE, 'SESS');
		}

	}
	
	public function __destruct(){ 
		/*
		errors::raise('SESS '.$this->SESSRef.' destruct', CORE_LOG_WARNING, 'SESS');
		sessions::store($this->SESSRef);
		*/ 
	}
	
	public function get_sessid(){ return $this->sessid; }
	
	protected function genid(){
		$this->sessid = CORE::hash();
		return TRUE;
	}

	
	//use a unique cooky channel, so i can use the PHPSESSID cooky to store sessids.
	//into an array, with SESSRef as key and sessid as value ( only crypt SESSRef sessid)
	//so only this channel can restore it.
	protected function restoreid(){
		if ( cookies::isdefined($this->SESSRef, sessions::cookref()) ){
			$this->sessid = cookies::read($this->SESSRef, sessions::cookref());
			return TRUE;
		}
		return FALSE;
	}
	
	protected function exportid(){
		return cookies::set($this->SESSRef, $this->sessid, sessions::cookref());
	}
	
	protected function restoredata($str){
		$str = urldecode($str);
		if ( FALSE === $str ){
			errors::raise('Session data could not be restored SESSRef : "'.$this->SESSRef.'", urldecode failure', CORE_LOG_ERROR, 'SESS');
			$this->CACHE = Array();
			return FALSE;
		}
		$this->CACHE = unserialize($str);
		unset($str);
		if ( FALSE === $this->CACHE ){
			$this->CACHE = Array();
			errors::raise('Session Data could not be restored for SESSRef: "'.$this->SESSRef.'", unserialize failure', CORE_LOG_ERROR, 'SESS');
			return FALSE;
		}
		return TRUE;
	}
	
	protected function storedata(){
		if ( !is_array($this->CACHE) ){ return urlencode(serialize(Array())); }
		$data = $this->check_data($this->CACHE);
		$str = serialize($data);
		unset($data);
		if ( FALSE === $str ){
			errors::raise('Session data could not be stored for SESSRef : "'.$this->SESSRef.'", serialize failure', CORE_LOG_ERROR, 'SESS');
			return FALSE;
		}
		return urlencode($str);
	}
	
	private function check_data($data){
		if ( is_array($data) ){
			foreach($data as $k => $v ){
				if ( is_object($v) ){
					errors::raise('Storing Object in Sessions is not available, droping var "'.$k.'"', CORE_LOG_WARNING, 'SESS');
					unset($data[$k]);
				} elseif ( is_array($v) ){ 
					$data[$k] = $this->check_data($v);
				}
			}
		}
		return $data;
	}
	
	public function isdefined($var){ return isset($this->CACHE[$var]); }
	
	public function read($var){
		if ( ! $this->isdefined($var) ){ return FALSE; }
		return $this->CACHE[$var];
	}
	
	public function readall(){ return $this->CACHE; }
	
	public function set($var, $val){
		$this->CACHE[$var] = $val;
		return TRUE;
	}
	
	public function del($var){
		if ( ! $this->isdefined($var) ){ return TRUE; }
		unset($this->CACHE[$var]);
	}
	
	public function clearcache(){
		unset($this->CACHE);
	}
	
}
return TRUE;
Return current item: Simple Way to Usenet