r1822 + finally stick with pool_id
[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');
194e3748
DO
1005 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1006 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1007 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 1008 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
6c12ef9b 1009 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
1010}
1011
732e4578 1012// Add single record.
ca461127
DO
1013function addRealServer ()
1014{
1015 global $root, $pageno, $tabno;
1016
1017 assertUIntArg ('id');
2910c1d4 1018 assertIPv4Arg ('remoteip');
ca461127
DO
1019 assertUIntArg ('rsport');
1020 assertStringArg ('rsconfig', TRUE);
1021 $pool_id = $_REQUEST['id'];
2910c1d4 1022 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
d6517a21
DO
1023 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
1024 else
1025 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully added");
1026}
1027
732e4578
DO
1028// Parse textarea submitted and try adding a real server for each line.
1029function addRealServers ()
1030{
1031 global $root, $pageno, $tabno;
1032
1033 assertUIntArg ('id');
1034 assertStringArg ('format');
1035 assertStringArg ('rawtext');
1036 $pool_id = $_REQUEST['id'];
1037 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1038 $ngood = $nbad = 0;
1039 $rsconfig = '';
1040 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1041 foreach (explode ('\n', $rawtext) as $line)
1042 {
1043 if (empty ($line))
1044 continue;
1045 $match = array ();
1046 switch ($_REQUEST['format'])
1047 {
1048 case 'ipvs_2': // address and port only
1049 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1050 continue;
7e7a8387 1051 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1052 $ngood++;
1053 else
1054 $nbad++;
1055 break;
1056 case 'ipvs_3': // address, port and weight
1057 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1058 continue;
7e7a8387 1059 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1060 $ngood++;
1061 else
1062 $nbad++;
1063 break;
e69c2aa3
DO
1064 case 'ssv_2': // IP address and port
1065 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1066 continue;
1067 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1068 $ngood++;
1069 else
1070 $nbad++;
1071 break;
732e4578
DO
1072 default:
1073 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
1074 break;
1075 }
1076 }
1077 if ($nbad == 0 and $ngood > 0)
1078 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1079 else
1080 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1081}
1082
d6517a21
DO
1083function addVService ()
1084{
1085 global $root, $pageno, $tabno;
1086
1087 assertIPv4Arg ('vip');
1088 assertUIntArg ('vport');
1089 assertStringArg ('proto');
1090 $proto = $_REQUEST['proto'];
1091 if ($proto != 'TCP' and $proto != 'UDP')
1092 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
1093 assertStringArg ('name', TRUE);
1094 assertStringArg ('vsconfig', TRUE);
1095 assertStringArg ('rsconfig', TRUE);
1096 $pool_id = $_REQUEST['id'];
1097 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1098 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
ca461127 1099 else
d6517a21 1100 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
1101}
1102
1103function deleteRealServer ()
1104{
1105 global $root, $pageno, $tabno;
1106
1107 assertUIntArg ('pool_id');
1108 assertUIntArg ('id');
fb1c4a54 1109 $pool_id = $_REQUEST['pool_id'];
d6517a21
DO
1110 if (!commitDeleteRS ($_REQUEST['id']))
1111 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1112 else
d6517a21 1113 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1114}
1115
3241551e
DO
1116function deleteLoadBalancer ()
1117{
1118 global $root, $pageno, $tabno;
1119
1120 assertUIntArg ('object_id');
1121 assertUIntArg ('pool_id');
c4d85fd1 1122 assertUIntArg ('vs_id');
3241551e 1123 $pool_id = $_REQUEST['pool_id'];
c4d85fd1 1124 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
d6517a21
DO
1125 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1126 else
1127 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1128}
1129
1130function deleteVService ()
1131{
1132 global $root, $pageno, $tabno;
1133
1134 assertUIntArg ('id');
1135 if (!commitDeleteVS ($_REQUEST['id']))
1136 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
3241551e 1137 else
d6517a21 1138 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
3241551e
DO
1139}
1140
fb1c4a54
DO
1141function updateRealServer ()
1142{
1143 global $root, $pageno, $tabno;
1144
1145 assertUIntArg ('id');
732e4578 1146 assertUIntArg ('rs_id');
d6517a21 1147 assertIPv4Arg ('rsip');
fb1c4a54
DO
1148 assertUIntArg ('rsport');
1149 assertStringArg ('rsconfig', TRUE);
3241551e 1150 // only necessary for generating next URL
732e4578
DO
1151 $pool_id = $_REQUEST['id'];
1152 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
d6517a21 1153 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1154 else
d6517a21 1155 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1156}
1157
c1ca768c 1158function updateLoadBalancer ()
3241551e
DO
1159{
1160 global $root, $pageno, $tabno;
1161
1162 assertUIntArg ('object_id');
1163 assertUIntArg ('pool_id');
c1ca768c 1164 assertUIntArg ('vs_id');
3241551e
DO
1165 assertStringArg ('vsconfig', TRUE);
1166 assertStringArg ('rsconfig', TRUE);
1167 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1168 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
d6517a21
DO
1169 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1170 else
28928596 1171 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
d6517a21
DO
1172}
1173
1174function updateVService ()
1175{
1176 global $root, $pageno, $tabno;
1177
1178 assertUIntArg ('id');
1179 assertIPv4Arg ('vip');
1180 assertUIntArg ('vport');
1181 assertStringArg ('proto');
1182 assertStringArg ('name', TRUE);
1183 assertStringArg ('vsconfig', TRUE);
1184 assertStringArg ('rsconfig', TRUE);
1185 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1186 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
3241551e 1187 else
d6517a21 1188 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
3241551e
DO
1189}
1190
1191function addLoadBalancer ()
1192{
1193 global $root, $pageno, $tabno;
1194
1195 assertUIntArg ('pool_id');
1196 assertUIntArg ('object_id');
c1ca768c 1197 assertUIntArg ('vs_id');
3241551e
DO
1198 assertStringArg ('vsconfig', TRUE);
1199 assertStringArg ('rsconfig', TRUE);
1200 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1201 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
d6517a21 1202 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
3241551e 1203 else
d6517a21 1204 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
3241551e
DO
1205}
1206
5ad76f01
DO
1207function addRSPool ()
1208{
1209 global $root, $pageno, $tabno;
1210
5ad76f01
DO
1211 assertStringArg ('name', TRUE);
1212 assertStringArg ('vsconfig', TRUE);
1213 assertStringArg ('rsconfig', TRUE);
70c24883 1214 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1215 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1216 else
1217 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1218}
1219
1220function deleteRSPool ()
1221{
1222 global $root, $pageno, $tabno;
1223
1224 assertUIntArg ('id');
1225 if (!commitDeleteRSPool ($_REQUEST['id']))
1226 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1227 else
1228 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1229}
1230
1231function updateRSPool ()
1232{
1233 global $root, $pageno, $tabno;
1234
1235 assertUIntArg ('id');
1236 assertStringArg ('name', TRUE);
1237 assertStringArg ('vsconfig', TRUE);
1238 assertStringArg ('rsconfig', TRUE);
1239 if (!commitUpdateRSPool ($_REQUEST['id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1240 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1241 else
1242 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1243}
1244
1f7d18fa
DO
1245function updateRSInService ()
1246{
1247 global $root, $pageno, $tabno;
1248 assertUIntArg ('id');
1249 assertUIntArg ('rscount');
1250 $pool_id = $_REQUEST['id'];
1251 $orig = getRSPoolInfo ($pool_id);
1252 $nbad = $ngood = 0;
1253 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1254 {
1255 $rs_id = $_REQUEST["rsid_${i}"];
1256 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1257 $newval = 'yes';
1258 else
1259 $newval = 'no';
1260 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1261 {
1262 if (commitSetInService ($rs_id, $newval))
1263 $ngood++;
1264 else
1265 $nbad++;
1266 }
1267 }
1268 if (!$nbad)
1269 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1270 else
1271 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1272}
1273
03eb5209
DO
1274function importPTRData ()
1275{
1276 global $root, $pageno, $tabno;
1277 assertUIntArg ('id');
1278 assertUIntArg ('addrcount');
1279 $nbad = $ngood = 0;
1280 $id = $_REQUEST['id'];
1281 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1282 {
3f3bd41e 1283 $inputname = "import_${i}";
03eb5209
DO
1284 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1285 continue;
1286 assertIPv4Arg ("addr_${i}");
75aa085b
DO
1287 assertStringArg ("descr_${i}", TRUE);
1288 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1289 // Non-existent addresses will not have this argument set in request.
1290 $rsvd = 'no';
03eb5209
DO
1291 if ($_REQUEST["rsvd_${i}"] == 'yes')
1292 $rsvd = 'yes';
3f3bd41e 1293 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1294 $ngood++;
1295 else
1296 $nbad++;
1297 }
1298 if (!$nbad)
0aa8633e 1299 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1300 else
0aa8633e 1301 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1302}
1303
f3f0161f
DO
1304function generateAutoPorts ()
1305{
1306 global $root, $pageno, $tabno;
1307 assertUIntArg ('object_id');
1308 $object_id = $_REQUEST['object_id'];
1309 $info = getObjectInfo ($object_id);
1310 // Navigate away in case of success, stay at the place otherwise.
1311 if (executeAutoPorts ($object_id, $info['objtype_id']))
1312 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1313 else
1314 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1315}
1316
24cbe8af 1317// Filter out implicit tags before storing the new tag set.
2034d968 1318function saveEntityTags ($realm, $bypass)
24cbe8af
DO
1319{
1320 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
2034d968
DO
1321 assertUIntArg ($bypass);
1322 $entity_id = $_REQUEST[$bypass];
ab379543
DO
1323 // Build a trail from the submitted data, minimize it,
1324 // then wipe existing records and store the new set instead.
2034d968 1325 wipeTags ($realm, $entity_id);
ab379543
DO
1326 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
1327 $n_succeeds = $n_errors = 0;
1328 foreach ($newtrail as $taginfo)
24cbe8af 1329 {
2034d968 1330 if (useInsertBlade ('TagStorage', array ('target_realm' => "'${realm}'", 'target_id' => $entity_id, 'tag_id' => $taginfo['id'])))
ab379543
DO
1331 $n_succeeds++;
1332 else
1333 $n_errors++;
24cbe8af 1334 }
ab379543 1335 if ($n_errors)
2034d968 1336 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1337 else
2034d968
DO
1338 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
1339}
1340
1341function saveObjectTags ()
1342{
1343 return saveEntityTags ('object', 'object_id');
1344}
1345
1346function saveIPv4PrefixTags ()
1347{
1348 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1349}
1350
41bd8c87
DO
1351function saveRackTags ()
1352{
1353 return saveEntityTags ('rack', 'rack_id');
1354}
1355
d817ba03
DO
1356function saveIPv4VSTags ()
1357{
1358 return saveEntityTags ('ipv4vs', 'id');
1359}
1360
1361function saveIPv4RSPoolTags ()
1362{
1363 return saveEntityTags ('ipv4rspool', 'id');
1364}
1365
fe7044ad
DO
1366function destroyTag ()
1367{
1368 global $root, $pageno, $tabno;
1369 assertUIntArg ('id');
1370 if (($ret = commitDestroyTag ($_REQUEST['id'])) == '')
1371 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1372 else
1373 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1374}
1375
1376function createTag ()
1377{
1378 global $root, $pageno, $tabno;
1379 assertStringArg ('tagname');
1380 assertUIntArg ('parent_id', TRUE);
1381 $tagname = trim ($_REQUEST['tagname']);
1382 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1383 $parent_id = 'NULL';
1384 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1385 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1386 else
1387 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1388}
1389
e673ee24 1390?>