Skip to content

Commit

Permalink
API Browser version 1.0.14, API client class version 1.1.3
Browse files Browse the repository at this point in the history
- API client class: added methods/functions count_alarms(),
list_current_channels(), list_dpi_stats()
- API client class: added getter/setter methods set_debug,
get_last_results_raw and get_last_error_message
- API client class: improved code efficiency when handling responses
- API browser tool: added support for count_alarms(),
list_current_channels(), list_dpi_stats()
- API browser tool: minor code cleanup
- examples: updated examples with new set_debug() method/function
  • Loading branch information
malle-pietje committed Apr 23, 2017
1 parent 50219f5 commit 8b4bcc2
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 106 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ This tool is for browsing data that is exposed through Ubiquiti's UniFi Controll

Please keep the following in mind:
- not all data collections/API endpoints are supported (yet), see the list below of currently supported data collections/API endpoints
- currently supports versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.3.8 has been confirmed to work)
- currently supports versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.4.12 has been confirmed to work)
- there is still work to be done to add/improve functionality and usability of this tool so suggestions/comments are welcome. Please use the github issue list or the Ubiquiti Community forums (https://community.ubnt.com/t5/UniFi-Wireless/UniFi-API-browser-tool-released/m-p/1392651) to share your ideas.
- please read the Security Notice below before installing this tool!

Expand Down Expand Up @@ -61,18 +61,21 @@ The UniFi API browser tool offers the following features:
- list network configuration
- list port configurations
- list port forwarding rules
- list current channels
- list DPI stats
- dynamic DNS configuration
- Messages
- list events
- list alarms
- count alarms

Please note that the bundled API client supports many more API endpoints, not all make sense to add to the API browser though.

### Credits
The PHP API client that comes bundled with this tool is based on the work done by the following developers:
- 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/5.3.8/unifi_sh_api
- and the API as published by Ubiquiti: https://www.ubnt.com/downloads/unifi/5.4.14/unifi_sh_api

Other included libraries:
- Bootstrap (version 3.3.6) http://getbootstrap.com/
Expand Down
5 changes: 3 additions & 2 deletions examples/auth_guest_with_note.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
* load the Unifi API connection class and log in to the controller
*/
require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$loginresults = $unifidata->login();
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();

/**
* To add note to a new device we need to do the following before authorizing the device:
Expand Down
6 changes: 3 additions & 3 deletions examples/extend_guest_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
$site_id = "default";
$site_name = "*enter your site name*";

$unifidata = new unifiapi ($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$unifidata->debug = false;
$loginresults = $unifidata->login();
$unifidata = new unifiapi ($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();

if ($loginresults === 400) {
print "UniFi controller login failure, please check your credentials in config.php.\n";
Expand Down
8 changes: 4 additions & 4 deletions examples/list_alarms.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

/**
* load the Unifi API connection class and log in to the controller and do our thing
* list_alarms()
*/
require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$loginresults = $unifidata->login();
$data = $unifidata->list_alarms();
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();
$data = $unifidata->list_alarms();

/**
* provide feedback in json format
Expand Down
7 changes: 4 additions & 3 deletions examples/list_ap_connected_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
* load the Unifi API connection class and log in to the controller and pull the requested data
*/
require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$loginresults = $unifidata->login();
$aps_array = $unifidata->list_aps();
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();
$aps_array = $unifidata->list_aps();

/**
* output the results in HTML format
Expand Down
7 changes: 4 additions & 3 deletions examples/list_site_health.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
*/
require_once('../phpapi/class.unifi.php');

$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$loginresults = $unifidata->login();
$result = $unifidata->list_health();
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();
$result = $unifidata->list_health();

/**
* output the results in correct json formatting
Expand Down
7 changes: 4 additions & 3 deletions examples/list_social_auth_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
* load the Unifi API connection class and log in to the controller and do our thing
*/
require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$loginresults = $unifidata->login();
$data = $unifidata->stat_payment();
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();
$data = $unifidata->stat_payment();

/**
* cycle through the results and print social auth details if set,
Expand Down
18 changes: 14 additions & 4 deletions examples/toggle_led.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@
* load the Unifi API connection class and log in to the controller to do our thing
*/
require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); // initialize the class instance
$loginresults = $unifidata->login(); // log into the controller
$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); // initialize the class instance
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login(); // log into the controller

