r3278 pull the last maintenance commit into trunk
[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.9');
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 '0.17.5' => 1322,
26 '0.17.6' => 1326,
27 '0.17.7' => 1326,
28 '0.17.8' => 1334,
29 );
30
31 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
32 define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
33 // The latter matches both SunOS and Linux-styled formats.
34 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');
35 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');
36 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');
37 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');
38 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');
39 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');
40 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');
41 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');
42 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');
43
44 function loadConfigDefaults() {
45 global $configCache;
46 $configCache = loadConfigCache();
47 if (!count ($configCache)) {
48 throw new RuntimeException('Failed to load configuration from the database.');
49 }
50 foreach ($configCache as $varname => &$row) {
51 $row['is_altered'] = 'no';
52 if ($row['vartype'] == 'uint') $row['varvalue'] = 0 + $row['varvalue'];
53 $row['defaultvalue'] = $row['varvalue'];
54 }
55 }
56
57 function alterConfigWithUserPreferences() {
58 global $configCache;
59 global $userConfigCache;
60 global $remote_username;
61 $userConfigCache = loadUserConfigCache($remote_username);
62 foreach($userConfigCache as $key => $row) {
63 if ($configCache[$key]['is_userdefined'] == 'yes') {
64 $configCache[$key]['varvalue'] = $row['varvalue'];
65 $configCache[$key]['is_altered'] = 'yes';
66 }
67 }
68 }
69
70 // Returns true if varname has a different value or varname is new
71 function isConfigVarChanged($varname, $varvalue) {
72 global $configCache;
73 if (!isset ($configCache))
74 throw new RuntimeException ("Configuration cache is unavailable");
75 if ($varname == '')
76 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
77 if (!isset ($configCache[$varname])) return true;
78 if ($configCache[$varname]['vartype'] == 'uint')
79 return $configCache[$varname]['varvalue'] !== 0 + $varvalue;
80 else
81 return $configCache[$varname]['varvalue'] !== $varvalue;
82 }
83
84 define ('VLAN_MIN_ID', 1);
85 define ('VLAN_MAX_ID', 4094);
86 define ('VLAN_DFL_ID', 1);
87
88 function getConfigVar ($varname = '')
89 {
90 global $configCache;
91 // We assume the only point of cache init, and it is init.php. If it
92 // has failed, we don't retry loading.
93 if (!isset ($configCache))
94 {
95 throw new RuntimeException ("Configuration cache is unavailable");
96 }
97 if ($varname == '')
98 {
99 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
100 }
101 if (isset ($configCache[$varname]))
102 {
103 // Try casting to int, if possible.
104 return $configCache[$varname]['varvalue'];
105 }
106 return NULL;
107 }
108
109 // In softfail mode die only on fatal errors, letting the user check
110 // and resubmit his input.
111 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
112 {
113 global $configCache;
114 if (!isset ($configCache))
115 throw new RuntimeException ("Configuration cache is unavailable");
116 if (!strlen ($varname))
117 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
118 // We don't operate on unknown data.
119 if (!isset ($configCache[$varname]))
120 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
121 if ($configCache[$varname]['is_hidden'] != 'no')
122 throw new InvalidArgException('$varname', $varname, "'${varname}' is a system variable and cannot be changed by user.");
123 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
124 throw new InvalidArgException('$varname', $varname, "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.");
125 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
126 throw new InvalidArgException('$varname', $varname, "'${varname}' can accept UINT values only");
127 // Update cache only if the changes went into DB.
128 storeConfigVar ($varname, $varvalue);
129 $configCache[$varname]['varvalue'] = $varvalue;
130 }
131
132 function setUserConfigVar ($varname = '', $varvalue = '')
133 {
134 global $configCache;
135 global $remote_username;
136 if (!isset ($configCache))
137 throw new RuntimeException ("Configuration cache is unavailable");
138 if (!strlen ($varname))
139 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
140 // We don't operate on unknown data.
141 if (!isset ($configCache[$varname]))
142 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
143 if ($configCache[$varname]['is_userdefined'] != 'yes')
144 throw new InvalidArgException('$varname', $varname, "'${varname}' cannot be changed by user.");
145 if ($configCache[$varname]['is_hidden'] != 'no')
146 throw new InvalidArgException('$varname', $varname, "'${varname}' is a system variable and cannot be changed by user.");
147 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
148 throw new InvalidArgException('$varname', $varname, "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.");
149 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
150 throw new InvalidArgException('$varname', $varname, "'${varname}' can accept UINT values only");
151 // Update cache only if the changes went into DB.
152 storeUserConfigVar ($remote_username, $varname, $varvalue);
153 $configCache[$varname]['varvalue'] = $varvalue;
154 }
155
156 function resetUserConfigVar ($varname = '')
157 {
158 global $configCache;
159 global $remote_username;
160 if (!isset ($configCache))
161 throw new RuntimeException ("Configuration cache is unavailable");
162 if (!strlen ($varname))
163 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
164 // We don't operate on unknown data.
165 if (!isset ($configCache[$varname]))
166 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
167 if ($configCache[$varname]['is_userdefined'] != 'yes')
168 throw new InvalidArgException('$varname', $varname, "'${varname}' cannot be changed by user.");
169 if ($configCache[$varname]['is_hidden'] != 'no')
170 throw new InvalidArgException('$varname', $varname, "'${varname}' is a system variable and cannot be changed by user.");
171 // Update cache only if the changes went into DB.
172 deleteUserConfigVar ($remote_username, $varname);
173 }
174
175 ?>