<?php
/*  Classe de gestion d'un fichier de configuration, inspiré du code d Olivier Meunier.en gnu gpl, du Code is DotClear Weblog.
Modif importantes pour adaptation lossim. retour erreur et lieaison avec couche abstraction type sql
*/
if (!defined('ERR_10')) define('ERR_10', 'ERR_10: Prefix ou colonne introuvable');
class configfile
{
	var $file;
	var $content;
	var $prefix ;

	function configfile($file){
		clearstatcache();
		if (file_exists($file)) {
			$this->file = $file;
			$f = fopen($file, 'r');
			    while (!feof($f))  {
			           $ligne = fgets($f,4096);
			           if(strlen(trim($ligne))){
			         	$this->content .=$ligne;
			           }
			    }
			fclose($f);
			return $this->file;
		} else {
// 			return 'FILE NOT FOUND';
			$this->file = false;
		}
	}
	/* Construction retour erreur avec detail */
	function ret_error($type,$prefix, $colonne, $value=''){
		if (!isset($this->error))$this->error='';
		$detail2='';
		$match = '/(\$'.preg_quote($this->prefix.$colonne,'/').')[\s]*=[\s]*(.+);/mU';
		if (preg_match($match,$this->content)) return $this->error .="\n\n TYPE: ".ERR_20;
		if (!eregi('('.preg_quote($this->prefix).')',$this->content))return $this->error .="\n\n TYPE: ".ERR_21;

		$detail ="\n &nbsp;&nbsp;-- DETAIL -- ";
		$detail .="\n &nbsp;&nbsp;&nbsp; - prefix : ".$prefix;
		$detail .="\n &nbsp;&nbsp;&nbsp; - colonne : ".$colonne;
		switch($type) {
			case 'suppVar_not_fond':
				$type="\n\n TYPE: ".ERR_10;
				$detail2 .="\n &nbsp;&nbsp;&nbsp; - shearch : ".$value;
			break;
			case 'getVar_not_fond':
				$type="\n\n TYPE: ".ERR_10;
			break;
			case 'editVar_not_fond':
				$type="\n\n TYPE: ".ERR_10;
				$detail2  .="\n &nbsp;&nbsp;&nbsp; - value : ".$value;
			break;
			case 'rennVar_not_fond':
				$type="\n\n TYPE: ".ERR_10;
				$detail2  .="\n &nbsp;&nbsp;&nbsp; - new name colonne : ".$value;
			break;
		}
		$this->error .= $type .$detail . @$detail2 ;

		return $this->error;
	}
	/** edition d'une variable */
	function editVar($name,$value){
		if ($this->file !== false){
			if (is_array($name)) {
				$temp = '';
				reset($name);
				foreach ($name as $k => $v) {
					$temp .= '[\''.$v.'\']';
				}
				$name = $temp;
			} else {
				$name = "['$name']";
			}
			$match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);#/mU';
			if (preg_match($match,$this->content)){
				$value = $this->exp_var($value);
				$replace = '$1 = '.$value.';#';
				$this->content = preg_replace($match,$replace,$this->content);
				return true;
			} else {
				$this->ret_error('editVar_not_fond',$this->prefix, $name, $value);
				return false;
			}
		}
	}
	/** RENOMME d'une variable */
	function rennVar($name,$new_name){
		if ($this->file !== false){
			$name = "['$name']";
			$match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);#/mU';
			if (preg_match($match,$this->content)){
				$new_name = $this->exp_var($new_name);
				$replace = "\$".$this->prefix."[$new_name]".' = $2 ;#';
				$this->content = preg_replace($match,$replace,$this->content);
				return true;
			}else {
				$this->ret_error('rennVar_not_fond',$this->prefix, $name, $new_name);
				return false;
			}
		}
		else return false;
	}
	/** recuperation d'une variable sans evaluation (string en sortie)*/
	function getVar($name)	{
		if ($this->file !== false){
			if (is_array($name)) {
				$temp = '';
				reset($name);
				foreach ($name as $k => $v) {
					$temp .= '[\''.$v.'\']';
				}
				$name = $temp;
			} else {
				$name = "['$name']";
			}
			$match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);#/mU';
			if (preg_match($match	,$this->content,$res)){
				return trim($res[2]);
			}else {
				$this->ret_error('getVar_not_fond',$this->prefix, $name);
				return false;
			}
		}
		return false;
	}
	/** recup var sans (string en sortie) retourne ss forme de tbl avec key et value  Si colonne preciser, alors cheche dans colonne (ID, key 1 dans array 2d) la valeur de name. */
	function getVar_t($name='',$colonne='',$condition=''){
		if ($this->file !== false){
			$s=array();
			include ($this->file);
// 			if (!eregi($this->prefix,$this->content)) return  $this->ret_error('editVar_not_fond',$this->prefix, $name, $colonne,$condition);
			if( (isset(${$this->prefix}))AND(is_array(${$this->prefix}))){
				while ($r=each(${$this->prefix}))
					if ( ( tep_not_null($name) && $r['key']==$name ) OR empty($name) ){ // name correpsond ID si ok
						if (is_array($r['value'])){ // tabl 2D
							if ( (tep_not_null($colonne) && $r['value'][$colonne]==$condition) OR empty($colonne) ){ // clause condiftion
							while ($rs=each($r['value']))
								$s[ereg_replace("[\['|'\]|']","",$r['key'])][ereg_replace("[\['|'\]|']","",$rs['key'])] = $rs['value'];
							}
						}
						else { // tabl 1D
							$s[ereg_replace("[\['|'\]|']","",$r['key'])] = $r['value'];
						}
					}
// 					else {
// 						$this->ret_error('editVar_not_fond',$name, $colonne, $condition);
// 						return false;
// 					}
				return $s ;
			}else {
				$this->ret_error('editVar_not_fond',$this->prefix, $name, $colonne,$condition);
				return false;
			}
		}
		return false;
	}
	/** suppression d'une variable */
	function suppVar($name,$value=''){
		if (is_array($name)) {
			$temp = '';
			reset($name);
			foreach ($name as $k => $v) {
				$temp .= '[\''.$v.'\']';
			}
			$name = $temp;
		} else {
			$name = "['$name']";
		}
		$match = '/(\$'.preg_quote($this->prefix.$name,'/').')[\s]*=[\s]*(.+);#/mU';
		if (preg_match($match,$this->content)){
			$replace = null;
			$this->content = preg_replace($match,$replace,$this->content);
			return true;
		}
	}
	/** ajouter une variable */
	function addVar($name, $value, $comment=''){
		if (false === $this->getVar($name)){
			if (is_array($name)) {
				$temp = '';
				reset($name);
				foreach ($name as $k => $v) {
					$temp .= '[\''.$v.'\']';
				}
				$name = $temp;
			} else {
				$name = "['$name']";
			}
			//the ? > at the end of the file is used as a marker
			$string = '';
			if (!empty($comment)) $string = '/* '.$comment.' */'."\n";
			$value = $this->exp_var($value);
			$string .= '$'.$this->prefix.$name.' = '.$value.';#'."\n".'?>';
			$this->content = str_replace('?>', $string, $this->content);
			return true;
		}
		return false;
	}


	/** sauvegarde du fichier */
	function saveFile(){
// 		if ($this->file !== false){
// 		fopen($this->file,'w');
		if (($fp = @fopen($this->file,'w')) !== false) {
			if (fwrite($fp,$this->content,strlen($this->content)) !== false) {
				$res = true;
			} else {
				$res = false;
			}
			fclose($fp);
			return $res;
		} else {
			return false;
		}
	}

	/** exportation d'une variable */
	function exp_var($var){
		if (gettype($var) == 'array'){
			$arry_res = array();
			foreach ($var as $k => $v) {
				$arry_res[] = $k.' => '.$this->exp_var($v);
			}
			return 'array('.implode(',',$arry_res).')';
		}
		elseif (gettype($var) == 'string'){
			return "'".addslashes(stripSlashes($var))."'";
		}
		elseif (gettype($var) == 'boolean'){
			return ($var) ? 'true' : 'false';
		}
		else	{
			return $var;
		}
	}

	function return_content(){
		return $this->content;
	}
}
?>