r1913 + stick with %GPASS% and %GSKIP% delimiters
[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.12');
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", __FUNCTION__);
28 die;
29 }
30 if ($varname == '')
31 {
32 showError ("Missing argument", __FUNCTION__);
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', __FUNCTION__);
54 die;
55 }
56 if (empty ($varname))
57 {
58 showError ("Empty argument", __FUNCTION__);
59 die;
60 }
61 // We don't operate on unknown data.
62 if (!isset ($configCache[$varname]))
63 {
64 showError ("don't know how to handle '${varname}'", __FUNCTION__);
65 die;
66 }
67 if ($configCache[$varname]['is_hidden'] != 'no')
68 {
69 $errormsg = "'${varname}' is a system variable and cannot be changed by user.";
70 if ($softfail)
71 return $errormsg;
72 showError ($errormsg, __FUNCTION__);
73 die;
74 }
75 if (empty ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
76 {
77 $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
78 if ($softfail)
79 return $errormsg;
80 showError ($errormsg, __FUNCTION__);
81 die;
82 }
83 if (!empty ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
84 {
85 $errormsg = "'${varname}' can accept UINT values only";
86 if ($softfail)
87 return $errormsg;
88 showError ($errormsg, __FUNCTION__);
89 die;
90 }
91 // Update cache only if the changes went into DB.
92 if (storeConfigVar ($varname, $varvalue))
93 {
94 $configCache[$varname]['varvalue'] = $varvalue;
95 if ($softfail)
96 return '';
97 }
98 elseif ($softfail)
99 return "storeConfigVar ('${varname}', '${varvalue}') failed in setConfigVar()";
100 }
101
102 ?>