4 * This file is a library of operation handlers for RackTables.
8 function addPortForwarding ()
10 global $root, $pageno, $tabno;
12 assertUIntArg ('object_id');
13 assertIPv4Arg ('localip');
14 assertIPv4Arg ('remoteip');
15 assertUIntArg ('localport');
16 assertUIntArg ('proto');
17 assertStringArg ('description', TRUE);
18 $object_id = $_REQUEST['object_id'];
19 $localip = $_REQUEST['localip'];
20 $remoteip = $_REQUEST['remoteip'];
21 $localport = $_REQUEST['localport'];
22 $remoteport = $_REQUEST['remoteport'];
23 $proto = $_REQUEST['proto'];
24 $description = $_REQUEST['description'];
25 if (empty ($remoteport))
26 $remoteport = $localport;
28 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
31 $error=newPortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
34 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
37 return "${retpage}&error=".urlencode($error);
42 function delPortForwarding ()
44 global $root, $pageno, $tabno;
46 $object_id = $_REQUEST['object_id'];
47 $localip = $_REQUEST['localip'];
48 $remoteip = $_REQUEST['remoteip'];
49 $localport = $_REQUEST['localport'];
50 $remoteport = $_REQUEST['remoteport'];
51 $proto = $_REQUEST['proto'];
53 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
55 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
57 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
60 return "${retpage}&error=".urlencode($error);
65 function updPortForwarding ()
67 global $root, $pageno, $tabno;
69 $object_id = $_REQUEST['object_id'];
70 $localip = $_REQUEST['localip'];
71 $remoteip = $_REQUEST['remoteip'];
72 $localport = $_REQUEST['localport'];
73 $remoteport = $_REQUEST['remoteport'];
74 $proto = $_REQUEST['proto'];
75 $description = $_REQUEST['description'];
77 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
79 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
81 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
84 return "${retpage}&error=".urlencode($error);
89 function addPortForObject ()
91 global $root, $pageno, $tabno;
93 $object_id = $_REQUEST['object_id'];
94 $port_name = $_REQUEST['port_name'];
95 $port_l2address = $_REQUEST['port_l2address'];
96 $port_label = $_REQUEST['port_label'];
97 $port_type_id = $_REQUEST['port_type_id'];
100 if ($port_name == '')
101 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
104 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
107 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
111 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
117 function editPortForObject ()
119 global $root, $pageno, $tabno;
121 assertUIntArg ('port_id');
122 assertUIntArg ('object_id');
123 assertStringArg ('name');
124 $port_id = $_REQUEST['port_id'];
125 $object_id = $_REQUEST['object_id'];
126 $port_name = $_REQUEST['name'];
127 $port_l2address = $_REQUEST['l2address'];
128 $port_label = $_REQUEST['label'];
129 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
130 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
134 if ($port_name == '')
135 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
138 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
141 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
145 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
151 function delPortFromObject ()
153 global $root, $pageno, $tabno;
155 $port_id = $_REQUEST['port_id'];
156 $port_name = $_REQUEST['port_name'];
157 $object_id = $_REQUEST['object_id'];
158 $error = delObjectPort($port_id);
162 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
166 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
170 function linkPortForObject ()
172 global $root, $pageno, $tabno;
174 $port_id = $_REQUEST['port_id'];
175 $remote_port_id = $_REQUEST['remote_port_id'];
176 $object_id = $_REQUEST['object_id'];
177 $port_name = $_REQUEST['port_name'];
178 $remote_port_name = $_REQUEST['remote_port_name'];
179 $remote_object_name = $_REQUEST['remote_object_name'];
181 $error = linkPorts($port_id, $remote_port_id);
184 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
188 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully linked with port $remote_port_name at object $remote_object_name");
192 function unlinkPortForObject ()
194 global $root, $pageno, $tabno;
196 $port_id = $_REQUEST['port_id'];
197 $object_id = $_REQUEST['object_id'];
198 $port_name = $_REQUEST['port_name'];
199 $remote_port_name = $_REQUEST['remote_port_name'];
200 $remote_object_name = $_REQUEST['remote_object_name'];
202 $error = unlinkPort($port_id);
205 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
209 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully unlinked from port $remote_port_name at object $remote_object_name");
213 function addMultiPorts ()
215 global $root, $pageno, $tabno;
217 assertStringArg ('format');
218 assertStringArg ('input');
219 assertUIntArg ('port_type');
220 assertUIntArg ('object_id');
221 $format = $_REQUEST['format'];
222 $port_type = $_REQUEST['port_type'];
223 $object_id = $_REQUEST['object_id'];
224 // Input lines are escaped, so we have to explode and to chop by 2-char
225 // \n and \r respectively.
226 $lines1 = explode ('\n', $_REQUEST['input']);
227 foreach ($lines1 as $line)
229 $parts = explode ('\r', $line);
231 if (empty ($parts[0]))
234 $lines2[] = rtrim ($parts[0]);
237 foreach ($lines2 as $line)
242 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
243 list ($slot, $port) = explode ('/', $words[0]);
246 'name' => "e ${slot}/${port}",
247 'l2address' => $words[8],
248 'label' => "slot ${slot} port ${port}"
252 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
254 How Async Lines are Numbered in Cisco 3600 Series Routers
255 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
257 Understanding 16- and 32-Port Async Network Modules
258 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
261 $slot = floor (($async - 1) / 32);
262 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
263 $cable = $async - $slot * 32 - $octalgroup * 8;
264 $og_label[0] = 'async 0-7';
265 $og_label[1] = 'async 8-15';
266 $og_label[2] = 'async 16-23';
267 $og_label[3] = 'async 24-31';
270 'name' => "async ${async}",
272 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
276 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
277 $ifnumber = $words[0] * 1;
280 'name' => "e ${ifnumber}",
281 'l2address' => "${words[8]}",
282 'label' => "${ifnumber}"
286 $words = explode (' ', $line);
287 if (empty ($words[0]) or empty ($words[1]))
292 'l2address' => $words[1],
298 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
299 urlencode('Cannot process submitted data: unknown format code.');
303 // Create ports, if they don't exist.
304 $added_count = $updated_count = $error_count = 0;
305 foreach ($ports as $port)
307 $port_id = getPortID ($object_id, $port['name']);
308 if ($port_id === NULL)
310 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
318 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
326 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
327 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
330 function editAddressFromObject ()
334 $ip = $_REQUEST['ip'];
335 $object_id = $_REQUEST['object_id'];
336 $name = $_REQUEST['bond_name'];
337 $type = $_REQUEST['bond_type'];
338 $error = updateBond($ip, $object_id, $name, $type);
341 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
345 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
349 function delAddressFromObject ()
353 $ip = $_REQUEST['ip'];
354 $object_id = $_REQUEST['object_id'];
355 $error = unbindIpFromObject($ip, $object_id);
358 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
362 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
366 function delIpAssignment ()
370 $ip = $_REQUEST['ip'];
371 $object_id = $_REQUEST['object_id'];
372 $error = unbindIpFromObject($ip, $object_id);
375 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
379 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
383 function editIpAssignment ()
387 $ip = $_REQUEST['ip'];
388 $object_id = $_REQUEST['object_id'];
389 $name = $_REQUEST['bond_name'];
390 $type = $_REQUEST['bond_type'];
391 $error = updateBond($ip, $object_id, $name, $type);
395 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
399 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
403 function addIpAssignment ()
407 $ip = $_REQUEST['ip'];
408 $object_id = $_REQUEST['object_id'];
409 $name = $_REQUEST['bond_name'];
410 $type = $_REQUEST['bond_type'];
411 $error = bindIpToObject($ip, $object_id, $name, $type);
414 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
418 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
422 function addNewRange ()
424 global $root, $pageno, $tabno;
426 $range = $_REQUEST['range'];
427 $name = $_REQUEST['name'];
428 $is_bcast = $_REQUEST['is_bcast'];
429 $error = addRange($range, $name, $is_bcast == 'on');
432 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
436 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
440 function editRange ()
442 global $root, $pageno, $tabno;
444 $id = $_REQUEST['id'];
445 $name = $_REQUEST['name'];
446 $error = updateRange($id, $name);
449 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
453 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
460 global $root, $pageno, $tabno;
462 $id = $_REQUEST['id'];
463 $error = commitDeleteRange ($id);
466 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
470 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
475 function editAddress ()
477 global $root, $pageno, $tabno;
479 $ip = $_REQUEST['ip'];
480 $name = $_REQUEST['name'];
481 if (isset ($_REQUEST['reserved']))
482 $reserved = $_REQUEST['reserved'];
485 $error = updateAddress($ip, $name, $reserved=='on'?
'yes':'no');
488 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
492 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
497 function addAddressToObject ()
499 global $root, $pageno, $tabno;
501 assertStringArg ('ip');
502 assertUIntArg ('object_id');
503 assertStringArg ('name', TRUE);
504 assertStringArg ('type');
506 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
507 $object_id = $_REQUEST['object_id'];
508 $error = bindIpToObject($ip, $object_id, $_REQUEST['name'], $_REQUEST['type']);
510 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
512 return "${root}?page=$pageno&tab=${tabno}&object_id=${object_id}&message=".
513 urlencode("Address ${ip} was added successfully.");
516 function createUserAccount ()
518 global $root, $pageno, $tabno;
519 assertStringArg ('username');
520 assertStringArg ('realname', TRUE);
521 assertStringArg ('password');
522 $username = $_REQUEST['username'];
523 $password = hash (PASSWORD_HASH
, $_REQUEST['password']);
524 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
526 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
528 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
531 function updateUserAccount ()
533 global $root, $pageno, $tabno;
534 assertUIntArg ('id');
535 assertStringArg ('username');
536 assertStringArg ('realname', TRUE);
537 assertStringArg ('password');
538 // We might be asked to change username, so use user ID only.
539 $id = $_REQUEST['id'];
540 $username = $_REQUEST['username'];
541 $new_password = $_REQUEST['password'];
542 $old_hash = getHashByID ($id);
543 if ($old_hash == NULL)
545 showError ('getHashByID() failed', __FUNCTION__
);
548 // Update user password only if provided password is not the same as current password hash.
549 if ($new_password != $old_hash)
550 $new_password = hash (PASSWORD_HASH
, $new_password);
551 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
553 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
555 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
558 function enableUserAccount ()
560 global $root, $pageno, $tabno;
561 assertUIntArg ('id');
562 $id = $_REQUEST['id'];
563 $result = commitEnableUserAccount ($id, 'yes');
565 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
567 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
570 function disableUserAccount ()
572 global $root, $pageno, $tabno;
573 assertUIntArg ('id');
574 $id = $_REQUEST['id'];
578 $result = commitEnableUserAccount ($id, 'no');
580 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
582 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
585 function revokePermission ()
587 global $root, $pageno, $tabno;
588 assertUIntArg ('access_userid', TRUE);
589 assertStringArg ('access_page');
590 assertStringArg ('access_tab');
591 $result = commitRevokePermission
593 $_REQUEST['access_userid'],
594 $_REQUEST['access_page'],
595 $_REQUEST['access_tab']
598 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
600 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
603 function grantPermission ()
605 global $root, $pageno, $tabno;
606 assertUIntArg ('access_userid', TRUE);
607 assertStringArg ('access_page');
608 assertStringArg ('access_tab');
609 assertStringArg ('access_value');
610 $result = commitGrantPermission
612 $_REQUEST['access_userid'],
613 $_REQUEST['access_page'],
614 $_REQUEST['access_tab'],
615 $_REQUEST['access_value']
618 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
620 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
623 // This function find differences in users's submit and PortCompat table
624 // and modifies database accordingly.
625 function savePortMap ()
627 global $root, $pageno, $tabno;
628 $ptlist = getPortTypes();
629 $oldCompat = getPortCompat();
630 $newCompat = array();
631 foreach (array_keys ($ptlist) as $leftKey)
632 foreach (array_keys ($ptlist) as $rightKey)
633 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
634 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
635 // Build the new matrix from user's submit and compare it to
636 // the old matrix. Those pairs which appear on
637 // new matrix only, have to be stored in PortCompat table. Those who appear
638 // on the old matrix only, should be removed from PortCompat table.
639 // Those present in both matrices should be skipped.
640 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
641 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
642 $error_count = $success_count = 0;
643 foreach (array_keys ($ptlist) as $type1)
644 foreach (array_keys ($ptlist) as $type2)
645 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
646 switch ($oldCompatTable[$type1][$type2])
648 case TRUE: // new value is FALSE
649 if (removePortCompat ($type1, $type2) === TRUE)
654 case FALSE: // new value is TRUE
655 if (addPortCompat ($type1, $type2) === TRUE)
661 showError ('oldCompatTable is invalid', __FUNCTION__
);
665 "${root}?page=${pageno}&tab=${tabno}&" .
666 ($error_count == 0 ?
'message' : 'error') .
667 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
670 function deleteDictWord ()
672 global $root, $pageno, $tabno;
674 "${root}?page=${pageno}&tab=${tabno}&" .
675 "error=" . urlencode ('Dragon ate this word!');
678 function updateDictionary ()
680 global $root, $pageno, $tabno;
681 assertUIntArg ('chapter_no');
682 assertUIntArg ('dict_key');
683 assertStringArg ('dict_value');
684 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
685 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
687 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
690 function supplementDictionary ()
692 global $root, $pageno, $tabno;
693 assertUIntArg ('chapter_no');
694 assertStringArg ('dict_value');
695 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
696 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
698 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
701 function reduceDictionary ()
703 global $root, $pageno, $tabno;
704 assertUIntArg ('chapter_no');
705 assertUIntArg ('dict_key');
706 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
707 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
709 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
712 function addChapter ()
714 global $root, $pageno, $tabno;
715 assertStringArg ('chapter_name');
716 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
717 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
719 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
722 function updateChapter ()
724 global $root, $pageno, $tabno;
725 assertUIntArg ('chapter_no');
726 assertStringArg ('chapter_name');
727 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
728 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
730 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
733 function delChapter ()
735 global $root, $pageno, $tabno;
736 assertUIntArg ('chapter_no');
737 if (commitDeleteChapter ($_REQUEST['chapter_no']))
738 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
740 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
743 function changeAttribute ()
745 global $root, $pageno, $tabno;
746 assertUIntArg ('attr_id');
747 assertStringArg ('attr_name');
748 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
749 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
751 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
754 function createAttribute ()
756 global $root, $pageno, $tabno;
757 assertStringArg ('attr_name');
758 assertStringArg ('attr_type');
759 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
760 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
762 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
765 function deleteAttribute ()
767 global $root, $pageno, $tabno;
768 assertUIntArg ('attr_id');
769 if (commitDeleteAttribute ($_REQUEST['attr_id']))
770 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
772 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
775 function supplementAttrMap ()
777 global $root, $pageno, $tabno;
778 assertUIntArg ('attr_id');
779 assertUIntArg ('objtype_id');
780 assertUIntArg ('chapter_no');
781 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
782 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
784 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
787 function reduceAttrMap ()
789 global $root, $pageno, $tabno;
790 assertUIntArg ('attr_id');
791 assertUIntArg ('objtype_id');
792 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
793 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
795 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
798 function resetAttrValue ()
800 global $root, $pageno, $tabno;
801 assertUIntArg ('attr_id');
802 assertUIntArg ('object_id');
803 $object_id = $_REQUEST['object_id'];
804 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
805 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
807 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
810 function updateAttrValues ()
812 global $root, $pageno, $tabno;
813 assertUIntArg ('object_id');
814 $object_id = $_REQUEST['object_id'];
815 $oldvalues = getAttrValues ($object_id);
817 assertUIntArg ('num_attrs');
818 $num_attrs = $_REQUEST['num_attrs'];
821 for ($i = 0; $i < $num_attrs; $i++
)
823 assertUIntArg ("${i}_attr_id");
824 $attr_id = $_REQUEST["${i}_attr_id"];
826 // Field is empty, delete attribute and move on.
827 if (empty($_REQUEST["${i}_value"]))
829 commitResetAttrValue ($object_id, $attr_id);
833 // The value could be uint/float, but we don't know ATM. Let SQL
834 // server check this and complain.
835 assertStringArg ("${i}_value");
836 $value = $_REQUEST["${i}_value"];
837 switch ($oldvalues[$attr_id]['type'])
842 $oldvalue = $oldvalues[$attr_id]['value'];
845 $oldvalue = $oldvalues[$attr_id]['key'];
848 showError ('Internal structure error', __FUNCTION__
);
851 if ($value == $oldvalue)
854 // Note if the queries succeed or not, it determines which page they see.
855 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
858 if (in_array(false, $result))
859 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
861 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
864 function useupPort ()
866 global $root, $pageno, $tabno;
867 assertUIntArg ('port_id');
868 assertUIntArg ('object_id');
869 $object_id = $_REQUEST['object_id'];
870 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
871 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
873 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
878 global $root, $pageno, $tabno;
879 $oldvalues = loadConfigCache();
881 assertUIntArg ('num_vars');
882 $num_vars = $_REQUEST['num_vars'];
885 for ($i = 0; $i < $num_vars; $i++
)
887 assertStringArg ("${i}_varname");
888 assertStringArg ("${i}_varvalue", TRUE);
889 $varname = $_REQUEST["${i}_varname"];
890 $varvalue = $_REQUEST["${i}_varvalue"];
892 // If form value = value in DB, don't bother updating DB
893 if ($varvalue == getConfigVar ($varname))
896 // Note if the queries succeed or not, it determines which page they see.
897 $error = setConfigVar ($varname, $varvalue, TRUE);
903 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
905 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
908 function resetUIConfig()
910 global $root, $pageno, $tabno;
911 setConfigVar ('default_port_type','24');
912 setConfigVar ('MASSCOUNT','15');
913 setConfigVar ('MAXSELSIZE','30');
914 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
915 setConfigVar ('ROW_SCALE','2');
916 setConfigVar ('PORTS_PER_ROW','12');
917 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
918 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
919 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
920 setConfigVar ('USER_AUTH_SRC','database');
921 setConfigVar ('DEFAULT_SLB_VS_PORT','');
922 setConfigVar ('DEFAULT_SLB_RS_PORT','');
923 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
924 setConfigVar ('NATV4_PERFORMERS','4,7,8');
925 setConfigVar ('DETECT_URLS','no');
926 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
927 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
928 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
929 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
930 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
933 // Add single record.
934 function addRealServer ()
936 global $root, $pageno, $tabno;
938 assertUIntArg ('id');
939 assertIPv4Arg ('remoteip');
940 assertUIntArg ('rsport');
941 assertStringArg ('rsconfig', TRUE);
942 $pool_id = $_REQUEST['id'];
943 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
944 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
946 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully added");
949 // Parse textarea submitted and try adding a real server for each line.
950 function addRealServers ()
952 global $root, $pageno, $tabno;
954 assertUIntArg ('id');
955 assertStringArg ('format');
956 assertStringArg ('rawtext');
957 $pool_id = $_REQUEST['id'];
958 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
961 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
962 foreach (explode ('\n', $rawtext) as $line)
967 switch ($_REQUEST['format'])
969 case 'ipvs_2': // address and port only
970 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) /', $line, $match))
972 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
977 case 'ipvs_3': // address, port and weight
978 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
980 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
985 case 'ssv_2': // IP address and port
986 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
988 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
994 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__
. ': invalid format requested');
998 if ($nbad == 0 and $ngood > 0)
999 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1001 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1004 function addVService ()
1006 global $root, $pageno, $tabno;
1008 assertIPv4Arg ('vip');
1009 assertUIntArg ('vport');
1010 assertStringArg ('proto');
1011 $proto = $_REQUEST['proto'];
1012 if ($proto != 'TCP' and $proto != 'UDP')
1013 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__
. ': invalid protocol');
1014 assertStringArg ('name', TRUE);
1015 assertStringArg ('vsconfig', TRUE);
1016 assertStringArg ('rsconfig', TRUE);
1017 $pool_id = $_REQUEST['id'];
1018 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1019 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
1021 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
1024 function deleteRealServer ()
1026 global $root, $pageno, $tabno;
1028 assertUIntArg ('pool_id');
1029 assertUIntArg ('id');
1030 $pool_id = $_REQUEST['pool_id'];
1031 if (!commitDeleteRS ($_REQUEST['id']))
1032 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
1034 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
1037 function deleteLoadBalancer ()
1039 global $root, $pageno, $tabno;
1041 assertUIntArg ('object_id');
1042 assertUIntArg ('pool_id');
1043 assertUIntArg ('vs_id');
1044 $pool_id = $_REQUEST['pool_id'];
1045 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
1046 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1048 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1051 function deleteVService ()
1053 global $root, $pageno, $tabno;
1055 assertUIntArg ('id');
1056 if (!commitDeleteVS ($_REQUEST['id']))
1057 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
1059 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
1062 function updateRealServer ()
1064 global $root, $pageno, $tabno;
1066 assertUIntArg ('id');
1067 assertUIntArg ('rs_id');
1068 assertIPv4Arg ('rsip');
1069 assertUIntArg ('rsport');
1070 assertStringArg ('rsconfig', TRUE);
1071 // only necessary for generating next URL
1072 $pool_id = $_REQUEST['id'];
1073 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
1074 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
1076 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1079 function updateLoadBalancer ()
1081 global $root, $pageno, $tabno;
1083 assertUIntArg ('object_id');
1084 assertUIntArg ('pool_id');
1085 assertUIntArg ('vs_id');
1086 assertStringArg ('vsconfig', TRUE);
1087 assertStringArg ('rsconfig', TRUE);
1088 $pool_id = $_REQUEST['pool_id'];
1089 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1090 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1092 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
1095 function updateVService ()
1097 global $root, $pageno, $tabno;
1099 assertUIntArg ('id');
1100 assertIPv4Arg ('vip');
1101 assertUIntArg ('vport');
1102 assertStringArg ('proto');
1103 assertStringArg ('name', TRUE);
1104 assertStringArg ('vsconfig', TRUE);
1105 assertStringArg ('rsconfig', TRUE);
1106 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1107 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
1109 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
1112 function addLoadBalancer ()
1114 global $root, $pageno, $tabno;
1116 assertUIntArg ('pool_id');
1117 assertUIntArg ('object_id');
1118 assertUIntArg ('vs_id');
1119 assertStringArg ('vsconfig', TRUE);
1120 assertStringArg ('rsconfig', TRUE);
1121 $pool_id = $_REQUEST['pool_id'];
1122 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1123 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
1125 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
1128 function addRSPool ()
1130 global $root, $pageno, $tabno;
1132 assertStringArg ('name', TRUE);
1133 assertStringArg ('vsconfig', TRUE);
1134 assertStringArg ('rsconfig', TRUE);
1135 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1136 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1138 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1141 function deleteRSPool ()
1143 global $root, $pageno, $tabno;
1145 assertUIntArg ('id');
1146 if (!commitDeleteRSPool ($_REQUEST['id']))
1147 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1149 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1152 function updateRSPool ()
1154 global $root, $pageno, $tabno;
1156 assertUIntArg ('id');
1157 assertStringArg ('name', TRUE);
1158 assertStringArg ('vsconfig', TRUE);
1159 assertStringArg ('rsconfig', TRUE);
1160 if (!commitUpdateRSPool ($_REQUEST['id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1161 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1163 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1166 function updateRSInService ()
1168 global $root, $pageno, $tabno;
1169 assertUIntArg ('id');
1170 assertUIntArg ('rscount');
1171 $pool_id = $_REQUEST['id'];
1172 $orig = getRSPoolInfo ($pool_id);
1174 for ($i = 1; $i <= $_REQUEST['rscount']; $i++
)
1176 $rs_id = $_REQUEST["rsid_${i}"];
1177 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1181 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1183 if (commitSetInService ($rs_id, $newval))
1190 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1192 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1195 function importPTRData ()
1197 global $root, $pageno, $tabno;
1198 assertUIntArg ('id');
1199 assertUIntArg ('addrcount');
1201 $id = $_REQUEST['id'];
1202 for ($i = 0; $i < $_REQUEST['addrcount']; $i++
)
1204 $inputname = "import_${i}";
1205 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1207 assertIPv4Arg ("addr_${i}");
1208 assertStringArg ("descr_${i}", TRUE);
1209 assertStringArg ("rsvd_${i}");
1210 // Non-existent addresses will not have this argument set in request.
1212 if ($_REQUEST["rsvd_${i}"] == 'yes')
1214 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1220 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
1222 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
1225 function generateAutoPorts ()
1227 global $root, $pageno, $tabno;
1228 assertUIntArg ('object_id');
1229 $object_id = $_REQUEST['object_id'];
1230 $info = getObjectInfo ($object_id);
1231 // Navigate away in case of success, stay at the place otherwise.
1232 if (executeAutoPorts ($object_id, $info['objtype_id']))
1233 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1235 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1238 // Filter out implicit tags before storing the new tag set.
1239 function saveObjectTags ()
1241 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
1242 assertUIntArg ('object_id');
1243 $object_id = $_REQUEST['object_id'];
1244 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('This is a dummy message.');
1245 // FIXME: build a trail from the submitted data, minimize it;
1246 // then find and handle any differences between the result
1247 // and current explicit tag set (or wipe existing records and
1248 // store the whole new set instead).
1250 foreach ($_REQUEST['taglist'] as $tag_id)
1252 // if (tagOnTrail ($explicit