[ Index ]
 

Code source de Dotclear 1.2.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/classes/ -> class.ini.file.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of DotClear.
   4  # Copyright (c) 2004 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # DotClear is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # DotClear is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with DotClear; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class iniFile
  24  {
  25      var $content;
  26      var $var_reg = '/^[\s]*(%s)[\s]*?=[\s*](.*)$/m';
  27      
  28  	function iniFile($file)
  29      {
  30          if (file_exists($file)) {
  31              $this->file = $file;
  32              $this->content = implode('',file($file));
  33          } else {
  34              $this->file = false;
  35          }
  36      }
  37      
  38      /* édition d'une variable */
  39  	function editVar($name,$value)
  40      {
  41          if ($this->file !== false)
  42          {
  43              $match = sprintf($this->var_reg,preg_quote($name));
  44              
  45              if (preg_match($match,$this->content))
  46              {
  47                  $replace = '$1 = '.$value;
  48                  $this->content = preg_replace($match,$replace,$this->content);
  49              }
  50              else
  51              {
  52                  $this->createVar($name,$value);
  53              }
  54          }
  55      }
  56      
  57      /* création d'un variable */
  58  	function createVar($name,$value,$comment='')
  59      {
  60          $match = sprintf($this->var_reg,preg_quote($name));
  61          
  62          if ($comment != '') {
  63              $comment = '; '.str_replace("\n","\n; ",$comment)."\n";
  64          }
  65          
  66          if (!preg_match($match,$this->content))    {
  67              $this->content .= "\n\n".$comment.$name.' = '.$value;
  68          }
  69      }
  70      
  71      /* sauvegarde du fichier */
  72  	function saveFile()
  73      {
  74          if (($fp = @fopen($this->file,'w')) !== false) {
  75              if (@fwrite($fp,$this->content,strlen($this->content)) !== false) {
  76                  $res = true;
  77              } else {
  78                  $res = false;
  79              }
  80              fclose($fp);
  81              return $res;
  82          } else {
  83              return false;
  84          }
  85      }
  86      
  87      /*
  88      Static method that puts ini vars in constants or returns array
  89      */
  90  	function read($file,$return=false)
  91      {
  92          if (!file_exists($file)) {
  93              trigger_error('No config file',E_USER_ERROR);
  94              exit;
  95          }
  96          
  97          $f = file($file);
  98          
  99          if ($return) {
 100              $res = array();
 101          }
 102          
 103          foreach ($f as $v)
 104          {
 105              $v = trim ($v);
 106              if (substr($v,0,1) != ';' && $v != '') {
 107                  $p = strpos($v,'=');
 108                  $K = (string) trim(substr($v,0,$p));
 109                  $V = (string) trim(substr($v,($p+1)));
 110                  
 111                  if (substr($V,0,1) == '"' && substr($V,-1) == '"') {
 112                      $V = substr(substr($V,1),0,-1);
 113                  }
 114                  
 115                  if ($V === 'yes' || $V === 'true' || $V === '1') {
 116                      $V = true;
 117                  }
 118                  
 119                  if ($V === 'no' || $V === 'false' || $V === '0') {
 120                      $V = false;
 121                  }
 122                  
 123                  if ($return) {
 124                      $res[$K] = $V;
 125                  } elseif (!defined($K)) {
 126                      define($K,$V);
 127                  }
 128              }
 129          }
 130          
 131          if ($return) {
 132              return $res;
 133          }
 134      }
 135  }
 136  
 137  
 138  ?>


Généré le : Fri Feb 23 21:40:15 2007 par Balluche grâce à PHPXref 0.7