r1831 + make search form named
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
10function 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.
36function 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
55function 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
74function 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
e673ee24
DO
84function addPortForwarding ()
85{
86 global $root, $pageno, $tabno;
87
c87fd3fe
DO
88 assertUIntArg ('object_id');
89 assertIPv4Arg ('localip');
90 assertIPv4Arg ('remoteip');
91 assertUIntArg ('localport');
c87fd3fe 92 assertUIntArg ('proto');
7b1d9481 93 assertStringArg ('description', TRUE);
e673ee24
DO
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'];
e673ee24 100 $description = $_REQUEST['description'];
31fc4157
DO
101 if (empty ($remoteport))
102 $remoteport = $localport;
e673ee24
DO
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
118function 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'];
e673ee24
DO
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
141function 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
e673ee24
DO
165function 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
193function 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
227function 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
246function 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
268function 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
289function 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/*
330How Async Lines are Numbered in Cisco 3600 Series Routers
331http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
332
333Understanding 16- and 32-Port Async Network Modules
334http://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;
351d4dbf 361 case 'ssv1':
5b585342 362 $words = explode (' ', $line);
351d4dbf
DO
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;
e673ee24
DO
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
406function 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
425function 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
442function 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
459function 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
479function 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
498function addNewRange ()
499{
500 global $root, $pageno, $tabno;
5c0bb421
DO
501 assertStringArg ('range');
502 assertStringArg ('name');
e673ee24 503
2a201216 504 $range = $_REQUEST['range'];
e673ee24 505 $name = $_REQUEST['name'];
2dee289e
DO
506 $is_bcast = $_REQUEST['is_bcast'];
507 $error = addRange($range, $name, $is_bcast == 'on');
e673ee24
DO
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
518function 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
536function 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
553function 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
575function addAddressToObject ()
576{
577 global $root, $pageno, $tabno;
578
95d68083 579 assertStringArg ('ip');
d529fdc3
DO
580 assertUIntArg ('object_id');
581 assertStringArg ('name', TRUE);
582 assertStringArg ('type');
583 // Strip masklen.
584 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
83e744c8
DO
585 $object_id = $_REQUEST['object_id'];
586 $error = bindIpToObject($ip, $object_id, $_REQUEST['name'], $_REQUEST['type']);
e673ee24 587 if ($error != '')
83e744c8 588 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
e673ee24 589 else
83e744c8 590 return "${root}?page=$pageno&tab=${tabno}&object_id=${object_id}&message=".
d529fdc3 591 urlencode("Address ${ip} was added successfully.");
e673ee24
DO
592}
593
594function 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
609function 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 {
61e269b5 623 showError ('getHashByID() failed', __FUNCTION__);
e673ee24
DO
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
636function 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
648function 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
663function 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
681function 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.
703function 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:
61e269b5 739 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
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
748function deleteDictWord ()
749{
750 global $root, $pageno, $tabno;
751 return
752 "${root}?page=${pageno}&tab=${tabno}&" .
753 "error=" . urlencode ('Dragon ate this word!');
754}
755
756function 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
768function 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
779function 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
790function 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
800function 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
811function 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
821function 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
832function 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
843function 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
853function 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
865function 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
876function 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
0dfb8a2a 888function updateAttrValues ()
e673ee24
DO
889{
890 global $root, $pageno, $tabno;
e673ee24 891 assertUIntArg ('object_id');
e673ee24 892 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
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:
61e269b5 926 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
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))
e673ee24 937 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
938
939 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
940}
941
942function 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
4fe32e78
DY
954function updateUI ()
955{
c461c579 956 global $root, $pageno, $tabno;
4fe32e78
DY
957 $oldvalues = loadConfigCache();
958
959 assertUIntArg ('num_vars');
960 $num_vars = $_REQUEST['num_vars'];
c461c579 961 $error = '';
4fe32e78
DY
962
963 for ($i = 0; $i < $num_vars; $i++)
964 {
965 assertStringArg ("${i}_varname");
1f464cff 966 assertStringArg ("${i}_varvalue", TRUE);
4fe32e78 967 $varname = $_REQUEST["${i}_varname"];
4fe32e78 968 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 969
c461c579
DO
970 // If form value = value in DB, don't bother updating DB
971 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
972 continue;
973
974 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
975 $error = setConfigVar ($varname, $varvalue, TRUE);
976 if (!empty ($error))
977 break;
4fe32e78
DY
978 }
979
c461c579
DO
980 if ($error != '')
981 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 982
c461c579 983 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
984}
985
b07f617c
DO
986function resetUIConfig()
987{
6c12ef9b 988 global $root, $pageno, $tabno;
1f86e996 989 setConfigVar ('default_port_type','24');
b07f617c
DO
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');
a6305acc 997 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
998 setConfigVar ('USER_AUTH_SRC','database');
999 setConfigVar ('DEFAULT_SLB_VS_PORT','');
1000 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 1001 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 1002 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 1003 setConfigVar ('DETECT_URLS','no');
4b8d413e 1004 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 1005 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 1006 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
1007 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1008 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1009 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 1010 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
6c12ef9b 1011 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
1012}
1013
732e4578 1014// Add single record.
ca461127
DO
1015function addRealServer ()
1016{
1017 global $root, $pageno, $tabno;
1018
22a5b5ae 1019 assertUIntArg ('pool_id');
2910c1d4 1020 assertIPv4Arg ('remoteip');
ca461127
DO
1021 assertUIntArg ('rsport');
1022 assertStringArg ('rsconfig', TRUE);
22a5b5ae 1023 $pool_id = $_REQUEST['pool_id'];
2910c1d4 1024 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
841a7a7a 1025 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
d6517a21 1026 else
841a7a7a 1027 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
d6517a21
DO
1028}
1029
732e4578
DO
1030// Parse textarea submitted and try adding a real server for each line.
1031function 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;
7e7a8387 1053 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
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;
7e7a8387 1061 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1062 $ngood++;
1063 else
1064 $nbad++;
1065 break;
e69c2aa3
DO
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;
732e4578
DO
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)
841a7a7a 1080 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
732e4578 1081 else
841a7a7a 1082 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
732e4578
DO
1083}
1084
d6517a21
DO
1085function 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');
ca461127 1101 else
d6517a21 1102 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
1103}
1104
1105function deleteRealServer ()
1106{
1107 global $root, $pageno, $tabno;
1108
1109 assertUIntArg ('pool_id');
1110 assertUIntArg ('id');
fb1c4a54 1111 $pool_id = $_REQUEST['pool_id'];
d6517a21 1112 if (!commitDeleteRS ($_REQUEST['id']))
841a7a7a 1113 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1114 else
841a7a7a 1115 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1116}
1117
3241551e
DO
1118function deleteLoadBalancer ()
1119{
1120 global $root, $pageno, $tabno;
1121
1122 assertUIntArg ('object_id');
1123 assertUIntArg ('pool_id');
c4d85fd1 1124 assertUIntArg ('vs_id');
3241551e 1125 $pool_id = $_REQUEST['pool_id'];
c4d85fd1 1126 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
841a7a7a 1127 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
d6517a21 1128 else
841a7a7a 1129 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
d6517a21
DO
1130}
1131
1132function 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');
3241551e 1139 else
d6517a21 1140 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
3241551e
DO
1141}
1142
fb1c4a54
DO
1143function updateRealServer ()
1144{
1145 global $root, $pageno, $tabno;
1146
1147 assertUIntArg ('id');
732e4578 1148 assertUIntArg ('rs_id');
d6517a21 1149 assertIPv4Arg ('rsip');
fb1c4a54
DO
1150 assertUIntArg ('rsport');
1151 assertStringArg ('rsconfig', TRUE);
3241551e 1152 // only necessary for generating next URL
732e4578
DO
1153 $pool_id = $_REQUEST['id'];
1154 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
841a7a7a 1155 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1156 else
841a7a7a 1157 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1158}
1159
c1ca768c 1160function updateLoadBalancer ()
3241551e
DO
1161{
1162 global $root, $pageno, $tabno;
1163
1164 assertUIntArg ('object_id');
1165 assertUIntArg ('pool_id');
c1ca768c 1166 assertUIntArg ('vs_id');
3241551e
DO
1167 assertStringArg ('vsconfig', TRUE);
1168 assertStringArg ('rsconfig', TRUE);
1169 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1170 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
841a7a7a 1171 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
d6517a21 1172 else
841a7a7a 1173 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
d6517a21
DO
1174}
1175
1176function 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');
3241551e 1189 else
d6517a21 1190 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
3241551e
DO
1191}
1192
1193function addLoadBalancer ()
1194{
1195 global $root, $pageno, $tabno;
1196
1197 assertUIntArg ('pool_id');
1198 assertUIntArg ('object_id');
c1ca768c 1199 assertUIntArg ('vs_id');
3241551e
DO
1200 assertStringArg ('vsconfig', TRUE);
1201 assertStringArg ('rsconfig', TRUE);
1202 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1203 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
841a7a7a 1204 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
3241551e 1205 else
841a7a7a 1206 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
3241551e
DO
1207}
1208
5ad76f01
DO
1209function addRSPool ()
1210{
1211 global $root, $pageno, $tabno;
1212
5ad76f01
DO
1213 assertStringArg ('name', TRUE);
1214 assertStringArg ('vsconfig', TRUE);
1215 assertStringArg ('rsconfig', TRUE);
70c24883 1216 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
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
1222function deleteRSPool ()
1223{
1224 global $root, $pageno, $tabno;
1225
841a7a7a
DO
1226 assertUIntArg ('pool_id');
1227 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
5ad76f01
DO
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
1233function updateRSPool ()
1234{
1235 global $root, $pageno, $tabno;
1236
841a7a7a 1237 assertUIntArg ('pool_id');
5ad76f01
DO
1238 assertStringArg ('name', TRUE);
1239 assertStringArg ('vsconfig', TRUE);
1240 assertStringArg ('rsconfig', TRUE);
841a7a7a 1241 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
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
1f7d18fa
DO
1247function updateRSInService ()
1248{
1249 global $root, $pageno, $tabno;
841a7a7a 1250 assertUIntArg ('pool_id');
1f7d18fa 1251 assertUIntArg ('rscount');
841a7a7a 1252 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
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)
841a7a7a 1271 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1f7d18fa 1272 else
841a7a7a 1273 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1f7d18fa
DO
1274}
1275
03eb5209
DO
1276function 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 {
3f3bd41e 1285 $inputname = "import_${i}";
03eb5209
DO
1286 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1287 continue;
1288 assertIPv4Arg ("addr_${i}");
75aa085b
DO
1289 assertStringArg ("descr_${i}", TRUE);
1290 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1291 // Non-existent addresses will not have this argument set in request.
1292 $rsvd = 'no';
03eb5209
DO
1293 if ($_REQUEST["rsvd_${i}"] == 'yes')
1294 $rsvd = 'yes';
3f3bd41e 1295 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1296 $ngood++;
1297 else
1298 $nbad++;
1299 }
1300 if (!$nbad)
0aa8633e 1301 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1302 else
0aa8633e 1303 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1304}
1305
f3f0161f
DO
1306function 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
24cbe8af 1319// Filter out implicit tags before storing the new tag set.
2034d968 1320function saveEntityTags ($realm, $bypass)
24cbe8af
DO
1321{
1322 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
2034d968
DO
1323 assertUIntArg ($bypass);
1324 $entity_id = $_REQUEST[$bypass];
ab379543
DO
1325 // Build a trail from the submitted data, minimize it,
1326 // then wipe existing records and store the new set instead.
2034d968 1327 wipeTags ($realm, $entity_id);
ab379543
DO
1328 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
1329 $n_succeeds = $n_errors = 0;
1330 foreach ($newtrail as $taginfo)
24cbe8af 1331 {
2034d968 1332 if (useInsertBlade ('TagStorage', array ('target_realm' => "'${realm}'", 'target_id' => $entity_id, 'tag_id' => $taginfo['id'])))
ab379543
DO
1333 $n_succeeds++;
1334 else
1335 $n_errors++;
24cbe8af 1336 }
ab379543 1337 if ($n_errors)
2034d968 1338 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1339 else
2034d968
DO
1340 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
1341}
1342
1343function saveObjectTags ()
1344{
1345 return saveEntityTags ('object', 'object_id');
1346}
1347
1348function saveIPv4PrefixTags ()
1349{
1350 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1351}
1352
41bd8c87
DO
1353function saveRackTags ()
1354{
1355 return saveEntityTags ('rack', 'rack_id');
1356}
1357
d817ba03
DO
1358function saveIPv4VSTags ()
1359{
1360 return saveEntityTags ('ipv4vs', 'id');
1361}
1362
1363function saveIPv4RSPoolTags ()
1364{
1365 return saveEntityTags ('ipv4rspool', 'id');
1366}
1367
fe7044ad
DO
1368function 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
1378function 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
e673ee24 1392?>