//inifiles.inc.php
<?
if (!defined(“INI_FILE_PHP_CLASS”))
{
define(“INI_FILE_PHP_CLASS”,true);
//
// inifiles.inc.php
// ini files
//
class IniFile
{
var $filename = “”; // Filename to use
var $results = array(); // Array that stores the resulsts
var $loaded = false; // Is file already loaded?
// Constructor – requires filename
function IniFile($filename)
{
$this->setFile($filename);
}
// Set the file to use
function setFile($filename)
{
$this->filename = $filename;
$this->clear();
}
// Clear buffer
function clear()
{
$this->loaded = false;
unset($this->results);
}
// Loads a file
function loadFile()
{
// Check if file exists and if its readable
if(file_exists($this->filename) && is_file($this->filename) &&
is_readable($this->filename))
{
// Clears results
$this->clear();
// Parse the ini file
$this->results = parse_ini_file($this->filename,true);
// Mark loaded flag
$this->loaded = true;
}
}
// Returns the value of an option ($option) in a given section ($section)
// or an empty string if it was not found
function getValue($section, $option)
{
// If the file is not loaded yet…
if(!($this->loaded))
{
// …load it to memory
$this->loadFile();
}
// Does section exists in results array?
if(isset($this->results[$section]))
{
// Get the values of that section from results array
$sectionValues = $this->results[$section];
// Test if the result for that section is an array
if(is_array($sectionValues))
{
// It’s an array
// Now check if the index $option is defined
if(isset($sectionValues[$option]))
{
// Done. Return the value in that position
return $sectionValues[$option];
}
else
{
// No, the option in that section does not exist.
// Return an empty string
return “”;
}
}
// Is not an array, so section isn’t really a section
// but an option not belonging to any section
else
{
// Return the value
return $sectionValues;
}
}
// Did not find any section or option…
else
{
// Return an empty section
return “”;
}
}
// Returns an array with all the pairs option => value of a section
function getSection($section)
{
// If the file is not loaded yet…
if(!($this->loaded))
{
// …load it to memory
$this->loadFile();
}
// Does section exists in results array?
if(isset($this->results[$section]))
{
// Get the values of that section from results array
$sectionValues = $this->results[$section];
// Test if the result for that section is an array
if(is_array($sectionValues))
{
// Return the values of that section
return $sectionValues;
}
// Is not an array, so section isn’t really a section
// but an option not belonging to any section
else
{
$tmp = array();
$tmp[$section] = $sectionValues;
return $tmp;
}
}
// Did not find any section or option…
// …return an empty array
return array();
}
// Sets or changes the value of an option ($option) in a given section ($section).
// If $section is an empty string it sets/changes an option not belonging to
// any section.
// If $write is true then it updates the file in disk, else only in memory
// (this is the default behaviour)
function setValue($section, $option, $value, $write = false)
{
// If section is not null then store value in the section
if($section != “”)
{
$this->results[$section][$option] = $value;
}
// Else, store value outside any section
else
{
$this->results[$option] = $value;
}
if($write == true)
{
return $this->writeFile;
}
return true;
}
// Writes the buffer to the file.
// Note, it recreates the file, so any comments and empty lines are lost
function writeFile()
{
// Write success flag
$ok = true;
// Open file for writing
$fp = fopen($this->filename,”wb”);
// If failed return false
if(!$fp)
{
$ok = false;
}
else
{
// Do we have anything to write?
if(isset($this->results))
{
// Iterate through the results
// Get each section and its values
while(list($section, $values) = each($this->results))
{
// Is the contents of this section an array
if(is_array($values))
{
// Write section name
$res = fwrite($fp,”[$section]\n”);
// File error trapping
if($res == -1)
{
$ok = false;
break;
}
// Iterate through section
// Get each option and value
while(list($option, $value) = each($values))
{
// Write option
$res = fwrite($fp,”$option=$value\n”);
// File error trapping
if($res == -1)
{
$ok = false;
break;
}
}
if($ok == false) break;
}
// If contents is not an array
else
{
// Write option not belonging to any section
$res = fwrite($fp,”$section=$values\n”);
// File error trapping
if($res == -1)
{
$ok = false;
break;
}
}
}
}
// Close file
if(fclose($fp) == -1)
{
$ok = false;
}
}
// Return status
return $ok;
}
}
// Auxiliar function to get a value from an .ini file
// without creating instances of the class IniFile
function getValueFromINI($filename, $section, $option)
{
// Create a new instance
$inifile = new IniFile($filename);
// Call method to retrieve value
return $inifile -> getValue($section,$option);
}
}
?>
//example.php
<?php
include(‘inifile.php’);
// Create a new object to manipulate example.ini
$ini = new IniFile(“example.ini”);
// Get current date and time
$x = getdate();
// Add/change values in .ini file
$ini->setValue(“”,”outside”,”true”);
$ini->setValue(“main”,”user”,”hugo”);
$ini->setValue(“main”,”password”,””);
$ini->setValue(“options”,”width”,800);
$ini->setValue(“options”,”last_visit”,$x[‘mday’].’/’.$x[‘mon’].’/’.$x[‘year’].’ ‘.$x[‘hours’].’:’.$x[‘minutes’].’:’.$x[‘seconds’]);
// Update/create file
echo $ini->writeFile();
?>