123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- class iniFile
- {
- var $content;
- var $var_reg = '/^[\s]*(%s)[\s]*?=[\s*](.*)$/m';
-
- function iniFile($file)
- {
- if (file_exists($file)) {
- $this->file = $file;
- $this->content = implode('',file($file));
- } else {
- $this->file = false;
- }
- }
-
-
- function editVar($name,$value)
- {
- if ($this->file !== false)
- {
- $match = sprintf($this->var_reg,preg_quote($name));
-
- if (preg_match($match,$this->content))
- {
- $replace = '$1 = '.$value;
- $this->content = preg_replace($match,$replace,$this->content);
- }
- else
- {
- $this->createVar($name,$value);
- }
- }
- }
-
-
- function createVar($name,$value,$comment='')
- {
- $match = sprintf($this->var_reg,preg_quote($name));
-
- if ($comment != '') {
- $comment = '; '.str_replace("\n","\n; ",$comment)."\n";
- }
-
- if (!preg_match($match,$this->content)) {
- $this->content .= "\n\n".$comment.$name.' = '.$value;
- }
- }
-
-
- function saveFile()
- {
- 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;
- }
- }
-
-
- function read($file,$return=false)
- {
- if (!file_exists($file)) {
- trigger_error('No config file',E_USER_ERROR);
- exit;
- }
-
- $f = file($file);
-
- if ($return) {
- $res = array();
- }
-
- foreach ($f as $v)
- {
- $v = trim ($v);
- if (substr($v,0,1) != ';' && $v != '') {
- $p = strpos($v,'=');
- $K = (string) trim(substr($v,0,$p));
- $V = (string) trim(substr($v,($p+1)));
-
- if (substr($V,0,1) == '"' && substr($V,-1) == '"') {
- $V = substr(substr($V,1),0,-1);
- }
-
- if ($V === 'yes' || $V === 'true' || $V === '1') {
- $V = true;
- }
-
- if ($V === 'no' || $V === 'false' || $V === '0') {
- $V = false;
- }
-
- if ($return) {
- $res[$K] = $V;
- } elseif (!defined($K)) {
- define($K,$V);
- }
- }
- }
-
- if ($return) {
- return $res;
- }
- }
- }
- ?>
|