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