From 1d363deb18d860a5981e53df854dad1d5d7d8961 Mon Sep 17 00:00:00 2001 From: malle-pietje Date: Mon, 3 Mar 2025 14:06:49 +0100 Subject: [PATCH] added code examples demonstrating how to work with PPSKs --- examples/ppsks_examples/create_ppsk.php | 131 ++++++++++++++++++++++ examples/ppsks_examples/list_ppsks.php | 108 ++++++++++++++++++ examples/ppsks_examples/remove_ppsk.php | 141 ++++++++++++++++++++++++ 3 files changed, 380 insertions(+) create mode 100755 examples/ppsks_examples/create_ppsk.php create mode 100755 examples/ppsks_examples/list_ppsks.php create mode 100755 examples/ppsks_examples/remove_ppsk.php diff --git a/examples/ppsks_examples/create_ppsk.php b/examples/ppsks_examples/create_ppsk.php new file mode 100755 index 0000000..0a3efef --- /dev/null +++ b/examples/ppsks_examples/create_ppsk.php @@ -0,0 +1,131 @@ +set_debug($debug); + $login_results = $unifi_connection->login(); + $wlan_conf = $unifi_connection->list_wlanconf(); + + /** + * Get the details for the WLAN the PPSK will be created for. + */ + $wlan_details = []; + + foreach ($wlan_conf as $wlan) { + if ($wlan->_id === $new_ppsk_wlan_id) { + $wlan_details = $wlan; + + break; + } + } + + if (empty($wlan_details)) { + echo 'WLAN not found, exiting... Please check the $new_ppsk_wlan_id value ๐Ÿคจ' . PHP_EOL; + + exit; + } + + /** + * Create the new PPSK, then append it to the existing PPSKs array. + */ + $new_ppsk = [ + 'password' => $new_ppsk_password, + 'networkconf_id' => $new_ppsk_network_id, + ]; + + $wlan_details->private_preshared_keys[] = $new_ppsk; + + $unifi_connection->set_wlansettings_base($new_ppsk_wlan_id, $wlan_details); + + $request_end_time = microtime(true); + + /** + * Record end time. + */ + $end_time = microtime(true); + + /** + * Calculate and display the execution time. + */ + $execution_time = $end_time - $start_time; + + echo 'The PPSK has been created successfully!๐Ÿ‘' . PHP_EOL; + + echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL; + echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL; +} catch (CurlExtensionNotLoadedException $e) { + echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidBaseUrlException $e) { + echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidSiteNameException $e) { + echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL; +} catch (JsonDecodeException $e) { + echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginRequiredException $e) { + echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlGeneralErrorException $e) { + echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlTimeoutException $e) { + echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginFailedException $e) { + echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL; +} catch (Exception $e) { + echo 'General Exception: ' . $e->getMessage(). PHP_EOL; +} \ No newline at end of file diff --git a/examples/ppsks_examples/list_ppsks.php b/examples/ppsks_examples/list_ppsks.php new file mode 100755 index 0000000..39e4376 --- /dev/null +++ b/examples/ppsks_examples/list_ppsks.php @@ -0,0 +1,108 @@ +set_debug($debug); + $login_results = $unifi_connection->login(); + $wlan_conf = $unifi_connection->list_wlanconf(); + + /** + * Get the details for the WLAN the PPSK will be created for. + */ + $wlan_details = []; + + foreach ($wlan_conf as $wlan) { + /** + * Skip this SSID if private_pre_shared_keys is not set or empty. + */ + if (empty($wlan->private_preshared_keys)) { + continue; + } + + echo json_encode($wlan->private_preshared_keys, JSON_PRETTY_PRINT) . PHP_EOL; + } + + $request_end_time = microtime(true); + + /** + * Record end time. + */ + $end_time = microtime(true); + + /** + * Calculate and display the execution time. + */ + $execution_time = $end_time - $start_time; + + echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL; + echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL; +} catch (CurlExtensionNotLoadedException $e) { + echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidBaseUrlException $e) { + echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidSiteNameException $e) { + echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL; +} catch (JsonDecodeException $e) { + echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginRequiredException $e) { + echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlGeneralErrorException $e) { + echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlTimeoutException $e) { + echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginFailedException $e) { + echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL; +} catch (Exception $e) { + echo 'General Exception: ' . $e->getMessage(). PHP_EOL; +} \ No newline at end of file diff --git a/examples/ppsks_examples/remove_ppsk.php b/examples/ppsks_examples/remove_ppsk.php new file mode 100755 index 0000000..3a05150 --- /dev/null +++ b/examples/ppsks_examples/remove_ppsk.php @@ -0,0 +1,141 @@ +set_debug($debug); + $login_results = $unifi_connection->login(); + $wlan_conf = $unifi_connection->list_wlanconf(); + + foreach ($wlan_conf as $wlan) { + /** + * Skip this SSID if the private_pre_shared_keys array is not set or empty. + */ + if (empty($wlan->private_preshared_keys)) { + continue; + } + + $removals = 0; + + foreach ($wlan->private_preshared_keys as $ppsk) { + if ($ppsk->password === $ppsk_to_remove) { + echo 'Removing PPSK with password: "' . $ppsk_to_remove . '"' . PHP_EOL; + + /** + * Remove the PPSK from the private_preshared_keys array. + */ + $wlan->private_preshared_keys = array_values(array_filter($wlan->private_preshared_keys, function ($value) use ($ppsk_to_remove) { + return $value->password !== $ppsk_to_remove; + })); + + $removals++; + } + } + + /** + * Push the updated WLAN configuration back to the controller if we removed one or more PPSKs. + */ + if ($removals > 0) { + echo 'Pushing updated WLAN configuration back to the controller...' . PHP_EOL; + $unifi_connection->set_wlansettings_base($wlan->_id, $wlan); + $total_removals += $removals; + } + } + + $request_end_time = microtime(true); + + /** + * Record end time. + */ + $end_time = microtime(true); + + /** + * Calculate the execution time. + */ + $execution_time = $end_time - $start_time; + + if ($total_removals === 0) { + echo 'No PPSKs were removed, exiting...' . PHP_EOL; + + exit; + } + + echo 'Total PPSKs removed: ' . $total_removals . PHP_EOL; + + echo 'Full execution time: ' . $execution_time . ' seconds' . PHP_EOL; + echo 'Time to fetch, process and push data back: ' . ($request_end_time - $request_start_time) . ' seconds' . PHP_EOL; +} catch (CurlExtensionNotLoadedException $e) { + echo 'CurlExtensionNotLoadedException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidBaseUrlException $e) { + echo 'InvalidBaseUrlException: ' . $e->getMessage(). PHP_EOL; +} catch (InvalidSiteNameException $e) { + echo 'InvalidSiteNameException: ' . $e->getMessage(). PHP_EOL; +} catch (JsonDecodeException $e) { + echo 'JsonDecodeException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginRequiredException $e) { + echo 'LoginRequiredException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlGeneralErrorException $e) { + echo 'CurlGeneralErrorException: ' . $e->getMessage(). PHP_EOL; +} catch (CurlTimeoutException $e) { + echo 'CurlTimeoutException: ' . $e->getMessage(). PHP_EOL; +} catch (LoginFailedException $e) { + echo 'LoginFailedException: ' . $e->getMessage(). PHP_EOL; +} catch (Exception $e) { + echo 'General Exception: ' . $e->getMessage(). PHP_EOL; +} \ No newline at end of file