r1281 + setConfigVar() didn't work ever, fixed
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
8function 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
35function 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
59function 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
e673ee24
DO
83function addPortForObject ()
84{
85 global $root, $pageno, $tabno;
86
87 $object_id = $_REQUEST['object_id'];
88 $port_name = $_REQUEST['port_name'];
89 $port_l2address = $_REQUEST['port_l2address'];
90 $port_label = $_REQUEST['port_label'];
91 $port_type_id = $_REQUEST['port_type_id'];
92
93
94 if ($port_name == '')
95 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
96 else
97 {
98 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
99 if ($error != '')
100 {
101 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
102 }
103 else
104 {
105 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
106 }
107 }
108
109}
110
111function editPortForObject ()
112{
113 global $root, $pageno, $tabno;
114
115 assertUIntArg ('port_id');
116 assertUIntArg ('object_id');
117 assertStringArg ('name');
118 $port_id = $_REQUEST['port_id'];
119 $object_id = $_REQUEST['object_id'];
120 $port_name = $_REQUEST['name'];
121 $port_l2address = $_REQUEST['l2address'];
122 $port_label = $_REQUEST['label'];
123 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
124 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
125 else
126 $port_rc = 'NULL';
127
128 if ($port_name == '')
129 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
130 else
131 {
132 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
133 if ($error != '')
134 {
135 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
136 }
137 else
138 {
139 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
140 }
141 }
142
143}
144
145function delPortFromObject ()
146{
147 global $root, $pageno, $tabno;
148
149 $port_id = $_REQUEST['port_id'];
150 $port_name = $_REQUEST['port_name'];
151 $object_id = $_REQUEST['object_id'];
152 $error = delObjectPort($port_id);
153
154 if ($error != '')
155 {
156 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
157 }
158 else
159 {
160 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
161 }
162}
163
164function linkPortForObject ()
165{
166 global $root, $pageno, $tabno;
167
168 $port_id = $_REQUEST['port_id'];
169 $remote_port_id = $_REQUEST['remote_port_id'];
170 $object_id = $_REQUEST['object_id'];
171 $port_name = $_REQUEST['port_name'];
172 $remote_port_name = $_REQUEST['remote_port_name'];
173 $remote_object_name = $_REQUEST['remote_object_name'];
174
175 $error = linkPorts($port_id, $remote_port_id);
176 if ($error != '')
177 {
178 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
179 }
180 else
181 {
182 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");
183 }
184}
185
186function unlinkPortForObject ()
187{
188 global $root, $pageno, $tabno;
189
190 $port_id = $_REQUEST['port_id'];
191 $object_id = $_REQUEST['object_id'];
192 $port_name = $_REQUEST['port_name'];
193 $remote_port_name = $_REQUEST['remote_port_name'];
194 $remote_object_name = $_REQUEST['remote_object_name'];
195
196 $error = unlinkPort($port_id);
197 if ($error != '')
198 {
199 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
200 }
201 else
202 {
203 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");
204 }
205}
206
207function addMultiPorts ()
208{
209 global $root, $pageno, $tabno;
210 // Parse data.
211 assertStringArg ('format');
212 assertStringArg ('input');
213 assertUIntArg ('port_type');
214 assertUIntArg ('object_id');
215 $format = $_REQUEST['format'];
216 $port_type = $_REQUEST['port_type'];
217 $object_id = $_REQUEST['object_id'];
218 // Input lines are escaped, so we have to explode and to chop by 2-char
219 // \n and \r respectively.
220 $lines1 = explode ('\n', $_REQUEST['input']);
221 foreach ($lines1 as $line)
222 {
223 $parts = explode ('\r', $line);
224 reset ($parts);
225 if (empty ($parts[0]))
226 continue;
227 else
228 $lines2[] = rtrim ($parts[0]);
229 }
230 $ports = array();
231 foreach ($lines2 as $line)
232 {
233 switch ($format)
234 {
235 case 'fisxii':
236 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
237 list ($slot, $port) = explode ('/', $words[0]);
238 $ports[] = array
239 (
240 'name' => "e ${slot}/${port}",
241 'l2address' => $words[8],
242 'label' => "slot ${slot} port ${port}"
243 );
244 break;
245 case 'c3600asy':
246 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
247/*
248How Async Lines are Numbered in Cisco 3600 Series Routers
249http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
250
251Understanding 16- and 32-Port Async Network Modules
252http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
253*/
254 $async = $words[0];
255 $slot = floor (($async - 1) / 32);
256 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
257 $cable = $async - $slot * 32 - $octalgroup * 8;
258 $og_label[0] = 'async 0-7';
259 $og_label[1] = 'async 8-15';
260 $og_label[2] = 'async 16-23';
261 $og_label[3] = 'async 24-31';
262 $ports[] = array
263 (
264 'name' => "async ${async}",
265 'l2address' => '',
266 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
267 );
268 break;
269 case 'fiwg':
270 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
271 $ifnumber = $words[0] * 1;
272 $ports[] = array
273 (
274 'name' => "e ${ifnumber}",
275 'l2address' => "${words[8]}",
276 'label' => "${ifnumber}"
277 );
278 break;
279 default:
280 return
281 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
282 urlencode('Cannot process submitted data: unknown format code.');
283 break;
284 }
285 }
286 // Create ports, if they don't exist.
287 $added_count = $updated_count = $error_count = 0;
288 foreach ($ports as $port)
289 {
290 $port_id = getPortID ($object_id, $port['name']);
291 if ($port_id === NULL)
292 {
293 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
294 if ($result == '')
295 $added_count++;
296 else
297 $error_count++;
298 }
299 else
300 {
301 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
302 if ($result == '')
303 $updated_count++;
304 else
305 $error_count++;
306 }
307 }
308 return
309 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
310 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
311}
312
313function editAddressFromObject ()
314{
315 global $root;
316
317 $ip = $_REQUEST['ip'];
318 $object_id = $_REQUEST['object_id'];
319 $name = $_REQUEST['bond_name'];
320 $type = $_REQUEST['bond_type'];
321 $error = updateBond($ip, $object_id, $name, $type);
322 if ($error != '')
323 {
324 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
325 }
326 else
327 {
328 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully updated");
329 }
330}
331
332function delAddressFromObject ()
333{
334 global $root;
335
336 $ip = $_REQUEST['ip'];
337 $object_id = $_REQUEST['object_id'];
338 $error = unbindIpFromObject($ip, $object_id);
339 if ($error != '')
340 {
341 return "${root}?page=object&tab=network&object_id=$object_id&error=".urlencode($error);
342 }
343 else
344 {
345 return "${root}?page=object&tab=network&object_id=$object_id&message=".urlencode("Interface successfully deleted");
346 }
347}
348
349function delIpAssignment ()
350{
351 global $root;
352
353 $ip = $_REQUEST['ip'];
354 $object_id = $_REQUEST['object_id'];
355 $error = unbindIpFromObject($ip, $object_id);
356 if ($error != '')
357 {
358 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
359 }
360 else
361 {
362 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully deleted");
363 }
364}
365
366function editIpAssignment ()
367{
368 global $root;
369
370 $ip = $_REQUEST['ip'];
371 $object_id = $_REQUEST['object_id'];
372 $name = $_REQUEST['bond_name'];
373 $type = $_REQUEST['bond_type'];
374 $error = updateBond($ip, $object_id, $name, $type);
375
376 if ($error != '')
377 {
378 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
379 }
380 else
381 {
382 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully updated");
383 }
384}
385
386function addIpAssignment ()
387{
388 global $root;
389
390 $ip = $_REQUEST['ip'];
391 $object_id = $_REQUEST['object_id'];
392 $name = $_REQUEST['bond_name'];
393 $type = $_REQUEST['bond_type'];
394 $error = bindIpToObject($ip, $object_id, $name, $type);
395 if ($error != '')
396 {
397 return "${root}?page=ipaddress&tab=assignment&ip=$ip&error=".urlencode($error);
398 }
399 else
400 {
401 return "${root}?page=ipaddress&tab=assignment&ip=$ip&message=".urlencode("Interface successfully added");
402 }
403}
404
405function addNewRange ()
406{
407 global $root, $pageno, $tabno;
408
2a201216 409 $range = $_REQUEST['range'];
e673ee24 410 $name = $_REQUEST['name'];
2a201216 411 $error = addRange($range, $name);
e673ee24
DO
412 if ($error != '')
413 {
414 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
415 }
416 else
417 {
418 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range successfully added");
419 }
420}
421
422function editRange ()
423{
424 global $root, $pageno, $tabno;
425
426 $id = $_REQUEST['id'];
427 $name = $_REQUEST['name'];
428 $error = updateRange($id, $name);
429 if ($error != '')
430 {
431 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
432 }
433 else
434 {
435 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
436 }
437
438}
439
440function delRange ()
441{
442 global $root, $pageno, $tabno;
443
444 $id = $_REQUEST['id'];
445 $error = commitDeleteRange ($id);
446 if ($error != '')
447 {
448 return "${root}?page=${pageno}&tab=${tabno}&error=".urlencode($error);
449 }
450 else
451 {
452 return "${root}?page=${pageno}&tab=${tabno}&message=".urlencode("Range deleted");
453 }
454
455}
456
457function editAddress ()
458{
459 global $root, $pageno, $tabno;
460
461 $ip = $_REQUEST['ip'];
462 $name = $_REQUEST['name'];
463 if (isset ($_REQUEST['reserved']))
464 $reserved = $_REQUEST['reserved'];
465 else
466 $reserved = 'off';
467 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
468 if ($error != '')
469 {
470 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
471 }
472 else
473 {
474 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
475 }
476
477}
478
479function addAddressToObject ()
480{
481 global $root, $pageno, $tabno;
482
483 $ip = $_REQUEST['ip'];
484 $object_id = $_REQUEST['object_id'];
485 $name = $_REQUEST['name'];
486 $type = $_REQUEST['type'];
487 $error = bindIpToObject($ip, $object_id, $name, $type);
488 if ($error != '')
489 {
490 return "${root}?page=${pageno}&tab=${tabno}&object_id=$object_id&error=".urlencode($error);
491 }
492 else
493 {
494 return "${root}?page=$pageno&tab=${tabno}&object_id=$object_id&message=".urlencode("Interface successfully added");
495 }
496}
497
498function createUserAccount ()
499{
500 global $root, $pageno, $tabno;
501 assertStringArg ('username');
502 assertStringArg ('realname', TRUE);
503 assertStringArg ('password');
504 $username = $_REQUEST['username'];
505 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
506 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
507 if ($result == TRUE)
508 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
509 else
510 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
511}
512
513function updateUserAccount ()
514{
515 global $root, $pageno, $tabno;
516 assertUIntArg ('id');
517 assertStringArg ('username');
518 assertStringArg ('realname', TRUE);
519 assertStringArg ('password');
520 // We might be asked to change username, so use user ID only.
521 $id = $_REQUEST['id'];
522 $username = $_REQUEST['username'];
523 $new_password = $_REQUEST['password'];
524 $old_hash = getHashByID ($id);
525 if ($old_hash == NULL)
526 {
527 showError ('getHashByID() failed in updateUserAccount()');
528 return;
529 }
530 // Update user password only if provided password is not the same as current password hash.
531 if ($new_password != $old_hash)
532 $new_password = hash (PASSWORD_HASH, $new_password);
533 $result = commitUpdateUserAccount ($_REQUEST['id'], $username, $_REQUEST['realname'], $new_password);
534 if ($result == TRUE)
535 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
536 else
537 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
538}
539
540function enableUserAccount ()
541{
542 global $root, $pageno, $tabno;
543 assertUIntArg ('id');
544 $id = $_REQUEST['id'];
545 $result = commitEnableUserAccount ($id, 'yes');
546 if ($result == TRUE)
547 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
548 else
549 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
550}
551
552function disableUserAccount ()
553{
554 global $root, $pageno, $tabno;
555 assertUIntArg ('id');
556 $id = $_REQUEST['id'];
557 if ($id == 1)
558 $result = FALSE;
559 else
560 $result = commitEnableUserAccount ($id, 'no');
561 if ($result == TRUE)
562 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
563 else
564 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
565}
566
567function revokePermission ()
568{
569 global $root, $pageno, $tabno;
570 assertUIntArg ('access_userid', TRUE);
571 assertStringArg ('access_page');
572 assertStringArg ('access_tab');
573 $result = commitRevokePermission
574 (
575 $_REQUEST['access_userid'],
576 $_REQUEST['access_page'],
577 $_REQUEST['access_tab']
578 );
579 if ($result == TRUE)
580 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Revoke successfull.");
581 else
582 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error revoking permission.");
583}
584
585function grantPermission ()
586{
587 global $root, $pageno, $tabno;
588 assertUIntArg ('access_userid', TRUE);
589 assertStringArg ('access_page');
590 assertStringArg ('access_tab');
591 assertStringArg ('access_value');
592 $result = commitGrantPermission
593 (
594 $_REQUEST['access_userid'],
595 $_REQUEST['access_page'],
596 $_REQUEST['access_tab'],
597 $_REQUEST['access_value']
598 );
599 if ($result == TRUE)
600 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Grant successfull.");
601 else
602 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error granting permission.");
603}
604
605// This function find differences in users's submit and PortCompat table
606// and modifies database accordingly.
607function savePortMap ()
608{
609 global $root, $pageno, $tabno;
610 $ptlist = getPortTypes();
611 $oldCompat = getPortCompat();
612 $newCompat = array();
613 foreach (array_keys ($ptlist) as $leftKey)
614 foreach (array_keys ($ptlist) as $rightKey)
615 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
616 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
617 // Build the new matrix from user's submit and compare it to
618 // the old matrix. Those pairs which appear on
619 // new matrix only, have to be stored in PortCompat table. Those who appear
620 // on the old matrix only, should be removed from PortCompat table.
621 // Those present in both matrices should be skipped.
622 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
623 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
624 $error_count = $success_count = 0;
625 foreach (array_keys ($ptlist) as $type1)
626 foreach (array_keys ($ptlist) as $type2)
627 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
628 switch ($oldCompatTable[$type1][$type2])
629 {
630 case TRUE: // new value is FALSE
631 if (removePortCompat ($type1, $type2) === TRUE)
632 $success_count++;
633 else
634 $error_count++;
635 break;
636 case FALSE: // new value is TRUE
637 if (addPortCompat ($type1, $type2) === TRUE)
638 $success_count++;
639 else
640 $error_count++;
641 break;
642 default:
643 showError ('oldCompatTable is invalid in savePortMap()');
644 break;
645 }
646 return
647 "${root}?page=${pageno}&tab=${tabno}&" .
648 ($error_count == 0 ? 'message' : 'error') .
649 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
650}
651
652function deleteDictWord ()
653{
654 global $root, $pageno, $tabno;
655 return
656 "${root}?page=${pageno}&tab=${tabno}&" .
657 "error=" . urlencode ('Dragon ate this word!');
658}
659
660function updateDictionary ()
661{
662 global $root, $pageno, $tabno;
663 assertUIntArg ('chapter_no');
664 assertUIntArg ('dict_key');
665 assertStringArg ('dict_value');
666 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
667 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
668 else
669 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
670}
671
672function supplementDictionary ()
673{
674 global $root, $pageno, $tabno;
675 assertUIntArg ('chapter_no');
676 assertStringArg ('dict_value');
677 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
678 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
679 else
680 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
681}
682
683function reduceDictionary ()
684{
685 global $root, $pageno, $tabno;
686 assertUIntArg ('chapter_no');
687 assertUIntArg ('dict_key');
688 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
689 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
690 else
691 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
692}
693
694function addChapter ()
695{
696 global $root, $pageno, $tabno;
697 assertStringArg ('chapter_name');
698 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
699 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
700 else
701 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
702}
703
704function updateChapter ()
705{
706 global $root, $pageno, $tabno;
707 assertUIntArg ('chapter_no');
708 assertStringArg ('chapter_name');
709 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
710 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
711 else
712 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
713}
714
715function delChapter ()
716{
717 global $root, $pageno, $tabno;
718 assertUIntArg ('chapter_no');
719 if (commitDeleteChapter ($_REQUEST['chapter_no']))
720 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
721 else
722 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
723}
724
725function changeAttribute ()
726{
727 global $root, $pageno, $tabno;
728 assertUIntArg ('attr_id');
729 assertStringArg ('attr_name');
730 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
731 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
732 else
733 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
734}
735
736function createAttribute ()
737{
738 global $root, $pageno, $tabno;
739 assertStringArg ('attr_name');
740 assertStringArg ('attr_type');
741 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
742 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
743 else
744 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
745}
746
747function deleteAttribute ()
748{
749 global $root, $pageno, $tabno;
750 assertUIntArg ('attr_id');
751 if (commitDeleteAttribute ($_REQUEST['attr_id']))
752 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
753 else
754 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
755}
756
757function supplementAttrMap ()
758{
759 global $root, $pageno, $tabno;
760 assertUIntArg ('attr_id');
761 assertUIntArg ('objtype_id');
762 assertUIntArg ('chapter_no');
763 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
764 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
765 else
766 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
767}
768
769function reduceAttrMap ()
770{
771 global $root, $pageno, $tabno;
772 assertUIntArg ('attr_id');
773 assertUIntArg ('objtype_id');
774 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
775 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
776 else
777 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
778}
779
780function resetAttrValue ()
781{
782 global $root, $pageno, $tabno;
783 assertUIntArg ('attr_id');
784 assertUIntArg ('object_id');
785 $object_id = $_REQUEST['object_id'];
786 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
787 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
788 else
789 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
790}
791
0dfb8a2a 792function updateAttrValues ()
e673ee24
DO
793{
794 global $root, $pageno, $tabno;
e673ee24 795 assertUIntArg ('object_id');
e673ee24 796 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
797 $oldvalues = getAttrValues ($object_id);
798
799 assertUIntArg ('num_attrs');
800 $num_attrs = $_REQUEST['num_attrs'];
801 $result = array();
802
803 for ($i = 0; $i < $num_attrs; $i++)
804 {
805 assertUIntArg ("${i}_attr_id");
806 $attr_id = $_REQUEST["${i}_attr_id"];
807
808 // Field is empty, delete attribute and move on.
809 if (empty($_REQUEST["${i}_value"]))
810 {
811 commitResetAttrValue ($object_id, $attr_id);
812 continue;
813 }
814
815 // The value could be uint/float, but we don't know ATM. Let SQL
816 // server check this and complain.
817 assertStringArg ("${i}_value");
818 $value = $_REQUEST["${i}_value"];
819 switch ($oldvalues[$attr_id]['type'])
820 {
821 case 'uint':
822 case 'float':
823 case 'string':
824 $oldvalue = $oldvalues[$attr_id]['value'];
825 break;
826 case 'dict':
827 $oldvalue = $oldvalues[$attr_id]['key'];
828 break;
829 default:
830 showError ('Internal structure error in updateAttrValues()');
831 die;
832 }
833 if ($value == $oldvalue)
834 continue;
835
836 // Note if the queries succeed or not, it determines which page they see.
837 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
838 }
839
840 if (in_array(false, $result))
e673ee24 841 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
842
843 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
844}
845
846function useupPort ()
847{
848 global $root, $pageno, $tabno;
849 assertUIntArg ('port_id');
850 assertUIntArg ('object_id');
851 $object_id = $_REQUEST['object_id'];
852 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
853 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
854 else
855 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
856}
857
15793b53
DO
858// This function processes a submit from the VLAN configuration form.
859// It doesn't check any data at the moment, relying on a smart gateway.
860// It doesn't even check if the a port already belongs to the VLAN it
861// is being requested to be put into. This behaviour implies having a
862// smart enough gateway, which unconditionally fetches the current
863// configuration and then filters out 'set' requests. The gateway must
864// validate port names and VLAN numbers as well. Ouch.
b2940de0
DO
865function updateVLANMembership ()
866{
15793b53 867 global $root, $pageno, $tabno, $remote_username;
b2940de0 868 assertUIntArg ('object_id');
15793b53
DO
869 assertUIntArg ('portcount');
870 $object_id = $_REQUEST['object_id'];
871 $portcount = $_REQUEST['portcount'];
b2940de0 872
15793b53
DO
873 $endpoints = findAllEndpoints ($object_id);
874 if (count ($endpoints) == 0)
875 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
876 urlencode ('Can\'t find any mean to reach current object. Please either set FQDN attribute or assign an IP address to the object.');
877 elseif (count ($endpoints) > 1)
878 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
879 urlencode ('More than one IP address is assigned to this object, please configure FQDN attribute.');
b2940de0 880
15793b53 881// Just convert the input and feed it into the gateway.
2da7c9b0 882 $questions = array("connect ${endpoints[0]} hwtype swtype ${remote_username}");
15793b53
DO
883 for ($i = 0; $i < $portcount; $i++)
884 {
885 if (!isset ($_REQUEST["portname_${i}"]))
886 continue;
887 if (!isset ($_REQUEST["vlanid_${i}"]))
888 continue;
889 $portname = $_REQUEST["portname_${i}"];
890 $vlanid = $_REQUEST["vlanid_${i}"];
891 $questions[] = "set ${portname} ${vlanid}";
892 }
893 $data = queryGateway
894 (
895 $tabno,
15793b53
DO
896 $questions
897 );
b2940de0 898 $error_count = $success_count = 0;
15793b53
DO
899 foreach ($data as $reply)
900 if (strncmp ($reply, 'OK!', 3))
901 $error_count++;
902 else
903 $success_count++;
b2940de0
DO
904// Generate a message depending on error counter and redirect.
905 return
15793b53 906 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&" .
b2940de0
DO
907 ($error_count == 0 ? 'message' : 'error') .
908 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
909
910}
911
4fe32e78
DY
912function updateUI ()
913{
c461c579 914 global $root, $pageno, $tabno;
4fe32e78
DY
915 $oldvalues = loadConfigCache();
916
917 assertUIntArg ('num_vars');
918 $num_vars = $_REQUEST['num_vars'];
c461c579 919 $error = '';
4fe32e78
DY
920
921 for ($i = 0; $i < $num_vars; $i++)
922 {
923 assertStringArg ("${i}_varname");
4fe32e78 924 assertStringArg ("${i}_varvalue");
4fe32e78 925 $varname = $_REQUEST["${i}_varname"];
4fe32e78 926 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 927
c461c579
DO
928 // If form value = value in DB, don't bother updating DB
929 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
930 continue;
931
932 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
933 $error = setConfigVar ($varname, $varvalue, TRUE);
934 if (!empty ($error))
935 break;
4fe32e78
DY
936 }
937
c461c579
DO
938 if ($error != '')
939 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 940
c461c579 941 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
942}
943
e673ee24 944?>