r3278 pull the last maintenance commit into trunk
[racktables] / inc / config.php
CommitLineData
b325120a 1<?php
e673ee24 2/*
9c0b0016
DO
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
201056a2 6 * them have gone into the database, and there is a user interface
9c0b0016
DO
7 * for changing them. This file now provides a couple of functions to
8 * access the new config storage.
9 *
8d07e3e0
DO
10 */
11
8d07e3e0 12
9c0b0016 13// Current code version is subject to change with each new release.
fa9c9050 14define ('CODE_VERSION', '0.17.9');
7fa7047a
DO
15define ('CHAP_OBJTYPE', 1);
16define ('CHAP_PORTTYPE', 2);
e673ee24 17
ca3d68bd
DO
18$max_dict_key = array
19(
20 '0.17.0' => 988,
21 '0.17.1' => 988,
22 '0.17.2' => 1150,
70992855 23 '0.17.3' => 1150,
681f2138 24 '0.17.4' => 1150,
2803fbe6 25 '0.17.5' => 1322,
63811a09 26 '0.17.6' => 1326,
026a79ee 27 '0.17.7' => 1326,
ad9da675 28 '0.17.8' => 1334,
ca3d68bd
DO
29);
30
23818dde
DO
31define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
32define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
05771508
DO
33// The latter matches both SunOS and Linux-styled formats.
34define ('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');
35define ('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');
36define ('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');
1930dc7f 37define ('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');
d516d719
DO
38define ('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');
39define ('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');
40define ('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');
05771508
DO
41define ('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');
42define ('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');
26131670 43
3540d15c
DY
44function 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
57function 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
71function 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')
ed941e67 79 return $configCache[$varname]['varvalue'] !== 0 + $varvalue;
3540d15c 80 else
ed941e67 81 return $configCache[$varname]['varvalue'] !== $varvalue;
3540d15c
DY
82}
83
8198f2c6
DO
84define ('VLAN_MIN_ID', 1);
85define ('VLAN_MAX_ID', 4094);
86define ('VLAN_DFL_ID', 1);
87
9c0b0016
DO
88function 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 {
0cc24e9a 95 throw new RuntimeException ("Configuration cache is unavailable");
9c0b0016
DO
96 }
97 if ($varname == '')
98 {
0cc24e9a 99 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
9c0b0016
DO
100 }
101 if (isset ($configCache[$varname]))
102 {
103 // Try casting to int, if possible.
3540d15c 104 return $configCache[$varname]['varvalue'];
9c0b0016
DO
105 }
106 return NULL;
107}
108
c461c579
DO
109// In softfail mode die only on fatal errors, letting the user check
110// and resubmit his input.
111function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
9c0b0016
DO
112{
113 global $configCache;
114 if (!isset ($configCache))
0cc24e9a 115 throw new RuntimeException ("Configuration cache is unavailable");
59a83bd8 116 if (!strlen ($varname))
0cc24e9a 117 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
9c0b0016
DO
118 // We don't operate on unknown data.
119 if (!isset ($configCache[$varname]))
0cc24e9a 120 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
dc2f1801 121 if ($configCache[$varname]['is_hidden'] != 'no')
3540d15c 122 throw new InvalidArgException('$varname', $varname, "'${varname}' is a system variable and cannot be changed by user.");
59a83bd8 123 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
3540d15c 124 throw new InvalidArgException('$varname', $varname, "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.");
59a83bd8 125 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
3540d15c 126 throw new InvalidArgException('$varname', $varname, "'${varname}' can accept UINT values only");
9c0b0016 127 // Update cache only if the changes went into DB.
3540d15c
DY
128 storeConfigVar ($varname, $varvalue);
129 $configCache[$varname]['varvalue'] = $varvalue;
130}
131
132function 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;
3540d15c
DY
154}
155
156function 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);
9c0b0016 173}
8d07e3e0 174
e673ee24 175?>