r1168 Patch from Aaron Dummer (aaron@dummer.info)
[racktables] / inc / ophandlers.php
1 <?
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
21 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
22
23
24 $error=newPortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
25
26 if ($error == '')
27 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
28 else
29 {
30 return "${retpage}&error=".urlencode($error);
31 }
32
33 }
34
35 function delPortForwarding ()
36 {
37 global $root, $pageno, $tabno;
38
39 $object_id = $_REQUEST['object_id'];
40 $localip = $_REQUEST['localip'];
41 $remoteip = $_REQUEST['remoteip'];
42 $localport = $_REQUEST['localport'];
43 $remoteport = $_REQUEST['remoteport'];
44 $proto = $_REQUEST['proto'];
45 $mode = $_REQUEST['mode'];
46
47 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
48
49 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
50 if ($error == '')
51 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
52 else
53 {
54 return "${retpage}&error=".urlencode($error);
55 }
56
57 }
58
59 function updPortForwarding ()
60 {
61 global $root, $pageno, $tabno;
62
63 $object_id = $_REQUEST['object_id'];
64 $localip = $_REQUEST['localip'];
65 $remoteip = $_REQUEST['remoteip'];
66 $localport = $_REQUEST['localport'];
67 $remoteport = $_REQUEST['remoteport'];
68 $proto = $_REQUEST['proto'];
69 $description = $_REQUEST['description'];
70
71 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
72
73 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
74 if ($error == '')
75 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
76 else
77 {
78 return "${retpage}&error=".urlencode($error);
79 }
80
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 default:
282 return
283 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
284 urlencode('Cannot process submitted data: unknown format code.');
285 break;
286 }
287 }
288 // Create ports, if they don't exist.
289 $added_count = $updated_count = $error_count = 0;
290 foreach ($ports as $port)
291 {
292 $port_id = getPortID ($object_id, $port['name']);
293 if ($port_id === NULL)
294 {
295 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
296 if ($result == '')
297 $added_count++;
298 else
299 $error_count++;
300 }
301 else
302 {
303 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
304 if ($result == '')
305 $updated_count++;
306 else
307 $error_count++;
308 }
309 }
310 return
311 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
312 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
313 }
314
315 function editAddressFromObject ()
316 {
317 global $root;
318
319 $ip = $_REQUEST['ip'];
320 $object_id = $_REQUEST['object_id'];
321 $name = $_REQUEST['bond_name'];
322 $type = $_REQUEST['bond_type'];
323 $error = updateBond($ip, $object_id, $name, $type);
324 if ($error != '')
325 {
326 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
327 }
328 else
329 {
330 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
331 }
332 }
333
334 function delAddressFromObject ()
335 {
336 global $root;
337
338 $ip = $_REQUEST['ip'];
339 $object_id = $_REQUEST['object_id'];
340 $error = unbindIpFromObject($ip, $object_id);
341 if ($error != '')
342 {
343 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
344 }
345 else
346 {
347 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
348 }
349 }
350
351 function delIpAssignment ()
352 {
353 global $root;
354
355 $ip = $_REQUEST['ip'];
356 $object_id = $_REQUEST['object_id'];
357 $error = unbindIpFromObject($ip, $object_id);
358 if ($error != '')
359 {
360 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
361 }
362 else
363 {
364 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
365 }
366 }
367
368 function editIpAssignment ()
369 {
370 global $root;
371
372 $ip = $_REQUEST['ip'];
373 $object_id = $_REQUEST['object_id'];
374 $name = $_REQUEST['bond_name'];
375 $type = $_REQUEST['bond_type'];
376 $error = updateBond($ip, $object_id, $name, $type);
377
378 if ($error != '')
379 {
380 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
381 }
382 else
383 {
384 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
385 }
386 }
387
388 function addIpAssignment ()
389 {
390 global $root;
391
392 $ip = $_REQUEST['ip'];
393 $object_id = $_REQUEST['object_id'];
394 $name = $_REQUEST['bond_name'];
395 $type = $_REQUEST['bond_type'];
396 $error = bindIpToObject($ip, $object_id, $name, $type);
397 if ($error != '')
398 {
399 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
400 }
401 else
402 {
403 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
404 }
405 }
406
407 function addNewRange ()
408 {
409 global $root, $pageno, $tabno;
410
411 $range = $_REQUEST['range'];
412 $name = $_REQUEST['name'];
413 $error = addRange($range, $name);
414 if ($error != '')
415 {
416 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
417 }
418 else
419 {
420 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
421 }
422 }
423
424 function editRange ()
425 {
426 global $root, $pageno, $tabno;
427
428 $id = $_REQUEST['id'];
429 $name = $_REQUEST['name'];
430 $error = updateRange($id, $name);
431 if ($error != '')
432 {
433 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
434 }
435 else
436 {
437 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
438 }
439
440 }
441
442 function delRange ()
443 {
444 global $root, $pageno, $tabno;
445
446 $id = $_REQUEST['id'];
447 $error = commitDeleteRange ($id);
448 if ($error != '')
449 {
450 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
451 }
452 else
453 {
454 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
455 }
456
457 }
458
459 function editAddress ()
460 {
461 global $root, $pageno, $tabno;
462
463 $ip = $_REQUEST['ip'];
464 $name = $_REQUEST['name'];
465 if (isset ($_REQUEST['reserved']))
466 $reserved = $_REQUEST['reserved'];
467 else
468 $reserved = 'off';
469 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
470 if ($error != '')
471 {
472 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
473 }
474 else
475 {
476 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
477 }
478
479 }
480
481 function addAddressToObject ()
482 {
483 global $root, $pageno, $tabno;
484
485 $ip = $_REQUEST['ip'];
486 $object_id = $_REQUEST['object_id'];
487 $name = $_REQUEST['name'];
488 $type = $_REQUEST['type'];
489 $error = bindIpToObject($ip, $object_id, $name, $type);
490 if ($error != '')
491 {
492 return "${root}?page=${pageno}&tab=${tabno}&object_id=$object_id&error=".urlencode($error);
493 }
494 else
495 {
496 return "${root}?page=$pageno&tab=${tabno}&object_id=$object_id&message=".urlencode("Interface successfully added");
497 }
498 }
499
500 function createUserAccount ()
501 {
502 global $root, $pageno, $tabno;
503 assertStringArg ('username');
504 assertStringArg ('realname', TRUE);
505 assertStringArg ('password');
506 $username = $_REQUEST['username'];
507 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
508 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
509 if ($result == TRUE)
510 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
511 else
512 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
513 }
514
515 function updateUserAccount ()
516 {
517 global $root, $pageno, $tabno;
518 assertUIntArg ('id');
519 assertStringArg ('username');
520 assertStringArg ('realname', TRUE);
521 assertStringArg ('password');
522 // We might be asked to change username, so use user ID only.
523 $id = $_REQUEST['id'];
524 $username = $_REQUEST['username'];
525 $new_password = $_REQUEST['password'];
526 $old_hash = getHashByID ($id);
527 if ($old_hash == NULL)
528 {
529 showError ('getHashByID() failed in updateUserAccount()');
530 return;
531 }
532 // Update user password only if provided password is not the same as current password hash.
533 if ($new_password != $old_hash)
534 $new_password = hash (PASSWORD_HASH, $new_password);
535 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
536 if ($result == TRUE)
537 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
538 else
539 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
540 }
541
542 function enableUserAccount ()
543 {
544 global $root, $pageno, $tabno;
545 assertUIntArg ('id');
546 $id = $_REQUEST['id'];
547 $result = commitEnableUserAccount ($id, 'yes');
548 if ($result == TRUE)
549 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
550 else
551 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
552 }
553
554 function disableUserAccount ()
555 {
556 global $root, $pageno, $tabno;
557 assertUIntArg ('id');
558 $id = $_REQUEST['id'];
559 if ($id == 1)
560 $result = FALSE;
561 else
562 $result = commitEnableUserAccount ($id, 'no');
563 if ($result == TRUE)
564 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
565 else
566 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
567 }
568
569 function revokePermission ()
570 {
571 global $root, $pageno, $tabno;
572 assertUIntArg ('access_userid', TRUE);
573 assertStringArg ('access_page');
574 assertStringArg ('access_tab');
575 $result = commitRevokePermission
576 (
577 $_REQUEST['access_userid'],
578 $_REQUEST['access_page'],
579 $_REQUEST['access_tab']
580 );
581 if ($result == TRUE)
582 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
583 else
584 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
585 }
586
587 function grantPermission ()
588 {
589 global $root, $pageno, $tabno;
590 assertUIntArg ('access_userid', TRUE);
591 assertStringArg ('access_page');
592 assertStringArg ('access_tab');
593 assertStringArg ('access_value');
594 $result = commitGrantPermission
595 (
596 $_REQUEST['access_userid'],
597 $_REQUEST['access_page'],
598 $_REQUEST['access_tab'],
599 $_REQUEST['access_value']
600 );
601 if ($result == TRUE)
602 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
603 else
604 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
605 }
606
607 // This function find differences in users's submit and PortCompat table
608 // and modifies database accordingly.
609 function savePortMap ()
610 {
611 global $root, $pageno, $tabno;
612 $ptlist = getPortTypes();
613 $oldCompat = getPortCompat();
614 $newCompat = array();
615 foreach (array_keys ($ptlist) as $leftKey)
616 foreach (array_keys ($ptlist) as $rightKey)
617 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
618 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
619 // Build the new matrix from user's submit and compare it to
620 // the old matrix. Those pairs which appear on
621 // new matrix only, have to be stored in PortCompat table. Those who appear
622 // on the old matrix only, should be removed from PortCompat table.
623 // Those present in both matrices should be skipped.
624 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
625 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
626 $error_count = $success_count = 0;
627 foreach (array_keys ($ptlist) as $type1)
628 foreach (array_keys ($ptlist) as $type2)
629 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
630 switch ($oldCompatTable[$type1][$type2])
631 {
632 case TRUE: // new value is FALSE
633 if (removePortCompat ($type1, $type2) === TRUE)
634 $success_count++;
635 else
636 $error_count++;
637 break;
638 case FALSE: // new value is TRUE
639 if (addPortCompat ($type1, $type2) === TRUE)
640 $success_count++;
641 else
642 $error_count++;
643 break;
644 default:
645 showError ('oldCompatTable is invalid in savePortMap()');
646 break;
647 }
648 return
649 "${root}?page=${pageno}&tab=${tabno}&" .
650 ($error_count == 0 ? 'message' : 'error') .
651 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
652 }
653
654 function deleteDictWord ()
655 {
656 global $root, $pageno, $tabno;
657 return
658 "${root}?page=${pageno}&tab=${tabno}&" .
659 "error=" . urlencode ('Dragon ate this word!');
660 }
661
662 function updateDictionary ()
663 {
664 global $root, $pageno, $tabno;
665 assertUIntArg ('chapter_no');
666 assertUIntArg ('dict_key');
667 assertStringArg ('dict_value');
668 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
669 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
670 else
671 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
672 }
673
674 function supplementDictionary ()
675 {
676 global $root, $pageno, $tabno;
677 assertUIntArg ('chapter_no');
678 assertStringArg ('dict_value');
679 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
680 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
681 else
682 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
683 }
684
685 function reduceDictionary ()
686 {
687 global $root, $pageno, $tabno;
688 assertUIntArg ('chapter_no');
689 assertUIntArg ('dict_key');
690 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
691 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
692 else
693 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
694 }
695
696 function addChapter ()
697 {
698 global $root, $pageno, $tabno;
699 assertStringArg ('chapter_name');
700 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
701 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
702 else
703 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
704 }
705
706 function updateChapter ()
707 {
708 global $root, $pageno, $tabno;
709 assertUIntArg ('chapter_no');
710 assertStringArg ('chapter_name');
711 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
712 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
713 else
714 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
715 }
716
717 function delChapter ()
718 {
719 global $root, $pageno, $tabno;
720 assertUIntArg ('chapter_no');
721 if (commitDeleteChapter ($_REQUEST['chapter_no']))
722 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
723 else
724 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
725 }
726
727 function changeAttribute ()
728 {
729 global $root, $pageno, $tabno;
730 assertUIntArg ('attr_id');
731 assertStringArg ('attr_name');
732 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
733 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
734 else
735 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
736 }
737
738 function createAttribute ()
739 {
740 global $root, $pageno, $tabno;
741 assertStringArg ('attr_name');
742 assertStringArg ('attr_type');
743 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
744 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
745 else
746 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
747 }
748
749 function deleteAttribute ()
750 {
751 global $root, $pageno, $tabno;
752 assertUIntArg ('attr_id');
753 if (commitDeleteAttribute ($_REQUEST['attr_id']))
754 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
755 else
756 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
757 }
758
759 function supplementAttrMap ()
760 {
761 global $root, $pageno, $tabno;
762 assertUIntArg ('attr_id');
763 assertUIntArg ('objtype_id');
764 assertUIntArg ('chapter_no');
765 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
766 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
767 else
768 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
769 }
770
771 function reduceAttrMap ()
772 {
773 global $root, $pageno, $tabno;
774 assertUIntArg ('attr_id');
775 assertUIntArg ('objtype_id');
776 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
777 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
778 else
779 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
780 }
781
782 function resetAttrValue ()
783 {
784 global $root, $pageno, $tabno;
785 assertUIntArg ('attr_id');
786 assertUIntArg ('object_id');
787 $object_id = $_REQUEST['object_id'];
788 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
789 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
790 else
791 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
792 }
793
794 function updateAttrValue ()
795 {
796 global $root, $pageno, $tabno;
797 assertUIntArg ('attr_id');
798 assertUIntArg ('object_id');
799 // The value could be uint/float, but we don't know ATM. Let SQL
800 // server check this and complain.
801 assertStringArg ('value');
802 $object_id = $_REQUEST['object_id'];
803 if (commitUpdateAttrValue ($object_id, $_REQUEST['attr_id'], $_REQUEST['value']) === TRUE)
804 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
805 else
806 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
807 }
808
809 function useupPort ()
810 {
811 global $root, $pageno, $tabno;
812 assertUIntArg ('port_id');
813 assertUIntArg ('object_id');
814 $object_id = $_REQUEST['object_id'];
815 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
816 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
817 else
818 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
819 }
820
821 // This function processes a submit from the VLAN configuration form.
822 // It doesn't check any data at the moment, relying on a smart gateway.
823 // It doesn't even check if the a port already belongs to the VLAN it
824 // is being requested to be put into. This behaviour implies having a
825 // smart enough gateway, which unconditionally fetches the current
826 // configuration and then filters out 'set' requests. The gateway must
827 // validate port names and VLAN numbers as well. Ouch.
828 function updateVLANMembership ()
829 {
830 global $root, $pageno, $tabno, $remote_username;
831 assertUIntArg ('object_id');
832 assertUIntArg ('portcount');
833 $object_id = $_REQUEST['object_id'];
834 $portcount = $_REQUEST['portcount'];
835
836 $endpoints = findAllEndpoints ($object_id);
837 if (count ($endpoints) == 0)
838 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
839 urlencode ('Can\'t find any mean to reach current object. Please either set FQDN attribute or assign an IP address to the object.');
840 elseif (count ($endpoints) > 1)
841 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
842 urlencode ('More than one IP address is assigned to this object, please configure FQDN attribute.');
843
844 // Just convert the input and feed it into the gateway.
845 $questions = array();
846 for ($i = 0; $i < $portcount; $i++)
847 {
848 if (!isset ($_REQUEST["portname_${i}"]))
849 continue;
850 if (!isset ($_REQUEST["vlanid_${i}"]))
851 continue;
852 $portname = $_REQUEST["portname_${i}"];
853 $vlanid = $_REQUEST["vlanid_${i}"];
854 $questions[] = "set ${portname} ${vlanid}";
855 }
856 $data = queryGateway
857 (
858 $tabno,
859 array ($endpoints[0], 'hwtype', 'swtype', $remote_username),
860 $questions
861 );
862 $error_count = $success_count = 0;
863 foreach ($data as $reply)
864 if (strncmp ($reply, 'OK!', 3))
865 $error_count++;
866 else
867 $success_count++;
868 // Generate a message depending on error counter and redirect.
869 return
870 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&" .
871 ($error_count == 0 ? 'message' : 'error') .
872 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
873
874 }
875
876 ?>