$data = $unifidata->set_locate_ap($mac); // uncomment to switch locating on
//$data = $unifidata->unset_locate_ap($mac); // uncomment to switch locating off (choose either of these two lines!)
/**
* using the "old" deprecated methods/functions
*/
//$data = $unifidata->set_locate_ap($mac); // uncomment to switch locating on
//$data = $unifidata->unset_locate_ap($mac); // uncomment to switch locating off (choose either of these two lines!)

/**
* using the new method/function
*/
$data = $unifidata->locate_ap($mac, true); // uncomment to switch locating on
//$data = $unifidata->locate_ap($mac, false); // uncomment to switch locating off (choose either of these two lines!)

if ($data) {
/**
Expand Down
50 changes: 30 additions & 20 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.13
* VERSION: 1.0.14
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -20,7 +20,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.13');
define('API_BROWSER_VERSION', '1.0.14');

/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
Expand Down Expand Up @@ -52,12 +52,9 @@
$site_name = '';
$selection = '';
$output_format = 'json';
$theme = 'bootstrap';
$data = '';
$objects_count = '';
$alert_message = '';
$cookietimeout = '1800';
$debug = false;

/**
* load the configuration file
Expand Down Expand Up @@ -227,9 +224,9 @@
* create a new instance of the API client class and log in to the UniFi controller
* - if an error occurs during the login process, an alert is displayed on the page
*/
$unifidata = new unifiapi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']);
$unifidata->debug = $debug;
$loginresults = $unifidata->login();
$unifidata = new unifiapi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']);
$set_debug_mode = $unifidata->set_debug(trim($debug));
$loginresults = $unifidata->login();

if($loginresults === 400) {
$alert_message = '<div class="alert alert-danger" role="alert">HTTP response status: 400'
Expand Down Expand Up @@ -343,9 +340,17 @@
$data = $unifidata->list_events();
break;
case 'list_alarms':
$selection = 'list alerts';
$selection = 'list alarms';
$data = $unifidata->list_alarms();
break;
case 'count_alarms':
$selection = 'count all alarms';
$data = $unifidata->count_alarms();
break;
case 'count_active_alarms':
$selection = 'count active alarms';
$data = $unifidata->count_alarms(false);
break;
case 'list_wlanconf':
$selection = 'list wlan config';
$data = $unifidata->list_wlanconf();
Expand Down Expand Up @@ -382,6 +387,10 @@
$selection = 'dynamic dns configuration';
$data = $unifidata->list_dynamicdns();
break;
case 'list_current_channels':
$selection = 'current channels';
$data = $unifidata->list_current_channels();
break;
case 'list_portforwarding':
$selection = 'list port forwarding rules';
$data = $unifidata->list_portforwarding();
Expand All @@ -390,6 +399,10 @@
$selection = 'list port forwarding stats';
$data = $unifidata->list_portforward_stats();
break;
case 'list_dpi_stats':
$selection = 'list DPI stats';
$data = $unifidata->list_dpi_stats();
break;
case 'stat_voucher':
$selection = 'list hotspot vouchers';
$data = $unifidata->stat_voucher();
Expand Down Expand Up @@ -432,7 +445,7 @@
if ($theme === 'bootstrap') {
$css_url = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css';
} else {
$css_url = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' . $theme . '/bootstrap.min.css';
$css_url = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' . trim($theme) . '/bootstrap.min.css';
}

/**
Expand Down Expand Up @@ -488,13 +501,6 @@ function sites_sort($a, $b)
{
return strcmp($a->desc, $b->desc);
}

if (isset($_SESSION['controller'])) {
/**
* log off from the UniFi controller API
*/
$logout_results = $unifidata->logout();
}
?>
<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -537,7 +543,7 @@ function sites_sort($a, $b)
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand hidden-sm hidden-md" href="index.php">UniFi API browser</a>
<a class="navbar-brand hidden-sm hidden-md" href="index.php"><i class="fa fa-search fa-fw fa-lg" aria-hidden="true"></i> UniFi API browser</a>
</div>
<div id="navbar-main" class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-left">
Expand Down Expand Up @@ -595,7 +601,7 @@ function sites_sort($a, $b)
<?php } ?>
<!-- /sites dropdown -->
<!-- data collection dropdowns, only show when a site_id is selected -->
<?php if ($site_id) { ?>
<?php if ($site_id && isset($_SESSION['controller'])) { ?>
<li id="output-menu" class="dropdown">
<a id="output-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Output
Expand Down Expand Up @@ -667,6 +673,7 @@ function sites_sort($a, $b)
<?php } ?>
<!-- /site dashboard metrics -->
<li id="list_portforward_stats"><a href="?action=list_portforward_stats">port forwarding stats</a></li>
<li id="list_dpi_stats"><a href="?action=list_dpi_stats">DPI stats</a></li>
</ul>
</li>
<li id="hotspot-menu" class="dropdown">
Expand Down Expand Up @@ -703,6 +710,7 @@ function sites_sort($a, $b)
<li id="list_networkconf"><a href="?action=list_networkconf">list network configuration</a></li>
<li id="list_portconf"><a href="?action=list_portconf">list port configuration</a></li>
<li id="list_portforwarding"><a href="?action=list_portforwarding">list port forwarding rules</a></li>
<li id="list_current_channels"><a href="?action=list_current_channels">list current channels</a></li>
<li id="list_dynamicdns"><a href="?action=list_dynamicdns">dynamic DNS configuration</a></li>
</ul>
</li>
Expand All @@ -713,7 +721,9 @@ function sites_sort($a, $b)
</a>
<ul class="dropdown-menu">
<li class="dropdown-header">Select a data collection</li>
<li id="list_alarms"><a href="?action=list_alarms">list alerts</a></li>
<li id="list_alarms"><a href="?action=list_alarms">list alarms</a></li>
<li id="count_alarms"><a href="?action=count_alarms">count all alarms</a></li>
<li id="count_active_alarms"><a href="?action=count_active_alarms">count active alarms</a></li>
<li id="list_events"><a href="?action=list_events">list events</a></li>
</ul>
</li>
Expand Down
7 changes: 7 additions & 0 deletions phpapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ This directory contains a PHP class, which is based off the work of @domwo and @
The class currently supports the following functions/methods to get/set data through the UniFi controller API:
- login()
- logout()
- set_debug()
- get_last_results_raw()
- get_last_error_message()
- add_site()
- adopt_device()
- authorize_guest()
Expand All @@ -19,6 +21,7 @@ The class currently supports the following functions/methods to get/set data thr
- led_override()
- list_admins()
- list_alarms()
- count_alarms()
- list_aps()
- list_clients()
- list_dashboard()
Expand All @@ -40,6 +43,8 @@ The class currently supports the following functions/methods to get/set data thr
- list_users()
- list_wlan_groups()
- list_wlanconf()
- list_current_channels()
- list_dpi_stats()
- reconnect_sta()
- rename_ap()
- restart_ap()
Expand Down Expand Up @@ -79,6 +84,7 @@ Please refer to the source code for more details on each function/method and it'
A basic example how to use the class:

```php
...
/**
* load the Unifi API connection class, log in to the controller and request the alarms collection
* (this example assumes you have already assigned the correct values to the variables used)
Expand All @@ -87,6 +93,7 @@ require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controller_user, $controller_password, $controller_url, $site_id, $controller_version);
$login = $unifidata->login();
$results = $unifidata->list_alarms(); // returns the alarms in a PHP array
...
```

Have a look at the files in the `examples` directory for more examples how to use this class.
Expand Down
Loading

0 comments on commit 8b4bcc2

Please sign in to comment.