Files
DomFramework/config.php

142 lines
4.8 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
/** Manage the configurations of the module done by administrator in a config
file
It is based on the module configuration defaults
POC :
$config = new config();
$config->default = array ("param"=>"default",
"param2"=>array (1,2,3),
"param3"=>null);
$var = $config->get ("param");
*/
class config
{
/** All the parameters allowed with their default value */
public $default = array ();
/** Use the .php to protect the informations */
public $confFile = "./datas/configuration.php";
/** The constructor can update the confFile variable if it is defined in a
constant */
public function __construct ()
{
if (defined("CONFIGFILE"))
$this->confFile = CONFIGFILE;
}
/** List all the parameters configurable in the software */
public function params ()
{
return $this->default;
}
/** Get the value of the provided parameter recorded in .php file
If it is not set in .php file, use the default value
@param string $param The option name to be returned */
public function get ($param)
{
if (!array_key_exists ($param, $this->default))
throw new Exception ("Unknown parameter '$param'");
if (!file_exists ($this->confFile))
{
if (@file_put_contents ($this->confFile,
"<?php\r\n\$conf = array ();\r\n")
=== FALSE)
throw new Exception (sprintf (
_("No configuration file '%s' available and it can't be created"),
$this->confFile));
}
elseif (! is_readable ($this->confFile))
throw new Exception (sprintf (
_("The configuration file '%s' is not readable"),
$this->confFile));
$conf = array ();
$rc = include ($this->confFile);
if ($rc !== 1)
throw new Exception ("Error in configuration file");
if (array_key_exists ($param, $conf))
return $conf[$param];
return $this->default[$param];
}
/** Define a value for the parameter in the config file. Add all the default
values if they are not defined
@param string $param The option name
@param mixed $value The option value */
public function set ($param, $value)
{
if (!array_key_exists ($param, $this->default))
throw new Exception ("Unknown parameter '$param'");
if (!file_exists ($this->confFile))
{
if (@file_put_contents ($this->confFile,
"<?php\r\n\$conf =array ();\r\n")
=== FALSE)
throw new Exception (sprintf (
"No configuration file '%s' available and it can't be created",
$this->confFile));
}
elseif (! is_readable ($this->confFile))
throw new Exception (sprintf (
_("The configuration file '%s' is not readable"),
$this->confFile));
if (!is_writeable ($this->confFile))
throw new Exception (sprintf (
"Configuration file '%s' is write protected",
$this->confFile));
$conf = array ();
$rc = include ($this->confFile);
if ($rc !== 1)
throw new Exception ("Error in configuration file");
$newconf = array_merge ($this->default, $conf, array ($param=>$value));
$txt = "<?php\r\n";
$txt .= "\$conf = array(\r\n";
$txt = $this->writePHP ($newconf, $txt, 4);
$txt .= ");\r\n";
if (@file_put_contents ($this->confFile, $txt, LOCK_EX) === FALSE)
throw new Exception (sprintf ("Can't save configuration file '%s'",
$this->confFile));
}
/** Display the $values in PHP format to be "require" easily
@param mixed $values Values to be recorded
@param string $phpcode Actual value of the php code
@param integer $indent Number of spaces in the indentation of config */
private function writePHP ($values, $phpcode, $indent)
{
foreach ($values as $key=>$val)
{
$phpcode .= str_pad (" ", $indent);
$phpcode .= (is_numeric ($key)) ? $key : "\"$key\"";
$phpcode .= " => ";
if (is_bool ($val))
{
if ($val === FALSE) $phpcode .= "FALSE,\r\n";
else $phpcode .= "TRUE,\r\n";
}
elseif (is_null ($val))
$phpcode .= "NULL,\r\n";
elseif (is_int ($val) || is_float ($val))
$phpcode .= "$val,\r\n";
elseif (is_string ($val))
$phpcode .= "\"$val\",\r\n";
elseif (is_array ($val))
{
$phpcode .= "array (\r\n";
$phpcode = $this->writePHP ($val, $phpcode, $indent+4);
$phpcode .= str_pad (" ", $indent);
$phpcode .= "),\r\n";
}
else
throw new Exception ("Config : missing type ".gettype ($val));
}
return $phpcode;
}
}