#native_company# #native_desc#
#native_cta#

INI Files

By Jorgen Horstink
on August 9, 2002

Version: 3.0

Type: Function

Category: File Management

License: Other

Description: Don’t have a MySQL database? Wanna store some information? Well, than you need my functions. This are very usefull functions. As my title says, it are functions for reading and writing INI-files. I have written 11 functions for handling INI-files.
All functions are very simple, and it is possible to access several INI-files at the same time!

Functionlist:
– ini_close ( [ resource link_identifier ] )
– ini_connect ( string file [ , resource link_identifier ] )
– ini_drop_section ( string section , [ resource link_identifier ] )
– ini_drop_key ( string section , string key [ , resource link_identifier ] )
– ini_error ( )
– ini_get_sections ( [ resource link_identifier ] )
– ini_get_keys ( string section [ , resource link_identifier ] )
– ini_read ( string section , string key [ , resource link_identifier ] )
– ini_report_error ( string error )
– ini_section_exists ( string section [ , resource link_identifier ] )
– ini_key_exists ( string section , string key [ , resource link_identifier ] )
– ini_key_position ( string section , string key [ , resource link_identifier ] )
– ini_write ( string section [ , string key , [ string value , [ resource link_identifier ]]] )

<?php

// All variables for the error reporting
$E_FILE_NOT_EXISTS = "Het opgegeven bestand bestaat niet.n";
$E_STREAM_EXISTS = "Er is al een verbinding met het opgegeven bestand.n";
$E_NO_STREAM = "De opgegeven link_identifier bestaat niet.n";

$E_NO_SECTION = "De opgegeven sectie bestaat niet.n";
$E_NO_KEY = "De opgegeven sleutel bestaat niet.n";

/**
 * @return bool
 * @param link_identifier = "" resource
 * @desc Close a stream to an ini-file, all data will be saved
 */
function ini_close($link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	// Get all data stream
	$get_all_data = $GLOBALS['ini_stream'];
	// Get all filenames corresponding to the stream
	$get_all_names = $GLOBALS['ini_name_stream'];

	if (is_array($get_all_data))
		$get_data = $get_all_data[$link_identifier];
	if (is_array($get_all_names))
		$get_name = $get_all_names[$link_identifier];

	// Unset the data of the stream
	$left_part = array_slice($get_all_data, 0, $link_identifier);
	$right_part = array_slice($get_all_data, $link_identifier + 1, count($get_all_data));
	$merged = array_merge($left_part, $right_part);
	$GLOBALS['ini_stream'] = $merged;

	$left_names = array_slice($get_all_names, 0, $link_identifier);
	$right_names = array_slice($get_all_names, $link_identifier + 1, count($get_all_names));
	$merged_names = array_merge($left_names, $right_names);
	$GLOBALS['ini_name_stream'] = $merged_names;


	if (!empty($get_name))
	{
		// Prepare the data for writing into the file
		for ($i = 0; $i < count($get_data); $i++)
		{
			if ($i != count($get_data) - 1)
				$add_enter = chr(13).chr(10);
			else
				unset($add_enter);

			$data .= $get_data[$i] . $add_enter;
		}

		// Write the data into the file
		$fp = fopen($get_name, "w+");
			fwrite($fp, $data);
		fclose($fp);
		// Report no error and return true; connection succesfull closed
		ini_report_error("");
		return(true);
	} else {
		// Report the error and return false; connection unsuccesfull closed
		ini_report_error($GLOBALS["E_NO_STREAM"]);
		return(false);
	}
}


/**
 * @return resource
 * @param file string
 * @desc Connect to an ini-file
 */
