Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Last active May 1, 2016 15:46
Show Gist options
  • Save jesobreira/6b0727e0a55507479745 to your computer and use it in GitHub Desktop.
Save jesobreira/6b0727e0a55507479745 to your computer and use it in GitHub Desktop.
PHP AutoIt-like INI file management
<?php
/*
All Ini-related functions from AutoIt are available.
Just add the paamayim nekudotayim (::) between "Ini" and the functio.
E.g.: IniWrite becomes Ini::Write, IniReadSectionNames becomes Ini::ReadSectionNames
Docs: https://www.autoitscript.com/autoit3/docs/functions/IniWrite.htm
*/
include 'phpini.class.php';
Ini::Write('example.ini', 'test', 'random_number', rand());
echo Ini::Read('example.ini', 'test', 'random_number', 'no content');
<?php
class Ini {
public function Delete($filename, $section, $key = false) {
$parse = Ini::readINIfile($filename);
if($key) {
unset($parse[$section][$key]);
} else {
unset($parse[$section]);
}
Ini::write_ini_file($parse, $filename);
}
public function Read($filename, $section, $key, $default) {
$parse = Ini::readINIfile($filename);
if(isset($parse[$section]) AND isset($parse[$section][$key])) {
return $parse[$section][$key];
} else {
return $default;
}
}
public function ReadSection($filename, $section, $au3like = false) {
$parse = Ini::readINIfile($filename);
if(isset($parse[$section])) {
if($au3like) { //0 => key, 1 => value
$return = array();
$return[0] = array( 0 => sizeof($parse[$section]) );
foreach($parse[$section] as $key=>$value) {
$return[] = array(
0 => $key,
1 => $value
);
}
return $return;
} else { // key=>value
return $parse[$section];
}
} else {
return false;
}
}
public function ReadSectionNames($filename) {
$parse = Ini::readINIfile($filename);
if($parse) {
$return = array();
foreach($parse as $key=>$value) {
$return[] = $key;
}
return $return;
} else {
return array();
}
}
public function RenameSection($filename, $section, $newsection, $flag = 0) {
$parse = Ini::readINIfile($filename);
if(!$parse) return false;
if(array_key_exists($newsection, $parse)) {
if($flag==0) { // Fail if "new section" already exist
return false;
} else { // Overwrite "new section"
unset($parse[$newsection]);
}
}
$parse[$newsection] = $parse[$section];
unset($parse[$section]);
return true;
}
public function Write($filename, $section, $key, $value) {
if(!file_exists($filename)) file_put_contents($filename, null);
if(is_writable($filename)) {
$parse = Ini::readINIfile($filename);
if(isset($parse[$section])) {
$parse[$section][$key] = $value;
} else {
$parse[$section] = array(
$key => $value
);
}
Ini::write_ini_file($parse, $filename);
} else {
return false;
}
}
public function WriteSection($filename, $section, $data, $index = 1) {
$parse = Ini::readINIfile($filename);
if(!$parse) return false;
if(is_string($data)) {
$arr = array();
parse_str($data, $arr);
} else $arr = $section;
}
// =========== internal use only ===========
private function write_ini_file($assoc_arr, $path, $has_sections=TRUE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]\r\n";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = \"".$elem2[$i]."\"\r\n";
}
}
else if($elem2=="") $content .= $key2." = \r\n";
else $content .= $key2."=".$elem2."\r\n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key."[] = \"".$elem[$i]."\"\r\n";
}
}
else if($elem=="") $content .= $key."=\r\n";
else $content .= $key."=".$elem."\r\n";
}
}
return Ini::safefilerewrite($path, $content);
}
private function safefilerewrite($fileName, $dataToSave) {
$dataToSave = trim($dataToSave);
if ($fp = fopen($fileName, 'w'))
{
$startTime = microtime(TRUE);
do
{ $canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));
//file was locked so now we can store information
if ($canWrite)
{
$write = fwrite($fp, $dataToSave);
flock($fp, LOCK_UN);
}
fclose($fp);
return $write;
} else {
return false;
}
}
private function readINIfile($filename) {
if(!file_exists($filename) || !is_readable($filename)) return false;
$array1 = file($filename);
$array2 = array();
if(!sizeof($array1)) return array();
$section = '';
for ($line_num = 0; $line_num <= sizeof($array1); $line_num++) {
$filedata = @$array1[$line_num];
$dataline = trim($filedata);
$firstchar = substr($dataline, 0, 1);
if ($firstchar!=';' && $firstchar!='#' && $dataline!='') {
//It's an entry (not a comment and not a blank line)
if ($firstchar == '[' && substr($dataline, -1, 1) == ']') {
//It's a section
$section = strtolower(substr($dataline, 1, -1));
}else{
//It's a key...
$delimiter = strpos($dataline, '=');
if ($delimiter > 0) {
//...with a value
$key = strtolower(trim(substr($dataline, 0, $delimiter)));
$array2[$section][$key] = '';
$value = trim(substr($dataline, $delimiter + 1));
while (substr($value, -1, 1) == '\\') {
//...value continues on the next line
$value = substr($value, 0, strlen($value)-1);
$array2[$section][$key] .= stripcslashes($value);
$line_num++;
$value = trim($array1[$line_num]);
}
$array2[$section][$key] .= stripcslashes($value);
$array2[$section][$key] = trim($array2[$section][$key]);
if (substr($array2[$section][$key], 0, 1) == '"' && substr($array2[$section][$key], -1, 1) == '"') {
$array2[$section][$key] = substr($array2[$section][$key], 1, -1);
}
}else{
//...without a value
$array2[$section][strtolower(trim($dataline))]='';
}
}
}else{
//It's a comment or blank line. Ignore.
}
}
return $array2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment