Location: PHPKode > scripts > TIniFile > tinifile/IniFile.class.php
<?php

	/**
	* @author Gastón Furini
	* @version 0.5
	* @since 05/12/2006 07:11:16 p.m.
	* @copyright 2006 - DevStudios Software [http://devstudios.com.ar]
	*/

	if(!defined('IniFile.class.PHP')){
		define('IniFile.class.PHP', true);

		class IniFile{
			var $FileName;
			var $IniParsed;
			var $PrivateMessage;
			
			function IniFile($FileName){
				$this->PrivateMessage = "Private config file";
				$this->FileName = $FileName;
				if(file_exists($FileName)){
					$this->IniParsed = parse_ini_file($FileName, true);
					if(count($this->IniParsed) == 0)
						$this->IniParsed = false;
				}else
					$this->IniParsed = false;
			}

			function Clear(){
				unset($this->IniParsed);
				$this->IniParsed = array();
			}
			
			function DeleteKey($Section, $Ident){
				if($this->ValueExists($Section, $Ident))
					unset($this->IniParsed[$Section][$Ident]);
			}
			
			function EraseSection($Section){
				if($this->SectionExists($Section))
					unset($this->IniParsed[$Section]);
			}
			
			function GetStrings(&$Content){
				$Content = '';
				$sections = '';
				foreach($this->IniParsed as $key => $item){
					if(is_array($item)){
						$sections .= "\n[{$key}]\n";
						foreach($item as $key2 => $item2){
							if(is_numeric($item2) || is_bool($item2))
								$sections .= "{$key2} = {$item2}\n";
							else
								$sections .= "{$key2} = \"{$item2}\"\n";
						}     
					}else{
						if(is_numeric($item) || is_bool($item))
							$Content .= "{$key} = {$item}\n";
						else
						$Content .= "{$key} = \"{$item}\"\n";
			    }
				}
				$Content .= $sections;
			}
			
			function ReadSection($Section, &$Strings){
				if($this->SectionExists($Section)){
					$Strings = array_keys($this->IniParsed[$Section]);
				}else
					$Strings = null;
			}
			
			function ReadSections(&$Strings){
				if($this->IniParsed){
					$Strings = array_keys($this->IniParsed);
				}else
					$Strings = null;
			}
			
			function ReadSectionValues($Section, &$Strings){
				if($this->SectionExists($Section)){
					$Strings = array_values($this->IniParsed[$Section]);
				}else
					$Strings = null;
			}
			
			function Read($Section, $Ident, $Default){
				if($this->ValueExists($Section, $Ident))
					return $this->IniParsed[$Section][$Ident];
				else
					return $Default;
			}
			
			function Rename($FileName, $DeleteOld = false){
				$this->GetStrings($Content);
				$Content = ";<?php exit('".$this->PrivateMessage."'); ?>\n".$Content;
				if(!$handle = fopen($FileName, 'w'))
					return false;
				if(!fwrite($handle, $Content))
					return false;
				fclose($handle);
				if($DeleteOld)
					unlink($this->FileName);
				$this->FileName = $FileName;
				return true;
			}
			
			function SectionExists($Section){
				if($this->IniParsed && strlen($Section) > 0)
					return array_key_exists($Section, $this->IniParsed);
				else
					return false;
			}
			
			function UpdateFile(){
				$this->GetStrings($Content);
				$Content = ";<?php exit('".$this->PrivateMessage."'); ?>\n".$Content;
				if(!$handle = fopen($this->FileName, 'w'))
					return false;
				if(!fwrite($handle, $Content))
					return false;
				fclose($handle);
				return true;
			}
						
			function ValueExists($Section, $Ident){
				if($this->SectionExists($Section) && strlen($Ident) > 0)
					return array_key_exists($Ident, $this->IniParsed[$Section]);
				else
					return false;
			}
			
			function Write($Section, $Ident, $Value){
				$this->IniParsed[$Section][$Ident] = $Value;
			}
			
			function WriteSection($Section){
				$this->IniParsed[$Section] = array();
			}
			
			function RenameSection($Section, $NewSection){
				if($this->SectionExists($Section) && !$this->SectionExists($NewSection)){
					$this->IniParsed[$NewSection] = $this->IniParsed[$Section];
					unset($this->IniParsed[$Section]);
				}
			}
		}
	}	
?>
Return current item: TIniFile