Skip to content

Commit

Permalink
general code clean up and additions to API client class
Browse files Browse the repository at this point in the history
- removed redundant function "get_vouchers", function "stat_voucher"
provides same functionality
- modified function authorize_guest to include optional parameters for
speed and data limits as well as AP MAC address
  • Loading branch information
malle-pietje committed Mar 17, 2016
1 parent f81e7b4 commit 0f83f50
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
3 changes: 3 additions & 0 deletions config.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
$controllerversion = ''; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0)
$cookietimeout = '3600'; // time of inactivity in seconds, after which the PHP session cookie will be refreshed
// this means the site and data collection will need to be selected again
$theme = 'bootstrap'; // your default theme of choice, pick one from the list below
// bootstrap/cerulean/cosmo/cyborg/darkly/flatly/journal/lumen/paper/
// readable/sandstone/simplex/slate/spacelab/superhero/united/yeti
$debug = false; // set to true (without quotes) to enable debug output to the browser and PHP error log
?>
9 changes: 5 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

/*
load the settings 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')) {
Expand Down Expand Up @@ -97,7 +98,7 @@
if (isset($_SESSION['siteid'])) {
$siteid = $_SESSION['siteid'];
$sitename = $_SESSION['sitename'];

if (isset($_GET['action'])) {
$action = $_GET['action'];
$_SESSION['action'] = $action;
Expand All @@ -106,7 +107,7 @@
$action = $_SESSION['action'];
}
}

if (isset($_GET['outputformat'])) {
$outputformat = $_GET['outputformat'];
$_SESSION['outputformat'] = $outputformat;
Expand All @@ -115,7 +116,7 @@
$outputformat = $_SESSION['outputformat'];
}
}

if (isset($_GET['theme'])) {
$theme = $_GET['theme'];
$_SESSION['theme'] = $theme;
Expand Down Expand Up @@ -295,7 +296,7 @@
case 'list_portforward_stats':
$selection = 'list port forwarding stats';
$data = $unifidata->list_portforward_stats();
break;
break;
case 'stat_voucher':
$selection = 'list hotspot vouchers';
$data = $unifidata->stat_voucher();
Expand Down
117 changes: 54 additions & 63 deletions phpapi/class.unifi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
and the API as published by Ubiquiti:
https://dl.ubnt.com/unifi/4.7.6/unifi_sh_api
NOTE: this Class will only work with Unifi Controller versions 4.x. There are no checks to prevent you from
trying to use it with a pre-4.x version controller.
NOTE:
this Class will only work with Unifi Controller versions 4.x. There are no checks to prevent you from
trying to use it with a pre-4.x version controller.
IMPORTANT CHANGES:
- function "list_vouchers" has been removed and has been replaced by "stat_voucher"
------------------------------------------------------------------------------------
The MIT License (MIT)
Expand All @@ -37,13 +41,11 @@
*/
class unifiapi {

public $user = '';
public $password = '';
public $site = 'default';
public $baseurl = 'https://127.0.0.1:8443';
public $version = '4.8.6';

public $is_loggedin = false;
private $cookies = '/tmp/unify_browser';
public $debug = false;
Expand Down Expand Up @@ -129,14 +131,27 @@ public function logout() {
/*
Authorize a MAC address
parameter <MAC address>
parameter <minutes until expires from now>
parameter <minutes> = minutes (from now) until authorization expires
optional parameter <up> = upload speed limit in kbps
optional parameter <down> = download speed limit in kbps
optional parameter <MBytes> = data transfer limit in MB
optional parameter <ap_mac> = AP MAC address to which client is connected, should result in faster authorization
return true on success
*/
public function authorize_guest($mac,$minutes) {
public function authorize_guest($mac, $minutes, $up = NULL, $down = NULL, $MBytes = NULL, $ap_mac = NULL) {
if (!$this->is_loggedin) return false;
$mac = strtolower($mac);
$return = false;
$json = json_encode(array('cmd' => 'authorize-guest', 'mac' => $mac, 'minutes' => $minutes));
$authorize_array = array('cmd' => 'authorize-guest', 'mac' => $mac, 'minutes' => $minutes);

/*
if we have received values for up/down/MBytes we append them to the payload array to be submitted
*/
if (isset($up)) $authorize_array['up'] = $up;
if (isset($down)) $authorize_array['down'] = $down;
if (isset($MBytes)) $authorize_array['bytes'] = $MBytes;
if (isset($ap_mac)) $authorize_array['ap_mac'] = $ap_mac;
$json = json_encode($authorize_array);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/stamgr','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
Expand Down Expand Up @@ -276,8 +291,8 @@ public function stat_hourly_site($start = NULL, $end = NULL) {

/*
hourly stats method for all access points
parameter <start>
parameter <end>
optional parameter <start>
optional parameter <end>
NOTE: defaults to the past 7*24 hours, but unifi controller does not
keep these stats longer than 5 hours (controller v 4.6.6)
*/
Expand All @@ -302,8 +317,8 @@ public function stat_hourly_aps($start = NULL, $end = NULL) {

/*
show all login sessions
parameter <start>
parameter <end>
optional parameter <start>
optional parameter <end>
NOTE: default start value is 7 days ago
*/
public function stat_sessions($start = NULL, $end = NULL) {
Expand All @@ -327,8 +342,8 @@ public function stat_sessions($start = NULL, $end = NULL) {

/*
show all authorizations
parameter <start>
parameter <end>
optional parameter <start>
optional parameter <end>
NOTE: defaults to the past 7*24 hours
*/
public function stat_auths($start = NULL, $end = NULL) {
Expand All @@ -352,7 +367,7 @@ public function stat_auths($start = NULL, $end = NULL) {

/*
get details of all clients ever connected to the site
parameter <historyhours>
optional parameter <historyhours>
NOTE: "within" only allows to select clients that were online in that period.
Stats per client are totals, irrespective of "within" value
defaults to 1 year of history
Expand Down Expand Up @@ -625,12 +640,17 @@ public function list_networkconf() {

/*
stat vouchers
returns an array of hotspot vouchers
optional parameter <create_time>
returns an array of hotspot voucher objects
*/
public function stat_voucher() {
public function stat_voucher($create_time = NULL) {
if (!$this->is_loggedin) return false;
$return = array();
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/voucher'));
$json = json_encode(array());
if (trim($create_time) != NULL) {
$json=json_encode(array('create_time' => $create_time));
}
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/voucher','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
Expand Down Expand Up @@ -1029,7 +1049,6 @@ public function list_wlanconf() {
return $return;
}


/*
list alarms
returns an array of known alarms
Expand All @@ -1051,57 +1070,29 @@ public function list_alarms() {
return $return;
}

/*
list vouchers
parameter <create_time>
returns an array of voucher objects
*/
public function get_vouchers($create_time='') {
if (!$this->is_loggedin) return false;
$return = array();
$json = json_encode(array());
if (trim($create_time) != '') {
$json=json_encode(array('create_time' => $create_time));
}
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/voucher','json='.$json));
if (isset($content_decoded->meta->rc)) {
if ($content_decoded->meta->rc == 'ok') {
if (is_array($content_decoded->data)) {
foreach ($content_decoded->data as $voucher) {
$return[]= $voucher;
}
}
}
}
return $return;
}

/*
create voucher(s)
parameter <minutes>
parameter <minutes> = minutes the voucher is valid after activation
parameter <number_of_vouchers_to_create>
parameter <note>
parameter <up>
parameter <down>
parameter <mb>
returns an array of vouchers codes (Note: without the "-" in the middle)
optional parameter <note> = note text to add to voucher when printing
optional parameter <up> = upload speed limit in kbps
optional parameter <down> = download speed limit in kbps
optional parameter <MBytes> = data transfer limit in MB
returns an array of vouchers codes (NOTE: without the "-" in the middle)
*/
public function create_voucher($minutes,$number_of_vouchers_to_create=1,$note='',$up=0,$down=0,$Mbytes=0) {
public function create_voucher($minutes, $number_of_vouchers_to_create = 1, $note = NULL, $up = NULL, $down = NULL, $MBytes = NULL) {
if (!$this->is_loggedin) return false;
$return = array();
$json = array('cmd' => 'create-voucher', 'expire' => $minutes, 'n' => $number_of_vouchers_to_create);
if (trim($note) != '') {
$json += array('note'=>$note);
}
if ($up > 0) {
$json += array('up'=>$up);
}
if ($down > 0) {
$json += array('down'=>$down);
}
if ($Mbytes > 0) {
$json += array('bytes'=>$Mbytes);
}

/*
if we have received values for note/up/down/MBytes we append them to the payload array to be submitted
*/
if (isset($note)) $json += array('note' => trim($note));
if (isset($up)) $json += array('up' => $up);
if (isset($down)) $json += array('down' => $down);
if (isset($MBytes)) $json += array('bytes' => $MBytes);

$json = json_encode($json);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cmd/hotspot','json='.$json));
if ($content_decoded->meta->rc == 'ok') {
Expand Down

0 comments on commit 0f83f50

Please sign in to comment.