API client class v1.1.74

- minor refactoring
- fixed minor typos
- updated README to reflect limited visibility when using read-only administrator accounts, reported by @KetchupBomb and @NickDunas
This commit is contained in:
malle-pietje
2021-11-08 10:30:22 +01:00
parent 310abc43b5
commit fbfd6a8246
2 changed files with 37 additions and 35 deletions

View File

@@ -2,8 +2,7 @@
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
(version 6.4.54 has been confirmed to work) as well as UniFi OS-based controllers .
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.50 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).
The package can be installed manually or by using
@@ -16,11 +15,10 @@ easy inclusion in your projects.
- PHP version 5.5.0 or higher
- PHP json and PHP cURL modules
- tested on Apache 2.4 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.2.24 and cURL 7.58.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 (normally TCP port 8443 or port 443 for
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 this class
- you must use **accounts with local access**, not pure UniFi Cloud accounts, to access the UniFi Controller API through
this class
## UniFi OS Support
@@ -86,7 +84,7 @@ require_once 'path/to/src/Client.php';
### Download the Release
If you prefer not to use composer or git,
simply [download the package](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip), uncompress the zip
simply [download the package](https://github.com/Art-of-WiFi/UniFi-API-client/archive/master.zip), unpack the zip
file, then include the file containing the class in your code like so:
```php
@@ -129,6 +127,11 @@ own PHP code.
this feature in production environments where you have a valid SSL cert installed on the UniFi Controller that is
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
visibility on certain collection/object properties. See this [issue](https://github.com/Art-of-WiFi/UniFi-API-client/issues/129)
and this [issue](https://github.com/Art-of-WiFi/UniFi-API-browser/issues/94) for an example where the WPA2 password
isn't accessible for **read-only** administrator accounts.
## Functions/methods supported
The class currently supports the following functions/methods to GET/POST/PUT/DELETE data

View File

@@ -26,14 +26,14 @@ class Client
* NOTE:
* do not modify the values here, instead use the constructor or the getter and setter functions/methods
*/
const CLASS_VERSION = '1.1.73';
const CLASS_VERSION = '1.1.74';
protected $baseurl = 'https://127.0.0.1:8443';
protected $user = '';
protected $password = '';
protected $site = 'default';
protected $version = '6.2.26';
protected $debug = false;
protected $is_loggedin = false;
protected $is_logged_in = false;
protected $is_unifi_os = false;
protected $exec_retries = 0;
protected $cookies = '';
@@ -109,7 +109,7 @@ class Client
/**
* logout, if needed
*/
if ($this->is_loggedin) {
if ($this->is_logged_in) {
$this->logout();
}
}
@@ -124,12 +124,11 @@ class Client
/**
* skip the login process if already logged in
*/
if ($this->is_loggedin === true) {
return true;
if ($this->update_unificookie()) {
$this->is_logged_in = true;
}
if ($this->update_unificookie()) {
$this->is_loggedin = true;
if ($this->is_logged_in === true) {
return true;
}
@@ -218,8 +217,8 @@ class Client
* check the HTTP response code
*/
if ($http_code >= 200 && $http_code < 400) {
$this->is_loggedin = true;
return $this->is_loggedin;
$this->is_logged_in = true;
return $this->is_logged_in;
}
return false;
@@ -277,8 +276,8 @@ class Client
curl_close($ch);
$this->is_loggedin = false;
$this->cookies = '';
$this->is_logged_in = false;
$this->cookies = '';
return true;
}
@@ -1422,7 +1421,7 @@ class Client
* NOTES:
* this is an experimental function, please do not use unless you know exactly what you're doing
*
* @return bool|array|string 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
*/
public function generate_backup()
{
@@ -1526,7 +1525,7 @@ class Client
* @param string $locale_id _id value of the locale section
* @param object|array $payload stdClass object or associative array containing the configuration to apply to the
* site, must be a (partial) object/array structured in the same manner as is
* returned by list_settings() for section with the the "locale" key. Valid
* returned by list_settings() for section with the "locale" key. Valid
* timezones can be obtained in Javascript as explained here:
* https://stackoverflow.com/questions/38399465/how-to-get-list-of-all-timezones-in-javascript
* or in PHP using timezone_identifiers_list(). Do not include the _id property, it
@@ -1710,13 +1709,13 @@ class Client
*
* @param string $admin_id _id value of the admin user to assign, can be obtained using the
* list_all_admins() method/function
* @param bool $readonly optional, whether or not the new admin has readonly
* @param bool $readonly optional, whether the new admin has readonly
* permissions, default value is false which gives the new admin
* Administrator permissions
* @param bool $device_adopt optional, whether or not the new admin has permissions to
* @param bool $device_adopt optional, whether the new admin has permissions to
* adopt devices, default value is false. With versions < 5.9.X this only applies
* when readonly is true.
* @param bool $device_restart optional, whether or not the new admin has permissions to
* @param bool $device_restart optional, whether the new admin has permissions to
* restart devices, default value is false. With versions < 5.9.X this only applies
* when readonly is true.
* @return bool true on success
@@ -3176,7 +3175,7 @@ class Client
* Fetch access points and other devices under management of the controller (USW and/or USG devices)
*
* NOTE:
* changed function/method name to fit it's purpose
* changed function/method name to fit its purpose
*
* @param string $device_mac optional, the MAC address of a single device for which the call must be made
* @return array containing known device objects (or a single device when using the <device_mac> parameter)
@@ -3611,7 +3610,7 @@ class Client
/**
* guard clause to check if logged in when needed
*/
if ($login_required && !$this->is_loggedin) {
if ($login_required && !$this->is_logged_in) {
return false;
}
@@ -3838,17 +3837,17 @@ class Client
$cookie_crumbs = explode(';', $cookie);
foreach ($cookie_crumbs as $cookie_crumb) {
if (strpos($cookie_crumb, 'unifises') !== false) {
$this->cookies = $cookie_crumb;
$this->is_loggedin = true;
$this->is_unifi_os = false;
$this->cookies = $cookie_crumb;
$this->is_logged_in = true;
$this->is_unifi_os = false;
break;
}
if (strpos($cookie_crumb, 'TOKEN') !== false) {
$this->cookies = $cookie_crumb;
$this->is_loggedin = true;
$this->is_unifi_os = true;
$this->cookies = $cookie_crumb;
$this->is_logged_in = true;
$this->is_unifi_os = true;
break;
}
@@ -3864,7 +3863,7 @@ class Client
*
* @param string $path path for the request
* @param object|array $payload optional, payload to pass with the request
* @return bool|array|string response returned by the controller API, false upon error
* @return bool|string response returned by the controller API, false upon error
*/
protected function exec_curl($path, $payload = null)
{
@@ -3969,8 +3968,8 @@ class Client
$_SESSION['unificookie'] = '';
}
$this->is_loggedin = false;
$this->cookies = '';
$this->is_logged_in = false;
$this->cookies = '';
$this->exec_retries++;
curl_close($ch);
@@ -3982,7 +3981,7 @@ class Client
/**
* when re-login was successful, simply execute the same cURL request again
*/
if ($this->is_loggedin) {
if ($this->is_logged_in) {
if ($this->debug) {
error_log(__FUNCTION__ . ': re-logged in, calling exec_curl again');
}