Location: PHPKode > projects > Simple Way to Usenet > libs/core/misc/client_configs.class.php
<?php
/**
 * 2 Store level : Runtime, Store  
 * 5 inputs type : post, get, cooky, session, profile 
 * dyn store level on construct.
 * 
 * on construct, define available inputs and theyr conf,
 * 				 define inputs relation and store level updating that store
 * $inp['get'] = array(1, '')
 * $inp['post'] = array(1, '')
 * $inp['sess'] = array(3, 'profile')
 * $inp['prof'] = array(4, 'site_profile')
 * 
 * $store['sess'] = array('inputs'=>'sess', 'updateby'=>'get,post,prof111')
 * $store['prof'] = array('inputs'=>'prof', 'updateby'=>'post')
 * 
 * 
 *  
 * session : expect a valid session handler as conf
 * profile : expect a valid profile cnf as conf
 * 
 * @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 client_configs {
	
	private $run, $store, $run_update, $store_update;
	private static $PROF;
	
	const FOREVER	= 5;
	const FORSOME	= 6;
	
	const POST 	= -1;
	const GET 	= 1;
	const COOKIES= 2;
	const SESSION= 3;
	const PROFILE= 4;
	
	public function __construct($run=NULL, $store=NULL, $run_update=NULL, $store_update=NULL){
		if ( NULL === $run ){			$run 		 = Array(self::SESSION=>'profile');	}
		if ( NULL === $store ){			$store 		 = Array(self::PROFILE=>'site_profile'); }
		if ( NULL === $run_update ){	$run_update  = Array(self::GET=>'', self::POST=>''); }
		if ( NULL === $store_update ){	$store_update= Array(self::POST=>''); }
		$this->run 			= $run;
		$this->run_update 	= $run_update;
		$this->store 		= $store;
		$this->store_update = $store_update;
		
	}
	
	public function __destruct(){
		
	}
	
	public function is_updated($ref){
		$engines = $this->merge_engines($this->run_update);
		$engines = $this->merge_engines($this->store_update, $engines);
		if ( $ref == 'search_engine' ){
			//errors::raise("CC is_updated : $ref =>".print_r($engines, TRUE), CORE_LOG_NOTICE, 'SEARCH');
		}
		return $this->multi_isdefined(&$engines, $ref);
	}
	
	public function get_updated($ref){
		$engines = $this->merge_engines($this->run_update);
		$engines = $this->merge_engines($this->store_update, $engines);
		return $this->multi_read(&$engines, $ref);
	}
	
	public function store_updated($ref){
		$store_dest=Array();
		$engines = $this->merge_engines($this->run_update);
		if ( FALSE !== $this->multi_isdefined($engines, $ref) ){
			$store_dest = $this->merge_engines($this->run, $store_dest);
		}
		$engines = $this->merge_engines($this->store_update);
		if ( FALSE !== $this->multi_isdefined($engines, $ref) ){
			$store_dest = $this->merge_engines($this->store, $store_dest);
		}
		return $this->multi_store($store_dest, $ref, $this->get_updated($ref) );
	}
	
	public function del_updated($ref){
		$engines = $this->merge_engines($this->run_update);
		$engines = $this->merge_engines($this->store_update, $engines);
		return $this->multi_remove(&$engines, $ref);
	}

	public function is_stored($ref){
		$engines = $this->merge_engines($this->run);
		$engines = $this->merge_engines($this->store, $engines);
		if ( $ref == 'search_engine' ){
			//errors::raise("CC is_stored : $ref ".print_r($engines, TRUE), CORE_LOG_NOTICE, 'SEARCH');
		}
		return $this->multi_isdefined(&$engines, $ref);
	}
	
	public function get_stored($ref){
		$engines = $this->merge_engines($this->run);
		$engines = $this->merge_engines($this->store, $engines);
		return $this->multi_read(&$engines, $ref);
	}
	
	public function store($ref, $val, $dest=NULL){
		if ( $dest === self::FOREVER ){
			$engines = $this->merge_engines($this->store); 
		} elseif ( $dest === self::FORSOME ){
			$engines = $this->merge_engines($this->run); 
		} else {
			$engines = $this->merge_engines($this->run);
			$engines = $this->merge_engines($this->store, $engines);
		}
		return $this->multi_store($engines, $ref, $val);
	}
	
	public function del_stored($ref){
		$engines = $this->merge_engines($this->run);
		$engines = $this->merge_engines($this->store, $engines);
		return $this->multi_remove(&$engines, $ref);
	}
	
	
	
	
	private function merge_engines($new, $buf=Array()){
		if ( !is_array($new) ){ return $buf; }
		foreach($new as $type => $conf ){
			$ok=TRUE;
			foreach($buf as $engine => &$data ){
				if ($data[0] === $type AND $data[1] == $conf ){ $ok=FALSE; }
			}
			if ( $ok ){ $buf[] = Array($type, $conf); }
		}
		return $buf;
	}
	

	
	
	private function init_engine($engine, $conf){
		static $once;
		$hash = @md5($engine.$conf);
		if ( isset($once[$hash]) ){ return; }
		//errors::raise("CC : INIT engine $engine : $conf", CORE_LOG_NOTICE, 'SEARCH');
		switch($engine){
			case self::POST :		break;
			case self::GET :		break;
			case self::COOKIES :	break;
			case self::SESSION :	if ( !sessions::istarted($conf) ){ sessions::start($conf); }
									break;
			case self::PROFILE :	if ( !isset(self::$PROF[$conf]) ){ self::$PROF[$conf] = new profile($conf); }
									break;
		}
		$once[$hash] = TRUE;
		return;
	}
	
	private function multi_isdefined(&$engines, $ref){
		if ( !is_array($engines) ){ return FALSE; }
		foreach($engines as $key => &$data ){
			self::init_engine($data[0], $data[1]);
			switch($data[0]){
					case self::POST :		$o = isset($_POST[$ref]);break;
					case self::GET :		$o = isset($_GET[$ref]);break;
					case self::COOKIES :	$o = cookies::isdefined($ref, $data[1]);break;
					case self::SESSION :	$o = sessions::isdefined($ref, $data[1]);break;
					case self::PROFILE :	$o = self::$PROF[$data[1]]->isdefined($ref);break;
			}
			if ( $ref == 'search_engine' ){
				//errors::raise("CC : $data[0] isdefined : $ref => [".$o."]", CORE_LOG_NOTICE, 'SEARCH');
			}
			if ( isset($o) ){ 
				if ( $o === TRUE ){ break; }
			}
		}
		if ( !isset($o) ){ return FALSE; }
		return $o;
	}
	
	private function multi_read(&$engines, $ref){
		foreach($engines as $key => &$data ){
			self::init_engine($data[0], $data[1]);
			switch($data[0]){
				case self::POST :		if ( isset($_POST[$ref]) ){ $o = $_POST[$ref];}break;
				case self::GET :		if ( isset($_GET[$ref]) ){ 	$o = $_GET[$ref];}break;
				case self::COOKIES :	if ( TRUE === cookies::isdefined($ref, $data[1]) ){ $o = cookies::read($ref, $data[1]); }break;
				case self::SESSION :	if ( TRUE === sessions::isdefined($ref, $data[1]) ){ $o = sessions::read($ref, $data[1]); }break;
				case self::PROFILE :	if ( TRUE === self::$PROF[$data[1]]->isdefined($ref) ){ $o = self::$PROF[$data[1]]->read($ref); }break;
			}
			if ( isset($o) ){ return $o; }
		}
		return FALSE;
	}
	
	private function multi_store(&$engines, $ref, $val){
		foreach($engines as $k => &$data ){
			self::init_engine($data[0], $data[1]);
			switch($data[0]){
				case self::POST :		$_POST[$ref] = $val;break;
				case self::GET :		$_GET[$ref] = $val;break;
				case self::COOKIES :	$o = cookies::set($ref, $val, $data[1]);break;
				case self::SESSION :	$o = sessions::set($ref, $val, $data[1]);break;
				case self::PROFILE :	$o = self::$PROF[$data[1]]->set($ref, $val);break;
			}
			if ( $ref == 'search_engine' ){
				//errors::raise("CC : $data[0] Store : $ref => $val ", CORE_LOG_NOTICE, 'SEARCH');
			}
			if ( isset($o) ){ 
				if ( $o === FALSE ){ 
					//could not be stored into $type:$conf....
					return FALSE;
				}
			}
		}
		return TRUE;
	}
	
	private function multi_remove(&$engines, $ref){
		foreach($engines as $k => &$data ){
			self::init_engine($data[0], $data[1]);
			switch($data[0]){
				case self::POST :		unset($_POST[$ref]);break;
				case self::GET :		unset($_GET[$ref]);break;
				case self::COOKIES :	cookies::del($ref, $data[1]);break;
				case self::SESSION :	sessions::del($ref, $data[1]);break;
				case self::PROFILE :	self::$PROF[$data[1]]->del($ref);break;
			}
		}
		return TRUE;
	}
	
	
}
Return current item: Simple Way to Usenet