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