Skip to content

Commit

Permalink
API Browser version 1.0.18, API client class version 1.1.7
Browse files Browse the repository at this point in the history
- API client class: clarification of $site_id in the README
- API client class: added method/function list_radius_accounts()
- API browser tool: added support for new list_radius_accounts()
function/method
- API browser tool: added check for php-curl module
- API browser tool: general code cleanup
  • Loading branch information
malle-pietje committed Jul 26, 2017
1 parent 2a72216 commit 4cb1b7b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 32 deletions.
84 changes: 57 additions & 27 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.17
* VERSION: 1.0.18
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -20,7 +20,14 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.17');
define('API_BROWSER_VERSION', '1.0.18');

/**
* check whether the PHP curl module is available
*/
if (!function_exists('curl_version')) {
exit('The <b>PHP curl</b> module is not installed! Please correct this before you proceed!<br>');
}

/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
Expand Down Expand Up @@ -61,7 +68,7 @@
* - 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')) {
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>';
Expand Down Expand Up @@ -228,39 +235,55 @@
$set_debug_mode = $unifidata->set_debug(trim($debug));
$loginresults = $unifidata->login();

if($loginresults === 400) {
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>';
}

/**
* Get the list of sites managed by the UniFi controller (if not already stored in the $_SESSION array)
*/
if (!isset($_SESSION['sites']) || empty($_SESSION['sites'])) {
$sites = $unifidata->list_sites();
$_SESSION['sites'] = $sites;
/**
* to prevent unwanted errors we assign empty values to the following variables
*/
$sites = [];
$detected_controller_version = 'undetected';
} else {
$sites = $_SESSION['sites'];
}
/**
* Get the list of sites managed by the UniFi controller (if not already stored in the $_SESSION array)
*/
if (!isset($_SESSION['sites']) || empty($_SESSION['sites'])) {
$sites = $unifidata->list_sites();
if (is_array($sites)) {
$_SESSION['sites'] = $sites;
} else {
$sites = [];

/**
* Get the version of the UniFi controller (if not already stored in the $_SESSION array or when 'undetected')
*/
if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === 'undetected') {
$site_info = $unifidata->stat_sysinfo();
$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>';
}

if (isset($site_info[0]->version)) {
$detected_controller_version = $site_info[0]->version;
$_SESSION['detected_controller_version'] = $detected_controller_version;
} else {
$detected_controller_version = 'undetected';
$_SESSION['detected_controller_version'] = 'undetected';
$sites = $_SESSION['sites'];
}

} else {
$detected_controller_version = $_SESSION['detected_controller_version'];
/**
* Get the version of the UniFi controller (if not already stored in the $_SESSION array or when 'undetected')
*/
if (!isset($_SESSION['detected_controller_version']) || $_SESSION['detected_controller_version'] === 'undetected') {
$site_info = $unifidata->stat_sysinfo();

if (isset($site_info[0]->version)) {
$detected_controller_version = $site_info[0]->version;
$_SESSION['detected_controller_version'] = $detected_controller_version;
} else {
$detected_controller_version = 'undetected';
$_SESSION['detected_controller_version'] = 'undetected';
}

} else {
$detected_controller_version = $_SESSION['detected_controller_version'];
}
}
}

Expand Down Expand Up @@ -427,6 +450,10 @@
$selection = 'list_admins';
$data = $unifidata->list_admins();
break;
case 'list_radius_accounts':
$selection = 'list_radius_accounts';
$data = $unifidata->list_radius_accounts();
break;
default:
break;
}
Expand Down Expand Up @@ -518,7 +545,7 @@ function sites_sort($site_a, $site_b)
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">

