Version 3.0.0
More functions, more options!
Class name: ini
- close ([ Resource link_identifier ])
- connect (String file)
- drop_key (String section, String key [, Resource link_identifier])
- drop_section (String section [, Resource link_identifier])
- get_keys(String section, [Resource link_identifier])
- get_section ([Resource link_identifier])
- key_exists (String section, String key, [Resource link_identifier])
- section_exists (String section, [Resource link_identifier])
- read (String section, String key, [Resource link_identifier])
- write (String section [, String key [, String value [, Resource Link_Identifier]]])
By : jorgen
<?php
///////////////////////////////////////////////////////////////////////////////
//
// 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
// Return: Link_Identifier (integer)
function connect($file)
{
$temp = $this->f_temp;
$this->f_temp .= $file;
$t = 0; // Set the counter var
// 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;
}
// Create the temporary file
$fp = fopen($this->f_temp, "w+");
fwrite($fp, time());
fclose($fp);
$this->f_temp = $temp;
// 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);
}
// Return: Boolean
function close($link = "")
{
$data_string = "";
// 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);
// Build the new String...
for ($i = 0; $i < sizeof($get_data_from_array); $i++)
{
$get_data_from_key = $get_data_from_array[$array_keys[$i]];
$key_array_keys = array_keys($get_data_from_key);
$data_string .= "[" . $array_keys[$i] . "]" . $this->crlf;
for ($j = 0; $j < sizeof($key_array_keys); $j++)
$data_string .= $key_array_keys[$j] . "=" . $get_data_from_key[$key_array_keys[$j]] . $this->crlf;
}
// 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);
$get_data = $this->ini_data[$link];
return(array_keys($get_data));
}
// 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 ($this->ini_data[$link][$section][$key]);
}
// 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. |
More File Manipulation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times. FREE! Go There Now!
|
|
|
|
You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |