r1573 + fixed typo
[racktables] / inc / ophandlers.php
1 <?php
2 /*
3 *
4 * This file is a library of operation handlers for RackTables.
5 *
6 */
7
8 function addPortForwarding ()
9 {
10 global $root, $pageno, $tabno;
11
12 $object_id = $_REQUEST['object_id'];
13 $localip = $_REQUEST['localip'];
14 $remoteip = $_REQUEST['remoteip'];
15 $localport = $_REQUEST['localport'];
16 $remoteport = $_REQUEST['remoteport'];
17 $proto = $_REQUEST['proto'];
18 $mode = $_REQUEST['mode'];
19 $description = $_REQUEST['description'];
20 if (empty ($remoteport))
21 $remoteport = $localport;
22
23 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
24
25
26 $error=newPortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
27
28 if ($error == '')
29 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
30 else
31 {
32 return "${retpage}&error=".urlencode($error);
33 }
34
35 }
36
37 function delPortForwarding ()
38 {
39 global $root, $pageno, $tabno;
40
41 $object_id = $_REQUEST['object_id'];
42 $localip = $_REQUEST['localip'];
43 $remoteip = $_REQUEST['remoteip'];
44 $localport = $_REQUEST['localport'];
45 $remoteport = $_REQUEST['remoteport'];
46 $proto = $_REQUEST['proto'];
47 $mode = $_REQUEST['mode'];
48
49 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
50
51 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
52 if ($error == '')
53 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
54 else
55 {
56 return "${retpage}&error=".urlencode($error);
57 }
58
59 }
60
61 function updPortForwarding ()
62 {
63 global $root, $pageno, $tabno;
64
65 $object_id = $_REQUEST['object_id'];
66 $localip = $_REQUEST['localip'];
67 $remoteip = $_REQUEST['remoteip'];
68 $localport = $_REQUEST['localport'];
69 $remoteport = $_REQUEST['remoteport'];
70 $proto = $_REQUEST['proto'];
71 $description = $_REQUEST['description'];
72
73 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
74
75 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
76 if ($error == '')
77 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
78 else
79 {
80 return "${retpage}&error=".urlencode($error);
81 }
82
83 }
84
85 function addPortForObject ()
86 {
87 global $root, $pageno, $tabno;
88
89 $object_id = $_REQUEST['object_id'];
90 $port_name = $_REQUEST['port_name'];
91 $port_l2address = $_REQUEST['port_l2address'];
92 $port_label = $_REQUEST['port_label'];
93 $port_type_id = $_REQUEST['port_type_id'];
94
95
96 if ($port_name == '')
97 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
98 else
99 {
100 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
101 if ($error != '')
102 {
103 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
104 }
105 else
106 {
107 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
108 }
109 }
110
111 }
112
113 function editPortForObject ()
114 {
115 global $root, $pageno, $tabno;
116
117 assertUIntArg ('port_id');
118 assertUIntArg ('object_id');
119 assertStringArg ('name');
120 $port_id = $_REQUEST['port_id'];
121 $object_id = $_REQUEST['object_id'];
122 $port_name = $_REQUEST['name'];
123 $port_l2address = $_REQUEST['l2address'];
124 $port_label = $_REQUEST['label'];
125 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
126 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
127 else
128 $port_rc = 'NULL';
129
130 if ($port_name == '')
131 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
132 else
133 {
134 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
135 if ($error != '')
136 {
137 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
138 }
139 else
140 {
141 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
142 }
143 }
144
145 }
146
147 function delPortFromObject ()
148 {
149 global $root, $pageno, $tabno;
150
151 $port_id = $_REQUEST['port_id'];
152 $port_name = $_REQUEST['port_name'];
153 $object_id = $_REQUEST['object_id'];
154 $error = delObjectPort($port_id);
155
156 if ($error != '')
157 {
158 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
159 }
160 else
161 {
162 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
163 }
164 }
165
166 function linkPortForObject ()
167 {
168 global $root, $pageno, $tabno;
169
170 $port_id = $_REQUEST['port_id'];
171 $remote_port_id = $_REQUEST['remote_port_id'];
172 $object_id = $_REQUEST['object_id'];
173 $port_name = $_REQUEST['port_name'];
174 $remote_port_name = $_REQUEST['remote_port_name'];
175 $remote_object_name = $_REQUEST['remote_object_name'];
176
177 $error = linkPorts($port_id, $remote_port_id);
178 if ($error != '')
179 {
180 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
181 }
182 else
183 {
184 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");
185 }
186 }
187
188 function unlinkPortForObject ()
189 {
190 global $root, $pageno, $tabno;
191
192 $port_id = $_REQUEST['port_id'];
193 $object_id = $_REQUEST['object_id'];
194 $port_name = $_REQUEST['port_name'];
195 $remote_port_name = $_REQUEST['remote_port_name'];
196 $remote_object_name = $_REQUEST['remote_object_name'];
197
198 $error = unlinkPort($port_id);
199 if ($error != '')
200 {
201 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
202 }
203 else
204 {
205 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");
206 }
207 }
208
209 function addMultiPorts ()
210 {
211 global $root, $pageno, $tabno;
212 // Parse data.
213 assertStringArg ('format');
214 assertStringArg ('input');
215 assertUIntArg ('port_type');
216 assertUIntArg ('object_id');
217 $format = $_REQUEST['format'];
218 $port_type = $_REQUEST['port_type'];
219 $object_id = $_REQUEST['object_id'];
220 // Input lines are escaped, so we have to explode and to chop by 2-char
221 // \n and \r respectively.
222 $lines1 = explode ('\n', $_REQUEST['input']);
223 foreach ($lines1 as $line)
224 {
225 $parts = explode ('\r', $line);
226 reset ($parts);
227 if (empty ($parts[0]))
228 continue;
229 else
230 $lines2[] = rtrim ($parts[0]);
231 }
232 $ports = array();
233 foreach ($lines2 as $line)
234 {
235 switch ($format)
236 {
237 case 'fisxii':
238 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
239 list ($slot, $port) = explode ('/', $words[0]);
240 $ports[] = array
241 (
242 'name' => "e ${slot}/${port}",
243 'l2address' => $words[8],
244 'label' => "slot ${slot} port ${port}"
245 );
246 break;
247 case 'c3600asy':
248 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
249 /*
250 How Async Lines are Numbered in Cisco 3600 Series Routers
251 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
252
253 Understanding 16- and 32-Port Async Network Modules
254 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
255 */
256 $async = $words[0];
257 $slot = floor (($async - 1) / 32);
258 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
259 $cable = $async - $slot * 32 - $octalgroup * 8;
260 $og_label[0] = 'async 0-7';
261 $og_label[1] = 'async 8-15';
262 $og_label[2] = 'async 16-23';
263 $og_label[3] = 'async 24-31';
264 $ports[] = array
265 (
266 'name' => "async ${async}",
267 'l2address' => '',
268 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
269 );
270 break;
271 case 'fiwg':
272 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
273 $ifnumber = $words[0] * 1;
274 $ports[] = array
275 (
276 'name' => "e ${ifnumber}",
277 'l2address' => "${words[8]}",
278 'label' => "${ifnumber}"
279 );
280 break;
281 case 'ssv1':
282 $words = explode (' ', $line);
283 if (empty ($words[0]) or empty ($words[1]))
284 continue;
285 $ports[] = array
286 (
287 'name' => $words[0],
288 'l2address' => $words[1],
289 'label' => ''
290 );
291 break;
292 default:
293 return
294 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
295 urlencode('Cannot process submitted data: unknown format code.');
296 break;
297 }
298 }
299 // Create ports, if they don't exist.
300 $added_count = $updated_count = $error_count = 0;
301 foreach ($ports as $port)
302 {
303 $port_id = getPortID ($object_id, $port['name']);
304 if ($port_id === NULL)
305 {
306 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
307 if ($result == '')
308 $added_count++;
309 else
310 $error_count++;
311 }
312 else
313 {
314 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
315 if ($result == '')
316 $updated_count++;
317 else
318 $error_count++;
319 }
320 }
321 return
322 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
323 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
324 }
325
326 function editAddressFromObject ()
327 {
328 global $root;
329
330 $ip = $_REQUEST['ip'];
331 $object_id = $_REQUEST['object_id'];
332 $name = $_REQUEST['bond_name'];
333 $type = $_REQUEST['bond_type'];
334 $error = updateBond($ip, $object_id, $name, $type);
335 if ($error != '')
336 {
337 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
338 }
339 else
340 {
341 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
342 }
343 }
344
345 function delAddressFromObject ()
346 {
347 global $root;
348
349 $ip = $_REQUEST['ip'];
350 $object_id = $_REQUEST['object_id'];
351 $error = unbindIpFromObject($ip, $object_id);
352 if ($error != '')
353 {
354 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
355 }
356 else
357 {
358 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
359 }
360 }
361
362 function delIpAssignment ()
363 {
364 global $root;
365
366 $ip = $_REQUEST['ip'];
367 $object_id = $_REQUEST['object_id'];
368 $error = unbindIpFromObject($ip, $object_id);
369 if ($error != '')
370 {
371 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
372 }
373 else
374 {
375 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
376 }
377 }
378
379 function editIpAssignment ()
380 {
381 global $root;
382
383 $ip = $_REQUEST['ip'];
384 $object_id = $_REQUEST['object_id'];
385 $name = $_REQUEST['bond_name'];
386 $type = $_REQUEST['bond_type'];
387 $error = updateBond($ip, $object_id, $name, $type);
388
389 if ($error != '')
390 {
391 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
392 }
393 else
394 {
395 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
396 }
397 }
398
399 function addIpAssignment ()
400 {
401 global $root;
402
403 $ip = $_REQUEST['ip'];
404 $object_id = $_REQUEST['object_id'];
405 $name = $_REQUEST['bond_name'];
406 $type = $_REQUEST['bond_type'];
407 $error = bindIpToObject($ip, $object_id, $name, $type);
408 if ($error != '')
409 {
410 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
411 }
412 else
413 {
414 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
415 }
416 }
417
418 function addNewRange ()
419 {
420 global $root, $pageno, $tabno;
421
422 $range = $_REQUEST['range'];
423 $name = $_REQUEST['name'];
424 $is_bcast = $_REQUEST['is_bcast'];
425 $error = addRange($range, $name, $is_bcast == 'on');
426 if ($error != '')
427 {
428 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
429 }
430 else
431 {
432 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
433 }
434 }
435
436 function editRange ()
437 {
438 global $root, $pageno, $tabno;
439
440 $id = $_REQUEST['id'];
441 $name = $_REQUEST['name'];
442 $error = updateRange($id, $name);
443 if ($error != '')
444 {
445 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
446 }
447 else
448 {
449 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
450 }
451
452 }
453
454 function delRange ()
455 {
456 global $root, $pageno, $tabno;
457
458 $id = $_REQUEST['id'];
459 $error = commitDeleteRange ($id);
460 if ($error != '')
461 {
462 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
463 }
464 else
465 {
466 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
467 }
468
469 }
470
471 function editAddress ()
472 {
473 global $root, $pageno, $tabno;
474
475 $ip = $_REQUEST['ip'];
476 $name = $_REQUEST['name'];
477 if (isset ($_REQUEST['reserved']))
478 $reserved = $_REQUEST['reserved'];
479 else
480 $reserved = 'off';
481 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
482 if ($error != '')
483 {
484 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
485 }
486 else
487 {
488 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
489 }
490
491 }
492
493 function addAddressToObject ()
494 {
495 global $root, $pageno, $tabno;
496
497 $ip = $_REQUEST['ip'];
498 $object_id = $_REQUEST['object_id'];
499 $name = $_REQUEST['name'];
500 $type = $_REQUEST['type'];
501 $error = bindIpToObject($ip, $object_id, $name, $type);
502 if ($error != '')
503 {
504 return "${root}?page=${pageno}&tab=${tabno}&object_id=$object_id&error=".urlencode($error);
505 }
506 else
507 {
508 return "${root}?page=$pageno&tab=${tabno}&object_id=$object_id&message=".urlencode("Interface successfully added");
509 }
510 }
511
512 function createUserAccount ()
513 {
514 global $root, $pageno, $tabno;
515 assertStringArg ('username');
516 assertStringArg ('realname', TRUE);
517 assertStringArg ('password');
518 $username = $_REQUEST['username'];
519 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
520 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
521 if ($result == TRUE)
522 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
523 else
524 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
525 }
526
527 function updateUserAccount ()
528 {
529 global $root, $pageno, $tabno;
530 assertUIntArg ('id');
531 assertStringArg ('username');
532 assertStringArg ('realname', TRUE);
533 assertStringArg ('password');
534 // We might be asked to change username, so use user ID only.
535 $id = $_REQUEST['id'];
536 $username = $_REQUEST['username'];
537 $new_password = $_REQUEST['password'];
538 $old_hash = getHashByID ($id);
539 if ($old_hash == NULL)
540 {
541 showError ('getHashByID() failed', __FUNCTION__);
542 return;
543 }
544 // Update user password only if provided password is not the same as current password hash.
545 if ($new_password != $old_hash)
546 $new_password = hash (PASSWORD_HASH, $new_password);
547 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
548 if ($result == TRUE)
549 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
550 else
551 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
552 }
553
554 function enableUserAccount ()
555 {
556 global $root, $pageno, $tabno;
557 assertUIntArg ('id');
558 $id = $_REQUEST['id'];
559 $result = commitEnableUserAccount ($id, 'yes');
560 if ($result == TRUE)
561 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
562 else
563 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
564 }
565
566 function disableUserAccount ()
567 {
568 global $root, $pageno, $tabno;
569 assertUIntArg ('id');
570 $id = $_REQUEST['id'];
571 if ($id == 1)
572 $result = FALSE;
573 else
574 $result = commitEnableUserAccount ($id, 'no');
575 if ($result == TRUE)
576 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
577 else
578 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
579 }
580
581 function revokePermission ()
582 {
583 global $root, $pageno, $tabno;
584 assertUIntArg ('access_userid', TRUE);
585 assertStringArg ('access_page');
586 assertStringArg ('access_tab');
587 $result = commitRevokePermission
588 (
589 $_REQUEST['access_userid'],
590 $_REQUEST['access_page'],
591 $_REQUEST['access_tab']
592 );
593 if ($result == TRUE)
594 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
595 else
596 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
597 }
598
599 function grantPermission ()
600 {
601 global $root, $pageno, $tabno;
602 assertUIntArg ('access_userid', TRUE);
603 assertStringArg ('access_page');
604 assertStringArg ('access_tab');
605 assertStringArg ('access_value');
606 $result = commitGrantPermission
607 (
608 $_REQUEST['access_userid'],
609 $_REQUEST['access_page'],
610 $_REQUEST['access_tab'],
611 $_REQUEST['access_value']
612 );
613 if ($result == TRUE)
614 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
615 else
616 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
617 }
618
619 // This function find differences in users's submit and PortCompat table
620 // and modifies database accordingly.
621 function savePortMap ()
622 {
623 global $root, $pageno, $tabno;
624 $ptlist = getPortTypes();
625 $oldCompat = getPortCompat();
626 $newCompat = array();
627 foreach (array_keys ($ptlist) as $leftKey)
628 foreach (array_keys ($ptlist) as $rightKey)
629 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
630 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
631 // Build the new matrix from user's submit and compare it to
632 // the old matrix. Those pairs which appear on
633 // new matrix only, have to be stored in PortCompat table. Those who appear
634 // on the old matrix only, should be removed from PortCompat table.
635 // Those present in both matrices should be skipped.
636 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
637 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
638 $error_count = $success_count = 0;
639 foreach (array_keys ($ptlist) as $type1)
640 foreach (array_keys ($ptlist) as $type2)
641 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
642 switch ($oldCompatTable[$type1][$type2])
643 {
644 case TRUE: // new value is FALSE
645 if (removePortCompat ($type1, $type2) === TRUE)
646 $success_count++;
647 else
648 $error_count++;
649 break;
650 case FALSE: // new value is TRUE
651 if (addPortCompat ($type1, $type2) === TRUE)
652 $success_count++;
653 else
654 $error_count++;
655 break;
656 default:
657 showError ('oldCompatTable is invalid', __FUNCTION__);
658 break;
659 }
660 return
661 "${root}?page=${pageno}&tab=${tabno}&" .
662 ($error_count == 0 ? 'message' : 'error') .
663 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
664 }
665
666 function deleteDictWord ()
667 {
668 global $root, $pageno, $tabno;
669 return
670 "${root}?page=${pageno}&tab=${tabno}&" .
671 "error=" . urlencode ('Dragon ate this word!');
672 }
673
674 function updateDictionary ()
675 {
676 global $root, $pageno, $tabno;
677 assertUIntArg ('chapter_no');
678 assertUIntArg ('dict_key');
679 assertStringArg ('dict_value');
680 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
681 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
682 else
683 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
684 }
685
686 function supplementDictionary ()
687 {
688 global $root, $pageno, $tabno;
689 assertUIntArg ('chapter_no');
690 assertStringArg ('dict_value');
691 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
692 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
693 else
694 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
695 }
696
697 function reduceDictionary ()
698 {
699 global $root, $pageno, $tabno;
700 assertUIntArg ('chapter_no');
701 assertUIntArg ('dict_key');
702 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
703 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
704 else
705 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
706 }
707
708 function addChapter ()
709 {
710 global $root, $pageno, $tabno;
711 assertStringArg ('chapter_name');
712 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
713 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
714 else
715 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
716 }
717
718 function updateChapter ()
719 {
720 global $root, $pageno, $tabno;
721 assertUIntArg ('chapter_no');
722 assertStringArg ('chapter_name');
723 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
724 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
725 else
726 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
727 }
728
729 function delChapter ()
730 {
731 global $root, $pageno, $tabno;
732 assertUIntArg ('chapter_no');
733 if (commitDeleteChapter ($_REQUEST['chapter_no']))
734 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
735 else
736 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
737 }
738
739 function changeAttribute ()
740 {
741 global $root, $pageno, $tabno;
742 assertUIntArg ('attr_id');
743 assertStringArg ('attr_name');
744 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
745 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
746 else
747 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
748 }
749
750 function createAttribute ()
751 {
752 global $root, $pageno, $tabno;
753 assertStringArg ('attr_name');
754 assertStringArg ('attr_type');
755 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
756 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
757 else
758 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
759 }
760
761 function deleteAttribute ()
762 {
763 global $root, $pageno, $tabno;
764 assertUIntArg ('attr_id');
765 if (commitDeleteAttribute ($_REQUEST['attr_id']))
766 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
767 else
768 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
769 }
770
771 function supplementAttrMap ()
772 {
773 global $root, $pageno, $tabno;
774 assertUIntArg ('attr_id');
775 assertUIntArg ('objtype_id');
776 assertUIntArg ('chapter_no');
777 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
778 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
779 else
780 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
781 }
782
783 function reduceAttrMap ()
784 {
785 global $root, $pageno, $tabno;
786 assertUIntArg ('attr_id');
787 assertUIntArg ('objtype_id');
788 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
789 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
790 else
791 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
792 }
793
794 function resetAttrValue ()
795 {
796 global $root, $pageno, $tabno;
797 assertUIntArg ('attr_id');
798 assertUIntArg ('object_id');
799 $object_id = $_REQUEST['object_id'];
800 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
801 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
802 else
803 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
804 }
805
806 function updateAttrValues ()
807 {
808 global $root, $pageno, $tabno;
809 assertUIntArg ('object_id');
810 $object_id = $_REQUEST['object_id'];
811 $oldvalues = getAttrValues ($object_id);
812
813 assertUIntArg ('num_attrs');
814 $num_attrs = $_REQUEST['num_attrs'];
815 $result = array();
816
817 for ($i = 0; $i < $num_attrs; $i++)
818 {
819 assertUIntArg ("${i}_attr_id");
820 $attr_id = $_REQUEST["${i}_attr_id"];
821
822 // Field is empty, delete attribute and move on.
823 if (empty($_REQUEST["${i}_value"]))
824 {
825 commitResetAttrValue ($object_id, $attr_id);
826 continue;
827 }
828
829 // The value could be uint/float, but we don't know ATM. Let SQL
830 // server check this and complain.
831 assertStringArg ("${i}_value");
832 $value = $_REQUEST["${i}_value"];
833 switch ($oldvalues[$attr_id]['type'])
834 {
835 case 'uint':
836 case 'float':
837 case 'string':
838 $oldvalue = $oldvalues[$attr_id]['value'];
839 break;
840 case 'dict':
841 $oldvalue = $oldvalues[$attr_id]['key'];
842 break;
843 default:
844 showError ('Internal structure error', __FUNCTION__);
845 die;
846 }
847 if ($value == $oldvalue)
848 continue;
849
850 // Note if the queries succeed or not, it determines which page they see.
851 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
852 }
853
854 if (in_array(false, $result))
855 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
856
857 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
858 }
859
860 function useupPort ()
861 {
862 global $root, $pageno, $tabno;
863 assertUIntArg ('port_id');
864 assertUIntArg ('object_id');
865 $object_id = $_REQUEST['object_id'];
866 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
867 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
868 else
869 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
870 }
871
872 // This function processes a submit from the VLAN configuration form.
873 // It doesn't check any data at the moment, relying on a smart gateway.
874 // It doesn't even check if the a port already belongs to the VLAN it
875 // is being requested to be put into. This behaviour implies having a
876 // smart enough gateway, which unconditionally fetches the current
877 // configuration and then filters out 'set' requests. The gateway must
878 // validate port names and VLAN numbers as well. Ouch.
879 function updateVLANMembership ()
880 {
881 global $root, $pageno, $tabno, $remote_username;
882 assertUIntArg ('object_id');
883 assertUIntArg ('portcount');
884 $object_id = $_REQUEST['object_id'];
885 $portcount = $_REQUEST['portcount'];
886
887 $endpoints = findAllEndpoints ($object_id);
888 if (count ($endpoints) == 0)
889 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
890 urlencode ('Can\'t find any mean to reach current object. Please either set FQDN attribute or assign an IP address to the object.');
891 elseif (count ($endpoints) > 1)
892 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
893 urlencode ('More than one IP address is assigned to this object, please configure FQDN attribute.');
894
895 // Just convert the input and feed it into the gateway.
896 $questions = array("connect ${endpoints[0]} hwtype swtype ${remote_username}");
897 for ($i = 0; $i < $portcount; $i++)
898 {
899 if (!isset ($_REQUEST["portname_${i}"]))
900 continue;
901 if (!isset ($_REQUEST["vlanid_${i}"]))
902 continue;
903 $portname = $_REQUEST["portname_${i}"];
904 $vlanid = $_REQUEST["vlanid_${i}"];
905 $questions[] = "set ${portname} ${vlanid}";
906 }
907 $data = queryGateway
908 (
909 $tabno,
910 $questions
911 );
912 $error_count = $success_count = 0;
913 foreach ($data as $reply)
914 if (strncmp ($reply, 'OK!', 3))
915 $error_count++;
916 else
917 $success_count++;
918 // Generate a message depending on error counter and redirect.
919 return
920 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&" .
921 ($error_count == 0 ? 'message' : 'error') .
922 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
923
924 }
925
926 function updateUI ()
927 {
928 global $root, $pageno, $tabno;
929 $oldvalues = loadConfigCache();
930
931 assertUIntArg ('num_vars');
932 $num_vars = $_REQUEST['num_vars'];
933 $error = '';
934
935 for ($i = 0; $i < $num_vars; $i++)
936 {
937 assertStringArg ("${i}_varname");
938 assertStringArg ("${i}_varvalue", TRUE);
939 $varname = $_REQUEST["${i}_varname"];
940 $varvalue = $_REQUEST["${i}_varvalue"];
941
942 // If form value = value in DB, don't bother updating DB
943 if ($varvalue == getConfigVar ($varname))
944 continue;
945
946 // Note if the queries succeed or not, it determines which page they see.
947 $error = setConfigVar ($varname, $varvalue, TRUE);
948 if (!empty ($error))
949 break;
950 }
951
952 if ($error != '')
953 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
954
955 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
956 }
957
958 function resetUIConfig()
959 {
960 global $root, $pageno, $tabno;
961 setConfigVar ('default_port_type','24');
962 setConfigVar ('MASSCOUNT','15');
963 setConfigVar ('MAXSELSIZE','30');
964 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
965 setConfigVar ('ROW_SCALE','2');
966 setConfigVar ('PORTS_PER_ROW','12');
967 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
968 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
969 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
970 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
971 }
972
973 // Add single record.
974 function addRealServer ()
975 {
976 global $root, $pageno, $tabno;
977
978 assertUIntArg ('id');
979 assertIPv4Arg ('rsip');
980 assertUIntArg ('rsport');
981 assertStringArg ('rsconfig', TRUE);
982 $pool_id = $_REQUEST['id'];
983 if (!addRStoRSPool ($pool_id, $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
984 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
985 else
986 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully added");
987 }
988
989 // Parse textarea submitted and try adding a real server for each line.
990 function addRealServers ()
991 {
992 global $root, $pageno, $tabno;
993
994 assertUIntArg ('id');
995 assertStringArg ('format');
996 assertStringArg ('rawtext');
997 $pool_id = $_REQUEST['id'];
998 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
999 $ngood = $nbad = 0;
1000 $rsconfig = '';
1001 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1002 foreach (explode ('\n', $rawtext) as $line)
1003 {
1004 if (empty ($line))
1005 continue;
1006 $match = array ();
1007 switch ($_REQUEST['format'])
1008 {
1009 case 'ipvs_2': // address and port only
1010 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1011 continue;
1012 if (addRStoRSPool ($pool_id, $match[1], $match[2], ''))
1013 $ngood++;
1014 else
1015 $nbad++;
1016 break;
1017 case 'ipvs_3': // address, port and weight
1018 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1019 continue;
1020 if (addRStoRSPool ($pool_id, $match[1], $match[2], 'weight ' . $match[3]))
1021 $ngood++;
1022 else
1023 $nbad++;
1024 break;
1025 default:
1026 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
1027 break;
1028 }
1029 }
1030 if ($nbad == 0 and $ngood > 0)
1031 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
1032 else
1033 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
1034 }
1035
1036 function addVService ()
1037 {
1038 global $root, $pageno, $tabno;
1039
1040 assertIPv4Arg ('vip');
1041 assertUIntArg ('vport');
1042 assertStringArg ('proto');
1043 $proto = $_REQUEST['proto'];
1044 if ($proto != 'TCP' and $proto != 'UDP')
1045 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
1046 assertStringArg ('name', TRUE);
1047 assertStringArg ('vsconfig', TRUE);
1048 assertStringArg ('rsconfig', TRUE);
1049 $pool_id = $_REQUEST['id'];
1050 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1051 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
1052 else
1053 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
1054 }
1055
1056 function deleteRealServer ()
1057 {
1058 global $root, $pageno, $tabno;
1059
1060 assertUIntArg ('pool_id');
1061 assertUIntArg ('id');
1062 $pool_id = $_REQUEST['pool_id'];
1063 if (!commitDeleteRS ($_REQUEST['id']))
1064 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
1065 else
1066 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
1067 }
1068
1069 function deleteLoadBalancer ()
1070 {
1071 global $root, $pageno, $tabno;
1072
1073 assertUIntArg ('object_id');
1074 assertUIntArg ('pool_id');
1075 assertUIntArg ('vs_id');
1076 $pool_id = $_REQUEST['pool_id'];
1077 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
1078 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
1079 else
1080 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
1081 }
1082
1083 function deleteVService ()
1084 {
1085 global $root, $pageno, $tabno;
1086
1087 assertUIntArg ('id');
1088 if (!commitDeleteVS ($_REQUEST['id']))
1089 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
1090 else
1091 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
1092 }
1093
1094 function updateRealServer ()
1095 {
1096 global $root, $pageno, $tabno;
1097
1098 assertUIntArg ('id');
1099 assertUIntArg ('rs_id');
1100 assertIPv4Arg ('rsip');
1101 assertUIntArg ('rsport');
1102 assertStringArg ('rsconfig', TRUE);
1103 // only necessary for generating next URL
1104 $pool_id = $_REQUEST['id'];
1105 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
1106 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
1107 else
1108 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1109 }
1110
1111 function updateLoadBalancer ()
1112 {
1113 global $root, $pageno, $tabno;
1114
1115 assertUIntArg ('object_id');
1116 assertUIntArg ('pool_id');
1117 assertUIntArg ('vs_id');
1118 assertStringArg ('vsconfig', TRUE);
1119 assertStringArg ('rsconfig', TRUE);
1120 $pool_id = $_REQUEST['pool_id'];
1121 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1122 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
1123 else
1124 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
1125 }
1126
1127 function updateVService ()
1128 {
1129 global $root, $pageno, $tabno;
1130
1131 assertUIntArg ('id');
1132 assertIPv4Arg ('vip');
1133 assertUIntArg ('vport');
1134 assertStringArg ('proto');
1135 assertStringArg ('name', TRUE);
1136 assertStringArg ('vsconfig', TRUE);
1137 assertStringArg ('rsconfig', TRUE);
1138 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1139 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
1140 else
1141 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
1142 }
1143
1144 function addLoadBalancer ()
1145 {
1146 global $root, $pageno, $tabno;
1147
1148 assertUIntArg ('pool_id');
1149 assertUIntArg ('object_id');
1150 assertUIntArg ('vs_id');
1151 assertStringArg ('vsconfig', TRUE);
1152 assertStringArg ('rsconfig', TRUE);
1153 $pool_id = $_REQUEST['pool_id'];
1154 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1155 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
1156 else
1157 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
1158 }
1159
1160 function addRSPool ()
1161 {
1162 global $root, $pageno, $tabno;
1163
1164 assertStringArg ('name', TRUE);
1165 assertStringArg ('vsconfig', TRUE);
1166 assertStringArg ('rsconfig', TRUE);
1167 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1168 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1169 else
1170 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1171 }
1172
1173 function deleteRSPool ()
1174 {
1175 global $root, $pageno, $tabno;
1176
1177 assertUIntArg ('id');
1178 if (!commitDeleteRSPool ($_REQUEST['id']))
1179 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1180 else
1181 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1182 }
1183
1184 function updateRSPool ()
1185 {
1186 global $root, $pageno, $tabno;
1187
1188 assertUIntArg ('id');
1189 assertStringArg ('name', TRUE);
1190 assertStringArg ('vsconfig', TRUE);
1191 assertStringArg ('rsconfig', TRUE);
1192 if (!commitUpdateRSPool ($_REQUEST['id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1193 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1194 else
1195 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1196 }
1197
1198 function updateRSInService ()
1199 {
1200 global $root, $pageno, $tabno;
1201 assertUIntArg ('id');
1202 assertUIntArg ('rscount');
1203 $pool_id = $_REQUEST['id'];
1204 $orig = getRSPoolInfo ($pool_id);
1205 $nbad = $ngood = 0;
1206 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1207 {
1208 $rs_id = $_REQUEST["rsid_${i}"];
1209 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1210 $newval = 'yes';
1211 else
1212 $newval = 'no';
1213 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1214 {
1215 if (commitSetInService ($rs_id, $newval))
1216 $ngood++;
1217 else
1218 $nbad++;
1219 }
1220 }
1221 if (!$nbad)
1222 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1223 else
1224 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1225 }
1226
1227 ?>