r4138 commitResetObject(): use appropriate function for DELETE
[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.
4414e057 14define ('CODE_VERSION', '0.19.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,
98cc6bd8 34 '0.18.2' => 1352,
0891c041 35 '0.18.3' => 1356,
ec523868 36 '0.18.4' => 1364,
712010be 37 '0.18.5' => 1370,
3bcdacf6 38 '0.19.0' => 1394,
ca3d68bd
DO
39);
40
b7b4b225
DO
41define ('TAGNAME_REGEXP', '/^[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
42define ('AUTOTAGNAME_REGEXP', '/^\$[\p{L}0-9]([. _~-]?[\p{L}0-9])*$/u');
05771508 43// The latter matches both SunOS and Linux-styled formats.
b7b4b225
DO
44define ('RE_L2_IFCFG', '/^[0-9a-f]{1,2}(:[0-9a-f]{1,2}){5}$/i');
45define ('RE_L2_CISCO', '/^[0-9a-f]{4}(\.[0-9a-f]{4}){2}$/i');
c5553818 46define ('RE_L2_HUAWEI', '/^[0-9a-f]{4}(-[0-9a-f]{4}){2}$/i');
b7b4b225
DO
47define ('RE_L2_SOLID', '/^[0-9a-f]{12}$/i');
48define ('RE_L2_IPCFG', '/^[0-9a-f]{2}(-[0-9a-f]{2}){5}$/i');
49define ('RE_L2_WWN_COLON', '/^[0-9a-f]{1,2}(:[0-9a-f]{1,2}){7}$/i');
50define ('RE_L2_WWN_HYPHEN', '/^[0-9a-f]{2}(-[0-9a-f]{2}){7}$/i');
51define ('RE_L2_WWN_SOLID', '/^[0-9a-f]{16}$/i');
aeaf5779
DO
52define ('RE_IP4_ADDR', '#^[0-9]{1,3}(\.[0-9]{1,3}){3}$#');
53define ('RE_IP4_NET', '#^[0-9]{1,3}(\.[0-9]{1,3}){3}/[0-9]{1,2}$#');
52ce8bd7 54define ('E_8021Q_NOERROR', 0);
d973196a
DO
55define ('E_8021Q_VERSION_CONFLICT', 101);
56define ('E_8021Q_PULL_REMOTE_ERROR', 102);
57define ('E_8021Q_PUSH_REMOTE_ERROR', 103);
5701baec 58define ('E_8021Q_SYNC_DISABLED', 104);
bc90c415
DO
59define ('VLAN_MIN_ID', 1);
60define ('VLAN_MAX_ID', 4094);
61define ('VLAN_DFL_ID', 1);
26131670 62
3540d15c
DY
63function loadConfigDefaults() {
64 global $configCache;
65 $configCache = loadConfigCache();
d108b098 66 if (!count ($configCache))
3a089a44 67 throw new RackTablesError ('Failed to load configuration from the database.', RackTablesError::INTERNAL);
3540d15c
DY
68 foreach ($configCache as $varname => &$row) {
69 $row['is_altered'] = 'no';
70 if ($row['vartype'] == 'uint') $row['varvalue'] = 0 + $row['varvalue'];
71 $row['defaultvalue'] = $row['varvalue'];
72 }
73}
74
75function alterConfigWithUserPreferences() {
76 global $configCache;
77 global $userConfigCache;
78 global $remote_username;
79 $userConfigCache = loadUserConfigCache($remote_username);
80 foreach($userConfigCache as $key => $row) {
81 if ($configCache[$key]['is_userdefined'] == 'yes') {
82 $configCache[$key]['varvalue'] = $row['varvalue'];
83 $configCache[$key]['is_altered'] = 'yes';
84 }
85 }
86}
87
88// Returns true if varname has a different value or varname is new
89function isConfigVarChanged($varname, $varvalue) {
90 global $configCache;
91 if (!isset ($configCache))
3a089a44 92 throw new RackTablesError ('configuration cache is unavailable', RackTablesError::INTERNAL);
3540d15c
DY
93 if ($varname == '')
94 throw new InvalidArgException('$varname', $varname, 'Empty variable name');
95 if (!isset ($configCache[$varname])) return true;
96 if ($configCache[$varname]['vartype'] == 'uint')
ed941e67 97 return $configCache[$varname]['varvalue'] !== 0 + $varvalue;
3540d15c 98 else
ed941e67 99 return $configCache[$varname]['varvalue'] !== $varvalue;
3540d15c
DY
100}
101
9c0b0016
DO
102function getConfigVar ($varname = '')
103{
104 global $configCache;
105 // We assume the only point of cache init, and it is init.php. If it
106 // has failed, we don't retry loading.
107 if (!isset ($configCache))
3a089a44 108 throw new RackTablesError ('configuration cache is unavailable', RackTablesError::INTERNAL);
0294f734
DO
109 if
110 (
111 $varname == ''
112 or ! array_key_exists ($varname, $configCache)
113 )
114 throw new InvalidArgException ('$varname', $varname);
115 return $configCache[$varname]['varvalue'];
9c0b0016
DO
116}
117
c461c579
DO
118// In softfail mode die only on fatal errors, letting the user check
119// and resubmit his input.
120function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
9c0b0016
DO
121{
122 global $configCache;
123 if (!isset ($configCache))
3a089a44 124 throw new RackTablesError ('configuration cache is unavailable', RackTablesError::INTERNAL);
0294f734
DO
125 if
126 (
127 $varname == ''
128 or ! array_key_exists ($varname, $configCache)
129 )
130 throw new InvalidArgException ('$varname', $varname);
dc2f1801 131 if ($configCache[$varname]['is_hidden'] != 'no')
0294f734
DO
132 throw new InvalidRequestArgException ('$varname', $varname, 'a hidden variable cannot be changed by user');
133 if (!mb_strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
134 throw new InvalidRequestArgException ('$varvalue', $varvalue, "'${varname}' is required to have a non-empty value");
135 if (mb_strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
136 throw new InvalidRequestArgException ('$varvalue', $varvalue, "'${varname}' can accept UINT values only");
9c0b0016 137 // Update cache only if the changes went into DB.
68a0bd8a 138 usePreparedUpdateBlade ('Config', array ('varvalue' => $varvalue), array ('varname' => $varname));
3540d15c
DY
139 $configCache[$varname]['varvalue'] = $varvalue;
140}
141
142function setUserConfigVar ($varname = '', $varvalue = '')
143{
144 global $configCache;
145 global $remote_username;
146 if (!isset ($configCache))
3a089a44 147 throw new RackTablesError ('configuration cache is unavailable', RackTablesError::INTERNAL);
0294f734
DO
148 if
149 (
150 $varname == ''
151 or ! array_key_exists ($varname, $configCache)
152 )
153 throw new InvalidArgException ('$varname', $varname);
3540d15c 154 if ($configCache[$varname]['is_userdefined'] != 'yes')
0294f734 155 throw new InvalidRequestArgException ('$varname', $varname, 'a system-wide setting cannot be changed by user');
3540d15c 156 if ($configCache[$varname]['is_hidden'] != 'no')
0294f734
DO
157 throw new InvalidRequestArgException ('$varname', $varname, 'a hidden variable cannot be changed by user');
158 if (!mb_strlen ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
159 throw new InvalidRequestArgException ('$varvalue', $varvalue, "'${varname}' is required to have a non-empty value");
160 if (mb_strlen ($varvalue) && $configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
161 throw new InvalidRequestArgException ('$varvalue', $varvalue, "'${varname}' can accept UINT values only");
3540d15c 162 // Update cache only if the changes went into DB.
68a0bd8a
DO
163 usePreparedExecuteBlade
164 (
165 'REPLACE UserConfig SET varvalue=?, varname=?, user=?',
166 array ($varvalue, $varname, $remote_username)
167 );
3540d15c 168 $configCache[$varname]['varvalue'] = $varvalue;
3540d15c
DY
169}
170
171function resetUserConfigVar ($varname = '')
172{
173 global $configCache;
174 global $remote_username;
175 if (!isset ($configCache))
3a089a44 176 throw new RackTablesError ('configuration cache is unavailable', RackTablesError::INTERNAL);
0294f734
DO
177 if
178 (
179 $varname == ''
180 or ! array_key_exists ($varname, $configCache)
181 )
182 throw new InvalidArgException ('$varname', $varname);
3540d15c 183 if ($configCache[$varname]['is_userdefined'] != 'yes')
0294f734 184 throw new InvalidRequestArgException ('$varname', $varname, 'a system-wide setting cannot be changed by user');
3540d15c 185 if ($configCache[$varname]['is_hidden'] != 'no')
0294f734 186 throw new InvalidRequestArgException ('$varname', $varname, 'a hidden variable cannot be changed by user');
3540d15c 187 // Update cache only if the changes went into DB.
68a0bd8a 188 usePreparedDeleteBlade ('UserConfig', array ('varname' => $varname, 'user' => $remote_username));
9c0b0016 189}
8d07e3e0 190
e673ee24 191?>