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://www.gfx3.org
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 aims to manage all the gfx config in general contained in gfx3/config.
0016  */
0017 
0018 class EConfig{
0019   
0020   private static $safe_path;
0021   private static $config_path;
0022   public static $data;
0023   
0024   public static function load(){
0025     EConfig::$safe_path = getcwd();
0026     EConfig::load_all();
0027     //after loading all the confs restore previous working directory
0028     chdir(EConfig::$safe_path);
0029   }
0030   
0031   public static function print_debug_info(){
0032     var_dump(EConfig::$data);
0033   }
0034   
0035   /*
0036    * Takes an array and returns the same array of strings without
0037    * every string of php code.
0038    */
0039   public static function erase_php_code($lines){
0040     $result = array();
0041     $inside = false;
0042     foreach($lines as $line){
0043       //if opening php string found, count as we're inside php code
0044       if(stristr($line, '<?php')){
0045         $inside = true;
0046       }
0047       //if this is not php code, copy line
0048       if(!$inside){
0049         $result[] = $line;
0050       }
0051       //if closing php string is found, we're not.
0052       if(stristr($line, '?>')){
0053         $inside = false;
0054       }
0055     }
0056     return $result;
0057   }
0058   
0059   /*
0060    * returns a parsed file array mapped as
0061    * $array[$key] = $value
0062    */
0063   public static function parse_file($filename){
0064     //initializing empty array
0065     $result = array();
0066     
0067     //mapping file line per line
0068     //$cache = new ECacheFile($filename);
0069     //$file = $cache->get();
0070     //$file = explode("\n",$file);
0071     $file = file($filename);
0072     $file = EConfig::erase_php_code($file);
0073     foreach($file as $line){
0074       if(!empty($line)){
0075         $chunks = explode("|",$line);
0076         //gives correct key and correct value, erasing line break.
0077         //control if is set more than once
0078         if(isset($result[$chunks[0]])){
0079           //ELog::warning("<b>".$chunks[0]."</b> property is set more than once in ".$filename." config file!");
0080         } else
0081         //control if is set to empty value
0082         if(count($chunks)<2){
0083           ELog::warning("<b>".$chunks[0]."</b> property has empty value in ".$filename." config file!");
0084         } else
0085         //load 1:1 data and gives a simple value
0086         if(count($chunks)==2){
0087           $result[$chunks[0]] = rtrim($chunks[1], "\n");
0088         } else 
0089         //load 1:n data and gives an array
0090         if(count($chunks)>2){
0091           $data = array();
0092           for($i=1;$i<count($chunks);$i++){
0093             $data[] = rtrim($chunks[$i], "\n");
0094           }
0095           
0096           $result[$chunks[0]] = $data;
0097         } else {
0098           ELog::error("unknown error in EConfig loading");
0099         }
0100       }
0101     }
0102     return $result;
0103   }
0104   
0105   /*
0106    * return an array with every config file merged
0107    */
0108   public static function load_all(){
0109     EConfig::$data = array();
0110     //enters in conf directory
0111     chdir(ELoader::$config_path);
0112     //parse every single conf file and place it in an associative array
0113     foreach(glob("*") as $filename){
0114       $name = EFileSystem::get_file_name($filename);
0115       EConfig::$data[$name] = EConfig::parse_file($filename);
0116     }
0117   }
0118   
0119 }
0120 
0121 ?>