Skip to content

Commit

Permalink
API Browser version 1.0.28
Browse files Browse the repository at this point in the history
- include PHP API client class version  1.1.30
- support site names which contain URL-unfriendly characters such as single and double quotes
  • Loading branch information
malle-pietje committed Jul 30, 2018
1 parent c6222d2 commit 2b34c23
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 35 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ Please keep the following in mind:
- 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://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:
- browse data collections/API endpoints exposed by the UniFi Controller API in an easy manner
Expand Down Expand Up @@ -91,7 +86,7 @@ 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.
### Requirements
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.0.7 and cURL 7.37.0)
- 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))
- clients using this tool should have internet access because the CSS and JS files are loaded from CDNs.
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.27');
define('API_BROWSER_VERSION', '1.0.28');
define('API_CLASS_VERSION', get_client_version());

/**
Expand Down Expand Up @@ -711,7 +711,7 @@ function get_client_version()
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";
echo '<li id="' . $site->name . '"><a href="?site_id=' . $site->name . '&site_name=' . htmlspecialchars($site->desc) . '">' . $site->desc . '</a></li>' . "\n";
}
?>
</ul>
Expand Down Expand Up @@ -1037,7 +1037,7 @@ function get_client_version()
var alert_message = '<?php echo $alert_message ?>';
var action = '<?php echo $action ?>';
var site_id = '<?php echo $site_id ?>';
var site_name = '<?php echo $site_name ?>';
var site_name = "<?php echo htmlspecialchars($site_name) ?>";
var controller_id = '<?php echo $controller_id ?>';
var output_format = '<?php echo $output_format ?>';
var selection = '<?php echo $selection ?>';
Expand Down
19 changes: 14 additions & 5 deletions vendor/art-of-wifi/unifi-api-client/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## UniFi Controller API client class

A PHP class which provides access to Ubiquiti's **UniFi Controller API**, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.29 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
A PHP class which provides access to Ubiquiti's **UniFi Controller API**, versions 4.X.X and 5.X.X of the UniFi Controller software are supported (version 5.8.24 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).

This class can be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.

Expand All @@ -20,6 +20,7 @@ The class currently supports the following functions/methods to get/post/put/del
- create_radius_account()
- create_site()
- create_usergroup()
- create_user()
- create_voucher()
- create_wlan()
- delete_device()
Expand All @@ -31,6 +32,9 @@ The class currently supports the following functions/methods to get/post/put/del
- disable_ap()
- edit_usergroup()
- extend_guest_validity()
- forget_sta() (supported on controller version 5.9.X and higher)
- invite_admin()
- revoke_admin()
- led_override()
- list_admins()
- list_all_admins()
Expand Down Expand Up @@ -100,12 +104,15 @@ The class currently supports the following functions/methods to get/post/put/del
- stat_allusers()
- stat_auths()
- stat_client()
- stat_5minutes_aps() (supported on controller version 5.5.* and higher)
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
- stat_hourly_aps()
- stat_daily_aps()
- stat_5minutes_site() (supported on controller version 5.5.* and higher)
- stat_5minutes_site() (supported on controller version 5.5.X and higher)
- stat_hourly_site()
- stat_daily_site()
- stat_5minutes_user (supported on controller version 5.7.X and higher)
- stat_hourly_user() (supported on controller version 5.7.X and higher)
- stat_daily_user() (supported on controller version 5.7.X and higher)
- stat_payment()
- stat_sessions()
- stat_sites()
Expand All @@ -118,6 +125,8 @@ The class currently supports the following functions/methods to get/post/put/del
- unset_locate_ap() (deprecated but still available as alias)
- upgrade_device()
- upgrade_device_external()
- start_rolling_upgrade()
- cancel_rolling_upgrade()

Internal functions, getters/setters:

Expand All @@ -133,7 +142,7 @@ Please refer to the source code for more details on the functions/methods and th

## Requirements

- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.0.7 and cURL 7.37.0)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running

## Installation ##
Expand Down Expand Up @@ -232,7 +241,7 @@ If you would like to contribute code (improvements), please open an issue and in
This class 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://dl.ubnt.com/unifi/5.6.18-8261dc5066/unifi_sh_api
- and the API as published by Ubiquiti: https://dl.ubnt.com/unifi/5.8.24/unifi_sh_api

## Important Disclaimer

Expand Down
90 changes: 90 additions & 0 deletions vendor/art-of-wifi/unifi-api-client/examples/block_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* PHP API usage example
*
* contributed by: @malcolmcif, based on another Art of WiFi example
* description: basic PHP script to block a list of mac addresses passed in via command line,
* output is to console in non json format
*
* usage:
* php block_list.php <list of comma seperated mac addresses>
*
* example:
* php block_list.php 09:09:09:09:09:09,10:10:10:10:10:10
*
*/

/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');

/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example
*/
require_once('config.php');

$debug=false;
/**
* the MAC address(es) of the device(s) to block
*/
$macs_to_block = explode(',',$argv[1]);

/**
* The site to authorize the device with
*/
$site_id = 'MUST_DEFINE_THIS';
if ($site_id == "MUST_DEFINE_THIS")
{
print 'ERROR: set the site id in your script';
return;
}

