- applied changes from PR #258, contributed by @mvarian

This commit is contained in:
malle-pietje
2024-12-19 11:52:19 +01:00
parent 19dc76ddfb
commit a5ca53a5b8

View File

@@ -33,7 +33,7 @@ use UniFi_API\Exceptions\MethodDeprecatedException;
class Client
{
/** Constants. */
const CLASS_VERSION = '2.0.2';
const CLASS_VERSION = '2.0.3';
const CURL_METHODS_ALLOWED = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
const DEFAULT_CURL_METHOD = 'GET';
@@ -97,13 +97,13 @@ class Client
* @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'
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')) {
@@ -234,7 +234,7 @@ class Client
return $this->is_logged_in = true;
}
throw new LoginFailedException($http_code);
throw new LoginFailedException('HTTP response: ' . $http_code);
}
/**
@@ -306,7 +306,7 @@ class Client
* @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
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];
@@ -420,12 +420,12 @@ class Client
* @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
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];
@@ -499,7 +499,7 @@ class Client
* @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)
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;
@@ -524,7 +524,7 @@ class Client
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_hourly_site(int $start = null, int $end = null, array $attribs = null)
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;
@@ -546,7 +546,7 @@ class Client
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_daily_site(int $start = null, int $end = null, array $attribs = null)
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;
@@ -568,7 +568,7 @@ class Client
* @throws Exception
* @see stat_5minutes_site() for details on attribs
*/
public function stat_monthly_site(int $start = null, int $end = null, array $attribs = null)
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;
@@ -616,7 +616,7 @@ class Client
* @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)
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;
@@ -645,7 +645,7 @@ class Client
* @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)
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;
@@ -674,7 +674,7 @@ class Client
* @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)
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;
@@ -703,7 +703,7 @@ class Client
* @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)
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;
@@ -736,7 +736,7 @@ class Client
* @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)
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;
@@ -763,7 +763,7 @@ class Client
* @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)
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;
@@ -794,7 +794,7 @@ class Client
* @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)
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;
@@ -825,7 +825,7 @@ class Client
* @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)
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;
@@ -856,7 +856,7 @@ class Client
* @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)
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;
@@ -878,7 +878,7 @@ class Client
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_hourly_gateway(int $start = null, int $end = null, array $attribs = null)
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;
@@ -900,7 +900,7 @@ class Client
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_daily_gateway(int $start = null, int $end = null, array $attribs = null)
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;
@@ -922,7 +922,7 @@ class Client
* @throws Exception
* @see stat_5minutes_gateway() for details on attribs
*/
public function stat_monthly_gateway(int $start = null, int $end = null, array $attribs = null)
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;
@@ -942,7 +942,7 @@ class Client
* @return array|bool returns an array of speed test result objects
* @throws Exception
*/
public function stat_speedtest_results(int $start = null, int $end = null)
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;
@@ -963,7 +963,7 @@ class Client
* @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)
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;
@@ -982,12 +982,12 @@ class Client
* @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
* @param string|null $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')
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;
@@ -1014,7 +1014,7 @@ class Client
* @return array|bool returns an array of login session objects for all devices or a single device
* @throws Exception
*/
public function stat_sta_sessions_latest(string $mac, int $limit = null)
public function stat_sta_sessions_latest(string $mac, ?int $limit = null)
{
$limit = empty($limit) ? 5 : $limit;
$payload = ['mac' => strtolower($mac), '_limit' => $limit, '_sort' => '-assoc_time'];
@@ -1032,7 +1032,7 @@ class Client
* @return array|bool returns an array of authorization objects
* @throws Exception
*/
public function stat_auths(int $start = null, int $end = null)
public function stat_auths(?int $start = null, ?int $end = null)
{
$end = empty($end) ? time() : $end;
$start = empty($start) ? $end - (7 * 24 * 3600) : $start;
@@ -1082,7 +1082,7 @@ class Client
* single client device object, false upon error
* @throws Exception
*/
public function list_clients(string $mac = null)
public function list_clients(?string $mac = null)
{
if (is_string($mac)) {
$mac = strtolower(trim($mac));
@@ -1142,7 +1142,7 @@ class Client
* @return array|bool returns an array containing a single object with attributes of the updated client on success
* @throws Exception
*/
public function edit_client_fixedip(string $client_id, bool $use_fixedip, string $network_id = null, string $fixed_ip = null)
public function edit_client_fixedip(string $client_id, bool $use_fixedip, ?string $network_id = null, ?string $fixed_ip = null)
{
$this->curl_method = 'PUT';
@@ -1514,7 +1514,7 @@ class Client
* @return bool return true on success
* @throws Exception
*/
public function create_tag(string $name, array $macs = null): bool
public function create_tag(string $name, ?array $macs = null): bool
{
$payload = ['name' => $name];
@@ -2151,7 +2151,7 @@ class Client
* @return array|bool containing hotspot voucher objects
* @throws Exception
*/
public function stat_voucher(int $create_time = null)
public function stat_voucher(?int $create_time = null)
{
$payload = isset($create_time) ? ['create_time' => $create_time] : [];
@@ -2165,7 +2165,7 @@ class Client
* @return array|bool containing hotspot payments
* @throws Exception
*/
public function stat_payment(int $within = null)
public function stat_payment(?int $within = null)
{
$path_suffix = isset($within) ? '?within=' . $within : '';
@@ -2223,9 +2223,9 @@ class Client
int $count = 1,
int $quota = 0,
string $note = '',
int $up = null,
int $down = null,
int $megabytes = null
?int $up = null,
?int $down = null,
?int $megabytes = null
)
{
$payload = [
@@ -2313,7 +2313,7 @@ class Client
* @return array|bool containing filtered DPI stats
* @throws Exception
*/
public function list_dpi_stats_filtered(string $type = 'by_cat', array $cat_filter = null)
public function list_dpi_stats_filtered(string $type = 'by_cat', ?array $cat_filter = null)
{
if (!in_array($type, ['by_cat', 'by_app'])) {
return false;
@@ -2991,23 +2991,23 @@ class Client
* @throws Exception
*/
public function create_wlan(
string $name,
string $x_passphrase,
string $usergroup_id,
string $wlangroup_id,
bool $enabled = true,
bool $hide_ssid = false,
bool $is_guest = false,
string $security = 'open',
string $wpa_mode = 'wpa2',
string $wpa_enc = 'ccmp',
bool $vlan_enabled = null,
string $vlan_id = null,
bool $uapsd_enabled = false,
bool $schedule_enabled = false,
array $schedule = [],
array $ap_group_ids = null,
array $payload = []
string $name,
string $x_passphrase,
string $usergroup_id,
string $wlangroup_id,
bool $enabled = true,
bool $hide_ssid = false,
bool $is_guest = false,
string $security = 'open',
string $wpa_mode = 'wpa2',
string $wpa_enc = 'ccmp',
?bool $vlan_enabled = null,
?string $vlan_id = null,
bool $uapsd_enabled = false,
bool $schedule_enabled = false,
array $schedule = [],
?array $ap_group_ids = null,
array $payload = []
): bool
{
$payload = array_merge($payload, [
@@ -3183,7 +3183,7 @@ class Client
* @return array|bool containing the alarm count
* @throws Exception
*/
public function count_alarms(bool $archived = null)
public function count_alarms(?bool $archived = null)
{
$path_suffix = $archived === false ? '?archived=false' : null;
@@ -3457,11 +3457,11 @@ class Client
* @throws Exception
*/
public function create_radius_account(
string $name,
string $x_password,
int $tunnel_type = null,
int $tunnel_medium_type = null,
string $vlan = null
string $name,
string $x_password,
?int $tunnel_type = null,
?int $tunnel_medium_type = null,
?string $vlan = null
)
{
$tunnel_type_values = [null, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
@@ -3581,7 +3581,7 @@ class Client
* available.
* @throws Exception
*/
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 = [])
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 = [])
{
$end = empty($end) ? time() * 1000 : $end;
$start = empty($start) ? $end - (7 * 24 * 3600 * 1000) : $start;
@@ -4438,11 +4438,8 @@ class Client
curl_close($ch);
/** Login, again. */
$this->login();
/** When the re-login was successful, execute the same cURL request again. */
if ($this->is_logged_in) {
/** Re-login, and if successful, execute the same cURL request again. */
if ($this->login()) {
if ($this->debug) {
error_log(__FUNCTION__ . ': re-logged in, calling exec_curl again');
}