diff --git a/README.md b/README.md index 1ff3387..641a5dd 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## UniFi controller API client class -A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser). +A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser). This class can now also be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects. @@ -49,6 +49,7 @@ The class currently supports the following functions/methods to get/post/put/del - list_guests() - list_health() - list_hotspotop() +- list_known_rogueaps() - list_networkconf() - list_portconf() - list_portforward_stats() @@ -65,11 +66,13 @@ The class currently supports the following functions/methods to get/post/put/del - list_wlan_groups() - list_wlanconf() - locate_ap() +- power_cycle_switch_port() - reconnect_sta() - rename_ap() - restart_ap() - revoke_voucher() - set_ap_radiosettings() +- set_device_settings_base() - set_guestlogin_settings() - set_locate_ap() (deprecated but still available as alias) - set_networksettings_base() @@ -98,6 +101,7 @@ The class currently supports the following functions/methods to get/post/put/del - stat_sessions() - stat_sites() - stat_sta_sessions_latest() +- stat_status() - stat_sysinfo() - stat_voucher() - unauthorize_guest() diff --git a/src/Client.php b/src/Client.php index 6f979f9..b8d7ec2 100755 --- a/src/Client.php +++ b/src/Client.php @@ -782,9 +782,9 @@ class Client } /** - * List rogue access points - * ------------------------ - * returns an array of known rogue access point objects + * List rogue/neighboring access points + * ------------------------------------ + * returns an array of rogue/neighboring access point objects * optional parameter = hours to go back to list discovered "rogue" access points (default = 24 hours) */ public function list_rogueaps($within = 24) @@ -795,6 +795,18 @@ class Client return $this->process_response($response); } + /** + * List known rogue access points + * ------------------------------ + * returns an array of known rogue access point objects + */ + public function list_known_rogueaps() + { + if (!$this->is_loggedin) return false; + $response = $this->exec_curl('/api/s/'.$this->site.'/stat/rogueknown'); + return $this->process_response($response); + } + /** * List sites * ---------- @@ -888,6 +900,17 @@ class Client return $this->process_response($response); } + /** + * List controller status + * ---------------------- + * returns an array containing general controller status info + */ + public function stat_status() + { + $response = $this->exec_curl('/status'); + return $this->process_response($response); + } + /** * List self * --------- @@ -1177,14 +1200,10 @@ class Client { if (!$this->is_loggedin) return false; $this->request_type = 'PUT'; - $override_mode_options = ['off', 'on', 'default']; - if (in_array($override_mode, $override_mode_options)) { - $json = json_encode(['led_override' => $override_mode]); - $response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json); - return $this->process_response_boolean($response); - } - - return false; + if (!in_array($override_mode, ['off', 'on', 'default'])) return false; + $json = json_encode(['led_override' => $override_mode]); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json); + return $this->process_response_boolean($response); } /** @@ -1253,14 +1272,10 @@ class Client */ public function set_ap_wlangroup($wlantype_id, $device_id, $wlangroup_id) { if (!$this->is_loggedin) return false; - $wlantype_id_options = ['ng', 'na']; - if (in_array($wlantype_id, $wlantype_id_options)) { - $json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]); - $response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json); - return $this->process_response_boolean($response); - } - - return false; + if (in_array($wlantype_id, ['ng', 'na'])) return false; + $json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]); + $response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json); + return $this->process_response_boolean($response); } /** @@ -1532,16 +1547,12 @@ class Client */ public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs) { - $mac_filter_policy_options = ['allow', 'deny']; - if (in_array($mac_filter_policy, $mac_filter_policy_options)) { - $payload = new \stdClass(); - $payload->mac_filter_enabled = (bool)$mac_filter_enabled; - $payload->mac_filter_policy = $mac_filter_policy; - $payload->mac_filter_list = $macs; - return $this->set_wlansettings_base($wlan_id, $payload); - } - - return false; + if (in_array($mac_filter_policy, ['allow', 'deny'])) return false; + $payload = new \stdClass(); + $payload->mac_filter_enabled = (bool)$mac_filter_enabled; + $payload->mac_filter_policy = $mac_filter_policy; + $payload->mac_filter_list = $macs; + return $this->set_wlansettings_base($wlan_id, $payload); } /** @@ -1625,6 +1636,25 @@ class Client return $this->process_response_boolean($response); } + /** + * Power-cycle the PoE output of a switch port + * ------------------------------------------- + * return true on success + * required parameter = string; main MAC address of the switch + * required parameter = integer; port number/index of the port to be affected + * + * NOTES: + * - only applies to switches and their PoE ports... + */ + public function power_cycle_switch_port($switch_mac, $port_idx) + { + if (!$this->is_loggedin) return false; + $json = ['mac' => $switch_mac, 'port_idx' => intval($port_idx), 'cmd' => 'power-cycle']; + $json = json_encode($json); + $response = $this->exec_curl('/api/s/'.$this->site.'/cmd/devmgr', 'json='.$json); + return $this->process_response_boolean($response); + } + /** * Trigger an RF scan by an AP * --------------------------- @@ -1653,6 +1683,23 @@ class Client return $this->process_response($response); } + /** + * Update device settings, base (using REST) + * ----------------------------------------- + * return true on success + * required parameter = 24 char string; _id of the device which can be found with the list_devices() function + * required parameter = stdClass object or associative array containing the configuration to apply to the device, must be a + * (partial) object/array structured in the same manner as is returned by list_devices() for the device. + */ + public function set_device_settings_base($device_id, $device_settings) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode($device_settings); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($device_id), 'json='.$json); + return $this->process_response_boolean($response); + } + /** * List Radius profiles (using REST) * -------------------------------------- @@ -1727,23 +1774,20 @@ class Client public function create_radius_account($name, $x_password, $tunnel_type, $tunnel_medium_type, $vlan = null) { if (!$this->is_loggedin) return false; - $tunnel_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13]; - $tunnel_medium_type_options = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; - if (in_array($tunnel_type, $tunnel_type_options) && in_array($tunnel_medium_type, $tunnel_medium_type_options)) { - $this->request_type = 'POST'; - $account_details = [ - 'name' => $name, - 'x_password' => $x_password, - 'tunnel_type' => (int) $tunnel_type, - 'tunnel_medium_type' => (int) $tunnel_medium_type - ]; - if (isset($vlan)) $account_details['vlan'] = (int) $vlan; - $json = json_encode($account_details); - $response = $this->exec_curl('/api/s/'.$this->site.'/rest/account', 'json='.$json); - return $this->process_response($response); - } - - return false; + $tunnel_types = [1,2,3,4,5,6,7,8,9,10,11,12,13]; + $tunnel_medium_types = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; + if (!in_array($tunnel_type, $tunnel_types) || !in_array($tunnel_medium_type, $tunnel_medium_types)) return false; + $this->request_type = 'POST'; + $account_details = [ + 'name' => $name, + 'x_password' => $x_password, + 'tunnel_type' => (int) $tunnel_type, + 'tunnel_medium_type' => (int) $tunnel_medium_type + ]; + if (isset($vlan)) $account_details['vlan'] = (int) $vlan; + $json = json_encode($account_details); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/account', 'json='.$json); + return $this->process_response($response); } /**