Compare commits

...

8 Commits

Author SHA1 Message Date
malle-pietje
a13ff36409 Merge branch 'master' of github.com:Art-of-WiFi/UniFi-API-client 2017-10-26 17:07:48 +02:00
malle-pietje
c4bc1243eb - minor code cleanup and input validation/sanitization improvements
- added delete_device() function/method
- added move_device() function/method
- added optional `type` parameter to stat_sessions()
- updated README
2017-10-26 17:06:59 +02:00
malle-pietje
c25f4d1937 Update disable_device.php 2017-10-20 09:25:59 +02:00
malle-pietje
e3e70cce72 fixed bug caused by typo with "if (in_array()..." 2017-10-19 11:53:04 +02:00
malle-pietje
449baa70a3 updated README 2017-10-19 11:24:24 +02:00
malle-pietje
2dc5136eba minor code clean up
added archive_alarm() function/method
2017-10-19 11:20:33 +02:00
malle-pietje
e1f3cd6e73 updates to README 2017-10-18 10:09:55 +02:00
malle-pietje
141b0ff55f fixed typo in list_known_rogueaps() 2017-10-18 08:35:11 +02:00
3 changed files with 145 additions and 36 deletions

View File

@@ -1,21 +1,22 @@
## UniFi controller API client class
## UniFi Controller API client class
A PHP class which provides access to Ubiquiti's **UniFi Controller API**. Versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
A PHP class which provides access to Ubiquiti's **UniFi Controller API**, versions 4.x.x and 5.x.x of the UniFi Controller software are supported (version 5.6.18 has been confirmed to work). It's a standalone version of the class which is used in our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
This class can now also be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
This class can be installed using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
### Donations
If you'd like to support further development of this PHP API client class, please use the PayPal donate button below. All donations go to the project maintainer.
If you find this PHP API client class useful and wish to support it's further development, please use the PayPal donate button below. All donations go to the project maintainer.
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=M7TVNVX3Z44VN)
## Methods and functions supported
The class currently supports the following functions/methods to get/post/put/delete data through the UniFi controller API:
The class currently supports the following functions/methods to get/post/put/delete data through the UniFi Controller API:
- login()
- logout()
- adopt_device()
- archive_alarm()
- authorize_guest()
- block_sta()
- count_alarms()
@@ -119,16 +120,16 @@ Internal functions, getters/setters:
- get_last_results_raw()
- get_last_error_message()
Please refer to the source code for more details on each function/method and their parameters.
Please refer to the source code for more details on the functions/methods and their parameters.
## Requirements
- a web server with PHP and cURL modules installed (tested on apache2 with PHP Version 5.6.1 and cURL 7.42.1)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi controller is running
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running
## Installation ##
You can use **Composer**, **Git** or simply **Download the Release** to install the API client class.
You can use [Composer](#composer), [Git](#git) or simply [Download the Release](#download-the-release) to install the API client class.
### Composer
@@ -138,6 +139,16 @@ Once composer is installed, simply execute this command from the shell in your p
```sh
composer require art-of-wifi/unifi-api-client
```
Or you can manually add the package to your composer.json file:
```javascript
{
"require": {
"art-of-wifi/unifi-api-client": "^1.1"
}
}
```
Finally, be sure to include the autoloader in your code:
@@ -189,18 +200,15 @@ $results = $unifi_connection->list_alarms(); // returns a PHP array con
Please refer to the `examples/` directory for some more detailed examples which you can use as a starting point for your own PHP code.
### IMPORTANT NOTES:
#### IMPORTANT NOTES:
In the example above, the last parameter (`true`) that is passed to the constructor, enables validation of the controller's SSL certificate which is otherwise **disabled** by default.
It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.
1. The last parameter (`true`) that is passed to the constructor, enables validation of the controller's SSL certificate which is otherwise **disabled** by default. It is highly recommended to enable this feature in production environments where you have a valid SSL cert installed on the UniFi Controller, and which is associated with the FQDN of the server as used in the `controller_url` parameter. This option was added with API client version 1.1.16.
---
2. In the example above, `$site_id` is the 8 character short site "name" which is visible in the URL when managing the site in the UniFi Controller:
In the example above, `$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`
`https://<controller IP address or FQDN>:8443/manage/site/jl3z2shm/dashboard`
In this case, `jl3z2shm` is the value required for $site_id.
...In this case, `jl3z2shm` is the value required for $site_id.
## Need help or have suggestions?
@@ -219,4 +227,4 @@ This class is largely based on the work done by the following developers:
## Important Disclaimer
Many of the functions in this API client class are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi controller API.
Many of the functions in this API client class are not officially supported by UBNT and as such, may not be supported in future versions of the UniFi Controller API.

50
examples/disable_device.php Executable file
View File

@@ -0,0 +1,50 @@
<?php
/**
* PHP API usage example
*
* contributed by: Art of WiFi
* description: example basic PHP script to disable/enable a device, returns true upon success
*/
/**
* using the composer autoloader
*/
require_once('vendor/autoload.php');
/**
* include the config file (place your credentials etc. there if not already present)
* see the config.template.php file for an example
*/
require_once('config.php');
/**
* the 24 character id of the device to disable/enable
*/
$device_id = '<enter the id of your device here>';
/**
* the site to which the device belongs
*/
$site_id = '<enter your site id here>';
/**
* initialize the UniFi API connection class and log in to the controller
*/
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);
$set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login();
/**
* then we disable the device
*/
$disable_result = $unifi_connection->disable_ap($device_id, true);
/**
* or we enable the device, uncomment as neccesary (then also comment the previous call)
*/
//$disable_result = $unifi_connection->disable_ap($device_id, false);
/**
* provide feedback in json format
*/
echo json_encode($disable_result, JSON_PRETTY_PRINT);