function ini_connect($file)
{
	// Check whether or not the file does exist...
	if (file_exists($file))
	{
		// Open the file for reading
		$fp = fopen($file, "r+");
			$content = fread($fp, filesize($file));
		fclose($fp);

		// Make an array...
		$ar_lines = explode(chr(13).chr(10), $content);

		// Check whether or not $ini_stream is already an array
		if (!is_array($GLOBALS['ini_stream']))
			$GLOBALS['ini_stream'] = array ();
		if (!is_array($GLOBALS['ini_name_stream']))
			$GLOBALS['ini_name_stream'] = array ();

		// Check whether or not there already is an stream to the chosen file
		for ($i = 0; $i < count($GLOBALS['ini_name_stream']); $i++)
		{
			$stream_names = $GLOBALS['ini_name_stream'];
			if ($stream_names[$i] == $file)
			{
				// Report the error: the chosen stream already exists. Return the existing stream
				ini_report_error($GLOBALS['E_STREAM_EXISTS']);
				return ($i);
			}
		}
		// Add in $ini_stream a new 'stream' with all data...
		array_push($GLOBALS['ini_stream'], $ar_lines);
		// Add in $ini_name_stream a new 'stream' with the filename
		array_push($GLOBALS['ini_name_stream'], $file);

		// Report no error
		ini_report_error("");

		// Return the resource link_identifier
		return (count($GLOBALS['ini_stream']) - 1);
	} else {
		// Report the error an return false
		ini_report_error($GLOBALS['E_FILE_NOT_EXISTS']);
		return (false);
	}
}

/**
 * @return bool
 * @param section string
 * @param link_identifier = "" resource
 * @desc Drops a complete section, including all keys
 */
function ini_drop_section($section, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	// Get all stream corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	$ar_data = $ar_all_streams[$link_identifier];

	// Check whether or not the section exists
	if (ini_section_exists($section, $link_identifier))
	{
		$place_section = array_search("[".$section."]", $ar_data);
		// Start on the next line, else the script will thing it has found the new section
		$t = $place_section + 1;
		while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
		{
			$t++;
		}

		// Drop the chosen section...
		$left_part = array_slice($ar_data, 0, $place_section);
		$right_part = array_slice($ar_data, $t, count($ar_data));
		$ar_new = array_merge($left_part, $right_part);

		$ar_all_streams[$link_identifier] = $ar_new;
		$GLOBALS['ini_stream'] = $ar_all_streams;

		ini_report_error("");
		return (true);
	} else {
		// Report the error no section found and return false...
		ini_report_error($GLOBALS['E_NO_SECTION']);
		return (false);
	}
}

/**
 * @return bool
 * @param section string
 * @param key string
 * @param link_identifier = "" resource
 * @desc Drops a key, if it exists, in the chosen section
 */
function ini_drop_key($section, $key, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	$key_found = false;

	// Get all stream data corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	if(is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];

	if (is_array($ar_data))
	{
		// Check whether or not the section exists
		if (ini_section_exists($section, $link_identifier))
		{
			$place_section = array_search("[".$section."]", $ar_data);
			// Start on the next line, else the script will thing it has found the new section
			$t = $place_section + 1;
			while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
			{
				// Check whether or not the line contains an =
				$pos = strpos($ar_data[$t], "=");
				if ($pos)
					$get_key = trim(substr($ar_data[$t], 0, $pos));

				// Check whether or not the key is found in the section
				if ($get_key == trim($key))
				{
					$key_found = true;
					$key_id = $t;
					break;
				}
				$t++;
			}

			if ($key_found == true)
			{
				$ar_left = array_slice($ar_data, 0, $key_id);
				$ar_right = array_slice($ar_data, $key_id + 1, count($ar_data));
				$ar_new = array_merge($ar_left, $ar_right);

				$ar_all_streams[$link_identifier] = $ar_new;
				$GLOBALS['ini_stream'] = $ar_all_streams;

				ini_report_error("");
				return (true);
			}
		} else {
			ini_report_error($GLOBALS['E_NO_SECTION']);
			return (false);
		}
	} else {
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return (false);
	}

	ini_report_error("");
	return (false);
}

/**
 * @return string
 * @desc Get the last error description
 */
function ini_error()
{
	// Return the last error
	return ($GLOBALS['ini_last_error']);
}

/**
 * @return array
 * @param link_identifier = "" resource
 * @desc Get all sections corresponding to the link_identifier
 */
function ini_get_sections($link_identifier = "")
{
	// Check whether or not the link_identifier parameter is set
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	$ar_all_streams = $GLOBALS['ini_stream'];
	if (is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];
		
	$ar_return = array();
	if (is_array($ar_data))
	{
		for ($i = 0; $i < count($ar_data); $i++)
		{
			if (substr($ar_data[$i], 0, 1) == "[")
			{
				$ar_data[$i] = str_replace("[", "", $ar_data[$i]);
				$ar_data[$i] = str_replace("]", "", $ar_data[$i]);
				array_push($ar_return, $ar_data[$i]);	
			}
		}	
	}
	
	return ($ar_return);
}

