Skip to content

Commit

Permalink
API Browser version 1.0.15, API client class version 1.1.4
Browse files Browse the repository at this point in the history
- API client class: added methods/functions upgrade_device(),
upgrade_device_external(), spectrum_scan() and spectrum_scan_state()
- API browser tool: embedded the favicon in base64 encoded format
- API browser tool: added a dynamic "back to top" butoon
- examples: added examples ap_scanning_state.php and
auth_guest_basic.php
  • Loading branch information
malle-pietje committed May 12, 2017
1 parent 8b4bcc2 commit 9cbcb60
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 18 deletions.
35 changes: 35 additions & 0 deletions examples/ap_scanning_state.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* PHP API usage example
*
* contributed by: slooffmaster
* description: example basic PHP script to fetch an Access Point's scanning state/results
*/

/**
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
*/
require_once('config-local.php');

/**
* site id and MAC address of AP to query
*/
$site_id = '<enter your site id here>';
$ap_mac = '<enter MAC address of Access Point to check>';

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

/**
* provide feedback in json format
*/
echo json_encode($data, JSON_PRETTY_PRINT);
48 changes: 48 additions & 0 deletions examples/auth_guest_basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* PHP API usage example
*
* contributed by: slooffmaster
* description: example basic PHP script to perform a basic auth of a guest device
*/

/**
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
*/
require_once('../config.php');

/**
* the MAC address of the device to authorize
*/
$mac = '<enter MAC address of guest device to auth>';

/**
* the duration to authorize the device for in minutes
*/
$duration = 2000;

/**
* The site to authorize the device with
*/
$site_id = '<enter your site id here>';

/**
* 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);
$set_debug_mode = $unifidata->set_debug($debug);
$loginresults = $unifidata->login();

/**
* then we authorize the device for the requested duration
*/
$auth_result = $unifidata->authorize_guest($mac, $duration);

/**
* provide feedback in json format
*/
echo json_encode($auth_result, JSON_PRETTY_PRINT);
4 changes: 2 additions & 2 deletions examples/auth_guest_with_note.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* PHP API usage example
*
* contributed by: slooffmaster
* description: example basic PHP script to auth a guest device and attach a note to it
* description: example basic PHP script to auth a guest device and attach a note to it, this method would normally be used for offline authorization only!
*/

/**
* include the config file (place you credentials etc. there if not already present)
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
Expand Down
2 changes: 1 addition & 1 deletion examples/list_alarms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* include the config file (place you credentials etc. there if not already present)
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
Expand Down
2 changes: 1 addition & 1 deletion examples/list_site_health.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/**
* include the config file (place you credentials etc. there if not already present)
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
Expand Down
2 changes: 1 addition & 1 deletion examples/list_social_auth_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/**
* include the config file (place you credentials etc. there if not already present)
* include the config file (place your credentials etc. there if not already present)
*
* NOTE:
* this example will only work out of the box with a single controller config file!
Expand Down
Binary file removed favicon.ico
Binary file not shown.
51 changes: 46 additions & 5 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.14
* VERSION: 1.0.15
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -20,7 +20,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.14');
define('API_BROWSER_VERSION', '1.0.15');

/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
Expand Down Expand Up @@ -511,11 +511,15 @@ function sites_sort($a, $b)
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- Latest compiled and minified versions of Bootstrap, Font-awesome and Highlight.js CSS, loaded from CDN -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

<!-- Load the appropriate Bootstrap CSS file from CDN -->
<link rel="stylesheet" href="<?php echo $css_url ?>">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.css" integrity="sha256-OhImf+9TMPW5iYXKNT4eRNntf3fCtVYe5jZqo/mrSQA=" crossorigin="anonymous">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="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==">

<!-- custom CSS styling -->
<style>
body {
Expand All @@ -531,7 +535,16 @@ function sites_sort($a, $b)
#output_panel_loading {
color: rgba(0,0,0,.4);
}

.back-to-top {
cursor: pointer;
position: fixed;
bottom: 20px;
right: 20px;
display:none;
}
</style>
<!-- /custom CSS styling -->
</head>
<body>
<!-- top navbar -->
Expand Down Expand Up @@ -755,7 +768,9 @@ function sites_sort($a, $b)
<li id="united"><a href="?theme=united">United</a></li>
<li id="yeti"><a href="?theme=yeti">Yeti</a></li>
<li role="separator" class="divider"></li>
<li id="reset_session" data-toggle="tooltip" data-placement="top" data-original-title="In some cases this will fix login errors (e.g. empty sites list)"><a href="?reset_session=true"><i class="fa fa-refresh"></i> Reset PHP session</a></li>
<li id="reset_session" data-toggle="tooltip" data-placement="top" data-original-title="In some cases this will fix login errors (e.g. empty sites list)">
<a href="?reset_session=true"><i class="fa fa-refresh"></i> Reset PHP session</a>
</li>
<li role="separator" class="divider"></li>
<li id="info" data-toggle="modal" data-target="#about_modal"><a href="#"><i class="fa fa-info-circle"></i> About UniFi API browser</a></li>
</ul>
Expand Down Expand Up @@ -817,6 +832,9 @@ function sites_sort($a, $b)
<?php } ?>
<!-- /data-panel -->
</div>
<!-- Back to Top button element -->
<a id="back-to-top" href="#" class="btn btn-primary back-to-top" role="button" title="Back to top" data-toggle="tooltip" data-placement="left"><i class="fa fa-chevron-up" aria-hidden="true"></i></a>
<!-- /Back to Top button element -->
<!-- Modal -->
<div class="modal fade" id="about_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
Expand Down Expand Up @@ -877,7 +895,6 @@ function sites_sort($a, $b)
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.js" integrity="sha256-yB+xHoEi5PoOnEAgHNbRMIbN4cNtOXAmBzkhNE/tQlI=" crossorigin="anonymous"></script>

<script>
$(document).ready(function() {
/**
Expand Down Expand Up @@ -1032,6 +1049,30 @@ function sites_sort($a, $b)
$('#span_api_browser_update').html('<i class="fa fa-spinner fa-spin fa-fw"></i> checking for updates</span>');
$('#span_api_browser_update').removeClass('label-warning').removeClass('label-danger').addClass('label-success');
})

/**
* manage display of the "back to top" button element
*/
$(window).scroll(function () {
if ($(this).scrollTop() > 50) {
$('#back-to-top').fadeIn();
} else {
$('#back-to-top').fadeOut();
}
});

