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