forked from netharbour/netharbour
/
devices.php
executable file
·1908 lines (1689 loc) · 67.5 KB
/
devices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once("sessionCheck.php");
//control bar, $_GET['mode'] is used to determine if this site is in Ajax mode, if it is don't repeat functionalities that are already executed without AJAX
if(!isset($_GET['mode']))
{include("controlBar.php");}
?>
<?
if(!isset($_GET['mode']))
{
?>
<div id="main">
<h1 id="mainTitle">DEVICES</h1>
<?
}
//inserts all the javascripts
?>
<script type="text/javascript" src="js/modal-message.js"></script>
<script type="text/javascript" src="js/ajax-dynamic-content.js"></script>
<?
include_once "classes/Device.php";
include_once 'classes/DeviceForm.php';
include_once 'classes/EdittingTools.php';
include_once 'classes/PopLocations.php';
include_once 'classes/PrivateData.php';
include_once 'classes/AAA.php';
//Make a new contact, a new tool bar, and a new form
$tool = new EdittingTools();
$deviceForm = new DeviceForm("auto", 3);
$devices = new Device();
$status;
//infoKey generates a set of key names to store the key values
$deviceKey = array("name", "device_fqdn", "location", "device_type", "snmp_ro","device_oob", "notes" );
//heading is the array of headlines in the table
$headings = array("Device Information");
//titles are the subcategories for each headline. In the array "heading" means make a room there for the headline
$titles = array("Device Name", "Management IP/FQDN", "Device Location", "Device Type",
"SNMP Community String.tip.Read only SNMP community used for SNMP data collection","Out of Band Access", "Notes");
//make a list of device types and locations in the database
$deviceTypes = Device_type::get_device_types();
$location = Location::get_locations();
//checks the status of what's happening. If something was done successfully, it will be displayed
if(isset($_GET['mode']) || isset($_GET['delete']))
{
switch (success)
{
case $_GET['update']:
$deviceForm->success("Updated successfully");
break;
case $_GET['add']:
$deviceForm->success("Added new data successfully");
break;
case $_GET['delete']:
$deviceForm->success("Deleted and archived device successfully");
break;
}
}
//if the user is editting or viewing a device ID, check if there is an update being made. If there are no updates then show them the client information form.
if(($_GET['action'] == edit && $_SESSION['access'] >= 50) || $_GET['action'] == showID)
{
//get the new device corresponding to the ID
$devices = new Device($_GET['ID']);
$id = $devices->get_device_id();
//if this is a port we're editting get that ID instead
if(isset($_POST['portID']))
{
$ports = new ControlPort($_POST['portID']);
}
//checks to see if this is a valid ID or not
if($devices->get_device_id() =="")
{
$deviceForm->error("Warning: Failed to load. Reason: ".$devices->get_error(), $_GET['ID']);
}
//if this is an update then update the device or port
if(isset($_POST['updateInfo']) && $_SESSION['access'] >= 50)
{
updateDevice($devices);
}
else if(isset($_POST['updatePort']))
{
updatePort($ports, $id);
}
//if the user is adding a port, then add it... ***NOTE THIS IS BEING ADDED HERE IN THE DISPLAYING EDITTING SECTION BECAUSE OF AJAX FUNCTIONS.....***
else if(isset($_POST['addPort']) && $_SESSION['access'] >= 50)
{
addPort($id);
}
//or else display the device information
else
{
displayDevice($devices);
}
}
else if ($_GET['action'] == "list_device_types") {
displayDeviceTypes();
}
else if ($_GET['action'] == "show_device_type") {
DisplayDeviceType();
}
else if ($_GET['action'] == "edit_device_type") {
EditDeviceType();
}
else if ($_GET['action'] == "archive_device_type") {
ArchiveDeviceType();
}
else if ($_GET['action'] == "add_device_type") {
AddDeviceType();
}
//if the user prompts to add a new device
else if ($_GET['action'] == add && $_SESSION['access'] >= 50)
{
//add the device
if(isset($_POST['addData']))
{
addDevice($devices);
}
//if it is a new device type, add the new device type
else if(isset($_POST['addType']))
{
$deviceType = new Device_type();
addDeviceType($deviceType);
}
//if it is a new location, add the location
else if(isset($_POST['addLocation']))
{
$location = new Location();
addLocation($location);
}
//or else dispay the form necessary for adding
else {
addDeviceForm($devices);
}
}
//if the user prompts to remove a device
else if($_GET['action'] == remove && $_SESSION['access'] >= 50)
{
//remove the port if it is a port
if(isset($_GET['portID']))
{
$ports = new ControlPort($_GET['portID']);
removePort($ports);
}
//or else get the device and remove it
else
{
$devices = new Device($_GET['ID']);
removeDevice($devices);
}
}
//show an IP report
else if($_GET['action'] == ipReport)
{
displayIP();
}
//display all the archived devices for the user to see
else if($_GET['action'] == showArchived)
{
displayAllArchived($devices);
}
//If the user changes teh control port from power to console and vise verca, give them different options
else if ($_GET['action'] == changeControlledPort && $_SESSION['access'] >= 50)
{
//give power controls a grouping value
echo "<td class='info'>GROUP ";
if ($_GET['mode']=='power_control')
{
echo "<input type='text' name='group' value='' />";
}
else {echo "NOT APPLICABLE";}
echo "</td>";
$types = $devices->get_devices_by_class($_GET['mode']);
echo "<select name='controlledDevice'>";
foreach ($types as $id=>$value)
{
echo "<option value='$id'>$value</option>";
}
echo "</select>";
}
//if nothing else, display all the devices for the user to see
else
{
displayAll($devices);
}
?>
</div>
<?php
//Footer
if(!isset($_GET['mode']))
{include("footer.php");} ?>
<?
/*****************************************************FUNCTIONS************************************************/
//This function displays all the device
function displayAll($devices)
{
//global the tool and make a tool bar for adding a device and display all the archived device, and displaying the IP Report
global $tool, $deviceForm;
if ($_SESSION['access'] >= 50)
{
$toolNames = array("Add New Device", "All Archived Device", "IP Report","Device Types");
$toolIcons = array("add", "device", "report","icons/checklist.png");
$toolHandlers = array(
"handleEvent('devices.php?action=add')",
"handleEvent('devices.php?action=showArchived')",
"handleEvent('devices.php?action=ipReport')",
"handleEvent('devices.php?action=list_device_types')"
);
}
else
{
$toolNames = array("All Archived Device", "IP Report");
$toolIcons = array("device", "report");
$toolHandlers = array("handleEvent('devices.php?action=showArchived')", "handleEvent('devices.php?action=ipReport')");
}
echo $tool->createNewTools($toolNames, $toolIcons, $toolHandlers);
$keyHandlers = array();
$keyData = array();
$keyTitle = array();
foreach (Device::get_devices() as $id => $name)
{
$deviceInfo = new Device($id);
array_push($keyHandlers, "handleEvent('devices.php?action=showID&ID=$id')");
array_push($keyTitle, $name);
array_push($keyData, $deviceInfo->get_type_name());
array_push($keyData, $deviceInfo->get_location_name());
}
//get all the device and display them all in the 3 sections "Device Name", "Device Type", "Location".
$headings = array("Device Name", "Device Type", "Location");
echo $deviceForm->showAll($headings, $keyTitle, $keyData, $keyHandlers);
}
//This function displays all the archived devices
function displayAllArchived($devices)
{
//global the tool and make a tool bar for adding a device, and the IP Report
global $tool, $deviceForm;
if ($_SESSION['access'] >= 50)
{
$toolNames = array("Add New Device", "All Device", "IP Report");
$toolIcons = array("add", "device", "report");
$toolHandlers = array("handleEvent('devices.php?action=add')", "handleEvent('devices.php')");
}
else
{
$toolNames = array("All Device", "IP Report");
$toolIcons = array("device", "report");
$toolHandlers = array("handleEvent('devices.php')", "handleEvent('devices.php?action=ipReport')");
}
echo $tool->createNewTools($toolNames, $toolIcons, $toolHandlers);
$keyHandlers = array();
$keyData = array();
$keyTitle = array();
foreach (Device::get_devices(1) as $id => $name)
{
$deviceInfo = new Device($id);
array_push($keyHandlers, "handleEvent('devices.php?action=showID&ID=$id')");
array_push($keyTitle, $name);
array_push($keyData, $deviceInfo->get_type_name());
array_push($keyData, $deviceInfo->get_location_name());
}
//get all the devices and display them all in the 3 sections "Device Name", "Device Type", "Location".
$headings = array("Device Name", "Device Type", "Location");
echo $deviceForm->showAll($headings, $keyTitle, $keyData, $keyHandlers);
}
//Updating the device. This is where the devices stores updated values
function updateDevice($devices)
{
//global all variables
global $deviceKey, $deviceForm, $status, $deviceTypes, $location;
//create an empty temporary array to store all the new values given from the form
$tempDeviceInfo=array();
$deviceKey = array("name", "device_fqdn", "location", "device_type", "snmp_ro","device_oob", "notes");
foreach($deviceKey as $index => $key)
{
$tempDeviceInfo[$key] = htmlspecialchars(trim($_POST[$key]),ENT_QUOTES);
}
//add slashes to these 2 to make sure it does not display wrongly
$tempDeviceInfo[notes] = addslashes($tempDeviceInfo[notes]);
$tempDeviceInfo[name] = addslashes($tempDeviceInfo[name]);
//check if this ID is valid
if (!(is_numeric($devices->get_device_id())))
{
$deviceForm->error("Warning: Failed to update. Reason: ".$devices->get_error(), $_GET['ID']);
}
//if it works then set all the values into the device and update it
else{
//makes sure the device has a name
if ($devices->set_name($tempDeviceInfo[name]))
{
//set all the values to the query
$devices->set_device_fqdn($tempDeviceInfo[device_fqdn]);
$devices->set_location_id($tempDeviceInfo[location]);
$devices->set_device_type($tempDeviceInfo[device_type]);
$devices->set_device_oob($tempDeviceInfo[snmp_ro]);
$devices->set_device_oob($tempDeviceInfo[device_oob]);
$devices->set_notes($tempDeviceInfo[notes]);
//if the update is sucessful go back to show the new updates or else show an error
if($devices->update())
{
$status="success";
$_SESSION['action'] = "Updated device: ".$tempDeviceInfo[name];
echo "<meta http-equiv=\"REFRESH\" content=\"0;url=".$_SERVER['PHP_SELF']."?action=showID&ID=$_GET[ID]&update=$status\">";
}
else{
$deviceForm->error("Warning: Failed to update. Reason: ".$devices->get_error(), $_GET['ID']);
}
}
//if there are no names then show error
else
{
$deviceForm->error("Warning: Failed to update. Reason: ".$devices->get_error(), $_GET['ID']);
}
}
}
/*Updating the Ports. This is where the Ports stores updated values*/
function updatePort($ports ,$id)
{
//global all variables
global $deviceKey, $deviceForm, $status, $deviceTypes, $location;
//create an empty temporary array to store all the new values given from the form
$tempDeviceInfo=array();
$pType = $_POST['pType'];
if(empty($pType)) {
$deviceForm->error("Warning: Failed to add port. Reason: Unable to determine port typ pType");
return false;
}
if($pType == "cport") {
$deviceKey = array("description", "physicalPort", "portName", "portType", "group", "managedDevice");
}
elseif($pType == "mport") {
$deviceKey = array("description", "physicalPort", "portName", "portType", "group", "controlledDevice");
}
else {
$deviceForm->error("Warning: Failed to add port. Reason: invalid pType");
return false;
}
foreach($deviceKey as $index => $key)
{
$tempDeviceInfo[$key] = htmlspecialchars(trim($_POST[$key]),ENT_QUOTES);
}
//add slashes to these 2 to make sure it does not display wrongly
$tempDeviceInfo[description] = addslashes($tempDeviceInfo[description]);
$tempDeviceInfo[portName] = addslashes($tempDeviceInfo[portName]);
//check if this ID is valid
if (!(is_numeric($ports->get_id())))
{
$deviceForm->error("Warning: Failed to update. Reason: ".$ports->get_error(), $_GET['ID']);
}
//if it works then set all the values into the Port and update it
else{
if (true)
{
$ports->set_name($tempDeviceInfo[portName]);
//set all the values to the query
$ports->set_port($tempDeviceInfo[physicalPort]);
$ports->set_group($tempDeviceInfo[group]);
if($pType == "cport") {
$ports->set_control_device_id($id);
$ports->set_managed_device_id($tempDeviceInfo[managedDevice]);
}
else
{
$ports->set_managed_device_id($id);
$ports->set_control_device_id($tempDeviceInfo[controlledDevice]);
}
$ports->set_type($tempDeviceInfo[portType]);
$ports->set_description($tempDeviceInfo[description]);
//if the update is sucessful go back to show the new updates or else show an error
if($ports->update())
{
$status="success";
$_SESSION['action'] = "Updated device port: ".$tempDeviceInfo[portName];
echo "<meta http-equiv=\"REFRESH\" content=\"0;url=".$_SERVER['PHP_SELF']."?action=showID&ID=$_GET[ID]&update=$status&tab=2\">";
}
else{
$deviceForm->error("Warning: Failed to update. Reason: ".$ports->get_error(), $_GET['ID']);
}
}
//if there are no names then show error
else
{
$deviceForm->error("Warning: Failed to update. Reason: ".$ports->get_error(), $_GET['ID']);
}
}
}
//display the current device information
function displayDevice($devices)
{
//global all variables
global $deviceKey, $deviceForm, $tool, $headings, $titles, $deviceTypes, $location;
//if this isn't in ajax mode display the Ajax buttons
if(!isset($_GET['mode'])){
if ($_GET['tab'] == 2)
{$name = array($devices->get_name(), "Interface", "Device Control.first.");}
else if ($_GET['tab'] == 1)
{$name = array($devices->get_name(), "Interface.first.", "Device Control");}
else
{$name = array($devices->get_name().".first.", "Interface", "Device Control");}
$page = array("devices.php?action=showID&ID=$_GET[ID]&mode=deviceInfo", "devices.php?action=showID&ID=$_GET[ID]&mode=deviceInterface", "devices.php?action=showID&ID=$_GET[ID]&mode=deviceControl");
echo $tool->createNewButtons($name, "devicePart", $page);
}
//the division for the interfae, control port, and info page to show
echo "<div id='devicePart'>";
//success message for the ajax mode
switch (success)
{
case $_GET['update']:
$deviceForm->success("Updated successfully");
break;
case $_GET['add']:
$deviceForm->success("Added new data successfully");
break;
case $_GET['delete']:
$deviceForm->success("Deleted and archived data successfully");
break;
}
//if ajax mod is part of displaying the interface
if($_GET['mode'] == deviceInterface || $_GET['tab']==1)
{
//set the table attributes
$deviceForm->setCols(11);
$deviceForm->setTableWidth("100%");
$deviceForm->setTitleWidth("10%");
//create tools for this mode
/*Taken out for user interface issues
$toolNames = array("All Devices", "All Archived Device");
$toolIcons = array("devices", "devices");
$toolHandlers = array("handleEvent('devices.php')", "handleEvent('devices.php?action=showArchived')");
echo $tool->createNewTools($toolNames, $toolIcons, $toolHandlers);*/
//can be displayed in both percent and bPS mode
if($_GET['output']==percent)
{
$headings = array("Interface name", "Interface alias", "Interface description", "Status",
"Discovered interface speed(bps)",
"Current throughput in %
<a href='#' style='color:yellow;' onclick=\"handleEvent('devices.php?action=showID&ID=$_GET[ID]&output=bps&tab=1');\">[switch to bps] </a>",
"Interface MTU", "IPv4/IPv6 address",
"Interface duplex", "Interface type", "Discovered interface index");
}
else {
$headings = array("Interface name", "Interface alias", "Interface description", "Status",
"Discovered interface speed(bps)",
"Current throughput in bps
<a href='#' style='color:yellow;' onclick=\"handleEvent('devices.php?action=showID&ID=$_GET[ID]&output=percent&tab=1');\">[switch to percent] </a>",
"Interface MTU", "IPv4/IPv6 address",
"Interface duplex", "Interface type", "Discovered interface index");
}
//get all the interfacese
$interfaces = $devices->get_interfaces();
$info = array();
$title = array();
$handlers = array();
//put all the interface information into the arrays
foreach($interfaces as $id => $value)
{
//array_push($title, "");
array_push($title, $value->get_name().'//'.$value->get_interface_id().'//'.$value->get_device_id());
array_push($info, $value->get_alias());
array_push($info, $value->get_descr());
array_push($info, $value->get_oper_status());
$speed = $tool->calculator("convertBits", $value->get_speed());
array_push($info, $speed);
//calculate the percentage if it's in percent mode, otherwise convert it in to the right bits
if ($value->get_inbits() > $value->get_outbits()){$highBits = $value->get_inbits();}
else {$highBits = $value->get_outbits();}
if($_GET['output']==percent)
{
if ($value->get_speed() > 0) {
$percentage = $highBits/$value->get_speed();
$percentage = $tool->calculator("convertPercent", $percentage);
} else {
$percentage = "0%";
}
array_push($info, $percentage);
}
else
{
$highBits = $tool->calculator("convertBits", $highBits);
array_push($info, $highBits);
}
array_push($info, $value->get_mtu());
$ipv4=$value->get_ipv4_addresses();
$ipv6=$value->get_ipv6_addresses();
$ip="";
//store both ipv4 and ipv6 addresses
foreach($ipv4 as $ipID=>$ipValue)
{
$ip.=$ipID."<br/>";
}
foreach($ipv6 as $ipID=>$ipValue)
{
$ip.=$ipID."<br/>";
}
array_push($info, $ip);
array_push($info, "");
array_push($info, $value->get_type());
array_push($info, $value->get_ifindex());
//prepare strings into html format to display the graph and push it into the handler
$nameTitle = str_replace(" ", "%20", $value->get_name());
$name = str_replace(" ", "-", $value->get_name());
$name = str_replace("/", "-", $name);
$graphLink="rrdgraph.php?file=deviceid".$value->get_device_id()."_".$name.".rrd&title=".$nameTitle."---Bits%20Per%20Second&type=traffic";
array_push($handlers, $graphLink);
}
//If there are info, display the interfaces, else give a warniing message
if (count($info)>0)
{
echo $deviceForm->showAll($headings, $title, $info, $handlers, 3);
}
else
{
$deviceForm->warning("You have no Interfaces");
}
}
//if the ajax mode is in device control
elseif ($_GET['mode'] == deviceControl || $_GET['tab'] == 2)
{
//set the form attributes
$deviceForm->setCols(7);
/*check if the device type is a power or console device
*if it is a control or console device, the user can add both management and control port
*Otherwise the user can only add management ports
*/
//if ($devices->get_device_type()==5 || $devices->get_device_type()==6)
if ($devices->get_device_class()=='console_server' || $devices->get_device_class()=='power_control')
{
$cPorts = array();
if ($_SESSION['access'] >= 50)
{
$toolNames = array("New management port.tip.Management Ports are ports that help you manage this device using power or console ports.", "New control port.tip.Control ports are ports that help you manage connected devices. Examples are power ports on remote power controls and serial ports on console servers.");
$toolIcons = array("add", "add");
$toolHandlers = array("return LoadPage('devices.php?action=add&item=mPort&mode=deviceControl&ID=$_GET[ID]', 'devicePart');",
"return LoadPage('devices.php?action=add&item=cPort&mode=deviceControl&ID=$_GET[ID]&deviceClass=".$devices->get_device_class()."', 'devicePart');");
}
}
else
{
if ($_SESSION['access'] >= 50)
{
$toolNames = array("New management port.tip.Management Ports are ports that help you manage this device using power or console ports.");
$toolIcons = array("add");
$toolHandlers = array("return LoadPage('devices.php?action=add&item=mPort&mode=deviceControl&ID=$_GET[ID]', 'devicePart');");
}
}
//create the tool
echo $tool->createNewTools($toolNames, $toolIcons, $toolHandlers);
//create the headings titles and info emptyy arrays for both management and control ports
$cHeadings = array("Description", "Physical port", "Port name", "Port type", "Group", "Managed device", "Action");
$mHeadings = array("Description", "Physical port", "Port name", "Port type", "Group", "Control device", "Action");
$cTitles = array();
$mTitles = array();
$cInfo = array();
$mInfo = array();
//get all the control and management ports to store in an array for display
$management = $devices->get_management_ports();
$control = $devices->get_control_ports();
$mPorts = array();
$index = 0;
//insert the ports into an array
foreach($management as $id => $value)
{
$mPorts[$index] = new ControlPort($id);
$index++;
}
foreach($control as $id => $value)
{
$cPorts[$index] = new ControlPort($id);
$index++;
}
//push the info of these ports into the table
foreach($mPorts as $id=>$value)
{
array_push($mTitles, $mPorts[$id]->get_description());
}
if (isset($cPorts)) {
foreach($cPorts as $id=>$value)
{
array_push($cTitles, $cPorts[$id]->get_description());
}
}
foreach($mPorts as $id=>$value)
{
array_push($mInfo, $mPorts[$id]->get_port());
array_push($mInfo, $mPorts[$id]->get_name());
array_push($mInfo, $mPorts[$id]->get_type());
array_push($mInfo, $mPorts[$id]->get_group());
array_push($mInfo, $mPorts[$id]->get_control_device_name());
$portID=$mPorts[$id]->get_id();
if ($_SESSION['access'] >= 50)
{array_push($mInfo, "<a href='#' onclick=\"return LoadPage('devices.php?action=edit&ID=$_GET[ID]&mode=deviceControl&mportID=$id&portID=$portID', 'devicePart');\">Edit</a> | <a href='#' onclick=\"handleEvent('devices.php?action=remove&ID=$_GET[ID]&mportID=$id&portID=$portID');\">Delete</a>");}
else
{array_push($mInfo, 'No Access');}
}
if (isset($cPorts)) {
foreach($cPorts as $id=>$value)
{
array_push($cInfo, $cPorts[$id]->get_port());
array_push($cInfo, $cPorts[$id]->get_name());
array_push($cInfo, $cPorts[$id]->get_type());
array_push($cInfo, $cPorts[$id]->get_group());
array_push($cInfo, $cPorts[$id]->get_managed_device_name());
$portID=$cPorts[$id]->get_id();
if ($_SESSION['access'] >= 50)
{array_push($cInfo, "<a href='#' onclick=\"return LoadPage('devices.php?action=edit&ID=$_GET[ID]&mode=deviceControl&cportID=$id&portID=$portID', 'devicePart');\">Edit</a> | <a href='#' onclick=\"handleEvent('devices.php?action=remove&ID=$_GET[ID]&cportID=$id&portID=$portID');\">Delete</a>");}
else
{array_push($cInfo, 'No Access');}
}
}
//if the user is editting this information, make it all editable
if ($_GET['action'] == edit)
{
$deviceForm->setCols(2);
$fieldType = array("hidden","", "", "", "static", "", "drop_down");
//checks to see if it's management or control ports to give different forms
if(isset($_GET['mportID']))
{
$name=$devices->get_name();
$headings = array("Port Information for ".$name);
$titles = array("pType","Description.tip.A descriptive name for this connection, i.e. \"console connection for router1, routing engine 2\" or \"Remote power cycle group for router1\"",
"Physical port.tip.Which port is this device physically connected to",
"Port name.tip.Name of port. This will also be the name used for scripts",
"Port type", "Group", "Control device");
$group = $mPorts[$_GET['mportID']]->get_group();
if($group == ''){
$fieldType = array("hidden","", "", "", "static", "static", "drop_down");
$group = "NOT APPLICABLE";
}
$info = array("mport",$mPorts[$_GET['mportID']]->get_description(), $mPorts[$_GET['mportID']]->get_port(),
$mPorts[$_GET['mportID']]->get_name(), $mPorts[$_GET['mportID']]->get_type(),
$group, $mPorts[$_GET['mportID']]->get_control_device_name());
if ($mPorts[$_GET['mportID']]->get_type() == "console")
{$portTypeName="console_server";}
else
{$portTypeName="power_control";}
$types = $devices->get_devices_by_class($portTypeName);
$deviceKey = array("pType","description", "physicalPort", "portName", "portType", "group", "controlledDevice");
}
else if (isset($_GET['cportID']))
{
$name=$devices->get_name();
$headings = array("Port Information for ".$name);
$titles = array("pType","Description.tip.A descriptive name for this connection, i.e. \"console connection for router1, routing engine 2\" or \"Remote power cycle group for router1\"",
"Physical port.tip.Which port is this device physically connected to",
"Port name.tip.Name of port. This will also be the name used for scripts",
"Port type", "Group",
"Managed device.tip.Select a device or select \"Other Device\" if you want to manage a device that is not in the database. If you select \"Other Device\" please make sure to have a good port description.");
$group = $cPorts[$_GET['cportID']]->get_group();
if($group == ''){
$fieldType = array("hidden","", "", "", "static", "static", "drop_down");
$group = "NOT APPLICABLE";
}
$info = array("cport",$cPorts[$_GET['cportID']]->get_description(), $cPorts[$_GET['cportID']]->get_port(),
$cPorts[$_GET['cportID']]->get_name(), $cPorts[$_GET['cportID']]->get_type(),
$group, $cPorts[$_GET['cportID']]->get_managed_device_name());
$types = $devices->get_devices();
array_push($types, "Other devices");
$deviceKey = array("pType","description", "physicalPort", "portName", "portType", "group", "managedDevice");
}
$deviceForm->setFieldType($fieldType);
echo $deviceForm->editPortForm($headings, $titles, $info, $deviceKey, $types);
}
//if the user is showing the information make it all uneditable
else if($_GET['action'] == showID)
{
//show the ports
if(isset($cPorts)) {
echo "<div style='clear:both;'></div><h2>Control Ports</h2>";
echo $deviceForm->showAll($cHeadings, $cTitles, $cInfo);
}
if(isset($mPorts)) {
echo "<div style='clear:both;'></div><h2>Management Ports</h2>";
echo $deviceForm->showAll($mHeadings, $mTitles, $mInfo);
}
if(!isset($mPorts) && !isset($cPorts))
{$deviceForm->warning("You have no Ports");}
}
}
//display all the device info
else {
$deviceForm->setCols(2);
//make the tool bar for this page
if ($_SESSION['access'] >= 50)
{
$toolNames = array("Edit Device", "Delete Device");
$toolIcons = array("edit", "delete");
$toolHandlers = array("handleEvent('devices.php?action=edit&ID=$_GET[ID]');",
"handleEvent('devices.php?action=remove&ID=$_GET[ID]')",
);
}
echo $tool->createNewTools($toolNames, $toolIcons, $toolHandlers);
//make the headings
$headings = array("Device Information");
//store all the device information values into an array
$info = array($devices->get_name(), $devices->get_device_fqdn(), $devices->get_location_name(),
$devices->get_type_name(), $devices->get_snmp_ro(), $devices->get_device_oob(), $devices->get_notes()
);
//if the user is editting this information, make it all editable
if ($_GET['action'] == edit)
{
$deviceKey = array("name", "device_fqdn", "location", "device_type",
"SNMP Community String.tip.Read only SNMP community used for SNMP data collection", "device_oob", "notes");
$fieldType = array("","","drop_down", "drop_down", "","", "text_area");
$deviceForm->setFieldType($fieldType);
$type = array($location, $deviceTypes);
echo $deviceForm->editDeviceForm($headings, $titles, $info, $deviceKey, $type);
}
//if the user is showing the informating make it all uneditable
elseif(($_GET['action'] == showID) )
{
//store all the device information values into an array
$info = array($devices->get_name(), $devices->get_device_fqdn(), $devices->get_location_name(),
$devices->get_type_name(), $devices->get_snmp_ro(), $devices->get_device_oob(), nl2br($devices->get_notes())
);
echo $deviceForm->showDeviceForm($headings, $titles, $info);
// Everything below is for viewing & edditing Private Data for this device.
$modalForms ="";
echo "<div style='clear:both;'></div>";
echo "<h2>Private Data</h2>";
// Here we check if we just deleted a private data entry
if (isset($_POST['delete_private_data'])) {
$form = new Form();
// Yes update
$privDataObj = new PrivateData($_POST['private_data_id']);
if ($privDataObj->delete($_POST['group_pass'])) {
$form->success("Private entry Deleted") ;
$_SESSION['action'] = "Removed private data for: ".$devices->get_name();
} else {
$form->error("Warning: Failed to delete Private data Reason: ".$privDataObj->get_error(), $_GET['ID']) ;
unset($_POST['group_pass']);
}
}
// Check if we just added a private data Type
if (isset($_POST['add_private_data_type'])) {
$form = new Form();
$no_error = true;
// Check mandotry fields
if (($_POST['pdtype_name'] == '') ) {
$form->error("Error: Private DataType name is empty") ;
$no_error = false;
}
elseif (($_POST['pdtype_desc'] == '') ) {
$form->error("Error: Private DataType Description is empty") ;
$no_error = false;
}
if ($no_error) {
$privDataTypeObj = new PrivateDataType();
$privDataTypeObj->set_name($_POST['pdtype_name']);
$privDataTypeObj->set_desc($_POST['pdtype_desc']);
if ($privDataTypeObj->insert()) {
$form->success("Private data type '". $_POST['pdtype_name'] ."' Added") ;
$_SESSION['action'] = "Added private data Type";
} else {
$form->error("Warning: Failed to Add Private data Reason: ".$privDataTypeObj->get_error());
}
}
}
// Check if we just added a private data entry
if (isset($_POST['add_private_data_for_group'])) {
$form = new Form();
$no_error = true;
// Check mandotry fields
if ((!is_numeric($_POST['device_id'])) ) {
$form->error("Error: Invalid device id") ;
$no_error = false;
}
elseif ((!is_numeric($_POST['group_id'])) ) {
$form->error("Error: No Group Specified") ;
$no_error = false;
}
elseif ((!is_numeric($_POST['private_data_type'])) ) {
$form->error("Error: No Private Data type specified") ;
$no_error = false;
}
elseif (($_POST['private_data_password'] == '') ) {
$form->error("Warning: Private Data string was empty") ;
//$no_error = false;
}
if ($no_error) {
$privDataObj = new PrivateData();
$privDataObj->set_group_id($_POST['group_id']);
$privDataObj->set_type_id($_POST['private_data_type']);
$privDataObj->set_device_id($_POST['device_id']);
$privDataObj->set_notes($_POST['private_data_notes']);
$privDataObj->set_name($_POST['private_data_desc']);
$privDataObj->set_private_data($_POST['private_data_password']);
if ($privDataObj->insert($_POST['group_pass'])) {
$form->success("Private data entry Added") ;
$_SESSION['action'] = "Added private data for: ".$devices->get_name();
} else {
$form->error("Warning: Failed to Add Private data Reason: ".$privDataObj->get_error(), $_GET['ID']) ;
unset($_POST['group_pass']);
}
}
}
echo "<a name='modal' href='#Add_privatedata_modal'><img src='icons/Add.png' height=18>Add Private Data</a><br>";
// Add Modal for adding Private data types
$modalForm = new Form("auto", 2);
$modalForm->setHeadings(array("<br><br>Add Private Data Type"));
$modalForm->setTitles(array("Name.tip.Descriptive String for this type","Description"));
$modalForm->setData(array("",""));
$modalForm->setDatabase(array("pdtype_name","pdtype_desc"));
// Change button text
$modalForm->setUpdateValue("add_private_data_type");
$modalForm->setUpdateText("Add Private Data Type");
$modalForm->setModalID("add_pdtype_modal");
$private_data_type_modal = $modalForm->modalForm();
unset($modalForm);
// End Modal for adding Private data types
// Create modal for adding a new Private data entry
// This modal should ask for which group to add it as and the password
// We need to know all groups this user is in:
$user = new User($_SESSION['userid']);
$user_groups = $user->get_groups();
if ((sizeof($user_groups)) == 1) {
foreach ($user_groups as $gid => $gname) {
$group_data = $gname;
}
} else {
$group_data = "";
}
$modalForm = new Form("auto", 2);
$modalForm->setHeadings(array("For which group would you like to add private" ));
$modalForm->setTitles(array("Group","Group Password.tip.This is the shared secret for the group you selected above.",
"Fill in Private Data Details below:","Description",
"Private Data<br><small>Stored encrypted</small>.tip.This is the data that will be encrypted",
"Type <br><small><a name='modal' href='#add_pdtype_modal'>Add Private data type</a></small>",
"Notes <br><small><i>Stored encrypted</i></small>.tip.This data will be AES encrypted","device_id"));
$modalForm->setData(array("$group_data","","","","","","",$_GET['ID']));
$modalForm->setDatabase(array("group_id","group_pass","dummy","private_data_desc","private_data_password",
"private_data_type","private_data_notes","device_id"));
$modalForm->setFieldType(array(0=>'drop_down',1=>'password_autocomplete_off',
2=>'static',5=>'drop_down',6=>'text_area',7=>'hidden'));
// Drop down
// We need to know all groups this user is in:
$modalForm->setType($user_groups);
$dataTypes = PrivateDataType::get_private_data_types();
$modalForm->setType($dataTypes);
//End Dropdown
// Change button text
$modalForm->setUpdateValue("add_private_data_for_group");
$modalForm->setUpdateText("Add");
$modalForm->setModalID("Add_privatedata_modal");
echo $modalForm->modalForm();
unset($modalForm);
// End modal
// Also create a table with PrivateData
// 1st get all entries for this device
$all_private_data = PrivateData::get_private_data_by_device($_GET['ID']);
// Only if there is any private data for this device
if (($all_private_data) &&($_GET['action'] == showID)) {
$i++;
// Check if we just updated the info,
// If so we need to update Private data
if (isset($_POST['update_private_data'])) {
// Yes update
$tmpform = new Form();
$privDataObj = new PrivateData($_POST['private_data_id']);
$privDataObj->set_name($_POST['private_data_desc']);
$privDataObj->set_notes($_POST['private_data_notes']);
$privDataObj->set_type_id($_POST['private_data_type']);
$privDataObj->set_private_data($_POST['private_data_password']);
if ($privDataObj->update($_POST['group_pass'])) {
$tmpform->success("Private data updated Succesfully") ;
unset($tmpform);
$_SESSION['action'] = "Updated private data for: ".$devices->get_name();
} else {
print "NOK " . $privDataObj->get_error();
$tmpform->error("Warning: Failed to Update Private data Reason: ".$privDataObj->get_error(), $_GET['ID']) ;
}
}
// Placeholder for modal forms
$heading = array("Type","Description","Private Data","Group","Actions");
$data = array();
foreach($all_private_data as $id =>$group_id) {
$privDataObj = new PrivateData($id);
// Only show tooltip when data is available
// This is for type description
if ($privDataObj->get_type_desc() != '') {
$type_tooltip = ".tip.Private Data Type keyword:<br> ".$privDataObj->get_type_name();
} else {
$type_tooltip = "";
}
// This is for type name + Notes
//if ($privDataObj->get_notes() != '') {
// $name_tooltip = ".tip.<b>Notes:</b><br>".nl2br($privDataObj->get_notes());
//} else {
// $name_tooltip = "";
//}