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