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