Compare commits

...

5 Commits

Author SHA1 Message Date
malle-pietje
9e092f8b85 Merge branch 'master' of github.com:Art-of-WiFi/UniFi-API-client
merge for device-basic
2023-03-28 12:22:42 +02:00
Sebastian Grodzicki
e4dc2e273c Add stat/device-basic endpoint (#188)
Merge to include device-basic endpoint
2023-03-28 12:21:43 +02:00
malle-pietje
67d318ca91 - bumped version to class v1.1.80
- added generate_backup_site() method, contributed by @labo-jad
2023-03-28 12:19:29 +02:00
malle-pietje
cbe89d913c API client class v1.1.79
- updated README to reflect support for 7.X
- `get_last_error_message()` now always returns a string which is empty if no message is available
- applied PR #151 in slightly different way for consistency with other similar code sections, contributed by @banakito
- changed default argument values in several methods/functions from null to an empty string
2022-03-28 11:57:57 +02:00
malle-pietje
4fe1780e1d API client class v1.1.78
- fixed `create_radius_account()` method to make Tunnel Type and Medium optional parameters, reported by @CodeByJacob
2022-03-11 13:24:31 +01:00
3 changed files with 79 additions and 49 deletions

View File

@@ -2,8 +2,9 @@
A PHP class that provides access to Ubiquiti's [**UniFi Network Controller**](https://unifi-network.ui.com/) API. A PHP class that provides access to Ubiquiti's [**UniFi Network Controller**](https://unifi-network.ui.com/) API.
UniFi Network Controller software versions 4.X.X, 5.X.X and 6.X.X are supported as well as UniFi OS-based controllers (version 6.5.55 has been confirmed to work). UniFi Network Controller software versions 4.X.X, 5.X.X, 6.X.X, and 7.X.X (version 7.2.93 has been confirmed to work)
This class is used by our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser). are supported as well as UniFi OS-based controllers. This class is used by our API browser tool which can be found
[here](https://github.com/Art-of-WiFi/UniFi-API-browser).
The package can be installed manually or by using The package can be installed manually or by using
composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for
@@ -15,20 +16,24 @@ easy inclusion in your projects.
- PHP 5.5.0 or higher - PHP 5.5.0 or higher
- PHP json and PHP cURL modules - PHP json and PHP cURL modules
- tested on Apache 2.4 with PHP 5.6.1 and cURL 7.42.1 and with PHP 7.4.9 and cURL 7.68.0 - tested on Apache 2.4 with PHP 5.6.1 and cURL 7.42.1 and with PHP 7.4.9 and cURL 7.68.0
- direct network connectivity between this server and the host and port (normally TCP port 8443 or port 443 for - direct network connectivity between this server and the host and port (usually TCP port 8443 or port 443 for
UniFi OS) where the UniFi Controller is running UniFi OS) where the UniFi Controller is running
- you must use **accounts with local access**, not pure UniFi Cloud accounts, to access the UniFi Controller API through - you must use **accounts with local access**, not UniFi Cloud accounts, to access the UniFi Controller API
this class through this class
## UniFi OS Support ## UniFi OS Support
Support for UniFi OS-based controllers (UniFi Dream Router, UniFi Dream Machine, UniFi Dream Machine Pro Support for UniFi OS-based controllers has been added as of version 1.1.47:
or Cloud Key Gen2/Cloud Key Gen2 Plus with firmware version 2.0.24 or higher) has - UniFi Dream Router (UDR)
been added as of version 1.1.47. The class automatically detects UniFi OS devices and - UniFi Dream Machine (UDM)
adjusts URLs and several functions/methods accordingly. If your own code implements strict - UniFi Dream Machine Pro (UDM PRO)
validation of the URL that is passed to the constructor, please adapt your logic to - UniFi Cloud Key Gen2 (UCK G2), firmware version 2.0.24 or higher
allow URLs without a port suffix or with port 443 when dealing with a UniFi OS-based - UniFi Cloud Key Gen2 Plus (UCK G2 Plus), firmware version 2.0.24 or higher
controller. - UniFi Cloud Console, details [here](https://help.ui.com/hc/en-us/articles/4415364143511)
The class automatically detects UniFi OS-based controllers and adjusts URLs and several functions/methods accordingly.
If your own code implements strict validation of the URL that is passed to the constructor, please adapt your
logic to allow URLs without a port suffix or with port 443 when working with a UniFi OS-based controller.
Please test all methods you plan on using thoroughly before using the API Client with Please test all methods you plan on using thoroughly before using the API Client with
UniFi OS devices in a production environment. UniFi OS devices in a production environment.
@@ -128,9 +133,10 @@ own PHP code.
associated with the FQDN in the `controller_url` parameter. This option was added with API client version 1.1.16. associated with the FQDN in the `controller_url` parameter. This option was added with API client version 1.1.16.
3. Using an administrator account (`$controller_user` in the above example) with **read-only** permissions can limit 3. Using an administrator account (`$controller_user` in the above example) with **read-only** permissions can limit
visibility on certain collection/object properties. See this [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues/129) visibility on certain collection/object properties. See this
and this [issue](https://github.com/Art-of-WiFi/UniFi-API-browser/issues/94) for an example where the WPA2 password [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues/129) and this
isn't accessible for **read-only** administrator accounts. [issue](https://github.com/Art-of-WiFi/UniFi-API-browser/issues/94) for an example where the WPA2 password isn't
visible for **read-only** administrator accounts.
## Functions/methods supported ## Functions/methods supported
@@ -178,6 +184,8 @@ more details on each of the functions/methods and their respective parameters.
- edit_usergroup() - edit_usergroup()
- extend_guest_validity() - extend_guest_validity()
- forget_sta() (supported on controller version 5.9.X and higher) - forget_sta() (supported on controller version 5.9.X and higher)
- generate_backup()
- generate_backup_site()
- invite_admin() - invite_admin()
- led_override() - led_override()
- list_admins() - list_admins()
@@ -347,7 +355,7 @@ This class is based on the initial work by the following developers:
and the API as published by Ubiquiti: and the API as published by Ubiquiti:
- https://dl.ui.com/unifi/6.5.55/unifi_sh_api - https://dl.ui.com/unifi/7.0.25/unifi_sh_api
## Important Disclaimer ## Important Disclaimer

View File

@@ -8,6 +8,9 @@
* usage: If the file is called via a web URL, it should be called like: update_switch_poe-mode.php?poe_mode=off * usage: If the file is called via a web URL, it should be called like: update_switch_poe-mode.php?poe_mode=off
* If the file is called via the command line, it should be called like: php update_switch_poe-mode.php off * If the file is called via the command line, it should be called like: php update_switch_poe-mode.php off
* The values can be "off" or "auto" * The values can be "off" or "auto"
*
* IMPORTANT INFORMATION:
* This example no longer works with controller versions 7.1.X and higher. You now need to work with port profiles to enable POE on a switch port.
*/ */
/** /**
@@ -91,4 +94,4 @@ if (!$update_device) {
echo json_encode($error, JSON_PRETTY_PRINT); echo json_encode($error, JSON_PRETTY_PRINT);
} }
echo json_encode($update_device, JSON_PRETTY_PRINT); echo json_encode($update_device, JSON_PRETTY_PRINT);

View File

@@ -13,7 +13,7 @@ namespace UniFi_API;
* *
* @package UniFi_Controller_API_Client_Class * @package UniFi_Controller_API_Client_Class
* @author Art of WiFi <info@artofwifi.net> * @author Art of WiFi <info@artofwifi.net>
* @version Release: 1.1.77 * @version Release: 1.1.80
* @license This class is subject to the MIT license that is bundled with this package in the file LICENSE.md * @license This class is subject to the MIT license that is bundled with this package in the file LICENSE.md
* @example This directory in the package repository contains a collection of examples: * @example This directory in the package repository contains a collection of examples:
* https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples * https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples
@@ -26,7 +26,7 @@ class Client
* NOTE: * NOTE:
* do not modify the values here, instead use the constructor or the getter and setter functions/methods * do not modify the values here, instead use the constructor or the getter and setter functions/methods
*/ */
const CLASS_VERSION = '1.1.77'; const CLASS_VERSION = '1.1.80';
protected $baseurl = 'https://127.0.0.1:8443'; protected $baseurl = 'https://127.0.0.1:8443';
protected $user = ''; protected $user = '';
protected $password = ''; protected $password = '';
@@ -38,7 +38,7 @@ class Client
protected $exec_retries = 0; protected $exec_retries = 0;
protected $cookies = ''; protected $cookies = '';
protected $last_results_raw = null; protected $last_results_raw = null;
protected $last_error_message = null; protected $last_error_message = '';
protected $curl_ssl_verify_peer = false; protected $curl_ssl_verify_peer = false;
protected $curl_ssl_verify_host = false; protected $curl_ssl_verify_host = false;
protected $curl_http_version = CURL_HTTP_VERSION_NONE; protected $curl_http_version = CURL_HTTP_VERSION_NONE;
@@ -424,11 +424,10 @@ class Client
* Add/modify/remove a client-device note * Add/modify/remove a client-device note
* *
* @param string $user_id id of the client-device to be modified * @param string $user_id id of the client-device to be modified
* @param string $note optional, note to be applied to the client-device, when empty or not set, * @param string $note optional, note to be applied to the client-device
* the existing note for the client-device is removed and "noted" attribute set to false
* @return bool returns true upon success * @return bool returns true upon success
*/ */
public function set_sta_note($user_id, $note = null) public function set_sta_note($user_id, $note = '')
{ {
$payload = ['note' => $note]; $payload = ['note' => $note];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload); return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload);
@@ -442,7 +441,7 @@ class Client
* the existing name for the client device is removed * the existing name for the client device is removed
* @return bool returns true upon success * @return bool returns true upon success
*/ */
public function set_sta_name($user_id, $name = null) public function set_sta_name($user_id, $name = '')
{ {
$payload = ['name' => $name]; $payload = ['name' => $name];
return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload); return $this->fetch_results_boolean('/api/s/' . $this->site . '/upd/user/' . trim($user_id), $payload);
@@ -1397,6 +1396,16 @@ class Client
return $this->fetch_results('/api/s/' . $this->site . '/list/user'); return $this->fetch_results('/api/s/' . $this->site . '/list/user');
} }
/**
* List of site devices with a basic subset of fields (e.g., mac, state, adopted, disabled, type, model, name)
*
* @return array|false an array containing known UniFi device objects), false upon error
*/
public function list_devices_basic()
{
return $this->fetch_results('/api/s/' . $this->site . '/stat/device-basic');
}
/** /**
* Fetch UniFi devices * Fetch UniFi devices
* *
@@ -1448,10 +1457,9 @@ class Client
} }
/** /**
* Generate backup * Generate a backup
* *
* NOTES: * NOTES: this is an experimental function, please do not use unless you know exactly what you're doing
* this is an experimental function, please do not use unless you know exactly what you're doing
* *
* @return array|bool URL from where the backup file can be downloaded once generated, false upon failure * @return array|bool URL from where the backup file can be downloaded once generated, false upon failure
*/ */
@@ -1472,6 +1480,20 @@ class Client
return $this->fetch_results('/api/s/' . $this->site . '/cmd/backup', $payload); return $this->fetch_results('/api/s/' . $this->site . '/cmd/backup', $payload);
} }
/**
* Generate a backup/export of the current site
*
* NOTES: this is an experimental function, please do not use unless you know exactly what you're doing
*
* @return array|bool URL from where the backup/export file can be downloaded once generated, false upon failure
*/
public function generate_backup_site()
{
$payload = ['cmd' => 'export-site'];
return $this->fetch_results('/api/s/' . $this->site . '/cmd/backup', $payload);
}
/** /**
* Fetch sites * Fetch sites
* *
@@ -1706,7 +1728,8 @@ class Client
$device_adopt = false, $device_adopt = false,
$device_restart = false $device_restart = false
) { ) {
$email_valid = filter_var(trim($email), FILTER_VALIDATE_EMAIL); $email = trim($email);
$email_valid = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email_valid) { if (!$email_valid) {
trigger_error('The email address provided is invalid!'); trigger_error('The email address provided is invalid!');
return false; return false;
@@ -1714,7 +1737,7 @@ class Client
$payload = [ $payload = [
'name' => trim($name), 'name' => trim($name),
'email' => trim($email), 'email' => $email,
'for_sso' => $enable_sso, 'for_sso' => $enable_sso,
'cmd' => 'invite-admin', 'cmd' => 'invite-admin',
'role' => 'admin', 'role' => 'admin',
@@ -1895,10 +1918,10 @@ class Client
* @param string $note optional, note to attach to the hotspot operator * @param string $note optional, note to attach to the hotspot operator
* @return bool true upon success * @return bool true upon success
*/ */
public function create_hotspotop($name, $x_password, $note = null) public function create_hotspotop($name, $x_password, $note = '')
{ {
$payload = ['name' => $name, 'x_password' => $x_password]; $payload = ['name' => $name, 'x_password' => $x_password];
if (is_string($note)) { if (!empty($note)) {
$payload['note'] = trim($note); $payload['note'] = trim($note);
} }
@@ -1934,7 +1957,7 @@ class Client
$minutes, $minutes,
$count = 1, $count = 1,
$quota = 0, $quota = 0,
$note = null, $note = '',
$up = null, $up = null,
$down = null, $down = null,
$megabytes = null $megabytes = null
@@ -1946,7 +1969,7 @@ class Client
'quota' => intval($quota), 'quota' => intval($quota),
]; ];
if (is_string($note)) { if (!empty($note)) {
$payload['note'] = trim($note); $payload['note'] = trim($note);
} }
@@ -2575,7 +2598,7 @@ class Client
* @return array containing wireless networks and their settings, or an array containing a single wireless network * @return array containing wireless networks and their settings, or an array containing a single wireless network
* when using the <wlan_id> parameter * when using the <wlan_id> parameter
*/ */
public function list_wlanconf($wlan_id = null) public function list_wlanconf($wlan_id = '')
{ {
return $this->fetch_results('/api/s/' . $this->site . '/rest/wlanconf/' . trim($wlan_id)); return $this->fetch_results('/api/s/' . $this->site . '/rest/wlanconf/' . trim($wlan_id));
} }
@@ -2676,12 +2699,12 @@ class Client
* @param string $name optional, SSID * @param string $name optional, SSID
* @return bool true on success * @return bool true on success
*/ */
public function set_wlansettings($wlan_id, $x_passphrase, $name = null) public function set_wlansettings($wlan_id, $x_passphrase, $name = '')
{ {
$payload = []; $payload = [];
$payload['x_passphrase'] = trim($x_passphrase); $payload['x_passphrase'] = trim($x_passphrase);
if (is_string($name)) { if (!empty($name)) {
$payload['name'] = trim($name); $payload['name'] = trim($name);
} }
@@ -2804,7 +2827,7 @@ class Client
* by default all alarms are archived * by default all alarms are archived
* @return bool true on success * @return bool true on success
*/ */
public function archive_alarm($alarm_id = null) public function archive_alarm($alarm_id = '')
{ {
$payload = ['cmd' => 'archive-all-alarms']; $payload = ['cmd' => 'archive-all-alarms'];
if (!empty($alarm_id)) { if (!empty($alarm_id)) {
@@ -3043,9 +3066,9 @@ class Client
*/ */
public function create_radius_account($name, $x_password, $tunnel_type = null, $tunnel_medium_type = null, $vlan = null) public function create_radius_account($name, $x_password, $tunnel_type = null, $tunnel_medium_type = null, $vlan = null)
{ {
$tunnel_types = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; $tunnel_type_values = [null, 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]; $tunnel_medium_type_values = [null, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
if ((!is_null($tunnel_type) && !in_array($tunnel_type, $tunnel_types)) || (!is_null($tunnel_medium_type) && !in_array($tunnel_medium_type, $tunnel_medium_types)) || ($tunnel_type ^ $tunnel_medium_type)) { if (!in_array($tunnel_type, $tunnel_type_values) || !in_array($tunnel_medium_type, $tunnel_medium_type_values) || ($tunnel_type xor $tunnel_medium_type)) {
return false; return false;
} }
@@ -3364,7 +3387,7 @@ class Client
* *
* @param boolean $return_json true returns the results in "pretty printed" json format, * @param boolean $return_json true returns the results in "pretty printed" json format,
* false returns PHP stdClass Object format (default) * false returns PHP stdClass Object format (default)
* @return false|string|null the raw results as returned by the controller API * @return false|string|object|null the raw results as returned by the controller API
*/ */
public function get_last_results_raw($return_json = false) public function get_last_results_raw($return_json = false)
{ {
@@ -3382,16 +3405,12 @@ class Client
/** /**
* Get last error message * Get last error message
* *
* @return object|bool the error message of the last method called in PHP stdClass Object format, returns false if * @return string the error message of the last method called in PHP stdClass Object format, an empty string when
* unavailable * none available
*/ */
public function get_last_error_message() public function get_last_error_message()
{ {
if (!is_null($this->last_error_message)) { return $this->last_error_message;
return $this->last_error_message;
}
return false;
} }
/** /**
@@ -3655,7 +3674,7 @@ class Client
if (isset($response->meta->rc)) { if (isset($response->meta->rc)) {
if ($response->meta->rc === 'ok') { if ($response->meta->rc === 'ok') {
$this->last_error_message = null; $this->last_error_message = '';
if (is_array($response->data) && !$boolean) { if (is_array($response->data) && !$boolean) {
return $response->data; return $response->data;
} }