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