Permalink
Browse files

API Browser version 1.0.19, API client class version 1.1.8

- Merge in the pull request by @mcmilk (mainly reformat comments)
- API client class: consistent use of trigger_error() for actual errors
- API browser tool: fix switching between accounts on same controller (using "stale" unificookie from $_SESSION would cause login with previous credentials)
  • Loading branch information...
Erik Slooff
Erik Slooff committed Jul 28, 2017
1 parent 9cadc12 commit 8de5e1e7cf676324f5e259b2a91e3ba4bd955ab9
Showing with 53 additions and 32 deletions.
  1. +2 −0 .gitignore
  2. +6 −6 index.php
  3. +45 −26 phpapi/class.unifi.php
@@ -0,0 +1,2 @@
# Ignore personal config file
config.php
12 index.php 100644 → 100755
@@ -127,13 +127,14 @@
$_SESSION['controller_id'] = $_GET['controller_id'];
/**
* clear the variables from the $_SESSION array that are associated with the previous controller
* clear the variables from the $_SESSION array that are associated with the previous controller session
*/
unset($_SESSION['site_id']);
unset($_SESSION['site_name']);
unset($_SESSION['sites']);
unset($_SESSION['action']);
unset($_SESSION['detected_controller_version']);
unset($_SESSION['unificookie']);
} else {
if (isset($_SESSION['controller']) && isset($controllers)) {
$controller = $_SESSION['controller'];
@@ -238,16 +239,15 @@
if ($loginresults === 400) {
$alert_message = '<div class="alert alert-danger" role="alert">HTTP response status: 400'
. '<br>This is probably caused by a UniFi controller login failure, please check your credentials in '
. 'config.php. After correcting your credentials, please restart your browser or use the <b>Reset PHP session</b> function in the dropdown '
. 'menu on the right, before attempting to use the API browser tool again.</div>';
. 'config.php. After correcting your credentials, please restart your browser or use the <b>Reset PHP session</b> function '
. 'in the dropdown menu on the right, before attempting to use the API browser tool again.</div>';
/**
* to prevent unwanted errors we assign empty values to the following variables
*/
$sites = [];
$detected_controller_version = 'undetected';
} else {
/**
* Remember authentication cookie to the controller.
*/
@@ -265,8 +265,8 @@
$alert_message = '<div class="alert alert-danger" role="alert">No sites available'
. '<br>This is probably caused by incorrect access rights in the UniFi controller for the credentials provided in '
. 'config.php. After updating your credentials, please restart your browser or use the <b>Reset PHP session</b> function in the dropdown '
. 'menu on the right, before attempting to use the API browser tool again.</div>';
. 'config.php. After updating your credentials, please restart your browser or use the <b>Reset PHP session</b> '
. 'function in the dropdown menu on the right, before attempting to use the API browser tool again.</div>';
}
} else {
71 phpapi/class.unifi.php 100644 → 100755
@@ -9,7 +9,7 @@
* and the API as published by Ubiquiti:
* https://www.ubnt.com/downloads/unifi/5.3.8/unifi_sh_api
*
* VERSION: 1.1.7
* VERSION: 1.1.8
*
* NOTES:
* - this class will only work with UniFi Controller versions 4.x and 5.x. There are no checks to prevent
@@ -34,7 +34,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_CLASS_VERSION', '1.1.7');
define('API_CLASS_VERSION', '1.1.8');
class UnifiApi
{
@@ -71,10 +71,14 @@ function __construct($user = '', $password = '', $baseurl = '', $site = '', $ver
function __destruct()
{
/* if user has $_SESSION['unificookie'] set, do not logout here */
/**
* if user has $_SESSION['unificookie'] set, do not logout here
*/
if (isset($_SESSION['unificookie'])) return;
/* logout, if needed */
/**
* logout, if needed
*/
if ($this->is_loggedin) {
$this->logout();
}
@@ -85,7 +89,9 @@ function __destruct()
*/
public function login()
{
/* if user has $_SESSION['unificookie'] set, skip the login ;) */
/**
* if user has $_SESSION['unificookie'] set, skip the login ;)
*/
if (isset($_SESSION['unificookie'])) {
$this->is_loggedin = true;
return $this->is_loggedin;
@@ -99,7 +105,7 @@ public function login()
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));
if (($content = curl_exec($ch)) === false) {
error_log('cURL error: '.curl_error($ch));
trigger_error('cURL error: '.curl_error($ch));
}
if ($this->debug) {
@@ -131,7 +137,7 @@ public function login()
}
if ($code === 400) {
error_log('We have received an HTTP response status: 400. Probably a controller login failure');
trigger_error('We have received an HTTP response status: 400. Probably a controller login failure');
return $code;
}
}
@@ -152,15 +158,6 @@ public function logout()
return true;
}
/**
* GetCookie from UniFi Controller
*/
public function getcookie()
{
if (!$this->is_loggedin) return false;
return $this->cookies;
}
/****************************************************************
* setter/getter functions from here:
****************************************************************/
@@ -218,6 +215,17 @@ public function get_last_error_message()
return false;
}
/**
* Get Cookie from UniFi Controller
* --------------------------------
* returns the UniFi controller cookie
*/
public function getcookie()
{
if (!$this->is_loggedin) return false;
return $this->cookies;
}
/****************************************************************
* Functions to access UniFi controller API routes from here:
****************************************************************/
@@ -1565,14 +1573,15 @@ private function process_response($response)
return true;
} elseif ($response->meta->rc === 'error') {
/**
* we have an error; set latest_error_message if we have a message
* we have an error:
* set $this->set last_error_message if the returned error message is available
*/
if (isset($response->meta->msg)) {
$this->last_error_message = $response->meta->msg;
}
if ($this->debug) {
error_log('Last error message: ' . $this->last_error_message);
trigger_error('Last error message: ' . $this->last_error_message);
}
}
}
@@ -1593,14 +1602,14 @@ private function process_response_boolean($response)
} elseif ($response->meta->rc === 'error') {
/**
* we have an error:
* set latest_error_message if the returned error message is available
* set $this->last_error_message if the returned error message is available
*/
if (isset($response->meta->msg)) {
$this->last_error_message = $response->meta->msg;
}
if ($this->debug) {
error_log('Last error message: ' . $this->last_error_message);
trigger_error('Last error message: ' . $this->last_error_message);
}
}
}
@@ -1633,31 +1642,41 @@ private function exec_curl($url, $data = '')
}
if (($content = curl_exec($ch)) === false) {
error_log('cURL error: '.curl_error($ch));
trigger_error('cURL error: '.curl_error($ch));
}
/* has the session timed out ?! */
/**
* has the session timed out?
*/
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$strerr = '{ "data" : [ ] , "meta" : { "msg" : "api.err.LoginRequired" , "rc" : "error"}}';
if ($httpcode == 401 && strcmp($content, $strerr) == 0) {
trigger_error("cURL: Needed reconnect to UniFi Controller.");
trigger_error("cURL: Needed reconnect to UniFi Controller");
/* explicit unset the old cookie now */
/**
* explicit unset the old cookie now
*/
if (isset($_SESSION['unificookie'])) {
unset($_SESSION['unificookie']);
$have_cookie_in_use = 1;
}
$this->login();
/* when login was okay, exec the same command again */
/**
* when login was okay, exec the same command again
*/
if ($this->is_loggedin) {
curl_close ($ch);
/* setup the cookie for the user within $_SESSION */
/**
* setup the cookie for the user within $_SESSION
*/
if (isset($have_cookie_in_use)) {
$_SESSION['unificookie'] = $this->cookies;
unset($have_cookie_in_use);
}
return $this->exec_curl($url, $data);
}
}

0 comments on commit 8de5e1e

Please sign in to comment.