Files
DomFramework/inifile.php
2020-09-07 14:13:56 +00:00

171 lines
5.6 KiB
PHP

<?php
/** DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
/** Manage .ini files like in php.ini
Support the sections (or not) */
class inifile
{
/** Return an array with the .ini file content
* If the sections are true, the sections are analyzed too
* This function is the same as parse_ini_file PHP internal
* @param string $file The ini file to read
* @param boolean|null $sections Manage the sections if true. False by
* default
*/
public function getFile ($file, $sections=false)
{
if (! file_exists ($file))
throw new \Exception (sprintf (dgettext ("domframework",
"File '%s' not found"), $file), 404);
if (! is_readable ($file))
throw new \Exception (sprintf (dgettext ("domframework",
"File '%s' not readable"), $file), 500);
return $this->getString (file_get_contents ($file, $sections));
}
/** Return an array with the .ini string content
* If the sections are true, the sections are analyzed too
* This function is the same as parse_ini_string PHP internal
* @param string $string The ini string to read
* @param boolean|null $sections Manage the sections if true. False by
* default
*/
public function getString ($string, $sections=false)
{
$res = parse_ini_string ($string, $sections);
// The DomFramework is PHP 5.3 compatible. I need to overwrite the bools and
// null values. The INI_SCANNER_TYPED is available as PHP 5.6.1
foreach ($res as $key=>$val)
{
if ($val === "null")
$res[$key] = null;
elseif ($val === "true")
$res[$key] = true;
elseif ($val === "false")
$res[$key] = false;
elseif (is_array ($val))
{
foreach ($val as $k=>$v)
{
if ($v === "null")
$res[$key][$k] = null;
elseif ($v === "true")
$res[$key][$k] = true;
elseif ($v === "false")
$res[$key][$k] = false;
elseif (is_numeric ($v))
$res[$key][$k] = $v + 0;
}
}
elseif (is_numeric ($val))
$res[$key] = $val + 0;
}
return $res;
}
/** Return a string containing a .ini content from the provided array.
* If the sections are true, define the first child of the array as sections
* @param array $array The array read from a .ini file
* @param boolean|null $sections Manage the sections if true. False by
* default
*/
public function setString ($array, $sections=false)
{
if (! is_array ($array))
throw new \Exception (
dgettext ("domframework",
"inifile::setString : provided data is not an array"),
500);
$content = "";
if ($sections !== false)
{
foreach ($array as $section=>$sub)
{
if (! is_array ($sub))
throw new \Exception (
dgettext ("domframework",
"inifile::setString : provided data is not an array"),
500);
$content .= "[$section]\n";
foreach ($sub as $key=>$val)
{
if (is_array ($val))
{
foreach ($val as $k=>$v)
{
if (!is_scalar ($v) && ! is_null ($v))
throw new \Exception (sprintf (
dgettext ("domframework",
"Provided value for '%s' is not scalar"),
$key), 500);
if ($v === null) $v = "null";
$content .= $key."[$k] = \"$v\"\n";
}
}
else
{
if ($val === null) $val = "null";
$content .= "$key = \"$val\"\n";
}
}
$content .= "\n";
}
}
else
{
foreach ($array as $key=>$val)
{
if (is_array ($val))
{
foreach ($val as $k=>$v)
{
if (!is_scalar ($v) && ! is_null ($v))
throw new \Exception (sprintf (
dgettext ("domframework",
"Provided value for '%s' is not scalar"),
$key), 500);
if ($v === null) $v = "null";
$content .= $key."[$k] = \"$v\"\n";
}
}
else
{
if ($val === null) $val = "null";
$content .= "$key = \"$val\"\n";
}
}
$content .= "\n";
}
return $content;
}
/** Save a file containing a .ini content from the provided array.
* Don't create the directory if it doesn't exists
* If the sections are true, define the first child of the array as sections
* @param string $file The .ini file to store
* @param array $array The array to store in file
* @param boolean|null $sections Manage the sections if true. False by
* default
*/
public function setFile ($file, $array, $sections=false)
{
$dir = basename ($file);
if (! file_exists ($dir) || ! is_readable ($dir) || ! is_writeable ($dir))
throw new \Exception (sprintf (
dgettext ("domframework",
"Directory '%s' available or not readable or not writeable"),
$dir), 500);
if (file_exists ($file) && ! is_writeable ($file))
throw new \Exception (sprintf (dgettext ("domframework",
"File '%s' is not writeable"), $file),
500);
$content = $this->setString ($array, $sections);
return file_put_contents ($file, $content);
}
}