r1807 + use bypass arg for explicit tag loader call
[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;
501
2a201216 502 $range = $_REQUEST['range'];
e673ee24 503 $name = $_REQUEST['name'];
2dee289e
DO
504 $is_bcast = $_REQUEST['is_bcast'];
505 $error = addRange($range, $name, $is_bcast == 'on');
e673ee24
DO
506 if ($error != '')
507 {
508 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
509 }
510 else
511 {
512 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
513 }
514}
515
516function editRange ()
517{
518 global $root, $pageno, $tabno;
519
520 $id = $_REQUEST['id'];
521 $name = $_REQUEST['name'];
522 $error = updateRange($id, $name);
523 if ($error != '')
524 {
525 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
526 }
527 else
528 {
529 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
530 }
531
532}
533
534function delRange ()
535{
536 global $root, $pageno, $tabno;
537
538 $id = $_REQUEST['id'];
539 $error = commitDeleteRange ($id);
540 if ($error != '')
541 {
542 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
543 }
544 else
545 {
546 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
547 }
548
549}
550
551function editAddress ()
552{
553 global $root, $pageno, $tabno;
554
555 $ip = $_REQUEST['ip'];
556 $name = $_REQUEST['name'];
557 if (isset ($_REQUEST['reserved']))
558 $reserved = $_REQUEST['reserved'];
559 else
560 $reserved = 'off';
561 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
562 if ($error != '')
563 {
564 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
565 }
566 else
567 {
568 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
569 }
570
571}
572
573function addAddressToObject ()
574{
575 global $root, $pageno, $tabno;
576
95d68083 577 assertStringArg ('ip');
d529fdc3
DO
578 assertUIntArg ('object_id');
579 assertStringArg ('name', TRUE);
580 assertStringArg ('type');
581 // Strip masklen.
582 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
83e744c8
DO
583 $object_id = $_REQUEST['object_id'];
584 $error = bindIpToObject($ip, $object_id, $_REQUEST['name'], $_REQUEST['type']);
e673ee24 585 if ($error != '')
83e744c8 586 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
e673ee24 587 else
83e744c8 588 return "${root}?page=$pageno&tab=${tabno}&object_id=${object_id}&message=".
d529fdc3 589 urlencode("Address ${ip} was added successfully.");
e673ee24
DO
590}
591
592function createUserAccount ()
593{
594 global $root, $pageno, $tabno;
595 assertStringArg ('username');
596 assertStringArg ('realname', TRUE);
597 assertStringArg ('password');
598 $username = $_REQUEST['username'];
599 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
600 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
601 if ($result == TRUE)
602 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
603 else
604 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
605}
606
607function updateUserAccount ()
608{
609 global $root, $pageno, $tabno;
610 assertUIntArg ('id');
611 assertStringArg ('username');
612 assertStringArg ('realname', TRUE);
613 assertStringArg ('password');
614 // We might be asked to change username, so use user ID only.
615 $id = $_REQUEST['id'];
616 $username = $_REQUEST['username'];
617 $new_password = $_REQUEST['password'];
618 $old_hash = getHashByID ($id);
619 if ($old_hash == NULL)
620 {
61e269b5 621 showError ('getHashByID() failed', __FUNCTION__);
e673ee24
DO
622 return;
623 }
624 // Update user password only if provided password is not the same as current password hash.
625 if ($new_password != $old_hash)
626 $new_password = hash (PASSWORD_HASH, $new_password);
627 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
628 if ($result == TRUE)
629 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
630 else
631 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
632}
633
634function enableUserAccount ()
635{
636 global $root, $pageno, $tabno;
637 assertUIntArg ('id');
638 $id = $_REQUEST['id'];
639 $result = commitEnableUserAccount ($id, 'yes');
640 if ($result == TRUE)
641 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
642 else
643 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
644}
645
646function disableUserAccount ()
647{
648 global $root, $pageno, $tabno;
649 assertUIntArg ('id');
650 $id = $_REQUEST['id'];
651 if ($id == 1)
652 $result = FALSE;
653 else
654 $result = commitEnableUserAccount ($id, 'no');
655 if ($result == TRUE)
656 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
657 else
658 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
659}
660
661function revokePermission ()
662{
663 global $root, $pageno, $tabno;
664 assertUIntArg ('access_userid', TRUE);
665 assertStringArg ('access_page');
666 assertStringArg ('access_tab');
667 $result = commitRevokePermission
668 (
669 $_REQUEST['access_userid'],
670 $_REQUEST['access_page'],
671 $_REQUEST['access_tab']
672 );
673 if ($result == TRUE)
674 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
675 else
676 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
677}
678
679function grantPermission ()
680{
681 global $root, $pageno, $tabno;
682 assertUIntArg ('access_userid', TRUE);
683 assertStringArg ('access_page');
684 assertStringArg ('access_tab');
685 assertStringArg ('access_value');
686 $result = commitGrantPermission
687 (
688 $_REQUEST['access_userid'],
689 $_REQUEST['access_page'],
690 $_REQUEST['access_tab'],
691 $_REQUEST['access_value']
692 );
693 if ($result == TRUE)
694 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
695 else
696 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
697}
698
699// This function find differences in users's submit and PortCompat table
700// and modifies database accordingly.
701function savePortMap ()
702{
703 global $root, $pageno, $tabno;
704 $ptlist = getPortTypes();
705 $oldCompat = getPortCompat();
706 $newCompat = array();
707 foreach (array_keys ($ptlist) as $leftKey)
708 foreach (array_keys ($ptlist) as $rightKey)
709 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
710 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
711 // Build the new matrix from user's submit and compare it to
712 // the old matrix. Those pairs which appear on
713 // new matrix only, have to be stored in PortCompat table. Those who appear
714 // on the old matrix only, should be removed from PortCompat table.
715 // Those present in both matrices should be skipped.
716 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
717 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
718 $error_count = $success_count = 0;
719 foreach (array_keys ($ptlist) as $type1)
720 foreach (array_keys ($ptlist) as $type2)
721 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
722 switch ($oldCompatTable[$type1][$type2])
723 {
724 case TRUE: // new value is FALSE
725 if (removePortCompat ($type1, $type2) === TRUE)
726 $success_count++;
727 else
728 $error_count++;
729 break;
730 case FALSE: // new value is TRUE
731 if (addPortCompat ($type1, $type2) === TRUE)
732 $success_count++;
733 else
734 $error_count++;
735 break;
736 default:
61e269b5 737 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
738 break;
739 }
740 return
741 "${root}?page=${pageno}&tab=${tabno}&" .
742 ($error_count == 0 ? 'message' : 'error') .
743 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
744}
745
746function deleteDictWord ()
747{
748 global $root, $pageno, $tabno;
749 return
750 "${root}?page=${pageno}&tab=${tabno}&" .
751 "error=" . urlencode ('Dragon ate this word!');
752}
753
754function updateDictionary ()
755{
756 global $root, $pageno, $tabno;
757 assertUIntArg ('chapter_no');
758 assertUIntArg ('dict_key');
759 assertStringArg ('dict_value');
760 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
761 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
762 else
763 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
764}
765
766function supplementDictionary ()
767{
768 global $root, $pageno, $tabno;
769 assertUIntArg ('chapter_no');
770 assertStringArg ('dict_value');
771 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
772 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
773 else
774 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
775}
776
777function reduceDictionary ()
778{
779 global $root, $pageno, $tabno;
780 assertUIntArg ('chapter_no');
781 assertUIntArg ('dict_key');
782 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
783 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
784 else
785 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
786}
787
788function addChapter ()
789{
790 global $root, $pageno, $tabno;
791 assertStringArg ('chapter_name');
792 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
793 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
794 else
795 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
796}
797
798function updateChapter ()
799{
800 global $root, $pageno, $tabno;
801 assertUIntArg ('chapter_no');
802 assertStringArg ('chapter_name');
803 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
804 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
805 else
806 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
807}
808
809function delChapter ()
810{
811 global $root, $pageno, $tabno;
812 assertUIntArg ('chapter_no');
813 if (commitDeleteChapter ($_REQUEST['chapter_no']))
814 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
815 else
816 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
817}
818
819function changeAttribute ()
820{
821 global $root, $pageno, $tabno;
822 assertUIntArg ('attr_id');
823 assertStringArg ('attr_name');
824 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
825 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
826 else
827 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
828}
829
830function createAttribute ()
831{
832 global $root, $pageno, $tabno;
833 assertStringArg ('attr_name');
834 assertStringArg ('attr_type');
835 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
836 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
837 else
838 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
839}
840
841function deleteAttribute ()
842{
843 global $root, $pageno, $tabno;
844 assertUIntArg ('attr_id');
845 if (commitDeleteAttribute ($_REQUEST['attr_id']))
846 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
847 else
848 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
849}
850
851function supplementAttrMap ()
852{
853 global $root, $pageno, $tabno;
854 assertUIntArg ('attr_id');
855 assertUIntArg ('objtype_id');
856 assertUIntArg ('chapter_no');
857 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
858 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
859 else
860 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
861}
862
863function reduceAttrMap ()
864{
865 global $root, $pageno, $tabno;
866 assertUIntArg ('attr_id');
867 assertUIntArg ('objtype_id');
868 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
869 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
870 else
871 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
872}
873
874function resetAttrValue ()
875{
876 global $root, $pageno, $tabno;
877 assertUIntArg ('attr_id');
878 assertUIntArg ('object_id');
879 $object_id = $_REQUEST['object_id'];
880 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
881 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
882 else
883 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
884}
885
0dfb8a2a 886function updateAttrValues ()
e673ee24
DO
887{
888 global $root, $pageno, $tabno;
e673ee24 889 assertUIntArg ('object_id');
e673ee24 890 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
891 $oldvalues = getAttrValues ($object_id);
892
893 assertUIntArg ('num_attrs');
894 $num_attrs = $_REQUEST['num_attrs'];
895 $result = array();
896
897 for ($i = 0; $i < $num_attrs; $i++)
898 {
899 assertUIntArg ("${i}_attr_id");
900 $attr_id = $_REQUEST["${i}_attr_id"];
901
902 // Field is empty, delete attribute and move on.
903 if (empty($_REQUEST["${i}_value"]))
904 {
905 commitResetAttrValue ($object_id, $attr_id);
906 continue;
907 }
908
909 // The value could be uint/float, but we don't know ATM. Let SQL
910 // server check this and complain.
911 assertStringArg ("${i}_value");
912 $value = $_REQUEST["${i}_value"];
913 switch ($oldvalues[$attr_id]['type'])
914 {
915 case 'uint':
916 case 'float':
917 case 'string':
918 $oldvalue = $oldvalues[$attr_id]['value'];
919 break;
920 case 'dict':
921 $oldvalue = $oldvalues[$attr_id]['key'];
922 break;
923 default:
61e269b5 924 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
925 die;
926 }
927 if ($value == $oldvalue)
928 continue;
929
930 // Note if the queries succeed or not, it determines which page they see.
931 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
932 }
933
934 if (in_array(false, $result))
e673ee24 935 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
936
937 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
938}
939
940function useupPort ()
941{
942 global $root, $pageno, $tabno;
943 assertUIntArg ('port_id');
944 assertUIntArg ('object_id');
945 $object_id = $_REQUEST['object_id'];
946 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
947 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
948 else
949 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
950}
951
4fe32e78
DY
952function updateUI ()
953{
c461c579 954 global $root, $pageno, $tabno;
4fe32e78
DY
955 $oldvalues = loadConfigCache();
956
957 assertUIntArg ('num_vars');
958 $num_vars = $_REQUEST['num_vars'];
c461c579 959 $error = '';
4fe32e78
DY
960
961 for ($i = 0; $i < $num_vars; $i++)
962 {
963 assertStringArg ("${i}_varname");
1f464cff 964 assertStringArg ("${i}_varvalue", TRUE);
4fe32e78 965 $varname = $_REQUEST["${i}_varname"];
4fe32e78 966 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 967
c461c579
DO
968 // If form value = value in DB, don't bother updating DB
969 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
970 continue;
971
972 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
973 $error = setConfigVar ($varname, $varvalue, TRUE);
974 if (!empty ($error))
975 break;
4fe32e78
DY
976 }
977
c461c579
DO
978 if ($error != '')
979 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 980
c461c579 981 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
982}
983
b07f617c
DO
984function resetUIConfig()
985{
6c12ef9b 986 global $root, $pageno, $tabno;
1f86e996 987 setConfigVar ('default_port_type','24');
b07f617c
DO
988 setConfigVar ('MASSCOUNT','15');
989 setConfigVar ('MAXSELSIZE','30');
990 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
991 setConfigVar ('ROW_SCALE','2');
992 setConfigVar ('PORTS_PER_ROW','12');
993 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
994 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 995 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
996 setConfigVar ('USER_AUTH_SRC','database');
997 setConfigVar ('DEFAULT_SLB_VS_PORT','');
998 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 999 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 1000 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 1001 setConfigVar ('DETECT_URLS','no');
4b8d413e 1002 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 1003 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 1004 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
a477e405 1005 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
6c12ef9b 1006 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
1007}
1008
732e4578 1009// Add single record.
ca461127
DO
1010function addRealServer ()
1011{
1012 global $root, $pageno, $tabno;
1013
1014 assertUIntArg ('id');
2910c1d4 1015 assertIPv4Arg ('remoteip');
ca461127
DO
1016 assertUIntArg ('rsport');
1017 assertStringArg ('rsconfig', TRUE);
1018 $pool_id = $_REQUEST['id'];
2910c1d4 1019 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
d6517a21
DO
1020 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
1021 else
1022 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully added");
1023}
1024
732e4578
DO
1025// Parse textarea submitted and try adding a real server for each line.
1026function addRealServers ()
1027{
1028 global $root, $pageno, $tabno;
1029
1030 assertUIntArg ('id');
1031 assertStringArg ('format');
1032 assertStringArg ('rawtext');
1033 $pool_id = $_REQUEST['id'];
1034 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1035 $ngood = $nbad = 0;
1036 $rsconfig = '';
1037 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1038 foreach (explode ('\n', $rawtext) as $line)
1039 {
1040 if (empty ($line))
1041 continue;
1042 $match = array ();
1043 switch ($_REQUEST['format'])
1044 {
1045 case 'ipvs_2': // address and port only
1046 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1047 continue;
7e7a8387 1048 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1049 $ngood++;
1050 else
1051 $nbad++;
1052 break;
1053 case 'ipvs_3': // address, port and weight
1054 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1055 continue;
7e7a8387 1056 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1057 $ngood++;
1058 else
1059 $nbad++;
1060 break;
e69c2aa3
DO
1061 case 'ssv_2': // IP address and port
1062 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1063 continue;
1064 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1065 $ngood++;
1066 else
1067 $nbad++;
1068 break;
732e4578
DO
1069 default:
1070 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
1071 break;
1072 }
1073 }
1074 if ($nbad == 0 and $ngood > 0)
1075 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1076 else
1077 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1078}
1079
d6517a21
DO
1080function addVService ()
1081{
1082 global $root, $pageno, $tabno;
1083
1084 assertIPv4Arg ('vip');
1085 assertUIntArg ('vport');
1086 assertStringArg ('proto');
1087 $proto = $_REQUEST['proto'];
1088 if ($proto != 'TCP' and $proto != 'UDP')
1089 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
1090 assertStringArg ('name', TRUE);
1091 assertStringArg ('vsconfig', TRUE);
1092 assertStringArg ('rsconfig', TRUE);
1093 $pool_id = $_REQUEST['id'];
1094 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1095 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
ca461127 1096 else
d6517a21 1097 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
1098}
1099
1100function deleteRealServer ()
1101{
1102 global $root, $pageno, $tabno;
1103
1104 assertUIntArg ('pool_id');
1105 assertUIntArg ('id');
fb1c4a54 1106 $pool_id = $_REQUEST['pool_id'];
d6517a21
DO
1107 if (!commitDeleteRS ($_REQUEST['id']))
1108 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1109 else
d6517a21 1110 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1111}
1112
3241551e
DO
1113function deleteLoadBalancer ()
1114{
1115 global $root, $pageno, $tabno;
1116
1117 assertUIntArg ('object_id');
1118 assertUIntArg ('pool_id');
c4d85fd1 1119 assertUIntArg ('vs_id');
3241551e 1120 $pool_id = $_REQUEST['pool_id'];
c4d85fd1 1121 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
d6517a21
DO
1122 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1123 else
1124 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1125}
1126
1127function deleteVService ()
1128{
1129 global $root, $pageno, $tabno;
1130
1131 assertUIntArg ('id');
1132 if (!commitDeleteVS ($_REQUEST['id']))
1133 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
3241551e 1134 else
d6517a21 1135 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
3241551e
DO
1136}
1137
fb1c4a54
DO
1138function updateRealServer ()
1139{
1140 global $root, $pageno, $tabno;
1141
1142 assertUIntArg ('id');
732e4578 1143 assertUIntArg ('rs_id');
d6517a21 1144 assertIPv4Arg ('rsip');
fb1c4a54
DO
1145 assertUIntArg ('rsport');
1146 assertStringArg ('rsconfig', TRUE);
3241551e 1147 // only necessary for generating next URL
732e4578
DO
1148 $pool_id = $_REQUEST['id'];
1149 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
d6517a21 1150 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1151 else
d6517a21 1152 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1153}
1154
c1ca768c 1155function updateLoadBalancer ()
3241551e
DO
1156{
1157 global $root, $pageno, $tabno;
1158
1159 assertUIntArg ('object_id');
1160 assertUIntArg ('pool_id');
c1ca768c 1161 assertUIntArg ('vs_id');
3241551e
DO
1162 assertStringArg ('vsconfig', TRUE);
1163 assertStringArg ('rsconfig', TRUE);
1164 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1165 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
d6517a21
DO
1166 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1167 else
28928596 1168 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
d6517a21
DO
1169}
1170
1171function updateVService ()
1172{
1173 global $root, $pageno, $tabno;
1174
1175 assertUIntArg ('id');
1176 assertIPv4Arg ('vip');
1177 assertUIntArg ('vport');
1178 assertStringArg ('proto');
1179 assertStringArg ('name', TRUE);
1180 assertStringArg ('vsconfig', TRUE);
1181 assertStringArg ('rsconfig', TRUE);
1182 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1183 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
3241551e 1184 else
d6517a21 1185 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
3241551e
DO
1186}
1187
1188function addLoadBalancer ()
1189{
1190 global $root, $pageno, $tabno;
1191
1192 assertUIntArg ('pool_id');
1193 assertUIntArg ('object_id');
c1ca768c 1194 assertUIntArg ('vs_id');
3241551e
DO
1195 assertStringArg ('vsconfig', TRUE);
1196 assertStringArg ('rsconfig', TRUE);
1197 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1198 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
d6517a21 1199 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
3241551e 1200 else
d6517a21 1201 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
3241551e
DO
1202}
1203
5ad76f01
DO
1204function addRSPool ()
1205{
1206 global $root, $pageno, $tabno;
1207
5ad76f01
DO
1208 assertStringArg ('name', TRUE);
1209 assertStringArg ('vsconfig', TRUE);
1210 assertStringArg ('rsconfig', TRUE);
70c24883 1211 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1212 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1213 else
1214 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1215}
1216
1217function deleteRSPool ()
1218{
1219 global $root, $pageno, $tabno;
1220
1221 assertUIntArg ('id');
1222 if (!commitDeleteRSPool ($_REQUEST['id']))
1223 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1224 else
1225 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1226}
1227
1228function updateRSPool ()
1229{
1230 global $root, $pageno, $tabno;
1231
1232 assertUIntArg ('id');
1233 assertStringArg ('name', TRUE);
1234 assertStringArg ('vsconfig', TRUE);
1235 assertStringArg ('rsconfig', TRUE);
1236 if (!commitUpdateRSPool ($_REQUEST['id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1237 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1238 else
1239 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1240}
1241
1f7d18fa
DO
1242function updateRSInService ()
1243{
1244 global $root, $pageno, $tabno;
1245 assertUIntArg ('id');
1246 assertUIntArg ('rscount');
1247 $pool_id = $_REQUEST['id'];
1248 $orig = getRSPoolInfo ($pool_id);
1249 $nbad = $ngood = 0;
1250 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1251 {
1252 $rs_id = $_REQUEST["rsid_${i}"];
1253 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1254 $newval = 'yes';
1255 else
1256 $newval = 'no';
1257 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1258 {
1259 if (commitSetInService ($rs_id, $newval))
1260 $ngood++;
1261 else
1262 $nbad++;
1263 }
1264 }
1265 if (!$nbad)
1266 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1267 else
1268 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1269}
1270
03eb5209
DO
1271function importPTRData ()
1272{
1273 global $root, $pageno, $tabno;
1274 assertUIntArg ('id');
1275 assertUIntArg ('addrcount');
1276 $nbad = $ngood = 0;
1277 $id = $_REQUEST['id'];
1278 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1279 {
3f3bd41e 1280 $inputname = "import_${i}";
03eb5209
DO
1281 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1282 continue;
1283 assertIPv4Arg ("addr_${i}");
75aa085b
DO
1284 assertStringArg ("descr_${i}", TRUE);
1285 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1286 // Non-existent addresses will not have this argument set in request.
1287 $rsvd = 'no';
03eb5209
DO
1288 if ($_REQUEST["rsvd_${i}"] == 'yes')
1289 $rsvd = 'yes';
3f3bd41e 1290 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1291 $ngood++;
1292 else
1293 $nbad++;
1294 }
1295 if (!$nbad)
0aa8633e 1296 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1297 else
0aa8633e 1298 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1299}
1300
f3f0161f
DO
1301function generateAutoPorts ()
1302{
1303 global $root, $pageno, $tabno;
1304 assertUIntArg ('object_id');
1305 $object_id = $_REQUEST['object_id'];
1306 $info = getObjectInfo ($object_id);
1307 // Navigate away in case of success, stay at the place otherwise.
1308 if (executeAutoPorts ($object_id, $info['objtype_id']))
1309 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1310 else
1311 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1312}
1313
24cbe8af
DO
1314// Filter out implicit tags before storing the new tag set.
1315function saveObjectTags ()
1316{
1317 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
1318 assertUIntArg ('object_id');
1319 $object_id = $_REQUEST['object_id'];
ab379543
DO
1320 // Build a trail from the submitted data, minimize it,
1321 // then wipe existing records and store the new set instead.
fe7044ad 1322 useDeleteBlade ('RackObjectTags', 'object_id', $object_id, FALSE, TRUE);
ab379543
DO
1323 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
1324 $n_succeeds = $n_errors = 0;
1325 foreach ($newtrail as $taginfo)
24cbe8af 1326 {
ab379543
DO
1327 if (useInsertBlade ('RackObjectTags', array ('object_id' => $object_id, 'tag_id' => $taginfo['id'])))
1328 $n_succeeds++;
1329 else
1330 $n_errors++;
24cbe8af 1331 }
ab379543
DO
1332 if ($n_errors)
1333 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
1334 else
1335 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
24cbe8af
DO
1336}
1337
fe7044ad
DO
1338function destroyTag ()
1339{
1340 global $root, $pageno, $tabno;
1341 assertUIntArg ('id');
1342 if (($ret = commitDestroyTag ($_REQUEST['id'])) == '')
1343 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1344 else
1345 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1346}
1347
1348function createTag ()
1349{
1350 global $root, $pageno, $tabno;
1351 assertStringArg ('tagname');
1352 assertUIntArg ('parent_id', TRUE);
1353 $tagname = trim ($_REQUEST['tagname']);
1354 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1355 $parent_id = 'NULL';
1356 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1357 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1358 else
1359 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1360}
1361
e673ee24 1362?>