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