/**
* initialize the UniFi API connection class and log in to the controller
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login(); // always true regardless of site id

foreach ($macs_to_block as &$mac)
{
// block_result is always true even if mac address does not exist :(
$block_result = $unifi_connection->block_sta($mac);

/**
* NOTE:
* during testing I had some strange behavior where clients were not reconnecting to the network correctly,
* they appeared unblocked and received a valid IP address but could not actually get any data.
* the clients did not come to life until I disabled the SSID and then re enabled it.
* I guessed maybe these commands were occurring too quickly for the controller so I have slowed them down;
* since introducing the sleep I have not seen the above behavior so it might be fixed
*/
sleep(1);

$getid_result = $unifi_connection->stat_client($mac);

if (property_exists($getid_result[0], "oui")) // this field(manufacturer) seems to exist on valid mac addresses
{
if (property_exists($getid_result[0], "name")) // this is the alias field if it has been defined
{
$name = $getid_result[0]->name;
}
else
{
$name = $getid_result[0]->hostname;
}
print 'blocked ' . $name . PHP_EOL;
}
else
{
print 'ERROR: could not block ' . $mac . PHP_EOL;
print ' check mac address is valid and part of your network' . PHP_EOL;
}
}

/**
* No json formatted data
*/
//echo json_encode($block_result, JSON_PRETTY_PRINT);
51 changes: 51 additions & 0 deletions vendor/art-of-wifi/unifi-api-client/examples/list_user_stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* PHP API usage example
*
* contributed by: Art of WiFi
* description: example basic PHP script to pull stats for s epcific user/client device from the UniFi controller and output in json format
*/

/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');

/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example
*/
require_once('config.php');

/**
* the site to use
*/
$site_id = '<enter your site id here>';

/**
* MAC address of client to fetch stats for
*/
$mac = '<MAC address>';

/**
* array of attributes to collect
* valid attributes:
* rx_bytes, tx_bytes, signal, rx_rate, tx_rate, rx_retries, tx_retries, rx_packets, tx_packets
*/
//$attribs = ['rx_bytes', 'tx_bytes', 'signal', 'rx_rate', 'tx_rate', 'rx_retries', 'tx_retries', 'rx_packets', 'tx_packets'];
$attribs = ['rx_bytes', 'tx_bytes'];

/**
* initialize the UniFi API connection class and log in to the controller and do our thing
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, true);
$set_debug_mode = $unifi_connection->set_debug(false);
$loginresults = $unifi_connection->login();
//$data = $unifi_connection->stat_5minutes_user($mac, null, null, $attribs);
//$data = $unifi_connection->stat_hourly_user($mac, null, null, $attribs);
$data = $unifi_connection->stat_daily_user($mac, null, null, $attribs);

/**
* provide feedback in json format
*/
echo json_encode($data, JSON_PRETTY_PRINT);
90 changes: 90 additions & 0 deletions vendor/art-of-wifi/unifi-api-client/examples/unblock_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* PHP API usage example
*
* contributed by: @malcolmcif, based on another Art of WiFi example
* description: basic PHP script to unblock a list of mac addresses passed in via command line,
* output is to console in non json format
*
* usage:
* php unblock_list.php <list of comma seperated mac addresses>
*
* example:
* php unblock_list.php 09:09:09:09:09:09,10:10:10:10:10:10
*
*/

/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');

/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example
*/
require_once('config.php');

$debug=false;
/**
* the MAC addresses of the device(s) to unblock
*/
$macs_to_unblock = explode(',',$argv[1]);

/**
* The site to authorize the device with
*/
$site_id = 'MUST_DEFINE_THIS';
if ($site_id == "MUST_DEFINE_THIS")
{
print 'ERROR: set the site id in your script';
return;
}

/**
* initialize the UniFi API connection class and log in to the controller
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login(); // always true regardless of site id

foreach ($macs_to_unblock as &$mac)
{
// block_result is always true even if mac address does not exist :(
$block_result = $unifi_connection->unblock_sta($mac);

/**
* NOTE:
* during testing I had some strange behavior where clients were not reconnecting to the network correctly,
* they appeared unblocked and received a valid IP address but could not actually get any data.
* the clients did not come to life until I disabled the SSID and then re enabled it.
* I guessed maybe these commands were occurring too quickly for the controller so I have slowed them down;
* since introducing the sleep I have not seen the above behavior so it might be fixed
*/
sleep(1);

$getid_result = $unifi_connection->stat_client($mac);

if (property_exists($getid_result[0], "oui")) // this field(manufacturer) seems to exist on valid mac addresses
{
if (property_exists($getid_result[0], "name"))
{
$name = $getid_result[0]->name;
}
else
{
$name = $getid_result[0]->hostname;
}
print 'unblocked ' . $name . PHP_EOL;
}
else
{
print 'ERROR: could not unblock ' . $mac . PHP_EOL;
print ' check mac address is valid and part of your network' . PHP_EOL;
}
}

/**
* provide feedback in json format
*/
//echo json_encode($block_result, JSON_PRETTY_PRINT);
Loading

0 comments on commit 2b34c23

Please sign in to comment.