All pastes #429768 Raw Edit

Someone

public php v1 · immutable
·published 2007-04-08 12:51 UTC
rendered paste body
<?php/************************************************************************** Software	: ini_file.class.php** Purpose	: Reads and writes INI (Configuration) files.** Author	: Dean Newman of Project07.com** Licence	: GNU/GPL v2** About	: This system will need read/write access to the ini file. (CHMOD:777)************************************************************************/class ini{	/************************************************************************	** Function	: ini() - Constructor	** About	: This function currenty does nothing. (Placeholder)	************************************************************************/	function ini()	{			}	/************************************************************************	** Function	: read() - File Reader	** About	: This function will read an ini file in to a php array	************************************************************************/	function read($file, $group = false, $var = false)	{		// Filled through function and returned		$output = array();		// Stores the current group		$last_head = false;		// Check readability		if ( ! is_readable($file) )		{			die("Unable to read $file");		}		// Read file		$handle = @file($file);		for ( $i = 0 ; $i < sizeof ( $handle ) ; $i++ )		{			// Set some common variables			$line = $handle[$i];			$fchar = substr($line, 0, 1);			$cmtsrt = array(';', '#');			// Match groups			preg_match('#\[(.*?)\]#is', $line, $m);			if ( $m[1] && !in_array($fchar, $cmtsrt) )			{				$last_head = $m[1];			}			unset($m);			// Match variables			preg_match('#(.*?)=#is', $line, $m);			if ( $m[1] && !in_array($fchar, $cmtsrt) )			{				// Strip comments (; kind)				if ( $comment = strstr($line, ';') )				{					$line = str_replace($comment, '', $line);				}				// Strip comments (# kind)				if ( $comment = strstr($line, '#') )				{					$line = str_replace($comment, '', $line);				}				// Remove whitespace/var name						$line = str_replace($m[1] . '=', '', $line);				$line = trim(rtrim($line));				// Add to array				if ( $group == $last_head || !$group )				{					if ( $var == trim(rtrim($m[1])) )					{						// Return this var only						return $line;					}					else					{						$output[$last_head][ trim(rtrim($m[1])) ] = $line;					}				}			}		}		// Return this group only		if ( $group )		{			return $output[$group];		}		// Got the data, Return it		return $output;	}	/************************************************************************	** Function	: write() - File editor	** About	: This function writes/Updates entries in the ini file	************************************************************************/	function write($file, $section, $var, $value)	{		if ( empty ( $section ) || empty ( $var ) )		{			die("INI->Unable to write to file due to empty section or var (Section: $section -> Var: $var -> Value: $value)");		}			$contents = $this->read($file);		$contents[$section][$var] = $value;		if ( empty ( $value ) )		{			unset($contents[$section][$var]);		}		$output = ";\n; File created by Afterlife(69)'s INI Class\n; Copyright 2006, Dean Newman - Licenced under GNU/GPL v2\n;\n";		while ( list ( $key, $array ) = @each ( $contents ) )		{			if ( is_array ( $contents[$key] ) )			{				$output .= "[$key]\n";			}			while ( list ( $var, $value ) = @each ( $array ) )			{				$output .= "$var = $value\n";			}			$output .= "\n";		}				$handle = @fopen($file, 'w');		@fwrite($handle, $output);		@fclose($handle);	}}/************************************************************************** Software	: ini_file.class.php** Purpose	: Reads ini files into an array, can also read single groups or vars** Author	: Dean Newman of Project07.com** Licence	: GNU/GPL v2************************************************************************/?>