Files
DomFramework/config.php

332 lines
9.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 = null;
/** Select the configuration file */
public function selectConfFile ()
{
if (defined("CONFIGFILE"))
$this->confFile = CONFIGFILE;
elseif (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 ()
{
$this->selectConfFile ();
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)
{
$this->selectConfFile ();
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))
{
if (! is_array ($conf[$param]))
return $conf[$param];
// if the configuration is an array, check if all the keys are defined
// or use the default
foreach ($this->default[$param] as $key=>$val)
{
if (! isset ($conf[$param][$key]))
$conf[$param][$key] = $val;
}
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)
{
$this->selectConfFile ();
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;
}
private function tokenRead (&$tokens, $output = array ())
{
$lasttokens = array ();
$startDefault = false;
$parenthesis = 0;
$i = 0;
while (count ($tokens) > 0 && $i < 5000)
{
$i++;
$token = array_shift ($tokens);
if (is_array ($token))
{
list($id, $text) = $token;
if ($id === T_WHITESPACE) continue;
if ($id === T_VARIABLE && $text === '$this')
{
array_push ($lasttokens, "this");
continue;
}
if (end ($lasttokens) === "this" && $id === T_OBJECT_OPERATOR)
{
array_push ($lasttokens, "->");
continue;
}
if ($lasttokens === array ("this","->") && $id === T_STRING &&
$text === "default")
{
$startDefault = true;
$lasttokens = array ();
}
if ($startDefault === false)
continue;
// Here we are in the default array
echo "TEXT=$text<br>\n";
}
else
{
if ($startDefault === true)
{
if ($token === "(")
$parenthesis++;
if ($token === ")")
{
$parenthesis--;
if ($parenthesis === 0)
return $output;
}
}
echo "TOK =$token<br>\n";
}
}
if ($i === 5000)
throw new Exception ("Too much lines in file (>5000)", 500);
return $output;
}
/** Create the HTML form to configure the software where the definitions are
done. Allow to edit an existing configuration */
public function configHTML ($values=array ())
{
$debug = 1;
if (!defined('T_ML_COMMENT'))
define('T_ML_COMMENT', T_COMMENT);
else
define('T_DOC_COMMENT', T_ML_COMMENT);
$source = file_get_contents ("models/model_configuration.php");
$tokens = token_get_all ($source);
// In debug, display the tokens for analyzis
if ($debug)
{
echo "DEBUG<br/>\n";
foreach ($tokens as $token)
{
if (is_string ($token))
echo "STRING : $token<br>\n";
else
{
list($id, $text, $line) = $token;
if ($id === 379) continue;
echo token_name ($id)."(id $id) $text [line $line]<br>\n";
}
}
}
return $this->tokenRead ($tokens);
exit;
$params = array ();
$string = "";
$start = false;
foreach ($tokens as $token)
{
if (is_array ($token))
{
list($id, $text) = $token;
// Look at the $this->default, and skip all before this token
if ($id === T_STRING && $text === "default" && $start === false)
{
// Found the start of the analysis
$start = true;
continue;
}
if ($start === false)
continue;
$string = "";
switch ($id)
{
case T_COMMENT:
case T_ML_COMMENT: // we've defined this
case T_DOC_COMMENT: // and this
// Skip the lines which don't contains phpdoc standard
if (substr ($text, 0, 3) !== "/**")
continue;
$definition = trim (substr ($text, 4, -2));
echo $definition."<br>\n";
break;
case T_CONSTANT_ENCAPSED_STRING:
$params[$text] = $definition;
break;
}
}
elseif ($start === true && substr ($string.$token, -2) === ");")
{
// Skip all the lines after the end of the $this->default definition
break;
}
else
{
$string .= $token;
}
}
print_r ($params);
exit;
$fields = $this->configRead ($this->default);
exit;
print_r ($fields);
$f = new form ();
$f->fields ($fields);
echo $f->printHTML ();
}
/** 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;
}
}