r1346 + last changes before RackTables 0.14.7 release
[racktables] / inc / config.php
1 <?php
2 /*
3 *
4 * This file used to hold a collection of constants, variables and arrays,
5 * which drived the way misc RackTables functions performed. Now most of
6 * them have gone into the database, and there is a user interface
7 * for changing them. This file now provides a couple of functions to
8 * access the new config storage.
9 *
10 */
11
12
13 // Current code version is subject to change with each new release.
14 define ('CODE_VERSION', '0.14.7');
15
16 // The name of hash used to store account password hashes
17 // in the database. I think, we are happy with this one forever.
18 define ('PASSWORD_HASH', 'sha1');
19
20 function getConfigVar ($varname = '')
21 {
22 global $configCache;
23 // We assume the only point of cache init, and it is init.php. If it
24 // has failed, we don't retry loading.
25 if (!isset ($configCache))
26 {
27 showError ("Configuration cache is unavailable in getConfigVar()");
28 die;
29 }
30 if ($varname == '')
31 {
32 showError ("Missing argument to getConfigVar()");
33 die;
34 }
35 if (isset ($configCache[$varname]))
36 {
37 // Try casting to int, if possible.
38 if ($configCache[$varname]['vartype'] == 'uint')
39 return 0 + $configCache[$varname]['varvalue'];
40 else
41 return $configCache[$varname]['varvalue'];
42 }
43 return NULL;
44 }
45
46 // In softfail mode die only on fatal errors, letting the user check
47 // and resubmit his input.
48 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
49 {
50 global $configCache;
51 if (!isset ($configCache))
52 {
53 showError ('Configuration cache is unavailable in setConfigVar()');
54 die;
55 }
56 if (empty ($varname))
57 {
58 showError ("Empty argument to setConfigVar()");
59 die;
60 }
61 // We don't operate on unknown data.
62 if (!isset ($configCache[$varname]))
63 {
64 showError ("setConfigVar() doesn't know how to handle '${varname}'");
65 die;
66 }
67 if (empty ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
68 {
69 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
70 if ($softfail)
71 return $errormsg;
72 showError ($errormsg);
73 die;
74 }
75 if (!empty ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
76 {
77 $errormsg = "'${varname}' can accept UINT values only";
78 if ($softfail)
79 return $errormsg;
80 showError ($errormsg);
81 die;
82 }
83 // Update cache only if the changes went into DB.
84 if (storeConfigVar ($varname, $varvalue))
85 {
86 $configCache[$varname]['varvalue'] = $varvalue;
87 if ($softfail)
88 return '';
89 }
90 elseif ($softfail)
91 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
92 }
93
94 ?>