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