r2022 + working on the "my account"
[racktables] / inc / ophandlers.php
1 <?php
2 /*
3 *
4 * This file is a library of operation handlers for RackTables.
5 *
6 */
7
8 // This function assures that specified argument was passed
9 // and is a number greater than zero.
10 function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
11 {
12 if (!isset ($_REQUEST[$argname]))
13 {
14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
30 die();
31 }
32 }
33
34 // This function assures that specified argument was passed
35 // and is a non-empty string.
36 function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
37 {
38 if (!isset ($_REQUEST[$argname]))
39 {
40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
51 die();
52 }
53 }
54
55 function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
56 {
57 if (!isset ($_REQUEST[$argname]))
58 {
59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
70 die();
71 }
72 }
73
74 function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
75 {
76 assertStringArg ($argname, $caller, $ok_if_empty);
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
80 die();
81 }
82 }
83
84 function addPortForwarding ()
85 {
86 global $root, $pageno, $tabno;
87
88 assertUIntArg ('object_id', __FUNCTION__);
89 assertIPv4Arg ('localip', __FUNCTION__);
90 assertIPv4Arg ('remoteip', __FUNCTION__);
91 assertUIntArg ('localport', __FUNCTION__);
92 assertStringArg ('proto', __FUNCTION__);
93 assertStringArg ('description', __FUNCTION__, TRUE);
94 $object_id = $_REQUEST['object_id'];
95 $remoteport = $_REQUEST['remoteport'];
96 if (empty ($remoteport))
97 $remoteport = $_REQUEST['localport'];
98
99 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
100
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 );
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
121 function 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'];
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
144 function 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
168 function 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
196 function editPortForObject ()
197 {
198 global $root, $pageno, $tabno;
199
200 assertUIntArg ('port_id', __FUNCTION__);
201 assertUIntArg ('object_id', __FUNCTION__);
202 assertStringArg ('name', __FUNCTION__);
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
230 function 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
249 function 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
271 function 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
292 function addMultiPorts ()
293 {
294 global $root, $pageno, $tabno;
295 // Parse data.
296 assertStringArg ('format', __FUNCTION__);
297 assertStringArg ('input', __FUNCTION__);
298 assertUIntArg ('port_type', __FUNCTION__);
299 assertUIntArg ('object_id', __FUNCTION__);
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 /*
333 How Async Lines are Numbered in Cisco 3600 Series Routers
334 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
335
336 Understanding 16- and 32-Port Async Network Modules
337 http://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;
364 case 'ssv1':
365 $words = explode (' ', $line);
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;
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
409 function updIPv4Allocation ()
410 {
411 global $pageno, $tabno;
412 assertIPv4Arg ('ip', __FUNCTION__);
413 assertUIntArg ('object_id', __FUNCTION__);
414 assertStringArg ('bond_name', __FUNCTION__, TRUE);
415 assertStringArg ('bond_type', __FUNCTION__);
416
417 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
418 if ($error != '')
419 return buildRedirectURL_ERR ($error);
420 else
421 return buildRedirectURL_OK ('allocation updated');
422 }
423
424 function delIPv4Allocation ()
425 {
426 global $pageno, $tabno;
427 assertIPv4Arg ('ip', __FUNCTION__);
428 assertUIntArg ('object_id', __FUNCTION__);
429
430 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
431 if ($error != '')
432 return buildRedirectURL_ERR ($error);
433 else
434 return buildRedirectURL_OK ('deallocated');
435 }
436
437 function addIPv4Allocation ()
438 {
439 global $pageno, $tabno;
440 assertIPv4Arg ('ip', __FUNCTION__);
441 assertUIntArg ('object_id', __FUNCTION__);
442 assertStringArg ('bond_name', __FUNCTION__, TRUE);
443 assertStringArg ('bond_type', __FUNCTION__);
444
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 }
458 if ($error != '')
459 return buildRedirectURL_ERR ($error);
460 else
461 return buildRedirectURL_OK ('allocated');
462 }
463
464 function addIPv4Prefix ()
465 {
466 global $root, $pageno, $tabno;
467 assertStringArg ('range', __FUNCTION__);
468 assertStringArg ('name', __FUNCTION__);
469
470 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
471 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
472 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
473 if ($error != '')
474 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
475 else
476 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix successfully added");
477 }
478
479 function delIPv4Prefix ()
480 {
481 global $root, $pageno, $tabno;
482
483 assertUIntArg ('id', __FUNCTION__);
484 $error = destroyIPv4Prefix ($_REQUEST['id']);
485 if ($error != '')
486 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
487 else
488 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix deleted");
489 }
490
491 function editRange ()
492 {
493 global $pageno, $tabno;
494 assertUIntArg ('id', __FUNCTION__);
495 assertStringArg ('name', __FUNCTION__);
496
497 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
498 if ($error != '')
499 return buildRedirectURL_ERR ($error);
500 else
501 return buildRedirectURL_OK ('IPv4 prefix updated');
502 }
503
504 function editAddress ()
505 {
506 global $pageno, $tabno;
507 assertIPv4Arg ('ip', __FUNCTION__);
508 assertStringArg ('name', __FUNCTION__, TRUE);
509
510 if (isset ($_REQUEST['reserved']))
511 $reserved = $_REQUEST['reserved'];
512 else
513 $reserved = 'off';
514 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
515 if ($error != '')
516 return buildRedirectURL_ERR ($error);
517 else
518 return buildRedirectURL_OK ('IPv4 address updated');
519 }
520
521 function createUser ()
522 {
523 global $root, $pageno, $tabno;
524 assertStringArg ('username', __FUNCTION__);
525 assertStringArg ('realname', __FUNCTION__, TRUE);
526 assertStringArg ('password', __FUNCTION__);
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
536 function updateUser ()
537 {
538 global $root, $pageno, $tabno;
539 assertUIntArg ('user_id', __FUNCTION__);
540 assertStringArg ('username', __FUNCTION__);
541 assertStringArg ('realname', __FUNCTION__, TRUE);
542 assertStringArg ('password', __FUNCTION__);
543 // We might be asked to change username, so use user ID only.
544 $id = $_REQUEST['user_id'];
545 $username = $_REQUEST['username'];
546 $new_password = $_REQUEST['password'];
547 $old_hash = getHashByID ($id);
548 if ($old_hash == NULL)
549 {
550 showError ('getHashByID() failed', __FUNCTION__);
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);
556 $result = commitUpdateUserAccount ($id, $username, $_REQUEST['realname'], $new_password);
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
563 function enableUser ()
564 {
565 global $root, $pageno, $tabno;
566 assertUIntArg ('user_id', __FUNCTION__);
567 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
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
573 function disableUser ()
574 {
575 global $root, $pageno, $tabno;
576 assertUIntArg ('user_id', __FUNCTION__);
577 $id = $_REQUEST['user_id'];
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
588 // This function find differences in users's submit and PortCompat table
589 // and modifies database accordingly.
590 function 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:
626 showError ('oldCompatTable is invalid', __FUNCTION__);
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
635 function deleteDictWord ()
636 {
637 global $root, $pageno, $tabno;
638 return
639 "${root}?page=${pageno}&tab=${tabno}&" .
640 "error=" . urlencode ('Dragon ate this word!');
641 }
642
643 function updateDictionary ()
644 {
645 global $root, $pageno, $tabno;
646 assertUIntArg ('chapter_no', __FUNCTION__);
647 assertUIntArg ('dict_key', __FUNCTION__);
648 assertStringArg ('dict_value', __FUNCTION__);
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
655 function supplementDictionary ()
656 {
657 global $root, $pageno, $tabno;
658 assertUIntArg ('chapter_no', __FUNCTION__);
659 assertStringArg ('dict_value', __FUNCTION__);
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
666 function reduceDictionary ()
667 {
668 global $root, $pageno, $tabno;
669 assertUIntArg ('chapter_no', __FUNCTION__);
670 assertUIntArg ('dict_key', __FUNCTION__);
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
677 function addChapter ()
678 {
679 global $root, $pageno, $tabno;
680 assertStringArg ('chapter_name', __FUNCTION__);
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
687 function updateChapter ()
688 {
689 global $root, $pageno, $tabno;
690 assertUIntArg ('chapter_no', __FUNCTION__);
691 assertStringArg ('chapter_name', __FUNCTION__);
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
698 function delChapter ()
699 {
700 global $root, $pageno, $tabno;
701 assertUIntArg ('chapter_no', __FUNCTION__);
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
708 function changeAttribute ()
709 {
710 global $root, $pageno, $tabno;
711 assertUIntArg ('attr_id', __FUNCTION__);
712 assertStringArg ('attr_name', __FUNCTION__);
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
719 function createAttribute ()
720 {
721 global $root, $pageno, $tabno;
722 assertStringArg ('attr_name', __FUNCTION__);
723 assertStringArg ('attr_type', __FUNCTION__);
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
730 function deleteAttribute ()
731 {
732 global $root, $pageno, $tabno;
733 assertUIntArg ('attr_id', __FUNCTION__);
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
740 function supplementAttrMap ()
741 {
742 global $root, $pageno, $tabno;
743 assertUIntArg ('attr_id', __FUNCTION__);
744 assertUIntArg ('objtype_id', __FUNCTION__);
745 assertUIntArg ('chapter_no', __FUNCTION__);
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
752 function reduceAttrMap ()
753 {
754 global $root, $pageno, $tabno;
755 assertUIntArg ('attr_id', __FUNCTION__);
756 assertUIntArg ('objtype_id', __FUNCTION__);
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
763 function resetAttrValue ()
764 {
765 global $root, $pageno, $tabno;
766 assertUIntArg ('attr_id', __FUNCTION__);
767 assertUIntArg ('object_id', __FUNCTION__);
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
775 function updateAttrValues ()
776 {
777 global $root, $pageno, $tabno;
778 assertUIntArg ('object_id', __FUNCTION__);
779 $object_id = $_REQUEST['object_id'];
780 $oldvalues = getAttrValues ($object_id);
781
782 assertUIntArg ('num_attrs', __FUNCTION__);
783 $num_attrs = $_REQUEST['num_attrs'];
784 $result = array();
785
786 for ($i = 0; $i < $num_attrs; $i++)
787 {
788 assertUIntArg ("${i}_attr_id", __FUNCTION__);
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.
800 assertStringArg ("${i}_value", __FUNCTION__);
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:
813 showError ('Internal structure error', __FUNCTION__);
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))
824 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
825
826 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
827 }
828
829 function useupPort ()
830 {
831 global $root, $pageno, $tabno;
832 assertUIntArg ('port_id', __FUNCTION__);
833 assertUIntArg ('object_id', __FUNCTION__);
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
841 function updateUI ()
842 {
843 global $root, $pageno, $tabno;
844 $oldvalues = loadConfigCache();
845
846 assertUIntArg ('num_vars', __FUNCTION__);
847 $num_vars = $_REQUEST['num_vars'];
848 $error = '';
849
850 for ($i = 0; $i < $num_vars; $i++)
851 {
852 assertStringArg ("${i}_varname", __FUNCTION__);
853 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
854 $varname = $_REQUEST["${i}_varname"];
855 $varvalue = $_REQUEST["${i}_varvalue"];
856
857 // If form value = value in DB, don't bother updating DB
858 if ($varvalue == getConfigVar ($varname))
859 continue;
860
861 // Note if the queries succeed or not, it determines which page they see.
862 $error = setConfigVar ($varname, $varvalue, TRUE);
863 if (!empty ($error))
864 break;
865 }
866
867 if ($error != '')
868 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
869
870 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
871 }
872
873 function resetUIConfig()
874 {
875 global $root, $pageno, $tabno;
876 setConfigVar ('default_port_type','24');
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');
884 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
885 setConfigVar ('USER_AUTH_SRC','database');
886 setConfigVar ('DEFAULT_SLB_VS_PORT','');
887 setConfigVar ('DEFAULT_SLB_RS_PORT','');
888 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
889 setConfigVar ('NATV4_PERFORMERS','4,7,8');
890 setConfigVar ('DETECT_URLS','no');
891 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
892 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
893 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
894 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
895 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
896 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
897 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
898 setConfigVar ('IPV4_AUTO_RELEASE','1');
899 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
900 }
901
902 // Add single record.
903 function addRealServer ()
904 {
905 global $root, $pageno, $tabno;
906
907 assertUIntArg ('pool_id', __FUNCTION__);
908 assertIPv4Arg ('remoteip', __FUNCTION__);
909 assertUIntArg ('rsport', __FUNCTION__);
910 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
911 $pool_id = $_REQUEST['pool_id'];
912 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
913 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
914 else
915 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
916 }
917
918 // Parse textarea submitted and try adding a real server for each line.
919 function addRealServers ()
920 {
921 global $root, $pageno, $tabno;
922
923 assertUIntArg ('pool_id', __FUNCTION__);
924 assertStringArg ('format', __FUNCTION__);
925 assertStringArg ('rawtext', __FUNCTION__);
926 $pool_id = $_REQUEST['pool_id'];
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;
941 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
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;
949 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
950 $ngood++;
951 else
952 $nbad++;
953 break;
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;
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)
968 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
969 else
970 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
971 }
972
973 function addVService ()
974 {
975 global $root, $pageno, $tabno;
976
977 assertIPv4Arg ('vip', __FUNCTION__);
978 assertUIntArg ('vport', __FUNCTION__);
979 assertStringArg ('proto', __FUNCTION__);
980 $proto = $_REQUEST['proto'];
981 if ($proto != 'TCP' and $proto != 'UDP')
982 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
983 assertStringArg ('name', __FUNCTION__, TRUE);
984 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
985 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
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');
989 else
990 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
991 }
992
993 function deleteRealServer ()
994 {
995 global $root, $pageno, $tabno;
996
997 assertUIntArg ('pool_id', __FUNCTION__);
998 assertUIntArg ('id', __FUNCTION__);
999 $pool_id = $_REQUEST['pool_id'];
1000 if (!commitDeleteRS ($_REQUEST['id']))
1001 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
1002 else
1003 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
1004 }
1005
1006 function deleteLoadBalancer ()
1007 {
1008 global $root, $pageno, $tabno;
1009
1010 assertUIntArg ('object_id', __FUNCTION__);
1011 assertUIntArg ('pool_id', __FUNCTION__);
1012 assertUIntArg ('vs_id', __FUNCTION__);
1013 if (!commitDeleteLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id']))
1014 return buildRedirectURL_ERR ('commitDeleteLB() failed');
1015 else
1016 return buildRedirectURL_OK ('Load balancer was successfully deleted');
1017 }
1018
1019 function deleteVService ()
1020 {
1021 global $root, $pageno, $tabno;
1022
1023 assertUIntArg ('id', __FUNCTION__);
1024 if (!commitDeleteVS ($_REQUEST['id']))
1025 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
1026 else
1027 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
1028 }
1029
1030 function updateRealServer ()
1031 {
1032 global $root, $pageno, $tabno;
1033
1034 assertUIntArg ('pool_id', __FUNCTION__);
1035 assertUIntArg ('rs_id', __FUNCTION__);
1036 assertIPv4Arg ('rsip', __FUNCTION__);
1037 assertUIntArg ('rsport', __FUNCTION__);
1038 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1039 // only necessary for generating next URL
1040 $pool_id = $_REQUEST['pool_id'];
1041 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
1042 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
1043 else
1044 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1045 }
1046
1047 function updateLoadBalancer ()
1048 {
1049 global $root, $pageno, $tabno;
1050
1051 assertUIntArg ('object_id', __FUNCTION__);
1052 assertUIntArg ('pool_id', __FUNCTION__);
1053 assertUIntArg ('vs_id', __FUNCTION__);
1054 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1055 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1056 if (!commitUpdateLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1057 return buildRedirectURL_ERR ('commitUpdateLB() failed');
1058 else
1059 return buildRedirectURL_OK ('Load balancer info was successfully updated');
1060 }
1061
1062 function updateVService ()
1063 {
1064 global $root, $pageno, $tabno;
1065
1066 assertUIntArg ('id', __FUNCTION__);
1067 assertIPv4Arg ('vip', __FUNCTION__);
1068 assertUIntArg ('vport', __FUNCTION__);
1069 assertStringArg ('proto', __FUNCTION__);
1070 assertStringArg ('name', __FUNCTION__, TRUE);
1071 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1072 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1073 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1074 return buildRedirectURL_ERR ('commitUpdateVS() failed');
1075 else
1076 return buildRedirectURL_OK ('Virtual service was successfully updated');
1077 }
1078
1079 function addLoadBalancer ()
1080 {
1081 global $root, $pageno, $tabno;
1082
1083 assertUIntArg ('pool_id', __FUNCTION__);
1084 assertUIntArg ('object_id', __FUNCTION__);
1085 assertUIntArg ('vs_id', __FUNCTION__);
1086 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1087 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1088 if (!addLBtoRSPool ($_REQUEST['pool_id'], $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1089 return buildRedirectURL_ERR ('addLBtoRSPool() failed');
1090 else
1091 return buildRedirectURL_OK ('Load balancer was successfully added');
1092 }
1093
1094 function addRSPool ()
1095 {
1096 global $root, $pageno, $tabno;
1097
1098 assertStringArg ('name', __FUNCTION__, TRUE);
1099 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1100 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1101 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1102 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1103 else
1104 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1105 }
1106
1107 function deleteRSPool ()
1108 {
1109 global $root, $pageno, $tabno;
1110
1111 assertUIntArg ('pool_id', __FUNCTION__);
1112 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1113 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1114 else
1115 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1116 }
1117
1118 function updateRSPool ()
1119 {
1120 global $root, $pageno, $tabno;
1121
1122 assertUIntArg ('pool_id', __FUNCTION__);
1123 assertStringArg ('name', __FUNCTION__, TRUE);
1124 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1125 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1126 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1127 return buildRedirectURL_ERR ('commitUpdateRSPool() failed');
1128 else
1129 return buildRedirectURL_OK ('Real server pool was successfully updated');
1130 }
1131
1132 function updateRSInService ()
1133 {
1134 global $root, $pageno, $tabno;
1135 assertUIntArg ('pool_id', __FUNCTION__);
1136 assertUIntArg ('rscount', __FUNCTION__);
1137 $pool_id = $_REQUEST['pool_id'];
1138 $orig = getRSPoolInfo ($pool_id);
1139 $nbad = $ngood = 0;
1140 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1141 {
1142 $rs_id = $_REQUEST["rsid_${i}"];
1143 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1144 $newval = 'yes';
1145 else
1146 $newval = 'no';
1147 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1148 {
1149 if (commitSetInService ($rs_id, $newval))
1150 $ngood++;
1151 else
1152 $nbad++;
1153 }
1154 }
1155 if (!$nbad)
1156 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1157 else
1158 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1159 }
1160
1161 function importPTRData ()
1162 {
1163 global $root, $pageno, $tabno;
1164 assertUIntArg ('id', __FUNCTION__);
1165 assertUIntArg ('addrcount', __FUNCTION__);
1166 $nbad = $ngood = 0;
1167 $id = $_REQUEST['id'];
1168 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1169 {
1170 $inputname = "import_${i}";
1171 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1172 continue;
1173 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1174 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1175 assertStringArg ("rsvd_${i}", __FUNCTION__);
1176 // Non-existent addresses will not have this argument set in request.
1177 $rsvd = 'no';
1178 if ($_REQUEST["rsvd_${i}"] == 'yes')
1179 $rsvd = 'yes';
1180 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1181 $ngood++;
1182 else
1183 $nbad++;
1184 }
1185 if (!$nbad)
1186 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
1187 else
1188 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
1189 }
1190
1191 function generateAutoPorts ()
1192 {
1193 global $root, $pageno, $tabno;
1194 assertUIntArg ('object_id', __FUNCTION__);
1195 $object_id = $_REQUEST['object_id'];
1196 $info = getObjectInfo ($object_id);
1197 // Navigate away in case of success, stay at the place otherwise.
1198 if (executeAutoPorts ($object_id, $info['objtype_id']))
1199 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1200 else
1201 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1202 }
1203
1204 // Filter out implicit tags before storing the new tag set.
1205 function saveEntityTags ($realm, $bypass)
1206 {
1207 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
1208 assertUIntArg ($bypass, __FUNCTION__);
1209 $entity_id = $_REQUEST[$bypass];
1210 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1211 // Build a chain from the submitted data, minimize it,
1212 // then wipe existing records and store the new set instead.
1213 deleteTagsForEntity ($realm, $entity_id);
1214 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1215 $n_succeeds = $n_errors = 0;
1216 foreach ($newchain as $taginfo)
1217 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1218 $n_succeeds++;
1219 else
1220 $n_errors++;
1221 if ($n_errors)
1222 return buildRedirectURL_ERR ("Tried chaining ${n_succeeds} tags, but experienced ${n_errors} errors.");
1223 else
1224 return buildRedirectURL_OK ("Chained ${n_succeeds} tags");
1225 }
1226
1227 function saveObjectTags ()
1228 {
1229 return saveEntityTags ('object', 'object_id');
1230 }
1231
1232 function saveIPv4PrefixTags ()
1233 {
1234 return saveEntityTags ('ipv4net', 'id');
1235 }
1236
1237 function saveRackTags ()
1238 {
1239 return saveEntityTags ('rack', 'rack_id');
1240 }
1241
1242 function saveIPv4VSTags ()
1243 {
1244 return saveEntityTags ('ipv4vs', 'id');
1245 }
1246
1247 function saveIPv4RSPoolTags ()
1248 {
1249 return saveEntityTags ('ipv4rspool', 'pool_id');
1250 }
1251
1252 function saveUserTags ()
1253 {
1254 return saveEntityTags ('user', 'user_id');
1255 }
1256
1257 function destroyTag ()
1258 {
1259 global $root, $pageno, $tabno;
1260 assertUIntArg ('tag_id', __FUNCTION__);
1261 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1262 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1263 else
1264 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1265 }
1266
1267 function createTag ()
1268 {
1269 global $root, $pageno, $tabno;
1270 assertStringArg ('tag_name', __FUNCTION__);
1271 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1272 $tagname = trim ($_REQUEST['tag_name']);
1273 if (!validTagName ($tagname))
1274 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
1275 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1276 $parent_id = 'NULL';
1277 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1278 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1279 else
1280 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1281 }
1282
1283 function updateTag ()
1284 {
1285 global $root, $pageno, $tabno;
1286 assertUIntArg ('tag_id', __FUNCTION__);
1287 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1288 assertStringArg ('tag_name', __FUNCTION__);
1289 $tagname = trim ($_REQUEST['tag_name']);
1290 if (!validTagName ($tagname))
1291 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
1292 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1293 $parent_id = 'NULL';
1294 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1295 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Updated tag '${tagname}'.");
1296 else
1297 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not update tag '${tagname}' because of error '${ret}'");
1298 }
1299
1300 function rollTags ()
1301 {
1302 global $root, $pageno, $tabno;
1303 assertUIntArg ('row_id', __FUNCTION__);
1304 assertStringArg ('sum', __FUNCTION__, TRUE);
1305 assertUIntArg ('realsum', __FUNCTION__);
1306 $row_id = $_REQUEST['row_id'];
1307 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1308 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&error=" . urlencode ("test failed");
1309 $racks = getRacksForRow ($row_id);
1310 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1311 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1312 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
1313 $ndupes = $nnew = 0;
1314 foreach ($tagstack as $taginfo)
1315 foreach ($racks as $rackInfo)
1316 {
1317 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1318 $nnew++;
1319 else
1320 $ndupes++;
1321 // FIXME: do something likewise for all object inside current rack
1322 }
1323 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&message=" . urlencode ("${nnew} new records done, ${ndupes} already existed");
1324 }
1325
1326 function changeMyPassword ()
1327 {
1328 global $accounts, $root, $pageno, $tabno, $remote_username;
1329 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1330 return buildRedirectURL_ERR ('Can only change password under DB authentication.');
1331 assertStringArg ('oldpassword');
1332 assertStringArg ('newpassword1');
1333 assertStringArg ('newpassword2');
1334 if ($accounts[$remote_username]['user_password_hash'] != $_REQUEST['oldpassword'])
1335 return buildRedirectURL_ERR ('Old password doesn\'t match.');
1336 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1337 return buildRedirectURL_ERR ('New passwords don\'t match.');
1338 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
1339 return buildRedirectURL_OK ('Password changed successfully.');
1340 else
1341 return buildRedirectURL_ERR ('Password change failed.');
1342 }
1343
1344 function saveRackCode ()
1345 {
1346 global $root, $pageno, $tabno;
1347 assertStringArg ('rackcode');
1348 // For the test to succeed, unescape LFs, strip CRs.
1349 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1350 $parseTree = getRackCode ($newcode);
1351 if ($parseTree['result'] != 'ACK')
1352 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Verification failed: ' . $parseTree['load']);
1353 if (saveScript ('RackCode', $newcode))
1354 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Saved successfully.');
1355 else
1356 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Save failed.');
1357 }
1358
1359 // This handler's context is pre-built, but not authorized. It is assumed, that the
1360 // handler will take existing context and before each commit check authorization
1361 // on the base chain plus necessary context added.
1362 function setPortVLAN ()
1363 {
1364 assertUIntArg ('portcount', __FUNCTION__);
1365 $data = getSwitchVLANs ($_REQUEST['object_id']);
1366 if ($data === NULL)
1367 return buildRedirectURL_ERR ('getSwitchVLANs() failed');
1368 list ($vlanlist, $portlist) = $data;
1369 // Here we just build up 1 set command for the gateway with all of the ports
1370 // included. The gateway is expected to filter unnecessary changes silently
1371 // and to provide a list of responses with either error or success message
1372 // for each of the rest.
1373 $nports = $_REQUEST['portcount'];
1374 $prefix = 'set ';
1375 $log = array();
1376 $setcmd = '';
1377 for ($i = 0; $i < $nports; $i++)
1378 if
1379 (
1380 !isset ($_REQUEST['portname_' . $i]) ||
1381 !isset ($_REQUEST['vlanid_' . $i]) ||
1382 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1383 )
1384 $log[] = array ('code' => 'error', 'message' => "Ignoring malformed record #${i} in form submit");
1385 elseif
1386 (
1387 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1388 $portlist[$i]['vlaind'] == 'TRUNK'
1389 )
1390 continue;
1391 else
1392 {
1393 $portname = $_REQUEST['portname_' . $i];
1394 $oldvlanid = $portlist[$i]['vlanid'];
1395 $newvlanid = $_REQUEST['vlanid_' . $i];
1396 // Finish the security context and evaluate it.
1397 $annex = array();
1398 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1399 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1400 if (!permitted (NULL, NULL, NULL, $annex))
1401 {
1402 $log[] = array ('code' => 'error', 'message' => "Permission denied moving port ${portname} from VLAN${oldvlanid} to VLAN${newvlanid}");
1403 continue;
1404 }
1405 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1406 $prefix = ';';
1407 }
1408 // Feed the gateway and interpret its (non)response.
1409 if ($setcmd != '')
1410 $log = array_merge ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1411 else
1412 $log[] = array ('code' => 'warning', 'message' => 'nothing happened...');
1413 return buildWideRedirectURL ($log);
1414 }
1415
1416 ?>