Skip to content

Commit

Permalink
API Browser version 1.0.8, API client class version 1.0.12
Browse files Browse the repository at this point in the history
- API Browser: added option to clear PHP session, useful when login
errors occur, mostly after upgrades or incorrect credential changes
- API client class: modified list_clients() function/method to also
allow request for a single device
  • Loading branch information
malle-pietje committed Dec 12, 2016
1 parent 0584fc4 commit 70f982d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
23 changes: 20 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* the currently supported data collections/API endpoints in the README.md file
* - this tool currently supports versions 4.x and 5.x of the UniFi Controller software
*
* VERSION: 1.0.7
* VERSION: 1.0.8
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -20,13 +20,24 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.7');
define('API_BROWSER_VERSION', '1.0.8');

/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
*/
session_start();

/**
* check whether user has requested to clear the PHP session
* - this function can be useful when login errors occur, mostly after upgrades or incorrect credential changes
*/
if (isset($_GET['reset_session']) && $_GET['reset_session'] == true) {
$_SESSION = array();
session_unset();
session_destroy();
session_start();
}

/**
* starting timing of the session here
*/
Expand Down Expand Up @@ -707,6 +718,8 @@ function sites_sort($a, $b)
<li id="united"><a href="?theme=united">United</a></li>
<li id="yeti"><a href="?theme=yeti">Yeti</a></li>
<li role="separator" class="divider"></li>
<li id="reset_session" data-toggle="tooltip" data-placement="top" data-original-title="In some cases this will fix login errors (empty sites list)"><a href="?reset_session=true"><i class="fa fa-refresh"></i> Reset PHP session</a></li>
<li role="separator" class="divider"></li>
<li id="info" data-toggle="modal" data-target="#aboutModal"><a href="#"><i class="fa fa-info-circle"></i> About UniFi API Browser</a></li>
</ul>
</li>
Expand Down Expand Up @@ -833,11 +846,15 @@ function sites_sort($a, $b)
/**
* highlight and mark the selected options in the dropdown menus for $controller_id, $action, $site_id, $theme and $output_format
* NOTE:
* these actions are performed conditionally when values are set for the respective PHP variables
* these actions are performed conditionally if values are set for the respective PHP variables
*/
('<?php echo $action ?>' != '') ? $('#<?php echo $action ?>').addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;
('<?php echo $site_id ?>' != '') ? $('#<?php echo $site_id ?>').addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;
('<?php echo $controller_id ?>' != '') ? $('#controller_<?php echo $controller_id ?>').addClass('active').find('a').append(' <i class="fa fa-check"></i>') : false;

/**
* these two options have default values so no conditions needed here
*/
$('#<?php echo $output_format ?>').addClass('active').find('a').append(' <i class="fa fa-check"></i>');
$('#<?php echo $theme ?>').addClass('active').find('a').append(' <i class="fa fa-check"></i>');

Expand Down
18 changes: 9 additions & 9 deletions phpapi/class.unifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.0.11
* VERSION: 1.0.12
*
* NOTES:
* - this Class will only work with UniFi Controller versions 4.x and 5.x. There are no checks to prevent
Expand All @@ -26,7 +26,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_CLASS_VERSION', '1.0.11');
define('API_CLASS_VERSION', '1.0.12');

class unifiapi {
public $user = '';
Expand All @@ -37,7 +37,6 @@ class unifiapi {
public $is_loggedin = FALSE;
public $debug = FALSE;
private $cookies = '';
private $payload_len = '';
private $request_type = 'POST';

function __construct($user = '', $password = '', $baseurl = '', $site = '', $version = '') {
Expand Down Expand Up @@ -524,14 +523,15 @@ public function list_guests($within = 8760) {
}

/**
* List client devices
* -------------------
* returns an array of client device objects
* List online client device(s)
* ----------------------------
* returns an array of online client device objects, or in case of a single device request, returns a single client device object
* optional parameter <client_mac> = the MAC address of a single online client device for which the call must be made
*/
public function list_clients() {
public function list_clients($client_mac = NULL) {
if (!$this->is_loggedin) return FALSE;
$return = array();
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/sta'));
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/sta/'.$client_mac));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
Expand All @@ -549,7 +549,7 @@ public function list_clients() {
* Get data for a single client device
* -----------------------------------
* returns an object with the client device information
* required parameter <client_mac> = client MAC address
* required parameter <client_mac> = client device MAC address
*/
public function stat_client($client_mac) {
if (!$this->is_loggedin) return FALSE;
Expand Down

0 comments on commit 70f982d

Please sign in to comment.