r2062 + implement RackCode parse tree caching and save roughly 3 seconds delay per...
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
2c6c7645 10function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
17c32f4c
DO
11{
12 if (!isset ($_REQUEST[$argname]))
13 {
2c6c7645 14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
2c6c7645 19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
2c6c7645 24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
2c6c7645 29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
30 die();
31 }
32}
33
34// This function assures that specified argument was passed
35// and is a non-empty string.
2c6c7645 36function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
37{
38 if (!isset ($_REQUEST[$argname]))
39 {
2c6c7645 40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
2c6c7645 45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
2c6c7645 50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
51 die();
52 }
53}
54
2c6c7645 55function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
56{
57 if (!isset ($_REQUEST[$argname]))
58 {
2c6c7645 59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
2c6c7645 64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
2c6c7645 69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
70 die();
71 }
72}
73
2c6c7645 74function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c 75{
2c6c7645 76 assertStringArg ($argname, $caller, $ok_if_empty);
17c32f4c
DO
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
2c6c7645 79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
80 die();
81 }
82}
83
e673ee24
DO
84function addPortForwarding ()
85{
2c6c7645
DO
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 90 assertStringArg ('proto', __FUNCTION__);
2c6c7645 91 assertStringArg ('description', __FUNCTION__, TRUE);
103b1e1e 92 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
31fc4157 93 if (empty ($remoteport))
f75eb878 94 $remoteport = $_REQUEST['localport'];
e673ee24 95
9bea3a8b
DO
96 $error = newPortForwarding
97 (
103b1e1e 98 $_REQUEST['object_id'],
9bea3a8b
DO
99 $_REQUEST['localip'],
100 $_REQUEST['localport'],
101 $_REQUEST['remoteip'],
102 $remoteport,
103 $_REQUEST['proto'],
104 $_REQUEST['description']
105 );
e673ee24
DO
106
107 if ($error == '')
103b1e1e 108 return buildRedirectURL_OK ('NATv4 rule was successfully added.');
e673ee24 109 else
103b1e1e 110 return buildRedirectURL_ERR ($error);
e673ee24
DO
111}
112
113function delPortForwarding ()
114{
103b1e1e
DO
115 assertUIntArg ('object_id', __FUNCTION__);
116 assertIPv4Arg ('localip', __FUNCTION__);
117 assertIPv4Arg ('remoteip', __FUNCTION__);
118 assertUIntArg ('localport', __FUNCTION__);
119 assertUIntArg ('remoteport', __FUNCTION__);
120 assertStringArg ('proto', __FUNCTION__);
e673ee24 121
103b1e1e
DO
122 $error = deletePortForwarding
123 (
124 $_REQUEST['object_id'],
125 $_REQUEST['localip'],
126 $_REQUEST['localport'],
127 $_REQUEST['remoteip'],
128 $_REQUEST['remoteport'],
129 $_REQUEST['proto']
130 );
e673ee24 131 if ($error == '')
103b1e1e 132 return buildRedirectURL_OK ('NATv4 rule was successfully deleted.');
e673ee24 133 else
103b1e1e 134 return buildRedirectURL_ERR ($error);
e673ee24
DO
135}
136
137function updPortForwarding ()
138{
103b1e1e
DO
139 assertUIntArg ('object_id', __FUNCTION__);
140 assertIPv4Arg ('localip', __FUNCTION__);
141 assertIPv4Arg ('remoteip', __FUNCTION__);
142 assertUIntArg ('localport', __FUNCTION__);
143 assertUIntArg ('remoteport', __FUNCTION__);
144 assertStringArg ('proto', __FUNCTION__);
145 assertStringArg ('description', __FUNCTION__);
e673ee24 146
103b1e1e
DO
147 $error = updatePortForwarding
148 (
149 $_REQUEST['object_id'],
150 $_REQUEST['localip'],
151 $_REQUEST['localport'],
152 $_REQUEST['remoteip'],
153 $_REQUEST['remoteport'],
154 $_REQUEST['proto'],
155 $_REQUEST['description']
156 );
e673ee24 157 if ($error == '')
103b1e1e 158 return buildRedirectURL_OK ('NATv4 rule was successfully updated');
e673ee24 159 else
103b1e1e 160 return buildRedirectURL_ERR ($error);
e673ee24
DO
161}
162
e673ee24
DO
163function addPortForObject ()
164{
103b1e1e
DO
165 assertUIntArg ('object_id', __FUNCTION__);
166 assertStringArg ('port_name', __FUNCTION__, TRUE);
167 if (empty ($_REQUEST['port_name']))
168 return buildRedirectURL_ERR ('Port name cannot be empty');
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
170 if ($error != '')
171 return buildRedirectURL_ERR ($error);
e673ee24 172 else
3a2cb4d6 173 return buildRedirectURL_OK ("Port ${_REQUEST['port_name']} was added successfully");
e673ee24
DO
174}
175
176function editPortForObject ()
177{
2c6c7645 178 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e
DO
179 assertStringArg ('name', __FUNCTION__, TRUE);
180 if (empty ($_REQUEST['name']))
181 return buildRedirectURL_ERR ('Port name cannot be empty');
182
e673ee24
DO
183 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
184 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
185 else
186 $port_rc = 'NULL';
103b1e1e
DO
187 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
188 if ($error != '')
189 return buildRedirectURL_ERR ($error);
e673ee24 190 else
3a2cb4d6 191 return buildRedirectURL_OK ("Port ${_REQUEST['name']} was updated successfully");
e673ee24
DO
192}
193
194function delPortFromObject ()
195{
103b1e1e 196 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 197 $port_name = $_REQUEST['port_name'];
103b1e1e 198 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
199
200 if ($error != '')
103b1e1e 201 return buildRedirectURL_ERR ($error);
e673ee24 202 else
3a2cb4d6 203 return buildRedirectURL_OK ("Port ${_REQUEST['port_name']} was deleted successfully");
e673ee24
DO
204}
205
206function linkPortForObject ()
207{
103b1e1e
DO
208 assertUIntArg ('port_id', __FUNCTION__);
209 assertUIntArg ('remote_port_id', __FUNCTION__);
e673ee24
DO
210 $object_id = $_REQUEST['object_id'];
211 $port_name = $_REQUEST['port_name'];
212 $remote_port_name = $_REQUEST['remote_port_name'];
213 $remote_object_name = $_REQUEST['remote_object_name'];
214
103b1e1e 215 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 216 if ($error != '')
103b1e1e 217 return buildRedirectURL_ERR ($error);
e673ee24 218 else
103b1e1e 219 return buildRedirectURL_OK ("Port $port_name successfully linked with port $remote_port_name at object $remote_object_name");
e673ee24
DO
220}
221
222function unlinkPortForObject ()
223{
103b1e1e 224 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 225
103b1e1e 226 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 227 if ($error != '')
103b1e1e 228 return buildRedirectURL_ERR ($error);
e673ee24 229 else
3a2cb4d6 230 return buildRedirectURL_OK ("Port ${_REQUEST['port_name']} was successfully unlinked from ${_REQUEST['remote_port_name']}@${_REQUEST['remote_object_name']}");
e673ee24
DO
231}
232
233function addMultiPorts ()
234{
2c6c7645
DO
235 assertStringArg ('format', __FUNCTION__);
236 assertStringArg ('input', __FUNCTION__);
237 assertUIntArg ('port_type', __FUNCTION__);
238 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
239 $format = $_REQUEST['format'];
240 $port_type = $_REQUEST['port_type'];
241 $object_id = $_REQUEST['object_id'];
242 // Input lines are escaped, so we have to explode and to chop by 2-char
243 // \n and \r respectively.
244 $lines1 = explode ('\n', $_REQUEST['input']);
245 foreach ($lines1 as $line)
246 {
247 $parts = explode ('\r', $line);
248 reset ($parts);
249 if (empty ($parts[0]))
250 continue;
251 else
252 $lines2[] = rtrim ($parts[0]);
253 }
254 $ports = array();
255 foreach ($lines2 as $line)
256 {
257 switch ($format)
258 {
259 case 'fisxii':
260 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
261 list ($slot, $port) = explode ('/', $words[0]);
262 $ports[] = array
263 (
264 'name' => "e ${slot}/${port}",
265 'l2address' => $words[8],
266 'label' => "slot ${slot} port ${port}"
267 );
268 break;
269 case 'c3600asy':
270 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
271/*
272How Async Lines are Numbered in Cisco 3600 Series Routers
273http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
274
275Understanding 16- and 32-Port Async Network Modules
276http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
277*/
278 $async = $words[0];
279 $slot = floor (($async - 1) / 32);
280 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
281 $cable = $async - $slot * 32 - $octalgroup * 8;
282 $og_label[0] = 'async 0-7';
283 $og_label[1] = 'async 8-15';
284 $og_label[2] = 'async 16-23';
285 $og_label[3] = 'async 24-31';
286 $ports[] = array
287 (
288 'name' => "async ${async}",
289 'l2address' => '',
290 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
291 );
292 break;
293 case 'fiwg':
294 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
295 $ifnumber = $words[0] * 1;
296 $ports[] = array
297 (
298 'name' => "e ${ifnumber}",
299 'l2address' => "${words[8]}",
300 'label' => "${ifnumber}"
301 );
302 break;
351d4dbf 303 case 'ssv1':
5b585342 304 $words = explode (' ', $line);
351d4dbf
DO
305 if (empty ($words[0]) or empty ($words[1]))
306 continue;
307 $ports[] = array
308 (
309 'name' => $words[0],
310 'l2address' => $words[1],
311 'label' => ''
312 );
313 break;
e673ee24 314 default:
103b1e1e 315 return buildRedirectURL_ERR ('Cannot process submitted data: unknown format code.');
e673ee24
DO
316 break;
317 }
318 }
319 // Create ports, if they don't exist.
320 $added_count = $updated_count = $error_count = 0;
321 foreach ($ports as $port)
322 {
323 $port_id = getPortID ($object_id, $port['name']);
324 if ($port_id === NULL)
325 {
326 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
327 if ($result == '')
328 $added_count++;
329 else
330 $error_count++;
331 }
332 else
333 {
334 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
335 if ($result == '')
336 $updated_count++;
337 else
338 $error_count++;
339 }
340 }
103b1e1e 341 return buildRedirectURL_OK ("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
e673ee24
DO
342}
343
b4c1ef87 344function updIPv4Allocation ()
e673ee24 345{
b4c1ef87
DO
346 assertIPv4Arg ('ip', __FUNCTION__);
347 assertUIntArg ('object_id', __FUNCTION__);
348 assertStringArg ('bond_name', __FUNCTION__, TRUE);
349 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 350
b4c1ef87 351 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 352 if ($error != '')
46f92ff7 353 return buildRedirectURL_ERR ($error);
e673ee24 354 else
46f92ff7 355 return buildRedirectURL_OK ('allocation updated');
e673ee24
DO
356}
357
b4c1ef87 358function delIPv4Allocation ()
e673ee24 359{
b4c1ef87
DO
360 assertIPv4Arg ('ip', __FUNCTION__);
361 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 362
b4c1ef87 363 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 364 if ($error != '')
46f92ff7 365 return buildRedirectURL_ERR ($error);
e673ee24 366 else
46f92ff7 367 return buildRedirectURL_OK ('deallocated');
e673ee24
DO
368}
369
b4c1ef87 370function addIPv4Allocation ()
e673ee24 371{
b4c1ef87
DO
372 assertIPv4Arg ('ip', __FUNCTION__);
373 assertUIntArg ('object_id', __FUNCTION__);
374 assertStringArg ('bond_name', __FUNCTION__, TRUE);
375 assertStringArg ('bond_type', __FUNCTION__);
da958e52 376
b4c1ef87
DO
377 // Strip masklen.
378 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
379 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
380 $address = getIPAddress ($ip);
381 if ($address['exists'] and ($address['reserved'] == 'yes' or strlen ($address['name']) > 0))
382 {
383 $release = getConfigVar ('IPV4_AUTO_RELEASE');
384 if ($release >= 1)
385 $address['reserved'] = 'no';
386 if ($release >= 2)
387 $address['name'] = '';
388 updateAddress ($ip, $address['name'], $address['reserved']);
389 }
e673ee24 390 if ($error != '')
46f92ff7 391 return buildRedirectURL_ERR ($error);
e673ee24 392 else
46f92ff7 393 return buildRedirectURL_OK ('allocated');
e673ee24
DO
394}
395
42023f03 396function addIPv4Prefix ()
e673ee24 397{
2c6c7645
DO
398 assertStringArg ('range', __FUNCTION__);
399 assertStringArg ('name', __FUNCTION__);
e673ee24 400
fa05e3de
DO
401 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
402 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 403 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 404 if ($error != '')
103b1e1e 405 return buildRedirectURL_ERR ($error);
e673ee24 406 else
103b1e1e 407 return buildRedirectURL_OK ('IPv4 prefix successfully added');
e673ee24
DO
408}
409
42023f03 410function delIPv4Prefix ()
e673ee24 411{
da958e52 412 assertUIntArg ('id', __FUNCTION__);
42023f03 413 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 414 if ($error != '')
103b1e1e 415 return buildRedirectURL_ERR ($error);
e673ee24 416 else
103b1e1e 417 return buildRedirectURL_OK ('IPv4 prefix deleted');
e673ee24
DO
418}
419
42023f03 420function editRange ()
e673ee24 421{
da958e52
DO
422 assertUIntArg ('id', __FUNCTION__);
423 assertStringArg ('name', __FUNCTION__);
e673ee24 424
da958e52 425 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 426 if ($error != '')
46f92ff7 427 return buildRedirectURL_ERR ($error);
e673ee24 428 else
46f92ff7 429 return buildRedirectURL_OK ('IPv4 prefix updated');
e673ee24
DO
430}
431
432function editAddress ()
433{
da958e52
DO
434 assertIPv4Arg ('ip', __FUNCTION__);
435 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 436
e673ee24
DO
437 if (isset ($_REQUEST['reserved']))
438 $reserved = $_REQUEST['reserved'];
439 else
440 $reserved = 'off';
da958e52 441 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 442 if ($error != '')
46f92ff7 443 return buildRedirectURL_ERR ($error);
e673ee24 444 else
46f92ff7 445 return buildRedirectURL_OK ('IPv4 address updated');
e673ee24
DO
446}
447
cced6b7d 448function createUser ()
e673ee24 449{
2c6c7645
DO
450 assertStringArg ('username', __FUNCTION__);
451 assertStringArg ('realname', __FUNCTION__, TRUE);
452 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
453 $username = $_REQUEST['username'];
454 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
455 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
456 if ($result == TRUE)
103b1e1e 457 return buildRedirectURL_OK ("User account ${username} created.");
e673ee24 458 else
103b1e1e 459 return buildRedirectURL_ERR ("Error creating user account ${username}.");
e673ee24
DO
460}
461
cced6b7d 462function updateUser ()
e673ee24 463{
cced6b7d 464 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
465 assertStringArg ('username', __FUNCTION__);
466 assertStringArg ('realname', __FUNCTION__, TRUE);
467 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
468 $username = $_REQUEST['username'];
469 $new_password = $_REQUEST['password'];
103b1e1e 470 $old_hash = getHashByID ($_REQUEST['user_id']);
e673ee24 471 if ($old_hash == NULL)
103b1e1e 472 return buildRedirectURL_ERR ('getHashByID() failed');
e673ee24
DO
473 // Update user password only if provided password is not the same as current password hash.
474 if ($new_password != $old_hash)
475 $new_password = hash (PASSWORD_HASH, $new_password);
103b1e1e 476 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 477 if ($result == TRUE)
103b1e1e 478 return buildRedirectURL_OK ("User account ${username} updated.");
e673ee24 479 else
103b1e1e 480 return buildRedirectURL_ERR ("Error updating user account ${username}.");
e673ee24
DO
481}
482
cced6b7d 483function enableUser ()
e673ee24 484{
cced6b7d
DO
485 assertUIntArg ('user_id', __FUNCTION__);
486 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
103b1e1e 487 return buildRedirectURL_OK ('User account enabled.');
e673ee24 488 else
103b1e1e 489 return buildRedirectURL_ERR ('Error enabling user account.');
e673ee24
DO
490}
491
cced6b7d 492function disableUser ()
e673ee24 493{
cced6b7d 494 assertUIntArg ('user_id', __FUNCTION__);
103b1e1e 495 if ($_REQUEST['user_id'] == 1)
e673ee24
DO
496 $result = FALSE;
497 else
103b1e1e 498 $result = commitEnableUserAccount ($_REQUEST['user_id'], 'no');
e673ee24 499 if ($result == TRUE)
103b1e1e 500 return buildRedirectURL_OK ('User account disabled.');
e673ee24 501 else
103b1e1e 502 return buildRedirectURL_ERR ('Error disabling user account.');
e673ee24
DO
503}
504
e673ee24
DO
505// This function find differences in users's submit and PortCompat table
506// and modifies database accordingly.
507function savePortMap ()
508{
e673ee24
DO
509 $ptlist = getPortTypes();
510 $oldCompat = getPortCompat();
511 $newCompat = array();
512 foreach (array_keys ($ptlist) as $leftKey)
513 foreach (array_keys ($ptlist) as $rightKey)
514 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
515 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
516 // Build the new matrix from user's submit and compare it to
517 // the old matrix. Those pairs which appear on
518 // new matrix only, have to be stored in PortCompat table. Those who appear
519 // on the old matrix only, should be removed from PortCompat table.
520 // Those present in both matrices should be skipped.
521 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
522 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
523 $error_count = $success_count = 0;
524 foreach (array_keys ($ptlist) as $type1)
525 foreach (array_keys ($ptlist) as $type2)
526 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
527 switch ($oldCompatTable[$type1][$type2])
528 {
529 case TRUE: // new value is FALSE
530 if (removePortCompat ($type1, $type2) === TRUE)
531 $success_count++;
532 else
533 $error_count++;
534 break;
535 case FALSE: // new value is TRUE
536 if (addPortCompat ($type1, $type2) === TRUE)
537 $success_count++;
538 else
539 $error_count++;
540 break;
541 default:
61e269b5 542 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
543 break;
544 }
103b1e1e
DO
545 if ($error_count == 0)
546 return buildRedirectURL_OK ("${error_count} failures and ${success_count} successfull changes.");
547 else
548 return buildRedirectURL_ERR ("${error_count} failures and ${success_count} successfull changes.");
e673ee24
DO
549}
550
551function updateDictionary ()
552{
2c6c7645
DO
553 assertUIntArg ('chapter_no', __FUNCTION__);
554 assertUIntArg ('dict_key', __FUNCTION__);
555 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 556 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
103b1e1e 557 return buildRedirectURL_OK ('Update succeeded.');
e673ee24 558 else
103b1e1e 559 return buildRedirectURL_ERR ('Update failed!');
e673ee24
DO
560}
561
562function supplementDictionary ()
563{
2c6c7645
DO
564 assertUIntArg ('chapter_no', __FUNCTION__);
565 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 566 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
103b1e1e 567 return buildRedirectURL_OK ('Supplement succeeded.');
e673ee24 568 else
103b1e1e 569 return buildRedirectURL_ERR ('Supplement failed!');
e673ee24
DO
570}
571
572function reduceDictionary ()
573{
2c6c7645
DO
574 assertUIntArg ('chapter_no', __FUNCTION__);
575 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 576 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
103b1e1e 577 return buildRedirectURL_OK ('Reduction succeeded.');
e673ee24 578 else
103b1e1e 579 return buildRedirectURL_ERR ('Reduction failed!');
e673ee24
DO
580}
581
582function addChapter ()
583{
2c6c7645 584 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 585 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
103b1e1e 586 return buildRedirectURL_OK ('Chapter was added.');
e673ee24 587 else
103b1e1e 588 return buildRedirectURL_ERR ('Error adding chapter.');
e673ee24
DO
589}
590
591function updateChapter ()
592{
2c6c7645
DO
593 assertUIntArg ('chapter_no', __FUNCTION__);
594 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 595 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
103b1e1e 596 return buildRedirectURL_OK ('Chapter was updated.');
e673ee24 597 else
103b1e1e 598 return buildRedirectURL_ERR ('Error updating chapter.');
e673ee24
DO
599}
600
601function delChapter ()
602{
2c6c7645 603 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 604 if (commitDeleteChapter ($_REQUEST['chapter_no']))
103b1e1e 605 return buildRedirectURL_OK ('Chapter was deleted.');
e673ee24 606 else
103b1e1e 607 return buildRedirectURL_ERR ('Error deleting chapter.');
e673ee24
DO
608}
609
610function changeAttribute ()
611{
2c6c7645
DO
612 assertUIntArg ('attr_id', __FUNCTION__);
613 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 614 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
103b1e1e 615 return buildRedirectURL_OK ('Rename successful.');
e673ee24 616 else
103b1e1e 617 return buildRedirectURL_ERR ('Error renaming attribute.');
e673ee24
DO
618}
619
620function createAttribute ()
621{
2c6c7645
DO
622 assertStringArg ('attr_name', __FUNCTION__);
623 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 624 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
103b1e1e 625 return buildRedirectURL_OK ("Attribute '${_REQUEST['attr_name']}' created.");
e673ee24 626 else
103b1e1e 627 return buildRedirectURL_ERR ('Error creating attribute.');
e673ee24
DO
628}
629
630function deleteAttribute ()
631{
2c6c7645 632 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 633 if (commitDeleteAttribute ($_REQUEST['attr_id']))
103b1e1e 634 return buildRedirectURL_OK ('Attribute was deleted.');
e673ee24 635 else
103b1e1e 636 return buildRedirectURL_ERR ('Error deleting attribute.');
e673ee24
DO
637}
638
639function supplementAttrMap ()
640{
2c6c7645
DO
641 assertUIntArg ('attr_id', __FUNCTION__);
642 assertUIntArg ('objtype_id', __FUNCTION__);
643 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 644 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
103b1e1e 645 return buildRedirectURL_OK ('Supplement succeeded.');
e673ee24 646 else
103b1e1e 647 return buildRedirectURL_ERR ('Supplement failed!');
e673ee24
DO
648}
649
650function reduceAttrMap ()
651{
2c6c7645
DO
652 assertUIntArg ('attr_id', __FUNCTION__);
653 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 654 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
103b1e1e 655 return buildRedirectURL_OK ('Reduction succeeded.');
e673ee24 656 else
103b1e1e 657 return buildRedirectURL_ERR ("Reduction failed!");
e673ee24
DO
658}
659
22bb04da 660function clearSticker ()
e673ee24 661{
2c6c7645
DO
662 assertUIntArg ('attr_id', __FUNCTION__);
663 assertUIntArg ('object_id', __FUNCTION__);
22bb04da
DO
664 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
665 return buildRedirectURL_OK ('Reset succeeded.');
e673ee24 666 else
22bb04da 667 return buildRedirectURL_ERR ('Reset failed!');
e673ee24
DO
668}
669
22bb04da
DO
670function updateObject ()
671{
672 assertUIntArg ('object_id', __FUNCTION__);
673 assertUIntArg ('object_type_id', __FUNCTION__);
674 assertStringArg ('object_name', __FUNCTION__, TRUE);
675 assertStringArg ('object_label', __FUNCTION__, TRUE);
676 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
677 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
678 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
679 $has_problems = 'yes';
680 else
681 $has_problems = 'no';
682
683 if (commitUpdateObject (
684 $_REQUEST['object_id'],
685 $_REQUEST['object_name'],
686 $_REQUEST['object_label'],
687 $_REQUEST['object_barcode'],
688 $_REQUEST['object_type_id'],
689 $has_problems,
690 $_REQUEST['object_asset_no'],
691 $_REQUEST['object_comment']
692 ) !== TRUE)
693 return buildRedirectURL_ERR ('commitUpdateObject() failed');
694 // Invalidate thumb cache of all racks objects could occupy.
695 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
696 resetThumbCache ($rack_id);
697 return buildRedirectURL_OK ('Update done');
698}
699
700function updateStickers ()
e673ee24 701{
2c6c7645 702 assertUIntArg ('object_id', __FUNCTION__);
f59e8cf5
DO
703 assertUIntArg ('num_attrs', __FUNCTION__);
704 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a
DO
705 $result = array();
706
103b1e1e 707 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 708 {
2c6c7645 709 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
710 $attr_id = $_REQUEST["${i}_attr_id"];
711
712 // Field is empty, delete attribute and move on.
713 if (empty($_REQUEST["${i}_value"]))
714 {
103b1e1e 715 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
716 continue;
717 }
718
719 // The value could be uint/float, but we don't know ATM. Let SQL
720 // server check this and complain.
2c6c7645 721 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
722 $value = $_REQUEST["${i}_value"];
723 switch ($oldvalues[$attr_id]['type'])
724 {
725 case 'uint':
726 case 'float':
727 case 'string':
728 $oldvalue = $oldvalues[$attr_id]['value'];
729 break;
730 case 'dict':
731 $oldvalue = $oldvalues[$attr_id]['key'];
732 break;
733 default:
61e269b5 734 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
735 die;
736 }
737 if ($value == $oldvalue)
738 continue;
739
740 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 741 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a
DO
742 }
743
103b1e1e
DO
744 if (in_array (FALSE, $result))
745 return buildRedirectURL_ERR ('One or more update(s) failed!');
0dfb8a2a 746
103b1e1e 747 return buildRedirectURL_OK ('Update(s) succeeded.');
e673ee24
DO
748}
749
750function useupPort ()
751{
2c6c7645 752 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 753 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
103b1e1e 754 return buildRedirectURL_OK ('Reservation removed.');
e673ee24 755 else
103b1e1e 756 return buildRedirectURL_ERR ('Error removing reservation!');
e673ee24
DO
757}
758
4fe32e78
DY
759function updateUI ()
760{
2c6c7645 761 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 762 $error = '';
4fe32e78 763
103b1e1e 764 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 765 {
2c6c7645
DO
766 assertStringArg ("${i}_varname", __FUNCTION__);
767 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 768 $varname = $_REQUEST["${i}_varname"];
4fe32e78 769 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 770
c461c579
DO
771 // If form value = value in DB, don't bother updating DB
772 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
773 continue;
774
775 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
776 $error = setConfigVar ($varname, $varvalue, TRUE);
777 if (!empty ($error))
778 break;
4fe32e78
DY
779 }
780
c461c579 781 if ($error != '')
103b1e1e 782 return buildRedirectURL_ERR ('Update failed with error: ' . $error);
4fe32e78 783
103b1e1e 784 return buildRedirectURL_OK ('Update succeeded.');
4fe32e78
DY
785}
786
b07f617c
DO
787function resetUIConfig()
788{
1f86e996 789 setConfigVar ('default_port_type','24');
b07f617c
DO
790 setConfigVar ('MASSCOUNT','15');
791 setConfigVar ('MAXSELSIZE','30');
792 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
793 setConfigVar ('ROW_SCALE','2');
794 setConfigVar ('PORTS_PER_ROW','12');
795 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
796 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 797 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
798 setConfigVar ('USER_AUTH_SRC','database');
799 setConfigVar ('DEFAULT_SLB_VS_PORT','');
800 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 801 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 802 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 803 setConfigVar ('DETECT_URLS','no');
4b8d413e 804 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 805 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 806 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
807 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
808 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
809 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 810 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 811 setConfigVar ('IPV4_AUTO_RELEASE','1');
103b1e1e 812 return buildRedirectURL_OK ('Reset complete');
b07f617c
DO
813}
814
732e4578 815// Add single record.
ca461127
DO
816function addRealServer ()
817{
2c6c7645
DO
818 assertUIntArg ('pool_id', __FUNCTION__);
819 assertIPv4Arg ('remoteip', __FUNCTION__);
820 assertUIntArg ('rsport', __FUNCTION__);
821 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
822 if (!addRStoRSPool (
823 $_REQUEST['pool_id'],
824 $_REQUEST['remoteip'],
825 $_REQUEST['rsport'],
826 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
827 $_REQUEST['rsconfig']
828 ))
829 return buildRedirectURL_ERR ('addRStoRSPool() failed');
d6517a21 830 else
103b1e1e 831 return buildRedirectURL_OK ('Real server was successfully added');
d6517a21
DO
832}
833
732e4578
DO
834// Parse textarea submitted and try adding a real server for each line.
835function addRealServers ()
836{
cbadbb38 837 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
838 assertStringArg ('format', __FUNCTION__);
839 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
840 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
841 $ngood = $nbad = 0;
842 $rsconfig = '';
843 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
844 foreach (explode ('\n', $rawtext) as $line)
845 {
846 if (empty ($line))
847 continue;
848 $match = array ();
849 switch ($_REQUEST['format'])
850 {
851 case 'ipvs_2': // address and port only
852 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
853 continue;
103b1e1e 854 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
855 $ngood++;
856 else
857 $nbad++;
858 break;
859 case 'ipvs_3': // address, port and weight
860 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
861 continue;
103b1e1e 862 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
863 $ngood++;
864 else
865 $nbad++;
866 break;
e69c2aa3
DO
867 case 'ssv_2': // IP address and port
868 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
869 continue;
103b1e1e 870 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
871 $ngood++;
872 else
873 $nbad++;
874 break;
732e4578 875 default:
103b1e1e 876 return buildRedirectURL_ERR (__FUNCTION__ . ': invalid format requested');
732e4578
DO
877 break;
878 }
879 }
880 if ($nbad == 0 and $ngood > 0)
103b1e1e 881 return buildRedirectURL_OK ("Successfully added ${ngood} real servers");
732e4578 882 else
103b1e1e 883 return buildRedirectURL_ERR ("Added ${ngood} real servers and encountered ${nbad} errors");
732e4578
DO
884}
885
d6517a21
DO
886function addVService ()
887{
2c6c7645
DO
888 assertIPv4Arg ('vip', __FUNCTION__);
889 assertUIntArg ('vport', __FUNCTION__);
890 assertStringArg ('proto', __FUNCTION__);
103b1e1e
DO
891 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
892 return buildRedirectURL_ERR (__FUNCTION__ . ': invalid protocol');
2c6c7645
DO
893 assertStringArg ('name', __FUNCTION__, TRUE);
894 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
895 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
896 $error = commitCreateVS
897 (
103b1e1e
DO
898 $_REQUEST['vip'],
899 $_REQUEST['vport'],
7320b661 900 $_REQUEST['proto'],
103b1e1e
DO
901 $_REQUEST['name'],
902 $_REQUEST['vsconfig'],
c63a8d6e
DO
903 $_REQUEST['rsconfig'],
904 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
905 );
906 if ($error != '')
907 return buildRedirectURL_ERR ($error);
ca461127 908 else
103b1e1e 909 return buildRedirectURL_OK ('Virtual service was successfully created');
fb1c4a54
DO
910}
911
912function deleteRealServer ()
913{
2c6c7645 914 assertUIntArg ('id', __FUNCTION__);
d6517a21 915 if (!commitDeleteRS ($_REQUEST['id']))
103b1e1e 916 return buildRedirectURL_ERR ('commitDeleteRS() failed');
fb1c4a54 917 else
103b1e1e 918 return buildRedirectURL_OK ('Real server was successfully deleted');
fb1c4a54
DO
919}
920
3241551e
DO
921function deleteLoadBalancer ()
922{
2c6c7645
DO
923 assertUIntArg ('object_id', __FUNCTION__);
924 assertUIntArg ('pool_id', __FUNCTION__);
925 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
926 if (!commitDeleteLB (
927 $_REQUEST['object_id'],
928 $_REQUEST['pool_id'],
929 $_REQUEST['vs_id']
930 ))
49fb2686 931 return buildRedirectURL_ERR ('commitDeleteLB() failed');
d6517a21 932 else
49fb2686 933 return buildRedirectURL_OK ('Load balancer was successfully deleted');
d6517a21
DO
934}
935
936function deleteVService ()
937{
e02e1941
DO
938 assertUIntArg ('vs_id', __FUNCTION__);
939 if (!commitDeleteVS ($_REQUEST['vs_id']))
940 return buildRedirectURL_ERR ('commitDeleteVS() failed');
3241551e 941 else
e02e1941 942 return buildRedirectURL_OK ('Virtual service was successfully deleted');
3241551e
DO
943}
944
fb1c4a54
DO
945function updateRealServer ()
946{
2c6c7645
DO
947 assertUIntArg ('rs_id', __FUNCTION__);
948 assertIPv4Arg ('rsip', __FUNCTION__);
949 assertUIntArg ('rsport', __FUNCTION__);
950 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
951 if (!commitUpdateRS (
952 $_REQUEST['rs_id'],
953 $_REQUEST['rsip'],
954 $_REQUEST['rsport'],
955 $_REQUEST['rsconfig']
956 ))
957 return buildRedirectURL_ERR ('commitUpdateRS() failed');
fb1c4a54 958 else
103b1e1e 959 return buildRedirectURL_OK ('Real server was successfully updated');
ca461127
DO
960}
961
c1ca768c 962function updateLoadBalancer ()
3241551e 963{
2c6c7645
DO
964 assertUIntArg ('object_id', __FUNCTION__);
965 assertUIntArg ('pool_id', __FUNCTION__);
966 assertUIntArg ('vs_id', __FUNCTION__);
967 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
968 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
969 if (!commitUpdateLB (
970 $_REQUEST['object_id'],
971 $_REQUEST['pool_id'],
972 $_REQUEST['vs_id'],
973 $_REQUEST['vsconfig'],
974 $_REQUEST['rsconfig']
975 ))
49fb2686 976 return buildRedirectURL_ERR ('commitUpdateLB() failed');
d6517a21 977 else
49fb2686 978 return buildRedirectURL_OK ('Load balancer info was successfully updated');
d6517a21
DO
979}
980
981function updateVService ()
982{
e02e1941 983 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
984 assertIPv4Arg ('vip', __FUNCTION__);
985 assertUIntArg ('vport', __FUNCTION__);
986 assertStringArg ('proto', __FUNCTION__);
987 assertStringArg ('name', __FUNCTION__, TRUE);
988 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
989 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
990 if (!commitUpdateVS (
991 $_REQUEST['vs_id'],
992 $_REQUEST['vip'],
993 $_REQUEST['vport'],
994 $_REQUEST['proto'],
995 $_REQUEST['name'],
996 $_REQUEST['vsconfig'],
997 $_REQUEST['rsconfig']
998 ))
c9890988 999 return buildRedirectURL_ERR ('commitUpdateVS() failed');
3241551e 1000 else
c9890988 1001 return buildRedirectURL_OK ('Virtual service was successfully updated');
3241551e
DO
1002}
1003
1004function addLoadBalancer ()
1005{
2c6c7645
DO
1006 assertUIntArg ('pool_id', __FUNCTION__);
1007 assertUIntArg ('object_id', __FUNCTION__);
1008 assertUIntArg ('vs_id', __FUNCTION__);
1009 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1010 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1011 if (!addLBtoRSPool (
1012 $_REQUEST['pool_id'],
1013 $_REQUEST['object_id'],
1014 $_REQUEST['vs_id'],
1015 $_REQUEST['vsconfig'],
1016 $_REQUEST['rsconfig']
1017 ))
49fb2686 1018 return buildRedirectURL_ERR ('addLBtoRSPool() failed');
3241551e 1019 else
49fb2686 1020 return buildRedirectURL_OK ('Load balancer was successfully added');
3241551e
DO
1021}
1022
5ad76f01
DO
1023function addRSPool ()
1024{
2c6c7645
DO
1025 assertStringArg ('name', __FUNCTION__, TRUE);
1026 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1027 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1028 $error = commitCreateRSPool
1029 (
103b1e1e
DO
1030 $_REQUEST['name'],
1031 $_REQUEST['vsconfig'],
c63a8d6e
DO
1032 $_REQUEST['rsconfig'],
1033 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1034 );
1035 if ($error != '')
1036 return buildRedirectURL_ERR ($error);
5ad76f01 1037 else
103b1e1e 1038 return buildRedirectURL_OK ('RS pool was successfully created');
5ad76f01
DO
1039}
1040
1041function deleteRSPool ()
1042{
2c6c7645 1043 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1044 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
103b1e1e 1045 return buildRedirectURL_ERR ('commitDeleteRSPool() failed');
5ad76f01 1046 else
103b1e1e 1047 return buildRedirectURL_OK ('RS pool was successfully deleted');
5ad76f01
DO
1048}
1049
1050function updateRSPool ()
1051{
2c6c7645
DO
1052 assertUIntArg ('pool_id', __FUNCTION__);
1053 assertStringArg ('name', __FUNCTION__, TRUE);
1054 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1055 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1056 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
ea5a3250 1057 return buildRedirectURL_ERR ('commitUpdateRSPool() failed');
5ad76f01 1058 else
103b1e1e 1059 return buildRedirectURL_OK ('RS pool was successfully updated');
5ad76f01
DO
1060}
1061
1f7d18fa
DO
1062function updateRSInService ()
1063{
2c6c7645 1064 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1065 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1066 $orig = getRSPoolInfo ($pool_id);
1067 $nbad = $ngood = 0;
1068 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1069 {
1070 $rs_id = $_REQUEST["rsid_${i}"];
1071 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1072 $newval = 'yes';
1073 else
1074 $newval = 'no';
1075 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1076 {
1077 if (commitSetInService ($rs_id, $newval))
1078 $ngood++;
1079 else
1080 $nbad++;
1081 }
1082 }
1083 if (!$nbad)
103b1e1e 1084 return buildRedirectURL_OK ($ngood . " real server(s) were successfully (de)activated");
1f7d18fa 1085 else
103b1e1e 1086 return buildRedirectURL_ERR ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1f7d18fa
DO
1087}
1088
03eb5209
DO
1089function importPTRData ()
1090{
2c6c7645 1091 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1092 $nbad = $ngood = 0;
03eb5209
DO
1093 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1094 {
3f3bd41e 1095 $inputname = "import_${i}";
03eb5209
DO
1096 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1097 continue;
2c6c7645
DO
1098 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1099 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1100 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1101 // Non-existent addresses will not have this argument set in request.
1102 $rsvd = 'no';
03eb5209
DO
1103 if ($_REQUEST["rsvd_${i}"] == 'yes')
1104 $rsvd = 'yes';
3f3bd41e 1105 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1106 $ngood++;
1107 else
1108 $nbad++;
1109 }
1110 if (!$nbad)
103b1e1e 1111 return buildRedirectURL_OK ($ngood . " IP address(es) were successfully updated");
03eb5209 1112 else
103b1e1e 1113 return buildRedirectURL_ERR ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1114}
1115
f3f0161f
DO
1116function generateAutoPorts ()
1117{
103b1e1e 1118 global $pageno;
2c6c7645 1119 assertUIntArg ('object_id', __FUNCTION__);
103b1e1e 1120 $info = getObjectInfo ($_REQUEST['object_id']);
f3f0161f 1121 // Navigate away in case of success, stay at the place otherwise.
103b1e1e
DO
1122 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1123 return buildRedirectURL_OK ('Generation complete', $pageno, 'ports');
f3f0161f 1124 else
103b1e1e 1125 return buildRedirectURL_ERR ('executeAutoPorts() failed');
f3f0161f
DO
1126}
1127
24cbe8af 1128// Filter out implicit tags before storing the new tag set.
2034d968 1129function saveEntityTags ($realm, $bypass)
24cbe8af 1130{
103b1e1e 1131 global $explicit_tags, $implicit_tags;
2c6c7645 1132 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1133 $entity_id = $_REQUEST[$bypass];
e04931c8 1134 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1135 // Build a chain from the submitted data, minimize it,
ab379543 1136 // then wipe existing records and store the new set instead.
49fb1027 1137 deleteTagsForEntity ($realm, $entity_id);
6e49bd1f 1138 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1139 $n_succeeds = $n_errors = 0;
6e49bd1f 1140 foreach ($newchain as $taginfo)
eb6ea26f 1141 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1142 $n_succeeds++;
1143 else
1144 $n_errors++;
ab379543 1145 if ($n_errors)
6e49bd1f 1146 return buildRedirectURL_ERR ("Tried chaining ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1147 else
6e49bd1f 1148 return buildRedirectURL_OK ("Chained ${n_succeeds} tags");
2034d968
DO
1149}
1150
1151function saveObjectTags ()
1152{
1153 return saveEntityTags ('object', 'object_id');
1154}
1155
1156function saveIPv4PrefixTags ()
1157{
1158 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1159}
1160
41bd8c87
DO
1161function saveRackTags ()
1162{
1163 return saveEntityTags ('rack', 'rack_id');
1164}
1165
d817ba03
DO
1166function saveIPv4VSTags ()
1167{
1a4beb9e 1168 return saveEntityTags ('ipv4vs', 'vs_id');
d817ba03
DO
1169}
1170
1171function saveIPv4RSPoolTags ()
1172{
2e4170aa 1173 return saveEntityTags ('ipv4rspool', 'pool_id');
d817ba03
DO
1174}
1175
cced6b7d
DO
1176function saveUserTags ()
1177{
1178 return saveEntityTags ('user', 'user_id');
1179}
1180
fe7044ad
DO
1181function destroyTag ()
1182{
49fb1027
DO
1183 assertUIntArg ('tag_id', __FUNCTION__);
1184 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
103b1e1e 1185 return buildRedirectURL_OK ('Successfully deleted tag.');
fe7044ad 1186 else
103b1e1e 1187 return buildRedirectURL_ERR ("Error deleting tag: '${ret}'");
fe7044ad
DO
1188}
1189
1190function createTag ()
1191{
49fb1027 1192 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1193 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1194 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1195 if (!validTagName ($tagname))
1196 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
0f3c6639
DO
1197 if (tagExistsInDatabase ($tagname))
1198 return buildRedirectURL_ERR ("Tag '${tagname}' (or similar name) already exists");
fe7044ad
DO
1199 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1200 $parent_id = 'NULL';
1201 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
103b1e1e 1202 return buildRedirectURL_OK ("Created tag '${tagname}'.");
fe7044ad 1203 else
103b1e1e 1204 return buildRedirectURL_ERR ("Could not create tag '${tagname}' because of error '${ret}'");
fe7044ad
DO
1205}
1206
49fb1027
DO
1207function updateTag ()
1208{
49fb1027
DO
1209 assertUIntArg ('tag_id', __FUNCTION__);
1210 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1211 assertStringArg ('tag_name', __FUNCTION__);
1212 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1213 if (!validTagName ($tagname))
1214 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
49fb1027
DO
1215 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1216 $parent_id = 'NULL';
1217 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
103b1e1e 1218 return buildRedirectURL_OK ("Updated tag '${tagname}'.");
49fb1027 1219 else
103b1e1e 1220 return buildRedirectURL_ERR ("Could not update tag '${tagname}' because of error '${ret}'");
49fb1027
DO
1221}
1222
eb6ea26f
DO
1223function rollTags ()
1224{
eb6ea26f
DO
1225 assertUIntArg ('row_id', __FUNCTION__);
1226 assertStringArg ('sum', __FUNCTION__, TRUE);
1227 assertUIntArg ('realsum', __FUNCTION__);
1228 $row_id = $_REQUEST['row_id'];
1229 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
103b1e1e 1230 return buildRedirectURL_ERR ('Turing test failed');
eb6ea26f
DO
1231 $racks = getRacksForRow ($row_id);
1232 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1233 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1234 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
eb6ea26f
DO
1235 $ndupes = $nnew = 0;
1236 foreach ($tagstack as $taginfo)
1237 foreach ($racks as $rackInfo)
1238 {
1239 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1240 $nnew++;
1241 else
1242 $ndupes++;
1243 // FIXME: do something likewise for all object inside current rack
1244 }
103b1e1e 1245 return buildRedirectURL_OK ("${nnew} new records done, ${ndupes} already existed");
eb6ea26f
DO
1246}
1247
9457ca59 1248function changeMyPassword ()
cced6b7d 1249{
103b1e1e 1250 global $accounts, $remote_username;
9457ca59
DO
1251 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1252 return buildRedirectURL_ERR ('Can only change password under DB authentication.');
0ac7e4b3
DO
1253 assertStringArg ('oldpassword', __FUNCTION__);
1254 assertStringArg ('newpassword1', __FUNCTION__);
1255 assertStringArg ('newpassword2', __FUNCTION__);
1256 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
9457ca59 1257 return buildRedirectURL_ERR ('Old password doesn\'t match.');
cced6b7d 1258 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
9457ca59 1259 return buildRedirectURL_ERR ('New passwords don\'t match.');
0ac7e4b3 1260 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
9457ca59 1261 return buildRedirectURL_OK ('Password changed successfully.');
cced6b7d 1262 else
9457ca59 1263 return buildRedirectURL_ERR ('Password change failed.');
cced6b7d
DO
1264}
1265
1266function saveRackCode ()
1267{
cced6b7d 1268 assertStringArg ('rackcode');
e6a4adb9
DO
1269 // For the test to succeed, unescape LFs, strip CRs.
1270 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1271 $parseTree = getRackCode ($newcode);
1272 if ($parseTree['result'] != 'ACK')
103b1e1e 1273 return buildRedirectURL_ERR ('Verification failed: ' . $parseTree['load']);
4a6a28f1 1274 saveScript ('RackCodeCache', '');
cf25e649 1275 if (saveScript ('RackCode', $newcode))
103b1e1e 1276 return buildRedirectURL_OK ('Saved successfully.');
cced6b7d 1277 else
103b1e1e 1278 return buildRedirectURL_ERR ('Save failed.');
cced6b7d
DO
1279}
1280
46f92ff7
DO
1281// This handler's context is pre-built, but not authorized. It is assumed, that the
1282// handler will take existing context and before each commit check authorization
1283// on the base chain plus necessary context added.
1284function setPortVLAN ()
1285{
1286 assertUIntArg ('portcount', __FUNCTION__);
1287 $data = getSwitchVLANs ($_REQUEST['object_id']);
1288 if ($data === NULL)
1289 return buildRedirectURL_ERR ('getSwitchVLANs() failed');
1290 list ($vlanlist, $portlist) = $data;
1291 // Here we just build up 1 set command for the gateway with all of the ports
1292 // included. The gateway is expected to filter unnecessary changes silently
1293 // and to provide a list of responses with either error or success message
1294 // for each of the rest.
1295 $nports = $_REQUEST['portcount'];
1296 $prefix = 'set ';
1297 $log = array();
1298 $setcmd = '';
1299 for ($i = 0; $i < $nports; $i++)
1300 if
1301 (
1302 !isset ($_REQUEST['portname_' . $i]) ||
1303 !isset ($_REQUEST['vlanid_' . $i]) ||
1304 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1305 )
1306 $log[] = array ('code' => 'error', 'message' => "Ignoring malformed record #${i} in form submit");
1307 elseif
1308 (
1309 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1310 $portlist[$i]['vlaind'] == 'TRUNK'
1311 )
1312 continue;
1313 else
1314 {
1315 $portname = $_REQUEST['portname_' . $i];
1316 $oldvlanid = $portlist[$i]['vlanid'];
1317 $newvlanid = $_REQUEST['vlanid_' . $i];
1318 // Finish the security context and evaluate it.
1319 $annex = array();
1320 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1321 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1322 if (!permitted (NULL, NULL, NULL, $annex))
1323 {
1324 $log[] = array ('code' => 'error', 'message' => "Permission denied moving port ${portname} from VLAN${oldvlanid} to VLAN${newvlanid}");
1325 continue;
1326 }
1327 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1328 $prefix = ';';
1329 }
1330 // Feed the gateway and interpret its (non)response.
1331 if ($setcmd != '')
1332 $log = array_merge ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1333 else
1334 $log[] = array ('code' => 'warning', 'message' => 'nothing happened...');
1335 return buildWideRedirectURL ($log);
1336}
1337
e673ee24 1338?>