Skip to content

Commit

Permalink
API client class v1.1.51
Browse files Browse the repository at this point in the history
- updated README with all getter and setter methods/functions
- updated get_cookies() and get_cookie() for consistency
- get_cookie() remains for backward compatibility only, should not be used in new code
- fix rare cases where the client would end up in a loop, e.g. issuing a request for a site the cached credentials have no
access to
  • Loading branch information
malle-pietje committed Mar 19, 2020
1 parent 0ebbb4e commit 49d0986
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 55 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## UniFi Controller API client class

A PHP class that provides access to Ubiquiti's [**UniFi SDN Controller**](https://unifi-sdn.ui.com/) API, versions 4.X.X and 5.X.X of the UniFi SDN Controller software are supported (version 5.12.35 has been confirmed to work) as well as UniFi OS-based controllers (version 5.12.59 has been confirmed to work). This class is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
A PHP class that provides access to Ubiquiti's [**UniFi SDN Controller**](https://unifi-sdn.ui.com/) API, versions 4.X.X and 5.X.X of the UniFi SDN Controller software are supported (version 5.12.66 has been confirmed to work) as well as UbiOS-based controllers (version 5.12.59 has been confirmed to work). This class is used in 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 using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.

Expand All @@ -9,11 +9,11 @@ The package can be installed manually or using composer/[packagist](https://pack
- a web server with PHP and cURL modules installed (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)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running

## UniFi OS Support
## UbiOS Support

Support for UniFi OS-based controllers (UniFi Dream Machine Pro) has been added as of version 1.1.47. The class automatically detects UniFi OS devices and adjusts URLs and several functions/methods accordingly. If your own code applies strict validation of the URL that is passed to the constructor, please adapt your logic to allow URLs without a port suffix when dealing with a UniFi OS-based controller.
Support for UbiOS-based controllers (UniFi Dream Machine Pro) has been added as of version 1.1.47. The class automatically detects UbiOS devices and adjusts URLs and several functions/methods accordingly. If your own code applies strict validation of the URL that is passed to the constructor, please adapt your logic to allow URLs without a port suffix when dealing with a UbiOS-based controller.

Please test all methods you plan on using thoroughly before using the API Client with UniFi OS devices in a production environment.
Please test all methods you plan on using thoroughly before using the API Client with UbiOS devices in a production environment.

## Installation

Expand Down Expand Up @@ -100,7 +100,7 @@ Please refer to the `examples/` directory for some more detailed examples which

## Functions/methods supported

The class currently supports the following functions/methods to GET/POST/PUT/DELETE data through the UniFi Controller API. Please refer to the source code for more details on the functions/methods and their respective parameters.
The class currently supports the following functions/methods to GET/POST/PUT/DELETE data through the UniFi Controller API. Please refer to the comments in the source code for more details on the functions/methods and their respective parameters.

- login()
- logout()
Expand Down Expand Up @@ -242,15 +242,28 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- cancel_rolling_upgrade()
- cmd_stat()

Internal functions, getters/setters:
Other functions, getters/setters:

- set_debug()
- get_debug()
- set_site()
- get_site()
- get_cookie() (renamed from getcookie())
- set_cookies()
- get_cookies()
- get_cookie() (renamed from getcookie(), deprecated but still available, use get_cookies() instead)
- get_last_results_raw()
- get_last_error_message()
- set_request_type()
- get_request_type()
- set_ssl_verify_peer()
- get_ssl_verify_peer()
- set_ssl_verify_host()
- get_ssl_verify_host()
- set_connection_timeout()
- get_connection_timeout()
- set_is_unifi_os()
- get_is_unifi_os()


## Need help or have suggestions?

Expand Down
2 changes: 1 addition & 1 deletion examples/block_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login(); // always true regardless of site id

foreach ($macs_to_block as &$mac) {
foreach ($macs_to_block as $mac) {
// block_result is always true even if mac address does not exist :(
$block_result = $unifi_connection->block_sta($mac);

Expand Down
110 changes: 63 additions & 47 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public function login()
* based on the HTTP response code we either trigger an error or
* extract the cookie from the headers
*/
if ($http_code === 400) {
trigger_error('We received an HTTP response status: 400. Probably a controller login failure');
if ($http_code === 400 || $http_code === 401) {
trigger_error("We received the following HTTP response status: $http_code. Probably a controller login failure");

return $http_code;
}
Expand Down Expand Up @@ -3694,8 +3694,8 @@ public function get_last_error_message()
}

/**
* Get Cookie from UniFi controller
* --------------------------------
* Get Cookie from UniFi controller (singular and plural)
* ------------------------------------------------------
* returns the UniFi controller cookie
*
* NOTES:
Expand All @@ -3715,14 +3715,19 @@ public function get_cookie()
return $this->cookies;
}

/******************************************************************
* other getter/setter functions/methods from here, use with care!
******************************************************************/
public function get_cookies()
{
if (!$this->is_loggedin) {
return false;
}

return $this->cookies;
}

/******************************************************************
* other getter/setter functions/methods from here, use with care!
******************************************************************/

public function set_cookies($cookies_value)
{
$this->cookies = $cookies_value;
Expand All @@ -3733,6 +3738,18 @@ public function get_request_type()
return $this->request_type;
}

public function set_request_type($request_type)
{

if (!in_array($request_type, $this->request_types_allowed)) {
return false;
}

$this->request_type = $request_type;

return true;
}

public function get_ssl_verify_peer()
{
return $this->curl_ssl_verify_peer;
Expand Down Expand Up @@ -3773,18 +3790,6 @@ public function set_ssl_verify_host($ssl_verify_host)
return true;
}

public function set_request_type($request_type)
{

if (!in_array($request_type, $this->request_types_allowed)) {
return false;
}

$this->request_type = $request_type;

return true;
}

public function get_is_unifi_os()
{
return $this->is_unifi_os;
Expand Down Expand Up @@ -4102,58 +4107,69 @@ protected function exec_curl($path, $payload = null)
*/
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 401) {
curl_close($ch);
if ($this->debug) {
error_log('cURL debug: needed to reconnect to UniFi controller');
}

/**
* explicitly clear the expired Cookie/Token before logging in again
* explicitly clear the expired Cookie/Token and logging out before logging in again
*/
if (isset($_SESSION['unificookie'])) {
$_SESSION['unificookie'] = '';
$this->is_loggedin = false;
}

$this->is_loggedin = false;

/**
* then login again
*/
$this->login();

/**
* when login was successful, execute the same command again
* when login was successful, execute the same cURL request again
*/
if ($this->is_loggedin) {
return $this->exec_curl($path, $payload);
}
} else {
if ($this->debug) {
print PHP_EOL . '<pre>';
print PHP_EOL . '---------cURL INFO-----------' . PHP_EOL;
print_r(curl_getinfo($ch));
print PHP_EOL . '-------URL & PAYLOAD---------' . PHP_EOL;
print $url . PHP_EOL;
if (empty($json_payload)) {
print 'empty payload';
} else {
print $json_payload;
$content = curl_exec($ch);
if (curl_errno($ch)) {
trigger_error('cURL error: ' . curl_error($ch));
curl_close($ch);

return false;
}
} else {
curl_close($ch);

print PHP_EOL . '----------RESPONSE-----------' . PHP_EOL;
print $content;
print PHP_EOL . '-----------------------------' . PHP_EOL;
print '</pre>' . PHP_EOL;
return false;
}
}

curl_close($ch);

/**
* set request_type value back to default, just in case
*/
$this->request_type = 'GET';
if ($this->debug) {
print PHP_EOL . '<pre>';
print PHP_EOL . '---------cURL INFO-----------' . PHP_EOL;
print_r(curl_getinfo($ch));
print PHP_EOL . '-------URL & PAYLOAD---------' . PHP_EOL;
print $url . PHP_EOL;
if (empty($json_payload)) {
print 'empty payload';
} else {
print $json_payload;
}

return $content;
print PHP_EOL . '----------RESPONSE-----------' . PHP_EOL;
print $content;
print PHP_EOL . '-----------------------------' . PHP_EOL;
print '</pre>' . PHP_EOL;
}

curl_close($ch);

/**
* set request_type value back to default, just in case
*/
$this->request_type = 'GET';

return $content;

}

return false;
Expand Down

0 comments on commit 49d0986

Please sign in to comment.