From d66c3ddd75dbd2702c869a6b6d2168430d30c946 Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Mon, 18 Dec 2023 11:11:27 +0100 Subject: [PATCH] API client class v1.1.81 - hotfix to address cookie issues in UniFi OS 3.2.7, reported by @tflatebo - fixed minor typos - minor code reformatting of the examples - starting with this release, cookies are no longer supported when connecting to a UniFi OS-based controller - added set_vlan_to_port.php example, contributed by @SamuelSchnelly, #203 - allow additional parameters in `create_wlan()`'s payload, contributed by @sgrodzicki, #191 --- README.md | 3 + examples/ap_scanning_state.php | 9 +- examples/ap_upgrade_firmware.php | 11 +- examples/auth_guest_basic.php | 13 +- examples/auth_guest_with_note.php | 13 +- examples/block_list.php | 13 +- examples/change_super_mgmt.php | 21 ++- examples/change_wlan_password.php | 15 +- examples/create_site.php | 13 +- examples/create_voucher.php | 13 +- examples/delete_site.php | 13 +- examples/disable_device.php | 13 +- examples/disable_switch_port.php | 9 +- examples/execute_custom_api_request.php | 15 +- examples/extend_guest_auth.php | 16 +- examples/list_alarms.php | 9 +- examples/list_ap_connected_users.php | 15 +- examples/list_connected_users.php | 15 +- examples/list_site_health.php | 16 +- examples/list_sites.php | 15 +- examples/list_social_auth_details.php | 15 +- examples/list_user_stats.php | 15 +- examples/modify_smartpower_pdu_outlet.php | 79 +++++---- examples/reconnect_client.php | 13 +- examples/set_vlan_to_port.php | 153 ++++++++++++++++++ examples/toggle_led.php | 13 +- examples/unblock_list.php | 13 +- examples/update_ac-iw_ports.php | 23 ++- .../update_device_wlan_settings_5.5.X.php | 23 ++- examples/update_switch_poe-mode.php | 28 ++-- src/Client.php | 27 ++-- 31 files changed, 511 insertions(+), 151 deletions(-) create mode 100755 examples/set_vlan_to_port.php diff --git a/README.md b/README.md index 8455db2..593d7f2 100755 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ The class automatically detects UniFi OS-based controllers and adjusts URLs and If your own code implements strict validation of the URL that is passed to the constructor, please adapt your logic to allow URLs without a port suffix or with port 443 when working with a UniFi OS-based controller. +> **IMPORTANT NOTE**: cookies are no longer supported with UniFi OS-based controllers. If your application code does use cookies, + they will be ignored automatically when working with UniFi OS-based controllers. + Please test all methods you plan on using thoroughly before using the API Client with UniFi OS devices in a production environment. diff --git a/examples/ap_scanning_state.php b/examples/ap_scanning_state.php index ab4198c..918ab2a 100755 --- a/examples/ap_scanning_state.php +++ b/examples/ap_scanning_state.php @@ -28,7 +28,14 @@ $ap_mac = ''; * initialize the UniFi API connection class and log in to the controller and do our thing * spectrum_scan_state() */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); $data = $unifi_connection->spectrum_scan_state($ap_mac); diff --git a/examples/ap_upgrade_firmware.php b/examples/ap_upgrade_firmware.php index 086fe47..0704f5d 100755 --- a/examples/ap_upgrade_firmware.php +++ b/examples/ap_upgrade_firmware.php @@ -24,8 +24,15 @@ $device_mac = ''; * initialize the UniFi API connection class, log in to the controller * (this example assumes you have already assigned the correct values in config.php to the variables used) */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, false); -$login = $unifi_connection->login(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$login = $unifi_connection->login(); /** * Run the actual upgrade diff --git a/examples/auth_guest_basic.php b/examples/auth_guest_basic.php index c2b4ffc..5124618 100755 --- a/examples/auth_guest_basic.php +++ b/examples/auth_guest_basic.php @@ -45,9 +45,16 @@ $site_id = ''; /** * 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(); +$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 authorize the device for the requested duration diff --git a/examples/auth_guest_with_note.php b/examples/auth_guest_with_note.php index 9bf5bc4..de8b578 100755 --- a/examples/auth_guest_with_note.php +++ b/examples/auth_guest_with_note.php @@ -42,9 +42,16 @@ $note = 'Note to attach to newly authorized device'; /** * 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(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); /** * we authorize the device for the requested duration and attach the note to it's object diff --git a/examples/block_list.php b/examples/block_list.php index a247b00..9a3b38b 100755 --- a/examples/block_list.php +++ b/examples/block_list.php @@ -42,9 +42,16 @@ if ($site_id == "MUST_DEFINE_THIS") { /** * 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(); // always true regardless of site id +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); // always true regardless of site id foreach ($macs_to_block as $mac) { // block_result is always true even if mac address does not exist :( diff --git a/examples/change_super_mgmt.php b/examples/change_super_mgmt.php index fcb2ef5..8c420ff 100755 --- a/examples/change_super_mgmt.php +++ b/examples/change_super_mgmt.php @@ -25,19 +25,26 @@ $debug = false; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, true); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$site_settings = $unifi_connection->list_settings(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); -$super_mgmt_settings = []; +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$site_settings = $unifi_connection->list_settings(); + +$super_mgmt_settings = []; $super_mgmt_settings_id = ''; if (!empty($site_settings)) { - foreach($site_settings as $section) { + foreach ($site_settings as $section) { echo 'section key: ' . $section->key . PHP_EOL; if ($section->key === 'super_mgmt') { - $super_mgmt_settings = $section; + $super_mgmt_settings = $section; $super_mgmt_settings_id = $section->_id; } } diff --git a/examples/change_wlan_password.php b/examples/change_wlan_password.php index d2ebc8d..51a5c30 100755 --- a/examples/change_wlan_password.php +++ b/examples/change_wlan_password.php @@ -35,10 +35,17 @@ $new_password = ''; /** * 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(); -$results = $unifi_connection->set_wlansettings($wlan_id, $new_password); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$results = $unifi_connection->set_wlansettings($wlan_id, $new_password); /** * provide feedback in json format diff --git a/examples/create_site.php b/examples/create_site.php index d905937..342a3b6 100755 --- a/examples/create_site.php +++ b/examples/create_site.php @@ -30,9 +30,16 @@ $description = 'new site'; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$loginresults = $unifi_connection->login(); -$results = $unifi_connection->create_site($description); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$loginresults = $unifi_connection->login(); +$results = $unifi_connection->create_site($description); /** * provide feedback in json format diff --git a/examples/create_voucher.php b/examples/create_voucher.php index c42c045..2d02ad4 100755 --- a/examples/create_voucher.php +++ b/examples/create_voucher.php @@ -35,9 +35,16 @@ $site_id = ''; /** * 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(); +$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 create the required number of vouchers with the requested expiration value diff --git a/examples/delete_site.php b/examples/delete_site.php index a29fff9..2c83b53 100755 --- a/examples/delete_site.php +++ b/examples/delete_site.php @@ -31,9 +31,16 @@ $site_to_delete = '<_id value of the site>'; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$loginresults = $unifi_connection->login(); -$results = $unifi_connection->delete_site($site_to_delete); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$loginresults = $unifi_connection->login(); +$results = $unifi_connection->delete_site($site_to_delete); /** * provide feedback in json format diff --git a/examples/disable_device.php b/examples/disable_device.php index b36d2eb..5052d43 100755 --- a/examples/disable_device.php +++ b/examples/disable_device.php @@ -30,9 +30,16 @@ $site_id = ''; /** * 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(); +$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 diff --git a/examples/disable_switch_port.php b/examples/disable_switch_port.php index 0755cc5..cab7efb 100755 --- a/examples/disable_switch_port.php +++ b/examples/disable_switch_port.php @@ -46,7 +46,14 @@ $port_conf_id = ''; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, false); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); $data = $unifi_connection->list_devices($device_mac); diff --git a/examples/execute_custom_api_request.php b/examples/execute_custom_api_request.php index 61e0616..0379371 100755 --- a/examples/execute_custom_api_request.php +++ b/examples/execute_custom_api_request.php @@ -35,10 +35,17 @@ $return = 'array'; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$results = $unifi_connection->custom_api_request($url, $request_method, $payload, $return); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$results = $unifi_connection->custom_api_request($url, $request_method, $payload, $return); /** * provide feedback in JSON format or as PHP Object diff --git a/examples/extend_guest_auth.php b/examples/extend_guest_auth.php index be0389c..116bc83 100755 --- a/examples/extend_guest_auth.php +++ b/examples/extend_guest_auth.php @@ -23,9 +23,16 @@ require_once 'config.php'; $site_id = "default"; $site_name = "*enter your site name*"; -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); if ($loginresults === 400) { print "UniFi controller login failure, please check your credentials in config.php.\n"; @@ -36,7 +43,8 @@ if ($loginresults === 400) { * loop thru all known guests */ foreach ($guestlist as $guest) { - print "
" . $guest->_id . " (" . $guest->mac . "), valid until " . date(DATE_ATOM, $guest->end) . " (" . $guest->end . ")
"; + print "
" . $guest->_id . " (" . $guest->mac . "), valid until " .
+              date(DATE_ATOM, $guest->end) . " (" . $guest->end . ")
"; /** * just a sample: only extend validity of guests which have end date after 2017-04-02 diff --git a/examples/list_alarms.php b/examples/list_alarms.php index 38a7550..206a2a7 100755 --- a/examples/list_alarms.php +++ b/examples/list_alarms.php @@ -25,7 +25,14 @@ $site_id = ''; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); $data = $unifi_connection->list_alarms(); diff --git a/examples/list_ap_connected_users.php b/examples/list_ap_connected_users.php index 14c3809..2d1bc82 100755 --- a/examples/list_ap_connected_users.php +++ b/examples/list_ap_connected_users.php @@ -26,10 +26,17 @@ $site_id = ''; /** * initialize the UniFi API connection class and log in to the controller and pull the requested data */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$aps_array = $unifi_connection->list_aps(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$aps_array = $unifi_connection->list_aps(); /** * output the results in HTML format diff --git a/examples/list_connected_users.php b/examples/list_connected_users.php index a41b3e4..8e6f669 100755 --- a/examples/list_connected_users.php +++ b/examples/list_connected_users.php @@ -25,10 +25,17 @@ $site_id = ''; /** * initialize the UniFi API connection class and log in to the controller and pull the requested data */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$clients_array = $unifi_connection->list_clients(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$clients_array = $unifi_connection->list_clients(); /** * output the results in JSON format diff --git a/examples/list_site_health.php b/examples/list_site_health.php index daf03bb..16f1ee9 100755 --- a/examples/list_site_health.php +++ b/examples/list_site_health.php @@ -26,14 +26,20 @@ $site_id = ''; /** * initialize the UniFi API connection class and log in to the controller and pull the requested data */ +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$result = $unifi_connection->list_health(); +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$result = $unifi_connection->list_health(); /** * output the results in correct json formatting */ header('Content-Type: application/json'); -echo (json_encode($result, JSON_PRETTY_PRINT)); \ No newline at end of file +echo(json_encode($result, JSON_PRETTY_PRINT)); \ No newline at end of file diff --git a/examples/list_sites.php b/examples/list_sites.php index 97e89cb..c199e46 100755 --- a/examples/list_sites.php +++ b/examples/list_sites.php @@ -26,10 +26,17 @@ $site_id = 'default'; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$data = $unifi_connection->list_sites(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$data = $unifi_connection->list_sites(); /** * we can render the full results in json format diff --git a/examples/list_social_auth_details.php b/examples/list_social_auth_details.php index 8f9574f..c58ecc6 100755 --- a/examples/list_social_auth_details.php +++ b/examples/list_social_auth_details.php @@ -26,10 +26,17 @@ $site_id = ''; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -$data = $unifi_connection->stat_payment(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +$data = $unifi_connection->stat_payment(); /** * cycle through the results and print social auth details if set, diff --git a/examples/list_user_stats.php b/examples/list_user_stats.php index a4555e0..997cb8c 100755 --- a/examples/list_user_stats.php +++ b/examples/list_user_stats.php @@ -38,12 +38,19 @@ $attribs = ['rx_bytes', 'tx_bytes']; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion, true); -$set_debug_mode = $unifi_connection->set_debug(false); -$loginresults = $unifi_connection->login(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug(false); +$loginresults = $unifi_connection->login(); //$data = $unifi_connection->stat_5minutes_user($mac, null, null, $attribs); //$data = $unifi_connection->stat_hourly_user($mac, null, null, $attribs); -$data = $unifi_connection->stat_daily_user($mac, null, null, $attribs); +$data = $unifi_connection->stat_daily_user($mac, null, null, $attribs); /** * provide feedback in json format diff --git a/examples/modify_smartpower_pdu_outlet.php b/examples/modify_smartpower_pdu_outlet.php index 07b33ba..c6dc7c0 100755 --- a/examples/modify_smartpower_pdu_outlet.php +++ b/examples/modify_smartpower_pdu_outlet.php @@ -49,39 +49,52 @@ $new_cycle_enabled = false; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); -if ($loginresults) { - $pdu_details = $unifi_connection->list_devices($pdu_mac); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); - /** - * change the model below from USPPDUP to UP1 when using a USP-Plug (thanks to @thesohoguy for contributing this) - */ - if (!empty($pdu_details) && property_exists($pdu_details[0], 'model') && $pdu_details[0]->model === 'USPPDUP' && property_exists($pdu_details[0], 'outlet_overrides')) { - $device_id = $pdu_details[0]->_id; - $outlet_overrides = $pdu_details[0]->outlet_overrides; - - foreach ($outlet_overrides as $key => $value) { - if ($value->index === $outlet_idx) { - $outlet_overrides[$key]->relay_state = $new_relay_state; - $outlet_overrides[$key]->cycle_enabled = $new_cycle_enabled; - } - } - - $pdu_update = $unifi_connection->set_device_settings_base($device_id, ['outlet_overrides' => $outlet_overrides]); - - /** - * provide feedback in json format - */ - echo 'results:' . PHP_EOL . PHP_EOL; - echo json_encode($pdu_update, JSON_PRETTY_PRINT); - echo PHP_EOL; - } else { - echo 'not a PDU device?'; - echo PHP_EOL; - } -} else { +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); +if (!$loginresults) { echo 'we encountered a login error!'; echo PHP_EOL; -} \ No newline at end of file + exit(); +} + +$pdu_details = $unifi_connection->list_devices($pdu_mac); + +/** + * change the model below from USPPDUP to UP1 when using a USP-Plug (thanks to @thesohoguy for contributing this) + */ +if ( + !empty($pdu_details) && + property_exists($pdu_details[0], 'model') && + $pdu_details[0]->model === 'USPPDUP' && + property_exists($pdu_details[0], 'outlet_overrides') +) { + $device_id = $pdu_details[0]->_id; + $outlet_overrides = $pdu_details[0]->outlet_overrides; + + foreach ($outlet_overrides as $key => $value) { + if ($value->index === $outlet_idx) { + $outlet_overrides[$key]->relay_state = $new_relay_state; + $outlet_overrides[$key]->cycle_enabled = $new_cycle_enabled; + } + } + + $pdu_update = $unifi_connection->set_device_settings_base($device_id, + ['outlet_overrides' => $outlet_overrides]); + + /** + * provide feedback in json format + */ + echo 'results:' . PHP_EOL . PHP_EOL; + echo json_encode($pdu_update, JSON_PRETTY_PRINT); +} else { + echo 'not a PDU device?'; +} +echo PHP_EOL; \ No newline at end of file diff --git a/examples/reconnect_client.php b/examples/reconnect_client.php index 81d41d9..0e1efdd 100755 --- a/examples/reconnect_client.php +++ b/examples/reconnect_client.php @@ -30,9 +30,16 @@ $site_id = ''; /** * 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(); +$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 force the device to reconnect diff --git a/examples/set_vlan_to_port.php b/examples/set_vlan_to_port.php new file mode 100755 index 0000000..f41964c --- /dev/null +++ b/examples/set_vlan_to_port.php @@ -0,0 +1,153 @@ +'; + +/** + * initialize the UniFi API connection class and log in to the controller and do our thing + */ +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); + +/** + * change VLAN on port + */ +$port = 1; +$vlan = 200; + +/** + * MAC Address of the UniFi device + */ +$mac = ''; + +set_port_vlan($mac, $port, $vlan, $unifi_connection); + +/** + * Set specific VLAN on device port + * + * @param string $device_mac MAC Address of UNIFI device + * @param int $port_idx Port number of UNIFI device, note: started by 1 + * @param int $vlan_id VLAN ID to set + * @param UniFi_API\Client $unifi_connection + * + * @return bool true if result is success, false upon failure + */ +function set_port_vlan($device_mac, $port_idx, $vlan_id, $unifi_connection) +{ + $device = $unifi_connection->list_devices($device_mac); + + // if no device found + if (count($device) == 0) { + trigger_error('set_port_vlan() device not found'); + return false; + } + + $port_table = $device[0]->port_table; + $def_port = null; + + // check if port exists + $exist = false; + foreach ($port_table as $key => $port) { + if ($port->port_idx == $port_idx) { + $exist = true; + $def_port = $port; + break; + } + } + + if (!$exist) { + trigger_error('set_port_vlan() port_idx not found on device'); + return false; + } + + // check if vlan exists + $native_networkconf_id = null; + + foreach ($unifi_connection->list_networkconf() as $key => $network) { + if ($network->purpose == 'vlan-only' && $network->vlan == $vlan_id) { + $native_networkconf_id = $network->_id; + } + } + + if ($native_networkconf_id === null) { + trigger_error('set_port_vlan() vlan not exist'); + return false; + } + + $exist = false; + foreach ($device[0]->port_overrides as $key => $port) { + if ($port->port_idx == $port_idx) { + $exist = true; + $device[0]->port_overrides[$key]->native_networkconf_id = $native_networkconf_id; + break; + } + } + + if (!$exist) { + $device[0]->port_overrides[] = [ + 'port_idx' => isset($port_idx) ? $port_idx : 1, + 'setting_preference' => isset($setting_preference) ? $setting_preference : 'auto', + 'name' => isset($def_port->name) ? $def_port->name : 'Port 1', + 'op_mode' => isset($def_port->op_mode) ? $def_port->op_mode : 'switch', + 'port_security_enabled' => isset($def_port->port_security_enabled) ? $def_port->port_security_enabled : false, + 'port_security_mac_address' => isset($def_port->port_security_mac_address) ? $def_port->port_security_mac_address : [], + 'native_networkconf_id' => isset($native_networkconf_id) ? $native_networkconf_id : '', + 'excluded_networkconf_ids' => isset($def_port->excluded_networkconf_ids) ? $def_port->excluded_networkconf_ids : [], + 'show_traffic_restriction_as_allowlist' => isset($def_port->show_traffic_restriction_as_allowlist) ? $def_port->show_traffic_restriction_as_allowlist : false, + 'forward' => isset($def_port->forward) ? $def_port->forward : 'customize', + 'lldpmed_enabled' => isset($def_port->lldpmed_enabled) ? $def_port->lldpmed_enabled : true, + 'voice_networkconf_id' => isset($def_port->voice_networkconf_id) ? $def_port->voice_networkconf_id : '', + 'stormctrl_bcast_enabled' => isset($def_port->stormctrl_bcast_enabled) ? $def_port->stormctrl_bcast_enabled : false, + 'stormctrl_bcast_rate' => isset($def_port->stormctrl_bcast_rate) ? $def_port->stormctrl_bcast_rate : 100, + 'stormctrl_mcast_enabled' => isset($def_port->stormctrl_mcast_enabled) ? $def_port->stormctrl_mcast_enabled : false, + 'stormctrl_mcast_rate' => isset($def_port->stormctrl_mcast_rate) ? $def_port->stormctrl_mcast_rate : 100, + 'stormctrl_ucast_enabled' => isset($def_port->stormctrl_ucast_enabled) ? $def_port->stormctrl_ucast_enabled : false, + 'stormctrl_ucast_rate' => isset($def_port->stormctrl_ucast_rate) ? $def_port->stormctrl_ucast_rate : 100, + 'egress_rate_limit_kbps_enabled' => isset($def_port->egress_rate_limit_kbps_enabled) ? $def_port->egress_rate_limit_kbps_enabled : false, + 'autoneg' => isset($def_port->autoneg) ? $def_port->autoneg : true, + 'isolation' => isset($def_port->isolation) ? $def_port->isolation : false, + 'stp_port_mode' => isset($def_port->stp_port_mode) ? $def_port->stp_port_mode : true, + 'port_keepalive_enabled' => isset($def_port->port_keepalive_enabled) ? $def_port->port_keepalive_enabled : false + ]; + + } + + $payload = [ + 'port_overrides' => $device[0]->port_overrides + ]; + + $result = $unifi_connection->set_device_settings_base($device[0]->device_id, $payload); + + if ($result) { + return true; + } + + return false; +} \ No newline at end of file diff --git a/examples/toggle_led.php b/examples/toggle_led.php index 2bec7d1..4b81a4e 100755 --- a/examples/toggle_led.php +++ b/examples/toggle_led.php @@ -31,9 +31,16 @@ $mac = ''; /** * initialize the UniFi API connection class and log in to the controller to do our thing */ -$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); // initialize the class instance -$set_debug_mode = $unifi_connection->set_debug($debug); -$loginresults = $unifi_connection->login(); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); /** * using the new method/function diff --git a/examples/unblock_list.php b/examples/unblock_list.php index 30d41ab..7e01d6d 100755 --- a/examples/unblock_list.php +++ b/examples/unblock_list.php @@ -43,9 +43,16 @@ if ($site_id == "MUST_DEFINE_THIS") { /** * 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(); // always true regardless of site id +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug($debug); +$loginresults = $unifi_connection->login(); // always true regardless of site id foreach ($macs_to_unblock as &$mac) { // block_result is always true even if mac address does not exist :( diff --git a/examples/update_ac-iw_ports.php b/examples/update_ac-iw_ports.php index f2c0c2f..1135b53 100755 --- a/examples/update_ac-iw_ports.php +++ b/examples/update_ac-iw_ports.php @@ -47,11 +47,11 @@ $port_conf_id_port_2 = '<_id of port configuration to apply to port #2>'; $new_ports_config = [ 'port_overrides' => [ [ - 'port_idx' => 1, + 'port_idx' => 1, 'portconf_id' => $port_conf_id_port_1 ], [ - 'port_idx' => 2, + 'port_idx' => 2, 'portconf_id' => $port_conf_id_port_2 ] ] @@ -60,12 +60,19 @@ $new_ports_config = [ /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$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; -$update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config); +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$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; +$update_device = $unifi_connection->set_device_settings_base($device_id, $new_ports_config); if (!$update_device) { $error = $unifi_connection->get_last_results_raw(); diff --git a/examples/update_device_wlan_settings_5.5.X.php b/examples/update_device_wlan_settings_5.5.X.php index ee07272..bb685ae 100755 --- a/examples/update_device_wlan_settings_5.5.X.php +++ b/examples/update_device_wlan_settings_5.5.X.php @@ -51,22 +51,29 @@ $na_channel = 44; /** * initialize the UniFi API connection class and log in to the controller and do our thing */ -$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($ap_mac); -$radio_table = $data[0]->radio_table; -$device_id = $data[0]->device_id; +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$set_debug_mode = $unifi_connection->set_debug(false); +$loginresults = $unifi_connection->login(); +$data = $unifi_connection->list_devices($ap_mac); +$radio_table = $data[0]->radio_table; +$device_id = $data[0]->device_id; foreach ($radio_table as $radio) { if ($radio->radio === 'ng') { $radio->tx_power_mode = $ng_tx_power_mode; - $radio->channel = $ng_channel; + $radio->channel = $ng_channel; } if ($radio->radio === 'na') { $radio->tx_power_mode = $na_tx_power_mode; - $radio->channel = $na_channel; + $radio->channel = $na_channel; } } diff --git a/examples/update_switch_poe-mode.php b/examples/update_switch_poe-mode.php index 9b0039f..12bce44 100755 --- a/examples/update_switch_poe-mode.php +++ b/examples/update_switch_poe-mode.php @@ -42,20 +42,21 @@ $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){ +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)){ + 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){ + foreach ($ports as $port) { $add_conf[] = [ 'port_idx' => $port, 'poe_mode' => $poe_mode @@ -65,12 +66,19 @@ function update_ports($running_config, $ports, $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; +$unifi_connection = new UniFi_API\Client( + $controlleruser, + $controllerpassword, + $controllerurl, + $site_id, + $controllerversion +); + +$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" diff --git a/src/Client.php b/src/Client.php index b48cd77..cae39b0 100755 --- a/src/Client.php +++ b/src/Client.php @@ -13,7 +13,7 @@ namespace UniFi_API; * * @package UniFi_Controller_API_Client_Class * @author Art of WiFi - * @version Release: 1.1.80 + * @version Release: 1.1.81 * @license This class is subject to the MIT license that is bundled with this package in the file LICENSE.md * @example This directory in the package repository contains a collection of examples: * https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples @@ -23,15 +23,14 @@ class Client /** * private and protected properties * - * NOTE: - * do not modify the values here, instead use the constructor or the getter and setter functions/methods + * NOTE: do **not** modify the values here, instead use the constructor or the getter and setter functions/methods */ - const CLASS_VERSION = '1.1.80'; + const CLASS_VERSION = '1.1.81'; protected $baseurl = 'https://127.0.0.1:8443'; protected $user = ''; protected $password = ''; protected $site = 'default'; - protected $version = '6.2.26'; + protected $version = '7.3.76'; protected $debug = false; protected $is_logged_in = false; protected $is_unifi_os = false; @@ -100,9 +99,9 @@ class Client public function __destruct() { /** - * if $_SESSION['unificookie'] is set, do not log out here + * if $_SESSION['unificookie'] is set, do not log out here except when this is a UniFi OS-based controller */ - if (isset($_SESSION['unificookie'])) { + if (isset($_SESSION['unificookie']) && !$this->is_unifi_os) { return; } @@ -1397,9 +1396,9 @@ class Client } /** - * List of site devices with a basic subset of fields (e.g., mac, state, adopted, disabled, type, model, name) + * List of UniFi devices with a basic subset of properties (e.g., mac, state, adopted, disabled, type, model, name) * - * @return array|false an array containing known UniFi device objects), false upon error + * @return array|false an array containing known UniFi device objects, false upon error */ public function list_devices_basic() { @@ -1409,7 +1408,7 @@ class Client /** * Fetch UniFi devices * - * @param string $device_mac optional, the MAC address of a single UniFi device for which the call must be made + * @param array|string $device_mac optional, the MAC address of a single UniFi device for which the call must be made * @return array|false an array containing known UniFi device objects (or a single device when using the * parameter), false upon error */ @@ -2625,6 +2624,7 @@ class Client * @param array $schedule optional, schedule rules * @param array $ap_group_ids optional, array of ap group ids, required for UniFi controller versions 6.0.X * and higher + * @param array $payload optional, array of additional parameters (wlan_bands, wpa3_support, etc.) * @return bool true on success */ public function create_wlan( @@ -2643,9 +2643,10 @@ class Client $uapsd_enabled = false, $schedule_enabled = false, $schedule = [], - $ap_group_ids = null + $ap_group_ids = null, + $payload = [] ) { - $payload = [ + $payload = array_merge($payload,[ 'name' => trim($name), 'usergroup_id' => trim($usergroup_id), 'wlangroup_id' => trim($wlangroup_id), @@ -2658,7 +2659,7 @@ class Client 'uapsd_enabled' => $uapsd_enabled, 'schedule_enabled' => $schedule_enabled, 'schedule' => $schedule, - ]; + ]); if (!empty($vlan_id)) { $payload['networkconf_id'] = $vlan_id;