home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / phpiniclass.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  5.1 KB  |  193 lines

  1. PHP INI File Class 
  2.  
  3. A Simple Ini File Implementation to keep settings in a simple file instead of in a DB. 
  4.  
  5. <? 
  6. /////////////////////////////////////////////////////////////////////////  
  7. //  
  8. //  class.INIFile.php3  -  implements  a  simple  INIFile Parser  
  9. //   
  10. //  Author:  MO 
  11. //   
  12. //  Description:  
  13. //    I just wandered how to sawe simple parameters not in a database but in a file 
  14. //  So starting every time from scratch isn't comfortable and I desided to write this 
  15. //  small unit for workin with ini like files 
  16. //  Some  Examples:  
  17. //     
  18. //    $ini = new INIFile("./ini.ini"); 
  19. //  //Read entire group in an associative array 
  20. //    $grp = $ini->read_group("MAIN"); 
  21. //    //prints the variables in the group 
  22. //    if ($grp) 
  23. //    for(reset($grp); $key=key($grp); next($grp)) 
  24. //    { 
  25. //        echo "GROUP ".$key."=".$grp[$key]."<br>"; 
  26. //    } 
  27. //    //set a variable to a value 
  28. //    $ini->set_var("NEW","USER","JOHN"); 
  29. //  //Save the file 
  30. //    $ini->save_data(); 
  31.  
  32.  
  33. class  INIFile { 
  34.  
  35.     var $INI_FILE_NAME =  ""; 
  36.     var $ERROR =  ""; 
  37.     var $GROUPS = array(); 
  38.     var $CURRENT_GROUP =  ""; 
  39.      
  40.     function INIFile($inifilename= "") 
  41.     { 
  42.         if(!empty($inifilename)) 
  43.             if(!file_exists($inifilename)){ 
  44.                 $this->error( "This file does not exist!"); 
  45.                 return; 
  46.             } 
  47.         $this->parse($inifilename); 
  48.     } 
  49.  
  50.  
  51. // LOAD AND SAVE FUNCTIONS 
  52.      
  53.     function parse($inifilename) 
  54.     { 
  55.         $this->INI_FILE_NAME = $inifilename; 
  56.         $fp = fopen($inifilename,  "r+"); 
  57.         $contents = fread($fp, filesize($inifilename)); 
  58.         $ini_data = split( "\n",$contents); 
  59.          
  60.         while(list($key, $data) = each($ini_data)) 
  61.         { 
  62.             $this->parse_data($data); 
  63.         } 
  64.          
  65.         fclose($fp); 
  66.     } 
  67.      
  68.     function parse_data($data) 
  69.     { 
  70.         if(ereg( "\[([[:alnum:]]+)\]",$data,$out)) 
  71.         { 
  72.             $this->CURRENT_GROUP=$out[1]; 
  73.         } 
  74.         else 
  75.         { 
  76.             $split_data = split( "=", $data); 
  77.             $this->GROUPS[$this->CURRENT_GROUP][$split_data[0]]=$split_data[1]; 
  78.         } 
  79.     } 
  80.  
  81.     function save_data() 
  82.     { 
  83.         $fp = fopen($this->INI_FILE_NAME, "w"); 
  84.          
  85.         if(empty($fp)) 
  86.         { 
  87.             $this->Error( "Cannot create file $this->INI_FILE_NAME"); 
  88.             return false; 
  89.         } 
  90.          
  91.         $groups = $this->read_groups(); 
  92.         $group_cnt = count($groups); 
  93.          
  94.         for($i=0; $i<$group_cnt; $i++) 
  95.         { 
  96.             $group_name = $groups[$i]; 
  97.             $res = sprintf( "[%s]\n",$group_name); 
  98.             fwrite($fp, $res); 
  99.             $group = $this->read_group($group_name); 
  100.             for(reset($group); $key=key($group);next($group)) 
  101.             { 
  102.                 $res = sprintf( "%s=%s\n",$key,$group[$key]); 
  103.                 echo $res. "\n"; 
  104.                 fwrite($fp,$res); 
  105.             } 
  106.         } 
  107.          
  108.         fclose($fp); 
  109.     } 
  110.  
  111. // FUNCTIONS FOR GROUPS 
  112.      
  113.      //returns number of groups     
  114.     function get_group_count() 
  115.     { 
  116.         return count($this->GROUPS); 
  117.     } 
  118.      
  119.      //returns an array with the names of all the groups 
  120.     function read_groups() 
  121.     { 
  122.         $groups = array(); 
  123.         for(reset($this->GROUPS);$key=key($this->GROUPS);next($this->GROUPS)) 
  124.             $groups[]=$key; 
  125.         return $groups; 
  126.     } 
  127.      
  128.      //checks if a group exists 
  129.     function group_exists($group_name) 
  130.     { 
  131.         $group = $this->GROUPS[$group_name]; 
  132.         if (empty($group)) return false; 
  133.         else return true; 
  134.     } 
  135.  
  136.      //returns an associative array of the variables in one group     
  137.     function read_group($group) 
  138.     { 
  139.         $group_array = $this->GROUPS[$group]; 
  140.         if(!empty($group_array))  
  141.             return $group_array; 
  142.         else  
  143.         { 
  144.             $this->Error( "Group $group does not exist"); 
  145.             return false; 
  146.         } 
  147.     } 
  148.      
  149.      //adds a new group 
  150.     function add_group($group_name) 
  151.     { 
  152.         $new_group = $this->GROUPS[$group_name]; 
  153.         if(empty($new_group)) 
  154.         { 
  155.             $this->GROUPS[$group_name] = array(); 
  156.         } 
  157.         else $this->Error( "Group $group_name exists"); 
  158.     } 
  159.  
  160.  
  161. // FUNCTIONS FOR VARIABLES 
  162.      
  163.      //reads a single variable from a group 
  164.     function read_var($group, $var_name) 
  165.     { 
  166.         $var_value = $this->GROUPS[$group][$var_name]; 
  167.         if(!empty($var_value)) 
  168.             return $var_value; 
  169.         else 
  170.         { 
  171.             $this->Error( "$var_name does not exist in $group"); 
  172.             return false; 
  173.         } 
  174.     } 
  175.      
  176.      //sets a variable in a group 
  177.     function set_var($group, $var_name, $var_value) 
  178.     { 
  179.         if ($this->group_exists($group)) 
  180.             $this->GROUPS[$group][$var_name]=$var_value; 
  181.     }     
  182.  
  183. // ERROR FUNCTION 
  184.              
  185.     function Error($errmsg) 
  186.     { 
  187.         $this->ERROR = $errmsg; 
  188.         echo  "Error:".$this->ERROR. "<br>\n"; 
  189.         return; 
  190.     } 
  191. ?>
  192.