<!-- Load the base64 encoded favicon file -->
<link rel="icon" type="image/x-icon" sizes="16x16" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAADgpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gown/4KQL/+CkDP/enQD/8NOK///////y2Zr/358A/9+fAP/fnwD/358A/9+gAP/foAH/36EB/9+gAP/fnwD/36AA/9+gAv/dmQD/79CB/////////v3//vz4/9+gAP/foAD/36ED/9+hA//engD/3ZsA/92bAP/enAD/36EC/9+hBP/dmgD/8dSM///////+/Pj///////Pbnv/foAD/36EC/96dAf/dmwD/46sc/+i6Rf/ovEr/5bAr/9+fBP/dmgD/8deS///////+/Pb///////LYlf/enAD/36ED/92bAP/jrSf/9eKw//779v/////////////////36MH/9N+q///////+/fn///////HXkv/dmwD/36AD/92bAP/mtj///vv0//////////7////////////////////////////+/fv///////DUiv/dmgD/36AD/9+hAf/iqBv/+/Ti///////+/fr/8tiX/+e3Pf/mszT/7cpx//z36P///v3///////Pbn//cmAD/36EF/9+gAf/foAD/8dWR////////////68Vi/9yWAP/enAL/3pwC/9yXAP/krij//Pbn///////36MP/358A/9+gAf/foAD/36AA//ry2///////9uW7/92aAP/gogX/36ED/9+hA//gowj/3JcA/+3Jc/////////77/+SwK//enAD/36EC/9+gAP/+/Pf//////+7Of//dmgD/4KIE/9+gAP/foAD/36EC/96cAP/lszf////////////ou0n/3ZsA/9+hA//foAD//frz///////w0or/3ZkA/+CiBf/foAD/36AA/9+hA//enAD/5rZA////////////57lE/92bAP/foQP/36AA//jry///////+u/U/9+fB//engD/4KIF/+CiBf/gogX/3JYA//HWk////////frx/+OrHv/enQD/36EC/9+gAP/syG3////////////z253/358C/92ZAP/dmgL/3ZoA/+vEYP/+/fv///////Xgrv/dmwD/36EC/9+gAP/foAD/358C//fmvP////////////rw1v/w0ob/7s16//bluv///v3///////368f/krir/3p0A/9+hAv/foAD/36AA/96eAP/hpRP/9+jA/////////v3///////////////7///////z36f/mtj7/3ZsA/9+hAv/foAD/36AA/9+gAP/foQH/3p4B/96eBf/tyWz/+e3Q//779f/+/fn/+/Pg//HXkv/iqBr/3ZsA/9+hA//foAD/36AA/9+gAP/foAD/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==">
<link rel="icon" type="image/x-icon" sizes="16x16" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAADgpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gpAn/4KQJ/+CkCf/gown/4KQL/+CkDP/enQD/8NOK///////y2Zr/358A/9+fAP/fnwD/358A/9+gAP/foAH/36EB/9+gAP/fnwD/36AA/9+gAv/dmQD/79CB/////////v3//vz4/9+gAP/foAD/36ED/9+hA//engD/3ZsA/92bAP/enAD/36EC/9+hBP/dmgD/8dSM///////+/Pj///////Pbnv/foAD/36EC/96dAf/dmwD/46sc/+i6Rf/ovEr/5bAr/9+fBP/dmgD/8deS///////+/Pb///////LYlf/enAD/36ED/92bAP/jrSf/9eKw//779v/////////////////36MH/9N+q///////+/fn///////HXkv/dmwD/36AD/92bAP/mtj///vv0//////////7////////////////////////////+/fv///////DUiv/dmgD/36AD/9+hAf/iqBv/+/Ti///////+/fr/8tiX/+e3Pf/mszT/7cpx//z36P///v3///////Pbn//cmAD/36EF/9+gAf/foAD/8dWR////////////68Vi/9yWAP/enAL/3pwC/9yXAP/krij//Pbn///////36MP/358A/9+gAf/foAD/36AA//ry2///////9uW7/92aAP/gogX/36ED/9+hA//gowj/3JcA/+3Jc/////////77/+SwK//enAD/36EC/9+gAP/+/Pf//////+7Of//dmgD/4KIE/9+gAP/foAD/36EC/96cAP/lszf////////////ou0n/3ZsA/9+hA//foAD//frz///////w0or/3ZkA/+CiBf/foAD/36AA/9+hA//enAD/5rZA////////////57lE/92bAP/foQP/36AA//jry///////+u/U/9+fB//engD/4KIF/+CiBf/gogX/3JYA//HWk////////frx/+OrHv/enQD/36EC/9+gAP/syG3////////////z253/358C/92ZAP/dmgL/3ZoA/+vEYP/+/fv///////Xgrv/dmwD/36EC/9+gAP/foAD/358C//fmvP////////////rw1v/w0ob/7s16//bluv///v3///////368f/krir/3p0A/9+hAv/foAD/36AA/96eAP/hpRP/9+jA/////////v3///////////////7///////z36f/mtj7/3ZsA/9+hAv/foAD/36AA/9+gAP/foQH/3p4B/96eBf/tyWz/+e3Q//779f/+/fn/+/Pg//HXkv/iqBr/3ZsA/9+hA//foAD/36AA/9+gAP/foAD/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==">