/**
* scroll body to 0px (top) on click on the "back to top" button
*/
$('#back-to-top').click(function () {
$('#back-to-top').tooltip('hide');
$('body,html').animate({
scrollTop: 0
}, 500);
return false;
});

$('#back-to-top').tooltip('show');
});
</script>
</body>
Expand Down
83 changes: 75 additions & 8 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.3
* VERSION: 1.1.4
*
* NOTES:
* - this class will only work with UniFi Controller versions 4.x and 5.x. There are no checks to prevent
Expand All @@ -31,18 +31,18 @@
* with this package in the file LICENSE.md
*
*/
define('API_CLASS_VERSION', '1.1.3');
define('API_CLASS_VERSION', '1.1.4');

class unifiapi
{
/**
* public properties
*/
public $user = '';
public $password = '';
public $site = 'default';
public $baseurl = 'https://127.0.0.1:8443';
public $version = '4.8.20';
public $user = '';
public $password = '';
public $site = 'default';
public $baseurl = 'https://127.0.0.1:8443';
public $version = '5.4.12';

/**
* private properties
Expand Down Expand Up @@ -1303,8 +1303,75 @@ public function count_alarms($archived = null)
return $this->process_response($content_decoded);
}

/**
* Upgrade a device to the latest firmware
* ---------------------------------------
* return true on success
* required parameter <device_mac> = MAC address of the device to upgrade
*
* NOTES:
* - updates the device to the latest firmware known to the controller
*/
public function upgrade_device($device_mac)
{
if (!$this->is_loggedin) return false;
$json = array('mac' => $device_mac);
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/devmgr/upgrade', 'json='.$json));
return $this->process_response_boolean($content_decoded);
}

/**
* Upgrade a device to a specific firmware file
* --------------------------------------------
* return true on success
* required parameter <firmware_url> = URL for the firmware file to upgrade the device to
* required parameter <device_mac> = MAC address of the device to upgrade
*
* NOTES:
* - updates the device to the firmware file at the given URL
* - please take great care to select a valid firmware file for the device!
*/
public function upgrade_device_external($firmware_url, $device_mac)
{
if (!$this->is_loggedin) return false;
$json = array('url' => $firmware_url, 'mac' => $device_mac);
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/devmgr/upgrade-external', 'json='.$json));
return $this->process_response_boolean($content_decoded);
}

/**
* Trigger an RF scan by an AP
* ---------------------------
* return true on success
* required parameter <ap_mac> = MAC address of the AP
*/
public function spectrum_scan($ap_mac)
{
if (!$this->is_loggedin) return false;
$json = array('cmd' => 'spectrum-scan', 'mac' => $ap_mac);
$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/devmgr', 'json='.$json));
return $this->process_response_boolean($content_decoded);
}

/**
* Check the RF scanning state of an AP
* ------------------------------------
* returns an object with relevant information (results if available) regarding the RF scanning state of the AP
* required parameter <ap_mac> = MAC address of the AP
*/
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);
}

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

Expand Down

0 comments on commit 9cbcb60

Please sign in to comment.