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, $caller = 'N/A', $allow_zero = FALSE)
12 if (!isset ($_REQUEST[$argname]))
14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__
);
17 if (!is_numeric ($_REQUEST[$argname]))
19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__
);
22 if ($_REQUEST[$argname] < 0)
24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__
);
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__
);
34 // This function assures that specified argument was passed
35 // and is a non-empty string.
36 function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
38 if (!isset ($_REQUEST[$argname]))
40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__
);
43 if (!is_string ($_REQUEST[$argname]))
45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__
);
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__
);
55 function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
57 if (!isset ($_REQUEST[$argname]))
59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__
);
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__
);
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__
);
74 function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
76 assertStringArg ($argname, $caller, $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] . "' (calling function is [${caller}]).", __FUNCTION__
);
84 function addPortForwarding ()
86 assertUIntArg ('object_id', __FUNCTION__
);
87 assertIPv4Arg ('localip', __FUNCTION__
);
88 assertIPv4Arg ('remoteip', __FUNCTION__
);
89 assertUIntArg ('localport', __FUNCTION__
);
90 assertStringArg ('proto', __FUNCTION__
);
91 assertStringArg ('description', __FUNCTION__
, TRUE);
92 $remoteport = isset ($_REQUEST['remoteport']) ?
$_REQUEST['remoteport'] : '';
93 if (empty ($remoteport))
94 $remoteport = $_REQUEST['localport'];
96 $error = newPortForwarding
98 $_REQUEST['object_id'],
100 $_REQUEST['localport'],
101 $_REQUEST['remoteip'],
104 $_REQUEST['description']
108 return buildRedirectURL ('OK');
110 return buildRedirectURL ('ERR', array ($error));
113 function delPortForwarding ()
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__
);
122 $error = deletePortForwarding
124 $_REQUEST['object_id'],
125 $_REQUEST['localip'],
126 $_REQUEST['localport'],
127 $_REQUEST['remoteip'],
128 $_REQUEST['remoteport'],
132 return buildRedirectURL ('OK');
134 return buildRedirectURL ('ERR', array ($error));
137 function updPortForwarding ()
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__
);
147 $error = updatePortForwarding
149 $_REQUEST['object_id'],
150 $_REQUEST['localip'],
151 $_REQUEST['localport'],
152 $_REQUEST['remoteip'],
153 $_REQUEST['remoteport'],
155 $_REQUEST['description']
158 return buildRedirectURL ('OK');
160 return buildRedirectURL ('ERR', array ($error));
163 function addPortForObject ()
165 assertUIntArg ('object_id', __FUNCTION__
);
166 assertStringArg ('port_name', __FUNCTION__
, TRUE);
167 if (empty ($_REQUEST['port_name']))
168 return buildRedirectURL ('ERR1');
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
171 return buildRedirectURL ('ERR2', array ($error));
173 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
176 function editPortForObject ()
178 assertUIntArg ('port_id', __FUNCTION__
);
179 // tolerate empty value now to produce custom informative message later
180 assertStringArg ('name', __FUNCTION__
, TRUE);
181 if (empty ($_REQUEST['name']))
182 return buildRedirectURL ('ERR1');
184 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
185 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
188 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
190 return buildRedirectURL ('ERR2', array ($error));
192 return buildRedirectURL ('OK', array ($_REQUEST['name']));
195 function delPortFromObject ()
197 assertUIntArg ('port_id', __FUNCTION__
);
198 $error = delObjectPort ($_REQUEST['port_id']);
201 return buildRedirectURL ('ERR', array ($error));
203 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
206 function linkPortForObject ()
208 assertUIntArg ('port_id', __FUNCTION__
);
209 assertUIntArg ('remote_port_id', __FUNCTION__
);
210 assertStringArg ('port_name', __FUNCTION__
, TRUE);
211 assertStringArg ('remote_port_name', __FUNCTION__
, TRUE);
212 assertStringArg ('remote_object_name', __FUNCTION__
, TRUE);
214 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
216 return buildRedirectURL ('ERR', array ($error));
218 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
221 function unlinkPortForObject ()
223 assertUIntArg ('port_id', __FUNCTION__
);
224 assertStringArg ('port_name', __FUNCTION__
, TRUE);
225 assertStringArg ('remote_port_name', __FUNCTION__
, TRUE);
226 assertStringArg ('remote_object_name', __FUNCTION__
, TRUE);
228 $error = unlinkPort ($_REQUEST['port_id']);
230 return buildRedirectURL ('ERR', array ($error));
232 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
235 function addMultiPorts ()
237 assertStringArg ('format', __FUNCTION__
);
238 assertStringArg ('input', __FUNCTION__
);
239 assertUIntArg ('port_type', __FUNCTION__
);
240 assertUIntArg ('object_id', __FUNCTION__
);
241 $format = $_REQUEST['format'];
242 $port_type = $_REQUEST['port_type'];
243 $object_id = $_REQUEST['object_id'];
244 // Input lines are escaped, so we have to explode and to chop by 2-char
245 // \n and \r respectively.
246 $lines1 = explode ('\n', $_REQUEST['input']);
247 foreach ($lines1 as $line)
249 $parts = explode ('\r', $line);
251 if (empty ($parts[0]))
254 $lines2[] = rtrim ($parts[0]);
257 foreach ($lines2 as $line)
262 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
263 list ($slot, $port) = explode ('/', $words[0]);
266 'name' => "e ${slot}/${port}",
267 'l2address' => $words[8],
268 'label' => "slot ${slot} port ${port}"
272 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
274 How Async Lines are Numbered in Cisco 3600 Series Routers
275 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
277 Understanding 16- and 32-Port Async Network Modules
278 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
281 $slot = floor (($async - 1) / 32);
282 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
283 $cable = $async - $slot * 32 - $octalgroup * 8;
284 $og_label[0] = 'async 0-7';
285 $og_label[1] = 'async 8-15';
286 $og_label[2] = 'async 16-23';
287 $og_label[3] = 'async 24-31';
290 'name' => "async ${async}",
292 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
296 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
297 $ifnumber = $words[0] * 1;
300 'name' => "e ${ifnumber}",
301 'l2address' => "${words[8]}",
302 'label' => "${ifnumber}"
306 $words = explode (' ', $line);
307 if (empty ($words[0]) or empty ($words[1]))
312 'l2address' => $words[1],
317 return buildRedirectURL ('ERR');
321 // Create ports, if they don't exist.
322 $added_count = $updated_count = $error_count = 0;
323 foreach ($ports as $port)
325 $port_id = getPortID ($object_id, $port['name']);
326 if ($port_id === NULL)
328 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
336 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
343 return buildRedirectURL ('OK', array ($added_count, $updated_count, $error_count));
346 function updIPv4Allocation ()
348 assertIPv4Arg ('ip', __FUNCTION__
);
349 assertUIntArg ('object_id', __FUNCTION__
);
350 assertStringArg ('bond_name', __FUNCTION__
, TRUE);
351 assertStringArg ('bond_type', __FUNCTION__
);
353 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
355 return buildRedirectURL ('ERR', array ($error));
357 return buildRedirectURL ('OK');
360 function delIPv4Allocation ()
362 assertIPv4Arg ('ip', __FUNCTION__
);
363 assertUIntArg ('object_id', __FUNCTION__
);
365 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
367 return buildRedirectURL ('ERR', array ($error));
369 return buildRedirectURL ('OK');
372 function addIPv4Allocation ()
374 assertIPv4Arg ('ip', __FUNCTION__
);
375 assertUIntArg ('object_id', __FUNCTION__
);
376 assertStringArg ('bond_name', __FUNCTION__
, TRUE);
377 assertStringArg ('bond_type', __FUNCTION__
);
380 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
381 if (NULL === getIPv4AddressNetworkId ($ip))
382 return buildRedirectURL ('ERR1', array ($ip));
384 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
386 return buildRedirectURL ('ERR2', array ($error));
387 $address = getIPv4Address ($ip);
388 if ($address['reserved'] == 'yes' or !empty ($address['name']))
390 $release = getConfigVar ('IPV4_AUTO_RELEASE');
392 $address['reserved'] = 'no';
394 $address['name'] = '';
395 updateAddress ($ip, $address['name'], $address['reserved']);
397 return buildRedirectURL ('OK');
400 function addIPv4Prefix ()
402 assertStringArg ('range', __FUNCTION__
);
403 assertStringArg ('name', __FUNCTION__
);
405 $is_bcast = isset ($_REQUEST['is_bcast']) ?
$_REQUEST['is_bcast'] : 'off';
406 $taglist = isset ($_REQUEST['taglist']) ?
$_REQUEST['taglist'] : array();
407 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
409 return buildRedirectURL ('ERR', array ($error));
411 return buildRedirectURL ('OK');
414 function delIPv4Prefix ()
416 assertUIntArg ('id', __FUNCTION__
);
417 $error = destroyIPv4Prefix ($_REQUEST['id']);
419 return buildRedirectURL ('ERR', array ($error));
421 return buildRedirectURL ('OK');
424 function updIPv4Prefix ()
426 assertUIntArg ('id', __FUNCTION__
);
427 assertStringArg ('name', __FUNCTION__
);
429 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
431 return buildRedirectURL ('ERR', array ($error));
433 return buildRedirectURL ('OK');
436 function editAddress ()
438 assertIPv4Arg ('ip', __FUNCTION__
);
439 assertStringArg ('name', __FUNCTION__
, TRUE);
441 if (isset ($_REQUEST['reserved']))
442 $reserved = $_REQUEST['reserved'];
445 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ?
'yes' : 'no');
447 return buildRedirectURL ('ERR', array ($error));
449 return buildRedirectURL ('OK');
452 function createUser ()
454 assertStringArg ('username', __FUNCTION__
);
455 assertStringArg ('realname', __FUNCTION__
, TRUE);
456 assertStringArg ('password', __FUNCTION__
);
457 $username = $_REQUEST['username'];
458 $password = hash (PASSWORD_HASH
, $_REQUEST['password']);
459 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
461 return buildRedirectURL ('OK', array ($username));
463 return buildRedirectURL ('ERR', array ($username));
466 function updateUser ()
468 assertUIntArg ('user_id', __FUNCTION__
);
469 assertStringArg ('username', __FUNCTION__
);
470 assertStringArg ('realname', __FUNCTION__
, TRUE);
471 assertStringArg ('password', __FUNCTION__
);
472 $username = $_REQUEST['username'];
473 $new_password = $_REQUEST['password'];
474 $old_hash = getHashByID ($_REQUEST['user_id']);
475 if ($old_hash == NULL)
476 return buildRedirectURL ('ERR1');
477 // Update user password only if provided password is not the same as current password hash.
478 if ($new_password != $old_hash)
479 $new_password = hash (PASSWORD_HASH
, $new_password);
480 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
482 return buildRedirectURL ('OK', array ($username));
484 return buildRedirectURL ('ERR2', array ($username));
487 function enableUser ()
489 assertUIntArg ('user_id', __FUNCTION__
);
490 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
491 return buildRedirectURL ('OK');
493 return buildRedirectURL ('ERR');
496 function disableUser ()
498 assertUIntArg ('user_id', __FUNCTION__
);
499 if ($_REQUEST['user_id'] == 1)
500 return buildRedirectURL ('ERR1');
501 if (commitEnableUserAccount ($_REQUEST['user_id'], 'no'))
502 return buildRedirectURL ('OK');
504 return buildRedirectURL ('ERR2');
507 // This function find differences in users's submit and PortCompat table
508 // and modifies database accordingly.
509 function savePortMap ()
511 $ptlist = getPortTypes();
512 $oldCompat = getPortCompat();
513 $newCompat = array();
514 foreach (array_keys ($ptlist) as $leftKey)
515 foreach (array_keys ($ptlist) as $rightKey)
516 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
517 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
518 // Build the new matrix from user's submit and compare it to
519 // the old matrix. Those pairs which appear on
520 // new matrix only, have to be stored in PortCompat table. Those who appear
521 // on the old matrix only, should be removed from PortCompat table.
522 // Those present in both matrices should be skipped.
523 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
524 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
525 $error_count = $success_count = 0;
526 foreach (array_keys ($ptlist) as $type1)
527 foreach (array_keys ($ptlist) as $type2)
528 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
529 switch ($oldCompatTable[$type1][$type2])
531 case TRUE: // new value is FALSE
532 if (removePortCompat ($type1, $type2) === TRUE)
537 case FALSE: // new value is TRUE
538 if (addPortCompat ($type1, $type2) === TRUE)
544 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__
);
547 if ($error_count == 0)
548 return buildRedirectURL ('OK', array ($error_count, $success_count));
550 return buildRedirectURL ('ERR', array ($error_count, $success_count));
553 function updateDictionary ()
555 assertUIntArg ('chapter_no', __FUNCTION__
);
556 assertUIntArg ('dict_key', __FUNCTION__
);
557 assertStringArg ('dict_value', __FUNCTION__
);
558 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
559 return buildRedirectURL ('OK');
561 return buildRedirectURL ('ERR');
564 function supplementDictionary ()
566 assertUIntArg ('chapter_no', __FUNCTION__
);
567 assertStringArg ('dict_value', __FUNCTION__
);
568 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
569 return buildRedirectURL ('OK');
571 return buildRedirectURL ('ERR');
574 function reduceDictionary ()
576 assertUIntArg ('chapter_no', __FUNCTION__
);
577 assertUIntArg ('dict_key', __FUNCTION__
);
578 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
579 return buildRedirectURL ('OK');
581 return buildRedirectURL ('ERR');
584 function addChapter ()
586 assertStringArg ('chapter_name', __FUNCTION__
);
587 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
588 return buildRedirectURL ('OK');
590 return buildRedirectURL ('ERR');
593 function updateChapter ()
595 assertUIntArg ('chapter_no', __FUNCTION__
);
596 assertStringArg ('chapter_name', __FUNCTION__
);
597 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
598 return buildRedirectURL ('OK');
600 return buildRedirectURL ('ERR');
603 function delChapter ()
605 assertUIntArg ('chapter_no', __FUNCTION__
);
606 if (commitDeleteChapter ($_REQUEST['chapter_no']))
607 return buildRedirectURL ('OK');
609 return buildRedirectURL ('ERR');
612 function changeAttribute ()
614 assertUIntArg ('attr_id', __FUNCTION__
);
615 assertStringArg ('attr_name', __FUNCTION__
);
616 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
617 return buildRedirectURL ('OK');
619 return buildRedirectURL ('ERR');
622 function createAttribute ()
624 assertStringArg ('attr_name', __FUNCTION__
);
625 assertStringArg ('attr_type', __FUNCTION__
);
626 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
627 return buildRedirectURL ('OK', array ($_REQUEST['attr_name']));
629 return buildRedirectURL ('ERR');
632 function deleteAttribute ()
634 assertUIntArg ('attr_id', __FUNCTION__
);
635 if (commitDeleteAttribute ($_REQUEST['attr_id']))
636 return buildRedirectURL ('OK');
638 return buildRedirectURL ('ERR');
641 function supplementAttrMap ()
643 assertUIntArg ('attr_id', __FUNCTION__
);
644 assertUIntArg ('objtype_id', __FUNCTION__
);
645 assertUIntArg ('chapter_no', __FUNCTION__
);
646 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
647 return buildRedirectURL ('OK');
649 return buildRedirectURL ('ERR');
652 function reduceAttrMap ()
654 assertUIntArg ('attr_id', __FUNCTION__
);
655 assertUIntArg ('objtype_id', __FUNCTION__
);
656 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
657 return buildRedirectURL ('OK');
659 return buildRedirectURL ('ERR');
662 function clearSticker ()
664 assertUIntArg ('attr_id', __FUNCTION__
);
665 assertUIntArg ('object_id', __FUNCTION__
);
666 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
667 return buildRedirectURL ('OK');
669 return buildRedirectURL ('ERR');
672 function updateObject ()
674 assertUIntArg ('object_id', __FUNCTION__
);
675 assertUIntArg ('object_type_id', __FUNCTION__
);
676 assertStringArg ('object_name', __FUNCTION__
, TRUE);
677 assertStringArg ('object_label', __FUNCTION__
, TRUE);
678 assertStringArg ('object_barcode', __FUNCTION__
, TRUE);
679 assertStringArg ('object_asset_no', __FUNCTION__
, TRUE);
680 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
681 $has_problems = 'yes';
683 $has_problems = 'no';
685 if (commitUpdateObject (
686 $_REQUEST['object_id'],
687 $_REQUEST['object_name'],
688 $_REQUEST['object_label'],
689 $_REQUEST['object_barcode'],
690 $_REQUEST['object_type_id'],
692 $_REQUEST['object_asset_no'],
693 $_REQUEST['object_comment']
695 return buildRedirectURL ('ERR');
696 // Invalidate thumb cache of all racks objects could occupy.
697 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
698 resetThumbCache ($rack_id);
699 return buildRedirectURL ('OK');
702 function updateStickers ()
704 assertUIntArg ('object_id', __FUNCTION__
);
705 assertUIntArg ('num_attrs', __FUNCTION__
);
706 $oldvalues = getAttrValues ($_REQUEST['object_id']);
709 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++
)
711 assertUIntArg ("${i}_attr_id", __FUNCTION__
);
712 $attr_id = $_REQUEST["${i}_attr_id"];
714 // Field is empty, delete attribute and move on.
715 if (empty($_REQUEST["${i}_value"]))
717 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
721 // The value could be uint/float, but we don't know ATM. Let SQL
722 // server check this and complain.
723 assertStringArg ("${i}_value", __FUNCTION__
);
724 $value = $_REQUEST["${i}_value"];
725 switch ($oldvalues[$attr_id]['type'])
730 $oldvalue = $oldvalues[$attr_id]['value'];
733 $oldvalue = $oldvalues[$attr_id]['key'];
736 showError ('Internal structure error', __FUNCTION__
);
739 if ($value == $oldvalue)
742 // Note if the queries succeed or not, it determines which page they see.
743 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
746 if (in_array (FALSE, $result))
747 return buildRedirectURL ('ERR');
749 return buildRedirectURL ('OK');
752 function useupPort ()
754 assertUIntArg ('port_id', __FUNCTION__
);
755 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
756 return buildRedirectURL ('OK');
758 return buildRedirectURL ('ERR');
763 assertUIntArg ('num_vars', __FUNCTION__
);
766 for ($i = 0; $i < $_REQUEST['num_vars']; $i++
)
768 assertStringArg ("${i}_varname", __FUNCTION__
);
769 assertStringArg ("${i}_varvalue", __FUNCTION__
, TRUE);
770 $varname = $_REQUEST["${i}_varname"];
771 $varvalue = $_REQUEST["${i}_varvalue"];
773 // If form value = value in DB, don't bother updating DB
774 if ($varvalue == getConfigVar ($varname))
777 // Note if the queries succeed or not, it determines which page they see.
778 $error = setConfigVar ($varname, $varvalue, TRUE);
784 return buildRedirectURL ('ERR', array ($error));
786 return buildRedirectURL ('OK');
789 function resetUIConfig()
791 setConfigVar ('default_port_type','24');
792 setConfigVar ('MASSCOUNT','15');
793 setConfigVar ('MAXSELSIZE','30');
794 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
795 setConfigVar ('ROW_SCALE','2');
796 setConfigVar ('PORTS_PER_ROW','12');
797 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
798 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
799 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
800 setConfigVar ('USER_AUTH_SRC','database');
801 setConfigVar ('DEFAULT_SLB_VS_PORT','');
802 setConfigVar ('DEFAULT_SLB_RS_PORT','');
803 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
804 setConfigVar ('NATV4_PERFORMERS','4,7,8');
805 setConfigVar ('DETECT_URLS','no');
806 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
807 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
808 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
809 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
810 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
811 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
812 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
813 setConfigVar ('IPV4_AUTO_RELEASE','1');
814 setConfigVar ('SHOW_LAST_TAB', 'no');
815 setConfigVar ('COOKIE_TTL', '1209600');
816 return buildRedirectURL ('OK');
819 // Add single record.
820 function addRealServer ()
822 assertUIntArg ('pool_id', __FUNCTION__
);
823 assertIPv4Arg ('remoteip', __FUNCTION__
);
824 assertUIntArg ('rsport', __FUNCTION__
);
825 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
827 $_REQUEST['pool_id'],
828 $_REQUEST['remoteip'],
830 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
831 $_REQUEST['rsconfig']
833 return buildRedirectURL ('ERR');
835 return buildRedirectURL ('OK');
838 // Parse textarea submitted and try adding a real server for each line.
839 function addRealServers ()
841 assertUIntArg ('pool_id', __FUNCTION__
);
842 assertStringArg ('format', __FUNCTION__
);
843 assertStringArg ('rawtext', __FUNCTION__
);
844 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
847 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
848 foreach (explode ('\n', $rawtext) as $line)
853 switch ($_REQUEST['format'])
855 case 'ipvs_2': // address and port only
856 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) /', $line, $match))
858 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
863 case 'ipvs_3': // address, port and weight
864 if (!preg_match ('/^ -> ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
866 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
871 case 'ssv_2': // IP address and port
872 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
874 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
880 return buildRedirectURL ('ERR1');
884 if ($nbad == 0 and $ngood > 0)
885 return buildRedirectURL ('OK', array ($ngood));
887 return buildRedirectURL ('ERR2', array ($ngood, $nbad));
890 function addVService ()
892 assertIPv4Arg ('vip', __FUNCTION__
);
893 assertUIntArg ('vport', __FUNCTION__
);
894 assertStringArg ('proto', __FUNCTION__
);
895 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
896 return buildRedirectURL ('ERR1');
897 assertStringArg ('name', __FUNCTION__
, TRUE);
898 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
899 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
900 $error = commitCreateVS
906 $_REQUEST['vsconfig'],
907 $_REQUEST['rsconfig'],
908 isset ($_REQUEST['taglist']) ?
$_REQUEST['taglist'] : array()
911 return buildRedirectURL ('ERR2', array ($error));
913 return buildRedirectURL ('OK');
916 function deleteRealServer ()
918 assertUIntArg ('id', __FUNCTION__
);
919 if (!commitDeleteRS ($_REQUEST['id']))
920 return buildRedirectURL ('ERR');
922 return buildRedirectURL ('OK');
925 function deleteLoadBalancer ()
927 assertUIntArg ('object_id', __FUNCTION__
);
928 assertUIntArg ('pool_id', __FUNCTION__
);
929 assertUIntArg ('vs_id', __FUNCTION__
);
930 if (!commitDeleteLB (
931 $_REQUEST['object_id'],
932 $_REQUEST['pool_id'],
935 return buildRedirectURL ('ERR');
937 return buildRedirectURL ('OK');
940 function deleteVService ()
942 assertUIntArg ('vs_id', __FUNCTION__
);
943 if (!commitDeleteVS ($_REQUEST['vs_id']))
944 return buildRedirectURL ('ERR');
946 return buildRedirectURL ('OK');
949 function updateRealServer ()
951 assertUIntArg ('rs_id', __FUNCTION__
);
952 assertIPv4Arg ('rsip', __FUNCTION__
);
953 assertUIntArg ('rsport', __FUNCTION__
);
954 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
955 if (!commitUpdateRS (
959 $_REQUEST['rsconfig']
961 return buildRedirectURL ('ERR');
963 return buildRedirectURL ('OK');
966 function updateLoadBalancer ()
968 assertUIntArg ('object_id', __FUNCTION__
);
969 assertUIntArg ('pool_id', __FUNCTION__
);
970 assertUIntArg ('vs_id', __FUNCTION__
);
971 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
972 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
973 if (!commitUpdateLB (
974 $_REQUEST['object_id'],
975 $_REQUEST['pool_id'],
977 $_REQUEST['vsconfig'],
978 $_REQUEST['rsconfig']
980 return buildRedirectURL ('ERR');
982 return buildRedirectURL ('OK');
985 function updateVService ()
987 assertUIntArg ('vs_id', __FUNCTION__
);
988 assertIPv4Arg ('vip', __FUNCTION__
);
989 assertUIntArg ('vport', __FUNCTION__
);
990 assertStringArg ('proto', __FUNCTION__
);
991 assertStringArg ('name', __FUNCTION__
, TRUE);
992 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
993 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
994 if (!commitUpdateVS (
1000 $_REQUEST['vsconfig'],
1001 $_REQUEST['rsconfig']
1003 return buildRedirectURL ('ERR');
1005 return buildRedirectURL ('OK');
1008 function addLoadBalancer ()
1010 assertUIntArg ('pool_id', __FUNCTION__
);
1011 assertUIntArg ('object_id', __FUNCTION__
);
1012 assertUIntArg ('vs_id', __FUNCTION__
);
1013 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
1014 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
1015 if (!addLBtoRSPool (
1016 $_REQUEST['pool_id'],
1017 $_REQUEST['object_id'],
1019 $_REQUEST['vsconfig'],
1020 $_REQUEST['rsconfig']
1022 return buildRedirectURL ('ERR');
1024 return buildRedirectURL ('OK');
1027 function addRSPool ()
1029 assertStringArg ('name', __FUNCTION__
, TRUE);
1030 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
1031 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
1032 $error = commitCreateRSPool
1035 $_REQUEST['vsconfig'],
1036 $_REQUEST['rsconfig'],
1037 isset ($_REQUEST['taglist']) ?
$_REQUEST['taglist'] : array()
1040 return buildRedirectURL ('ERR', array ($error));
1042 return buildRedirectURL ('OK');
1045 function deleteRSPool ()
1047 assertUIntArg ('pool_id', __FUNCTION__
);
1048 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1049 return buildRedirectURL ('ERR');
1051 return buildRedirectURL ('OK');
1054 function updateRSPool ()
1056 assertUIntArg ('pool_id', __FUNCTION__
);
1057 assertStringArg ('name', __FUNCTION__
, TRUE);
1058 assertStringArg ('vsconfig', __FUNCTION__
, TRUE);
1059 assertStringArg ('rsconfig', __FUNCTION__
, TRUE);
1060 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1061 return buildRedirectURL ('ERR');
1063 return buildRedirectURL ('OK');
1066 function updateRSInService ()
1068 assertUIntArg ('rscount', __FUNCTION__
);
1069 $pool_id = $_REQUEST['pool_id'];
1070 $orig = getRSPoolInfo ($pool_id);
1072 for ($i = 1; $i <= $_REQUEST['rscount']; $i++
)
1074 $rs_id = $_REQUEST["rsid_${i}"];
1075 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1079 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1081 if (commitSetInService ($rs_id, $newval))
1088 return buildRedirectURL ('OK', array ($ngood));
1090 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1093 function importPTRData ()
1095 assertUIntArg ('addrcount', __FUNCTION__
);
1097 for ($i = 0; $i < $_REQUEST['addrcount']; $i++
)
1099 $inputname = "import_${i}";
1100 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1102 assertIPv4Arg ("addr_${i}", __FUNCTION__
);
1103 assertStringArg ("descr_${i}", __FUNCTION__
, TRUE);
1104 assertStringArg ("rsvd_${i}", __FUNCTION__
);
1105 // Non-existent addresses will not have this argument set in request.
1107 if ($_REQUEST["rsvd_${i}"] == 'yes')
1109 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1115 return buildRedirectURL ('OK', array ($ngood));
1117 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1120 function generateAutoPorts ()
1123 assertUIntArg ('object_id', __FUNCTION__
);
1124 $info = getObjectInfo ($_REQUEST['object_id']);
1125 // Navigate away in case of success, stay at the place otherwise.
1126 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1127 return buildRedirectURL ('OK', array(), $pageno, 'ports');
1129 return buildRedirectURL ('ERR');
1132 // Filter out implicit tags before storing the new tag set.
1133 function saveEntityTags ($realm, $bypass)
1135 global $explicit_tags, $implicit_tags;
1136 assertUIntArg ($bypass, __FUNCTION__
);
1137 $entity_id = $_REQUEST[$bypass];
1138 $taglist = isset ($_REQUEST['taglist']) ?
$_REQUEST['taglist'] : array();
1139 // Build a chain from the submitted data, minimize it,
1140 // then wipe existing records and store the new set instead.
1141 deleteTagsForEntity ($realm, $entity_id);
1142 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1143 $n_succeeds = $n_errors = 0;
1144 foreach ($newchain as $taginfo)
1145 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1150 return buildRedirectURL ('ERR', array ($n_succeeds, $n_errors));
1152 return buildRedirectURL ('OK', array ($n_succeeds));
1155 function saveObjectTags ()
1157 return saveEntityTags ('object', 'object_id');
1160 function saveIPv4PrefixTags ()
1162 return saveEntityTags ('ipv4net', 'id');
1165 function saveRackTags ()
1167 return saveEntityTags ('rack', 'rack_id');
1170 function saveIPv4VSTags ()
1172 return saveEntityTags ('ipv4vs', 'vs_id');
1175 function saveIPv4RSPoolTags ()
1177 return saveEntityTags ('ipv4rspool', 'pool_id');
1180 function saveUserTags ()
1182 return saveEntityTags ('user', 'user_id');
1185 function destroyTag ()
1187 assertUIntArg ('tag_id', __FUNCTION__
);
1188 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1189 return buildRedirectURL ('OK');
1191 return buildRedirectURL ('ERR');
1194 function createTag ()
1196 assertStringArg ('tag_name', __FUNCTION__
);
1197 assertUIntArg ('parent_id', __FUNCTION__
, TRUE);
1198 $tagname = trim ($_REQUEST['tag_name']);
1199 if (!validTagName ($tagname))
1200 return buildRedirectURL ('ERR1', array ($tagname));
1201 if (tagExistsInDatabase ($tagname))
1202 return buildRedirectURL ('ERR2', array ($tagname));
1203 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1204 $parent_id = 'NULL';
1205 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1206 return buildRedirectURL ('OK', array ($tagname));
1208 return buildRedirectURL ('ERR', array ($tagname, $ret));
1211 function updateTag ()
1213 assertUIntArg ('tag_id', __FUNCTION__
);
1214 assertUIntArg ('parent_id', __FUNCTION__
, TRUE);
1215 assertStringArg ('tag_name', __FUNCTION__
);
1216 $tagname = trim ($_REQUEST['tag_name']);
1217 if (!validTagName ($tagname))
1218 return buildRedirectURL ('ERR');
1219 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1220 $parent_id = 'NULL';
1221 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1222 return buildRedirectURL ('OK', array ($tagname));
1224 return buildRedirectURL ('ERR', array ($tagname, $ret));
1227 function rollTags ()
1229 assertUIntArg ('row_id', __FUNCTION__
);
1230 assertStringArg ('sum', __FUNCTION__
, TRUE);
1231 assertUIntArg ('realsum', __FUNCTION__
);
1232 $row_id = $_REQUEST['row_id'];
1233 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1234 return buildRedirectURL ('ERR');
1235 $racks = getRacksForRow ($row_id);
1236 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1237 $newtags = isset ($_REQUEST['taglist']) ?
$_REQUEST['taglist'] : array();
1238 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
1239 $ndupes = $nnew = 0;
1240 foreach ($tagstack as $taginfo)
1241 foreach ($racks as $rackInfo)
1243 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1247 // FIXME: do something likewise for all object inside current rack
1249 return buildRedirectURL ('OK', array ($nnew, $ndupes));
1252 function changeMyPassword ()
1254 global $accounts, $remote_username;
1255 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1256 return buildRedirectURL ('ERR1');
1257 assertStringArg ('oldpassword', __FUNCTION__
);
1258 assertStringArg ('newpassword1', __FUNCTION__
);
1259 assertStringArg ('newpassword2', __FUNCTION__
);
1260 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH
, $_REQUEST['oldpassword']))
1261 return buildRedirectURL ('ERR2');
1262 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1263 return buildRedirectURL ('ERR3');
1264 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH
, $_REQUEST['newpassword1'])))
1265 return buildRedirectURL ('OK');
1267 return buildRedirectURL ('ERR4');
1270 function saveRackCode ()
1272 assertStringArg ('rackcode');
1273 // For the test to succeed, unescape LFs, strip CRs.
1274 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1275 $parseTree = getRackCode ($newcode);
1276 if ($parseTree['result'] != 'ACK')
1277 return buildRedirectURL ('ERR1', array ($parseTree['load']));
1278 saveScript ('RackCodeCache', '');
1279 if (saveScript ('RackCode', $newcode))
1280 return buildRedirectURL ('OK');
1282 return buildRedirectURL ('ERR2');
1285 // This handler's context is pre-built, but not authorized. It is assumed, that the
1286 // handler will take existing context and before each commit check authorization
1287 // on the base chain plus necessary context added.
1288 function setPortVLAN ()
1290 assertUIntArg ('portcount', __FUNCTION__
);
1291 $data = getSwitchVLANs ($_REQUEST['object_id']);
1293 return buildRedirectURL ('ERR1');
1294 list ($vlanlist, $portlist) = $data;
1295 // Here we just build up 1 set command for the gateway with all of the ports
1296 // included. The gateway is expected to filter unnecessary changes silently
1297 // and to provide a list of responses with either error or success message
1298 // for each of the rest.
1299 $nports = $_REQUEST['portcount'];
1301 $log = array ('v' => 2, 'm' => array());
1303 for ($i = 0; $i < $nports; $i++
)
1306 !isset ($_REQUEST['portname_' . $i]) ||
1307 !isset ($_REQUEST['vlanid_' . $i]) ||
1308 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1310 $log['m'][] = array ('c' => 158, 'a' => array ($i));
1313 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1314 $portlist[$i]['vlaind'] == 'TRUNK'
1319 $portname = $_REQUEST['portname_' . $i];
1320 $oldvlanid = $portlist[$i]['vlanid'];
1321 $newvlanid = $_REQUEST['vlanid_' . $i];
1322 // Finish the security context and evaluate it.
1324 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1325 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1326 if (!permitted (NULL, NULL, NULL, $annex))
1328 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
1331 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1334 // Feed the gateway and interpret its (non)response.
1336 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1338 $log['m'][] = array ('c' => 201);
1339 return buildWideRedirectURL ($log);
1342 function submitSLBConfig ()
1344 assertUIntArg ('object_id', __FUNCTION__
);
1345 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
1346 $msglog = activateSLBConfig ($_REQUEST['object_id'], html_entity_decode ($newconfig, ENT_QUOTES
, 'UTF-8'));
1347 return buildWideRedirectURL ($msglog);
1350 function submitRouterConfig ()
1352 assertUIntArg ('object_id', __FUNCTION__
);
1353 $msglog = activateRouterConfig ($_REQUEST['object_id'], buildRouterConfig ($_REQUEST['object_id']));
1354 return buildWideRedirectURL ($msglog);