-
Notifications
You must be signed in to change notification settings - Fork 152
/
Client.php
executable file
·4518 lines (3989 loc) · 170 KB
/
Client.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
namespace UniFi_API;
use Exception;
use UniFi_API\Exceptions\CurlGeneralErrorException;
use UniFi_API\Exceptions\CurlExtensionNotLoadedException;
use UniFi_API\Exceptions\CurlTimeoutException;
use UniFi_API\Exceptions\EmailInvalidException;
use UniFi_API\Exceptions\InvalidBaseUrlException;
use UniFi_API\Exceptions\InvalidCurlMethodException;
use UniFi_API\Exceptions\InvalidSiteNameException;
use UniFi_API\Exceptions\JsonDecodeException;
use UniFi_API\Exceptions\LoginFailedException;
use UniFi_API\Exceptions\LoginRequiredException;
use UniFi_API\Exceptions\MethodDeprecatedException;
/**
* The UniFi API client class.
*
* This UniFi API client class is based on the work done by the following developers:
* domwo: https://community.ui.com/questions/little-php-class-for-unifi-api/933d3fb3-b401-4499-993a-f9af079a4a3a
* fbagnol: https://github.com/fbagnol/class.unifi.php
* and the API as published by Ubiquiti:
* https://dl.ui.com/unifi/<UniFi controller version number>/unifi_sh_api
*
* @package UniFi_Controller_API_Client_Class
* @author Art of WiFi <[email protected]>
* @license This class is subject to the MIT license bundled with this package in the file LICENSE.md
* @example This directory in the package repository contains a collection of examples:
* https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples
*/
class Client
{
/** Constants. */
const CLASS_VERSION = '2.0.2';
const CURL_METHODS_ALLOWED = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
const DEFAULT_CURL_METHOD = 'GET';
/**
* Protected properties.
*
* @note do **not** directly edit the property values below, instead use the constructor or the respective
* getter and setter functions/methods
*/
protected string $baseurl = '';
protected string $user = '';
protected string $password = '';
protected string $site = '';
protected string $version = '';
protected bool $debug = false;
protected bool $is_logged_in = false;
protected bool $is_unifi_os = false;
protected int $exec_retries = 0;
protected string $cookies = '';
protected int $cookies_created_at = 0;
protected $last_results_raw = null;
protected string $last_error_message = '';
protected bool $curl_ssl_verify_peer = false;
protected int $curl_ssl_verify_host = 0;
protected int $curl_http_version = CURL_HTTP_VERSION_NONE;
protected string $curl_method = self::DEFAULT_CURL_METHOD;
protected int $curl_request_timeout = 30;
protected int $curl_connect_timeout = 10;
protected string $unificookie_name = 'unificookie';
protected array $curl_headers = [
'Accept: application/json',
'Content-Type: application/json',
'Expect:',
];
protected array $default_site_stats_attribs = [
'bytes',
'wan-tx_bytes',
'wan-rx_bytes',
'wlan_bytes',
'num_sta',
'lan-num_sta',
'wlan-num_sta',
'time',
];
/**
* Construct an instance of the UniFi API client class.
*
* @param string $user username to use when connecting to the UniFi controller
* @param string $password password to use when connecting to the UniFi controller
* @param string $baseurl base URL of the UniFi controller which *must* include an 'https://' prefix,
* a port suffix (e.g. :8443) is required for non-UniFi OS controllers,
* do not add trailing slashes, defaults to 'https://127.0.0.1:8443'
* @param string|null $site short site name to access, defaults to 'default'
* @param string|null $version the version number of the controller, defaults to '8.0.28'
* @param bool $ssl_verify whether to validate the controller's SSL certificate or not, a value of true
* is recommended for production environments to prevent potential MitM attacks, defaults
* to false which disables validation of the controller's SSL certificate
* @param string $unificookie_name name of the cookie to use, defaults to 'unificookie'. This is only necessary
* when you have multiple apps using this API client on the same web server.
* @throws CurlExtensionNotLoadedException|InvalidBaseUrlException|InvalidSiteNameException
*/
public function __construct(
string $user,
string $password,
string $baseurl = 'https://127.0.0.1:8443',
string $site = 'default',
string $version = '8.0.28',
bool $ssl_verify = false,
string $unificookie_name = 'unificookie'
)
{
if (!extension_loaded('curl')) {
throw new CurlExtensionNotLoadedException();
}
$this->check_base_url($baseurl);
$this->check_site($site);
$this->baseurl = trim($baseurl);
$this->site = strtolower(trim($site));
$this->user = trim($user);
$this->password = trim($password);
$this->version = trim($version);
$this->unificookie_name = trim($unificookie_name);
if ($ssl_verify === true) {
$this->curl_ssl_verify_peer = true;
$this->curl_ssl_verify_host = 2;
}
}
/**
* This method is called as soon as there are no other references to the class instance.
*
* @see https://www.php.net/manual/en/language.oop5.decon.php
* @note to force the class instance to log out when you're done, call logout()
* @throws CurlGeneralErrorException|CurlTimeoutException|CurlTimeoutException
*/
public function __destruct()
{
/** If $_SESSION[$this->unificookie_name] is set, do not log out here. */
if (isset($_SESSION[$this->unificookie_name])) {
return;
}
/** Log out, if needed. */
if ($this->is_logged_in) {
$this->logout();
}
}
/**
* Login to the UniFi controller.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses
* @return bool returns true upon a successful login
* @throws LoginFailedException|CurlTimeoutException|CurlGeneralErrorException
*/
public function login(): bool
{
/** Skip the login process if already logged in. */
if ($this->update_unificookie()) {
$this->is_logged_in = true;
}
if ($this->is_logged_in === true) {
return true;
}
/** Prepare cURL and options to check whether this is a "regular" controller or one based on UniFi OS. */
$ch = $this->get_curl_handle();
$curl_options = [
CURLOPT_URL => $this->baseurl . '/',
];
curl_setopt_array($ch, $curl_options);
/** Execute the cURL request and get the HTTP response code. */
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$error_number = curl_errno($ch);
if ($error_number) {
if (in_array($error_number, [CURLE_OPERATION_TIMEDOUT, CURLE_OPERATION_TIMEOUTED])) {
throw new CurlTimeoutException(curl_error($ch), $http_code, curl_getinfo($ch));
}
throw new CurlGeneralErrorException(curl_error($ch), $http_code, curl_getinfo($ch));
}
/** Prepare the actual login. */
$curl_options = [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode(['username' => $this->user, 'password' => $this->password]),
CURLOPT_HTTPHEADER => $this->curl_headers,
CURLOPT_REFERER => $this->baseurl . '/login',
CURLOPT_URL => $this->baseurl . '/api/login',
];
/** Specific to UniFi OS-based controllers. */
if ($http_code === 200) {
$this->is_unifi_os = true;
$curl_options[CURLOPT_URL] = $this->baseurl . '/api/auth/login';
}
curl_setopt_array($ch, $curl_options);
/** Execute the cURL request and get the HTTP response code. */
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($this->debug) {
print PHP_EOL . '<pre>';
print PHP_EOL . '-----------LOGIN-------------' . PHP_EOL;
print_r(curl_getinfo($ch));
print PHP_EOL . '----------RESPONSE-----------' . PHP_EOL;
print $response;
print PHP_EOL . '-----------------------------' . PHP_EOL;
print '</pre>' . PHP_EOL;
}
$error_number = curl_errno($ch);
if ($error_number) {
if (in_array($error_number, [CURLE_OPERATION_TIMEDOUT, CURLE_OPERATION_TIMEOUTED])) {
throw new CurlTimeoutException(curl_error($ch), $http_code, curl_getinfo($ch));
}
throw new CurlGeneralErrorException(curl_error($ch), $http_code, curl_getinfo($ch));
}
/** If the HTTP response code is 200, we are logged in. */
if ($http_code === 200) {
curl_close($ch);
return $this->is_logged_in = true;
}
throw new LoginFailedException($http_code);
}
/**
* Logout from the UniFi controller.
*
* @return bool true upon success
* @throws CurlGeneralErrorException|CurlTimeoutException|CurlTimeoutException
*/
public function logout(): bool
{
/** Prepare cURL and options. */
$ch = $this->get_curl_handle();
$curl_options = [
CURLOPT_HEADER => true,
CURLOPT_POST => true,
];
$logout_path = '/logout';
if ($this->is_unifi_os) {
$logout_path = '/api/auth/logout';
$curl_options[CURLOPT_CUSTOMREQUEST] = 'POST';
$this->create_x_csrf_token_header();
}
$curl_options[CURLOPT_HTTPHEADER] = $this->curl_headers;
$curl_options[CURLOPT_URL] = $this->baseurl . $logout_path;
curl_setopt_array($ch, $curl_options);
/** Execute the cURL request to logout. */
curl_exec($ch);
$error_number = curl_errno($ch);
if ($error_number) {
if (in_array($error_number, [CURLE_OPERATION_TIMEDOUT, CURLE_OPERATION_TIMEOUTED])) {
throw new CurlTimeoutException(curl_error($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE), curl_getinfo($ch));
}
throw new CurlGeneralErrorException(curl_error($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE), curl_getinfo($ch));
}
curl_close($ch);
$this->is_logged_in = false;
$this->cookies = '';
$this->cookies_created_at = 0;
return true;
}
/****************************************************************
* Functions to access UniFi controller API routes from here:
****************************************************************/
/**
* Authorize a client device.
*
* @param string $mac client MAC address
* @param int $minutes minutes (from now) until authorization expires
* @param int|null $up optional, upload speed limit in kbps
* @param int|null $down optional, download speed limit in kbps
* @param int|null $megabytes optional, data transfer limit in MB
* @param string|null $ap_mac optional, AP MAC address to which client is connected, should result in faster
* authorization for the client device
* @return bool true upon success
* @throws Exception
*/
public function authorize_guest(string $mac, int $minutes, int $up = null, int $down = null, int $megabytes = null, string $ap_mac = null): bool
{
$payload = ['cmd' => 'authorize-guest', 'mac' => strtolower($mac), 'minutes' => $minutes];
/** append received values for up/down/megabytes/ap_mac to the payload array to be submitted */
if (!empty($up)) {
$payload['up'] = $up;
}
if (!empty($down)) {
$payload['down'] = $down;
}
if (!empty($megabytes)) {
$payload['bytes'] = $megabytes;
}
if (!empty($ap_mac) && filter_var($ap_mac, FILTER_VALIDATE_MAC)) {
$payload['ap_mac'] = strtolower($ap_mac);
}
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Unauthorize a client device.
*
* @param string $mac client MAC address
* @return bool true upon success
* @throws Exception
*/
public function unauthorize_guest(string $mac): bool
{
$payload = ['cmd' => 'unauthorize-guest', 'mac' => strtolower($mac)];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Reconnect a client device
*
* @param string $mac client MAC address
* @return bool true upon success
* @throws Exception
*/
public function reconnect_sta(string $mac): bool
{
$payload = ['cmd' => 'kick-sta', 'mac' => strtolower($mac)];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Block a client device.
*
* @param string $mac client MAC address
* @return bool true upon success
* @throws Exception
*/
public function block_sta(string $mac): bool
{
$payload = ['cmd' => 'block-sta', 'mac' => strtolower($mac)];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Unblock a client device.
*
* @param string $mac client MAC address
* @return bool true upon success
* @throws Exception
*/
public function unblock_sta(string $mac): bool
{
$payload = ['cmd' => 'unblock-sta', 'mac' => strtolower($mac)];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Forget one or more client devices.
*
* @note only supported with controller versions 5.9.X and higher, can be
* slow (up to 5 minutes) on larger controllers
* @param array|string $mac array of client MAC addresses (strings) or a single MAC address string
* @return bool true upon success
* @throws Exception
*/
public function forget_sta($mac): bool
{
$payload = [
'cmd' => 'forget-sta',
'macs' => array_map('strtolower', (array)$mac)
];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/cmd/stamgr', $payload);
}
/**
* Create a new user/client-device.
*
* @param string $mac client MAC address
* @param string $user_group_id _id value for the user group the new user/client-device should belong to which
* can be obtained from the output of list_usergroups()
* @param string|null $name optional, name to be given to the new user/client-device
* @param string|null $note optional, note to be applied to the new user/client-device
* @param bool|null $is_guest optional, defines whether the new user/client-device is a guest or not
* @param bool|null $is_wired optional, defines whether the new user/client-device is wired or not
* @return array|bool returns an array with a single object containing details of the new user/client-device on
* success, else returns false
* @throws Exception
*/
public function create_user(
string $mac,
string $user_group_id,
string $name = null,
string $note = null,
bool $is_guest = null,
bool $is_wired = null
)
{
$new_user = ['mac' => strtolower($mac), 'usergroup_id' => $user_group_id];
if (!empty($name)) {
$new_user['name'] = $name;
}
if (!empty($note)) {
$new_user['note'] = $note;
}
if (!empty($is_guest)) {
$new_user['is_guest'] = $is_guest;
}
if (!empty($is_wired)) {
$new_user['is_wired'] = $is_wired;
}
$payload = ['objects' => [['data' => $new_user]]];
return $this->fetch_results('/api/s/' . $this->site . '/group/user', $payload);
}
/**
* Add/modify/remove a client-device note
*
* @param string $user_id id of the client-device to be modified
* @param string $note optional, note to be applied to the client-device
* @return bool true upon success
* @throws Exception
*/
public function set_sta_note(string $user_id, string $note = ''): bool
{
$payload = ['note' => $note];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload);
}
/**
* Add/modify/remove a client device name
*
* @param string $user_id id of the client-device to be modified
* @param string $name optional, name to be applied to the client device, when empty or not set,
* the existing name for the client device is removed
* @return bool true upon success
* @throws Exception
*/
public function set_sta_name(string $user_id, string $name = ''): bool
{
$payload = ['name' => $name];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload);
}
/**
* Fetch 5-minute site stats.
*
* @note - 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
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs optional, array of attributes to collect. Default values:
* 'bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta',
* 'wlan-num_sta', 'time'
* Example values:
* 'airtime_avg', 'latency_avg', 'latency_min', 'latency_max'
* @return array|bool returns an array of 5-minute stats objects for the current site
* @throws Exception
*/
public function stat_5minutes_site(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (12 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? $this->default_site_stats_attribs : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/5minutes.site', $payload);
}
/**
* Fetch hourly site stats.
*
* TODO: add support for optional attrib parameter
* airtime_avg
*
* @note - defaults to the past 7*24 hours
* - "bytes" are no longer returned with controller version 4.9.1 and later
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs optional, array of attributes to collect.
* @return array|bool returns an array of hourly stats objects for the current site
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_hourly_site(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? $this->default_site_stats_attribs : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/hourly.site', $payload);
}
/**
* Fetch daily site stats.
*
* @note - defaults to the past 52*7*24 hours
* - "bytes" are no longer returned with controller version 4.9.1 and later
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs optional, array of attributes to collect.
* @return array|bool returns an array of daily stats objects for the current site
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_daily_site(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? (time() - (time() % 3600)) * 1000 : $end;
$start = empty($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? $this->default_site_stats_attribs : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/daily.site', $payload);
}
/**
* Fetch monthly site stats.
*
* @note - defaults to the past 52 weeks (52*7*24 hours)
* - "bytes" are no longer returned with controller version 4.9.1 and later
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs optional, array of attributes to collect.
* @return array|bool returns an array of monthly stats objects for the current site
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_monthly_site(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? (time() - (time() % 3600)) * 1000 : $end;
$start = empty($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? $this->default_site_stats_attribs : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/monthly.site', $payload);
}
/**
* Fetch 5-minutes stats for a single access point or all access points.
*
* @note - 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
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param string|null $mac optional, AP MAC address to return stats for, when empty,
* stats for all APs are returned
* @param array|null $attribs optional, array of attributes to collect, default: (bytes, num_sta, time). Example values:
* 'bytes',
* 'num_sta',
* 'time',
* 'wifi_tx_attempts',
* 'tx_retries',
* 'wifi_tx_dropped',
* 'mac_filter_rejections',
* 'user-wlan-num_sta_connected',
* 'user-wlan-num_sta_disconnected',
* 'na-wifi_tx_attempts',
* 'ng-wifi_tx_attempts',
* 'na-wifi_tx_dropped',
* 'ng-wifi_tx_dropped',
* 'na-tx_retries',
* 'ng-tx_retries',
* 'na-tx_packets',
* 'ng-tx_packets',
* 'na-tx_bytes',
* 'ng-tx_bytes',
* 'na-rx_packets',
* 'ng-rx_packets',
* 'na-rx_bytes',
* 'ng-rx_bytes',
* @return array|bool returns an array of 5-minute stats objects
* @throws Exception
*/
public function stat_5minutes_aps(int $start = null, int $end = null, string $mac = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (12 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['bytes', 'num_sta', 'time'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/5minutes.ap', $payload);
}
/**
* Fetch hourly stats for a single access point or all access points.
*
* @note - defaults to the past 7*24 hours
* - make sure that the retention policy for hourly stats is set to the correct value in
* the controller settings
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param string|null $mac optional, AP MAC address to return stats for, when empty,
* stats for all APs are returned
* @param array|null $attribs optional, array of attributes to collect, default: (bytes, num_sta, time).
* @return array|bool returns an array of hourly stats objects
* @throws Exception
* @see stat_5minutes_aps() for supported attribs
*/
public function stat_hourly_aps(int $start = null, int $end = null, string $mac = null, array $attribs = null)
{
$end = empty($end) ? (time() * 1000) : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['bytes', 'num_sta', 'time'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/hourly.ap', $payload);
}
/**
* Fetch daily stats for a single access point or all access points.
*
* @note - defaults to the past 7*24 hours
* - make sure that the retention policy for hourly stats is set to the correct value in
* the controller settings
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param string|null $mac optional, AP MAC address to return stats for, when empty,
* stats for all APs are returned
* @param array|null $attribs optional, array of attributes to collect, default: (bytes, num_sta, time).
* @return array|bool returns an array of daily stats objects
* @throws Exception
* @see stat_5minutes_aps() for supported attribs
*/
public function stat_daily_aps(int $start = null, int $end = null, string $mac = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['bytes', 'num_sta', 'time'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/daily.ap', $payload);
}
/**
* Fetch monthly stats for a single access point or all access points
*
* @note - defaults to the past 52 weeks (52*7*24 hours)
* - make sure that the retention policy for hourly stats is set to the correct value in
* the controller settings
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param string|null $mac optional, AP MAC address to return stats for, when empty,
* stats for all APs are returned
* @param array|null $attribs optional, array of attributes to collect, default: (bytes, num_sta, time).
* @return array|bool returns an array of monthly stats objects
* @throws Exception
* @see stat_5minutes_aps() for supported attribs
*/
public function stat_monthly_aps(int $start = null, int $end = null, string $mac = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['bytes', 'num_sta', 'time'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/monthly.ap', $payload);
}
/**
* Fetch 5-minutes stats for a single user/client device or all user/client devices.
*
* @note - defaults to the past 12 hours
* - only supported with UniFi controller versions 5.8.X and higher
* - make sure that the retention policy for 5-minute stats is set to the correct value in
* the controller settings
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
* section
* @param string|null $mac optional, MAC address of the user/client device to return stats for
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned, valid values are:
* rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets,
* tx_packets, satisfaction, wifi_tx_attempts, 'duration'
* default value is ['rx_bytes', 'tx_bytes', 'time']
* @return array|bool returns an array of 5-minute stats objects
* @throws Exception
*/
public function stat_5minutes_user(string $mac = null, int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (12 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'rx_bytes', 'tx_bytes'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end, 'mac' => strtolower($mac)];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/5minutes.user', $payload);
}
/**
* Fetch hourly stats for a single user/client device or all user/client devices.
*
* @note - defaults to the past 7*24 hours
* - only supported with UniFi controller versions 5.8.X and higher
* - make sure that the retention policy for hourly stats is set to the correct value in
* the controller settings
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
* section
* @param string|null $mac optional, MAC address of the user/client device to return stats for
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of hourly stats objects
* @throws Exception
* @see stat_5minutes_user() for details on attribs
*/
public function stat_hourly_user(string $mac = null, int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'rx_bytes', 'tx_bytes'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/hourly.user', $payload);
}
/**
* Fetch daily stats for a single user/client device or all user/client devices.
*
* @note - defaults to the past 7*24 hours
* - only supported with UniFi controller versions 5.8.X and higher
* - make sure that the retention policy for daily stats is set to the correct value in
* the controller settings
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
* section
* @param string|null $mac optional, MAC address of the user/client device to return stats for
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of daily stats objects
* @throws Exception
* @see stat_5minutes_user() for details on attribs
*/
public function stat_daily_user(string $mac = null, int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'rx_bytes', 'tx_bytes'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/daily.user', $payload);
}
/**
* Fetch monthly stats for a single user/client device or all user/client devices.
*
* @note - defaults to the past 13 weeks (52*7*24 hours)
* - only supported with UniFi controller versions 5.8.X and higher
* - make sure that the retention policy for monthly stats is set to the correct value in
* the controller settings
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
* section
* @param string|null $mac optional, MAC address of the user/client device to return stats for
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of monthly stats objects
* @throws Exception
* @see stat_5minutes_user() for details on attribs
*/
public function stat_monthly_user(string $mac = null, int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (13 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'rx_bytes', 'tx_bytes'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
if (!empty($mac)) {
$payload['mac'] = strtolower($mac);
}
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/monthly.user', $payload);
}
/**
* Fetch 5-minute gateway stats.
*
* @note - 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
* - requires a UniFi gateway
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned, valid values are:
* mem, cpu, loadavg_5, lan-rx_errors, lan-tx_errors, lan-rx_bytes,
* lan-tx_bytes, lan-rx_packets, lan-tx_packets, lan-rx_dropped, lan-tx_dropped
* default is ['time', 'mem', 'cpu', 'loadavg_5']
* @return array|bool returns an array of 5-minute stats objects for the gateway belonging to the current site
* @throws Exception
*/
public function stat_5minutes_gateway(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (12 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/5minutes.gw', $payload);
}
/**
* Fetch hourly gateway stats.
*
* @note - defaults to the past 7*24 hours
* - requires a UniFi gateway
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of hourly stats objects for the gateway belonging to the current site
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_hourly_gateway(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/hourly.gw', $payload);
}
/**
* Fetch daily gateway stats.
*
* @note - defaults to the past 52 weeks (52*7*24 hours)
* - requires a UniFi gateway
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of hourly stats objects for the gateway belonging to the current site
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_daily_gateway(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? (time() - (time() % 3600)) * 1000 : $end;
$start = empty($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/daily.gw', $payload);
}
/**
* Fetch monthly gateway stats.
*
* @note - defaults to the past 52 weeks (52*7*24 hours)
* - requires a UniFi gateway
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param array|null $attribs array containing attributes (strings) to be returned
* @return array|bool returns an array of monthly stats objects for the gateway belonging to the current site
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_monthly_gateway(int $start = null, int $end = null, array $attribs = null)
{
$end = empty($end) ? (time() - (time() % 3600)) * 1000 : $end;
$start = empty($start) ? $end - (52 * 7 * 24 * 3600 * 1000) : $start;
$attribs = empty($attribs) ? ['time', 'mem', 'cpu', 'loadavg_5'] : array_merge(['time'], $attribs);
$payload = ['attrs' => $attribs, 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/monthly.gw', $payload);
}
/**
* Fetch speed test results.
*
* @note - defaults to the past 24 hours
* - requires a UniFi gateway
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @return array|bool returns an array of speed test result objects
* @throws Exception
*/
public function stat_speedtest_results(int $start = null, int $end = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (24 * 3600 * 1000) : $start;
$payload = ['attrs' => ['xput_download', 'xput_upload', 'latency', 'time'], 'start' => $start, 'end' => $end];
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/archive.speedtest', $payload);
}
/**
* Fetch IPS/IDS events.
*
* @note - defaults to the past 24 hours
* - requires a UniFi gateway
* - supported in UniFi controller versions 5.9.X and higher
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param int|null $limit optional, maximum number of events to return, defaults to 10000
* @return array|bool returns an array of IPS/IDS event objects
* @throws Exception
*/
public function stat_ips_events(int $start = null, int $end = null, int $limit = null)
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (24 * 3600 * 1000) : $start;
$limit = empty($limit) ? 10000 : $limit;
$payload = ['start' => $start, 'end' => $end, '_limit' => $limit];
return $this->fetch_results('/api/s/' . $this->site . '/stat/ips/event', $payload);
}
/**
* Fetch login sessions
*
* @note defaults to the past 7*24 hours
*
* @param int|null $start optional, Unix timestamp in milliseconds
* @param int|null $end optional, Unix timestamp in milliseconds
* @param string|null $mac optional, client MAC address to return sessions for (can only be used when start and end
* are also provided)
* @param string $type optional, client type to return sessions for, can be 'all', 'guest' or 'user'; default
* value is 'all'
* @return array|bool returns an array of login session objects for all devices or a single device
* @throws Exception
*/
public function stat_sessions(int $start = null, int $end = null, string $mac = null, string $type = 'all')
{
if (!in_array($type, ['all', 'guest', 'user'])) {
return false;
}
$end = empty($end) ? time() : $end;
$start = empty($start) ? $end - (7 * 24 * 3600) : $start;
$payload = ['type' => $type, 'start' => $start, 'end' => $end];
if (!empty($mac)) {