forked from Art-of-WiFi/UniFi-API-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
1079 lines (1021 loc) · 51.3 KB
/
index.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
/**
* UniFi API browser
*
* This tool is for browsing data that is exposed through Ubiquiti's UniFi Controller API,
* written in PHP, JavaScript and the Bootstrap CSS framework.
*
* Please keep the following in mind:
* - not all data collections/API endpoints are supported (yet), see the list of
* the currently supported data collections/API endpoints in the README.md file
* - this tool currently supports versions 4.x and 5.x of the UniFi Controller software
*
* VERSION: 1.0.17
*
* ------------------------------------------------------------------------------------
*
* Copyright (c) 2016, Slooffmaster
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.17');
/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
*/
session_start();
/**
* check whether user has requested to clear the PHP session
* - this function can be useful when login errors occur, mostly after upgrades or incorrect credential changes
*/
if (isset($_GET['reset_session']) && $_GET['reset_session'] == true) {
$_SESSION = [];
session_unset();
session_destroy();
session_start();
}
/**
* starting timing of the session here
*/
$time_start = microtime(true);
/**
* assign variables which are required later on together with their default values
*/
$controller_id = '';
$action = '';
$site_id = '';
$site_name = '';
$selection = '';
$output_format = 'json';
$data = '';
$objects_count = '';
$alert_message = '';
/**
* load the configuration file
* - allows override of several of the previously declared variables
* - if the config.php file is unreadable or does not exist, an alert is displayed on the page
*/
if(!is_readable('config.php')) {
$alert_message = '<div class="alert alert-danger" role="alert">The file <code>config.php</code> is not readable or does not exist.'
. '<br>If you have not yet done so, please copy/rename the <code>config.template.php</code> file to <code>config.php</code> and follow '
. 'the instructions inside to enter your credentials and controller details.</div>';
} else {
require_once('config.php');
}
/**
* load the UniFi API client class
*/
require_once('phpapi/class.unifi.php');
/**
* determine whether we have reached the cookie timeout, if so, refresh the PHP session
* else, update last activity time stamp
*/
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $cookietimeout)) {
/**
* last activity was longer than $cookietimeout seconds ago
*/
session_unset();
session_destroy();
if ($debug) {
error_log('UniFi API browser INFO: session cookie timed out');
}
}
$_SESSION['last_activity'] = time();
/**
* collect cURL version details for the info modal
*/
$curl_info = curl_version();
$curl_version = $curl_info['version'];
/**
* process the GET variables and store them in the $_SESSION array
* if a GET variable is not set, get the values from the $_SESSION array (if available)
*
* Process in this order:
* - controller_id
* Only process this after controller_id is set:
* - site_id
* Only process these after site_id is set:
* - action
* - output_format
* - theme
*/
if (isset($_GET['controller_id'])) {
/**
* user has requested a controller switch
*/
$controller = $controllers[$_GET['controller_id']];
$controller_id = $_GET['controller_id'];
$_SESSION['controller'] = $controller;
$_SESSION['controller_id'] = $_GET['controller_id'];
/**
* clear the variables from the $_SESSION array that are associated with the previous controller
*/
unset($_SESSION['site_id']);
unset($_SESSION['site_name']);
unset($_SESSION['sites']);
unset($_SESSION['action']);
unset($_SESSION['detected_controller_version']);
} else {
if (isset($_SESSION['controller']) && isset($controllers)) {
$controller = $_SESSION['controller'];
$controller_id = $_SESSION['controller_id'];
} else {
if (!isset($controllers)) {
/**
* if the user has configured a single controller, we push it's details
* to the $_SESSION and $controller arrays
*/
$_SESSION['controller'] = [
'user' => $controlleruser,
'password' => $controllerpassword,
'url' => $controllerurl,
'name' => 'Controller',
'version' => $controllerversion
];
$controller = $_SESSION['controller'];
}
}
if (isset($_GET['site_id'])) {
$site_id = $_GET['site_id'];
$_SESSION['site_id'] = $site_id;
$site_name = $_GET['site_name'];
$_SESSION['site_name'] = $site_name;
} else {
if (isset($_SESSION['site_id'])) {
$site_id = $_SESSION['site_id'];
$site_name = $_SESSION['site_name'];
}
}
}
/**
* get requested theme or use the theme stored in the $_SESSION array
*/
if (isset($_GET['theme'])) {
$theme = $_GET['theme'];
$_SESSION['theme'] = $theme;
} else {
if (isset($_SESSION['theme'])) {
$theme = $_SESSION['theme'];
}
}
/**
* get requested output_format or use the output_format stored in the $_SESSION array
*/
if (isset($_GET['output_format'])) {
$output_format = $_GET['output_format'];
$_SESSION['output_format'] = $output_format;
} else {
if (isset($_SESSION['output_format'])) {
$output_format = $_SESSION['output_format'];
}
}
/**
* get requested action or use the action stored in the $_SESSION array
*/
if (isset($_GET['action'])) {
$action = $_GET['action'];
$_SESSION['action'] = $action;
} else {
if (isset($_SESSION['action'])) {
$action = $_SESSION['action'];
}
}
/**
* display info message when no controller, site or data collection is selected
* placed here so they can be overwritten by more "severe" error messages later on
*/
if ($action === '') {
$alert_message = '<div class="alert alert-info" role="alert">Please select a data collection/API endpoint from the dropdown menus'
. ' <i class="fa fa-arrow-circle-up"></i></div>';
}
if ($site_id === '' && isset($_SESSION['controller'])) {
$alert_message = '<div class="alert alert-info" role="alert">Please select a site from the Sites dropdown menu <i class="fa fa-arrow-circle-up">'
. '</i></div>';
}
if (!isset($_SESSION['controller'])) {
$alert_message = '<div class="alert alert-info" role="alert">Please select a controller from the Controllers dropdown menu <i class="fa fa-arrow-circle-up">'
. '</i></div>';
}
/**
* Do this when a controller has been selected and was stored in the $_SESSION array
*/
if (isset($_SESSION['controller'])) {
/**
* create a new instance of the API client class and log in to the UniFi controller
* - if an error occurs during the login process, an alert is displayed on the page
*/
$unifidata = new UnifiApi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']);
$set_debug_mode = $unifidata->set_debug(trim($debug));
$loginresults = $unifidata->login();
if($loginresults === 400) {
$alert_message = '<div class="alert alert-danger" role="alert">HTTP response status: 400'
. '<br>This is probably caused by a UniFi controller login failure, please check your credentials in '
. 'config.php. After correcting your credentials, please restart your browser or use the <b>Reset PHP session</b> function in the dropdown '
. 'menu on the right, before attempting to use the API browser tool again.</div>';
}
/**
* Get the list of sites managed by the UniFi controller (if not already stored in the $_SESSION array)
*/
if (!isset($_SESSION['sites']) || empty($_SESSION['sites'])) {
$sites = $unifidata->list_sites();
$_SESSION['sites'] = $sites;
} else {
$sites = $_SESSION['sites'];
}
/**
* Get the version of the UniFi controller (if not already stored in the $_SESSION array or when 'undetected')
*/
if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === 'undetected') {
$site_info = $unifidata->stat_sysinfo();
if (isset($site_info[0]->version)) {
$detected_controller_version = $site_info[0]->version;
$_SESSION['detected_controller_version'] = $detected_controller_version;
} else {
$detected_controller_version = 'undetected';
$_SESSION['detected_controller_version'] = 'undetected';
}
} else {
$detected_controller_version = $_SESSION['detected_controller_version'];
}
}
/**
* execute timing of controller login
*/
$time_1 = microtime(true);
$time_after_login = $time_1 - $time_start;
if (isset($unifidata)) {
/**
* select the required call to the UniFi Controller API based on the selected action
*/
switch ($action) {
case 'list_clients':
$selection = 'list online clients';
$data = $unifidata->list_clients();
break;
case 'stat_allusers':
$selection = 'stat all users';
$data = $unifidata->stat_allusers();
break;
case 'stat_auths':
$selection = 'stat active authorisations';
$data = $unifidata->stat_auths();
break;
case 'list_guests':
$selection = 'list guests';
$data = $unifidata->list_guests();
break;
case 'list_usergroups':
$selection = 'list usergroups';
$data = $unifidata->list_usergroups();
break;
case 'stat_hourly_site':
$selection = 'hourly site stats';
$data = $unifidata->stat_hourly_site();
break;
case 'stat_daily_site':
$selection = 'daily site stats';
$data = $unifidata->stat_daily_site();
break;
case 'stat_hourly_aps':
$selection = 'hourly ap stats';
$data = $unifidata->stat_hourly_aps();
break;
case 'stat_daily_aps':
$selection = 'daily ap stats';
$data = $unifidata->stat_daily_aps();
break;
case 'stat_sysinfo':
$selection = 'sysinfo';
$data = $unifidata->stat_sysinfo();
break;
case 'list_devices':
$selection = 'list devices';
$data = $unifidata->list_devices();
break;
case 'list_wlan_groups':
$selection = 'list wlan groups';
$data = $unifidata->list_wlan_groups();
break;
case 'stat_sessions':
$selection = 'stat sessions';
$data = $unifidata->stat_sessions();
break;
case 'list_users':
$selection = 'list users';
$data = $unifidata->list_users();
break;
case 'list_rogueaps':
$selection = 'list rogue access points';
$data = $unifidata->list_rogueaps();
break;
case 'list_events':
$selection = 'list events';
$data = $unifidata->list_events();
break;
case 'list_alarms':
$selection = 'list alarms';
$data = $unifidata->list_alarms();
break;
case 'count_alarms':
$selection = 'count all alarms';
$data = $unifidata->count_alarms();
break;
case 'count_active_alarms':
$selection = 'count active alarms';
$data = $unifidata->count_alarms(false);
break;
case 'list_wlanconf':
$selection = 'list wlan config';
$data = $unifidata->list_wlanconf();
break;
case 'list_health':
$selection = 'site health metrics';
$data = $unifidata->list_health();
break;
case 'list_dashboard':
$selection = 'site dashboard metrics';
$data = $unifidata->list_dashboard();
break;
case 'list_settings':
$selection = 'list site settings';
$data = $unifidata->list_settings();
break;
case 'list_sites':
$selection = 'details of available sites';
$data = $sites;
break;
case 'list_extension':
$selection = 'list VoIP extensions';
$data = $unifidata->list_extension();
break;
case 'list_portconf':
$selection = 'list port configuration';
$data = $unifidata->list_portconf();
break;
case 'list_networkconf':
$selection = 'list network configuration';
$data = $unifidata->list_networkconf();
break;
case 'list_dynamicdns':
$selection = 'dynamic dns configuration';
$data = $unifidata->list_dynamicdns();
break;
case 'list_current_channels':
$selection = 'current channels';
$data = $unifidata->list_current_channels();
break;
case 'list_portforwarding':
$selection = 'list port forwarding rules';
$data = $unifidata->list_portforwarding();
break;
case 'list_portforward_stats':
$selection = 'list port forwarding stats';
$data = $unifidata->list_portforward_stats();
break;
case 'list_dpi_stats':
$selection = 'list DPI stats';
$data = $unifidata->list_dpi_stats();
break;
case 'stat_voucher':
$selection = 'list hotspot vouchers';
$data = $unifidata->stat_voucher();
break;
case 'stat_payment':
$selection = 'list hotspot payments';
$data = $unifidata->stat_payment();
break;
case 'list_hotspotop':
$selection = 'list hotspot operators';
$data = $unifidata->list_hotspotop();
break;
case 'list_self':
$selection = 'self';
$data = $unifidata->list_self();
break;
case 'stat_sites':
$selection = 'all site stats';
$data = $unifidata->stat_sites();
break;
case 'list_admins':
$selection = 'list_admins';
$data = $unifidata->list_admins();
break;
default:
break;
}
}
/**
* count the number of objects collected from the UniFi controller
*/
if ($action != '') {
$objects_count = count($data);
}
/**
* construct the url for the Bootstrap CSS file based on the selected theme (standard Bootstrap or one of the Bootswatch themes)
*/
if ($theme === 'bootstrap') {
$css_url = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css';
} else {
$css_url = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' . trim($theme) . '/bootstrap.min.css';
}
/**
* execute timing of data collection from UniFi controller
*/
$time_2 = microtime(true);
$time_after_load = $time_2 - $time_start;
/**
* calculate all the timings/percentages
*/
$time_end = microtime(true);
$time_total = $time_end - $time_start;
$login_perc = ($time_after_login/$time_total)*100;
$load_perc = (($time_after_load - $time_after_login)/$time_total)*100;
$remain_perc = 100 - $login_perc-$load_perc;
/**
* shared functions
*/
function print_output($output_format, $data)
{
/**
* function to print the output
* switch depending on the selected $output_format
*/
switch ($output_format) {
case 'json':
echo json_encode($data, JSON_PRETTY_PRINT);
break;
case 'json_color':
echo json_encode($data);
break;
case 'php_array':
print_r($data);
break;
case 'php_var_dump':
var_dump($data);
break;
case 'php_var_export':
var_export($data);
break;
default:
echo json_encode($data, JSON_PRETTY_PRINT);
break;
}
}
/**
* function to sort the sites collection alpabetically by description
*/
function sites_sort($site_a, $site_b)
{
return strcmp($site_a->desc, $site_b->desc);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>UniFi API browser</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- Latest compiled and minified versions of Bootstrap, Font-awesome and Highlight.js CSS, loaded from CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<!-- Load the appropriate Bootstrap CSS file from CDN -->
<link rel="stylesheet" href="<?php echo $css_url ?>">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.css" integrity="sha256-OhImf+9TMPW5iYXKNT4eRNntf3fCtVYe5jZqo/mrSQA=" crossorigin="anonymous">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<!-- Load the base64 encoded favicon file -->
<link rel="icon" type="image/x-icon" sizes="16x16" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAADgpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gown/4KQL/+CkDP/enQD/8NOK///////y2Zr/358A/9+fAP/fnwD/358A/9+gAP/foAH/36EB/9+gAP/fnwD/36AA/9+gAv/dmQD/79CB/////////v3//vz4/9+gAP/foAD/36ED/9+hA//engD/3ZsA/92bAP/enAD/36EC/9+hBP/dmgD/8dSM///////+/Pj///////Pbnv/foAD/36EC/96dAf/dmwD/46sc/+i6Rf/ovEr/5bAr/9+fBP/dmgD/8deS///////+/Pb///////LYlf/enAD/36ED/92bAP/jrSf/9eKw//779v/////////////////36MH/9N+q///////+/fn///////HXkv/dmwD/36AD/92bAP/mtj///vv0//////////7////////////////////////////+/fv///////DUiv/dmgD/36AD/9+hAf/iqBv/+/Ti///////+/fr/8tiX/+e3Pf/mszT/7cpx//z36P///v3///////Pbn//cmAD/36EF/9+gAf/foAD/8dWR////////////68Vi/9yWAP/enAL/3pwC/9yXAP/krij//Pbn///////36MP/358A/9+gAf/foAD/36AA//ry2///////9uW7/92aAP/gogX/36ED/9+hA//gowj/3JcA/+3Jc/////////77/+SwK//enAD/36EC/9+gAP/+/Pf//////+7Of//dmgD/4KIE/9+gAP/foAD/36EC/96cAP/lszf////////////ou0n/3ZsA/9+hA//foAD//frz///////w0or/3ZkA/+CiBf/foAD/36AA/9+hA//enAD/5rZA////////////57lE/92bAP/foQP/36AA//jry///////+u/U/9+fB//engD/4KIF/+CiBf/gogX/3JYA//HWk////////frx/+OrHv/enQD/36EC/9+gAP/syG3////////////z253/358C/92ZAP/dmgL/3ZoA/+vEYP/+/fv///////Xgrv/dmwD/36EC/9+gAP/foAD/358C//fmvP////////////rw1v/w0ob/7s16//bluv///v3///////368f/krir/3p0A/9+hAv/foAD/36AA/96eAP/hpRP/9+jA/////////v3///////////////7///////z36f/mtj7/3ZsA/9+hAv/foAD/36AA/9+gAP/foQH/3p4B/96eBf/tyWz/+e3Q//779f/+/fn/+/Pg//HXkv/iqBr/3ZsA/9+hA//foAD/36AA/9+gAP/foAD/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==">
<!-- custom CSS styling -->
<style>
body {
padding-top: 70px;
}
.scrollable-menu {
height: auto;
max-height: 600px;
overflow-x: hidden;
}
#output_panel_loading {
color: rgba(0,0,0,.4);
}
.back-to-top {
cursor: pointer;
position: fixed;
bottom: 20px;
right: 20px;
display:none;
}
</style>
<!-- /custom CSS styling -->
</head>
<body>
<!-- top navbar -->
<nav id="navbar" class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand hidden-sm hidden-md" href="index.php"><i class="fa fa-search fa-fw fa-lg" aria-hidden="true"></i> UniFi API browser</a>
</div>
<div id="navbar-main" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-left">
<!-- controllers dropdown, only show when multiple controllers have been configured -->
<?php if (isset($controllers)) { ?>
<li id="site-menu" class="dropdown">
<a id="controller-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php
/**
* here we display the UniFi controller name, if selected, else just label it
*/
if (isset($controller)) {
echo $controller['name'];
} else {
echo 'Controllers';
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu scrollable-menu" id="controllerslist">
<li class="dropdown-header">Select a controller</li>
<?php
/**
* here we loop through the configured UniFi controllers
*/
foreach ($controllers as $key => $value) {
echo '<li id="controller_' . $key . '"><a href="?controller_id=' . $key . '">' . $value['name'] . '</a></li>' . "\n";
}
?>
</ul>
</li>
<?php } ?>
<!-- /controllers dropdown -->
<!-- sites dropdown, only show when a controller has been selected -->
<?php if (isset($_SESSION['controller'])) { ?>
<li id="site-menu" class="dropdown">
<a id="site-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Sites
<span class="caret"></span>
</a>
<ul class="dropdown-menu scrollable-menu" id="siteslist">
<li class="dropdown-header">Select a site</li>
<?php
/**
* here we loop through the available sites, after we have sorted the sites collection
*/
usort($sites, "sites_sort");
foreach ($sites as $site) {
echo '<li id="' . $site->name . '"><a href="?site_id=' . $site->name . '&site_name=' . $site->desc . '">' . $site->desc . '</a></li>' . "\n";
}
?>
</ul>
</li>
<?php } ?>
<!-- /sites dropdown -->
<!-- data collection dropdowns, only show when a site_id is selected -->
<?php if ($site_id && isset($_SESSION['controller'])) { ?>
<li id="output-menu" class="dropdown">
<a id="output-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Output
<span class="caret"></span>
</a>
<ul class="dropdown-menu" id="outputselection">
<li class="dropdown-header">Select an output format</li>
<li id="json"><a href="?output_format=json">json (default)</a></li>
<li role="separator" class="divider"></li>
<li id="php_array"><a href="?output_format=php_array">PHP array</a></li>
<li id="php_var_dump"><a href="?output_format=php_var_dump">PHP var_dump</a></li>
<li id="php_var_export"><a href="?output_format=php_var_export">PHP var_export</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Nice but slow with large collections</li>
<li id="json_color"><a href="?output_format=json_color">json highlighted</a></li>
</ul>
</li>
<li id="user-menu" class="dropdown">
<a id="user-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Clients
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="list_clients"><a href="?action=list_clients">list online clients</a></li>
<li id="list_guests"><a href="?action=list_guests">list guests</a></li>
<li id="list_users"><a href="?action=list_users">list users</a></li>
<li id="list_usergroups"><a href="?action=list_usergroups">list user groups</a></li>
<li role="separator" class="divider"></li>
<li id="stat_allusers"><a href="?action=stat_allusers">stat all users</a></li>
<li id="stat_auths"><a href="?action=stat_auths">stat authorisations</a></li>
<li id="stat_sessions"><a href="?action=stat_sessions">stat sessions</a></li>
</ul>
</li>
<li id="ap-menu" class="dropdown">
<a id="ap-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Devices
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="list_devices"><a href="?action=list_devices">list devices</a></li>
<li id="list_wlan_groups"><a href="?action=list_wlan_groups">list wlan groups</a></li>
<li id="list_rogueaps"><a href="?action=list_rogueaps">list rogue access points</a></li>
</ul>
</li>
<li id="stats-menu" class="dropdown">
<a id="stats-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Stats
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="stat_hourly_site"><a href="?action=stat_hourly_site">hourly site stats</a></li>
<li id="stat_daily_site"><a href="?action=stat_daily_site">daily site stats</a></li>
<!-- all sites stats, only to be displayed when we have detected a capable controller version -->
<?php if ($detected_controller_version != 'undetected' && version_compare($detected_controller_version, '5.2.9') >= 0) { ?>
<li id="stat_sites"><a href="?action=stat_sites">all sites stats</a></li>
<?php } ?>
<!-- /all sites stats -->
<li role="separator" class="divider"></li>
<li id="stat_hourly_aps"><a href="?action=stat_hourly_aps">hourly access point stats</a></li>
<li id="stat_daily_aps"><a href="?action=stat_daily_aps">daily access point stats</a></li>
<li role="separator" class="divider"></li>
<li id="list_health"><a href="?action=list_health">site health metrics</a></li>
<!-- site dashboard metrics, only to be displayed when we have detected a capable controller version -->
<?php if ($detected_controller_version != 'undetected' && version_compare($detected_controller_version, '4.9.1') >= 0) { ?>
<li id="list_dashboard"><a href="?action=list_dashboard">site dashboard metrics</a></li>
<?php } ?>
<!-- /site dashboard metrics -->
<li id="list_portforward_stats"><a href="?action=list_portforward_stats">port forwarding stats</a></li>
<li id="list_dpi_stats"><a href="?action=list_dpi_stats">DPI stats</a></li>
</ul>
</li>
<li id="hotspot-menu" class="dropdown">
<a id="msg-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Hotspot
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="stat_voucher"><a href="?action=stat_voucher">stat vouchers</a></li>
<li id="stat_payment"><a href="?action=stat_payment">stat payments</a></li>
<li role="separator" class="divider"></li>
<li id="list_hotspotop"><a href="?action=list_hotspotop">list hotspot operators</a></li>
</ul>
</li>
<li id="config-menu" class="dropdown">
<a id="config-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Configuration
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="list_sites"><a href="?action=list_sites">list sites on this controller</a></li>
<li id="stat_sysinfo"><a href="?action=stat_sysinfo">sysinfo</a></li>
<li id="list_self"><a href="?action=list_self">self</a></li>
<li role="separator" class="divider"></li>
<li id="list_settings"><a href="?action=list_settings">list site settings</a></li>
<li id="list_admins"><a href="?action=list_admins">list admins for current site</a></li>
<li role="separator" class="divider"></li>
<li id="list_wlanconf"><a href="?action=list_wlanconf">list wlan configuration</a></li>
<li role="separator" class="divider"></li>
<li id="list_extension"><a href="?action=list_extension">list VoIP extensions</a></li>
<li role="separator" class="divider"></li>
<li id="list_networkconf"><a href="?action=list_networkconf">list network configuration</a></li>
<li id="list_portconf"><a href="?action=list_portconf">list port configuration</a></li>
<li id="list_portforwarding"><a href="?action=list_portforwarding">list port forwarding rules</a></li>
<li id="list_current_channels"><a href="?action=list_current_channels">list current channels</a></li>
<li id="list_dynamicdns"><a href="?action=list_dynamicdns">dynamic DNS configuration</a></li>
</ul>
</li>
<li id="msg-menu" class="dropdown">
<a id="msg-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Messages
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="list_alarms"><a href="?action=list_alarms">list alarms</a></li>
<li id="count_alarms"><a href="?action=count_alarms">count all alarms</a></li>
<li id="count_active_alarms"><a href="?action=count_active_alarms">count active alarms</a></li>
<li id="list_events"><a href="?action=list_events">list events</a></li>
</ul>
</li>
<?php } ?>
<!-- /data collection dropdowns -->
</ul>
<ul class="nav navbar-nav navbar-right">
<li id="theme-menu" class="dropdown">
<a id="theme-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-bars fa-lg"></i>
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a theme</li>
<li id="bootstrap"><a href="?theme=bootstrap">Bootstrap (default)</a></li>
<li id="cerulean"><a href="?theme=cerulean">Cerulean</a></li>
<li id="cosmo"><a href="?theme=cosmo">Cosmo</a></li>
<li id="cyborg"><a href="?theme=cyborg">Cyborg</a></li>
<li id="darkly"><a href="?theme=darkly">Darkly</a></li>
<li id="flatly"><a href="?theme=flatly">Flatly</a></li>
<li id="journal"><a href="?theme=journal">Journal</a></li>
<li id="lumen"><a href="?theme=lumen">Lumen</a></li>
<li id="paper"><a href="?theme=paper">Paper</a></li>
<li id="readable"><a href="?theme=readable">Readable</a></li>
<li id="sandstone"><a href="?theme=sandstone">Sandstone</a></li>
<li id="simplex"><a href="?theme=simplex">Simplex</a></li>
<li id="slate"><a href="?theme=slate">Slate</a></li>
<li id="spacelab"><a href="?theme=spacelab">Spacelab</a></li>
<li id="superhero"><a href="?theme=superhero">Superhero</a></li>
<li id="united"><a href="?theme=united">United</a></li>
<li id="yeti"><a href="?theme=yeti">Yeti</a></li>
<li role="separator" class="divider"></li>
<li id="reset_session" data-toggle="tooltip" data-placement="top" data-original-title="In some cases this will fix login errors (e.g. empty sites list)">
<a href="?reset_session=true"><i class="fa fa-refresh"></i> Reset PHP session</a>
</li>
<li role="separator" class="divider"></li>
<li id="info" data-toggle="modal" data-target="#about_modal"><a href="#"><i class="fa fa-info-circle"></i> About UniFi API browser</a></li>
</ul>
</li>
</ul>
</div><!-- /.nav-collapse -->
</div><!-- /.container-fluid -->
</nav><!-- /top navbar -->
<div class="container-fluid">
<div id="alert_placeholder"></div>
<!-- data-panel, only to be displayed once a controller has been configured and an action has been selected, while loading we display a temp div -->
<?php if (isset($_SESSION['controller']) && $action) { ?>
<div id="output_panel_loading" class="text-center">
<br>
<h2><i class="fa fa-spinner fa-spin fa-fw"></i></h2>
</div>
<div id="output_panel" class="panel panel-default" style="display: none">
<div class="panel-heading">
<!-- site info, only to be displayed when a site has been selected -->
<?php if ($site_id) { ?>
site id: <span id="span_site_id" class="label label-primary"></span>
site name: <span id="span_site_name" class="label label-primary"></span>
<?php } ?>
<!-- /site info -->
<!-- selection, only to be displayed when a selection has been made -->
<?php if ($selection) { ?>
collection: <span id="span_selection" class="label label-primary"></span>
<?php } ?>
<!-- /selection -->
output: <span id="span_output_format" class="label label-primary"></span>
<!-- objects_count, only to be displayed when we have results -->
<?php if ($objects_count !== '') { ?>
# of objects: <span id="span_objects_count" class="badge"></span>
<?php } ?>
<!-- /objects_count -->
</div>
<div class="panel-body">
<!--only display panel content when an action has been selected-->
<?php if ($action !== '' && isset($_SESSION['controller'])) { ?>
<!-- present the timing results using an HTML5 progress bar -->
<span id="span_elapsed_time"></span>
<br>
<div class="progress">
<div id="timing_login_perc" class="progress-bar progress-bar-warning" role="progressbar" aria-valuemin="0" aria-valuemax="100" data-toggle="tooltip" data-placement="bottom"></div>
<div id="timing_load_perc" class="progress-bar progress-bar-success" role="progressbar" aria-valuemin="0" aria-valuemax="100" data-toggle="tooltip" data-placement="bottom"></div>
<div id="timing_remain_perc" class="progress-bar progress-bar-primary" role="progressbar" aria-valuemin="0" aria-valuemax="100" data-toggle="tooltip" data-placement="bottom"></div>
</div>
<div id="toggle_button" style="display: none">
<button id="toggle-btn" type="button" class="btn btn-primary btn-xs"><i id="i_toggle-btn" class="fa fa-minus" aria-hidden="true"></i> Expand/collapse</button>
<button id="toggle-level2-btn" type="button" class="btn btn-primary btn-xs"><i id="i_toggle-level2-btn" class="fa fa-minus" aria-hidden="true"></i> Expand/collapse level 2</button>
<br><br>
</div>
<div id="output" style="display: none">
<pre id="pre_output"><?php print_output($output_format, $data) ?></pre>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
<!-- /data-panel -->
</div>
<!-- Back to Top button element -->
<a id="back-to-top" href="#" class="btn btn-primary back-to-top" role="button" title="Back to top" data-toggle="tooltip" data-placement="left"><i class="fa fa-chevron-up" aria-hidden="true"></i></a>
<!-- /Back to Top button element -->
<!-- Modal -->
<div class="modal fade" id="about_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><i class="fa fa-info-circle"></i> About UniFi API browser</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
A tool for browsing the data collections which are exposed through Ubiquiti's UniFi Controller API.
</div>
</div>
<div class="row">
<div class="col-sm-8 col-sm-offset-1"><a href="http://www.dereferer.org/?https://github.com/malle-pietje/UniFi-API-browser"
target="_blank">UniFi API browser on Github</a></div>
<div class="col-sm-8 col-sm-offset-1"><a href="http://www.dereferer.org/?http://community.ubnt.com/t5/UniFi-Wireless/UniFi-API-browser-tool-updates-and-discussion/m-p/1392651#U1392651"
target="_blank">UniFi API browser on Ubiquiti Community forum</a></div>
</div>
<hr>
<dl class="dl-horizontal col-sm-offset-2">
<dt>API browser version</dt>
<dd><span id="span_api_browser_version" class="label label-primary"></span> <span id="span_api_browser_update" class="label label-success"><i class="fa fa-spinner fa-spin fa-fw"></i> checking for updates</span></dd>
<dt>API client version</dt>
<dd><span id="span_api_class_version" class="label label-primary"></span></dd>
</dl>
<hr>
<dl class="dl-horizontal col-sm-offset-2">
<dt>controller user</dt>
<dd><span id="span_controller_user" class="label label-primary"></span></dd>
<dt>controller url</dt>
<dd><span id="span_controller_url" class="label label-primary"></span></dd>
<dt>version detected</dt>
<dd><span id="span_controller_version" class="label label-primary"></span></dd>
</dl>
<hr>
<dl class="dl-horizontal col-sm-offset-2">
<dt>PHP version</dt>
<dd><span id="span_php_version" class="label label-primary"></span></dd>
<dt>PHP memory_limit</dt>
<dd><span id="span_memory_limit" class="label label-primary"></span></dd>
<dt>PHP memory used</dt>
<dd><span id="span_memory_used" class="label label-primary"></span></dd>
<dt>cURL version</dt>
<dd><span id="span_curl_version" class="label label-primary"></span></dd>
<dt>operating system</dt>
<dd><span id="span_os_version" class="label label-primary"></span></dd>
</dl>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Latest compiled and minified JavaScript versions, loaded from CDN's, now including Source Integrity hashes, just in case... -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.js" integrity="sha256-yB+xHoEi5PoOnEAgHNbRMIbN4cNtOXAmBzkhNE/tQlI=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
/**
* we hide the loading div and show the output panel
*/
$("#output_panel_loading").hide();
$("#output_panel").show();
/**
* populate some Javascript variables with PHP output for cleaner code
*/
var alert_message = '<?php echo $alert_message ?>';
var action = '<?php echo $action ?>';
var site_id = '<?php echo $site_id ?>';
var site_name = '<?php echo $site_name ?>';
var controller_id = '<?php echo $controller_id ?>';
var output_format = '<?php echo $output_format ?>';
var selection = '<?php echo $selection ?>';
var objects_count = '<?php echo $objects_count ?>';
var timing_login_perc = '<?php echo $login_perc ?>';
var time_after_login = '<?php echo $time_after_login ?>';
var timing_load_perc = '<?php echo $load_perc ?>';
var time_for_load = '<?php echo ($time_after_load - $time_after_login) ?>';
var timing_remain_perc = '<?php echo $remain_perc ?>';
var timing_total_time = '<?php echo $time_total ?>';
var theme = '<?php echo $theme ?>';
var php_version = '<?php echo (phpversion()) ?>';
var memory_limit = '<?php echo (ini_get('memory_limit')) ?>';
var memory_used = '<?php echo round(memory_get_peak_usage(false)/1024/1024, 2) . 'M' ?>';
var curl_version = '<?php echo $curl_version ?>';
var os_version = '<?php echo (php_uname('s') . ' ' . php_uname('r')) ?>';
var api_browser_version = '<?php echo API_BROWSER_VERSION ?>';
var api_class_version = '<?php echo API_CLASS_VERSION ?>';
var controller_user = '<?php if (isset($_SESSION['controller'])) echo $controller['user'] ?>';
var controller_url = '<?php if (isset($_SESSION['controller'])) echo $controller['url'] ?>';
var controller_version = '<?php if (isset($_SESSION['controller'])) echo $detected_controller_version ?>';
/**
* update dynamic elements in the DOM using some of the above variables
*/
$('#alert_placeholder').html(alert_message);
$('#span_site_id').html(site_id);
$('#span_site_name').html(site_name);
$('#span_output_format').html(output_format);
$('#span_selection').html(selection);
$('#span_objects_count').html(objects_count);
$('#span_elapsed_time').html('total elapsed time: ' + timing_total_time + ' seconds');
$('#timing_login_perc').attr('aria-valuenow', timing_login_perc);
$('#timing_login_perc').css('width', timing_login_perc + '%');
$('#timing_login_perc').attr('data-original-title', time_after_login + ' seconds');
$('#timing_login_perc').html('API login time');
$('#timing_load_perc').attr('aria-valuenow', timing_load_perc);
$('#timing_load_perc').css('width', timing_load_perc + '%');
$('#timing_load_perc').attr('data-original-title', time_for_load + ' seconds');
$('#timing_load_perc').html('data load time');
$('#timing_remain_perc').attr('aria-valuenow', timing_remain_perc);
$('#timing_remain_perc').css('width', timing_remain_perc + '%');
$('#timing_remain_perc').attr('data-original-title', 'PHP overhead: ' + timing_remain_perc + '%');
$('#timing_remain_perc').html('PHP overhead');
$('#span_api_browser_version').html(api_browser_version);
$('#span_api_class_version').html(api_class_version);
$('#span_controller_user').html(controller_user);
$('#span_controller_url').html(controller_url);
$('#span_controller_version').html(controller_version);
$('#span_php_version').html(php_version);
$('#span_curl_version').html(curl_version);
$('#span_os_version').html(os_version);
$('#span_memory_limit').html(memory_limit);
$('#span_memory_used').html(memory_used);
/**
* highlight and mark the selected options in the dropdown menus for $controller_id, $action, $site_id, $theme and $output_format
*
* NOTE:
* these actions are performed conditionally
*/
(action != '') ? $('#' + action).addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;
(site_id != '') ? $('#' + site_id).addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;
(controller_id != '') ? $('#controller_' + controller_id).addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;
/**
* these two options have default values so no tests needed here
*/
$('#' + output_format).addClass('active').find('a').append(' <i class="fa fa-check"></i>');
$('#' + theme).addClass('active').find('a').append(' <i class="fa fa-check"></i>');
/**
* initialise the jquery-jsonview library, only when required
*/
if (output_format == 'json_color') {
$('#toggle_button').show();
$('#pre_output').JSONView($('#pre_output').text());
/**
* the expand/collapse toggle buttons to control the json view
*/
$('#toggle-btn').on('click', function() {
$('#pre_output').JSONView('toggle');