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