/////////////////////////////////////////////////////////////////////////////// // // INI-functions // Jorgen Horstink // // Class for reading and writing to INI files // (c) Copyright 2002 Next Avenue, The Netherlands // // All rights reserved. // ///////////////////////////////////////////////////////////////////////////////
error_reporting(E_ALL);
class ini { var $ini_data = array (); // Array to store all ini data var $ini_data_files = array (); // Array to store all file names var $crlf = "\n"; // "Carriage return", SEE function connect var $f_temp = "tempA9B4E3F4C9_"; // The temporary file, SEE function connect var $set_waiting_limit = 10; // The maximum waiting time in seconds, SEE function connect
// While another file is bussy... wait wait wait while (@is_array(file($this->f_temp))) { $t++;
// IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT // // If someone does not use the function Close, the temporary file will not be deleted. // But if you open the file for the second time, the function Connect notices that // the temporary file already exists. So the functions 'thinks' some other file is busy. But // that is not true, the file has not been deleted. // To solve this problem, the function writes the timestamp in the temporary file. When the // function notices that the file already exists, it checks whether or not the last opened // timestamp is too old. If the timestamp is too old, the function notices that the function // Close probably is not used. So the function deletes the temporary file and goes on... // // IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
// Get the time out of the file, when the last time the script is parsed $time = file($this->f_temp); // If the time is too old, delete the file and break the while-statement if ((time() - $this->set_waiting_limit) > $time[0]) { unlink($this->f_temp); break; }
sleep(1); // Check every second... if ($t > $this->set_waiting_limit) break; }
// If the file does not exist, return -1 if (!file_exists($file)) return (-1);
// Check whether or not there already is a connection with the selected file for ($i = 0; $i < sizeof($this->ini_data_files); $i++) if ($this->ini_data_files[$i] == $file) return ($i);
// Get all ini data and store it in an Array $ini_array = parse_ini_file($file, TRUE); // Add the Array to ini_data[] $this->ini_data[] = $ini_array; // Store the filename in $ini_data_files[] $this->ini_data_files[] = $file;
// Return the new Link_Identifier return (sizeof($this->ini_data) - 1); }
// If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
if (sizeof($this->ini_data) == 0) return (FALSE);
// Get all ini data corresponding to the Link_Identifier $get_data_from_array = $this->ini_data[$link]; $array_keys = array_keys($get_data_from_array);
// Get the filename $filename = $this->ini_data_files[$link]; // If the file does not exist, return FALSE if (!file_exists($filename)) return (FALSE);
@unlink($this->f_temp . $filename);
// Try to write all data into the file $fp = fopen($filename, "w+"); fwrite($fp, $data_string); fclose($fp);
// Delete the old data unset($this->ini_data[$link]);
// Delete the old filename unset($this->ini_data_files[$link]);
return (TRUE); }
// Return: Boolean function drop_key($section, $key, $link) { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// If the section does not exist, return FALSE if (!$this->section_exists($section)) return (FALSE);
// If the key does not exist, return FALSE if (!$this->key_exists($section, $key)) return (FALSE);
// Unset the key in the chosen section unset($this->ini_data[$link][$section][$key]); return (TRUE); }
// Return: Boolean function drop_section($section, $link) { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// If the section does not exist, return FALSE if (!$this->section_exists($section)) return (FALSE);
// Unset the section unset($this->ini_data[$link][$section]); return (TRUE); }
// Return: Array function get_keys($section, $link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// If the section does not exist, return FALSE if (!$this->section_exists($section, $link)) return (FALSE);
// Get all keys and return them $get_data = $this->ini_data[$link][$section];
return(array_keys($get_data)); }
// Return: Array function get_sections($link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// Return: Boolean function key_exists($section, $key, $link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// If the section does not exist, return FALSE if (!$this->section_exists($section, $link)) return (FALSE);
// Get all keys $keys = $this->get_keys($section, $link);
for ($i = 0; $i < sizeof($keys); $i++) if ($keys[$i] == $key) return (TRUE);
// If no section is found, return FALSE return (FALSE); }
// Return: Boolean function section_exists($section, $link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// Get all sections $sections = $this->get_sections($link);
// Check whether or not the section exists for ($i = 0; $i < sizeof($sections); $i++) if ($sections[$i] == $section) return (TRUE);
// If no section is found, return FALSE return (FALSE); }
// Return: String function read($section, $key, $link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
// If the section does not exist, return FALSE if (!$this->section_exists($section, $link)) return (FALSE);
// If the key does not exist, return FALSE if (!$this->key_exists($section, $key, $link)) return (FALSE);
// Return: Boolean function write($section, $key = "", $value = "", $link = "") { // If the parameter $link is empty, get the last Link_Identifier if (empty($link)) $link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE if (sizeof($this->ini_data) == 0) return (FALSE);
if (empty($section)) return (FALSE);
// If the section does not exist, make a new section if (!$this->section_exists($section)) $this->ini_data[$link][$section] = array ();
// If the key is not empty... if (!empty($key)) $this->ini_data[$link][$section][$key] = $value;
return (TRUE); }
}
// Make a new instance of the Class... $ini = new ini;
?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.