Repository navigation
Expand file tree
/
Copy pathunifi-api-client.php
More file actions
executable file
·2266 lines (2094 loc) · 90.2 KB
/
Copy pathunifi-api-client.php
File metadata and controls
executable file
·2266 lines (2094 loc) · 90.2 KB
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
/**
* This file is part of the art-of-wifi/unifi-api-client package
*
* This UniFi API client is based on the work done by the following developers:
* domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
* fbagnol: https://github.com/fbagnol/class.unifi.php
* and the API as published by Ubiquiti:
* https://www.ubnt.com/downloads/unifi/<UniFi controller version number>/unifi_sh_api
*
* Copyright (c) 2017, Art of WiFi <info@artofwifi.net>
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.md
*/
namespace UniFi_API;
/**
* the UniFi API client class
*/
class Client
{
/**
* private properties
*/
protected $baseurl = 'https://127.0.0.1:8443';
protected $site = 'default';
protected $version = '5.4.16';
protected $debug = false;
protected $is_loggedin = false;
private $cookies = '';
private $request_type = 'POST';
private $connect_timeout = 10;
private $last_results_raw = null;
private $last_error_message = null;
private $curl_ssl_verify_peer = false;
private $curl_ssl_verify_host = false;
/**
* Construct an instance of the UniFi API client class
* ---------------------------------------------------
* return a new class instance
* required parameter <user> = string; user name to use when connecting to the UniFi controller
* required parameter <password> = string; password to use when connecting to the UniFi controller
* optional parameter <baseurl> = string; base URL of the UniFi controller, must include "https://" prefix and port suffix (:8443)
* optional parameter <site> = string; short site name to access, defaults to "default"
* optional parameter <version> = string; the version number of the controller, defaults to "5.4.16"
* optional parameter <ssl_verify> = boolean; whether to validate the controller's SSL certificate or not, a value of true is
* recommended for production environments to prevent potential MitM attacks, default (false) is to
* not validate the controller certificate
*/
function __construct($user, $password, $baseurl = '', $site = '', $version = '', $ssl_verify = false)
{
if (!extension_loaded('curl')) {
trigger_error('The PHP curl extension is not loaded. Please correct this before proceeding!');
}
$this->user = trim($user);
$this->password = trim($password);
if (!empty($baseurl)) $this->baseurl = trim($baseurl);
if (!empty($site)) $this->site = trim($site);
if (!empty($version)) $this->version = trim($version);
if ($ssl_verify === true) {
$this->curl_ssl_verify_peer = true;
$this->curl_ssl_verify_host = 2;
}
$this->check_base_url();
$this->check_site($this->site);
$this->update_unificookie();
}
function __destruct()
{
/**
* if user has $_SESSION['unificookie'] set, do not logout here
*/
if (isset($_SESSION['unificookie'])) return;
/**
* logout, if needed
*/
if ($this->is_loggedin) $this->logout();
}
/**
* Login to UniFi Controller
* -------------------------
* returns true upon success
*/
public function login()
{
/**
* if user has $_SESSION['unificookie'] set, skip the login
*/
if (isset($_SESSION['unificookie'])) return $this->is_loggedin = true;
$ch = $this->get_curl_obj();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, $this->baseurl.'/login');
curl_setopt($ch, CURLOPT_URL, $this->baseurl.'/api/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
/**
* execute the cURL request
*/
$content = curl_exec($ch);
if (curl_errno($ch)) trigger_error('cURL error: '.curl_error($ch));
if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
print '<pre>';
print PHP_EOL.'-----------LOGIN-------------'.PHP_EOL;
print_r (curl_getinfo($ch));
print PHP_EOL.'----------RESPONSE-----------'.PHP_EOL;
print $content;
print PHP_EOL.'-----------------------------'.PHP_EOL;
print '</pre>';
}
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = trim(substr($content, $header_size));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
preg_match_all('|Set-Cookie: (.*);|U', substr($content, 0, $header_size), $results);
if (isset($results[1])) {
$this->cookies = implode(';', $results[1]);
if (!empty($body)) {
if (($code >= 200) && ($code < 400)) {
if (strpos($this->cookies, 'unifises') !== false) return $this->is_loggedin = true;
}
if ($code === 400) {
trigger_error('We have received an HTTP response status: 400. Probably a controller login failure');
return $code;
}
}
}
return false;
}
/**
* Logout from UniFi Controller
* ----------------------------
* returns true upon success
*/
public function logout()
{
if (!$this->is_loggedin) return false;
$this->exec_curl('/logout');
$this->is_loggedin = false;
$this->cookies = '';
return true;
}
/****************************************************************
* setter/getter functions from here:
****************************************************************/
/**
* Set site
* --------
* modify the private property site, returns the new (short) site name
* required parameter <site> = string; must be the short site name of a site to which the
* provided credentials have access
*
* NOTE:
* this method can be useful when switching between sites
*/
public function set_site($site)
{
$this->check_site($site);
$this->site = trim($site);
return $this->site;
}
/**
* Get site
* --------
* get the value of private property site, returns the current (short) site name
*/
public function get_site()
{
return $this->site;
}
/**
* Set debug mode
* --------------
* sets debug mode to true or false, returns false if a non-boolean parameter was passed
* required parameter <enable> = boolean; true will enable debug mode, false will disable it
*/
public function set_debug($enable)
{
if ($enable === true || $enable === false) {
$this->debug = $enable;
return true;
}
trigger_error('Error: the parameter for set_debug() must be boolean');
return false;
}
/**
* Get debug mode
* --------------
* get the value of private property debug, returns the current boolean value for debug
*/
public function get_debug()
{
return $this->debug;
}
/**
* Get last raw results
* --------------------
* returns the raw results of the last method called, returns false if unavailable
* optional parameter <return_json> = boolean; true will return the results in "pretty printed" json format,
* PHP stdClass Object format is returned by default
*/
public function get_last_results_raw($return_json = false)
{
if ($this->last_results_raw !== null) {
if ($return_json) return json_encode($this->last_results_raw, JSON_PRETTY_PRINT);
return $this->last_results_raw;
}
return false;
}
/**
* Get last error message
* ----------------------
* returns the error message of the last method called in PHP stdClass Object format, returns false if unavailable
*/
public function get_last_error_message()
{
if ($this->last_error_message !== null) return $this->last_error_message;
return false;
}
/**
* Get Cookie from UniFi Controller
* --------------------------------
* returns the UniFi controller cookie
*
* NOTES:
* - when the results from this method are stored in $_SESSION['unificookie'], the class will initially not
* log in to the controller when a subsequent request is made using a new instance. This speeds up the
* overall request considerably. If that subsequent request fails (e.g. cookies have expired), a new login
* is executed automatically and the value of $_SESSION['unificookie'] is updated.
*/
public function get_cookie()
{
if (!$this->is_loggedin) return false;
return $this->cookies;
}
/****************************************************************
* Functions to access UniFi controller API routes from here:
****************************************************************/
/**
* Authorize a client device
* -------------------------
* return true on success
* required parameter <mac> = client MAC address
* required parameter <minutes> = minutes (from now) until authorization expires
* optional parameter <up> = upload speed limit in kbps
* optional parameter <down> = download speed limit in kbps
* optional parameter <MBytes> = data transfer limit in MB
* optional parameter <ap_mac> = AP MAC address to which client is connected, should result in faster authorization
*/
public function authorize_guest($mac, $minutes, $up = null, $down = null, $MBytes = null, $ap_mac = null)
{
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$json = ['cmd' => 'authorize-guest', 'mac' => $mac, 'minutes' => intval($minutes)];
/**
* if we have received values for up/down/MBytes/ap_mac we append them to the payload array to be submitted
*/
if (isset($up)) $json['up'] = intval($up);
if (isset($down)) $json['down'] = intval($down);
if (isset($MBytes)) $json['bytes'] = intval($MBytes);
if (isset($ap_mac)) $json['ap_mac'] = $ap_mac;
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Unauthorize a client device
* ---------------------------
* return true on success
* required parameter <mac> = client MAC address
*/
public function unauthorize_guest($mac)
{
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$json = json_encode(['cmd' => 'unauthorize-guest', 'mac' => $mac]);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Reconnect a client device
* -------------------------
* return true on success
* required parameter <mac> = client MAC address
*/
public function reconnect_sta($mac)
{
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$json = json_encode(['cmd' => 'kick-sta', 'mac' => $mac]);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Block a client device
* ---------------------
* return true on success
* required parameter <mac> = client MAC address
*/
public function block_sta($mac)
{
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$json = json_encode(['cmd' => 'block-sta', 'mac' => $mac]);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Unblock a client device
* -----------------------
* return true on success
* required parameter <mac> = client MAC address
*/
public function unblock_sta($mac)
{
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$json = json_encode(['cmd' => 'unblock-sta', 'mac' => $mac]);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Add/modify/remove a client device note
* --------------------------------------
* return true on success
* required parameter <user_id> = id of the user device to be modified
* optional parameter <note> = note to be applied to the user device
*
* NOTES:
* - when note is empty or not set, the existing note for the user will be removed and "noted" attribute set to false
*/
public function set_sta_note($user_id, $note = null)
{
if (!$this->is_loggedin) return false;
$noted = (is_null($note)) || (empty($note)) ? false : true;
$json = json_encode(['note' => $note, 'noted' => $noted]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Add/modify/remove a client device name
* --------------------------------------
* return true on success
* required parameter <user_id> = id of the client device to be modified
* optional parameter <name> = name to be applied to the client device
*
* NOTES:
* - when name is empty or not set, the existing name for the client device will be removed
*/
public function set_sta_name($user_id, $name = null)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['name' => $name]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* 5 minutes site stats method
* ---------------------------
* returns an array of 5 minutes stats objects for the current site
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 12 hours
* - this function/method is only supported on controller versions 5.5.* and later
* - make sure that the retention policy for 5 minutes stats is set to the correct value in
* the controller settings
*/
public function stat_5minutes_site($start = null, $end = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(12*3600*1000) : intval($start);
$attributes = ['bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'];
$json = json_encode(['attrs' => $attributes, 'start' => $start, 'end' => $end]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/5minutes.site', 'json='.$json);
return $this->process_response($response);
}
/**
* Hourly site stats method
* ------------------------
* returns an array of hourly stats objects for the current site
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
* - "bytes" are no longer returned with controller version 4.9.1 and later
*/
public function stat_hourly_site($start = null, $end = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(7*24*3600*1000) : intval($start);
$attributes = ['bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'];
$json = json_encode(['attrs' => $attributes, 'start' => $start, 'end' => $end]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/hourly.site', 'json='.$json);
return $this->process_response($response);
}
/**
* Daily site stats method
* ------------------------
* returns an array of daily stats objects for the current site
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 52*7*24 hours
* - bytes" are no longer returned with controller version 4.9.1 and later
*/
public function stat_daily_site($start = null, $end = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time()-(time() % 3600))*1000) : intval($end);
$start = is_null($start) ? $end-(52*7*24*3600*1000) : intval($start);
$attributes = ['bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time'];
$json = json_encode(['attrs' => $attributes, 'start' => $start, 'end' => $end]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/daily.site', 'json='.$json);
return $this->process_response($response);
}
/**
* 5 minutes stats method for a single access point or all access points
* ---------------------------------------------------------------------
* returns an array of 5 minutes stats objects
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = AP MAC address to return stats for
*
* NOTES:
* - defaults to the past 12 hours
* - this function/method is only supported on controller versions 5.5.* and later
* - make sure that the retention policy for 5 minutes stats is set to the correct value in
* the controller settings
*/
public function stat_5minutes_aps($start = null, $end = null, $mac = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(12*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/5minutes.ap', 'json='.$json);
return $this->process_response($response);
}
/**
* Hourly stats method for a single access point or all access points
* ------------------------------------------------------------------
* returns an array of hourly stats objects
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = AP MAC address to return stats for
*
* NOTES:
* - defaults to the past 7*24 hours
* - UniFi controller does not keep these stats longer than 5 hours with versions < 4.6.6
*/
public function stat_hourly_aps($start = null, $end = null, $mac = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(7*24*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/hourly.ap', 'json='.$json);
return $this->process_response($response);
}
/**
* Daily stats method for a single access point or all access points
* -----------------------------------------------------------------
* returns an array of daily stats objects
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = AP MAC address to return stats for
*
* NOTES:
* - defaults to the past 7*24 hours
* - UniFi controller does not keep these stats longer than 5 hours with versions < 4.6.6
*/
public function stat_daily_aps($start = null, $end = null, $mac = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(7*24*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/daily.ap', 'json='.$json);
return $this->process_response($response);
}
/**
* Show all login sessions
* -----------------------
* returns an array of login session objects for all devices or a single device
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = client MAC address to return sessions for (can only be used when start and end are also provided)
* optional parameter <type> = client type to return sessions for, can be 'all', 'guest' or 'user'; default value is 'all'
*
* NOTES:
* - defaults to the past 7*24 hours
*/
public function stat_sessions($start = null, $end = null, $mac = null, $type = 'all')
{
if (!$this->is_loggedin) return false;
if (!in_array($type, ['all', 'guest', 'user'])) return false;
$end = is_null($end) ? time() : intval($end);
$start = is_null($start) ? $end-(7*24*3600) : intval($start);
$json = ['type'=> $type, 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/session', 'json='.$json);
return $this->process_response($response);
}
/**
* Show latest 'n' login sessions for a single client device
* ---------------------------------------------------------
* returns an array of latest login session objects for given client device
* required parameter <mac> = client MAC address
* optional parameter <limit> = maximum number of sessions to get (default value is 5)
*/
public function stat_sta_sessions_latest($mac, $limit = null)
{
if (!$this->is_loggedin) return false;
$limit = is_null($limit) ? 5 : intval($limit);
$json = json_encode(['mac' => $mac, '_limit' => $limit, '_sort'=> '-assoc_time']);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/session', 'json='.$json);
return $this->process_response($response);
}
/**
* Show all authorizations
* -----------------------
* returns an array of authorization objects
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
*
* NOTES:
* - defaults to the past 7*24 hours
*/
public function stat_auths($start = null, $end = null)
{
if (!$this->is_loggedin) return false;
$end = is_null($end) ? time() : intval($end);
$start = is_null($start) ? $end-(7*24*3600) : intval($start);
$json = json_encode(['start' => $start, 'end' => $end]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/authorization', 'json='.$json);
return $this->process_response($response);
}
/**
* List all client devices ever connected to the site
* --------------------------------------------------
* returns an array of client device objects
* optional parameter <historyhours> = hours to go back (default is 8760 hours or 1 year)
*
* NOTES:
* - <historyhours> is only used to select clients that were online within that period,
* the returned stats per client are all-time totals, irrespective of the value of <historyhours>
*/
public function stat_allusers($historyhours = 8760)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['type' => 'all', 'conn' => 'all', 'within' => intval($historyhours)]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/alluser', 'json='.$json);
return $this->process_response($response);
}
/**
* List guest devices
* ------------------
* returns an array of guest device objects with valid access
* optional parameter <within> = time frame in hours to go back to list guests with valid access (default = 24*365 hours)
*/
public function list_guests($within = 8760)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['within' => intval($within)]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/guest', 'json='.$json);
return $this->process_response($response);
}
/**
* List online client device(s)
* ----------------------------
* returns an array of online client device objects, or in case of a single device request, returns a single client device object
* optional parameter <client_mac> = the MAC address of a single online client device for which the call must be made
*/
public function list_clients($client_mac = null)
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/sta/'.trim($client_mac));
return $this->process_response($response);
}
/**
* Get details for a single client device
* --------------------------------------
* returns an object with the client device information
* required parameter <client_mac> = client device MAC address
*/
public function stat_client($client_mac)
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/user/'.trim($client_mac));
return $this->process_response($response);
}
/**
* List user groups
* ----------------
* returns an array of user group objects
*/
public function list_usergroups()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/list/usergroup');
return $this->process_response($response);
}
/**
* Assign client device to another group
* -------------------------------------
* return true on success
* required parameter <user_id> = id of the user device to be modified
* required parameter <group_id> = id of the user group to assign user to
*/
public function set_usergroup($user_id, $group_id)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['usergroup_id' => $group_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Update user group (using REST)
* ------------------------------
* returns an array containing a single object with attributes of the updated usergroup on success
* required parameter <group_id> = id of the user group
* required parameter <site_id> = id of the site
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*
*/
public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1, $group_up = -1)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode(['_id' => $group_id, 'name' => $group_name, 'qos_rate_max_down' => intval($group_dn), 'qos_rate_max_up' => intval($group_up), 'site_id' => $site_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup/'.trim($group_id), $json);
return $this->process_response($response);
}
/**
* Create user group (using REST)
* ---------------------------
* returns an array containing a single object with attributes of the new usergroup ("_id", "name", "qos_rate_max_down", "qos_rate_max_up", "site_id") on success
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*/
public function create_usergroup($group_name, $group_dn = -1, $group_up = -1)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['name' => $group_name, 'qos_rate_max_down' => intval($group_dn), 'qos_rate_max_up' => intval($group_up)]);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup', $json);
return $this->process_response($response);
}
/**
* Delete user group (using REST)
* ------------------------------
* returns true on success
* required parameter <group_id> = id of the user group
*/
public function delete_usergroup($group_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup/'.trim($group_id));
return $this->process_response_boolean($response);
}
/**
* List health metrics
* -------------------
* returns an array of health metric objects
*/
public function list_health()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/health');
return $this->process_response($response);
}
/**
* List dashboard metrics
* ----------------------
* returns an array of dashboard metric objects (available since controller version 4.9.1.alpha)
* optional parameter <five_minutes> = boolean; if true, return stats based on 5 minute intervals,
* returns hourly stats by default (supported on controller versions 5.5.* and higher)
*/
public function list_dashboard($five_minutes = false)
{
if (!$this->is_loggedin) return false;
$url_suffix = $five_minutes ? '?scale=5minutes' : null;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/dashboard'.$url_suffix);
return $this->process_response($response);
}
/**
* List client devices
* -------------------
* returns an array of known client device objects
*/
public function list_users()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/list/user');
return $this->process_response($response);
}
/**
* List access points and other devices under management of the controller (USW and/or USG devices)
* ------------------------------------------------------------------------------------------------
* returns an array of known device objects (or a single device when using the <device_mac> parameter)
* optional parameter <device_mac> = the MAC address of a single device for which the call must be made
*/
public function list_devices($device_mac = null)
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/device/'.trim($device_mac));
return $this->process_response($response);
}
/**
* List (device) tags (using REST)
* -------------------------------
* returns an array of known device tag objects
*
* NOTES: this endpoint was introduced with controller versions 5.5.X
*/
public function list_tags()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/tag');
return $this->process_response($response);
}
/**
* List rogue/neighboring access points
* ------------------------------------
* returns an array of rogue/neighboring access point objects
* optional parameter <within> = hours to go back to list discovered "rogue" access points (default = 24 hours)
*/
public function list_rogueaps($within = 24)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['within' => intval($within)]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/rogueap', 'json='.$json);
return $this->process_response($response);
}
/**
* List known rogue access points
* ------------------------------
* returns an array of known rogue access point objects
*/
public function list_known_rogueaps()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/rogueknown');
return $this->process_response($response);
}
/**
* List sites
* ----------
* returns a list sites hosted on this controller with some details
*/
public function list_sites()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/self/sites');
return $this->process_response($response);
}
/**
* List sites stats
* ----------------
* returns statistics for all sites hosted on this controller
*
* NOTES: this endpoint was introduced with controller version 5.2.9
*/
public function stat_sites()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/stat/sites');
return $this->process_response($response);
}
/**
* Create a site
* -------------
* returns an array containing a single object with attributes of the new site ("_id", "desc", "name") on success
* required parameter <description> = the long name for the new site
*
* NOTES: immediately after being added, the new site will be available in the output of the "list_sites" function
*/
public function create_site($description)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['desc' => $description, 'cmd' => 'add-site']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json);
return $this->process_response($response);
}
/**
* Delete a site
* -------------
* return true on success
* required parameter <site_id> = 24 char string; _id of the site to delete
*/
public function delete_site($site_id)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['site' => $site_id, 'cmd' => 'delete-site']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* List admins
* -----------
* returns an array containing administrator objects for selected site
*/
public function list_admins()
{
if (!$this->is_loggedin) return false;
$json = json_encode(['cmd' => 'get-admins']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json);
return $this->process_response($response);
}
/**
* List all admins
* ---------------
* returns an array containing administrator objects for all sites
*/
public function list_all_admins()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/stat/admin');
return $this->process_response($response);
}
/**
* List wlan_groups
* ----------------
* returns an array containing known wlan_groups
*/
public function list_wlan_groups()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/list/wlangroup');
return $this->process_response($response);
}
/**
* Show sysinfo
* ------------
* returns an array of known sysinfo data
*/
public function stat_sysinfo()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/sysinfo');
return $this->process_response($response);
}
/**
* Get controller status
* ---------------------
* returns true upon success (controller is online)
*
* NOTES: in order to get useful results (e.g. controller version) you can call get_last_results_raw()
* immediately after this method
*/
public function stat_status()
{
$response = $this->exec_curl('/status');
return $this->process_response_boolean($response);
}
/**
* List self
* ---------
* returns an array of information about the logged in user
*/
public function list_self()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/self');
return $this->process_response($response);
}
/**
* List vouchers
* -------------
* returns an array of hotspot voucher objects
* optional parameter <create_time> = Unix timestamp in seconds
*/
public function stat_voucher($create_time = null)
{
if (!$this->is_loggedin) return false;
$json = (trim($create_time) != null) ? json_encode(['create_time' => intval($create_time)]) : json_encode([]);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/voucher', 'json='.$json);
return $this->process_response($response);
}
/**
* List payments
* -------------
* returns an array of hotspot payments
* optional parameter <within> = number of hours to go back to fetch payments
*/
public function stat_payment($within = null)
{
if (!$this->is_loggedin) return false;
$url_suffix = (($within != null) ? '?within='.intval($within) : '');
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/payment'.$url_suffix);
return $this->process_response($response);
}
/**
* Create hotspot operator (using REST)
* ------------------------------------
* return true upon success
* required parameter <name> = name for the hotspot operator
* required parameter <x_password> = clear text password for the hotspot operator
* optional parameter <note> = note to attach to the hotspot operator
*/
public function create_hotspotop($name, $x_password, $note = null)
{
if (!$this->is_loggedin) return false;
$json = ['name' => $name, 'x_password' => $x_password];
if (isset($note)) $json['note'] = trim($note);
$json = json_encode($json);