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