Your IP : 216.73.217.177


Current Path : /home/balossw/www/temp/common/includes/
Upload File :
Current File : /home/balossw/www/temp/common/includes/ini.inc.php

<?php
/*---------------------------------------------------------------------------
		File name ............ includes/classes/ini.inc.php
		Author ............... Antoine Lemoine
		Creation date ........ 02.09.2009
		Modification date .... 29.01.2010
---------------------------------------------------------------------------*/


include_once $global->realpath('common/includes/files.inc.php');

if (!class_exists('IniFile'))
{
	class IniFile
	{
		var $file_exists;
		var $filename;
		var $table;

		function IniFile($arg=null)
		{
			if (isset($arg) && is_string($arg))
			{
				$this->filename = $arg;
				$this->table = $this->_parseIniFile($arg);
			}
			else if (isset($arg) && is_array($arg))$this->table = $arg;
			else$this->table = array();
		}

		function load($filename)
		{
			$new_table = $this->_parseIniFile($filename);
			foreach ($new_table as $group => $data)
			{
				if (!isset($this->table[$group]))$this->table[$group] = $data;
				else$this->table[$group] = array_merge($this->table[$group],$data);
			}
		}

		function exists()
		{
			return $this->file_exists;
		}

		function getTable()
		{
			return $this->table;
		}

		function _parseIniFile($filename)
		{
			$content = file_get_contents($filename);
			
			if (!$content) {
				$this->file_exists = false;
				return array();
			}

			$this->file_exists = true;

			$lines = explode("\n",$content);
			if (!is_array($lines)) $lines = array($lines);
		
			$table = array();
			$current_group = null;
		
			for ($i=0; $i<count($lines); $i++)
			{
				$line = trim($lines[$i]);
				if ($line!='')
				{
					if (preg_match('/^\[(.*)\]$/',$line,$result)) $current_group = $result[1];
					else if (preg_match('/^([^=]+)=(.*)$/',$line,$result))
					{
						if ($current_group!=null)
						{
							$value = $result[2];
							if ($value=='false') $value = false;
							else if ($value=='true') $value = true;
							$table[$current_group][$result[1]] = $value;
						}
					}
				}
			}
			
			return $table;
		}

		function get($group, $key, $default_value=null)
		{
			return isset($this->table[$group][$key]) ? $this->table[$group][$key] : $default_value;
		}

		function getArray($group, $key, $default_value=null)
		{
			return isset($this->table[$group][$key]) ? unserialize($this->table[$group][$key]) : $default_value;
		}

		function set($group, $key, $value)
		{
			$this->table[$group][$key] = is_array($value) ? serialize($value) : $value;
		}

		function write($filename=null)
		{
			if (!isset($filename)) $filename = $this->filename;

			$content = '';
			
			foreach ($this->table as $group_name => $data)
			{
				$content .= '['.$group_name."]\n";
				foreach ($data as $key => $value)
				{
					if (preg_match('/=/',$key)) return false;
					$content .= $key.'='.$value."\n";
				}
				$content .= "\n";
			}

			$file_handle = @fopen($filename,'w');
			if (!$file_handle) return false;
			else
			{
				fputs($file_handle,$content,strlen($content));
				fclose($file_handle);
			}
			return true;
		}
	}
}

?>