r1831 + make search form named
[racktables] / inc / ophandlers.php
1 <?php
2 /*
3 *
4 * This file is a library of operation handlers for RackTables.
5 *
6 */
7
8 // This function assures that specified argument was passed
9 // and is a number greater than zero.
10 function assertUIntArg ($argname, $allow_zero = FALSE)
11 {
12 if (!isset ($_REQUEST[$argname]))
13 {
14 showError ("Parameter '${argname}' is missing.", __FUNCTION__);
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
19 showError ("Parameter '${argname}' is not a number.", __FUNCTION__);
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
24 showError ("Parameter '${argname}' is less than zero.", __FUNCTION__);
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
29 showError ("Parameter '${argname}' is equal to zero.", __FUNCTION__);
30 die();
31 }
32 }
33
34 // This function assures that specified argument was passed
35 // and is a non-empty string.
36 function assertStringArg ($argname, $ok_if_empty = FALSE)
37 {
38 if (!isset ($_REQUEST[$argname]))
39 {
40 showError ("Parameter '${argname}' is missing.", __FUNCTION__);
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
45 showError ("Parameter '${argname}' is not a string.", __FUNCTION__);
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
50 showError ("Parameter '${argname}' is an empty string.", __FUNCTION__);
51 die();
52 }
53 }
54
55 function assertBoolArg ($argname, $ok_if_empty = FALSE)
56 {
57 if (!isset ($_REQUEST[$argname]))
58 {
59 showError ("Parameter '${argname}' is missing.", __FUNCTION__);
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
64 showError ("Parameter '${argname}' is not a string.", __FUNCTION__);
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
69 showError ("Parameter '${argname}' is an empty string.", __FUNCTION__);
70 die();
71 }
72 }
73
74 function assertIPv4Arg ($argname, $ok_if_empty = FALSE)
75 {
76 assertStringArg ($argname, $ok_if_empty);
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "'", __FUNCTION__);
80 die();
81 }
82 }
83
84 function addPortForwarding ()
85 {
86 global $root, $pageno, $tabno;
87
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;
103
104 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
105
106
107 $error=newPortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
108
109 if ($error == '')
110 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
111 else
112 {
113 return "${retpage}&error=".urlencode($error);
114 }
115
116 }
117
118 function delPortForwarding ()
119 {
120 global $root, $pageno, $tabno;
121
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'];
128
129 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
130
131 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
132 if ($error == '')
133 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
134 else
135 {
136 return "${retpage}&error=".urlencode($error);
137 }
138
139 }
140
141 function updPortForwarding ()
142 {
143 global $root, $pageno, $tabno;
144
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'];
152
153 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
154
155 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
156 if ($error == '')
157 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
158 else
159 {
160 return "${retpage}&error=".urlencode($error);
161 }
162
163 }
164
165 function addPortForObject ()
166 {
167 global $root, $pageno, $tabno;
168
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'];
174
175
176 if ($port_name == '')
177 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
178 else
179 {
180 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
181 if ($error != '')
182 {
183 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
184 }
185 else
186 {
187 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
188 }
189 }
190
191 }
192
193 function editPortForObject ()
194 {
195 global $root, $pageno, $tabno;
196
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'] . '"';
207 else
208 $port_rc = 'NULL';
209
210 if ($port_name == '')
211 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
212 else
213 {
214 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
215 if ($error != '')
216 {
217 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
218 }
219 else
220 {
221 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
222 }
223 }
224
225 }
226
227 function delPortFromObject ()
228 {
229 global $root, $pageno, $tabno;
230
231 $port_id = $_REQUEST['port_id'];
232 $port_name = $_REQUEST['port_name'];
233 $object_id = $_REQUEST['object_id'];
234 $error = delObjectPort($port_id);
235
236 if ($error != '')
237 {
238 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
239 }
240 else
241 {
242 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
243 }
244 }
245
246 function linkPortForObject ()
247 {
248 global $root, $pageno, $tabno;
249
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'];
256
257 $error = linkPorts($port_id, $remote_port_id);
258 if ($error != '')
259 {
260 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
261 }
262 else
263 {
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");
265 }
266 }
267
268 function unlinkPortForObject ()
269 {
270 global $root, $pageno, $tabno;
271
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'];
277
278 $error = unlinkPort($port_id);
279 if ($error != '')
280 {
281 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
282 }
283 else
284 {
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");
286 }
287 }
288
289 function addMultiPorts ()
290 {
291 global $root, $pageno, $tabno;
292 // Parse data.
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)
304 {
305 $parts = explode ('\r', $line);
306 reset ($parts);
307 if (empty ($parts[0]))
308 continue;
309 else
310 $lines2[] = rtrim ($parts[0]);
311 }
312 $ports = array();
313 foreach ($lines2 as $line)
314 {
315 switch ($format)
316 {
317 case 'fisxii':
318 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
319 list ($slot, $port) = explode ('/', $words[0]);
320 $ports[] = array
321 (
322 'name' => "e ${slot}/${port}",
323 'l2address' => $words[8],
324 'label' => "slot ${slot} port ${port}"
325 );
326 break;
327 case 'c3600asy':
328 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
329 /*
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
332
333 Understanding 16- and 32-Port Async Network Modules
334 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
335 */
336 $async = $words[0];
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';
344 $ports[] = array
345 (
346 'name' => "async ${async}",
347 'l2address' => '',
348 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
349 );
350 break;
351 case 'fiwg':
352 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
353 $ifnumber = $words[0] * 1;
354 $ports[] = array
355 (
356 'name' => "e ${ifnumber}",
357 'l2address' => "${words[8]}",
358 'label' => "${ifnumber}"
359 );
360 break;
361 case 'ssv1':
362 $words = explode (' ', $line);
363 if (empty ($words[0]) or empty ($words[1]))
364 continue;
365 $ports[] = array
366 (
367 'name' => $words[0],
368 'l2address' => $words[1],
369 'label' => ''
370 );
371 break;
372 default:
373 return
374 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
375 urlencode('Cannot process submitted data: unknown format code.');
376 break;
377 }
378 }
379 // Create ports, if they don't exist.
380 $added_count = $updated_count = $error_count = 0;
381 foreach ($ports as $port)
382 {
383 $port_id = getPortID ($object_id, $port['name']);
384 if ($port_id === NULL)
385 {
386 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
387 if ($result == '')
388 $added_count++;
389 else
390 $error_count++;
391 }
392 else
393 {
394 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
395 if ($result == '')
396 $updated_count++;
397 else
398 $error_count++;
399 }
400 }
401 return
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.");
404 }
405
406 function editAddressFromObject ()
407 {
408 global $root;
409
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);
415 if ($error != '')
416 {
417 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
418 }
419 else
420 {
421 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
422 }
423 }
424
425 function delAddressFromObject ()
426 {
427 global $root;
428
429 $ip = $_REQUEST['ip'];
430 $object_id = $_REQUEST['object_id'];
431 $error = unbindIpFromObject($ip, $object_id);
432 if ($error != '')
433 {
434 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
435 }
436 else
437 {
438 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
439 }
440 }
441
442 function delIpAssignment ()
443 {
444 global $root;
445
446 $ip = $_REQUEST['ip'];
447 $object_id = $_REQUEST['object_id'];
448 $error = unbindIpFromObject($ip, $object_id);
449 if ($error != '')
450 {
451 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
452 }
453 else
454 {
455 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
456 }
457 }
458
459 function editIpAssignment ()
460 {
461 global $root;
462
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);
468
469 if ($error != '')
470 {
471 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
472 }
473 else
474 {
475 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
476 }
477 }
478
479 function addIpAssignment ()
480 {
481 global $root;
482
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);
488 if ($error != '')
489 {
490 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
491 }
492 else
493 {
494 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
495 }
496 }
497
498 function addNewRange ()
499 {
500 global $root, $pageno, $tabno;
501 assertStringArg ('range');
502 assertStringArg ('name');
503
504 $range = $_REQUEST['range'];
505 $name = $_REQUEST['name'];
506 $is_bcast = $_REQUEST['is_bcast'];
507 $error = addRange($range, $name, $is_bcast == 'on');
508 if ($error != '')
509 {
510 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
511 }
512 else
513 {
514 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
515 }
516 }
517
518 function editRange ()
519 {
520 global $root, $pageno, $tabno;
521
522 $id = $_REQUEST['id'];
523 $name = $_REQUEST['name'];
524 $error = updateRange($id, $name);
525 if ($error != '')
526 {
527 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
528 }
529 else
530 {
531 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
532 }
533
534 }
535
536 function delRange ()
537 {
538 global $root, $pageno, $tabno;
539
540 $id = $_REQUEST['id'];
541 $error = commitDeleteRange ($id);
542 if ($error != '')
543 {
544 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
545 }
546 else
547 {
548 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
549 }
550
551 }
552
553 function editAddress ()
554 {
555 global $root, $pageno, $tabno;
556
557 $ip = $_REQUEST['ip'];
558 $name = $_REQUEST['name'];
559 if (isset ($_REQUEST['reserved']))
560 $reserved = $_REQUEST['reserved'];
561 else
562 $reserved = 'off';
563 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
564 if ($error != '')
565 {
566 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
567 }
568 else
569 {
570 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
571 }
572
573 }
574
575 function addAddressToObject ()
576 {
577 global $root, $pageno, $tabno;
578
579 assertStringArg ('ip');
580 assertUIntArg ('object_id');
581 assertStringArg ('name', TRUE);
582 assertStringArg ('type');
583 // Strip masklen.
584 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
585 $object_id = $_REQUEST['object_id'];
586 $error = bindIpToObject($ip, $object_id, $_REQUEST['name'], $_REQUEST['type']);
587 if ($error != '')
588 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
589 else
590 return "${root}?page=$pageno&tab=${tabno}&object_id=${object_id}&message=".
591 urlencode("Address ${ip} was added successfully.");
592 }
593
594 function createUserAccount ()
595 {
596 global $root, $pageno, $tabno;
597 assertStringArg ('username');
598 assertStringArg ('realname', TRUE);
599 assertStringArg ('password');
600 $username = $_REQUEST['username'];
601 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
602 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
603 if ($result == TRUE)
604 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
605 else
606 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
607 }
608
609 function updateUserAccount ()
610 {
611 global $root, $pageno, $tabno;
612 assertUIntArg ('id');
613 assertStringArg ('username');
614 assertStringArg ('realname', TRUE);
615 assertStringArg ('password');
616 // We might be asked to change username, so use user ID only.
617 $id = $_REQUEST['id'];
618 $username = $_REQUEST['username'];
619 $new_password = $_REQUEST['password'];
620 $old_hash = getHashByID ($id);
621 if ($old_hash == NULL)
622 {
623 showError ('getHashByID() failed', __FUNCTION__);
624 return;
625 }
626 // Update user password only if provided password is not the same as current password hash.
627 if ($new_password != $old_hash)
628 $new_password = hash (PASSWORD_HASH, $new_password);
629 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
630 if ($result == TRUE)
631 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
632 else
633 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
634 }
635
636 function enableUserAccount ()
637 {
638 global $root, $pageno, $tabno;
639 assertUIntArg ('id');
640 $id = $_REQUEST['id'];
641 $result = commitEnableUserAccount ($id, 'yes');
642 if ($result == TRUE)
643 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
644 else
645 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
646 }
647
648 function disableUserAccount ()
649 {
650 global $root, $pageno, $tabno;
651 assertUIntArg ('id');
652 $id = $_REQUEST['id'];
653 if ($id == 1)
654 $result = FALSE;
655 else
656 $result = commitEnableUserAccount ($id, 'no');
657 if ($result == TRUE)
658 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
659 else
660 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
661 }
662
663 function revokePermission ()
664 {
665 global $root, $pageno, $tabno;
666 assertUIntArg ('access_userid', TRUE);
667 assertStringArg ('access_page');
668 assertStringArg ('access_tab');
669 $result = commitRevokePermission
670 (
671 $_REQUEST['access_userid'],
672 $_REQUEST['access_page'],
673 $_REQUEST['access_tab']
674 );
675 if ($result == TRUE)
676 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
677 else
678 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
679 }
680
681 function grantPermission ()
682 {
683 global $root, $pageno, $tabno;
684 assertUIntArg ('access_userid', TRUE);
685 assertStringArg ('access_page');
686 assertStringArg ('access_tab');
687 assertStringArg ('access_value');
688 $result = commitGrantPermission
689 (
690 $_REQUEST['access_userid'],
691 $_REQUEST['access_page'],
692 $_REQUEST['access_tab'],
693 $_REQUEST['access_value']
694 );
695 if ($result == TRUE)
696 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
697 else
698 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
699 }
700
701 // This function find differences in users's submit and PortCompat table
702 // and modifies database accordingly.
703 function savePortMap ()
704 {
705 global $root, $pageno, $tabno;
706 $ptlist = getPortTypes();
707 $oldCompat = getPortCompat();
708 $newCompat = array();
709 foreach (array_keys ($ptlist) as $leftKey)
710 foreach (array_keys ($ptlist) as $rightKey)
711 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
712 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
713 // Build the new matrix from user's submit and compare it to
714 // the old matrix. Those pairs which appear on
715 // new matrix only, have to be stored in PortCompat table. Those who appear
716 // on the old matrix only, should be removed from PortCompat table.
717 // Those present in both matrices should be skipped.
718 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
719 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
720 $error_count = $success_count = 0;
721 foreach (array_keys ($ptlist) as $type1)
722 foreach (array_keys ($ptlist) as $type2)
723 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
724 switch ($oldCompatTable[$type1][$type2])
725 {
726 case TRUE: // new value is FALSE
727 if (removePortCompat ($type1, $type2) === TRUE)
728 $success_count++;
729 else
730 $error_count++;
731 break;
732 case FALSE: // new value is TRUE
733 if (addPortCompat ($type1, $type2) === TRUE)
734 $success_count++;
735 else
736 $error_count++;
737 break;
738 default:
739 showError ('oldCompatTable is invalid', __FUNCTION__);
740 break;
741 }
742 return
743 "${root}?page=${pageno}&tab=${tabno}&" .
744 ($error_count == 0 ? 'message' : 'error') .
745 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
746 }
747
748 function deleteDictWord ()
749 {
750 global $root, $pageno, $tabno;
751 return
752 "${root}?page=${pageno}&tab=${tabno}&" .
753 "error=" . urlencode ('Dragon ate this word!');
754 }
755
756 function updateDictionary ()
757 {
758 global $root, $pageno, $tabno;
759 assertUIntArg ('chapter_no');
760 assertUIntArg ('dict_key');
761 assertStringArg ('dict_value');
762 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
763 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
764 else
765 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
766 }
767
768 function supplementDictionary ()
769 {
770 global $root, $pageno, $tabno;
771 assertUIntArg ('chapter_no');
772 assertStringArg ('dict_value');
773 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
774 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
775 else
776 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
777 }
778
779 function reduceDictionary ()
780 {
781 global $root, $pageno, $tabno;
782 assertUIntArg ('chapter_no');
783 assertUIntArg ('dict_key');
784 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
785 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
786 else
787 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
788 }
789
790 function addChapter ()
791 {
792 global $root, $pageno, $tabno;
793 assertStringArg ('chapter_name');
794 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
795 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
796 else
797 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
798 }
799
800 function updateChapter ()
801 {
802 global $root, $pageno, $tabno;
803 assertUIntArg ('chapter_no');
804 assertStringArg ('chapter_name');
805 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
806 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
807 else
808 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
809 }
810
811 function delChapter ()
812 {
813 global $root, $pageno, $tabno;
814 assertUIntArg ('chapter_no');
815 if (commitDeleteChapter ($_REQUEST['chapter_no']))
816 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
817 else
818 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
819 }
820
821 function changeAttribute ()
822 {
823 global $root, $pageno, $tabno;
824 assertUIntArg ('attr_id');
825 assertStringArg ('attr_name');
826 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
827 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
828 else
829 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
830 }
831
832 function createAttribute ()
833 {
834 global $root, $pageno, $tabno;
835 assertStringArg ('attr_name');
836 assertStringArg ('attr_type');
837 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
838 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
839 else
840 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
841 }
842
843 function deleteAttribute ()
844 {
845 global $root, $pageno, $tabno;
846 assertUIntArg ('attr_id');
847 if (commitDeleteAttribute ($_REQUEST['attr_id']))
848 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
849 else
850 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
851 }
852
853 function supplementAttrMap ()
854 {
855 global $root, $pageno, $tabno;
856 assertUIntArg ('attr_id');
857 assertUIntArg ('objtype_id');
858 assertUIntArg ('chapter_no');
859 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
860 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
861 else
862 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
863 }
864
865 function reduceAttrMap ()
866 {
867 global $root, $pageno, $tabno;
868 assertUIntArg ('attr_id');
869 assertUIntArg ('objtype_id');
870 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
871 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
872 else
873 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
874 }
875
876 function resetAttrValue ()
877 {
878 global $root, $pageno, $tabno;
879 assertUIntArg ('attr_id');
880 assertUIntArg ('object_id');
881 $object_id = $_REQUEST['object_id'];
882 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
883 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
884 else
885 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
886 }
887
888 function updateAttrValues ()
889 {
890 global $root, $pageno, $tabno;
891 assertUIntArg ('object_id');
892 $object_id = $_REQUEST['object_id'];
893 $oldvalues = getAttrValues ($object_id);
894
895 assertUIntArg ('num_attrs');
896 $num_attrs = $_REQUEST['num_attrs'];
897 $result = array();
898
899 for ($i = 0; $i < $num_attrs; $i++)
900 {
901 assertUIntArg ("${i}_attr_id");
902 $attr_id = $_REQUEST["${i}_attr_id"];
903
904 // Field is empty, delete attribute and move on.
905 if (empty($_REQUEST["${i}_value"]))
906 {
907 commitResetAttrValue ($object_id, $attr_id);
908 continue;
909 }
910
911 // The value could be uint/float, but we don't know ATM. Let SQL
912 // server check this and complain.
913 assertStringArg ("${i}_value");
914 $value = $_REQUEST["${i}_value"];
915 switch ($oldvalues[$attr_id]['type'])
916 {
917 case 'uint':
918 case 'float':
919 case 'string':
920 $oldvalue = $oldvalues[$attr_id]['value'];
921 break;
922 case 'dict':
923 $oldvalue = $oldvalues[$attr_id]['key'];
924 break;
925 default:
926 showError ('Internal structure error', __FUNCTION__);
927 die;
928 }
929 if ($value == $oldvalue)
930 continue;
931
932 // Note if the queries succeed or not, it determines which page they see.
933 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
934 }
935
936 if (in_array(false, $result))
937 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
938
939 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
940 }
941
942 function useupPort ()
943 {
944 global $root, $pageno, $tabno;
945 assertUIntArg ('port_id');
946 assertUIntArg ('object_id');
947 $object_id = $_REQUEST['object_id'];
948 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
949 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
950 else
951 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
952 }
953
954 function updateUI ()
955 {
956 global $root, $pageno, $tabno;
957 $oldvalues = loadConfigCache();
958
959 assertUIntArg ('num_vars');
960 $num_vars = $_REQUEST['num_vars'];
961 $error = '';
962
963 for ($i = 0; $i < $num_vars; $i++)
964 {
965 assertStringArg ("${i}_varname");
966 assertStringArg ("${i}_varvalue", TRUE);
967 $varname = $_REQUEST["${i}_varname"];
968 $varvalue = $_REQUEST["${i}_varvalue"];
969
970 // If form value = value in DB, don't bother updating DB
971 if ($varvalue == getConfigVar ($varname))
972 continue;
973
974 // Note if the queries succeed or not, it determines which page they see.
975 $error = setConfigVar ($varname, $varvalue, TRUE);
976 if (!empty ($error))
977 break;
978 }
979
980 if ($error != '')
981 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
982
983 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
984 }
985
986 function resetUIConfig()
987 {
988 global $root, $pageno, $tabno;
989 setConfigVar ('default_port_type','24');
990 setConfigVar ('MASSCOUNT','15');
991 setConfigVar ('MAXSELSIZE','30');
992 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
993 setConfigVar ('ROW_SCALE','2');
994 setConfigVar ('PORTS_PER_ROW','12');
995 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
996 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
997 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
998 setConfigVar ('USER_AUTH_SRC','database');
999 setConfigVar ('DEFAULT_SLB_VS_PORT','');
1000 setConfigVar ('DEFAULT_SLB_RS_PORT','');
1001 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
1002 setConfigVar ('NATV4_PERFORMERS','4,7,8');
1003 setConfigVar ('DETECT_URLS','no');
1004 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
1005 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
1006 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
1007 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1008 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1009 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
1010 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
1011 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
1012 }
1013
1014 // Add single record.
1015 function addRealServer ()
1016 {
1017 global $root, $pageno, $tabno;
1018
1019 assertUIntArg ('pool_id');
1020 assertIPv4Arg ('remoteip');
1021 assertUIntArg ('rsport');
1022 assertStringArg ('rsconfig', TRUE);
1023 $pool_id = $_REQUEST['pool_id'];
1024 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
1025 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
1026 else
1027 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
1028 }
1029
1030 // Parse textarea submitted and try adding a real server for each line.
1031 function addRealServers ()
1032 {
1033 global $root, $pageno, $tabno;
1034
1035 assertUIntArg ('id');
1036 assertStringArg ('format');
1037 assertStringArg ('rawtext');
1038 $pool_id = $_REQUEST['id'];
1039 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1040 $ngood = $nbad = 0;
1041 $rsconfig = '';
1042 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1043 foreach (explode ('\n', $rawtext) as $line)
1044 {
1045 if (empty ($line))
1046 continue;
1047 $match = array ();
1048 switch ($_REQUEST['format'])
1049 {
1050 case 'ipvs_2': // address and port only
1051 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1052 continue;
1053 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1054 $ngood++;
1055 else
1056 $nbad++;
1057 break;
1058 case 'ipvs_3': // address, port and weight
1059 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1060 continue;
1061 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
1062 $ngood++;
1063 else
1064 $nbad++;
1065 break;
1066 case 'ssv_2': // IP address and port
1067 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1068 continue;
1069 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1070 $ngood++;
1071 else
1072 $nbad++;
1073 break;
1074 default:
1075 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
1076 break;
1077 }
1078 }
1079 if ($nbad == 0 and $ngood > 0)
1080 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1081 else
1082 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1083 }
1084
1085 function addVService ()
1086 {
1087 global $root, $pageno, $tabno;
1088
1089 assertIPv4Arg ('vip');
1090 assertUIntArg ('vport');
1091 assertStringArg ('proto');
1092 $proto = $_REQUEST['proto'];
1093 if ($proto != 'TCP' and $proto != 'UDP')
1094 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
1095 assertStringArg ('name', TRUE);
1096 assertStringArg ('vsconfig', TRUE);
1097 assertStringArg ('rsconfig', TRUE);
1098 $pool_id = $_REQUEST['id'];
1099 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1100 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
1101 else
1102 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
1103 }
1104
1105 function deleteRealServer ()
1106 {
1107 global $root, $pageno, $tabno;
1108
1109 assertUIntArg ('pool_id');
1110 assertUIntArg ('id');
1111 $pool_id = $_REQUEST['pool_id'];
1112 if (!commitDeleteRS ($_REQUEST['id']))
1113 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
1114 else
1115 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
1116 }
1117
1118 function deleteLoadBalancer ()
1119 {
1120 global $root, $pageno, $tabno;
1121
1122 assertUIntArg ('object_id');
1123 assertUIntArg ('pool_id');
1124 assertUIntArg ('vs_id');
1125 $pool_id = $_REQUEST['pool_id'];
1126 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
1127 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1128 else
1129 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1130 }
1131
1132 function deleteVService ()
1133 {
1134 global $root, $pageno, $tabno;
1135
1136 assertUIntArg ('id');
1137 if (!commitDeleteVS ($_REQUEST['id']))
1138 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
1139 else
1140 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
1141 }
1142
1143 function updateRealServer ()
1144 {
1145 global $root, $pageno, $tabno;
1146
1147 assertUIntArg ('id');
1148 assertUIntArg ('rs_id');
1149 assertIPv4Arg ('rsip');
1150 assertUIntArg ('rsport');
1151 assertStringArg ('rsconfig', TRUE);
1152 // only necessary for generating next URL
1153 $pool_id = $_REQUEST['id'];
1154 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
1155 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
1156 else
1157 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1158 }
1159
1160 function updateLoadBalancer ()
1161 {
1162 global $root, $pageno, $tabno;
1163
1164 assertUIntArg ('object_id');
1165 assertUIntArg ('pool_id');
1166 assertUIntArg ('vs_id');
1167 assertStringArg ('vsconfig', TRUE);
1168 assertStringArg ('rsconfig', TRUE);
1169 $pool_id = $_REQUEST['pool_id'];
1170 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1171 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1172 else
1173 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
1174 }
1175
1176 function updateVService ()
1177 {
1178 global $root, $pageno, $tabno;
1179
1180 assertUIntArg ('id');
1181 assertIPv4Arg ('vip');
1182 assertUIntArg ('vport');
1183 assertStringArg ('proto');
1184 assertStringArg ('name', TRUE);
1185 assertStringArg ('vsconfig', TRUE);
1186 assertStringArg ('rsconfig', TRUE);
1187 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1188 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
1189 else
1190 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
1191 }
1192
1193 function addLoadBalancer ()
1194 {
1195 global $root, $pageno, $tabno;
1196
1197 assertUIntArg ('pool_id');
1198 assertUIntArg ('object_id');
1199 assertUIntArg ('vs_id');
1200 assertStringArg ('vsconfig', TRUE);
1201 assertStringArg ('rsconfig', TRUE);
1202 $pool_id = $_REQUEST['pool_id'];
1203 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1204 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
1205 else
1206 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
1207 }
1208
1209 function addRSPool ()
1210 {
1211 global $root, $pageno, $tabno;
1212
1213 assertStringArg ('name', TRUE);
1214 assertStringArg ('vsconfig', TRUE);
1215 assertStringArg ('rsconfig', TRUE);
1216 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1217 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1218 else
1219 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1220 }
1221
1222 function deleteRSPool ()
1223 {
1224 global $root, $pageno, $tabno;
1225
1226 assertUIntArg ('pool_id');
1227 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1228 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1229 else
1230 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1231 }
1232
1233 function updateRSPool ()
1234 {
1235 global $root, $pageno, $tabno;
1236
1237 assertUIntArg ('pool_id');
1238 assertStringArg ('name', TRUE);
1239 assertStringArg ('vsconfig', TRUE);
1240 assertStringArg ('rsconfig', TRUE);
1241 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1242 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1243 else
1244 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1245 }
1246
1247 function updateRSInService ()
1248 {
1249 global $root, $pageno, $tabno;
1250 assertUIntArg ('pool_id');
1251 assertUIntArg ('rscount');
1252 $pool_id = $_REQUEST['pool_id'];
1253 $orig = getRSPoolInfo ($pool_id);
1254 $nbad = $ngood = 0;
1255 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1256 {
1257 $rs_id = $_REQUEST["rsid_${i}"];
1258 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1259 $newval = 'yes';
1260 else
1261 $newval = 'no';
1262 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1263 {
1264 if (commitSetInService ($rs_id, $newval))
1265 $ngood++;
1266 else
1267 $nbad++;
1268 }
1269 }
1270 if (!$nbad)
1271 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1272 else
1273 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1274 }
1275
1276 function importPTRData ()
1277 {
1278 global $root, $pageno, $tabno;
1279 assertUIntArg ('id');
1280 assertUIntArg ('addrcount');
1281 $nbad = $ngood = 0;
1282 $id = $_REQUEST['id'];
1283 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1284 {
1285 $inputname = "import_${i}";
1286 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1287 continue;
1288 assertIPv4Arg ("addr_${i}");
1289 assertStringArg ("descr_${i}", TRUE);
1290 assertStringArg ("rsvd_${i}");
1291 // Non-existent addresses will not have this argument set in request.
1292 $rsvd = 'no';
1293 if ($_REQUEST["rsvd_${i}"] == 'yes')
1294 $rsvd = 'yes';
1295 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1296 $ngood++;
1297 else
1298 $nbad++;
1299 }
1300 if (!$nbad)
1301 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
1302 else
1303 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
1304 }
1305
1306 function generateAutoPorts ()
1307 {
1308 global $root, $pageno, $tabno;
1309 assertUIntArg ('object_id');
1310 $object_id = $_REQUEST['object_id'];
1311 $info = getObjectInfo ($object_id);
1312 // Navigate away in case of success, stay at the place otherwise.
1313 if (executeAutoPorts ($object_id, $info['objtype_id']))
1314 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1315 else
1316 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1317 }
1318
1319 // Filter out implicit tags before storing the new tag set.
1320 function saveEntityTags ($realm, $bypass)
1321 {
1322 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
1323 assertUIntArg ($bypass);
1324 $entity_id = $_REQUEST[$bypass];
1325 // Build a trail from the submitted data, minimize it,
1326 // then wipe existing records and store the new set instead.
1327 wipeTags ($realm, $entity_id);
1328 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
1329 $n_succeeds = $n_errors = 0;
1330 foreach ($newtrail as $taginfo)
1331 {
1332 if (useInsertBlade ('TagStorage', array ('target_realm' => "'${realm}'", 'target_id' => $entity_id, 'tag_id' => $taginfo['id'])))
1333 $n_succeeds++;
1334 else
1335 $n_errors++;
1336 }
1337 if ($n_errors)
1338 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
1339 else
1340 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
1341 }
1342
1343 function saveObjectTags ()
1344 {
1345 return saveEntityTags ('object', 'object_id');
1346 }
1347
1348 function saveIPv4PrefixTags ()
1349 {
1350 return saveEntityTags ('ipv4net', 'id');
1351 }
1352
1353 function saveRackTags ()
1354 {
1355 return saveEntityTags ('rack', 'rack_id');
1356 }
1357
1358 function saveIPv4VSTags ()
1359 {
1360 return saveEntityTags ('ipv4vs', 'id');
1361 }
1362
1363 function saveIPv4RSPoolTags ()
1364 {
1365 return saveEntityTags ('ipv4rspool', 'id');
1366 }
1367
1368 function destroyTag ()
1369 {
1370 global $root, $pageno, $tabno;
1371 assertUIntArg ('id');
1372 if (($ret = commitDestroyTag ($_REQUEST['id'])) == '')
1373 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1374 else
1375 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1376 }
1377
1378 function createTag ()
1379 {
1380 global $root, $pageno, $tabno;
1381 assertStringArg ('tagname');
1382 assertUIntArg ('parent_id', TRUE);
1383 $tagname = trim ($_REQUEST['tagname']);
1384 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1385 $parent_id = 'NULL';
1386 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1387 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1388 else
1389 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1390 }
1391
1392 ?>