From 00fd5531ff4cbf5e1d354a87dc34243d7d8dc60e Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Sun, 19 Aug 2018 15:50:29 +0200 Subject: [PATCH] included API client class v1.1.31 added support for list_firewallgroups() implemented fix for site names with Ampersands not showing up correctly in the nav bar --- README.md | 5 +- composer.lock | 10 +- index.php | 9 +- vendor/art-of-wifi/unifi-api-client/README.md | 4 + .../unifi-api-client/examples/README.md | 2 + .../unifi-api-client/src/Client.php | 209 +++++++++++++----- vendor/composer/installed.json | 12 +- 7 files changed, 180 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 47e98a7..7e33e6e 100755 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ You will find examples and detailed instructions there. Please keep the following in mind: - the API Browser tool doesn't support all available data collections/API endpoints, see the list below of those that are currently supported -- currently, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.7.20 has been confirmed to work) +- currently, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.8.24 has been confirmed to work) - there is still work to be done to add/improve functionality and usability of this tool so suggestions/comments are welcome. Please use the github [issue](https://github.com/Art-of-WiFi/UniFi-API-browser/issues) list or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/UniFi-API-browser-tool-released/m-p/1392651) to share your ideas/questions. - please read the Security Notice below before installing this tool! @@ -68,11 +68,12 @@ The UniFi API browser tool offers the following features: - list admins for current site - sysinfo - self - - wlan config + - list wlan config - list VoIP extension - list network configuration - list port configurations - list port forwarding rules + - list firewall groups - list current channels - list DPI stats - dynamic DNS configuration diff --git a/composer.lock b/composer.lock index 6437b6d..2828d1a 100644 --- a/composer.lock +++ b/composer.lock @@ -9,16 +9,16 @@ "packages": [ { "name": "art-of-wifi/unifi-api-client", - "version": "v1.1.30", + "version": "v1.1.31", "source": { "type": "git", "url": "https://github.com/Art-of-WiFi/UniFi-API-client.git", - "reference": "871ab82608327b04a228629628545497cd2a872a" + "reference": "de407e6ac0607c920832b62ef45c240cdef80a5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Art-of-WiFi/UniFi-API-client/zipball/871ab82608327b04a228629628545497cd2a872a", - "reference": "871ab82608327b04a228629628545497cd2a872a", + "url": "https://api.github.com/repos/Art-of-WiFi/UniFi-API-client/zipball/de407e6ac0607c920832b62ef45c240cdef80a5c", + "reference": "de407e6ac0607c920832b62ef45c240cdef80a5c", "shasum": "" }, "require": { @@ -52,7 +52,7 @@ "ubiquiti", "unifi" ], - "time": "2018-07-02 13:06:31" + "time": "2018-08-19 10:13:53" }, { "name": "kint-php/kint", diff --git a/index.php b/index.php index 8d0b26f..291ab5d 100755 --- a/index.php +++ b/index.php @@ -17,7 +17,7 @@ * with this package in the file LICENSE.md * */ -define('API_BROWSER_VERSION', '1.0.28'); +define('API_BROWSER_VERSION', '1.0.29'); define('API_CLASS_VERSION', get_client_version()); /** @@ -395,6 +395,10 @@ $selection = 'list alarms'; $data = $unifidata->list_alarms(); break; + case 'list_firewallgroups': + $selection = 'list firewall groups'; + $data = $unifidata->list_firewallgroups(); + break; case 'count_alarms': $selection = 'count all alarms'; $data = $unifidata->count_alarms(); @@ -711,7 +715,7 @@ function get_client_version() usort($sites, "sites_sort"); foreach ($sites as $site) { - echo '
  • ' . $site->desc . '
  • ' . "\n"; + echo '
  • ' . $site->desc . '
  • ' . "\n"; } ?> @@ -840,6 +844,7 @@ function get_client_version()
  • list network configuration
  • list port configuration
  • list port forwarding rules
  • +
  • list firewall groups
  • dynamic DNS configuration
  • list country codes
  • diff --git a/vendor/art-of-wifi/unifi-api-client/README.md b/vendor/art-of-wifi/unifi-api-client/README.md index 9b29e0f..e3359f1 100755 --- a/vendor/art-of-wifi/unifi-api-client/README.md +++ b/vendor/art-of-wifi/unifi-api-client/README.md @@ -15,6 +15,7 @@ The class currently supports the following functions/methods to get/post/put/del - authorize_guest() - block_sta() - count_alarms() +- create_firewallgroup() - create_hotspotop() - create_network() - create_radius_account() @@ -24,12 +25,14 @@ The class currently supports the following functions/methods to get/post/put/del - create_voucher() - create_wlan() - delete_device() +- delete_firewallgroup() - delete_network() - delete_radius_account() - delete_site() - delete_usergroup() - delete_wlan() - disable_ap() +- edit_firewallgroup() - edit_usergroup() - extend_guest_validity() - forget_sta() (supported on controller version 5.9.X and higher) @@ -49,6 +52,7 @@ The class currently supports the following functions/methods to get/post/put/del - list_dynamicdns() - list_events() - list_extension() +- list_firewallgroups() - list_guests() - list_health() - list_hotspotop() diff --git a/vendor/art-of-wifi/unifi-api-client/examples/README.md b/vendor/art-of-wifi/unifi-api-client/examples/README.md index 319e47b..28db31d 100755 --- a/vendor/art-of-wifi/unifi-api-client/examples/README.md +++ b/vendor/art-of-wifi/unifi-api-client/examples/README.md @@ -6,6 +6,8 @@ This directory contains some PHP code examples which demonstrate usage of the PH Copy the appropriate example file to your working directory together with a copy of the config.template.php file which should be renamed to config.php. Then update the contents of your new config.php with your controller details and credentials and modify the example file as required to fit your needs. +Also make sure to update the path for the composer autoloader file (`vendor/autoload.php`) or the file containing the Class itself (`src/Client.php`) in your `require_once()` statement as required. + ### Contribute If you would like to share your own example file(s), please open an issue and include your code there or else create a pull request. diff --git a/vendor/art-of-wifi/unifi-api-client/src/Client.php b/vendor/art-of-wifi/unifi-api-client/src/Client.php index 7cf3777..e108638 100755 --- a/vendor/art-of-wifi/unifi-api-client/src/Client.php +++ b/vendor/art-of-wifi/unifi-api-client/src/Client.php @@ -25,7 +25,7 @@ class Client */ protected $baseurl = 'https://127.0.0.1:8443'; protected $site = 'default'; - protected $version = '5.4.16'; + protected $version = '5.6.39'; protected $debug = false; protected $is_loggedin = false; private $cookies = ''; @@ -441,8 +441,8 @@ public function set_sta_name($user_id, $name = null) * 5 minutes site stats method * --------------------------- * returns an array of 5-minute stats objects for the current site - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * * NOTES: * - defaults to the past 12 hours @@ -465,8 +465,8 @@ public function stat_5minutes_site($start = null, $end = null) * Hourly site stats method * ------------------------ * returns an array of hourly stats objects for the current site - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * * NOTES: * - defaults to the past 7*24 hours @@ -487,8 +487,8 @@ public function stat_hourly_site($start = null, $end = null) * Daily site stats method * ------------------------ * returns an array of daily stats objects for the current site - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * * NOTES: * - defaults to the past 52*7*24 hours @@ -509,8 +509,8 @@ public function stat_daily_site($start = null, $end = null) * 5 minutes stats method for a single access point or all access points * --------------------------------------------------------------------- * returns an array of 5-minute stats objects - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = AP MAC address to return stats for * * NOTES: @@ -535,8 +535,8 @@ public function stat_5minutes_aps($start = null, $end = null, $mac = null) * Hourly stats method for a single access point or all access points * ------------------------------------------------------------------ * returns an array of hourly stats objects - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = AP MAC address to return stats for * * NOTES: @@ -559,8 +559,8 @@ public function stat_hourly_aps($start = null, $end = null, $mac = null) * Daily stats method for a single access point or all access points * ----------------------------------------------------------------- * returns an array of daily stats objects - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = AP MAC address to return stats for * * NOTES: @@ -584,8 +584,8 @@ public function stat_daily_aps($start = null, $end = null, $mac = null) * ------------------------------------------------------ * returns an array of 5-minute stats objects * required parameter = MAC address of user/client device to return stats for - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = array containing attributes (strings) to be returned, valid values are: * rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets, tx_packets * default is ['rx_bytes', 'tx_bytes'] @@ -614,8 +614,8 @@ public function stat_5minutes_user($mac, $start = null, $end = null, $attribs = * ----------------------------------------------------- * returns an array of hourly stats objects * required parameter = MAC address of user/client device to return stats for - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = array containing attributes (strings) to be returned, valid values are: * rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets, tx_packets * default is ['rx_bytes', 'tx_bytes'] @@ -642,8 +642,8 @@ public function stat_hourly_user($mac, $start = null, $end = null, $attribs = nu * -------------------------------------------------- * returns an array of daily stats objects * required parameter = MAC address of user/client device to return stats for - * optional parameter = Unix timestamp in seconds - * optional parameter = Unix timestamp in seconds + * optional parameter = Unix timestamp in milliseconds + * optional parameter = Unix timestamp in milliseconds * optional parameter = array containing attributes (strings) to be returned, valid values are: * rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets, tx_packets * default is ['rx_bytes', 'tx_bytes'] @@ -784,18 +784,6 @@ public function stat_client($client_mac) return $this->process_response($response); } - /** - * List user groups - * ---------------- - * returns an array of user group objects - */ - public function list_usergroups() - { - if (!$this->is_loggedin) return false; - $response = $this->exec_curl('/api/s/'.$this->site.'/list/usergroup'); - return $this->process_response($response); - } - /** * Assign client device to another group * ------------------------------------- @@ -812,22 +800,39 @@ public function set_usergroup($user_id, $group_id) } /** - * Update user group (using REST) + * Update client fixedip (using REST) * ------------------------------ - * returns an array containing a single object with attributes of the updated usergroup on success - * required parameter = id of the user group - * required parameter = id of the site - * required parameter = name of the user group - * optional parameter = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited) - * optional parameter = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited) + * returns an array containing a single object with attributes of the updated client on success + * required parameter = id of the client + * required parameter = boolean defining whether if use_fixedip is true or false + * optional parameter = network id where the ip belongs to + * optional parameter = value of client's fixed_ip field * */ - public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1, $group_up = -1) + public function edit_client_fixedip($client_id, $use_fixedip, $network_id = null, $fixed_ip = null) { if (!$this->is_loggedin) return false; + if (!is_bool($use_fixedip)) return false; $this->request_type = 'PUT'; - $json = json_encode(['_id' => $group_id, 'name' => $group_name, 'qos_rate_max_down' => intval($group_dn), 'qos_rate_max_up' => intval($group_up), 'site_id' => $site_id]); - $response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup/'.trim($group_id), $json); + $data = ['_id' => $client_id, 'use_fixedip' => $use_fixedip]; + if($use_fixedip){ + if($network_id){ $data["network_id"] = $network_id; } + if($fixed_ip){ $data["fixed_ip"] = $fixed_ip; } + } + $json = json_encode($data); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/user/'.trim($client_id), $json); + return $this->process_response($response); + } + + /** + * List user groups + * ---------------- + * returns an array of user group objects + */ + public function list_usergroups() + { + if (!$this->is_loggedin) return false; + $response = $this->exec_curl('/api/s/'.$this->site.'/list/usergroup'); return $this->process_response($response); } @@ -842,11 +847,31 @@ public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1, public function create_usergroup($group_name, $group_dn = -1, $group_up = -1) { if (!$this->is_loggedin) return false; + $this->request_type = 'POST'; $json = json_encode(['name' => $group_name, 'qos_rate_max_down' => intval($group_dn), 'qos_rate_max_up' => intval($group_up)]); $response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup', $json); return $this->process_response($response); } + /** + * Modify user group (using REST) + * ------------------------------ + * returns an array containing a single object with attributes of the updated usergroup on success + * required parameter = id of the user group + * required parameter = id of the site + * required parameter = name of the user group + * optional parameter = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited) + * optional parameter = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited) + */ + public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1, $group_up = -1) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'PUT'; + $json = json_encode(['_id' => $group_id, 'name' => $group_name, 'qos_rate_max_down' => intval($group_dn), 'qos_rate_max_up' => intval($group_up), 'site_id' => $site_id]); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/usergroup/'.trim($group_id), $json); + return $this->process_response($response); + } + /** * Delete user group (using REST) * ------------------------------ @@ -861,6 +886,75 @@ public function delete_usergroup($group_id) return $this->process_response_boolean($response); } + /** + * List firewall groups (using REST) + * ---------------------------------- + * returns an array containing the current firewall groups on success + */ + public function list_firewallgroups() + { + if (!$this->is_loggedin) return false; + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/firewallgroup'); + return $this->process_response($response); + } + + /** + * Create firewall group (using REST) + * ---------------------------------- + * returns an array containing a single object with attributes of the new firewall group on success + * required parameter = name to assign to the firewall group + * required parameter = firewall group type; valid values are address-group, ipv6-address-group, port-group + * optional parameter = array containing the members of the new group (IPv4 addresses, IPv6 addresses or port numbers) + * (default is an empty array) + */ + public function create_firewallgroup($group_name, $group_type, $group_members = []) + { + if (!$this->is_loggedin) return false; + if (!in_array($group_type, ['address-group', 'ipv6-address-group', 'port-group'])) return false; + $this->request_type = 'POST'; + $json = json_encode(['name' => $group_name, 'group_type' => $group_type, 'group_members' => $group_members]); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/firewallgroup', $json); + return $this->process_response($response); + } + + /** + * Modify firewall group (using REST) + * ---------------------------------- + * returns an array containing a single object with attributes of the updated firewall group on success + * required parameter = _id value of the firewall group + * required parameter = site_id value of the firewall group + * required parameter = name of the firewall group + * required parameter = firewall group type; valid values are address-group, ipv6-address-group, port-group, + * group_type cannot be changed for an existing firewall group! + * optional parameter = array containing the members of the group (IPv4 addresses, IPv6 addresses or port numbers) + * which will overwrite the existing group_members (default is an empty array) + * + * + */ + public function edit_firewallgroup($group_id, $site_id, $group_name, $group_type, $group_members = []) + { + if (!$this->is_loggedin) return false; + if (!in_array($group_type, ['address-group', 'ipv6-address-group', 'port-group'])) return false; + $this->request_type = 'PUT'; + $json = json_encode(['_id' => $group_id, 'name' => $group_name, 'group_type' => $group_type, 'group_members' => $group_members, 'site_id' => $site_id]); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/firewallgroup/'.trim($group_id), $json); + return $this->process_response($response); + } + + /** + * Delete firewall group (using REST) + * ---------------------------------- + * returns true on success + * required parameter = id of the firewall group + */ + public function delete_firewallgroup($group_id) + { + if (!$this->is_loggedin) return false; + $this->request_type = 'DELETE'; + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/firewallgroup/'.trim($group_id)); + return $this->process_response_boolean($response); + } + /** * List health metrics * ------------------- @@ -1330,6 +1424,7 @@ public function stat_payment($within = null) public function create_hotspotop($name, $x_password, $note = null) { if (!$this->is_loggedin) return false; + $this->request_type = 'POST'; $json = ['name' => $name, 'x_password' => $x_password]; if (isset($note)) $json['note'] = trim($note); $json = json_encode($json); @@ -1562,8 +1657,9 @@ public function restart_ap($mac) public function disable_ap($ap_id, $disable) { if (!$this->is_loggedin) return false; + if (!is_bool($disable)) return false; $this->request_type = 'PUT'; - $json = json_encode(['disabled' => (bool)$disable]); + $json = json_encode(['disabled' => $disable]); $response = $this->exec_curl('/api/s/'.$this->site.'/rest/device/'.trim($ap_id), $json); return $this->process_response_boolean($response); } @@ -1603,6 +1699,7 @@ public function led_override($device_id, $override_mode) public function locate_ap($mac, $enable) { if (!$this->is_loggedin) return false; + if (!is_bool($enable)) return false; $mac = strtolower($mac); $cmd = (($enable) ? 'set-locate' : 'unset-locate'); $json = json_encode(['cmd' => $cmd, 'mac' => $mac]); @@ -1619,7 +1716,8 @@ public function locate_ap($mac, $enable) public function site_leds($enable) { if (!$this->is_loggedin) return false; - $json = json_encode(['led_enabled' => (bool)$enable]); + if (!is_bool($enable)) return false; + $json = json_encode(['led_enabled' => $enable]); $response = $this->exec_curl('/api/s/'.$this->site.'/set/setting/mgmt', 'json='.$json); return $this->process_response_boolean($response); } @@ -1769,11 +1867,12 @@ public function delete_device($mac) * List network settings (using REST) * ---------------------------------- * returns an array of (non-wireless) networks and their settings + * optional parameter = string; network id to get specific network data for */ - public function list_networkconf() + public function list_networkconf($network_id = '') { if (!$this->is_loggedin) return false; - $response = $this->exec_curl('/api/s/'.$this->site.'/rest/networkconf'); + $response = $this->exec_curl('/api/s/'.$this->site.'/rest/networkconf/'.trim($network_id)); return $this->process_response($response); } @@ -1929,9 +2028,9 @@ public function set_wlansettings_base($wlan_id, $wlan_settings) */ public function set_wlansettings($wlan_id, $x_passphrase, $name = null) { - $payload = (object)[]; - if (!is_null($x_passphrase)) $payload->x_passphrase = trim($x_passphrase); - if (!is_null($name)) $payload->name = trim($name); + $payload = []; + if (!is_null($x_passphrase)) $payload['x_passphrase'] = trim($x_passphrase); + if (!is_null($name)) $payload['name'] = trim($name); return $this->set_wlansettings_base($wlan_id, $payload); } @@ -1944,9 +2043,9 @@ public function set_wlansettings($wlan_id, $x_passphrase, $name = null) */ public function disable_wlan($wlan_id, $disable) { - $payload = (object)[]; - $action = ($disable) ? false : true; - $payload->enabled = (bool)$action; + if (!is_bool($disable)) return false; + $action = $disable ? false : true; + $payload = ['enabled' => $action]; return $this->set_wlansettings_base($wlan_id, $payload); } @@ -1977,11 +2076,9 @@ public function delete_wlan($wlan_id) */ public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs) { + if (!is_bool($mac_filter_enabled)) return false; if (!in_array($mac_filter_policy, ['allow', 'deny'])) return false; - $payload = (object)[]; - $payload->mac_filter_enabled = (bool)$mac_filter_enabled; - $payload->mac_filter_policy = $mac_filter_policy; - $payload->mac_filter_list = $macs; + $payload = ['mac_filter_enabled' => (bool)$mac_filter_enabled, 'mac_filter_policy' => $mac_filter_policy, 'mac_filter_list' => $macs]; return $this->set_wlansettings_base($wlan_id, $payload); } @@ -2661,4 +2758,4 @@ private function get_curl_obj() return $ch; } -} +} \ No newline at end of file diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 228f77e..e2708ec 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -61,24 +61,24 @@ }, { "name": "art-of-wifi/unifi-api-client", - "version": "v1.1.30", - "version_normalized": "1.1.30.0", + "version": "v1.1.31", + "version_normalized": "1.1.31.0", "source": { "type": "git", "url": "https://github.com/Art-of-WiFi/UniFi-API-client.git", - "reference": "871ab82608327b04a228629628545497cd2a872a" + "reference": "de407e6ac0607c920832b62ef45c240cdef80a5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Art-of-WiFi/UniFi-API-client/zipball/871ab82608327b04a228629628545497cd2a872a", - "reference": "871ab82608327b04a228629628545497cd2a872a", + "url": "https://api.github.com/repos/Art-of-WiFi/UniFi-API-client/zipball/de407e6ac0607c920832b62ef45c240cdef80a5c", + "reference": "de407e6ac0607c920832b62ef45c240cdef80a5c", "shasum": "" }, "require": { "ext-curl": "*", "php": ">=5.4.0" }, - "time": "2018-07-02 13:06:31", + "time": "2018-08-19 10:13:53", "type": "library", "installation-source": "dist", "autoload": {