| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @author Jonathan Gotti <nathan at the-ring dot homelinux dot net> |
|---|
| 4 | * @copyleft (l) 2003-2004 Jonathan Gotti |
|---|
| 5 | * @package config |
|---|
| 6 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|---|
| 7 | * @changelog - 2007-10-31 - now write_conf_file() keep empty lines. |
|---|
| 8 | * - 2007-04-30 - bug correction regarding single quote escaped in values string |
|---|
| 9 | * - bug correction when saving some multiple multiline values containing '=' at certain position on a line (not the first). |
|---|
| 10 | * - 2007-04-26 - replace double quote strings with single quote string at eval time to avoid replacement of escaped values (ie: \[rnt....]) |
|---|
| 11 | * - 2007-03-27 - change regexp in parse_conf_file() to better support multilines values (line ended by \) |
|---|
| 12 | * - 2005-09-30 - remove GUI_TYPE==GTK suppport |
|---|
| 13 | * - optimize all the code for better performance (parsing) and better multiline support (writing) |
|---|
| 14 | * - 2005-06-11 - now write_conf_file() can unset or comment some vars using --COMMENT--,--UNSET-- in place of the value |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * read a config file and define CONSTANT in it or return it as an array |
|---|
| 19 | * @param string $file_path |
|---|
| 20 | * @param bool $out default is FALSE mean define the value, return an array if set to TRUE |
|---|
| 21 | * @return array | bool depend on $out |
|---|
| 22 | */ |
|---|
| 23 | function parse_conf_file($file_path,$out = false){ |
|---|
| 24 | if(! file_exists($file_path)) |
|---|
| 25 | return FALSE; |
|---|
| 26 | # get file content |
|---|
| 27 | if(! is_array($conf = file($file_path))){ |
|---|
| 28 | return False; |
|---|
| 29 | } |
|---|
| 30 | # parse conf file |
|---|
| 31 | $preserve = FALSE; |
|---|
| 32 | foreach($conf as $line){ |
|---|
| 33 | if(preg_match("!^\s*#+!",$line)) |
|---|
| 34 | continue; |
|---|
| 35 | if($preserve && preg_match("!^\s*(.*?)(\\\\?)\s*$!",$line,$match)){ # continue line |
|---|
| 36 | $value .= "\n$match[1]"; |
|---|
| 37 | $preserve = ($match[2]!=='\\'?FALSE:TRUE); |
|---|
| 38 | }elseif(preg_match("!^\s*([^#=]+)=(.*?)(\\\\?)\s*$!",$line,$match)){ # read line |
|---|
| 39 | $var = trim($match[1]); |
|---|
| 40 | $value= $match[2]; |
|---|
| 41 | $preserve = ($match[3]!=='\\'?FALSE:TRUE); |
|---|
| 42 | }else{ |
|---|
| 43 | continue; # considered as commentary |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | if($preserve) continue; |
|---|
| 47 | $value = preg_replace(array("!(%([^%\s]+)%)!","!\\\\\s*\r?\n!"),array("'.\\2.'","\n"),trim($value)); |
|---|
| 48 | |
|---|
| 49 | if(! in_array(strtoupper($value),array('NULL','FALSE','TRUE')) ) |
|---|
| 50 | $value = "'".($out?preg_replace('!(\\\\|\')!','\\\\\1',$value):$value)."'"; |
|---|
| 51 | |
|---|
| 52 | if(! $out) |
|---|
| 53 | eval("define('".trim($var)."',".$value.");"); |
|---|
| 54 | else |
|---|
| 55 | eval('$out_[$var]='.$value.';'); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | return $out?@$out_:TRUE; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * prend un tableau associatif et ajoute les entrée dans un fichier de configuration |
|---|
| 63 | * en conservant les commentaires ainsi que les valeurs non renseigné |
|---|
| 64 | * @param string $file file to write configuration |
|---|
| 65 | * @param array $config the configuration to add to config file |
|---|
| 66 | * @param bool $force if true create the file if doesn't exist |
|---|
| 67 | * @return bool |
|---|
| 68 | * @changelog 2005-06-11 now can unset or comment some vars using --COMMENT--,--UNSET-- in place of the value |
|---|
| 69 | */ |
|---|
| 70 | function write_conf_file($file,$config,$force=FALSE){ |
|---|
| 71 | if(! is_array($config)) |
|---|
| 72 | return FALSE; |
|---|
| 73 | $fileExist = file_exists($file); |
|---|
| 74 | # check if file exist or not |
|---|
| 75 | if( !( $fileExist || $force ) ) |
|---|
| 76 | return FALSE; |
|---|
| 77 | $oldconf = $fileExist?file($file):null; |
|---|
| 78 | # get the old config |
|---|
| 79 | if( is_null($oldconf) && ! $force ) |
|---|
| 80 | return FALSE; |
|---|
| 81 | # first rewrite old conf |
|---|
| 82 | if(is_array($oldconf)){ |
|---|
| 83 | $follow = FALSE; |
|---|
| 84 | foreach($oldconf as $linenb => $line){ |
|---|
| 85 | if( preg_match("!^\s*#!",$line)){# keep comment lines |
|---|
| 86 | $newconf[$linenb]=$line; |
|---|
| 87 | }elseif( (!$follow) && preg_match("!^\s*([^#=]+)=([^#\\\\]+)(\\\\?)!",$line,$match)){ # first line of config var |
|---|
| 88 | $var = trim($match[1]); # get varname |
|---|
| 89 | |
|---|
| 90 | if(! isset($config[$var])) # not set so keep the line as is |
|---|
| 91 | $newconf[$linenb] = $line; |
|---|
| 92 | else # we have a new value we write it |
|---|
| 93 | $newconf[$linenb] = _write_conf_line($var,$config[$var],$line); |
|---|
| 94 | |
|---|
| 95 | if(preg_match('!\\\\\s*\n$!',$line)) |
|---|
| 96 | $follow = TRUE; |
|---|
| 97 | }elseif($follow){ # multiline values |
|---|
| 98 | if(!isset($config[$var])){ # keep old multiline values |
|---|
| 99 | $newconf[$linenb] = $line; |
|---|
| 100 | }elseif(trim($config[$var])==='--COMMENT--' ){ # comment all multilines values |
|---|
| 101 | $newconf[$linenb] = "#~ $line"; |
|---|
| 102 | } |
|---|
| 103 | if(! preg_match('!\\\\\s*\n$!',$line)) |
|---|
| 104 | $follow = FALSE; |
|---|
| 105 | }else{ |
|---|
| 106 | $newconf[$linenb] = $line; |
|---|
| 107 | } |
|---|
| 108 | if( (! $follow) && @isset($config[$var]) ) |
|---|
| 109 | unset($config[$var]); |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | if(count($config)>0){ # write new config vars at the end |
|---|
| 113 | foreach($config as $var=>$value) |
|---|
| 114 | $newconf[] = _write_conf_line($var,$value); |
|---|
| 115 | } |
|---|
| 116 | }elseif($force){ |
|---|
| 117 | foreach($config as $var=>$value) |
|---|
| 118 | $newconf[] = _write_conf_line($var,$value); |
|---|
| 119 | } |
|---|
| 120 | return array2file(@$newconf,$file); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * take an array and write each value as a line |
|---|
| 125 | * @param array $arr array containings data to write |
|---|
| 126 | * @param string $file file to write in it |
|---|
| 127 | * @return bool |
|---|
| 128 | */ |
|---|
| 129 | function array2file($arr,$file){ |
|---|
| 130 | if(! is_array($arr) ) return FALSE; |
|---|
| 131 | if(! $f = fopen($file,'w')) |
|---|
| 132 | return FALSE; |
|---|
| 133 | fputs($f,implode('',$arr)); |
|---|
| 134 | return fclose($f); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * used by write_conf_file to prepare line for passed values |
|---|
| 139 | */ |
|---|
| 140 | function _write_conf_line($var,$value=null,$oldline=null){ |
|---|
| 141 | $commented = (substr_count($value,'--COMMENT--')?TRUE:FALSE); |
|---|
| 142 | $value = $value?preg_replace("!([^\\\\])\r?\n!",($commented?"\\1\\\\\n# ":"\\1\\\\\n"),$value):$value; |
|---|
| 143 | if($commented) |
|---|
| 144 | $line = (($oldline && trim($value)==='--COMMENT--')?"#~ $oldline":"# $var = ".str_replace('--COMMENT--','',$value)."\n"); |
|---|
| 145 | elseif($value === '--UNSET--') |
|---|
| 146 | $line = ''; |
|---|
| 147 | else |
|---|
| 148 | $line = "$var = ".$value."\n"; |
|---|
| 149 | return $line; |
|---|
| 150 | } |
|---|
| 151 | ?> |
|---|