<?php
/*
SQLCONF.PHP SQLconf v1.1
----------- ------------
Copyright (C) 2003 by Max Timchenko http://www.maxvt.com/sqlconf/
Writing and reading configuration for your scripts has never been so
easy. Using MySQL as the backend and a minimal, automatically updated
conf.php file, SQLconf provides a solution for all your configuration
needs.
* This file:
- This file implements the SQLconf.
* Functions:
- getConf(module, name="")
Returns a configuration value for a given module and variable name.
Your program is responsible for interpreting the value.
- If name is not set, all module values are read and returned as an
array of (name, value) pairs.
- If module/name does not exist, FALSE is returned.
- setConf(module, name, value, exported=0)
Sets the configuration value for a given module and variable name,
overwriting an existing value if the module/name existed.
- If exported is set to 1, the variable will be written into conf.php
file as a global define() with the name module_name (in uppercase).
- clearConf(module, name)
Removes the configuration value with the given module/name. If
exported was set to 1 for the value, conf.php will be recreated.
- regenConf()
Recreates the configuration file. This function (normally) should not
be called directly.
* License:
- This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
- To read the license please visit http://www.gnu.org/copyleft/gpl.html
*/
require_once('conf.php');
// ------------------------------------------------------------ getConf()
function getConf ($module, $name = '') {
mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS)
or die('Could not connect: '.mysql_error());
mysql_select_db(MYSQL_DB);
$query = "SELECT name, value FROM conf WHERE module='$module' ";
if ($name != '') $query .= "AND name='$name'";
$result = mysql_query($query)
or die('Query failed: '.mysql_error());
if ($name != '')
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_row($result);
return $row[1];
}
else return FALSE;
// else, return an associative array for the module
$ret = FALSE;
while ($row = mysql_fetch_row($result))
$ret[$row[0]] = $row[1];
return $ret;
}
// ------------------------------------------------------------ setConf()
function setConf ($module, $name, $value, $exported = 0) {
mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS)
or die('Could not connect: '.mysql_error());
mysql_select_db(MYSQL_DB);
$query = "REPLACE INTO conf VALUES".
"('$module', '$name', '".mysql_escape_string($value).
"', '$exported')";
$result = mysql_query($query)
or die('Query failed: '.mysql_error());
if ($exported == 1)
regenConf();
}
// ---------------------------------------------------------- clearConf()
function clearConf($module, $name) {
mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS)
or die('Could not connect: '.mysql_error());
mysql_select_db(MYSQL_DB);
// Check existence and value of exported
$query = "SELECT exported FROM conf WHERE module='$module' AND ".
"name='$name'";
$result = mysql_query($query)
or die('Query failed: '.mysql_error());
if (mysql_num_rows($result) != 1) return FALSE;
$row = mysql_fetch_row($result);
$query = "DELETE FROM conf WHERE module='$module' AND name='$name'";
$result = mysql_query($query)
or die('Query failed: '.mysql_error());
if ($row[0] == 1)
regenConf();
}
// ---------------------------------------------------------- regenConf()
function regenConf() {
mysql_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS)
or die('Could not connect: '.mysql_error());
mysql_select_db(MYSQL_DB);
$query = "SELECT * FROM conf WHERE exported=1 ORDER BY module, name";
$result = mysql_query($query)
or die('Query failed: '.mysql_error());
$f = fopen('conf.php', 'w')
or die('Can not open conf.php for writing!');
fwrite($f, "<?php // This file is automatically generated. Do not edit.\n\n");
while ($row = mysql_fetch_row($result)) {
$mod = strtoupper($row[0]);
$nam = strtoupper($row[1]);
$val = $row[2];
// write numerics directly
if (is_numeric($val))
fwrite($f, "define('$mod"."_$nam', $val);\n");
else {
// escape the single quotes
$val = str_replace('\'','\\\'',$val);
// end-of-php will break the conf.php; replace it:
$val = str_replace('?'.'>', "?'.'>", $val);
fwrite($f,"define('$mod"."_$nam', '$val');\n");
}
}
fwrite($f, '?'.'>');
fclose($f);
}
?>