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