From 3fd8e69b4a356545c8d0a7db8e34f17e9140879e Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Fri, 13 Dec 2019 09:15:10 +0100 Subject: [PATCH] API client class v1.1.43 - updated code and instructions for `set_guestlogin_settings()`, thanks to @stoehrmark for reporting the issue - added list_connected_users.php example contributed by @gahujipo - added restart_device() which replaces restart_ap(), contributed by @leonardogyn - code styling cleanup across `Client.php` --- examples/ap_upgrade_firmware.php | 2 +- examples/list_connected_users.php | 37 ++++++++++++++ src/Client.php | 85 +++++++++++++++++++++---------- 3 files changed, 96 insertions(+), 28 deletions(-) create mode 100755 examples/list_connected_users.php diff --git a/examples/ap_upgrade_firmware.php b/examples/ap_upgrade_firmware.php index 78dacaf..9989460 100755 --- a/examples/ap_upgrade_firmware.php +++ b/examples/ap_upgrade_firmware.php @@ -3,7 +3,7 @@ * PHP API usage example * * contributed by: @4oo4 - * description: example script to check and upgrade device firmware (can be scheduled with systemd/cron) + * description: example script to upgrade device firmware (can be scheduled with systemd/cron) * to the most current version */ require_once('vendor/autoload.php'); diff --git a/examples/list_connected_users.php b/examples/list_connected_users.php new file mode 100755 index 0000000..0cc2c68 --- /dev/null +++ b/examples/list_connected_users.php @@ -0,0 +1,37 @@ +'; + +/** + * initialize the UniFi API connection class and log in to the controller and pull the requested data + */ +$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$clients_array = $unifi_connection->list_clients(); + +/** + * output the results in JSON format + */ +header('Content-Type: application/json; charset=utf-8'); +echo json_encode($clients_array); \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index c80419f..654bc90 100755 --- a/src/Client.php +++ b/src/Client.php @@ -537,9 +537,10 @@ class Client return false; } - $end = is_null($end) ? (time() * 1000) : intval($end); - $start = is_null($start) ? $end - (12 * 3600 * 1000) : intval($start); - $payload = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end]; + $end = is_null($end) ? (time() * 1000) : intval($end); + $start = is_null($start) ? $end - (12 * 3600 * 1000) : intval($start); + $attributes = ['bytes', 'num_sta', 'time']; + $payload = ['attrs' => $attributes, 'start' => $start, 'end' => $end]; if (!is_null($mac)) { $payload['mac'] = strtolower($mac); } @@ -567,9 +568,10 @@ class Client return false; } - $end = is_null($end) ? (time() * 1000) : intval($end); - $start = is_null($start) ? $end - (7 * 24 * 3600 * 1000) : intval($start); - $payload = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end]; + $end = is_null($end) ? (time() * 1000) : intval($end); + $start = is_null($start) ? $end - (7 * 24 * 3600 * 1000) : intval($start); + $attributes = ['bytes', 'num_sta', 'time']; + $payload = ['attrs' => $attributes, 'start' => $start, 'end' => $end]; if (!is_null($mac)) { $payload['mac'] = strtolower($mac); } @@ -597,9 +599,10 @@ class Client return false; } - $end = is_null($end) ? (time() * 1000) : intval($end); - $start = is_null($start) ? $end - (7 * 24 * 3600 * 1000) : intval($start); - $payload = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end]; + $end = is_null($end) ? (time() * 1000) : intval($end); + $start = is_null($start) ? $end - (7 * 24 * 3600 * 1000) : intval($start); + $attributes = ['bytes', 'num_sta', 'time']; + $payload = ['attrs' => $attributes, 'start' => $start, 'end' => $end]; if (!is_null($mac)) { $payload['mac'] = strtolower($mac); } @@ -2190,18 +2193,27 @@ class Client } /** - * Reboot an access point + * Reboot a device * ---------------------- * return true on success - * required parameter = device MAC address + * required parameter = device MAC address + * optional parameter = string; two options: 'soft' or 'hard', defaults to soft + * soft can be used for all devices, requests a plain restart of that device + * hard is special for PoE switches and besides the restart also requests a + * power cycle on all PoE capable ports. Keep in mind that a 'hard' reboot + * does *NOT* trigger a factory-reset, as it somehow could suggest. */ - public function restart_ap($mac) + public function restart_device($mac, $type = 'soft') { if (!$this->is_loggedin) { return false; } - $payload = ['cmd' => 'restart', 'mac' => strtolower($mac)]; + $payload = ['cmd' => 'restart', 'mac' => strtolower($mac)]; + if (!is_null($type) && in_array($type, ['soft', 'hard'])) { + $payload['type'] = strtolower($type); + } + $response = $this->exec_curl('/api/s/' . $this->site . '/cmd/devmgr', $payload); return $this->process_response_boolean($response); @@ -2356,9 +2368,6 @@ class Client * required parameter = string; WLAN type, can be either 'ng' (for WLANs 2G (11n/b/g)) or 'na' (WLANs 5G (11n/a/ac)) * required parameter = string; _id value of the access point to be modified * required parameter = string; _id value of the WLAN group to assign device to - * - * NOTES: - * - can for example be used to turn WiFi off */ public function set_ap_wlangroup($wlantype_id, $device_id, $wlangroup_id) { @@ -2370,7 +2379,11 @@ class Client return false; } - $payload = ['wlan_overrides' => [], 'wlangroup_id_' . $wlantype_id => $wlangroup_id]; + $payload = [ + 'wlan_overrides' => [], + 'wlangroup_id_' . $wlantype_id => $wlangroup_id + ]; + $response = $this->exec_curl('/api/s/' . $this->site . '/upd/device/' . trim($device_id), $payload); return $this->process_response_boolean($response); @@ -2380,14 +2393,15 @@ class Client * Update guest login settings * --------------------------- * return true on success - * required parameter - * required parameter - * required parameter - * required parameter - * required parameter - * required parameter - * required parameter - * required parameter + * required parameter = boolean; enable/disable the captive portal + * required parameter = boolean; enable/disable captive portal customizations + * required parameter = boolean; enable/disable captive portal redirect + * required parameter = string; url to redirect to, must include the http/https prefix, no trailing slashes + * required parameter = string; the captive portal (simple) password + * required parameter = numeric; number of units for the authorization expiry + * required parameter = numeric; number of minutes within a unit (a value 60 is required for hours) + * required parameter = 24 char string; value of _id for the site settings section where key = "guest_access", settings can be obtained + * using the list_settings() function * * NOTES: * - both portal parameters are set to the same value! @@ -2400,7 +2414,7 @@ class Client $x_password, $expire_number, $expire_unit, - $site_id + $section_id ) { if (!$this->is_loggedin) { return false; @@ -2414,7 +2428,7 @@ class Client 'x_password' => $x_password, 'expire_number' => $expire_number, 'expire_unit' => $expire_unit, - 'site_id' => $site_id + '_id' => $section_id ]; $response = $this->exec_curl('/api/s/' . $this->site . '/set/setting/guest_access', $payload); @@ -3355,6 +3369,23 @@ class Client return $this->site_leds(false); } + /** + * Reboot an access point + * ---------------------- + * return true on success + * required parameter = device MAC address + */ + public function restart_ap($mac) + { + trigger_error( + 'Function restart_ap() has been deprecated, use restart_device() instead.', + E_USER_DEPRECATED + ); + + return $this->restart_device($mac); + } + + /** * Custom API request * ------------------