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