Skip to content

Commit

Permalink
API Browser version 1.0.17, API client class version 1.1.6
Browse files Browse the repository at this point in the history
Minor general code cleanup and some small modifications to pull request
1d44322
  • Loading branch information
malle-pietje committed Jun 9, 2017
1 parent 1d46972 commit 12f9a2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
14 changes: 7 additions & 7 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.16
* VERSION: 1.0.17
*
* ------------------------------------------------------------------------------------
*
Expand All @@ -20,7 +20,7 @@
* with this package in the file LICENSE.md
*
*/
define('API_BROWSER_VERSION', '1.0.16');
define('API_BROWSER_VERSION', '1.0.17');

/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
Expand Down Expand Up @@ -435,7 +435,7 @@
/**
* count the number of objects collected from the UniFi controller
*/
if($action!=''){
if ($action != '') {
$objects_count = count($data);
}

Expand Down Expand Up @@ -856,14 +856,14 @@ function sites_sort($site_a, $site_b)
target="_blank">UniFi API browser on Ubiquiti Community forum</a></div>
</div>
<hr>
<dl class="dl-horizontal col-sm-offset-1">
<dl class="dl-horizontal col-sm-offset-2">
<dt>API browser version</dt>
<dd><span id="span_api_browser_version" class="label label-primary"></span> <span id="span_api_browser_update" class="label label-success"><i class="fa fa-spinner fa-spin fa-fw"></i> checking for updates</span></dd>
<dt>API Class version</dt>
<dt>API client version</dt>
<dd><span id="span_api_class_version" class="label label-primary"></span></dd>
</dl>
<hr>
<dl class="dl-horizontal col-sm-offset-1">
<dl class="dl-horizontal col-sm-offset-2">
<dt>controller user</dt>
<dd><span id="span_controller_user" class="label label-primary"></span></dd>
<dt>controller url</dt>
Expand All @@ -872,7 +872,7 @@ function sites_sort($site_a, $site_b)
<dd><span id="span_controller_version" class="label label-primary"></span></dd>
</dl>
<hr>
<dl class="dl-horizontal col-sm-offset-1">
<dl class="dl-horizontal col-sm-offset-2">
<dt>PHP version</dt>
<dd><span id="span_php_version" class="label label-primary"></span></dd>
<dt>PHP memory_limit</dt>
Expand Down
63 changes: 26 additions & 37 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.5
* VERSION: 1.1.6
*
* 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.5');
define('API_CLASS_VERSION', '1.1.6');

class UnifiApi
{
Expand Down Expand Up @@ -85,6 +85,10 @@ public function login()
curl_setopt($ch, CURLOPT_URL, $this->baseurl.'/api/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['username' => $this->user, 'password' => $this->password]));

if (($content = curl_exec($ch)) === false) {
error_log('cURL error: '.curl_error($ch));
}

if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);

Expand All @@ -97,10 +101,6 @@ public function login()
print '</pre>';
}

if (($content = curl_exec($ch)) === false) {
error_log('cURL error: '.curl_error($ch));
}

$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = trim(substr($content, $header_size));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand Down Expand Up @@ -556,23 +556,22 @@ public function set_usergroup($user_id, $group_id)
return $this->process_response_boolean($content_decoded);
}


