Compare commits

...

23 Commits

Author SHA1 Message Date
malle-pietje
8df2fcc522 Merge branch 'master' of github.com:Art-of-WiFi/UniFi-API-client 2017-09-08 12:37:17 +02:00
malle-pietje
eba7819c17 various changes and additions:
IMPORTANT CHANGE: renamed all functions/methods named add_*() to create_*()
improved data validation
modified several existing methods/functions to leverage REST endpoints
added create_network() method/function
added set_networksettings_base() method/function
added delete_network() method/function
added list_radius_profiles() method/function
added create_radius_account() method/function
added set_radius_account_base() method/function
added delete_radius_account() method/function
2017-09-08 12:36:12 +02:00
malle-pietje
462d8a81a1 Update README.md 2017-09-07 17:46:22 +02:00
malle-pietje
0373c8369f updated README 2017-09-07 12:48:14 +02:00
malle-pietje
cf41e75269 updated README 2017-09-07 12:36:12 +02:00
malle-pietje
e235267a8a updated README 2017-09-07 12:34:31 +02:00
malle-pietje
9896f6d85f updated README 2017-09-07 12:33:56 +02:00
malle-pietje
4f84cfc550 updated README 2017-09-07 12:33:09 +02:00
malle-pietje
efa6704c97 minor changes to examples and README for consistency 2017-09-07 12:31:47 +02:00
malle-pietje
5c893c1237 updated README 2017-09-07 12:29:13 +02:00
malle-pietje
ab38e6f7ad updated README 2017-09-07 12:27:20 +02:00
malle-pietje
0ac6653ec0 updated README 2017-09-06 17:16:32 +02:00
malle-pietje
34c0acf813 various improvements and additions:
- changed all public properties to private properties
- added getter/setter for site property
- modified the constructor to be more robust
- added check for existance of curl module
- added method/function set_wlansettings_base() which uses REST and is now leveraged by several other methods/functions
- added method/function set_wlan_mac_filter()
2017-09-06 17:12:17 +02:00
malle-pietje
b9067cd2b0 updated README 2017-09-05 17:56:14 +02:00
malle-pietje
3a23ad15c1 updated README 2017-09-05 17:52:20 +02:00
malle-pietje
9f79d825c4 updated README 2017-09-05 17:22:06 +02:00
malle-pietje
8155b81fed updated README 2017-09-05 16:18:03 +02:00
malle-pietje
edf91b19da updated README 2017-09-05 16:13:13 +02:00
Erik Slooff
15c9112894 updated README 2017-09-05 15:00:32 +02:00
Erik Slooff
0ec9d8735d updated README 2017-09-05 14:59:45 +02:00
Erik Slooff
3819cc8986 updated README 2017-09-05 14:46:51 +02:00
Erik Slooff
cd24ef1c2c updated examples to use the composer autoloader 2017-09-05 13:04:11 +02:00
Erik Slooff
e4d368cc72 fixed typo in composer.json 2017-09-05 12:58:51 +02:00
13 changed files with 532 additions and 172 deletions

172
README.md
View File

