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