/**
 * @return array
 * @param section string
 * @param link_identifier = "" resource
 * @desc Get all keys corresponding to the section and link_identifier
 */
function ini_get_keys($section, $link_identifier = "")
{
	// Check whether or not the link_identifier parameter is set
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;
		
	$ar_all_streams = $GLOBALS['ini_stream'];
	if (is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];
		
	$ar_return = array();	
	if (is_array($ar_data) && ini_section_exists($section, $link_identifier))
	{
		$begin = array_search("[".$section."]", $ar_data);
		$t = $begin + 1;
		while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
		{
			if (substr($ar_data[$t], 0, 1) != ";")
			{
				$pos = strpos($ar_data[$t], "=");
				if ($pos)
				{
					$get_key = trim(substr($ar_data[$t], 0, $pos));
					array_push($ar_return, $get_key);
				}	
			}
			$t++;	
		}
	}
	return ($ar_return);
}

/**
 * @return unknown
 * @param section string
 * @param key string
 * @param link_identifier = "" resource
 * @desc Read a key from the chosen section
 */
function ini_read($section, $key, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	if (empty($key))
	{
		ini_report_error($GLOBALS['E_NO_KEY']);
		return ("");
	}

	// Get all stream data corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	if(is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];

	if (is_array($ar_data))
	{
		// Check whether or not the section exists
		if (ini_section_exists($section, $link_identifier))
		{
			if (ini_key_exists($section, $key))
			{
				$place_section = array_search("[".$section."]", $ar_data);
				// Start on the next line, else the script will thing it has found the new section
				$t = $place_section + 1;
				while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
				{
					// Check whether or not the line contains an =
					$pos = strpos($ar_data[$t], "=");
					if ($pos)
						$get_key = trim(substr($ar_data[$t], 0, $pos));

					// Check whether or not the key is found in the section
					if ($get_key == trim($key))
					{
						// Get the value of the key and return it
						$value = trim(substr($ar_data[$t], $pos + 1, strlen($ar_data[$t])));
						ini_report_error("");
						return ($value);
					}
					$t++;
				}
			} else {
				ini_report_error($GLOBALS['E_NO_KEY']);
				return("");
			}
		} else {
			ini_report_error($GLOBALS['E_NO_SECTION']);
			return ("");
		}
	} else {
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return ("");
	}

	ini_report_error("");
	return ("");
}

/**
 * @return void
 * @param error string
 * @desc Report an error
 */
function ini_report_error($error)
{
	// Report the last error
	$GLOBALS['ini_last_error'] = $error;
}

/**
 * @return bool
 * @param section string
 * @param link_identifier = "" resource link_identifier
 * @desc Check whether or not the section exists
 */
function ini_section_exists($section, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	// Retrieve all data corresponding to the link_identifier
	$ar_all_data = $GLOBALS['ini_stream'];
	if (is_array($ar_all_data))
		$ar_data = $ar_all_data[$link_identifier];

	// Retrieve tre filename corresponding to the link_identifier
	$ar_all_names = $GLOBALS['ini_name_stream'];
	if (is_array($ar_all_names))
		$name = $ar_all_names[$link_identifier];

	// if the connection is valid...
	if (!empty($name))
	{
		$search_result = array_search("[".$section."]", $ar_data);
		ini_report_error("");
		// Check whether the section have been found or not 
		if (is_int($search_result))
			return (true);
		else
			return (false);
	} else {
		// The connection is not valid, so report the error and return false
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return (false);
	}
}

/**
 * @return bool
 * @param section string
 * @param key string
 * @param link_identifier = "" resource
 * @desc Checks whether or not the chosen key exist
 */
function ini_key_exists($section, $key, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	$key_found = false;

	// Get all stream data corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	if(is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];

	if (is_array($ar_data))
	{
		// Check whether or not the section exists
		if (ini_section_exists($section, $link_identifier))
		{
			$place_section = array_search("[".$section."]", $ar_data);
			// Start on the next line, else the script will thing it has found the new section
			$t = $place_section + 1;
			while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
			{
				// Check whether or not the line contains an =
				$pos = strpos($ar_data[$t], "=");
				if ($pos)
					$get_key = trim(substr($ar_data[$t], 0, $pos));

				// Check whether or not the key is found in the section
				if ($get_key == trim($key))
				{
					ini_report_error("");
					return (true);
					break;
				}
				$t++;
			}
		} else {
			ini_report_error($GLOBALS['E_NO_SECTION']);
			return (false);
		}
	} else {
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return (false);
	}
}