<!-- custom CSS styling -->
<style>
Expand All @@ -530,6 +557,7 @@ function sites_sort($site_a, $site_b)
height: auto;
max-height: 600px;
overflow-x: hidden;
overflow-y: auto;
}

#output_panel_loading {
Expand Down Expand Up @@ -725,6 +753,8 @@ function sites_sort($site_a, $site_b)
<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>
</ul>
</li>
<li id="msg-menu" class="dropdown">
Expand Down Expand Up @@ -1076,4 +1106,4 @@ function sites_sort($site_a, $site_b)
});
</script>
</body>
</html>
</html>
9 changes: 9 additions & 0 deletions phpapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ The class currently supports the following functions/methods to get/set data thr
- list_portconf()
- list_portforward_stats()
- list_portforwarding()
- list_radius_accounts() (supported on controller version 5.5.19 and higher)
- list_rogueaps()
- list_self()
- list_settings()
Expand Down Expand Up @@ -107,6 +108,14 @@ $results = $unifidata->list_alarms(); // returns the alarms in a PHP array
...
```

NOTE: $site_id is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi controller:

```
https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard
```

Here `jl3z2shm` is the value required for $site_id.

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

## Important Disclaimer
Expand Down
24 changes: 19 additions & 5 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.1.6
* VERSION: 1.1.7
*
* NOTES:
* - this class will only work with UniFi Controller versions 4.x and 5.x. There are no checks to prevent
Expand All @@ -34,7 +34,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_CLASS_VERSION', '1.1.6');
define('API_CLASS_VERSION', '1.1.7');

class UnifiApi
{
Expand Down Expand Up @@ -242,7 +242,7 @@ public function unauthorize_guest($mac)
$json = json_encode(['cmd' => 'unauthorize-guest', 'mac' => $mac]);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/stamgr', 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
}

/**
* Reconnect a client device
Expand Down Expand Up @@ -860,7 +860,7 @@ public function list_hotspotop()
* Create voucher(s)
* -----------------
* returns an array of voucher codes (without the dash "-" in the middle) by calling the stat_voucher method
* required parameter <minutes> = minutes the voucher is valid after activation
* required parameter <minutes> = minutes the voucher is valid after activation (expiration time)
* optional parameter <count> = number of vouchers to create, default value is 1
* optional parameter <quota> = single-use or multi-use vouchers, string value '0' is for multi-use, '1' is for single-use,
* "n" is for multi-use n times
Expand Down Expand Up @@ -1427,11 +1427,25 @@ public function spectrum_scan($ap_mac)
public function spectrum_scan_state($ap_mac)
{
if (!$this->is_loggedin) return false;
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/spectrum-scan/'.trim($ap_mac)));
return $this->process_response($content_decoded);
}

/**
* List Radius user accounts
* -------------------------
* returns an array of objects containing all Radius accounts for the current site
*
* NOTES:
* - this function/method is only supported on controller versions 5.5.19 and later
*/
public function list_radius_accounts()
{
if (!$this->is_loggedin) return false;
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/account'));
return $this->process_response($content_decoded);
}

/****************************************************************
* "Aliases" for deprecated functions from here, to support
* backward compatibility:
Expand Down

0 comments on commit 4cb1b7b

Please sign in to comment.