r3253 Fixed isConfigVarChanged()
[racktables] / inc / config.php
1 <?php
2 /*
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
6 * them have gone into the database, and there is a user interface
7 * for changing them. This file now provides a couple of functions to
8 * access the new config storage.
9 *
10 */
11
12
13 // Current code version is subject to change with each new release.
14 define ('CODE_VERSION', '0.17.8');
15 define ('CHAP_OBJTYPE', 1);
16 define ('CHAP_PORTTYPE', 2);
17
18 $max_dict_key = array
19 (
20 '0.17.0' => 988,
21 '0.17.1' => 988,
22 '0.17.2' => 1150,
23 '0.17.3' => 1150,
24 '0.17.4' => 1150,
25 '0.17.5' => 1322,
26 '0.17.6' => 1326,
27 '0.17.7' => 1326,
28 '0.17.8' => 1334,
29 );
30
31 define ('TAGNAME_REGEXP', '^[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
32 define ('AUTOTAGNAME_REGEXP', '^\$[[:alnum:]]([\. _~-]?[[:alnum:]])*$');
33 // The latter matches both SunOS and Linux-styled formats.
34 define ('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');
35 define ('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');
36 define ('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');
37 define ('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');
38 define ('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');
39 define ('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');
40 define ('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');
41 define ('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');
42 define ('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');
43
44 function 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
57 function 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
71 function 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')
79 return $configCache[$varname]['varvalue'] !== 0 + $varvalue;
80 else
81 return $configCache[$varname]['varvalue'] !== $varvalue;
82 }
83
84 function 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 {
91 throw new RuntimeException ("Configuration cache is unavailable");
92 }
93 if ($varname == '')
94 {
95 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
96 }
97 if (isset ($configCache[$varname]))
98 {
99 // Try casting to int, if possible.
100 return $configCache[$varname]['varvalue'];
101 }
102 return NULL;
103 }
104
105 // In softfail mode die only on fatal errors, letting the user check
106 // and resubmit his input.
107 function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
108 {
109 global $configCache;
110 if (!isset ($configCache))
111 throw new RuntimeException ("Configuration cache is unavailable");
112 if (!strlen ($varname))
113 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
114 // We don't operate on unknown data.
115 if (!isset ($configCache[$varname]))
116 throw new InvalidArgException('$varname', $varname, "Don't know how to handle '${varname}'");
117 if ($configCache[$varname]['is_hidden'] != 'no')
118 throw new InvalidArgException('$varname', $varname, "'${varname}' is a system variable and cannot be changed by user.");
119 if (!strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
120 throw new InvalidArgException('$varname', $varname, "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.");
121 if (strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
122 throw new InvalidArgException('$varname', $varname, "'${varname}' can accept UINT values only");
123 // Update cache only if the changes went into DB.
124 storeConfigVar ($varname, $varvalue);
125 $configCache[$varname]['varvalue'] = $varvalue;
126 }
127
128 function 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;
150 }
151
152 function 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);
169 }
170
171 ?>