/**
 * @return int
 * @param section string
 * @param key string
 * @param link_identifier = "" resource
 * @desc Gets the position of the key
 */
function ini_key_position($section, $key, $link_identifier = "")
{
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	$key_found = false;

	// Get all stream data corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	if(is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];

	if (is_array($ar_data))
	{
		// Check whether or not the section exists
		if (ini_section_exists($section, $link_identifier))
		{
			$place_section = array_search("[".$section."]", $ar_data);
			// Start on the next line, else the script will thing it has found the new section
			$t = $place_section + 1;
			while ($t < count($ar_data) && substr($ar_data[$t], 0, 1) != "[")
			{
				// Check whether or not the line contains an =
				$pos = strpos($ar_data[$t], "=");
				if ($pos)
					$get_key = trim(substr($ar_data[$t], 0, $pos));

				// Check whether or not the key is found in the section
				if ($get_key == trim($key))
				{
					ini_report_error("");
					return ($t);
					break;
				}
				$t++;
			}
		} else {
			ini_report_error($GLOBALS['E_NO_SECTION']);
			return (-1);
		}
	} else {
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return (-1);
	}
}

/**
 * @return bool
 * @param section string
 * @param key = "" string
 * @param value = "" string 
 * @param link_identifier = "" resource
 * @desc Write information into the stream
 */
function ini_write($section, $key = "", $value = "", $link_identifier = "")
{
	// Check whether or not the link parameter is set, if not, get the last stream
	if (!is_int($link_identifier))
		$link_identifier = count($GLOBALS['ini_stream']) - 1;

	if (empty ($section))
		return ($GLOBALS['E_NO_SECTION']);	
		
	// Get all stream data corresponding to the link_identifier
	$ar_all_streams = $GLOBALS['ini_stream'];
	if(is_array($ar_all_streams))
		$ar_data = $ar_all_streams[$link_identifier];

	// Checks whether or not the chosen link_identifier is valid...
	if (is_array($ar_data))
	{
		// Checks whether or not the section already exists...
		if(ini_section_exists($section, $link_identifier) == false)
		{
			array_push($ar_all_streams[$link_identifier], "[".$section."]");
			$GLOBALS['ini_stream'] = $ar_all_streams;
			
			// Reload all data since a new section is added
			$ar_all_streams = $GLOBALS['ini_stream'];
			if(is_array($ar_all_streams))
				$ar_data = $ar_all_streams[$link_identifier];
		}
		
		if (!empty($key))
		{
		// If the key does not already exist: add a new one
		if (ini_key_exists($section, $key, $link_identifier) == false)
		{
			$section_place = array_search("[".$section."]", $ar_data);
			
			$left_part = array_slice($ar_data, 0, $section_place + 1);
			$mid_part = $key . "=" . $value;
			$right_part = array_slice($ar_data, $section_place + 1, count($ar_data));
			$tot = array_merge($left_part, $mid_part);
			if (is_array($right_part) && count($right_part) != 0)
				$tot = array_merge($tot, $right_part);
				
			$ar_all_streams[$link_identifier] = $tot;
			$GLOBALS['ini_stream'] = $ar_all_streams;
			ini_report_error("");
			return (true);
		} // If the key does already exist: update the old one
		elseif (ini_key_exists($section, $key, $link_identifier) == true) {
			// Retrieve the position of the chosen key
			$pos_key = ini_key_position($section, $key, $link_identifier);
			// Check whether or not the key contains an = mark
			$get_pos = strpos($ar_data[$pos_key], "=");
			if ($get_pos)
			{
				// Update the key, save all data and return true
				$left_part_key = substr($ar_data[$pos_key], 0, $get_pos + 1);
				$tot_key = $left_part_key . $value;
				$ar_data[$pos_key] = $tot_key;
				$ar_all_streams[$link_identifier] = $ar_data;
				$GLOBALS['ini_stream'] = $ar_all_streams;
				ini_report_error("");
				return (true);
			}
		}
		}
	}
	else {
		ini_report_error($GLOBALS['E_NO_STREAM']);
		return (false);
	}
}

?>