@@ -1,53 +1,46 @@
## UniFi controller API client class
This PHP class provides access to the **UniFi Controller API** and is based off the work of @domwo and @fbagnol and the API shell client as published by UBNT.
Please refer to the code samples in the `examples` directory for a starting point for your own PHP code.
This PHP class provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.5.20 has been confirmed to work) are supported. It is an independent version of the class which is used in the API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
### Donations
If you'd like to support further development of this PHP API client class, please use the donate button below. All donations go to the project maintainer.
[![Donate](https://www.paypalobjects.com/en_GB/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
If you'd like to support further development of this PHP API client class, please use the PayPal donate button below. All donations go to the project maintainer.
### Install
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
Simply execute this command from your project directory:
```
composer require art-of-wifi/unifi-api-client
```
### Requirements
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- network connectivity between this web server and the server and port (normally port 8443) where the UniFi controller is running
### Methods and functions supported
## Methods and functions supported
This class currently supports the following functions/methods to get/set data through the UniFi controller API:
- login()
- logout()
- add_site()
- adopt_device()
- authorize_guest()
- unauthorize_guest()
- block_sta()
- unblock_sta()
- count_alarms()
- create_hotspotop()
- create_network()
- create_radius_account()
- create_site()
- create_usergroup()
- create_voucher()
- create_wlan()
- delete_network()
- delete_radius_account()
- delete_site()
- delete_usergroup()
- delete_wlan()
- disable_ap()
- edit_usergroup()
- extend_guest_validity()
- led_override()
- list_admins()
- list_alarms()
- count_alarms()
- upgrade_device()
- upgrade_device_external()
- spectrum_scan()
- spectrum_scan_state()
- list_devices()
- list_aps() (deprecated but still available as alias)
- list_clients()
- list_current_channels()
- list_dashboard()
- list_devices()
- list_dpi_stats()
- list_dynamicdns()
- list_events()
- list_extension()
@@ -59,6 +52,7 @@ This class currently supports the following functions/methods to get/set data th
- list_portforward_stats()
- list_portforwarding()
- list_radius_accounts() (supported on controller version 5.5.19 and higher)
- list_radius_profiles()
- list_rogueaps()
- list_self()
- list_settings()
@@ -68,38 +62,32 @@ This class currently supports the following functions/methods to get/set data th
- list_users()
- list_wlan_groups()
- list_wlanconf()
- list_current_channels()
- list_dpi_stats()
- locate_ap()
- reconnect_sta()
- rename_ap()
- restart_ap()
- revoke_voucher()
- extend_guest_validity()
- set_ap_radiosettings()
- set_guestlogin_settings()
- locate_ap()
- set_locate_ap() (deprecated but still available as alias)
- unset_locate_ap() (deprecated but still available as alias)
- set_networksettings_base()
- set_radius_account_base()
- set_sta_name()
- set_sta_note()
- set_usergroup()
- edit_usergroup()
- add_usergroup()
- delete_usergroup()
- edit_usergroup()
- add_usergroup()
- delete_usergroup()
- set_wlan_mac_filter()
- set_wlansettings()
- create_wlan()
- delete_wlan()
- set_wlansettings_base()
- site_leds()
- site_ledsoff() (deprecated but still available as alias)
- site_ledson() (deprecated but still available as alias)
- spectrum_scan()
- spectrum_scan_state()
- stat_allusers()
- stat_auths()
- stat_client()
- stat_daily_site()
- stat_daily_aps()
- stat_daily_site()
- stat_hourly_aps()
- stat_hourly_site()
- stat_payment()
@@ -108,42 +96,114 @@ This class currently supports the following functions/methods to get/set data th
- stat_sta_sessions_latest()
- stat_sysinfo()
- stat_voucher()
- unauthorize_guest()
- unblock_sta()
- unset_locate_ap() (deprecated but still available as alias)
- upgrade_device()
- upgrade_device_external()
Internal functions:
Internal functions, getters/setters:
- set_debug()
- set_site()
- set_site()
- get_site()
- get_cookie() (renamed from getcookie())
- get_last_results_raw()
- get_last_error_message()
Please refer to the source code for more details on each function/method and it's parameters.
### Example usage
## Credits
This class is largely based on the work done by the following developers:
- domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
- fbagnol: https://github.com/fbagnol/class.unifi.php
- and the API as published by Ubiquiti: https://www.ubnt.com/downloads/unifi/5.5.20/unifi_sh_api
## Requirements
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- network connectivity between this web server and the server and port (normally port 8443) where the UniFi controller is running
## Installation ##
You can use **Composer**, **Git** or simply **Download the Release**
### Composer
The preferred method is via [composer](https://getcomposer.org). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed.
Once composer is installed, simply execute this command from your project directory:
```sh
composer require art-of-wifi/unifi-api-client
```
Finally, be sure to include the autoloader in your code:
```php
require_once('vendor/autoload.php');
```
### Git
Execute the following `git` command from the shell in your project directory:
```sh
git clone https://github.com/Art-of-WiFi/UniFi-API-client.git
```
When git is done cloning, include the file containing the class like so in your code:
```php
require_once('path/to/src/Client.php');
```
### Download the Release
If you prefer not to use composer or git, you can simply [download the package](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip), uncompress the zip file, then include the file containing the class in your code like so:
```php
require_once('path/to/src/Client.php');
```
## Example usage
A basic example how to use the class:
```php
/**
* load the class using the composer autoloader
*/
require_once('vendor/autoload.php');
...
/**
* initialize the Unifi API connection class, log in to the controller and request the alarms collection
* (this example assumes you have already assigned the correct values to the variables used)
*/
$unifi_connection = new UniFi_API\Client($controller_user, $controller_password, $controller_url, $site_id, $controller_version);
$login = $unifi_connection->login();
$results = $unifi_connection->list_alarms(); // returns the alarms in a PHP array
...
$results = $unifi_connection->list_alarms(); // returns an PHP array containing alarm objects
```
>**NOTE:**
>
>$site_id is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi controller:
>
>```
>https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard
>```
>
>Here `jl3z2shm` is the value required for $site_id.
Please refer to the `examples/` directory for some more detailed examples which you can use as a starting point for your own PHP code.
Have a look at the files in the `examples` directory for more examples how to use this class.
### NOTE:
In the example above, `$site_id` is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi controller:
`https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard`
In this case, `jl3z2shm` is the value required for $site_id.
## Need help or have suggestions?
There is still work to be done to add functionality and improve the usability of this class, so all suggestions/comments are welcome. Please use the github [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues) list or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/PHP-class-to-access-the-UniFi-controller-API-updates-and/td-p/1512870) to share your ideas.
## Contribute
If you would like to contribute code (improvements), please open an issue and include your code there or else create a pull request.
## Important Disclaimer
Many of these functions are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi controller API.

View File

@@ -25,7 +25,7 @@
},
"autoload": {
"psr-4": {
"UniFi-API\\": "src/"
"UniFi_API\\": "src/"
}
}
}

