r2087 + update release notes, ChangeLog and tell the world it's RackTables 0.16.1
[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.16.1');
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 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
21 define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
22
23 function getConfigVar ($varname = '')
24 {
25 global $configCache;
26 // We assume the only point of cache init, and it is init.php. If it
27 // has failed, we don't retry loading.
28 if (!isset ($configCache))
29 {
30 showError ("Configuration cache is unavailable", __FUNCTION__);
31 die;
32 }
33 if ($varname == '')
34 {
35 showError ("Missing argument", __FUNCTION__);
36 die;
37 }
38 if (isset ($configCache[$varname]))
39 {
40 // Try casting to int, if possible.
41 if ($configCache[$varname]['vartype'] == 'uint')
42 return 0 + $configCache[$varname]['varvalue'];
43 else
44 return $configCache[$varname]['varvalue'];
45 }
46 return NULL;
47 }
48
49 // In softfail mode die only on fatal errors, letting the user check
50 // and resubmit his input.
51 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
52 {
53 global $configCache;
54 if (!isset ($configCache))
55 {
56 showError ('Configuration cache is unavailable', __FUNCTION__);
57 die;
58 }
59 if (empty ($varname))
60 {
61 showError ("Empty argument", __FUNCTION__);
62 die;
63 }
64 // We don't operate on unknown data.
65 if (!isset ($configCache[$varname]))
66 {
67 showError ("don't know how to handle '${varname}'", __FUNCTION__);
68 die;
69 }
70 if ($configCache[$varname]['is_hidden'] != 'no')
71 {
72 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
73 if ($softfail)
74 return $errormsg;
75 showError ($errormsg, __FUNCTION__);
76 die;
77 }
78 if (empty ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
79 {
80 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
81 if ($softfail)
82 return $errormsg;
83 showError ($errormsg, __FUNCTION__);
84 die;
85 }
86 if (!empty ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
87 {
88 $errormsg = "'${varname}' can accept UINT values only";
89 if ($softfail)
90 return $errormsg;
91 showError ($errormsg, __FUNCTION__);
92 die;
93 }
94 // Update cache only if the changes went into DB.
95 if (storeConfigVar ($varname, $varvalue))
96 {
97 $configCache[$varname]['varvalue'] = $varvalue;
98 if ($softfail)
99 return '';
100 }
101 elseif ($softfail)
102 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
103 }
104
105 ?>