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