Skip to content

Commit 70f6a37

Browse files
committed
- added get_system_log() method to get data from the system logs, returns pages results
- minor formatting changes
1 parent ff9e6f0 commit 70f6a37

File tree

1 file changed

+88
-31
lines changed

1 file changed

+88
-31
lines changed

Diff for: src/Client.php

+88-31
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class Client
2121
{
2222
/** Constants. */
23-
const CLASS_VERSION = '1.1.96';
23+
const CLASS_VERSION = '1.1.99';
2424
const CURL_METHODS_ALLOWED = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
2525
const DEFAULT_CURL_METHOD = 'GET';
2626

@@ -677,24 +677,24 @@ public function stat_monthly_aps(int $start = null, int $end = null, string $mac
677677
}
678678

679679
/**
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.
681681
*
682682
* @note - defaults to the past 12 hours
683683
* - only supported with UniFi controller versions 5.8.X and higher
684684
* - make sure that the retention policy for 5-minute stats is set to the correct value in
685685
* the controller settings
686686
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
687687
* 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
689689
* @param int|null $start optional, Unix timestamp in milliseconds
690690
* @param int|null $end optional, Unix timestamp in milliseconds
691691
* @param array|null $attribs array containing attributes (strings) to be returned, valid values are:
692692
* 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']
695695
* @return array|bool returns an array of 5-minute stats objects
696696
*/
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)
698698
{
699699
$end = empty($end) ? time() * 1000 : $end;
700700
$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
705705
}
706706

707707
/**
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.
709709
*
710710
* @note - defaults to the past 7*24 hours
711711
* - 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
714714
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
715715
* section
716716
* @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
718718
* @param int|null $start optional, Unix timestamp in milliseconds
719719
* @param int|null $end optional, Unix timestamp in milliseconds
720720
* @param array|null $attribs array containing attributes (strings) to be returned
721721
* @return array|bool returns an array of hourly stats objects
722722
*/
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)
724724
{
725725
$end = empty($end) ? time() * 1000 : $end;
726726
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
727727
$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+
}
729733

730734
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/hourly.user', $payload);
731735
}
732736

733737
/**
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.
735739
*
736740
* @note - defaults to the past 7*24 hours
737741
* - 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
740744
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
741745
* section
742746
* @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
744748
* @param int|null $start optional, Unix timestamp in milliseconds
745749
* @param int|null $end optional, Unix timestamp in milliseconds
746750
* @param array|null $attribs array containing attributes (strings) to be returned
747751
* @return array|bool returns an array of daily stats objects
748752
*/
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)
750754
{
751755
$end = empty($end) ? time() * 1000 : $end;
752756
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
753757
$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+
}
755763

756764
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/daily.user', $payload);
757765
}
758766

759767
/**
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.
761769
*
762770
* @note - defaults to the past 13 weeks (52*7*24 hours)
763771
* - only supported with UniFi controller versions 5.8.X and higher
764772
* - make sure that the retention policy for monthly stats is set to the correct value in
765773
* the controller settings
766774
* - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance
767775
* 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
770777
* @param int|null $start optional, Unix timestamp in milliseconds
771778
* @param int|null $end optional, Unix timestamp in milliseconds
772779
* @param array|null $attribs array containing attributes (strings) to be returned
773780
* @return array|bool returns an array of monthly stats objects
781+
* @see stat_5minutes_user() for details on attribs
774782
*/
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)
776784
{
777785
$end = empty($end) ? time() * 1000 : $end;
778786
$start = empty($start) ? $end - (13 * 7 * 24 * 3600 * 1000) : $start;
779787
$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+
}
781793

782794
return $this->fetch_results('/api/s/' . $this->site . '/stat/report/monthly.user', $payload);
783795
}
@@ -3365,6 +3377,63 @@ public function set_element_adoption(bool $enable): bool
33653377
return $this->fetch_results_boolean('/api/s/' . $this->site . '/set/setting/element_adopt', $payload);
33663378
}
33673379

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+
33683437
/**
33693438
* List device states
33703439
*
@@ -3954,38 +4023,30 @@ protected function catch_json_last_error(): bool
39544023
return true;
39554024
case JSON_ERROR_DEPTH:
39564025
$error = 'The maximum stack depth has been exceeded';
3957-
39584026
break;
39594027
case JSON_ERROR_STATE_MISMATCH:
39604028
$error = 'Invalid or malformed JSON';
3961-
39624029
break;
39634030
case JSON_ERROR_CTRL_CHAR:
39644031
$error = 'Control character error, possibly incorrectly encoded';
3965-
39664032
break;
39674033
case JSON_ERROR_SYNTAX:
39684034
$error = 'Syntax error, malformed JSON';
3969-
39704035
break;
39714036
case JSON_ERROR_UTF8:
39724037
/** PHP >= 5.3.3 */
39734038
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
3974-
39754039
break;
39764040
case JSON_ERROR_RECURSION:
39774041
/** PHP >= 5.5.0 */
39784042
$error = 'One or more recursive references in the value to be encoded';
3979-
39804043
break;
39814044
case JSON_ERROR_INF_OR_NAN:
39824045
/** PHP >= 5.5.0 */
39834046
$error = 'One or more NAN or INF values in the value to be encoded';
3984-
39854047
break;
39864048
case JSON_ERROR_UNSUPPORTED_TYPE:
39874049
$error = 'A value of a type that cannot be encoded was given';
3988-
39894050
break;
39904051
}
39914052

@@ -3994,11 +4055,9 @@ protected function catch_json_last_error(): bool
39944055
switch (json_last_error()) {
39954056
case JSON_ERROR_INVALID_PROPERTY_NAME:
39964057
$error = 'A property name that cannot be encoded was given';
3997-
39984058
break;
39994059
case JSON_ERROR_UTF16:
40004060
$error = 'Malformed UTF-16 characters, possibly incorrectly encoded';
4001-
40024061
break;
40034062
}
40044063
}
@@ -4116,15 +4175,13 @@ protected function response_header_callback($ch, string $header_line): int
41164175
$this->cookies = $cookie_crumb;
41174176
$this->is_logged_in = true;
41184177
$this->is_unifi_os = false;
4119-
41204178
break;
41214179
}
41224180

41234181
if (strpos($cookie_crumb, 'TOKEN') !== false) {
41244182
$this->cookies = $cookie_crumb;
41254183
$this->is_logged_in = true;
41264184
$this->is_unifi_os = true;
4127-
41284185
break;
41294186
}
41304187
}

0 commit comments

Comments
 (0)