Skip to content

Commit

Permalink
API Browser version 1.0.4, API client class version 1.0.8
Browse files Browse the repository at this point in the history
API client class: minor formatting cleanup
API Browser: fixed controller version detection
API Browser: made sites and controller dropdown lists scrollable
API Browser: sites dropdown list is now sorted by full name (desc)
API Browser: added `all sites stats` collection
  • Loading branch information
malle-pietje committed Oct 14, 2016
1 parent 62b1aee commit a68f210
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The UniFi API browser tool offers the following features:
- hourly site stats
- hourly access point stats
- daily site stats
- all sites stats (supported on controller version 5.2.9 and higher)
- health metrics
- dashboard metrics (supported on controller version 4.9.1.alpha and higher)
- port forward stats
Expand Down
57 changes: 41 additions & 16 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Ubiquiti Community forums for this:
* https://community.ubnt.com/t5/UniFi-Wireless/UniFi-API-browser-tool-released/m-p/1392651
*
* VERSION: 1.0.3
* VERSION: 1.0.4
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -25,7 +25,7 @@
*
*/

define('API_BROWSER_VERSION', '1.0.3');
define('API_BROWSER_VERSION', '1.0.4');

/**
* to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
Expand All @@ -51,6 +51,7 @@
$alert_message = '';
$cookietimeout = '1800';
$debug = false;
$detected_controller_version = '';

/**
* load the configuration file
Expand Down Expand Up @@ -105,6 +106,7 @@
unset($_SESSION['site_id']);
unset($_SESSION['site_name']);
unset($_SESSION['sites']);
unset($_SESSION['detected_controller_version']);
} else {
if (isset($_SESSION['controller']) && isset($controllers)) {
$controller = $_SESSION['controller'];
Expand Down Expand Up @@ -214,19 +216,19 @@
}

/**
* get the version of the controller (if not already stored in $_SESSION or when empty)
* only get the version once a site has been selected
* Get the version of the controller (if not already stored in $_SESSION or when empty)
*/
if($site_id != '') {
if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === '') {
$site_info = $unifidata->stat_sysinfo();
$detected_controller_version = $site_info[0]->version;
$_SESSION['detected_controller_version'] = $detected_controller_version;
if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] == '') {
$site_info = $unifidata->stat_sysinfo();
$detected_controller_version = $site_info[0]->version;

if ($detected_controller_version == '') {
$_SESSION['detected_controller_version'] = 'undetected';
} else {
$detected_controller_version = $_SESSION['detected_controller_version'];
$_SESSION['detected_controller_version'] = $detected_controller_version;
}
} else {
$detected_controller_version = 'undetected';
$detected_controller_version = $_SESSION['detected_controller_version'];
}
}

Expand Down Expand Up @@ -364,6 +366,10 @@
$selection = 'self';
$data = $unifidata->list_self();
break;
case 'stat_sites':
$selection = 'all site stats';
$data = $unifidata->stat_sites();
break;
default:
break;
}
Expand Down Expand Up @@ -432,6 +438,14 @@ function print_output($output_format, $data)
}
}

/**
* function to sort the sites collection
*/
function sites_sort($a, $b)
{
return strcmp($a->desc, $b->desc);
}

