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