<?php
// INI Files parser.
// Created for: Fatal1ty bot.
// Copyright Remco "MisterGT" Pander.
// Mail: mistergtx@gmail.com
$ini = new ini('Fatal1ty/bot.ini');
$ini -> setFileName('Fatal1ty/test.ini');
$var = $ini -> get('variable', 'sectie');
$ini -> set('variable', 'waarde', 'sectie');
$ini -> write();

class ini
{
	// File name.
	public $file_name = 'file.ini';
	
	// File contents.
	public $contents = null;
	
	// Parsed.
	public $variables = array();
	
	// Constructor function.
	public function __construct($file_name = null)
	{
		# File name given?
		if($file_name == null)
		{
			return;
		}
		
		# Set the file name.
		$this -> file_name = $file_name;
		
		# File does exists?
		if(!file_exists($file_name))
		{
			return;
		}
		
		# Parse the file.
		$this -> parse();
	}
	
	// Set a new filename.
	public function setFileName($name)
	{
		$this -> file_name = $name;
		
		return $this -> file_name;
	}
	
	// Parse a ini file.
	public function parse()
	{
		# Make sure the file exists.
		if(!isset($this -> file_name) || !file_exists($this -> file_name))
		{
			return false;
		}
		
		# Fetch the file contents.
		$this -> contents = file_get_contents($this -> file_name);
		
		# Check if everything still went fine..
		if($this -> contents == false)
		{
			return false;
		}
				
		# Split the contents up in newlines.
		$lines = explode("\n", $this -> contents);
		
        # Loop trought the lines.
        foreach($lines as $line)
        {
        	$line = trim($line);
        	
        	# Empty line
        	if($line == '')
        	{
        		continue;
        	}
        	
        	# Comment.
        	if(substr($line, 0, 1) == ';')
        	{
        		continue;
        	}
        	
        	# Section.
        	if(substr($line, 0, 1) == '[' && substr($line, - 1) == ']')
        	{
        		$this -> section = substr($line, 1, -1);
        		$this -> variables[$this -> section] = array();
        		
        		continue;
        	}
        	
        	# Variable.
        	$exploded = explode('=', $line);
        	
			# Trim
			foreach($exploded as $i => $variable)
			{
				$exploded[$i] = trim($variable);
			}
			
			$variable = $exploded[0];
			$answer   = $exploded[1];
			
			# If it already exists, make an array.
			if(isset($this -> variables[$this -> section][$variable]))
			{
				# Already an array?
				if(is_array($this -> variables[$this -> section][$variable]))
				{
					$this -> variables[$this -> section][$variable][] = $answer;
				}else{
					$answerCurrent = $this -> variables[$this -> section][$variable];
					$this -> variables[$this -> section][$variable] = array($answerCurrent, $answer);
				}
			}else{
				$this -> variables[$this -> section][$variable] = $answer;
			}
        }
		
		return count($this -> variables);
	}
	
	// Write a ini file.
	public function write()
	{
		$output = null;
		
		# Loop trought the sections.
		foreach($this -> variables as $section => $variables)
		{
			$output .= "[".$section."]\n";
			
			# Loop trought the variables.
			foreach($this -> variables[$section] as $variable => $answer)
			{
				# Array?
				if(is_array($this -> variables[$section][$variable]))
				{
					# Loop (again)
					foreach($this -> variables[$section][$variable] as $answerX)
					{
						$output .= $variable." = ".$answerX."\n";
					}
				}else{
					$output .= $variable." = ".$answer."\n";
				}
			}
		}
		
		# Write output.
		file_put_contents($this -> file_name, $output);		
	}
	
	// Get the answer from a variable.
	public function get($variable, $section)
	{
		if(!isset($this -> variables[$section][$variable]))
		{
			return null;
		}
		
		return $this -> variables[$section][$variable];
	}
	
	// Change a variable.
	public function set($variable, $new, $section)
	{
		$this -> variables[$section][$variable] = $new;
	}
	
	// Remove a variable.
	public function destroy($variable, $section)
	{
		if(isset($this -> variables[$section][$variable]))
		{
			unset($this -> variables[$section][$variable]);
		}
		
		return true;
	}
}
?>