File indexing completed on 2024-05-12 17:26:16

0001 <?php
0002 
0003 /*
0004  *   GFX 4
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 /*
0015  * This class represents a config file and can be used
0016  * to edit those files.
0017  * 
0018  * TODO: support also multivalue config files
0019  * 
0020  * example:
0021  * 
0022  * var1|data1
0023  * var2|data2
0024  * var3|data3
0025  * 
0026  */
0027 
0028 class EConfigFile {
0029   
0030   private $name = false;
0031   private $header = '<?php die(); ?>';
0032   private $data = false;
0033   
0034   /** Set the config file based on local site configuration.
0035    * 
0036    * @param $name name of config file based on local file configuration without conf.php
0037    * 
0038    * @return EConfigFile object
0039    */
0040   public function __construct($name=false){
0041     if($name!=false){
0042       $this->name = ELoader::$config_path.'/'.$name.'.conf.php';
0043       $this->set_abs_file($this->name);
0044     }
0045   }
0046   
0047   /**
0048    * Used to set config absolute file path.
0049    * Leave constructor empty and use this.
0050    */
0051   public function set_abs_file($filename)
0052   {
0053     $this->name = $filename;
0054     if(!file_exists($this->name)){
0055       ELog::error('Config file not existent: '.$this->name);
0056     } else {
0057       $this->data = EConfig::parse_file($this->name);
0058     }
0059   }
0060   
0061   public function get_data()
0062   {
0063     return $this->data;
0064   }
0065   
0066   /*
0067    * Gets a key value in config file if exists. Else return empty string.
0068    */
0069   public function get($var){
0070     if(isset($this->data[$var])){
0071       return $this->data[$var];
0072     } else {
0073       return '';
0074     }
0075   }
0076   
0077   /*
0078    * Modify or adds (if not present) a key/value pair on a config file
0079    * Change the memory array, not the file on disk.
0080    * In order to have it modified $this->save should be called.
0081    */
0082   public function set($var, $value){
0083     $this->data[$var] = $value;
0084   }
0085   
0086   /**
0087    * Save the representation in memory of the modified data
0088    * in the actual config file.
0089    */
0090   public function save()
0091   {
0092     $stream = fopen($this->name, 'w');
0093     fwrite($stream, $this->header."\n"); //writing protection header
0094     foreach($this->data as $key => $value){
0095       fwrite($stream, $key.'|'.$value."\n");
0096     }
0097     fclose($stream);
0098   }
0099   
0100   /*
0101    * Change the memory array, not the file on disk.
0102    */
0103   public function del($var){
0104     if(array_key_exists($var, $this->data)){
0105       //memory deletion
0106       unset($this->data[$var]);
0107       //rebuilding array indexes
0108       //$this->data = array_values($this->data);
0109     }
0110   }
0111 }
0112 
0113 ?>