Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/sessions/sess_file.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_file extends sess {
	
	private $init=TRUE;
	
	public function __construct($SESSRef){
		$path = configs::get('sessions', $SESSRef, 'path');
		if ( FALSE === $path ){
			errors::raise("Missing option 'path' for session channel $SESSRef, plz check your config !", CORE_LOG_ERROR, 'SESS');
			$this->init = FALSE;
		} else {
			$info = pathinfo($path);
			if ( FALSE === is_dir($info['dirname']) ){
				errors::raise("Invalid option 'path' for session channel $SESSRef : folder does not exists !", CORE_LOG_ERROR, 'SESS');
				$this->init = FALSE;
			} elseif ( FALSE === is_writable($info['dirname']) ){
				errors::raise("Invalid option 'path' for session channel $SESSRef : folder is not writable !", CORE_LOG_ERROR, 'SESS');
				$this->init = FALSE;
			}
		}
		parent::__construct($SESSRef);
	}
	
	public function __destruct(){ 

		if ( FALSE === $this->init ){ return; }
		//errors::raise("File sessions $this->SESSRef __destruct : ".print_r($this->readall(), TRUE), CORE_LOG_WARNING, 'SESS');
		$this->store();
		$this->clearcache();//unset cache
		unset($this);

	}
	
	public function start(){
		if ( FALSE === $this->init ){ return FALSE; }
		if ( $this->restoreid() ){ 
			//sessid could be restored from cookies
			if ( $this->exists() ){
				if ( TRUE === $this->load() ){
					errors::raise('Restoring existing session for '.$this->SESSRef.' : '.$this->get_sessid(), CORE_LOG_DEBUG, 'SESS'); 
					return TRUE; 				 
				}
			}	//known sessid we load it
		}
		$this->genid();
		if ( FALSE === $this->exportid() ){ return FALSE; }
		errors::raise('Creating new session for '.$this->SESSRef.' : '.$this->get_sessid(), CORE_LOG_DEBUG, 'SESS');
		if ( FALSE === $this->creat() ){ return FALSE; }
		return NULL;
	}
	
	public function store(){
		//Update sessfile with data in cache
		if ( FALSE === $this->init ){ return FALSE; }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		return file_put_contents( str_replace('%sessid', $this->get_sessid(), $path), $this->storedata());

	}
	
	public function tocleanup(){
		if ( FALSE === $this->init ){ return FALSE; }
		//remove expired sess, follow path and filename scheme
		static $hdl;
		if ( !isset($hdl) ){ $hdl=TRUE; }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		$path = pathinfo($path);
		if ( $hdl === TRUE ){
			$hdl = opendir($path['dirname']);
			if ( FALSE === $hdl ){ 
				errors::raise("Failed to open directory ".$path['dirname'].", This sessions will never drop any session !", CORE_LOG_WARNING, 'SESS');
				$hdl=TRUE;return FALSE; 
			}
		}
		$match = str_replace('%sessid', ')([a-f0-9]{32})(', $path['basename']);
		$lifetime = configs::get('sessions', $this->SESSRef, 'lifetime');
		$timeout  = configs::get('sessions', $this->SESSRef, 'timeout');
		if ( $lifetime == 0 AND $timeout == 0 ){
			errors::raise('This sessions will never drop any session !', CORE_LOG_WARNING, 'SESS');
			fclose($hdl);$hdl=TRUE;return FALSE;
		}
		while( FALSE !== ( $file = readdir($hdl) ) ){
			if ($file != '.' AND $file != '..' ){
				if ( preg_match("#($match)#", $file, $result) ){
					$cpath = $path['dirname'].'/'.$file;
					if ( $lifetime !== 0 ){
						if ( filectime($cpath) < (time() - $lifetime) ){ return $result[2]; }
					}
					if ( $timeout !== 0 ){
						if ( filemtime($cpath) < (time() - $timeout) ){ return $result[2]; }
					}
				}
			}
		}
		fclose($hdl);
		$hdl=TRUE;
		return FALSE;
	}
	
	
	public function cleanup($tocleanup){
		//remove one sessfile
		if ( FALSE === $this->init ){ return FALSE; }
		if ( FALSE === is_array($tocleanup) ){ return FALSE; }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		foreach($tocleanup as $k => $sessid){
			$file = str_replace('%sessid', $sessid, $path);
			unlink($file);
		}
		return TRUE;
	}
	
	public function destroy(){
		//remove one sessfile, and cache
		if ( FALSE === $this->init ){ return FALSE; }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		return unlink( str_replace('%sessid', $this->get_sessid(), $path) );
	}
	
	private function exists($sessid=NULL){
		//sessfile exists and is valid.
		if ( FALSE === $this->init ){ return FALSE; }
		if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		$path = str_replace('%sessid', $sessid, $path);
		//errors::raise('Sessions exists ? : '.file_exists(  $path ), CORE_LOG_NOTICE, 'SESS');
		return file_exists(  $path );
	}
	
	private function creat($sessid=NULL){
		if ( FALSE === $this->init ){ return FALSE; }
		if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		return file_put_contents( str_replace('%sessid', $sessid, $path), '');
		//creat sess file, empty data
	}
	
	private function load($sessid=NULL){
		if ( FALSE === $this->init ){ return FALSE; }
		if ( $sessid === NULL ){ $sessid = $this->get_sessid(); }
		$path = configs::get('sessions', $this->SESSRef, 'path');
		return $this->restoredata( file_get_contents( str_replace('%sessid', $sessid, $path) ) );
		//restore data from sessfile
	}
}
Return current item: Simple Way to Usenet