/**
* Edit user group
* ---------------
*
* required parameter <group_id> = id of the user group
* required parameter <site_id> = id of the site
* returns an array containing a single object with attributes of the updated usergroup on success
* required parameter <group_id> = id of the user group
* required parameter <site_id> = id of the site
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1)
*
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*
*/
public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1, $group_up = -1)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode(array('_id' => $group_id, 'name' => $group_name, 'qos_rate_max_down' => $group_dn, 'qos_rate_max_up' => $group_up, 'site_id' => $site_id));
$json = json_encode(['_id' => $group_id, 'name' => $group_name, 'qos_rate_max_down' => $group_dn, 'qos_rate_max_up' => $group_up, 'site_id' => $site_id]);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/usergroup/'.trim($group_id), $json));
return $this->process_response($content_decoded);
}
Expand All @@ -582,8 +581,8 @@ public function edit_usergroup($group_id, $site_id, $group_name, $group_dn = -1,
* --------------
* returns an array containing a single object with attributes of the new usergroup ("_id", "name", "qos_rate_max_down", "qos_rate_max_up", "site_id") on success
* required parameter <group_name> = name of the user group
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1)
* optional parameter <group_dn> = limit download bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
* optional parameter <group_up> = limit upload bandwidth in Kbps (default = -1, which sets bandwidth to unlimited)
*/
public function add_usergroup($group_name, $group_dn = -1, $group_up = -1)
{
Expand All @@ -595,8 +594,8 @@ public function add_usergroup($group_name, $group_dn = -1, $group_up = -1)

/**
* Delete user group
* --------------
* returns true on success
* -----------------
* returns true on success
* required parameter <group_id> = id of the user group
*/
public function delete_usergroup($group_id)
Expand Down Expand Up @@ -796,10 +795,9 @@ public function list_networkconf()
public function stat_voucher($create_time = null)
{
if (!$this->is_loggedin) return false;
$json = json_encode([]);
if (trim($create_time) != null) {
$json = json_encode(['create_time' => $create_time]);
} else {
$json = json_encode([]);
}

$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/voucher', 'json='.$json));
Expand All @@ -815,10 +813,9 @@ public function stat_voucher($create_time = null)
public function stat_payment($within = null)
{
if (!$this->is_loggedin) return false;
$url_suffix = '';
if ($within != null) {
$url_suffix = '?within='.$within;
} else {
$url_suffix = '';
}

$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/payment'.$url_suffix));
Expand Down Expand Up @@ -1271,7 +1268,7 @@ public function create_wlan(
public function delete_wlan($wlan_id)
{
if (!$this->is_loggedin) return false;
$json = [];
$json = json_encode([]);
$content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/del/wlanconf/'.trim($wlan_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
Expand Down Expand Up @@ -1523,14 +1520,14 @@ private function process_response($response)
{
$this->last_results_raw = $response;
if (isset($response->meta->rc)) {
if ($response->meta->rc == 'ok') {
if ($response->meta->rc === 'ok') {
$this->last_error_message = null;
if (is_array($response->data)) {
return $response->data;
}

return true;
} elseif ($response->meta->rc == 'error') {
} elseif ($response->meta->rc === 'error') {
/**
* we have an error; set latest_error_message if we have a message
*/
Expand All @@ -1541,11 +1538,7 @@ private function process_response($response)
if ($this->debug) {
error_log('Last error message: ' . $this->last_error_message);
}

return false;
}

return false;
}

return false;
Expand All @@ -1558,10 +1551,10 @@ private function process_response_boolean($response)
{
$this->last_results_raw = $response;
if (isset($response->meta->rc)) {
if ($response->meta->rc == 'ok') {
if ($response->meta->rc === 'ok') {
$this->last_error_message = null;
return true;
} elseif ($response->meta->rc == 'error') {
} elseif ($response->meta->rc === 'error') {
/**
* we have an error:
* set latest_error_message if the returned error message is available
Expand All @@ -1573,11 +1566,7 @@ private function process_response_boolean($response)
if ($this->debug) {
error_log('Last error message: ' . $this->last_error_message);
}

return false;
}

return false;
}

return false;
Expand All @@ -1593,7 +1582,7 @@ private function exec_curl($url, $data = '')

if (trim($data) != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
if ($this->request_type == 'PUT') {
if ($this->request_type === 'PUT') {
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Content-Length: '.strlen($data)]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} else {
Expand All @@ -1602,7 +1591,7 @@ private function exec_curl($url, $data = '')

} else {
curl_setopt($ch, CURLOPT_POST, false);
if ($this->request_type == 'DELETE') {
if ($this->request_type === 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
}
Expand Down

0 comments on commit 12f9a2f

Please sign in to comment.