if (isset($_SESSION['controller'])) {
/**
* log off from the UniFi controller API
Expand All @@ -454,6 +468,12 @@ function print_output($output_format, $data)
body {
padding-top: 70px;
}

.scrollable-menu {
height: auto;
max-height: 600px;
overflow-x: hidden;
}
</style>
</head>
<body>
Expand All @@ -466,7 +486,7 @@ function print_output($output_format, $data)
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.php">UniFi API browser</a>
<a class="navbar-brand hidden-sm hidden-md" href="index.php">UniFi API browser</a>
</div>
<div id="navbar-main" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-left">
Expand All @@ -485,7 +505,7 @@ function print_output($output_format, $data)
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu" id="controllerslist">
<ul class="dropdown-menu scrollable-menu" id="controllerslist">
<li class="dropdown-header">Select a controller</li>
<?php
/**
Expand All @@ -505,12 +525,14 @@ function print_output($output_format, $data)
Sites
<span class="caret"></span>
</a>
<ul class="dropdown-menu" id="siteslist">
<ul class="dropdown-menu scrollable-menu" id="siteslist">
<li class="dropdown-header">Select a site</li>
<?php
/**
* here we loop through the available sites
* here we loop through the available sites, after we have sorted the sites collection
*/
usort($sites, "sites_sort");

foreach ($sites as $site) {
echo '<li id="' . $site->name . '"><a href="?site_id=' . $site->name . '&site_name=' . $site->desc . '">' . $site->desc . '</a></li>' . "\n";
}
Expand Down Expand Up @@ -572,6 +594,9 @@ function print_output($output_format, $data)
<ul class="dropdown-menu">
<li id="stat_hourly_site"><a href="?action=stat_hourly_site">hourly site stats</a></li>
<li id="stat_daily_site"><a href="?action=stat_daily_site">daily site stats</a></li>
<?php if ($detected_controller_version != 'undetected' && version_compare($detected_controller_version, '5.2.9') >= 0) { ?>
<li id="list_dashboard"><a href="?action=stat_sites">all sites stats</a></li>
<?php } ?>
<li role="separator" class="divider"></li>
<li id="stat_hourly_aps"><a href="?action=stat_hourly_aps">hourly access point stats</a></li>
<li role="separator" class="divider"></li>
Expand Down Expand Up @@ -740,7 +765,7 @@ function print_output($output_format, $data)
<dt>controller url</dt>
<dd><span class="label label-primary"><?php if (isset($_SESSION['controller'])) { echo $controller['url']; } ?></span></dd>
<dt>version detected</dt>
<dd><span class="label label-primary"><?php echo $detected_controller_version ?></span></dd>
<dd><span class="label label-primary"><?php if (isset($_SESSION['controller'])) { echo $detected_controller_version; } ?></span></dd>
</dl>
<hr>
<dl class="dl-horizontal col-sm-offset-1">
Expand Down
26 changes: 13 additions & 13 deletions phpapi/class.unifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* domwo: http://community.ubnt.com/t5/UniFi-Wireless/little-php-class-for-unifi-api/m-p/603051
* fbagnol: https://github.com/fbagnol/class.unifi.php
* and the API as published by Ubiquiti:
* https://www.ubnt.com/downloads/unifi/4.8.20/unifi_sh_api
* https://www.ubnt.com/downloads/unifi/5.2.9/unifi_sh_api
*
* VERSION: 1.0.7
* VERSION: 1.0.8
*
* NOTES:
* - this Class will only work with UniFi Controller versions 4.x and higher. There are no checks to prevent
Expand All @@ -27,7 +27,7 @@
*
*/

define('API_CLASS_VERSION', '1.0.7');
define('API_CLASS_VERSION', '1.0.8');

class unifiapi {
public $user = '';
Expand All @@ -40,11 +40,11 @@ class unifiapi {
public $debug = FALSE;

function __construct($user = '', $password = '', $baseurl = '', $site = '', $version = '') {
if (!empty($user)) $this->user = $user;
if (!empty($password)) $this->password = $password;
if (!empty($baseurl)) $this->baseurl = $baseurl;
if (!empty($site)) $this->site = $site;
if (!empty($version)) $this->version = $version;
if (!empty($user)) $this->user = $user;
if (!empty($password)) $this->password = $password;
if (!empty($baseurl)) $this->baseurl = $baseurl;
if (!empty($site)) $this->site = $site;
if (!empty($version)) $this->version = $version;
}

function __destruct() {
Expand Down Expand Up @@ -76,9 +76,9 @@ public function login() {

if ($this->debug === TRUE) {
print '<pre>';
print PHP_EOL.'-----LOGIN-------------------'.PHP_EOL;
print PHP_EOL.'-----------LOGIN-------------'.PHP_EOL;
print_r (curl_getinfo($ch));
print PHP_EOL.'-----RESPONSE----------------'.PHP_EOL;
print PHP_EOL.'----------RESPONSE-----------'.PHP_EOL;
print $content;
print PHP_EOL.'-----------------------------'.PHP_EOL;
print '</pre>';
Expand Down Expand Up @@ -1359,12 +1359,12 @@ private function exec_curl($url, $data = '') {

if ($this->debug === TRUE) {
print '<pre>';
print PHP_EOL.'-----cURL INFO---------------'.PHP_EOL;
print PHP_EOL.'---------cURL INFO-----------'.PHP_EOL;
print_r (curl_getinfo($ch));
print PHP_EOL.'-----URL & PAYLOAD-----------'.PHP_EOL;
print PHP_EOL.'-------URL & PAYLOAD---------'.PHP_EOL;
print $url.PHP_EOL;
print $data;
print PHP_EOL.'-----RESPONSE----------------'.PHP_EOL;
print PHP_EOL.'---------RESPONSE------------'.PHP_EOL;
print $content;
print PHP_EOL.'-----------------------------'.PHP_EOL;
print '</pre>';
Expand Down

0 comments on commit a68f210

Please sign in to comment.