20
20
class Client
21
21
{
22
22
/** Constants. */
23
- const CLASS_VERSION = '1.1.96 ' ;
23
+ const CLASS_VERSION = '1.1.99 ' ;
24
24
const CURL_METHODS_ALLOWED = ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' ];
25
25
const DEFAULT_CURL_METHOD = 'GET ' ;
26
26
@@ -677,24 +677,24 @@ public function stat_monthly_aps(int $start = null, int $end = null, string $mac
677
677
}
678
678
679
679
/**
680
- * Fetch 5-minutes stats for a single user/client device.
680
+ * Fetch 5-minutes stats for a single user/client device or all user/client devices .
681
681
*
682
682
* @note - defaults to the past 12 hours
683
683
* - only supported with UniFi controller versions 5.8.X and higher
684
684
* - make sure that the retention policy for 5-minute stats is set to the correct value in
685
685
* the controller settings
686
686
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
687
687
* section
688
- * @param string $mac MAC address of the user/client device to return stats for
688
+ * @param string|null $mac optional, MAC address of the user/client device to return stats for
689
689
* @param int|null $start optional, Unix timestamp in milliseconds
690
690
* @param int|null $end optional, Unix timestamp in milliseconds
691
691
* @param array|null $attribs array containing attributes (strings) to be returned, valid values are:
692
692
* rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets,
693
- * tx_packets, satisfaction, wifi_tx_attempts
694
- * default value is ['rx_bytes', 'tx_bytes']
693
+ * tx_packets, satisfaction, wifi_tx_attempts, 'duration'
694
+ * default value is ['rx_bytes', 'tx_bytes', 'time' ]
695
695
* @return array|bool returns an array of 5-minute stats objects
696
696
*/
697
- public function stat_5minutes_user (string $ mac , int $ start = null , int $ end = null , array $ attribs = null )
697
+ public function stat_5minutes_user (string $ mac = null , int $ start = null , int $ end = null , array $ attribs = null )
698
698
{
699
699
$ end = empty ($ end ) ? time () * 1000 : $ end ;
700
700
$ start = empty ($ start ) ? $ end - (12 * 3600 * 1000 ) : $ start ;
@@ -705,7 +705,7 @@ public function stat_5minutes_user(string $mac, int $start = null, int $end = nu
705
705
}
706
706
707
707
/**
708
- * Fetch hourly stats for a single user/client device.
708
+ * Fetch hourly stats for a single user/client device or all user/client devices .
709
709
*
710
710
* @note - defaults to the past 7*24 hours
711
711
* - only supported with UniFi controller versions 5.8.X and higher
@@ -714,24 +714,28 @@ public function stat_5minutes_user(string $mac, int $start = null, int $end = nu
714
714
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
715
715
* section
716
716
* @see stat_5minutes_user() for details on attribs
717
- * @param string $mac MAC address of the user/client device to return stats fo
717
+ * @param string|null $mac optional, MAC address of the user/client device to return stats for
718
718
* @param int|null $start optional, Unix timestamp in milliseconds
719
719
* @param int|null $end optional, Unix timestamp in milliseconds
720
720
* @param array|null $attribs array containing attributes (strings) to be returned
721
721
* @return array|bool returns an array of hourly stats objects
722
722
*/
723
- public function stat_hourly_user (string $ mac , int $ start = null , int $ end = null , array $ attribs = null )
723
+ public function stat_hourly_user (string $ mac = null , int $ start = null , int $ end = null , array $ attribs = null )
724
724
{
725
725
$ end = empty ($ end ) ? time () * 1000 : $ end ;
726
726
$ start = empty ($ start ) ? $ end - (7 * 24 * 3600 * 1000 ) : $ start ;
727
727
$ attribs = empty ($ attribs ) ? ['time ' , 'rx_bytes ' , 'tx_bytes ' ] : array_merge (['time ' ], $ attribs );
728
- $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end , 'mac ' => strtolower ($ mac )];
728
+ $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end ];
729
+
730
+ if (!empty ($ mac )) {
731
+ $ payload ['mac ' ] = strtolower ($ mac );
732
+ }
729
733
730
734
return $ this ->fetch_results ('/api/s/ ' . $ this ->site . '/stat/report/hourly.user ' , $ payload );
731
735
}
732
736
733
737
/**
734
- * Fetch daily stats for a single user/client device.
738
+ * Fetch daily stats for a single user/client device or all user/client devices .
735
739
*
736
740
* @note - defaults to the past 7*24 hours
737
741
* - only supported with UniFi controller versions 5.8.X and higher
@@ -740,44 +744,52 @@ public function stat_hourly_user(string $mac, int $start = null, int $end = null
740
744
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
741
745
* section
742
746
* @see stat_5minutes_user() for details on attribs
743
- * @param string $mac MAC address of the user/client device to return stats for
747
+ * @param string|null $mac optional, MAC address of the user/client device to return stats for
744
748
* @param int|null $start optional, Unix timestamp in milliseconds
745
749
* @param int|null $end optional, Unix timestamp in milliseconds
746
750
* @param array|null $attribs array containing attributes (strings) to be returned
747
751
* @return array|bool returns an array of daily stats objects
748
752
*/
749
- public function stat_daily_user (string $ mac , int $ start = null , int $ end = null , array $ attribs = null )
753
+ public function stat_daily_user (string $ mac = null , int $ start = null , int $ end = null , array $ attribs = null )
750
754
{
751
755
$ end = empty ($ end ) ? time () * 1000 : $ end ;
752
756
$ start = empty ($ start ) ? $ end - (7 * 24 * 3600 * 1000 ) : $ start ;
753
757
$ attribs = empty ($ attribs ) ? ['time ' , 'rx_bytes ' , 'tx_bytes ' ] : array_merge (['time ' ], $ attribs );
754
- $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end , 'mac ' => strtolower ($ mac )];
758
+ $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end ];
759
+
760
+ if (!empty ($ mac )) {
761
+ $ payload ['mac ' ] = strtolower ($ mac );
762
+ }
755
763
756
764
return $ this ->fetch_results ('/api/s/ ' . $ this ->site . '/stat/report/daily.user ' , $ payload );
757
765
}
758
766
759
767
/**
760
- * Fetch monthly stats for a single user/client device.
768
+ * Fetch monthly stats for a single user/client device or all user/client devices .
761
769
*
762
770
* @note - defaults to the past 13 weeks (52*7*24 hours)
763
771
* - only supported with UniFi controller versions 5.8.X and higher
764
772
* - make sure that the retention policy for monthly stats is set to the correct value in
765
773
* the controller settings
766
774
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
767
775
* section
768
- * @see stat_5minutes_user() for details on attribs
769
- * @param string $mac MAC address of the user/client device to return stats for
776
+ * @param string|null $mac optional, MAC address of the user/client device to return stats for
770
777
* @param int|null $start optional, Unix timestamp in milliseconds
771
778
* @param int|null $end optional, Unix timestamp in milliseconds
772
779
* @param array|null $attribs array containing attributes (strings) to be returned
773
780
* @return array|bool returns an array of monthly stats objects
781
+ * @see stat_5minutes_user() for details on attribs
774
782
*/
775
- public function stat_monthly_user (string $ mac , int $ start = null , int $ end = null , array $ attribs = null )
783
+ public function stat_monthly_user (string $ mac = null , int $ start = null , int $ end = null , array $ attribs = null )
776
784
{
777
785
$ end = empty ($ end ) ? time () * 1000 : $ end ;
778
786
$ start = empty ($ start ) ? $ end - (13 * 7 * 24 * 3600 * 1000 ) : $ start ;
779
787
$ attribs = empty ($ attribs ) ? ['time ' , 'rx_bytes ' , 'tx_bytes ' ] : array_merge (['time ' ], $ attribs );
780
- $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end , 'mac ' => strtolower ($ mac )];
788
+ $ payload = ['attrs ' => $ attribs , 'start ' => $ start , 'end ' => $ end ];
789
+
790
+ if (!empty ($ mac )) {
791
+ $ payload ['mac ' ] = strtolower ($ mac );
792
+ }
781
793
782
794
return $ this ->fetch_results ('/api/s/ ' . $ this ->site . '/stat/report/monthly.user ' , $ payload );
783
795
}
@@ -3365,6 +3377,63 @@ public function set_element_adoption(bool $enable): bool
3365
3377
return $ this ->fetch_results_boolean ('/api/s/ ' . $ this ->site . '/set/setting/element_adopt ' , $ payload );
3366
3378
}
3367
3379
3380
+ /**
3381
+ * Fetch system log entries.
3382
+ *
3383
+ * @note - defaults to the past 7*24 hours
3384
+ * - results are paged; use $page_number to iterate over pages and $page_size to set page size
3385
+ * @param string $class optional, class of the system log entries to fetch, known valid values:
3386
+ * 'device-alert', 'next-ai-alert', 'vpn-alert', 'admin-activity', 'update-alert',
3387
+ * 'client-alert', 'threat-alert', 'triggers', default value is 'device-alert'
3388
+ * @param ?int $start optional, start time in milliseconds since the Unix epoch
3389
+ * @param ?int $end optional, end time in milliseconds since the Unix epoch
3390
+ * @param int $page_number optional, page number to fetch, default value is 0 (first page)
3391
+ * @param int $page_size optional, number of entries to fetch per page, default value is 100
3392
+ * @param array $custom_payload optional, an array of additional parameters to pass with the request. Is merged
3393
+ * with the default payload array constructed by this method using array_merge().
3394
+ * @return array|bool array containing results from the selected system log section/class, false on error.
3395
+ * The 'data' key in the returned array contains the actual system log entries.
3396
+ * The returned array also contains the page number and size, and the total number of entries
3397
+ * available.
3398
+ */
3399
+ public function get_system_log (string $ class = 'device-alert ' , int $ start = null , int $ end = null , int $ page_number = 0 , int $ page_size = 100 , array $ custom_payload = [])
3400
+ {
3401
+ $ end = empty ($ end ) ? time () * 1000 : $ end ;
3402
+ $ start = empty ($ start ) ? $ end - (7 * 24 * 3600 * 1000 ) : $ start ;
3403
+
3404
+ $ payload = [
3405
+ 'pageNumber ' => $ page_number ,
3406
+ 'pageSize ' => $ page_size ,
3407
+ 'timestampFrom ' => $ start ,
3408
+ 'timestampTo ' => $ end ,
3409
+ ];
3410
+
3411
+ switch ($ class ) {
3412
+ case 'next-ai-alert ' :
3413
+ $ payload ['nextAiCategory ' ] = ['CLIENT ' , 'DEVICE ' , 'INTERNET ' , 'VPN ' ];
3414
+ break ;
3415
+ case 'admin-activity ' :
3416
+ $ payload ['activity_keys ' ] = ['ACCESSED_NETWORK_WEB ' , 'ACCESSED_NETWORK_IOS ' , 'ACCESSED_NETWORK_ANDROID ' ];
3417
+ $ payload ['change_keys ' ] = ['CLIENT ' , 'DEVICE ' , 'HOTSPOT ' , 'INTERNET ' , 'NETWORK ' , 'PROFILE ' , 'ROUTING ' , 'SECURITY ' , 'SYSTEM ' , 'VPN ' , 'WIFI ' ];
3418
+ break ;
3419
+ case 'update-alert ' :
3420
+ $ payload ['systemLogDeviceTypes ' ] = ['GATEWAYS ' , 'SWITCHES ' , 'ACCESS_POINT ' , 'SMART_POWER ' , 'BUILDING_TO_BUILDING_BRIDGES ' , 'UNIFI_LTE ' ];
3421
+ break ;
3422
+ case 'client-alert ' :
3423
+ $ payload ['clientType ' ] = ['GUEST ' , 'TELEPORT ' , 'VPN ' , 'WIRELESS ' , 'RADIUS ' , 'WIRED ' ];
3424
+ $ payload ['guestAuthorizationMethod ' ] = ['FACEBOOK_SOCIAL_GATEWAY ' , 'FREE_TRIAL ' , 'GOOGLE_SOCIAL_GATEWAY ' , 'NONE ' , 'PASSWORD ' , 'PAYMENT ' , 'RADIUS ' , 'VOUCHER ' ];
3425
+ break ;
3426
+ case 'threat-alert ' :
3427
+ $ payload ['threatTypes ' ] = ['HONEYPOT ' , 'THREAT ' ];
3428
+ break ;
3429
+ case 'triggers ' :
3430
+ $ payload ['triggerTypes ' ] = ['TRAFFIC_RULE ' , 'TRAFFIC_ROUTE ' , 'FIREWALL_RULE ' ];
3431
+ break ;
3432
+ }
3433
+
3434
+ return $ this ->fetch_results ('/v2/api/site/ ' . $ this ->site . '/system-log/ ' . $ class , array_merge ($ payload , $ custom_payload ));
3435
+ }
3436
+
3368
3437
/**
3369
3438
* List device states
3370
3439
*
@@ -3954,38 +4023,30 @@ protected function catch_json_last_error(): bool
3954
4023
return true ;
3955
4024
case JSON_ERROR_DEPTH :
3956
4025
$ error = 'The maximum stack depth has been exceeded ' ;
3957
-
3958
4026
break ;
3959
4027
case JSON_ERROR_STATE_MISMATCH :
3960
4028
$ error = 'Invalid or malformed JSON ' ;
3961
-
3962
4029
break ;
3963
4030
case JSON_ERROR_CTRL_CHAR :
3964
4031
$ error = 'Control character error, possibly incorrectly encoded ' ;
3965
-
3966
4032
break ;
3967
4033
case JSON_ERROR_SYNTAX :
3968
4034
$ error = 'Syntax error, malformed JSON ' ;
3969
-
3970
4035
break ;
3971
4036
case JSON_ERROR_UTF8 :
3972
4037
/** PHP >= 5.3.3 */
3973
4038
$ error = 'Malformed UTF-8 characters, possibly incorrectly encoded ' ;
3974
-
3975
4039
break ;
3976
4040
case JSON_ERROR_RECURSION :
3977
4041
/** PHP >= 5.5.0 */
3978
4042
$ error = 'One or more recursive references in the value to be encoded ' ;
3979
-
3980
4043
break ;
3981
4044
case JSON_ERROR_INF_OR_NAN :
3982
4045
/** PHP >= 5.5.0 */
3983
4046
$ error = 'One or more NAN or INF values in the value to be encoded ' ;
3984
-
3985
4047
break ;
3986
4048
case JSON_ERROR_UNSUPPORTED_TYPE :
3987
4049
$ error = 'A value of a type that cannot be encoded was given ' ;
3988
-
3989
4050
break ;
3990
4051
}
3991
4052
@@ -3994,11 +4055,9 @@ protected function catch_json_last_error(): bool
3994
4055
switch (json_last_error ()) {
3995
4056
case JSON_ERROR_INVALID_PROPERTY_NAME :
3996
4057
$ error = 'A property name that cannot be encoded was given ' ;
3997
-
3998
4058
break ;
3999
4059
case JSON_ERROR_UTF16 :
4000
4060
$ error = 'Malformed UTF-16 characters, possibly incorrectly encoded ' ;
4001
-
4002
4061
break ;
4003
4062
}
4004
4063
}
@@ -4116,15 +4175,13 @@ protected function response_header_callback($ch, string $header_line): int
4116
4175
$ this ->cookies = $ cookie_crumb ;
4117
4176
$ this ->is_logged_in = true ;
4118
4177
$ this ->is_unifi_os = false ;
4119
-
4120
4178
break ;
4121
4179
}
4122
4180
4123
4181
if (strpos ($ cookie_crumb , 'TOKEN ' ) !== false ) {
4124
4182
$ this ->cookies = $ cookie_crumb ;
4125
4183
$ this ->is_logged_in = true ;
4126
4184
$ this ->is_unifi_os = true ;
4127
-
4128
4185
break ;
4129
4186
}
4130
4187
}
0 commit comments