Skip to content

Commit

Permalink
API browser version 1.0.22, API client class version 1.1.14
Browse files Browse the repository at this point in the history
API browser changes:
code cleanup
added support for the new list_radius_accounts() method function
display current site (if selected) in nav bar

API client changes:
IMPORTANT CHANGE: renamed all functions/methods named add_() to create_()
improved data validation
modified several existing methods/functions to leverage REST endpoints
added create_network() method/function
added set_networksettings_base() method/function
added delete_network() method/function
added list_radius_profiles() method/function
added create_radius_account() method/function
added set_radius_account_base() method/function
added delete_radius_account() method/function
  • Loading branch information
malle-pietje committed Sep 11, 2017
1 parent 2148aae commit 6d681ae
Show file tree
Hide file tree
Showing 4 changed files with 470 additions and 191 deletions.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ If you plan to create your own PHP code levering the UniFi controller API, it is
You will find examples and detailed instructions there.
Please keep the following in mind:
- the API Browser tool doesn't support all available data collections/API endpoints, see the list below of currently supported data collections/API endpoints
- currently, versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.5.20 has been confirmed to work) are supported
- 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.
- the API Browser tool doesn't support all available data collections/API endpoints, see the list below of those that are currently supported
- currently, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.5.20 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](https://github.com/Art-of-WiFi/UniFi-API-browser/issues) 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/questions.
- please read the Security Notice below before installing this tool!
### Donations
If you'd like to support further development of the API browser tool and the PHP API client class, please use the donate button below. All donations go to the project maintainer.
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
### Features
The UniFi API browser tool offers the following features:
Expand Down Expand Up @@ -88,19 +88,6 @@ The UniFi API browser tool offers the following features:
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.5.20/unifi_sh_api
Other included libraries:
- Bootstrap (version 3.3.7) http://getbootstrap.com
- Font-awesome (version 4.7.0) https://fortawesome.github.io/Font-Awesome
- jQuery (version 2.2.4) https://jquery.com
- jQuery JSONView (version 1.2.3) https://github.com/yesmeck/jquery-jsonview
- Kint (version 2.1.2) http://kint-php.github.io/kint
### Requirements
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- network connectivity between this web server and the server (and port) where the UniFi controller is running (in case you are seeing errors, please check out [this issue](https://github.com/Art-of-WiFi/UniFi-API-browser/issues/4))
Expand All @@ -125,6 +112,19 @@ Alternatively you may choose to download the zip file and unzip it in your direc
### Updates
If you have installed the tool using the `git clone` command, you can install updates by going into the directory where the tool has been installed, and running the `git pull` command from there.
### 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.5.20/unifi_sh_api
Other included libraries:
- Bootstrap (version 3.3.7) http://getbootstrap.com
- Font-awesome (version 4.7.0) https://fortawesome.github.io/Font-Awesome
- jQuery (version 2.2.4) https://jquery.com
- jQuery JSONView (version 1.2.3) https://github.com/yesmeck/jquery-jsonview
- Kint (version 2.1.2) http://kint-php.github.io/kint
### Security notice
The use of this tool is **not secured in any way**! Make sure to prevent unauthorised access to it, preventing exposure of details and credentials such as user names and passwords for access to the UniFi Controller!
Expand All @@ -133,20 +133,20 @@ Here are a couple of screenshots of the tool in action.
The controller selection dropdown menu:
![alt tag](https://user-images.githubusercontent.com/12016131/29446799-7081ad00-83ee-11e7-93df-37f31fb28391.PNG "Controller selection")
![alt text](https://user-images.githubusercontent.com/12016131/29446799-7081ad00-83ee-11e7-93df-37f31fb28391.PNG "Controller selection")
The site selection dropdown menu:
![alt tag](https://user-images.githubusercontent.com/12016131/29446798-707f17a2-83ee-11e7-8ea7-c273862787f4.PNG "Site selection")
![alt text](https://user-images.githubusercontent.com/12016131/29446798-707f17a2-83ee-11e7-8ea7-c273862787f4.PNG "Site selection")
Showing the online device data collection using the default Bootstrap theme:
![alt tag](https://user-images.githubusercontent.com/12016131/29446796-7077a76a-83ee-11e7-9b96-aa58209e572d.PNG "Online device collection")
![alt text](https://user-images.githubusercontent.com/12016131/29446796-7077a76a-83ee-11e7-9b96-aa58209e572d.PNG "Online device collection")
With one of the Bootswatch themes selected:
![alt tag](https://user-images.githubusercontent.com/12016131/29446800-708476d4-83ee-11e7-9cfd-c6e18f02a217.PNG "Dark theme selected")
![alt text](https://user-images.githubusercontent.com/12016131/29446800-708476d4-83ee-11e7-9cfd-c6e18f02a217.PNG "Dark theme selected")
The "About" modal:
![alt tag](https://user-images.githubusercontent.com/12016131/29446797-707d1e02-83ee-11e7-8c4a-a80ad08a72a0.PNG "About modal")
![alt text](https://user-images.githubusercontent.com/12016131/29446797-707d1e02-83ee-11e7-8c4a-a80ad08a72a0.PNG "About modal")
10 changes: 3 additions & 7 deletions config.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
$controlleruser = ''; // the user name for access to the UniFi Controller
$controllerpassword = ''; // the password for access to the UniFi Controller
$controllerurl = ''; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
$controllerversion = ''; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)

/**
* Multi controller configuration
Expand All @@ -32,24 +31,21 @@
'user' => '', // the user name for access to the UniFi Controller
'password' => '', // the password for access to the UniFi Controller
'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
'name' => '',
'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
'name' => '' // name for this controller which will be used in the dropdown menu
];
$controllers[1] = [
'user' => '', // the user name for access to the UniFi Controller
'password' => '', // the password for access to the UniFi Controller
'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
'name' => '',
'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
'name' => '' // name for this controller which will be used in the dropdown menu
];
$controllers[2] = [
'user' => '', // the user name for access to the UniFi Controller
'password' => '', // the password for access to the UniFi Controller
'url' => '', // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
'name' => '',
'version' => '' // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
'name' => '' // name for this controller which will be used in the dropdown menu
];
*/

Expand Down
76 changes: 47 additions & 29 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@
$time_start = microtime(true);

/**
* assign variables which are required later on together with their default values
* Declare variables which are required later on together with their default values
*/
$controller_id = '';
$action = '';
$site_id = '';
$site_name = '';
$selection = '';
$output_format = 'json';
$data = '';
$objects_count = '';
$alert_message = '';
$output_format = 'json';

/**
* load the configuration file
* - allows override of several of the previously declared variables
* - if the config.php file is unreadable or does not exist, an alert is displayed on the page
*/
if (!is_readable('config.php')) {
$alert_message = '<div class="alert alert-danger" role="alert">The file <code>config.php</code> is not readable or does not exist.'
. '<br>If you have not yet done so, please copy/rename the <code>config.template.php</code> file to <code>config.php</code> and follow '
. 'the instructions inside to enter your credentials and controller details.</div>';
$alert_message = '<div class="alert alert-danger" role="alert">The file <code>config.php</code> is not readable or does not exist.' .
'<br>If you have not yet done so, please copy/rename the <code>config.template.php</code> file to <code>config.php</code> and follow ' .
'the instructions inside to enter your credentials and controller details.</div>';
} else {
require_once('config.php');
}
Expand Down Expand Up @@ -156,8 +156,7 @@
'user' => $controlleruser,
'password' => $controllerpassword,
'url' => $controllerurl,
'name' => 'Controller',
'version' => $controllerversion
'name' => 'Controller'
];
$controller = $_SESSION['controller'];
}
Expand Down Expand Up @@ -217,18 +216,18 @@
* placed here so they can be overwritten by more "severe" error messages later on
*/
if ($action === '') {
$alert_message = '<div class="alert alert-info" role="alert">Please select a data collection/API endpoint from the dropdown menus'
. ' <i class="fa fa-arrow-circle-up"></i></div>';
$alert_message = '<div class="alert alert-info" role="alert">Please select a data collection/API endpoint from the dropdown menus' .
' <i class="fa fa-arrow-circle-up"></i></div>';
}

if ($site_id === '' && isset($_SESSION['controller'])) {
$alert_message = '<div class="alert alert-info" role="alert">Please select a site from the Sites dropdown menu <i class="fa fa-arrow-circle-up">'
. '</i></div>';
$alert_message = '<div class="alert alert-info" role="alert">Please select a site from the Sites dropdown menu <i class="fa fa-arrow-circle-up">' .
'</i></div>';
}

if (!isset($_SESSION['controller'])) {
$alert_message = '<div class="alert alert-info" role="alert">Please select a controller from the Controllers dropdown menu <i class="fa fa-arrow-circle-up">'
. '</i></div>';
$alert_message = '<div class="alert alert-info" role="alert">Please select a controller from the Controllers dropdown menu <i class="fa fa-arrow-circle-up">' .
'</i></div>';
}

/**
Expand All @@ -239,15 +238,15 @@
* 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 = new UnifiApi($controller['user'], $controller['password'], $controller['url'], $site_id);
$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'
. '<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>';
$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>';

/**
* to prevent unwanted errors we assign empty values to the following variables
Expand All @@ -258,7 +257,7 @@
/**
* Remember authentication cookie to the controller.
*/
$_SESSION['unificookie'] = $unifidata->getcookie();
$_SESSION['unificookie'] = $unifidata->get_cookie();

/**
* Get the list of sites managed by the UniFi controller (if not already stored in the $_SESSION array)
Expand All @@ -270,10 +269,11 @@
} else {
$sites = [];

$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>';
$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, or else check your web server error logs. 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 {
Expand Down Expand Up @@ -468,9 +468,13 @@
$data = $unifidata->list_admins();
break;
case 'list_radius_accounts':
$selection = 'list_radius_accounts';
$selection = 'list radius accounts';
$data = $unifidata->list_radius_accounts();
break;
case 'list_radius_profiles':
$selection = 'list radius profiles';
$data = $unifidata->list_radius_profiles();
break;
default:
break;
}
Expand Down Expand Up @@ -642,14 +646,24 @@ function sites_sort($site_a, $site_b)
<?php if (isset($_SESSION['controller'])) { ?>
<li id="site-menu" class="dropdown">
<a id="site-menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Sites

<?php
/**
* here we display the site name, if selected, else just label it
*/
if (!empty($site_name)) {
echo $site_name;
} else {
echo 'Sites';
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu scrollable-menu" id="siteslist">
<li class="dropdown-header">Select a site</li>
<?php
/**
* here we loop through the available sites, after we have sorted the sites collection
* here we loop through the available sites, after we've sorted the sites collection
*/
usort($sites, "sites_sort");

Expand Down Expand Up @@ -771,16 +785,20 @@ function sites_sort($site_a, $site_b)
<li id="list_admins"><a href="?action=list_admins">list admins for current site</a></li>
<li role="separator" class="divider"></li>
<li id="list_wlanconf"><a href="?action=list_wlanconf">list wlan configuration</a></li>
<li id="list_current_channels"><a href="?action=list_current_channels">list current channels</a></li>
<li role="separator" class="divider"></li>
<li id="list_extension"><a href="?action=list_extension">list VoIP extensions</a></li>
<li role="separator" class="divider"></li>
<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>
<li role="separator" class="divider"></li>
<li id="list_radius_accounts"><a href="?action=list_radius_accounts">list Radius accounts</a></li>
<!-- Radius-related collections, only to be displayed when we have detected a capable controller version -->
<?php if ($detected_controller_version != 'undetected' && version_compare($detected_controller_version, '5.5.19') >= 0) { ?>
<li role="separator" class="divider"></li>
<li id="list_radius_profiles"><a href="?action=list_radius_profiles">list Radius profiles</a></li>
<li id="list_radius_accounts"><a href="?action=list_radius_accounts">list Radius accounts</a></li>
<?php } ?>
</ul>
</li>
<li id="msg-menu" class="dropdown">
Expand Down
Loading

0 comments on commit 6d681ae

Please sign in to comment.