Skip to content

Commit

Permalink
Add session management to the UniFi PHP API (#36)
Browse files Browse the repository at this point in the history
* Add session management to the UniFi PHP API (contributed by @mcmilk )
- the variable $_SESSION['unificookie'] is checked, when
  creating a new UnifiApi instance
- the function getcookie() can be used for setting up this
  session variable
- when a reconnect is needed, it will be done, and the
  global session variable will be updated with the new
  cookie automatically
- this patch speeds up the whole API a lot

* Add the use of the $_SESSION['unificookie'] to the browser.
  • Loading branch information
mcmilk authored and malle-pietje committed Jul 28, 2017
1 parent 114e4f8 commit 9cadc12
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@
$sites = [];
$detected_controller_version = 'undetected';
} else {

/**
* Remember authentication cookie to the controller.
*/
$_SESSION['unificookie'] = $unifidata->getcookie();

/**
* Get the list of sites managed by the UniFi controller (if not already stored in the $_SESSION array)
*/
Expand Down
48 changes: 48 additions & 0 deletions phpapi/class.unifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ function __construct($user = '', $password = '', $baseurl = '', $site = '', $ver
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'];
}
}

function __destruct()
{
/* if user has $_SESSION['unificookie'] set, do not logout here */
if (isset($_SESSION['unificookie'])) return;

/* logout, if needed */
if ($this->is_loggedin) {
$this->logout();
}
Expand All @@ -78,6 +85,12 @@ function __destruct()
*/
public function login()
{
/* if user has $_SESSION['unificookie'] set, skip the login ;) */
if (isset($_SESSION['unificookie'])) {
$this->is_loggedin = true;
return $this->is_loggedin;
}

$ch = $this->get_curl_obj();

curl_setopt($ch, CURLOPT_HEADER, 1);
Expand Down Expand Up @@ -139,6 +152,15 @@ 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:
****************************************************************/
Expand Down Expand Up @@ -1614,6 +1636,32 @@ private function exec_curl($url, $data = '')
error_log('cURL error: '.curl_error($ch));
}

/* 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.");

/* 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 */
if ($this->is_loggedin) {
curl_close ($ch);

/* 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);
}
}

if ($this->debug) {
print '<pre>';
print PHP_EOL.'---------cURL INFO-----------'.PHP_EOL;
Expand Down

0 comments on commit 9cadc12

Please sign in to comment.