9607f991eaa1fb66da23ebc97a9f53982da667aa
4 * This file is a library of operation handlers for RackTables.
8 // This function assures that specified argument was passed
9 // and is a number greater than zero.
10 function assertUIntArg ($argname, $allow_zero = FALSE)
12 if (!isset ($_REQUEST[$argname]))
14 showError ("Parameter '${argname}' is missing.", __FUNCTION__
);
17 if (!is_numeric ($_REQUEST[$argname]))
19 showError ("Parameter '${argname}' is not a number.", __FUNCTION__
);
22 if ($_REQUEST[$argname] < 0)
24 showError ("Parameter '${argname}' is less than zero.", __FUNCTION__
);
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
29 showError ("Parameter '${argname}' is equal to zero.", __FUNCTION__
);
34 // This function assures that specified argument was passed
35 // and is a non-empty string.
36 function assertStringArg ($argname, $ok_if_empty = FALSE)
38 if (!isset ($_REQUEST[$argname]))
40 showError ("Parameter '${argname}' is missing.", __FUNCTION__
);
43 if (!is_string ($_REQUEST[$argname]))
45 showError ("Parameter '${argname}' is not a string.", __FUNCTION__
);
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
50 showError ("Parameter '${argname}' is an empty string.", __FUNCTION__
);
55 function assertBoolArg ($argname, $ok_if_empty = FALSE)
57 if (!isset ($_REQUEST[$argname]))
59 showError ("Parameter '${argname}' is missing.", __FUNCTION__
);
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
64 showError ("Parameter '${argname}' is not a string.", __FUNCTION__
);
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
69 showError ("Parameter '${argname}' is an empty string.", __FUNCTION__
);
74 function assertIPv4Arg ($argname, $ok_if_empty = FALSE)
76 assertStringArg ($argname, $ok_if_empty);
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "'", __FUNCTION__
);
84 function addPortForwarding ()
86 global $root, $pageno, $tabno;
88 assertUIntArg ('object_id');
89 assertIPv4Arg ('localip');
90 assertIPv4Arg ('remoteip');
91 assertUIntArg ('localport');
92 assertUIntArg ('proto');
93 assertStringArg ('description', TRUE);
94 $object_id = $_REQUEST['object_id'];
95 $localip = $_REQUEST['localip'];
96 $remoteip = $_REQUEST['remoteip'];
97 $localport = $_REQUEST['localport'];
98 $remoteport = $_REQUEST['remoteport'];
99 $proto = $_REQUEST['proto'];
100 $description = $_REQUEST['description'];
101 if (empty ($remoteport))
102 $remoteport = $localport;
104 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
107 $error=newPortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
110 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
113 return "${retpage}&error=".urlencode($error);
118 function delPortForwarding ()
120 global $root, $pageno, $tabno;
122 $object_id = $_REQUEST['object_id'];
123 $localip = $_REQUEST['localip'];
124 $remoteip = $_REQUEST['remoteip'];
125 $localport = $_REQUEST['localport'];
126 $remoteport = $_REQUEST['remoteport'];
127 $proto = $_REQUEST['proto'];
129 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
131 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
133 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
136 return "${retpage}&error=".urlencode($error);
141 function updPortForwarding ()
143 global $root, $pageno, $tabno;
145 $object_id = $_REQUEST['object_id'];
146 $localip = $_REQUEST['localip'];
147 $remoteip = $_REQUEST['remoteip'];
148 $localport = $_REQUEST['localport'];
149 $remoteport = $_REQUEST['remoteport'];
150 $proto = $_REQUEST['proto'];
151 $description = $_REQUEST['description'];
153 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
155 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
157 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
160 return "${retpage}&error=".urlencode($error);
165 function addPortForObject ()
167 global $root, $pageno, $tabno;
169 $object_id = $_REQUEST['object_id'];
170 $port_name = $_REQUEST['port_name'];
171 $port_l2address = $_REQUEST['port_l2address'];
172 $port_label = $_REQUEST['port_label'];
173 $port_type_id = $_REQUEST['port_type_id'];
176 if ($port_name == '')
177 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
180 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
183 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
187 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
193 function editPortForObject ()
195 global $root, $pageno, $tabno;
197 assertUIntArg ('port_id');
198 assertUIntArg ('object_id');
199 assertStringArg ('name');
200 $port_id = $_REQUEST['port_id'];
201 $object_id = $_REQUEST['object_id'];
202 $port_name = $_REQUEST['name'];
203 $port_l2address = $_REQUEST['l2address'];
204 $port_label = $_REQUEST['label'];
205 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
206 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
210 if ($port_name == '')
211 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
214 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
217 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
221 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
227 function delPortFromObject ()
229 global $root, $pageno, $tabno;
231 $port_id = $_REQUEST['port_id'];
232 $port_name = $_REQUEST['port_name'];
233 $object_id = $_REQUEST['object_id'];
234 $error = delObjectPort($port_id);
238 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
242 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
246 function linkPortForObject ()
248 global $root, $pageno, $tabno;
250 $port_id = $_REQUEST['port_id'];
251 $remote_port_id = $_REQUEST['remote_port_id'];
252 $object_id = $_REQUEST['object_id'];
253 $port_name = $_REQUEST['port_name'];
254 $remote_port_name = $_REQUEST['remote_port_name'];
255 $remote_object_name = $_REQUEST['remote_object_name'];
257 $error = linkPorts($port_id, $remote_port_id);
260 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
264 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");
268 function unlinkPortForObject ()
270 global $root, $pageno, $tabno;
272 $port_id = $_REQUEST['port_id'];
273 $object_id = $_REQUEST['object_id'];
274 $port_name = $_REQUEST['port_name'];
275 $remote_port_name = $_REQUEST['remote_port_name'];
276 $remote_object_name = $_REQUEST['remote_object_name'];
278 $error = unlinkPort($port_id);
281 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
285 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");
289 function addMultiPorts ()
291 global $root, $pageno, $tabno;
293 assertStringArg ('format');
294 assertStringArg ('input');
295 assertUIntArg ('port_type');
296 assertUIntArg ('object_id');
297 $format = $_REQUEST['format'];
298 $port_type = $_REQUEST['port_type'];
299 $object_id = $_REQUEST['object_id'];
300 // Input lines are escaped, so we have to explode and to chop by 2-char
301 // \n and \r respectively.
302 $lines1 = explode ('\n', $_REQUEST['input']);
303 foreach ($lines1 as $line)
305 $parts = explode ('\r', $line);
307 if (empty ($parts[0]))
310 $lines2[] = rtrim ($parts[0]);
313 foreach ($lines2 as $line)
318 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
319 list ($slot, $port) = explode ('/', $words[0]);
322 'name' => "e ${slot}/${port}",
323 'l2address' => $words[8],
324 'label' => "slot ${slot} port ${port}"
328 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
330 How Async Lines are Numbered in Cisco 3600 Series Routers
331 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
333 Understanding 16- and 32-Port Async Network Modules
334 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
337 $slot = floor (($async - 1) / 32);
338 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
339 $cable = $async - $slot * 32 - $octalgroup * 8;
340 $og_label[0] = 'async 0-7';
341 $og_label[1] = 'async 8-15';
342 $og_label[2] = 'async 16-23';
343 $og_label[3] = 'async 24-31';
346 'name' => "async ${async}",
348 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
352 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
353 $ifnumber = $words[0] * 1;
356 'name' => "e ${ifnumber}",
357 'l2address' => "${words[8]}",
358 'label' => "${ifnumber}"
362 $words = explode (' ', $line);
363 if (empty ($words[0]) or empty ($words[1]))
368 'l2address' => $words[1],
374 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
375 urlencode('Cannot process submitted data: unknown format code.');
379 // Create ports, if they don't exist.
380 $added_count = $updated_count = $error_count = 0;
381 foreach ($ports as $port)
383 $port_id = getPortID ($object_id, $port['name']);
384 if ($port_id === NULL)
386 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
394 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
402 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
403 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
406 function editAddressFromObject ()
410 $ip = $_REQUEST['ip'];
411 $object_id = $_REQUEST['object_id'];
412 $name = $_REQUEST['bond_name'];
413 $type = $_REQUEST['bond_type'];
414 $error = updateBond($ip, $object_id, $name, $type);
417 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
421 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
425 function delAddressFromObject ()
429 $ip = $_REQUEST['ip'];
430 $object_id = $_REQUEST['object_id'];
431 $error = unbindIpFromObject($ip, $object_id);
434 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
438 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
442 function delIpAssignment ()
446 $ip = $_REQUEST['ip'];
447 $object_id = $_REQUEST['object_id'];
448 $error = unbindIpFromObject($ip, $object_id);
451 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
455 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
459 function editIpAssignment ()
463 $ip = $_REQUEST['ip'];
464 $object_id = $_REQUEST['object_id'];
465 $name = $_REQUEST['bond_name'];
466 $type = $_REQUEST['bond_type'];
467 $error = updateBond($ip, $object_id, $name, $type);
471 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
475 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
479 function addIpAssignment ()
483 $ip = $_REQUEST['ip'];
484 $object_id = $_REQUEST['object_id'];
485 $name = $_REQUEST['bond_name'];
486 $type = $_REQUEST['bond_type'];
487 $error = bindIpToObject($ip, $object_id, $name, $type);
490 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
494 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
498 function addNewRange ()
500 global $root, $pageno, $tabno;
502 $range = $_REQUEST['range'];
503 $name = $_REQUEST['name'];
504 $is_bcast = $_REQUEST['is_bcast'];
505 $error = addRange($range, $name, $is_bcast == 'on');
508 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
512 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
516 function editRange ()
518 global $root, $pageno, $tabno;
520 $id = $_REQUEST['id'];
521 $name = $_REQUEST['name'];
522 $error = updateRange($id, $name);
525 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
529 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
536 global $root, $pageno, $tabno;
538 $id = $_REQUEST['id'];
539 $error = commitDeleteRange ($id);
542 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
546 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
551 function editAddress ()
553 global $root, $pageno, $tabno;
555 $ip = $_REQUEST['ip'];
556 $name = $_REQUEST['name'];
557 if (isset ($_REQUEST['reserved']))
558 $reserved = $_REQUEST['reserved'];
561 $error = updateAddress($ip, $name, $reserved=='on'?
'yes':'no');
564 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
568 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
573 function addAddressToObject ()
575 global $root, $pageno, $tabno;
577 assertStringArg ('ip');
578 assertUIntArg ('object_id');
579 assertStringArg ('name', TRUE);
580 assertStringArg ('type');
582 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
583 $object_id = $_REQUEST['object_id'];
584 $error = bindIpToObject($ip, $object_id, $_REQUEST['name'], $_REQUEST['type']);
586 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
588 return "${root}?page=$pageno&tab=${tabno}&object_id=${object_id}&message=".
589 urlencode("Address ${ip} was added successfully.");
592 function createUserAccount ()
594 global $root, $pageno, $tabno;
595 assertStringArg ('username');
596 assertStringArg ('realname', TRUE);
597 assertStringArg ('password');
598 $username = $_REQUEST['username'];
599 $password = hash (PASSWORD_HASH
, $_REQUEST['password']);
600 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
602 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
604 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
607 function updateUserAccount ()
609 global $root, $pageno, $tabno;
610 assertUIntArg ('id');
611 assertStringArg ('username');
612 assertStringArg ('realname', TRUE);
613 assertStringArg ('password');
614 // We might be asked to change username, so use user ID only.
615 $id = $_REQUEST['id'];
616 $username = $_REQUEST['username'];
617 $new_password = $_REQUEST['password'];
618 $old_hash = getHashByID ($id);
619 if ($old_hash == NULL)
621 showError ('getHashByID() failed', __FUNCTION__
);
624 // Update user password only if provided password is not the same as current password hash.
625 if ($new_password != $old_hash)
626 $new_password = hash (PASSWORD_HASH
, $new_password);
627 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
629 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
631 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
634 function enableUserAccount ()
636 global $root, $pageno, $tabno;
637 assertUIntArg ('id');
638 $id = $_REQUEST['id'];
639 $result = commitEnableUserAccount ($id, 'yes');
641 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
643 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
646 function disableUserAccount ()
648 global $root, $pageno, $tabno;
649 assertUIntArg ('id');
650 $id = $_REQUEST['id'];
654 $result = commitEnableUserAccount ($id, 'no');
656 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
658 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
661 function revokePermission ()
663 global $root, $pageno, $tabno;
664 assertUIntArg ('access_userid', TRUE);
665 assertStringArg ('access_page');
666 assertStringArg ('access_tab');
667 $result = commitRevokePermission
669 $_REQUEST['access_userid'],
670 $_REQUEST['access_page'],
671 $_REQUEST['access_tab']
674 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
676 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
679 function grantPermission ()
681 global $root, $pageno, $tabno;
682 assertUIntArg ('access_userid', TRUE);
683 assertStringArg ('access_page');
684 assertStringArg ('access_tab');
685 assertStringArg ('access_value');
686 $result = commitGrantPermission
688 $_REQUEST['access_userid'],
689 $_REQUEST['access_page'],
690 $_REQUEST['access_tab'],
691 $_REQUEST['access_value']
694 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
696 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
699 // This function find differences in users's submit and PortCompat table
700 // and modifies database accordingly.
701 function savePortMap ()
703 global $root, $pageno, $tabno;
704 $ptlist = getPortTypes();
705 $oldCompat = getPortCompat();
706 $newCompat = array();
707 foreach (array_keys ($ptlist) as $leftKey)
708 foreach (array_keys ($ptlist) as $rightKey)
709 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
710 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
711 // Build the new matrix from user's submit and compare it to
712 // the old matrix. Those pairs which appear on
713 // new matrix only, have to be stored in PortCompat table. Those who appear
714 // on the old matrix only, should be removed from PortCompat table.
715 // Those present in both matrices should be skipped.
716 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
717 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
718 $error_count = $success_count = 0;
719 foreach (array_keys ($ptlist) as $type1)
720 foreach (array_keys ($ptlist) as $type2)
721 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
722 switch ($oldCompatTable[$type1][$type2])
724 case TRUE: // new value is FALSE
725 if (removePortCompat ($type1, $type2) === TRUE)
730 case FALSE: // new value is TRUE
731 if (addPortCompat ($type1, $type2) === TRUE)
737 showError ('oldCompatTable is invalid', __FUNCTION__
);
741 "${root}?page=${pageno}&tab=${tabno}&" .
742 ($error_count == 0 ?
'message' : 'error') .
743 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
746 function deleteDictWord ()
748 global $root, $pageno, $tabno;
750 "${root}?page=${pageno}&tab=${tabno}&" .
751 "error=" . urlencode ('Dragon ate this word!');
754 function updateDictionary ()
756 global $root, $pageno, $tabno;
757 assertUIntArg ('chapter_no');
758 assertUIntArg ('dict_key');
759 assertStringArg ('dict_value');
760 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
761 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
763 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
766 function supplementDictionary ()
768 global $root, $pageno, $tabno;
769 assertUIntArg ('chapter_no');
770 assertStringArg ('dict_value');
771 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
772 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
774 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
777 function reduceDictionary ()
779 global $root, $pageno, $tabno;
780 assertUIntArg ('chapter_no');
781 assertUIntArg ('dict_key');
782 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
783 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
785 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
788 function addChapter ()
790 global $root, $pageno, $tabno;
791 assertStringArg ('chapter_name');
792 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
793 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
795 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
798 function updateChapter ()
800 global $root, $pageno, $tabno;
801 assertUIntArg ('chapter_no');
802 assertStringArg ('chapter_name');
803 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
804 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
806 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
809 function delChapter ()
811 global $root, $pageno, $tabno;
812 assertUIntArg ('chapter_no');
813 if (commitDeleteChapter ($_REQUEST['chapter_no']))
814 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
816 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
819 function changeAttribute ()
821 global $root, $pageno, $tabno;
822 assertUIntArg ('attr_id');
823 assertStringArg ('attr_name');
824 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
825 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
827 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
830 function createAttribute ()
832 global $root, $pageno, $tabno;
833 assertStringArg ('attr_name');
834 assertStringArg ('attr_type');
835 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
836 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
838 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
841 function deleteAttribute ()
843 global $root, $pageno, $tabno;
844 assertUIntArg ('attr_id');
845 if (commitDeleteAttribute ($_REQUEST['attr_id']))
846 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
848 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
851 function supplementAttrMap ()
853 global $root, $pageno, $tabno;
854 assertUIntArg ('attr_id');
855 assertUIntArg ('objtype_id');
856 assertUIntArg ('chapter_no');
857 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
858 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
860 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
863 function reduceAttrMap ()
865 global $root, $pageno, $tabno;
866 assertUIntArg ('attr_id');
867 assertUIntArg ('objtype_id');
868 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
869 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
871 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
874 function resetAttrValue ()
876 global $root, $pageno, $tabno;
877 assertUIntArg ('attr_id');
878 assertUIntArg ('object_id');
879 $object_id = $_REQUEST['object_id'];
880 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
881 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
883 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
886 function updateAttrValues ()
888 global $root, $pageno, $tabno;
889 assertUIntArg ('object_id');
890 $object_id = $_REQUEST['object_id'];
891 $oldvalues = getAttrValues ($object_id);
893 assertUIntArg ('num_attrs');
894 $num_attrs = $_REQUEST['num_attrs'];
897 for ($i = 0; $i < $num_attrs; $i++
)
899 assertUIntArg ("${i}_attr_id");
900 $attr_id = $_REQUEST["${i}_attr_id"];
902 // Field is empty, delete attribute and move on.
903 if (empty($_REQUEST["${i}_value"]))
905 commitResetAttrValue ($object_id, $attr_id);
909 // The value could be uint/float, but we don't know ATM. Let SQL
910 // server check this and complain.
911 assertStringArg ("${i}_value");
912 $value = $_REQUEST["${i}_value"];
913 switch ($oldvalues[$attr_id]['type'])
918 $oldvalue = $oldvalues[$attr_id]['value'];
921 $oldvalue = $oldvalues[$attr_id]['key'];
924 showError ('Internal structure error', __FUNCTION__
);
927 if ($value == $oldvalue)
930 // Note if the queries succeed or not, it determines which page they see.
931 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
934 if (in_array(false, $result))
935 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
937 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
940 function useupPort ()
942 global $root, $pageno, $tabno;
943 assertUIntArg ('port_id');
944 assertUIntArg ('object_id');
945 $object_id = $_REQUEST['object_id'];
946 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
947 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
949 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
954 global $root, $pageno, $tabno;
955 $oldvalues = loadConfigCache();
957 assertUIntArg ('num_vars');
958 $num_vars = $_REQUEST['num_vars'];
961 for ($i = 0; $i < $num_vars; $i++
)
963 assertStringArg ("${i}_varname");
964 assertStringArg ("${i}_varvalue", TRUE);
965 $varname = $_REQUEST["${i}_varname"];
966 $varvalue = $_REQUEST["${i}_varvalue"];
968 // If form value = value in DB, don't bother updating DB
969 if ($varvalue == getConfigVar ($varname))
972 // Note if the queries succeed or not, it determines which page they see.
973 $error = setConfigVar ($varname, $varvalue, TRUE);
979 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
981 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
984 function resetUIConfig()
986 global $root, $pageno, $tabno;
987 setConfigVar ('default_port_type','24');
988 setConfigVar ('MASSCOUNT','15');
989 setConfigVar ('MAXSELSIZE','30');
990 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
991 setConfigVar ('ROW_SCALE','2');
992 setConfigVar ('PORTS_PER_ROW','12');
993 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
994 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
995 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
996 setConfigVar ('USER_AUTH_SRC','database');
997 setConfigVar ('DEFAULT_SLB_VS_PORT','');
998 setConfigVar ('DEFAULT_SLB_RS_PORT','');
999 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
1000 setConfigVar ('NATV4_PERFORMERS','4,7,8');
1001 setConfigVar ('DETECT_URLS','no');
1002 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
1003 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
1004 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
1005 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1006 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1007 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
1008 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
1009 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
1012 // Add single record.
1013 function addRealServer ()
1015 global $root, $pageno, $tabno;
1017 assertUIntArg ('id');
1018 assertIPv4Arg ('remoteip');
1019 assertUIntArg ('rsport');
1020 assertStringArg ('rsconfig', TRUE);
1021 $pool_id = $_REQUEST['id'];
1022 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
1023 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
1025 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
1028 // Parse textarea submitted and try adding a real server for each line.
1029 function addRealServers ()
1031 global $root, $pageno, $tabno;
1033 assertUIntArg ('id');
1034 assertStringArg ('format');
1035 assertStringArg ('rawtext');
1036 $pool_id = $_REQUEST['id'];
1037 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1040 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1041 foreach (explode ('\n', $rawtext) as $line)
1046 switch ($_REQUEST['format'])
1048 case 'ipvs_2': // address and port only
1049 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) /', $line, $match))
1051 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1056 case 'ipvs_3': // address, port and weight
1057 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1059 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
1064 case 'ssv_2': // IP address and port
1065 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1067 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1073 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__
. ': invalid format requested');
1077 if ($nbad == 0 and $ngood > 0)
1078 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1080 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1083 function addVService ()
1085 global $root, $pageno, $tabno;
1087 assertIPv4Arg ('vip');
1088 assertUIntArg ('vport');
1089 assertStringArg ('proto');
1090 $proto = $_REQUEST['proto'];
1091 if ($proto != 'TCP' and $proto != 'UDP')
1092 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__
. ': invalid protocol');
1093 assertStringArg ('name', TRUE);
1094 assertStringArg ('vsconfig', TRUE);
1095 assertStringArg ('rsconfig', TRUE);
1096 $pool_id = $_REQUEST['id'];
1097 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1098 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
1100 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
1103 function deleteRealServer ()
1105 global $root, $pageno, $tabno;
1107 assertUIntArg ('pool_id');
1108 assertUIntArg ('id');
1109 $pool_id = $_REQUEST['pool_id'];
1110 if (!commitDeleteRS ($_REQUEST['id']))
1111 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
1113 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
1116 function deleteLoadBalancer ()
1118 global $root, $pageno, $tabno;
1120 assertUIntArg ('object_id');
1121 assertUIntArg ('pool_id');
1122 assertUIntArg ('vs_id');
1123 $pool_id = $_REQUEST['pool_id'];
1124 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
1125 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1127 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1130 function deleteVService ()
1132 global $root, $pageno, $tabno;
1134 assertUIntArg ('id');
1135 if (!commitDeleteVS ($_REQUEST['id']))
1136 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
1138 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
1141 function updateRealServer ()
1143 global $root, $pageno, $tabno;
1145 assertUIntArg ('id');
1146 assertUIntArg ('rs_id');
1147 assertIPv4Arg ('rsip');
1148 assertUIntArg ('rsport');
1149 assertStringArg ('rsconfig', TRUE);
1150 // only necessary for generating next URL
1151 $pool_id = $_REQUEST['id'];
1152 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
1153 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
1155 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1158 function updateLoadBalancer ()
1160 global $root, $pageno, $tabno;
1162 assertUIntArg ('object_id');
1163 assertUIntArg ('pool_id');
1164 assertUIntArg ('vs_id');
1165 assertStringArg ('vsconfig', TRUE);
1166 assertStringArg ('rsconfig', TRUE);
1167 $pool_id = $_REQUEST['pool_id'];
1168 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1169 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1171 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
1174 function updateVService ()
1176 global $root, $pageno, $tabno;
1178 assertUIntArg ('id');
1179 assertIPv4Arg ('vip');
1180 assertUIntArg ('vport');
1181 assertStringArg ('proto');
1182 assertStringArg ('name', TRUE);
1183 assertStringArg ('vsconfig', TRUE);
1184 assertStringArg ('rsconfig', TRUE);
1185 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1186 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
1188 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
1191 function addLoadBalancer ()
1193 global $root, $pageno, $tabno;
1195 assertUIntArg ('pool_id');
1196 assertUIntArg ('object_id');
1197 assertUIntArg ('vs_id');
1198 assertStringArg ('vsconfig', TRUE);
1199 assertStringArg ('rsconfig', TRUE);
1200 $pool_id = $_REQUEST['pool_id'];
1201 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1202 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
1204 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
1207 function addRSPool ()
1209 global $root, $pageno, $tabno;
1211 assertStringArg ('name', TRUE);
1212 assertStringArg ('vsconfig', TRUE);
1213 assertStringArg ('rsconfig', TRUE);
1214 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1215 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1217 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1220 function deleteRSPool ()
1222 global $root, $pageno, $tabno;
1224 assertUIntArg ('pool_id');
1225 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1226 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1228 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1231 function updateRSPool ()
1233 global $root, $pageno, $tabno;
1235 assertUIntArg ('pool_id');
1236 assertStringArg ('name', TRUE);
1237 assertStringArg ('vsconfig', TRUE);
1238 assertStringArg ('rsconfig', TRUE);
1239 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1240 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1242 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1245 function updateRSInService ()
1247 global $root, $pageno, $tabno;
1248 assertUIntArg ('pool_id');
1249 assertUIntArg ('rscount');
1250 $pool_id = $_REQUEST['pool_id'];
1251 $orig = getRSPoolInfo ($pool_id);
1253 for ($i = 1; $i <= $_REQUEST['rscount']; $i++
)
1255 $rs_id = $_REQUEST["rsid_${i}"];
1256 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1260 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1262 if (commitSetInService ($rs_id, $newval))
1269 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1271 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1274 function importPTRData ()
1276 global $root, $pageno, $tabno;
1277 assertUIntArg ('id');
1278 assertUIntArg ('addrcount');
1280 $id = $_REQUEST['id'];
1281 for ($i = 0; $i < $_REQUEST['addrcount']; $i++
)
1283 $inputname = "import_${i}";
1284 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1286 assertIPv4Arg ("addr_${i}");
1287 assertStringArg ("descr_${i}", TRUE);
1288 assertStringArg ("rsvd_${i}");
1289 // Non-existent addresses will not have this argument set in request.
1291 if ($_REQUEST["rsvd_${i}"] == 'yes')
1293 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1299 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
1301 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
1304 function generateAutoPorts ()
1306 global $root, $pageno, $tabno;
1307 assertUIntArg ('object_id');
1308 $object_id = $_REQUEST['object_id'];
1309 $info = getObjectInfo ($object_id);
1310 // Navigate away in case of success, stay at the place otherwise.
1311 if (executeAutoPorts ($object_id, $info['objtype_id']))
1312 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1314 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1317 // Filter out implicit tags before storing the new tag set.
1318 function saveEntityTags ($realm, $bypass)
1320 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
1321 assertUIntArg ($bypass);
1322 $entity_id = $_REQUEST[$bypass];
1323 // Build a trail from the submitted data, minimize it,
1324 // then wipe existing records and store the new set instead.
1325 wipeTags ($realm, $entity_id);
1326 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
1327 $n_succeeds = $n_errors = 0;
1328 foreach ($newtrail as $taginfo)
1330 if (useInsertBlade ('TagStorage', array ('target_realm' => "'${realm}'", 'target_id' => $entity_id, 'tag_id' => $taginfo['id'])))
1336 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
1338 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
1341 function saveObjectTags ()
1343 return saveEntityTags ('object', 'object_id');
1346 function saveIPv4PrefixTags ()
1348 return saveEntityTags ('ipv4net', 'id');
1351 function saveRackTags ()
1353 return saveEntityTags ('rack', 'rack_id');
1356 function saveIPv4VSTags ()
1358 return saveEntityTags ('ipv4vs', 'id');
1361 function saveIPv4RSPoolTags ()
1363 return saveEntityTags ('ipv4rspool', 'id');
1366 function destroyTag ()
1368 global $root, $pageno, $tabno;
1369 assertUIntArg ('id');
1370 if (($ret = commitDestroyTag ($_REQUEST['id'])) == '')
1371 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1373 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1376 function createTag ()
1378 global $root, $pageno, $tabno;
1379 assertStringArg ('tagname');
1380 assertUIntArg ('parent_id', TRUE);
1381 $tagname = trim ($_REQUEST['tagname']);
1382 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1383 $parent_id = 'NULL';
1384 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1385 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1387 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");