Compare commits

...

17 Commits

Author SHA1 Message Date
malle-pietje
69b43df148 API client class v1.1.57
- increased minimum required PHP version to 5.5.0
- minor syntax improvement based on Scrutinizer feedback
- added create_dynamicdns() and set_dynamicdns() methods, "borrowed" routes from @smos
- added set_element_adoption() method, contributed by @VWT-Dan
- made a start at changing the function/method comments to PHPDoc format (PSR-5) which will support auto-generated class documentation (https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#5-the-phpdoc-format)
- added check to throw an error when the $baseurl ends with a / character, thanks to @infraweavers for submitting #66
- fixed issue with logout() on UDM PROs, thanks go to @Olivier6767 for providing access to a UDM PRO, addresses #63
- applied several code styling improvements
2020-08-22 17:47:50 +02:00
malle-pietje
2b34890a67 - minor syntax improvement based on Scrutinizer feedback
- added create_dynamicdns() and set_dynamicdns() methods, "borrowed" routes from @smos
- made a start at changing the function/method comments to PHPDoc format (PSR-5) which will support auto-generated class documentation
(https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#5-the-phpdoc-format)
2020-06-20 16:12:13 +02:00
malle-pietje
0d99d4a776 API client class v1.1.56
- switched to use of curl_setopt_array instead of curl_setopt which should slightly reduce the overall PHP execution time (https://gist.github.com/masakielastic/3330620)
- several minor code improvements
2020-06-13 12:28:43 +02:00
malle-pietje
89ce2fd8dd - general code clean up
- removed repetitive code (checks using $this->is_loggedin())
- updated the main README to include the latest added functions/methods
2020-06-08 12:52:32 +02:00
malle-pietje
c77913fded API client class v1.1.54
- fixed list_alarms() method/function
2020-06-07 14:23:44 +02:00
malle-pietje
329ad595c9 API client class v1.1.53
- added list_routing() method/function, contributed by @VWT-Dan
- added list_firmware() method/function
- added get_class_version() method/function, returns the (semantic) version number of the Class
- cleaned up the class to remove repetitive code
- changed names of several protected functions for improved consistency
- general code clean up
- minor additions to the main README file
2020-06-07 14:11:52 +02:00
malle-pietje
badb615627 API client class v1.1.52
- fixed exec_curl() for cases where the cookie has expired and we need to re-login
- removed obsolete functions/methods
2020-04-14 14:59:00 +02:00
malle-pietje
49d0986bd7 API client class v1.1.51
- updated README with all getter and setter methods/functions
- updated get_cookies() and get_cookie() for consistency
- get_cookie() remains for backward compatibility only, should not be used in new code
- fix rare cases where the client would end up in a loop, e.g. issuing a request for a site the cached credentials have no
access to
2020-03-19 16:07:31 +01:00
malle-pietje
0ebbb4efac API client class v1.1.50
- removed several if statements
- added getter and setter for unifi_os property
- fixed logout method when working with UniFi OS-based controllers
2020-02-22 17:19:32 +01:00
malle-pietje
903d3e47d5 API client class v1.1.49
- improvements to internal use of $_SESSION['unificookie']
- other minor code improvements
- applies several applied patches as suggested by scrutinizer-ci.com
2020-02-07 08:27:39 +01:00
malle-pietje
e72dea7357 API client class v1.1.48
- applied several patches to Client.php as suggested by scrutinizer-ci.com
- moved resource checks to get_curl_resource() method/function
- extended create_user() function/method with several optional parameters
- changed headers that are passed with each request containing a payload
2020-02-06 11:51:20 +01:00
malle-pietje
17d895076f API client class v1.1.47
- applied patches to update_switch_poe-mode.php as suggested by scrutinizer-ci.com
- code clean up and improvements in preparation of support for UniFi OS-based controllers
- added support for UniFi OS-based controllers, thanks to @Scyto for providing access
- adapt login route and method, and base URL for UniFi OS-based controllers
- automatically identify UniFi OS-based controllers, thanks to @TwitchCaptain for this and several other suggestions
- relaxed URL validation to allow UniFi OS-based controllers to pass
- changed default HTTP method to GET
- many improvements throughout the code
2020-02-06 08:52:49 +01:00
malle-pietje
961d692125 - added support for UniFi OS-based controllers 2020-02-04 14:42:03 +01:00
malle-pietje
796ad9a82d code cleanup and improvements in preparation of support for UniFi OS-based controllers 2020-02-04 07:41:43 +01:00
malle-pietje
a3fc0732e2 API client class v1.1.46
- applied patches to update_switch_poe-mode.php as suggested by scrutinizer-ci.com
- placed warning that UniFI OS is not (yet) supported
2020-01-31 12:14:52 +01:00
malle-pietje
ca25c8ab52 re-added PHP_EOL in several places 2020-01-30 10:14:55 +01:00
malle-pietje
6754eb5041 API client class v1.1.45
- added function/method force_provision(), contributed by @VWT-Dan
- added example update_switch_poe-mode.php, contributed by @Kaltt
2020-01-30 10:09:34 +01:00
7 changed files with 1282 additions and 1499 deletions

View File

@@ -1,21 +1,29 @@
## UniFi Controller API client class ## UniFi Controller API client class
A PHP class which provides access to Ubiquiti's [**UniFi SDN Controller**](https://unifi-sdn.ui.com/) API, versions 4.X.X and 5.X.X of the UniFi SDN Controller software are supported (version 5.12.35 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). A PHP class that provides access to Ubiquiti's [**UniFi SDN Controller**](https://unifi-sdn.ui.com/) API, versions 4.X.X and 5.X.X of the UniFi SDN Controller software are supported (version 5.12.72 has been confirmed to work) as well as UbiOS-based controllers (version 5.12.59 has been confirmed to work). This class is used by our API browser tool which can be found [here](https://github.com/Art-of-WiFi/UniFi-API-browser).
This class can be installed manually or using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects. The package can be installed manually or using composer/[packagist](https://packagist.org/packages/art-of-wifi/unifi-api-client) for easy inclusion in your projects.
## Requirements ## Requirements
- a web server with PHP and cURL modules installed (tested on Apache 2.4 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.2.24 and cURL 7.58.0) - a server with PHP, version 5.5.0 or higher, and the PHP cURL module installed (tested on Apache 2.4 with PHP Version 5.6.1 and cURL 7.42.1 and with PHP 7.2.24 and cURL 7.58.0)
- network connectivity between this web server and the server and port (normally TCP port 8443) where the UniFi Controller is running - direct network connectivity between this server and the host and port (normally TCP port 8443) where the UniFi Controller is running
- you must use **local accounts**, not UniFi Cloud accounts, to access the UniFi Controller API through this class
## Installation ##
## UbiOS Support
Support for UbiOS-based controllers (UniFi Dream Machine Pro) has been added as of version 1.1.47. The class automatically detects UbiOS devices and adjusts URLs and several functions/methods accordingly. If your own code applies strict validation of the URL that is passed to the constructor, please adapt your logic to allow URLs without a port suffix when dealing with a UbiOS-based controller.
Please test all methods you plan on using thoroughly before using the API Client with UbiOS devices in a production environment.
## Installation
You can use [Composer](#composer), [Git](#git) or simply [Download the Release](#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 ### Composer
The preferred method is via [composer](https://getcomposer.org). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed. The preferred installation method is through [composer](https://getcomposer.org). Follow these [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed.
Once composer is installed, simply execute this command from the shell in your project directory: Once composer is installed, simply execute this command from the shell in your project directory:
@@ -94,22 +102,26 @@ Please refer to the `examples/` directory for some more detailed examples which
## Functions/methods supported ## Functions/methods supported
The class currently supports the following functions/methods to GET/POST/PUT/DELETE data through the UniFi Controller API. Please refer to the source code for more details on the functions/methods and their respective parameters. The class currently supports the following functions/methods to GET/POST/PUT/DELETE data through the UniFi Controller API. Please refer to the comments in the source code for more details on the functions/methods and their respective parameters.
- login() - login()
- logout() - logout()
- adopt_device() - adopt_device()
- archive_alarm() - archive_alarm()
- assign_existing_admin()
- authorize_guest() - authorize_guest()
- block_sta() - block_sta()
- cancel_rolling_upgrade()
- cmd_stat()
- count_alarms() - count_alarms()
- create_dynamicdns()
- create_firewallgroup() - create_firewallgroup()
- create_hotspotop() - create_hotspotop()
- create_network() - create_network()
- create_radius_account() - create_radius_account()
- create_site() - create_site()
- create_usergroup()
- create_user() - create_user()
- create_usergroup()
- create_voucher() - create_voucher()
- create_wlan() - create_wlan()
- custom_api_request() - custom_api_request()
@@ -127,12 +139,10 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- extend_guest_validity() - extend_guest_validity()
- forget_sta() (supported on controller version 5.9.X and higher) - forget_sta() (supported on controller version 5.9.X and higher)
- invite_admin() - invite_admin()
- assign_existing_admin()
- revoke_admin()
- led_override() - led_override()
- list_admins() - list_admins()
- list_all_admins()
- list_alarms() - list_alarms()
- list_all_admins()
- list_aps() (deprecated but still available as alias) - list_aps() (deprecated but still available as alias)
- list_backups() - list_backups()
- list_clients() - list_clients()
@@ -145,6 +155,7 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- list_events() - list_events()
- list_extension() - list_extension()
- list_firewallgroups() - list_firewallgroups()
- list_firmware()
- list_guests() - list_guests()
- list_health() - list_health()
- list_hotspotop() - list_hotspotop()
@@ -156,6 +167,7 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- list_radius_accounts() (supported on controller version 5.5.19 and higher) - list_radius_accounts() (supported on controller version 5.5.19 and higher)
- list_radius_profiles() - list_radius_profiles()
- list_rogueaps() - list_rogueaps()
- list_routing()
- list_self() - list_self()
- list_settings() - list_settings()
- list_sites() - list_sites()
@@ -167,14 +179,17 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- locate_ap() - locate_ap()
- move_device() - move_device()
- power_cycle_switch_port() - power_cycle_switch_port()
- reboot_cloudkey()
- reconnect_sta() - reconnect_sta()
- rename_ap() - rename_ap()
- restart_ap() (deprecated but still available as alias) - restart_ap() (deprecated but still available as alias)
- restart_device() - restart_device()
- reboot_cloudkey() - revoke_admin()
- revoke_voucher() - revoke_voucher()
- set_ap_radiosettings() - set_ap_radiosettings()
- set_device_settings_base() - set_device_settings_base()
- set_dynamicdns()
- set_element_adoption() (supported on controller version 5.13.X and higher)
- set_guestlogin_settings() - set_guestlogin_settings()
- set_guestlogin_settings_base() - set_guestlogin_settings_base()
- set_ips_settings_base() (supported on controller version 5.9.10 and higher) - set_ips_settings_base() (supported on controller version 5.9.10 and higher)
@@ -189,11 +204,11 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- set_site_name() - set_site_name()
- set_site_ntp() - set_site_ntp()
- set_site_snmp() - set_site_snmp()
- set_super_mgmt_settings_base()
- set_super_smtp_settings_base()
- set_super_identity_settings_base()
- set_sta_name() - set_sta_name()
- set_sta_note() - set_sta_note()
- set_super_identity_settings_base()
- set_super_mgmt_settings_base()
- set_super_smtp_settings_base()
- set_usergroup() - set_usergroup()
- set_wlan_mac_filter() - set_wlan_mac_filter()
- set_wlansettings() - set_wlansettings()
@@ -203,26 +218,27 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- site_ledson() (deprecated but still available as alias) - site_ledson() (deprecated but still available as alias)
- spectrum_scan() - spectrum_scan()
- spectrum_scan_state() - spectrum_scan_state()
- start_rolling_upgrade()
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
- stat_5minutes_gateway() (supported on controller version 5.7.X and higher)
- stat_5minutes_site() (supported on controller version 5.5.X and higher)
- stat_5minutes_user (supported on controller version 5.7.X and higher)
- stat_allusers() - stat_allusers()
- stat_auths() - stat_auths()
- stat_client() - stat_client()
- stat_5minutes_aps() (supported on controller version 5.5.X and higher)
- stat_hourly_aps()
- stat_daily_aps() - stat_daily_aps()
- stat_5minutes_gateway() (supported on controller version 5.7.X and higher)
- stat_hourly_gateway() (supported on controller version 5.7.X and higher)
- stat_daily_gateway() (supported on controller version 5.7.X and higher) - stat_daily_gateway() (supported on controller version 5.7.X and higher)
- stat_5minutes_site() (supported on controller version 5.5.X and higher)
- stat_hourly_site()
- stat_daily_site() - stat_daily_site()
- stat_5minutes_user (supported on controller version 5.7.X and higher)
- stat_hourly_user() (supported on controller version 5.7.X and higher)
- stat_daily_user() (supported on controller version 5.7.X and higher) - stat_daily_user() (supported on controller version 5.7.X and higher)
- stat_hourly_aps()
- stat_hourly_gateway() (supported on controller version 5.7.X and higher)
- stat_hourly_site()
- stat_hourly_user() (supported on controller version 5.7.X and higher)
- stat_ips_events() (supported on controller version 5.9.10 and higher)
- stat_payment() - stat_payment()
- stat_sessions() - stat_sessions()
- stat_sites() - stat_sites()
- stat_speedtest_results() - stat_speedtest_results()
- stat_ips_events() (supported on controller version 5.9.10 and higher)
- stat_sta_sessions_latest() - stat_sta_sessions_latest()
- stat_status() - stat_status()
- stat_sysinfo() - stat_sysinfo()
@@ -232,19 +248,30 @@ The class currently supports the following functions/methods to GET/POST/PUT/DEL
- unset_locate_ap() (deprecated but still available as alias) - unset_locate_ap() (deprecated but still available as alias)
- upgrade_device() - upgrade_device()
- upgrade_device_external() - upgrade_device_external()
- start_rolling_upgrade()
- cancel_rolling_upgrade()
- cmd_stat()
Internal functions, getters/setters: Other functions, getters/setters:
- set_debug() - get_class_version()
- get_connection_timeout()
- get_cookie() (renamed from getcookie(), deprecated but still available, use get_cookies() instead)
- get_cookies()
- get_debug() - get_debug()
- set_site() - get_is_unifi_os()
- get_site()
- get_cookie() (renamed from getcookie())
- get_last_results_raw()
- get_last_error_message() - get_last_error_message()
- get_last_results_raw()
- get_request_type()
- get_site()
- get_ssl_verify_host()
- get_ssl_verify_peer()
- set_connection_timeout()
- set_cookies()
- set_debug()
- set_is_unifi_os()
- set_request_type()
- set_site()
- set_ssl_verify_host()
- set_ssl_verify_peer()
## Need help or have suggestions? ## Need help or have suggestions?

View File

@@ -20,8 +20,9 @@
} }
], ],
"require": { "require": {
"php": ">=5.4.0", "php": ">=5.5.0",
"ext-curl": "*" "ext-curl": "*",
"ext-json": "*"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@@ -47,7 +47,7 @@ $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $
$set_debug_mode = $unifi_connection->set_debug($debug); $set_debug_mode = $unifi_connection->set_debug($debug);
$loginresults = $unifi_connection->login(); // always true regardless of site id $loginresults = $unifi_connection->login(); // always true regardless of site id
foreach ($macs_to_block as &$mac) { foreach ($macs_to_block as $mac) {
// block_result is always true even if mac address does not exist :( // block_result is always true even if mac address does not exist :(
$block_result = $unifi_connection->block_sta($mac); $block_result = $unifi_connection->block_sta($mac);

View File

@@ -15,8 +15,9 @@
*/ */
$controlleruser = ''; // the user name for access to the UniFi Controller $controlleruser = ''; // the user name for access to the UniFi Controller
$controllerpassword = ''; // the password for access to the UniFi Controller $controllerpassword = ''; // the password for access to the UniFi Controller
$controllerurl = ''; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443' $controllerurl = ''; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443', for UniFi OS-based
$controllerversion = ''; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) // controllers a port suffix isn't required, no trailing slashes should be added
$controllerversion = ''; // the version of the Controller software, e.g. '4.6.6' (must be at least 4.0.0)
/** /**
* set to true (without quotes) to enable debug output to the browser and the PHP error log * set to true (without quotes) to enable debug output to the browser and the PHP error log

View File

@@ -17,7 +17,7 @@ require_once('config.php');
* Check whether the cURL module supports SSL * Check whether the cURL module supports SSL
*/ */
if (!curl_version()['features'] & CURL_VERSION_SSL) { if (!curl_version()['features'] & CURL_VERSION_SSL) {
print 'SSL is not supported with this cURL installation!' . PHP_EOL; print PHP_EOL . 'SSL is not supported with this cURL installation!' . PHP_EOL;
} }
/** /**
@@ -25,8 +25,9 @@ if (!curl_version()['features'] & CURL_VERSION_SSL) {
*/ */
$ch = curl_init(); $ch = curl_init();
if (is_resource($ch)) {
/** /**
* Set the required cURL options * If we have a resource, we proceed and set the required cURL options
*/ */
curl_setopt($ch, CURLOPT_URL, $controllerurl); curl_setopt($ch, CURLOPT_URL, $controllerurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
@@ -50,7 +51,7 @@ curl_setopt($ch, CURLOPT_VERBOSE, true);
* $results contains the output as returned by the cURL request, * $results contains the output as returned by the cURL request,
* returns true when successful, else returns false * returns true when successful, else returns false
*/ */
print 'verbose output from the cURL request:' . PHP_EOL; print PHP_EOL . 'verbose output from the cURL request:' . PHP_EOL;
$results = curl_exec($ch); $results = curl_exec($ch);
print PHP_EOL . 'curl_getinfo output:' . PHP_EOL; print PHP_EOL . 'curl_getinfo output:' . PHP_EOL;
@@ -66,3 +67,6 @@ if (curl_errno($ch)) {
print PHP_EOL . '$results:' . PHP_EOL; print PHP_EOL . '$results:' . PHP_EOL;
print_r($results); print_r($results);
print PHP_EOL; print PHP_EOL;
} else {
print PHP_EOL . 'ERROR: cURL could not be initialized!' . PHP_EOL;
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* PHP API usage example to turn the PoE of the selected switch ports to "off" or "auto"
*
* contributed by: @Kaltt
* description: A use case for this script is to turn off the PoE of the port where a camera is connected in order to turn off the camera
*
* usage: If the file is called via a web URL, it should be called like: update_switch_poe-mode.php?poe_mode=off
* If the file is called via the command line, it should be called like: php update_switch_poe-mode.php off
* The values can be "off" or "auto"
*/
/**
* 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 site to use to log in to the controller
*/
$site_id = '<short site name of a site the credentials used have access to>';
/**
* the MAC address of the AC-IW device to re-configure
*/
$device_mac = '<enter MAC address>';
/**
* $lanports is an array that defines which ports should be changed
*/
$lanports = [6];
/**
* This is the function that reads out the current port configuration and changes the value for the poe_mode for the ports defined in $lanports
*/
function update_ports($running_config, $ports, $poe_mode){
/**
* Update already non-default ports
*/
$running_config_count = count($running_config);
for($i = 0; $i < $running_config_count; $i++){
if(in_array($running_config[$i]->port_idx, $ports)){
$running_config[$i]->poe_mode = $poe_mode;
unset($ports[array_search($running_config[$i]->port_idx, $ports)]);
}
}
$add_conf = [];
foreach($ports as $port){
$add_conf[] = [
'port_idx' => $port,
'poe_mode' => $poe_mode
];
}
return array_merge($running_config, $add_conf);
}
$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, false);
$set_debug_mode = $unifi_connection->set_debug(false);
$loginresults = $unifi_connection->login();
$data = $unifi_connection->list_devices($device_mac);
$device_id = $data[0]->device_id;
$current_conf = $data[0]->port_overrides;
/**
* This reads in the values provided via URL or in the command line, if nothing is set than it will poe_mode will be set to "auto"
*/
if (isset($_GET['poe_mode'])) {
$poe_mode = $_GET['poe_mode'];
} elseif (isset($argv[1])) {
$poe_mode = $argv[1];
} else {
$poe_mode = 'auto';
}
$new_ports_config = [
'port_overrides' => update_ports($current_conf, $lanports, $poe_mode)
];
$update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config);
if (!$update_device) {
$error = $unifi_connection->get_last_results_raw();
echo json_encode($error, JSON_PRETTY_PRINT);
}
echo json_encode($update_device, JSON_PRETTY_PRINT);

File diff suppressed because it is too large Load Diff