git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2092 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
175 lines
5.9 KiB
PHP
175 lines
5.9 KiB
PHP
<?php
|
|
/** DomFramework
|
|
@package domframework
|
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
|
|
|
require_once ("domframework/form.php");
|
|
|
|
/** 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;
|
|
if (file_exists ("./datas/configuration.php"))
|
|
$this->confFile = "./datas/configuration.php";
|
|
elseif (file_exists ("./data/configuration.php"))
|
|
$this->confFile = "./data/configuration.php";
|
|
}
|
|
|
|
/** 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 (dgettext("domframework",
|
|
"No configuration file '%s' available and it can't be created"),
|
|
$this->confFile));
|
|
}
|
|
elseif (! is_readable ($this->confFile))
|
|
throw new Exception (sprintf ( dgettext("domframework",
|
|
"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
|
|
@return TRUE if the parameter is saved or an exception */
|
|
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 (
|
|
dgettext("domframework",
|
|
"The configuration file '%s' is not readable"),
|
|
$this->confFile));
|
|
if (!is_writeable ($this->confFile))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Configuration file '%s' is write protected"),
|
|
$this->confFile));
|
|
$conf = array ();
|
|
$rc = include ($this->confFile);
|
|
if ($rc !== 1)
|
|
throw new Exception (dgettext("domframework",
|
|
"Error in configuration file"), 500);
|
|
$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 (dgettext("domframework",
|
|
"Can't save configuration file '%s'"),
|
|
$this->confFile));
|
|
return TRUE;
|
|
}
|
|
|
|
/** 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 (dgettext("domframework",
|
|
"Config : missing type ").gettype ($val), 500);
|
|
}
|
|
|
|
return $phpcode;
|
|
}
|
|
|
|
/** Return an array of fields */
|
|
private function configRead ($params)
|
|
{
|
|
$fields = array ();
|
|
foreach ($params as $p1=>$vals1)
|
|
{
|
|
if (is_array ($vals1))
|
|
{
|
|
$fields = array_merge ($fields, $this->configRead ($vals1));
|
|
}
|
|
else
|
|
{
|
|
$field = new formfield ($vals1, $vals1);
|
|
$field->group = $p1;
|
|
$fields[] = $field;
|
|
unset ($field);
|
|
}
|
|
}
|
|
return $fields;
|
|
}
|
|
}
|