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