diff --git a/README.md b/README.md
index 6558114..f9dd11f 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ This tool is for browsing data that is exposed through Ubiquiti's UniFi Controll
Please keep the following in mind:
- not all data collections/API endpoints are supported (yet), see the list below of currently supported data collections/API endpoints
-- currently supports versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.3.8 has been confirmed to work)
+- currently supports versions 4.x.x and 5.x.x of the UniFi Controller software (version 5.4.12 has been confirmed to work)
- 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 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.
- please read the Security Notice below before installing this tool!
@@ -61,10 +61,13 @@ The UniFi API browser tool offers the following features:
- list network configuration
- list port configurations
- list port forwarding rules
+ - list current channels
+ - list DPI stats
- dynamic DNS configuration
- Messages
- list events
- list alarms
+ - count alarms
Please note that the bundled API client supports many more API endpoints, not all make sense to add to the API browser though.
@@ -72,7 +75,7 @@ Please note that the bundled API client supports many more API endpoints, not al
The PHP API client that comes bundled with this tool 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://www.ubnt.com/downloads/unifi/5.3.8/unifi_sh_api
+- and the API as published by Ubiquiti: https://www.ubnt.com/downloads/unifi/5.4.14/unifi_sh_api
Other included libraries:
- Bootstrap (version 3.3.6) http://getbootstrap.com/
diff --git a/examples/auth_guest_with_note.php b/examples/auth_guest_with_note.php
index 4303d84..d26f523 100644
--- a/examples/auth_guest_with_note.php
+++ b/examples/auth_guest_with_note.php
@@ -38,8 +38,9 @@
* 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);
-$loginresults = $unifidata->login();
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
/**
* To add note to a new device we need to do the following before authorizing the device:
diff --git a/examples/extend_guest_auth.php b/examples/extend_guest_auth.php
index 25961f5..6909b4c 100644
--- a/examples/extend_guest_auth.php
+++ b/examples/extend_guest_auth.php
@@ -13,9 +13,9 @@
$site_id = "default";
$site_name = "*enter your site name*";
-$unifidata = new unifiapi ($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
-$unifidata->debug = false;
-$loginresults = $unifidata->login();
+$unifidata = new unifiapi ($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
if ($loginresults === 400) {
print "UniFi controller login failure, please check your credentials in config.php.\n";
diff --git a/examples/list_alarms.php b/examples/list_alarms.php
index 24c9754..ef251bb 100644
--- a/examples/list_alarms.php
+++ b/examples/list_alarms.php
@@ -21,12 +21,12 @@
/**
* load the Unifi API connection class and log in to the controller and do our thing
- * list_alarms()
*/
require_once('../phpapi/class.unifi.php');
-$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
-$loginresults = $unifidata->login();
-$data = $unifidata->list_alarms();
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
+$data = $unifidata->list_alarms();
/**
* provide feedback in json format
diff --git a/examples/list_ap_connected_users.php b/examples/list_ap_connected_users.php
index d3d48af..3dc325a 100644
--- a/examples/list_ap_connected_users.php
+++ b/examples/list_ap_connected_users.php
@@ -24,9 +24,10 @@
* load the Unifi API connection class and log in to the controller and pull the requested data
*/
require_once('../phpapi/class.unifi.php');
-$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
-$loginresults = $unifidata->login();
-$aps_array = $unifidata->list_aps();
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
+$aps_array = $unifidata->list_aps();
/**
* output the results in HTML format
diff --git a/examples/list_site_health.php b/examples/list_site_health.php
index 5eddcb7..1926155 100644
--- a/examples/list_site_health.php
+++ b/examples/list_site_health.php
@@ -25,9 +25,10 @@
*/
require_once('../phpapi/class.unifi.php');
-$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
-$loginresults = $unifidata->login();
-$result = $unifidata->list_health();
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
+$result = $unifidata->list_health();
/**
* output the results in correct json formatting
diff --git a/examples/list_social_auth_details.php b/examples/list_social_auth_details.php
index c660fa6..320b12e 100644
--- a/examples/list_social_auth_details.php
+++ b/examples/list_social_auth_details.php
@@ -24,9 +24,10 @@
* load the Unifi API connection class and log in to the controller and do our thing
*/
require_once('../phpapi/class.unifi.php');
-$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
-$loginresults = $unifidata->login();
-$data = $unifidata->stat_payment();
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login();
+$data = $unifidata->stat_payment();
/**
* cycle through the results and print social auth details if set,
diff --git a/examples/toggle_led.php b/examples/toggle_led.php
index 771b24c..b10b1d6 100644
--- a/examples/toggle_led.php
+++ b/examples/toggle_led.php
@@ -29,11 +29,21 @@
* load the Unifi API connection class and log in to the controller to do our thing
*/
require_once('../phpapi/class.unifi.php');
-$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); // initialize the class instance
-$loginresults = $unifidata->login(); // log into the controller
+$unifidata = new unifiapi($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); // initialize the class instance
+$set_debug_mode = $unifidata->set_debug($debug);
+$loginresults = $unifidata->login(); // log into the controller
-$data = $unifidata->set_locate_ap($mac); // uncomment to switch locating on
-//$data = $unifidata->unset_locate_ap($mac); // uncomment to switch locating off (choose either of these two lines!)
+/**
+ * using the "old" deprecated methods/functions
+ */
+//$data = $unifidata->set_locate_ap($mac); // uncomment to switch locating on
+//$data = $unifidata->unset_locate_ap($mac); // uncomment to switch locating off (choose either of these two lines!)
+
+/**
+ * using the new method/function
+ */
+$data = $unifidata->locate_ap($mac, true); // uncomment to switch locating on
+//$data = $unifidata->locate_ap($mac, false); // uncomment to switch locating off (choose either of these two lines!)
if ($data) {
/**
diff --git a/index.php b/index.php
index bc49559..0428728 100644
--- a/index.php
+++ b/index.php
@@ -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.13
+ * VERSION: 1.0.14
*
* ------------------------------------------------------------------------------------
*
@@ -20,7 +20,7 @@
* with this package in the file LICENSE.md
*
*/
-define('API_BROWSER_VERSION', '1.0.13');
+define('API_BROWSER_VERSION', '1.0.14');
/**
* in order to use the PHP $_SESSION array for temporary storage of variables, session_start() is required
@@ -52,12 +52,9 @@
$site_name = '';
$selection = '';
$output_format = 'json';
-$theme = 'bootstrap';
$data = '';
$objects_count = '';
$alert_message = '';
-$cookietimeout = '1800';
-$debug = false;
/**
* load the configuration file
@@ -227,9 +224,9 @@
* create a new instance of the API client class and log in to the UniFi controller
* - if an error occurs during the login process, an alert is displayed on the page
*/
- $unifidata = new unifiapi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']);
- $unifidata->debug = $debug;
- $loginresults = $unifidata->login();
+ $unifidata = new unifiapi($controller['user'], $controller['password'], $controller['url'], $site_id, $controller['version']);
+ $set_debug_mode = $unifidata->set_debug(trim($debug));
+ $loginresults = $unifidata->login();
if($loginresults === 400) {
$alert_message = '
HTTP response status: 400'
@@ -343,9 +340,17 @@
$data = $unifidata->list_events();
break;
case 'list_alarms':
- $selection = 'list alerts';
+ $selection = 'list alarms';
$data = $unifidata->list_alarms();
break;
+ case 'count_alarms':
+ $selection = 'count all alarms';
+ $data = $unifidata->count_alarms();
+ break;
+ case 'count_active_alarms':
+ $selection = 'count active alarms';
+ $data = $unifidata->count_alarms(false);
+ break;
case 'list_wlanconf':
$selection = 'list wlan config';
$data = $unifidata->list_wlanconf();
@@ -382,6 +387,10 @@
$selection = 'dynamic dns configuration';
$data = $unifidata->list_dynamicdns();
break;
+ case 'list_current_channels':
+ $selection = 'current channels';
+ $data = $unifidata->list_current_channels();
+ break;
case 'list_portforwarding':
$selection = 'list port forwarding rules';
$data = $unifidata->list_portforwarding();
@@ -390,6 +399,10 @@
$selection = 'list port forwarding stats';
$data = $unifidata->list_portforward_stats();
break;
+ case 'list_dpi_stats':
+ $selection = 'list DPI stats';
+ $data = $unifidata->list_dpi_stats();
+ break;
case 'stat_voucher':
$selection = 'list hotspot vouchers';
$data = $unifidata->stat_voucher();
@@ -432,7 +445,7 @@
if ($theme === 'bootstrap') {
$css_url = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css';
} else {
- $css_url = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' . $theme . '/bootstrap.min.css';
+ $css_url = 'https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/' . trim($theme) . '/bootstrap.min.css';
}
/**
@@ -488,13 +501,6 @@ function sites_sort($a, $b)
{
return strcmp($a->desc, $b->desc);
}
-
-if (isset($_SESSION['controller'])) {
- /**
- * log off from the UniFi controller API
- */
- $logout_results = $unifidata->logout();
-}
?>
@@ -537,7 +543,7 @@ function sites_sort($a, $b)
-
UniFi API browser
+
UniFi API browser
@@ -595,7 +601,7 @@ function sites_sort($a, $b)
-
+
list network configuration
list port configuration
list port forwarding rules
+
list current channels
dynamic DNS configuration
@@ -713,7 +721,9 @@ function sites_sort($a, $b)
diff --git a/phpapi/README.md b/phpapi/README.md
index 9bc93c9..5021d4a 100644
--- a/phpapi/README.md
+++ b/phpapi/README.md
@@ -5,7 +5,9 @@ This directory contains a PHP class, which is based off the work of @domwo and @
The class currently supports the following functions/methods to get/set data through the UniFi controller API:
- login()
- logout()
+- set_debug()
- get_last_results_raw()
+- get_last_error_message()
- add_site()
- adopt_device()
- authorize_guest()
@@ -19,6 +21,7 @@ The class currently supports the following functions/methods to get/set data thr
- led_override()
- list_admins()
- list_alarms()
+- count_alarms()
- list_aps()
- list_clients()
- list_dashboard()
@@ -40,6 +43,8 @@ The class currently supports the following functions/methods to get/set data thr
- list_users()
- list_wlan_groups()
- list_wlanconf()
+- list_current_channels()
+- list_dpi_stats()
- reconnect_sta()
- rename_ap()
- restart_ap()
@@ -79,6 +84,7 @@ Please refer to the source code for more details on each function/method and it'
A basic example how to use the class:
```php
+...
/**
* load the Unifi API connection class, log in to the controller and request the alarms collection
* (this example assumes you have already assigned the correct values to the variables used)
@@ -87,6 +93,7 @@ require_once('../phpapi/class.unifi.php');
$unifidata = new unifiapi($controller_user, $controller_password, $controller_url, $site_id, $controller_version);
$login = $unifidata->login();
$results = $unifidata->list_alarms(); // returns the alarms in a PHP array
+...
```
Have a look at the files in the `examples` directory for more examples how to use this class.
diff --git a/phpapi/class.unifi.php b/phpapi/class.unifi.php
index a1b149b..dbd7c4d 100644
--- a/phpapi/class.unifi.php
+++ b/phpapi/class.unifi.php
@@ -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.2
+ * VERSION: 1.1.3
*
* NOTES:
* - this class will only work with UniFi Controller versions 4.x and 5.x. There are no checks to prevent
@@ -31,29 +31,36 @@
* with this package in the file LICENSE.md
*
*/
-define('API_CLASS_VERSION', '1.1.2');
+define('API_CLASS_VERSION', '1.1.3');
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 $debug = false;
+ /**
+ * private properties
+ */
+ private $debug = false;
private $is_loggedin = false;
private $cookies = '';
private $request_type = 'POST';
- private $last_response;
+ private $last_results_raw;
+ private $last_error_message;
function __construct($user = '', $password = '', $baseurl = '', $site = '', $version = '')
{
- if (!empty($user)) $this->user = $user;
- if (!empty($password)) $this->password = $password;
- if (!empty($baseurl)) $this->baseurl = $baseurl;
- if (!empty($site)) $this->site = $site;
- if (!empty($version)) $this->version = $version;
+ if (!empty($user)) $this->user = trim($user);
+ if (!empty($password)) $this->password = trim($password);
+ if (!empty($baseurl)) $this->baseurl = trim($baseurl);
+ if (!empty($site)) $this->site = trim($site);
+ if (!empty($version)) $this->version = trim($version);
}
function __destruct()
@@ -77,7 +84,7 @@ public function login()
curl_setopt($ch, CURLOPT_URL, $this->baseurl.'/api/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('username' => $this->user, 'password' => $this->password)));
- if ($this->debug === true) {
+ if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
@@ -85,7 +92,7 @@ public function login()
error_log('curl error: '.curl_error($ch));
}
- if ($this->debug === true) {
+ if ($this->debug) {
print '
';
print PHP_EOL.'-----------LOGIN-------------'.PHP_EOL;
print_r (curl_getinfo($ch));
@@ -110,6 +117,7 @@ public function login()
$this->is_loggedin = true;
}
}
+
if ($code === 400) {
error_log('we have received an HTTP response status: 400. Probably a controller login failure');
return $code;
@@ -132,6 +140,68 @@ public function logout()
return true;
}
+ /****************************************************************
+ * setter/getter functions from here:
+ ****************************************************************/
+
+ /**
+ * Set debug mode
+ * --------------
+ * sets debug mode to true or false, returns false if a non-boolean parameter was passed
+ * required parameter = boolean; true will enable debug mode, false will disable it
+ */
+ public function set_debug($enable)
+ {
+ if ($enable) {
+ $this->debug = true;
+ } elseif ($enable === false) {
+ $this->debug = false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Get last raw results
+ * --------------------
+ * returns the raw results of the last method called in PHP stdClass Object format by default, returns false if not set
+ * optional parameter = boolean; true will return the results in "pretty printed" json format
+ *
+ * NOTE:
+ * this method can be used to get the full error as returned by the controller
+ */
+ public function get_last_results_raw($return_json = false)
+ {
+ if ($this->last_results_raw != null) {
+ if ($return_json) {
+ return json_encode($this->last_results_raw, JSON_PRETTY_PRINT);
+ } else {
+ return $this->last_results_raw;
+ }
+
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Get last error message
+ * ----------------------
+ * returns the error message of the last method called in PHP stdClass Object format, returns false if not set
+ */
+ public function get_last_error_message()
+ {
+ if (isset($this->last_error_message)) {
+ return $this->last_error_message;
+ } else {
+ return false;
+ }
+ }
+
+ /****************************************************************
+ * Functions to access UniFi controller API routes from here:
+ ****************************************************************/
+
/**
* Authorize a client device
* -------------------------
@@ -236,7 +306,7 @@ public function set_sta_note($user_id, $note = null)
if (!$this->is_loggedin) return false;
$noted = (is_null($note)) || (empty($note)) ? false : true;
$json = json_encode(array('note' => $note, 'noted' => $noted));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.$user_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -254,7 +324,7 @@ public function set_sta_name($user_id, $name = null)
{
if (!$this->is_loggedin) return false;
$json = json_encode(array('name' => $name));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.$user_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -411,7 +481,7 @@ public function stat_auths($start = null, $end = null)
*
* NOTES:
* - is only used to select clients that were online within that period,
- * the returned stats per client are all-time totals, irrespective of the value of
+ * the returned stats per client are all-time totals, irrespective of the value of
*/
public function stat_allusers($historyhours = 8760)
{
@@ -444,7 +514,7 @@ public function list_guests($within = 8760)
public function list_clients($client_mac = null)
{
if (!$this->is_loggedin) return false;
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/sta/'.$client_mac));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/sta/'.trim($client_mac)));
return $this->process_response($content_decoded);
}
@@ -457,7 +527,7 @@ public function list_clients($client_mac = null)
public function stat_client($client_mac)
{
if (!$this->is_loggedin) return false;
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.$client_mac));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/user/'.trim($client_mac)));
return $this->process_response($content_decoded);
}
@@ -484,7 +554,7 @@ public function set_usergroup($user_id, $group_id)
{
if (!$this->is_loggedin) return false;
$json = json_encode(array('usergroup_id' => $group_id));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.$user_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/user/'.trim($user_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -810,6 +880,30 @@ public function list_portforward_stats()
return $this->process_response($content_decoded);
}
+ /**
+ * List DPI stats
+ * --------------
+ * returns an array of DPI stats
+ */
+ public function list_dpi_stats()
+ {
+ if (!$this->is_loggedin) return false;
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/dpi'));
+ return $this->process_response($content_decoded);
+ }
+
+ /**
+ * List current channels
+ * ---------------------
+ * returns an array of currently allowed channels
+ */
+ public function list_current_channels()
+ {
+ if (!$this->is_loggedin) return false;
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/stat/current-channel'));
+ return $this->process_response($content_decoded);
+ }
+
/**
* List port forwarding settings
* -----------------------------
@@ -917,7 +1011,7 @@ public function disable_ap($ap_id, $disable)
if (!$this->is_loggedin) return false;
$this->request_type = 'PUT';
$json = json_encode(array('disabled' => (bool)$disable));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/device/'.$ap_id, $json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/device/'.trim($ap_id), $json));
return $this->process_response_boolean($content_decoded);
}
@@ -926,7 +1020,8 @@ public function disable_ap($ap_id, $disable)
* ------------------------------
* return true on success
* required parameter = 24 char string; value of _id for the device which can be obtained from the device list
- * required parameter = string, off/on/default; "off" will disable the LED of the device, "on" will enable the LED of the device,
+ * required parameter = string, off/on/default; "off" will disable the LED of the device,
+ * "on" will enable the LED of the device,
* "default" will apply the site-wide setting for device LEDs
*
* NOTES:
@@ -939,7 +1034,7 @@ public function led_override($device_id, $override_mode)
$override_mode_options = array("off", "on", "default");
if (in_array($override_mode, $override_mode_options)) {
$json = json_encode(array('led_override' => $override_mode));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/device/'.$device_id, $json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/rest/device/'.trim($device_id), $json));
return $this->process_response_boolean($content_decoded);
} else {
return false;
@@ -996,7 +1091,7 @@ public function set_ap_radiosettings($ap_id, $radio, $channel, $ht, $tx_power_mo
if (!$this->is_loggedin) return false;
$jsonsettings = json_encode(array('radio' => $radio, 'channel' => $channel, 'ht' => $ht, 'tx_power_mode' => $tx_power_mode, 'tx_power' =>$tx_power));
$json = '{"radio_table": ['.$jsonsettings.']}';
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/device/'.$ap_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/device/'.trim($ap_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -1046,7 +1141,7 @@ public function rename_ap($ap_id, $apname)
{
if (!$this->is_loggedin) return false;
$json = json_encode(array('name' => $apname));
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/device/'.$ap_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/device/'.trim($ap_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -1117,7 +1212,7 @@ public function delete_wlan($wlan_id)
{
if (!$this->is_loggedin) return false;
$json = array();
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/del/wlanconf/'.$wlan_id, 'json='.$json));
+ $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);
}
@@ -1134,10 +1229,10 @@ public function set_wlansettings($wlan_id, $x_passphrase, $name = null)
{
if (!$this->is_loggedin) return false;
$json = array();
- if (!is_null($x_passphrase)) $json['x_passphrase'] = $x_passphrase;
- if (!is_null($name)) $json['name'] = $name;
+ if (!is_null($x_passphrase)) $json['x_passphrase'] = trim($x_passphrase);
+ if (!is_null($name)) $json['name'] = trim($name);
$json = json_encode($json);
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.$wlan_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.trim($wlan_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -1154,7 +1249,7 @@ public function disable_wlan($wlan_id, $disable)
$action = ($disable) ? false : true;
$json = array('enabled' => (bool)$action);
$json = json_encode($json);
- $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.$wlan_id, 'json='.$json));
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/upd/wlanconf/'.trim($wlan_id), 'json='.$json));
return $this->process_response_boolean($content_decoded);
}
@@ -1195,26 +1290,17 @@ public function list_alarms()
}
/**
- * Get last raw results
- * --------------------
- * returns the raw results of the last method called, in PHP stdClass Object format, if not set returns false
- * optional parameter = true will return results in "pretty printed" json format
- *
- * NOTES:
- * for example this method can be used to get the original error message when the last used method returns false
+ * Count alarms
+ * ------------
+ * returns an array containing the alarm count
+ * optional parameter = boolean; if true all alarms will be counted, if false only non-archived (active) alarms will be counted
*/
- public function get_last_results_raw($return_json = false)
+ public function count_alarms($archived = null)
{
- if ($this->last_response != null) {
- if ($return_json) {
- return json_encode($this->last_response, JSON_PRETTY_PRINT);
- } else {
- return $this->last_response;
- }
-
- } else {
- return false;
- }
+ if (!$this->is_loggedin) return false;
+ $url_suffix = ($archived === false) ? '?archived=false' : null;
+ $content_decoded = json_decode($this->exec_curl($this->baseurl.'/api/s/'.$this->site.'/cnt/alarm'.$url_suffix));
+ return $this->process_response($content_decoded);
}
/****************************************************************
@@ -1289,25 +1375,34 @@ public function site_ledsoff()
*/
private function process_response($response)
{
- $this->last_response = $response;
+ $this->last_results_raw = $response;
if (isset($response->meta->rc)) {
if ($response->meta->rc == 'ok') {
+ $this->last_error_message = null;
if (is_array($response->data)) {
- $output = array();
- foreach ($response->data as $item) {
- $output[]= $item;
- }
+ return $response->data;
} else {
- $output = true;
+ return true;
+ }
+ } elseif ($response->meta->rc == 'error') {
+ /**
+ * we have an error; set latest_error_message if we have a message
+ */
+ if (isset($response->meta->msg)) {
+ $this->last_error_message = $response->meta->msg;
+ }
+
+ if ($this->debug) {
+ error_log('last error message: ' . $this->last_error_message);
}
+
+ return false;
} else {
- $output = false;
+ return false;
}
} else {
- $output = false;
+ return false;
}
-
- return $output;
}
/**
@@ -1315,15 +1410,31 @@ private function process_response($response)
*/
private function process_response_boolean($response)
{
- $this->last_response = $response;
- $output = false;
+ $this->last_results_raw = $response;
if (isset($response->meta->rc)) {
if ($response->meta->rc == 'ok') {
- $output = true;
+ $this->last_error_message = null;
+ return true;
+ } elseif ($response->meta->rc == 'error') {
+ /**
+ * we have an error:
+ * set latest_error_message if the returned error message is available
+ */
+ if (isset($response->meta->msg)) {
+ $this->last_error_message = $response->meta->msg;
+ }
+
+ if ($this->debug) {
+ error_log('last error message: ' . $this->last_error_message);
+ }
+
+ return false;
+ } else {
+ return false;
}
+ } else {
+ return false;
}
-
- return (bool)$output;
}
/**
@@ -1351,7 +1462,7 @@ private function exec_curl($url, $data = '')
error_log('curl error: '.curl_error($ch));
}
- if ($this->debug === true) {
+ if ($this->debug) {
print '';
print PHP_EOL.'---------cURL INFO-----------'.PHP_EOL;
print_r (curl_getinfo($ch));
@@ -1379,7 +1490,7 @@ private function get_curl_obj()
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if ($this->debug === true) {
+ if ($this->debug) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}