2011-06-10 21:55:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
|
|
|
* http://www.mysqldumper.net
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Ini
|
|
|
|
* @version SVN: $Rev$
|
|
|
|
* @author $Author$
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Class to handle ini-files
|
|
|
|
*
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Ini
|
|
|
|
*/
|
|
|
|
class Msd_Ini
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Data of loaded INI file.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_iniData = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filename of current INI file.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $_iniFilename = null;
|
|
|
|
|
2012-08-04 10:40:48 +00:00
|
|
|
/**
|
|
|
|
* Determines the escaping of the output.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $_escapeIniOutput = true;
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Class constructor
|
|
|
|
*
|
|
|
|
* @param array|string $options Configuration or filename of INI to load
|
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @return Msd_Ini
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct($options = array())
|
|
|
|
{
|
|
|
|
if (is_string($options)) {
|
|
|
|
$options = array(
|
|
|
|
'filename' => $options,
|
|
|
|
);
|
|
|
|
} elseif (!is_array($options)) {
|
|
|
|
$options = (array) $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['filename'])) {
|
|
|
|
$this->_iniFilename = (string) $options['filename'];
|
|
|
|
}
|
|
|
|
if ($this->_iniFilename !== null) {
|
|
|
|
$this->loadFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads an INI file.
|
|
|
|
*
|
|
|
|
* @param string $filename Name of file to load
|
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @throws Msd_Exception
|
|
|
|
*
|
2011-06-10 21:55:32 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function loadFile($filename = null)
|
|
|
|
{
|
|
|
|
if ($filename === null) {
|
|
|
|
$filename = $this->_iniFilename;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realpath($filename) === false) {
|
|
|
|
throw new Msd_Exception(
|
|
|
|
"INI file " . $filename . "doesn't exists."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$zfConfig = new Zend_Config_Ini(realpath($filename));
|
|
|
|
$this->_iniData = $zfConfig->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save to INI file.
|
|
|
|
*
|
|
|
|
* @param string $filename Name of file to save
|
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @throws Msd_Exception
|
|
|
|
*
|
|
|
|
* @return bool
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
2012-08-04 10:40:48 +00:00
|
|
|
public function saveFile($filename = null)
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
|
|
|
if ($filename === null) {
|
|
|
|
$filename = $this->_iniFilename;
|
|
|
|
}
|
|
|
|
if ($filename === null) {
|
|
|
|
throw new Msd_Exception(
|
|
|
|
'You must specify a filename to save the INI!'
|
|
|
|
);
|
|
|
|
}
|
2012-08-04 10:40:48 +00:00
|
|
|
$res = file_put_contents($filename, (string) $this);
|
|
|
|
return $res === false ? false : true;
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an array into the INI file format.
|
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @param array $array Array to convert.
|
|
|
|
* @param integer $level Current depth level in the array.
|
|
|
|
* @param string $prefix Prefix to use for var name.
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-04 10:40:48 +00:00
|
|
|
private function _arrayToIniString($array = null, $level = -1, $prefix = '')
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
|
|
|
if ($array === null) {
|
|
|
|
$array = $this->_iniData;
|
|
|
|
}
|
|
|
|
$level++;
|
|
|
|
$resultString = '';
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
2012-08-04 10:40:48 +00:00
|
|
|
if ($level == 0) {
|
|
|
|
$resultString .= '[' . $key . ']' . "\n";
|
|
|
|
$resultString .= $this->_arrayToIniString($value, $level);
|
|
|
|
} else {
|
|
|
|
$resultString .= $this->_arrayToIniString($value, $level, $key);
|
|
|
|
}
|
2011-06-10 21:55:32 +00:00
|
|
|
} else {
|
2012-08-04 10:40:48 +00:00
|
|
|
$newValue = $value;
|
|
|
|
if ($this->_escapeIniOutput) {
|
|
|
|
$newValue = "\"" . str_replace(
|
|
|
|
array('\\', '"'),
|
|
|
|
array('\\\\', '\\"'),
|
|
|
|
$value
|
|
|
|
) . "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultString .= ltrim("$prefix.$key", '.') . " = $newValue\n";
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
2012-08-04 10:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($level < 2) {
|
2011-06-10 21:55:32 +00:00
|
|
|
$resultString .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resultString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a variable from the data.
|
|
|
|
*
|
|
|
|
* @param string $key Name of variable
|
|
|
|
* @param string $section Name of section
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key, $section = null)
|
|
|
|
{
|
|
|
|
if ($section === null) {
|
|
|
|
return $this->_iniData[$key];
|
|
|
|
} else {
|
|
|
|
return $this->_iniData[$section][$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a variable
|
|
|
|
*
|
|
|
|
* @param string $key Name of variable
|
|
|
|
* @param mixed $value Value of variable
|
|
|
|
* @param string $section Section of variable
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set($key, $value, $section = null)
|
|
|
|
{
|
|
|
|
if ($section === null) {
|
|
|
|
$this->_iniData[$key] = $value;
|
|
|
|
} else {
|
|
|
|
$this->_iniData[$section][$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-04 10:40:48 +00:00
|
|
|
* Convert this class into a string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->_arrayToIniString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the INI data.
|
|
|
|
*
|
|
|
|
* @param array $iniData New INI data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setIniData($iniData)
|
|
|
|
{
|
|
|
|
$this->_iniData = $iniData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the parsed INI data.
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-04 10:40:48 +00:00
|
|
|
public function getIniData()
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
|
|
|
return $this->_iniData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-04 10:40:48 +00:00
|
|
|
* Disables the escaping of the output for the INI file.
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
2012-08-04 10:40:48 +00:00
|
|
|
* @return void
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
2012-08-04 10:40:48 +00:00
|
|
|
public function disableEscaping()
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
2012-08-04 10:40:48 +00:00
|
|
|
$this->setEscapeIniOutput(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables the escaping of the output for the INI file.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function enableEscaping()
|
|
|
|
{
|
|
|
|
$this->setEscapeIniOutput(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* En-/Disables the escaping of the output for the INI file.
|
|
|
|
*
|
|
|
|
* @param boolean $escapeIniOutput TRUE - Escaping enabled (default), FALSE - Escaping disabled
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setEscapeIniOutput($escapeIniOutput)
|
|
|
|
{
|
|
|
|
$this->_escapeIniOutput = $escapeIniOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves escaping status of the output for the INI file.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getEscapeIniOutput()
|
|
|
|
{
|
|
|
|
return $this->_escapeIniOutput;
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
|
|
|
}
|