View File

@@ -467,7 +467,7 @@ class Client
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(12*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = $mac;
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/5minutes.ap', 'json='.$json);
return $this->process_response($response);
@@ -491,7 +491,7 @@ class Client
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(7*24*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = $mac;
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/hourly.ap', 'json='.$json);
return $this->process_response($response);
@@ -515,7 +515,7 @@ class Client
$end = is_null($end) ? ((time())*1000) : intval($end);
$start = is_null($start) ? $end-(7*24*3600*1000) : intval($start);
$json = ['attrs' => ['bytes', 'num_sta', 'time'], 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = $mac;
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/report/daily.ap', 'json='.$json);
return $this->process_response($response);
@@ -528,17 +528,19 @@ class Client
* optional parameter <start> = Unix timestamp in seconds
* optional parameter <end> = Unix timestamp in seconds
* optional parameter <mac> = client MAC address to return sessions for (can only be used when start and end are also provided)
* optional parameter <type> = client type to return sessions for, can be 'all', 'guest' or 'user'; default value is 'all'
*
* NOTES:
* - defaults to the past 7*24 hours
*/
public function stat_sessions($start = null, $end = null, $mac = null)
public function stat_sessions($start = null, $end = null, $mac = null, $type = 'all')
{
if (!$this->is_loggedin) return false;
if (!in_array($type, ['all', 'guest', 'user'])) return false;
$end = is_null($end) ? time() : intval($end);
$start = is_null($start) ? $end-(7*24*3600) : intval($start);
$json = ['type'=> 'all', 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = $mac;
$json = ['type'=> $type, 'start' => $start, 'end' => $end];
if (!is_null($mac)) $json['mac'] = strtolower($mac);
$json = json_encode($json);
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/session', 'json='.$json);
return $this->process_response($response);
@@ -803,7 +805,7 @@ class Client
public function list_known_rogueaps()
{
if (!$this->is_loggedin) return false;
$response = $this->exec_curl('/api/s/'.$this->site.'/stat/rogueknown');
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/rogueknown');
return $this->process_response($response);
}
@@ -889,7 +891,7 @@ class Client
}
/**
* List sysinfo
* Show sysinfo
* ------------
* returns an array of known sysinfo data
*/
@@ -901,14 +903,17 @@ class Client
}
/**
* List controller status
* ----------------------
* returns an array containing general controller status info
* Get controller status
* ---------------------
* returns true upon success (controller is online)
*
* NOTES: in order to get useful results (e.g. controller version) you can call get_last_results_raw()
* immediately after this method
*/
public function stat_status()
{
$response = $this->exec_curl('/status');
return $this->process_response($response);
return $this->process_response_boolean($response);
}
/**
@@ -1272,7 +1277,7 @@ class Client
*/
public function set_ap_wlangroup($wlantype_id, $device_id, $wlangroup_id) {
if (!$this->is_loggedin) return false;
if (in_array($wlantype_id, ['ng', 'na'])) return false;
if (!in_array($wlantype_id, ['ng', 'na'])) return false;
$json = json_encode(['wlan_overrides' => [],'wlangroup_id_'.$wlantype_id => $wlangroup_id]);
$response = $this->exec_curl('/api/s/'.$this->site.'/upd/device/'.trim($device_id),'json='.$json);
return $this->process_response_boolean($response);
@@ -1335,6 +1340,35 @@ class Client
return $this->process_response_boolean($response);
}
/**
* Move a device to another site
* -----------------------------
* return true on success
* required parameter <mac> = string; MAC address of the device to move
* required parameter <site_id> = 24 char string; _id of the site to move the device to
*/
public function move_device($mac, $site_id)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['site' => $site_id, 'mac' => $mac, 'cmd' => 'move-device']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Delete a device from the current site
* -------------------------------------
* return true on success
* required parameter <mac> = string; MAC address of the device to delete
*/
public function delete_device($mac)
{
if (!$this->is_loggedin) return false;
$json = json_encode(['mac' => $mac, 'cmd' => 'delete-device']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/sitemgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* List network settings (using REST)
* ----------------------------------
@@ -1360,7 +1394,7 @@ class Client
if (!$this->is_loggedin) return false;
$this->request_type = 'POST';
$json = json_encode($network_settings);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/networkconf/', 'json='.$json);
$response = $this->exec_curl('/api/s/'.$this->site.'/rest/networkconf', 'json='.$json);
return $this->process_response($response);
}
@@ -1499,7 +1533,7 @@ class Client
*/
public function set_wlansettings($wlan_id, $x_passphrase, $name = null)
{
$payload = new \stdClass();
$payload = (object)[];
if (!is_null($x_passphrase)) $payload->x_passphrase = trim($x_passphrase);
if (!is_null($name)) $payload->name = trim($name);
return $this->set_wlansettings_base($wlan_id, $payload);
@@ -1514,7 +1548,7 @@ class Client
*/
public function disable_wlan($wlan_id, $disable)
{
$payload = new \stdClass();
$payload = (object)[];
$action = ($disable) ? false : true;
$payload->enabled = (bool)$action;
return $this->set_wlansettings_base($wlan_id, $payload);
@@ -1541,14 +1575,14 @@ class Client
* required parameter <wlan_id>
* required parameter <mac_filter_policy> = string, "allow" or "deny"; default MAC policy to apply
* required parameter <mac_filter_enabled> = boolean; true enables the policy, false disables it
* required parameter <macs> = array; must contain MAC strings to be placed in the MAC filter list,
* required parameter <macs> = array; must contain valid MAC strings to be placed in the MAC filter list,
* replacing existing values. Existing MAC filter list can be obtained
* through list_wlanconf().
*/
public function set_wlan_mac_filter($wlan_id, $mac_filter_policy, $mac_filter_enabled, array $macs)
{
if (in_array($mac_filter_policy, ['allow', 'deny'])) return false;
$payload = new \stdClass();
if (!in_array($mac_filter_policy, ['allow', 'deny'])) return false;
$payload = (object)[];
$payload->mac_filter_enabled = (bool)$mac_filter_enabled;
$payload->mac_filter_policy = $mac_filter_policy;
$payload->mac_filter_list = $macs;
@@ -1598,6 +1632,23 @@ class Client
return $this->process_response($response);
}
/**
* Archive alarms(s)
* -----------------
* return true on success
* optional parameter <alarm_id> = 24 char string; _id of the alarm to archive which can be found with the list_alarms() function,
* if not provided, *all* un-archived alarms for the current site will be archived!
*/
public function archive_alarm($alarm_id = null)
{
if (!$this->is_loggedin) return false;
$this->request_type = 'POST';
$json = json_encode(['cmd' => 'archive-all-alarms']);
if (!is_null($alarm_id)) $json = json_encode(['_id' => $alarm_id, 'cmd' => 'archive-alarm']);
$response = $this->exec_curl('/api/s/'.$this->site.'/cmd/evtmgr', 'json='.$json);
return $this->process_response_boolean($response);
}
/**
* Upgrade a device to the latest firmware
* ---------------------------------------