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