View File

@@ -6,6 +6,11 @@
* description: example basic PHP script to fetch an Access Point's scanning state/results
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -6,6 +6,11 @@
* description: example basic PHP script to perform a basic auth of a guest device
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -6,6 +6,11 @@
* description: example basic PHP script to auth a guest device and attach a note to it
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -6,6 +6,11 @@
* description: example basic PHP script to create a set of vouchers
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -6,6 +6,11 @@
* description: example of how to extend validity of guest authorizations
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -6,6 +6,11 @@
* description: example basic PHP script to pull current alarms from the UniFi controller and output in json format
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -7,6 +7,11 @@
* in raw HTML format
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc there if not already present)
* see the config.template.php file for an example

View File

@@ -7,6 +7,11 @@
* in json format
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -7,6 +7,11 @@
* them in basic HTML format
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -7,6 +7,11 @@
* output the response in json format
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example

View File

@@ -2,35 +2,33 @@
/**
* This file is part of the art-of-wifi/unifi-api-client package
*
* This UniFi API client is based on the work done by
* the following developers:
* This UniFi API client is based on the work done by the following developers:
* domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
* fbagnol: https://github.com/fbagnol/class.unifi.php
* and the API as published by Ubiquiti:
* https://www.ubnt.com/downloads/unifi/5.3.8/unifi_sh_api
* https://www.ubnt.com/downloads/unifi/<UniFi controller version number>/unifi_sh_api
*
* Copyright (c) 2017, Art of WiFi <info@artofwifi.net>
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.md
*
*/
namespace UniFi_API;
/**
* the UniFi API client class
*/
class Client
{
/**
* public properties
*/
public $user = '';
public $password = '';
public $site = 'default';
public $baseurl = 'https://127.0.0.1:8443';
public $version = '5.4.16';
/**
* private properties
*/
private $user;
private $password;
private $baseurl = 'https://127.0.0.1:8443';
private $site = 'default';
private $version = '5.4.16';
private $debug = false;
private $is_loggedin = false;
private $cookies = '';
@@ -38,16 +36,32 @@ class Client
private $last_results_raw;
private $last_error_message;
function __construct($user = '', $password = '', $baseurl = '', $site = '', $version = '')
function __construct($user, $password, $baseurl = '', $site = '', $version = '')
{
if (!empty($user)) $this->user = trim($user);
if (!empty($password)) $this->password = trim($password);
if (!extension_loaded('curl')) {
trigger_error('The PHP curl extension is not loaded. Please correct this before proceeding!');
}
$this->user = trim($user);
$this->password = trim($password);
if (!empty($baseurl)) $this->baseurl = trim($baseurl);
if (!empty($site)) $this->site = trim($site);
if (!empty($version)) $this->version = trim($version);
if (isset($_SESSION['unificookie'])) {
$this->cookies = $_SESSION['unificookie'];
}
$base_url_components = parse_url($this->baseurl);
if (empty($base_url_components['scheme']) || empty($base_url_components['host']) || empty($base_url_components['port'])) {
trigger_error('The URL provided is incomplete!');
}
if (strlen($this->site) !== 8 && $this->site !== 'default' && $this->debug) {
error_log('The provided short site name is probably incorrect');
}
}
function __destruct()
@@ -60,9 +74,7 @@ class Client
/**
* logout, if needed
*/
if ($this->is_loggedin) {
$this->logout();
}
if ($this->is_loggedin) $this->logout();
}
/**
@@ -143,6 +155,38 @@ class Client
* setter/getter functions from here:
****************************************************************/
/**
* Set site
* --------
* modify the private property site, returns the new (short) site name
* returns the new short name, or false if string length is incorrect or not 'default'
* required parameter <site> = string; must be the short site name of a site to which the
* provided credentials have access
*
* NOTE:
* this method can be useful when switching between sites
*/
public function set_site($site)
{
if (strlen($site) === 8 || $site === 'default') {
$this->site = $site;
return $this->site;
} else {
return false;
}
}
/**
* Get site
* --------
* get the value of private property site, returns the current (short) site name
*/
public function get_site()
{
return $this->site;
}
/**
* Set debug mode
* --------------
@@ -151,10 +195,12 @@ class Client
*/
public function set_debug($enable)
{
if ($enable) {
if ($enable === true) {
$this->debug = true;
return true;
} elseif ($enable === false) {
$this->debug = false;
return true;
}
return false;
@@ -201,7 +247,7 @@ class Client
* --------------------------------
* returns the UniFi controller cookie
*/
public function getcookie()
public function get_cookie()
{
if (!$this->is_loggedin) return false;
return $this->cookies;
@@ -574,8 +620,8 @@ class Client
}
/**
* Edit user group
* ---------------
* Update user group (using REST)
* ------------------------------
* returns an array containing a single object with attributes of the updated usergroup on success
* required parameter <group_id> = id of the user group
* required parameter <site_id> = id of the site
@@ -594,14 +640,14 @@ class Client
}
/**
* Add user group
* --------------
* Create user group (using REST)
* ---------------------------
* returns an array containing a single object with attributes of the new usergroup ("_id", "name", "qos_rate_max_down", "qos_rate_max_up", "site_id") on success
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*/
public function add_usergroup($group_name, $group_dn = -1, $group_up = -1)
public function create_usergroup($group_name, $group_dn = -1, $group_up = -1)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['name' => $group_name, 'qos_rate_max_down' => $group_dn, 'qos_rate_max_up' => $group_up]);
@@ -610,8 +656,8 @@ class Client
}
/**
* Delete user group
* -----------------
* Delete user group (using REST)
* ------------------------------
* returns true on success
* required parameter <group_id> = id of the user group
*/
@@ -673,8 +719,8 @@ class Client
}
/**
* List (device) tags
* ------------------
* List (device) tags (using REST)
* -------------------------------
* returns an array of known device tag objects
*
* NOTES: this endpoint was introduced with controller versions 5.5.X
@@ -727,14 +773,14 @@ class Client
}
/**
* Add a site
* ----------
* Create a site
* -------------
* returns an array containing a single object with attributes of the new site ("_id", "desc", "name") on success
* required parameter <description> = the long name for the new site
*
* NOTES: immediately after being added, the new site will be available in the output of the "list_sites" function
*/
public function add_site($description)
public function create_site($description)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['desc' => $description, 'cmd' => 'add-site']);
@@ -805,18 +851,6 @@ class Client
return $this->process_response($content_decoded);
}
/**
* List networkconf
* ----------------
* returns an array of network configuration data
*/
public function list_networkconf()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/list/networkconf'));
return $this->process_response($content_decoded);
}
/**
* List vouchers
* -------------
@@ -854,8 +888,8 @@ class Client
}
/**
* Create hotspot operator
* -----------------------
* Create hotspot operator (using REST)
* ------------------------------------
* return true upon success
* required parameter <name> = name for the hotspot operator
* required parameter <x_password> = clear text password for the hotspot operator
@@ -876,14 +910,14 @@ class Client
}
/**
* List hotspot operators
* ----------------------
* List hotspot operators (using REST)
* -----------------------------------
* returns an array of hotspot operators
*/
public function list_hotspotop()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/list/hotspotop'));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/hotspotop'));
return $this->process_response($content_decoded);
}
@@ -1072,8 +1106,8 @@ class Client
}
/**
* Disable/enable an access point
* ------------------------------
* Disable/enable an access point (using REST)
* -------------------------------------------
* return true on success
* required parameter <ap_id> = 24 char string; value of _id for the access point which can be obtained from the device list
* required parameter <disable> = boolean; true will disable the device, false will enable the device
@@ -1093,8 +1127,8 @@ class Client
}
/**
* Override LED mode for a device
* ------------------------------
* Override LED mode for a device (using REST)
* -------------------------------------------
* return true on success
* required parameter <device_id> = 24 char string; value of _id for the device which can be obtained from the device list
* required parameter <override_mode> = string, off/on/default; "off" will disable the LED of the device,
@@ -1153,8 +1187,8 @@ class Client
}
/**
* Set access point radio settings
* -------------------------------
* Update access point radio settings
* ----------------------------------
* return true on success
* required parameter <ap_id>
* required parameter <radio>(default=ng)
@@ -1172,8 +1206,8 @@ class Client
}
/**
* Set guest login settings
* ------------------------
* Update guest login settings
* ---------------------------
* return true on success
* required parameter <portal_enabled>
* required parameter <portal_customized>
@@ -1229,8 +1263,82 @@ class Client
}
/**
* Add a wlan
* ----------
* List network settings (using REST)
* ----------------------------------
* returns an array of (non-wireless) networks and their settings
*/
public function list_networkconf()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf'));
return $this->process_response($content_decoded);
}
/**
* Create a network (using REST)
* -----------------------------
* return an array with a single object containing details of the new network on success, else return false
* required parameter <network_settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object structured in the same manner as is returned by list_networkconf() for the specific network type.
* Do not include the _id property, it will be assigned by the controller and returned upon success.
*/
public function create_network($network_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'POST';
$json = json_encode($network_settings);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/', 'json='.$json));
return $this->process_response($content_decoded);
}
/**
* Update network settings, base (using REST)
* ------------------------------------------
* return true on success
* required parameter <network_id>
* required parameter <network_settings> = stdClass object or associative array containing the configuration to apply to the network, must be a (partial)
* object/array structured in the same manner as is returned by list_networkconf() for the network.
*/
public function set_networksettings_base($network_id, $network_settings)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($network_settings);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/'.trim($network_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
/**
* Delete a network (using REST)
* -----------------------------
* return true on success
* required parameter <network_id> = 24 char string; _id of the network which can be found with the list_networkconf() function
*/
public function delete_network($network_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/networkconf/'.trim($network_id)));
return $this->process_response_boolean($content_decoded);
}
/**
* List wlan settings (using REST)
* -------------------------------
* returns an array of wireless networks and their settings, or an array containing a single wireless network when using
* the <wlan_id> parameter
* optional parameter <wlan_id> = 24 char string; _id of the wlan to fetch the settings for
*/
public function list_wlanconf($wlan_id = null)
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response($content_decoded);
}
/**
* Create a wlan
* -------------
* return true on success
* required parameter <name> = string; SSID
* required parameter <x_passphrase> = string; new pre-shared key, minimal length is 8 characters, maximum length is 63
@@ -1291,22 +1399,25 @@ class Client
}
/**
* Delete a wlan
* -------------
* Update wlan settings, base (using REST)
* ---------------------------------------
* return true on success
* required parameter <wlan_id> = 24 char string; _id of the wlan that can be found with the list_wlanconf() function
* required parameter <wlan_id>
* required parameter <wlan_settings> = stdClass object or associative array containing the configuration to apply to the wlan, must be a
* (partial) object/array structured in the same manner as is returned by list_wlanconf() for the wlan.
*/
public function delete_wlan($wlan_id)
public function set_wlansettings_base($wlan_id, $wlan_settings)
{
if (!$this->is_loggedin) return false;
$json = json_encode([]);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/del/wlanconf/'.trim($wlan_id), 'json='.$json));
$this->request_type = 'PUT';
$json = json_encode($wlan_settings);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
/**
* Set wlan settings
* -----------------
* Update basic wlan settings
* --------------------------
* return true on success
* required parameter <wlan_id>
* required parameter <x_passphrase> = new pre-shared key, minimal length is 8 characters, maximum length is 63,
@@ -1315,13 +1426,10 @@ class Client
*/
public function set_wlansettings($wlan_id, $x_passphrase, $name = null)
{
if (!$this->is_loggedin) return false;
$json = [];
if (!is_null($x_passphrase)) $json['x_passphrase'] = trim($x_passphrase);
if (!is_null($name)) $json['name'] = trim($name);
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.trim($wlan_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
$payload = new \stdClass();
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);
}
/**
@@ -1333,14 +1441,46 @@ class Client
*/
public function disable_wlan($wlan_id, $disable)
{
if (!$this->is_loggedin) return false;
$payload = new \stdClass();
$action = ($disable) ? false : true;
$json = ['enabled' => (bool)$action];
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.trim($wlan_id), 'json='.$json));
$payload->enabled = (bool)$action;
return $this->set_wlansettings_base($wlan_id, $payload);
}
/**
* Delete a wlan (using REST)
* --------------------------
* return true on success
* required parameter <wlan_id> = 24 char string; _id of the wlan which can be found with the list_wlanconf() function
*/
public function delete_wlan($wlan_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/wlanconf/'.trim($wlan_id)));
return $this->process_response_boolean($content_decoded);
}
/**
* Update MAC filter for a wlan
* ----------------------------
* return true on success
* required parameter <wlan_id>
* required parameter <mac_filter_policy> = string, "allow" or "deny"; default MAC policy to apply
* required parameter <mac_filter_enabled> = boolean; true enables the policy, false disables it
* required parameter <macs> = array; must contain MAC strings to be placed in the MAC filter list,
* replacing existing values. Existing MAC filter list can be obtained
* through list_wlanconf().
*/
public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs)
{
$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);
}
/**
* List events
* -----------
@@ -1358,18 +1498,6 @@ class Client
return $this->process_response($content_decoded);
}
/**
* List wireless settings
* ----------------------
* returns an array of wireless networks and settings
*/
public function list_wlanconf()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/list/wlanconf'));
return $this->process_response($content_decoded);
}
/**
* List alarms
* -----------
@@ -1463,8 +1591,23 @@ class Client
}
/**
* List Radius user accounts
* -------------------------
* List Radius profiles (using REST)
* --------------------------------------
* returns an array of objects containing all Radius profiles for the current site
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function list_radius_profiles()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/radiusprofile'));
return $this->process_response($content_decoded);
}
/**
* List Radius user accounts (using REST)
* --------------------------------------
* returns an array of objects containing all Radius accounts for the current site
*
* NOTES:
@@ -1477,6 +1620,100 @@ class Client
return $this->process_response($content_decoded);
}
/**
* Create a Radius user account (using REST)
* -----------------------------------------
* returns an array containing a single object for the newly created account upon success, else returns false
* required parameter <name> = string; name for the new account
* required parameter <x_password> = string; password for the new account
* required parameter <tunnel_type> = integer; must be one of the following values:
* 1 Point-to-Point Tunneling Protocol (PPTP)
* 2 Layer Two Forwarding (L2F)
* 3 Layer Two Tunneling Protocol (L2TP)
* 4 Ascend Tunnel Management Protocol (ATMP)
* 5 Virtual Tunneling Protocol (VTP)
* 6 IP Authentication Header in the Tunnel-mode (AH)
* 7 IP-in-IP Encapsulation (IP-IP)
* 8 Minimal IP-in-IP Encapsulation (MIN-IP-IP)
* 9 IP Encapsulating Security Payload in the Tunnel-mode (ESP)
* 10 Generic Route Encapsulation (GRE)
* 11 Bay Dial Virtual Services (DVS)
* 12 IP-in-IP Tunneling
* 13 Virtual LANs (VLAN)
* required parameter <tunnel_medium_type> = integer; must be one of the following values:
* 1 IPv4 (IP version 4)
* 2 IPv6 (IP version 6)
* 3 NSAP
* 4 HDLC (8-bit multidrop)
* 5 BBN 1822
* 6 802 (includes all 802 media plus Ethernet "canonical format")
* 7 E.163 (POTS)
* 8 E.164 (SMDS, Frame Relay, ATM)
* 9 F.69 (Telex)
* 10 X.121 (X.25, Frame Relay)
* 11 IPX
* 12 Appletalk
* 13 Decnet IV
* 14 Banyan Vines
* 15 E.164 with NSAP format subaddress
* optional parameter <vlan> = integer; VLAN to assign to the account
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function create_radius_account($name, $x_password, $tunnel_type, $tunnel_medium_type, $vlan = null)
{
if (!$this->is_loggedin) 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);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account', 'json='.$json));
return $this->process_response($content_decoded);
}
/**
* Update Radius account, base (using REST)
* ----------------------------------------
* return true on success
* required parameter <account_id> = 24 char string; _id of the account which can be found with the list_radius_accounts() function
* required parameter <account_details> = stdClass object or associative array containing the new profile to apply to the account, must be a (partial)
* object/array structured in the same manner as is returned by list_radius_accounts() for the account.
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function set_radius_account_base($account_id, $account_details)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode($account_details);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account/'.trim($account_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
/**
* Delete a Radius account (using REST)
* ------------------------------------
* return true on success
* required parameter <account_id> = 24 char string; _id of the account which can be found with the list_radius_accounts() function
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function delete_radius_account($account_id)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'DELETE';
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account/'.trim($account_id)));
return $this->process_response_boolean($content_decoded);
}
/****************************************************************
* "Aliases" for deprecated functions from here, to support
* backward compatibility:
@@ -1508,6 +1745,7 @@ class Client
'Function set_locate_ap() has been deprecated, use locate_ap() instead.',
E_USER_DEPRECATED
);
return $this->locate_ap($mac, true);
}
@@ -1523,6 +1761,7 @@ class Client
'Function unset_locate_ap() has been deprecated, use locate_ap() instead.',
E_USER_DEPRECATED
);
return $this->locate_ap($mac, false);
}
@@ -1537,6 +1776,7 @@ class Client
'Function site_ledson() has been deprecated, use site_leds() instead.',
E_USER_DEPRECATED
);
return $this->site_leds(true);
}
@@ -1551,6 +1791,7 @@ class Client
'Function site_ledsoff() has been deprecated, use site_leds() instead.',
E_USER_DEPRECATED
);
return $this->site_leds(false);
}
@@ -1582,7 +1823,7 @@ class Client
}
if ($this->debug) {
trigger_error('Last error message: ' . $this->last_error_message);
trigger_error('Debug: Last error message: '.$this->last_error_message);
}
}
}
@@ -1610,7 +1851,7 @@ class Client
}
if ($this->debug) {
trigger_error('Last error message: ' . $this->last_error_message);
trigger_error('Debug: Last error message: '.$this->last_error_message);
}
}
}
@@ -1625,6 +1866,7 @@ class Client
{
$ch = $this->get_curl_obj();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if (trim($data) != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
@@ -1653,10 +1895,12 @@ class Client
$strerr = '{ "data" : [ ] , "meta" : { "msg" : "api.err.LoginRequired" , "rc" : "error"}}';
if ($httpcode == 401 && strcmp($content, $strerr) == 0) {
error_log('cURL: Needed reconnect to UniFi Controller');
if ($this->debug) {
error_log('cURL debug: Needed reconnect to UniFi Controller');
}
/**
* explicit unset the old cookie now
* explicitly unset the old cookie now
*/
if (isset($_SESSION['unificookie'])) {
unset($_SESSION['unificookie']);
@@ -1697,6 +1941,12 @@ class Client
}
curl_close($ch);
/**
* set request_type value back to default, just in case
*/
$this->request_type = 'POST';
return $content;
}