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