From 29c4ed5f3c9397420b891cf151777155973bdf5f Mon Sep 17 00:00:00 2001 From: Damian Peterson Date: Fri, 7 Jul 2023 08:03:43 +1200 Subject: [PATCH] Add first (and only?) file shell of the app. awaiting api access Tidy up colours and radio buttons Autofocus on username. Change default hop to 6am Add self verification process Add license. More tidying up. All that self-verification stuff was nonsense removed Get working with API Adjust styles of buttons. Change done message. Highlight selected hop API integration for changing hour of power Rounded buttons Change logout to log out --- .gitignore | 1 + favicon.ico | Bin 0 -> 1150 bytes index.php | 457 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 458 insertions(+) create mode 100644 .gitignore create mode 100644 favicon.ico create mode 100644 index.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..8eef6d3926e7a9eac91b2acd68d4f1d8b8ff7ff4 GIT binary patch literal 1150 zcmZQzU<5(|0R|wcz>vYhz#zuJz@P!dKp~(AL>x#lH~{6)fs)du|Fp#afq)Y8siB_| zGeCMlfLuR7bVEpT%pg@ivU+T6LTp0R7{5e literal 0 HcmV?d00001 diff --git a/index.php b/index.php new file mode 100644 index 0000000..8d93887 --- /dev/null +++ b/index.php @@ -0,0 +1,457 @@ + $selectedHour + ]; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $vars['API_URL'] . 'hop/' . $customerNumber . '/' . $connectionId .'/'); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $post); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken, 'Accept: application/json']); + $hourDetails = curl_exec($curl); + curl_close($curl); + + if (!$hourDetails) { + throw new Exception('Failed to get selected hour details'); + } + + $jsonHourDetails = json_decode($hourDetails, true); + if ($jsonHourDetails['error']) { + throw new Exception($jsonHourDetails['error']['detail'], $jsonHourDetails['error']['code']); + } else { + $selectedHour = $jsonHourDetails['data']['start']['interval']; + } +} + +/** + * Get access and refresh tokens for a customer from their login code and cookie them. + * + * @param $vars + * @param $code + * @return void + * @throws Exception + */ +function authorizeWithCode($vars, $code) { + $post = [ + 'code' => $code, + 'client_id' => $vars['CLIENT_ID'], + 'client_secret' => $vars['CLIENT_SECRET'], + 'grant_type' => 'authorization_code', + 'scope' => $vars['SCOPES'], + 'redirect_uri' => $vars['REDIRECT_URI'], + ]; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $vars['TOKEN_URL']); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $post); + $auth = curl_exec($curl); + curl_close($curl); + + if (!$auth) { + throw new Exception('Post to authorize failed', 500); + } + + $jsonAuth = json_decode($auth, true); + + if ($jsonAuth['error']) { + throw new Exception($jsonAuth['error']['detail'], $jsonAuth['error']['code']); + } else { + setcookie('access_token', $jsonAuth['access_token'], time() + $jsonAuth['expires_in'], "/"); + setcookie('refresh_token', $jsonAuth['refresh_token'], time() + (86400 * 90), "/"); + header('Location: /'); + die(); + } +} + +/** + * Refresh a customer's access token from their refresh token. + * + * @param $vars + * @param $refreshToken + * @return void + * @throws Exception + */ +function refreshToken($vars, $refreshToken) { + $post = [ + 'grant_type' => 'refresh_token', + 'refresh_token' => $refreshToken, + ]; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $vars['TOKEN_URL']); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $post); + curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Basic ' . base64_encode($vars['CLIENT_ID'] . ':' . $vars['CLIENT_SECRET']), 'Content-Type: multipart/form-data']); + $auth = curl_exec($curl); + curl_close($curl); + + if (!$auth) { + setcookie('refresh_token', '', time() - 1000, "/"); + throw new Exception('Failed to log back in automatically', 500); + } + + $jsonAuth = json_decode($auth, true); + + if ($jsonAuth['error']) { + setcookie('refresh_token', '', time() - 1000, "/"); + throw new Exception('Failed to log back in automatically (' . $auth . ')', 500); + } else { + setcookie('access_token', $jsonAuth['access_token'], time() + $jsonAuth['expires_in'], "/"); + setcookie('refresh_token', $jsonAuth['refresh_token'], time() + (86400 * 90), "/"); + header('Location: /'); + } +} + +// We've been pinged by Electric Kiwi with a new code. We need to get a token, cookie it and redirect to the homepage. +if ($_GET && key_exists('code', $_GET)) { + try { + authorizeWithCode($vars, $_GET['code']); + } catch (Exception $exception) { + $message = 'There was a problem signing in. Perhaps try again.'; + $isLoggedIn = false; + } +} + +// Logout has been requested so destroy access and refresh token cookies +if ($_GET && key_exists('logout', $_GET)) { + setcookie('access_token', '', time() - 1000, "/"); + setcookie('refresh_token', '', time() - 1000, "/"); + header('Location: /'); +} + +// Set state of landing page depending on whether customer has tokens +if (!isset($accessToken) && !isset($refreshToken)) { + // This user doesn't have a saved access or refresh token. Prompt login + $isLoggedIn = false; +} elseif (isset($accessToken) && isset($refreshToken)) { + // They have an access token so attempt to get customer details + try { + getCustomerDetails($vars, $accessToken, $customerNumber, $connectionId, $customerName); + getCurrentHour($vars, $accessToken, $customerNumber, $connectionId, $selectedHour); + $message = 'Select your hour of power.'; + } catch (Exception $exception) { + if ($exception->getCode() === 401) { + try { + refreshToken($vars, $refreshToken); + } catch (Exception $exception) { + $message = $exception->getMessage() . '. Please log in again below.'; + $isLoggedIn = false; + } + } + } +} elseif (isset($refreshToken)) { + try { + refreshToken($vars, $refreshToken); + } catch (Exception $exception) { + $message = $exception->getMessage() . '. Please log in again below.'; + $isLoggedIn = false; + } +} + +// The form was submitted. Update the hour of power +if ($isLoggedIn && $_POST) { + // Wanting to update hour of power + $selectedHour = $_POST['hour']; + $isValid = true; + + if (empty($selectedHour)) { + $message = 'Please select an hour for your hour of free power'; + $isValid = false; + } + + if ($isValid) { + try { + setCurrentHour($vars, $accessToken, $customerNumber, $connectionId, $selectedHour); + $message = 'Done. Hour changed to ' . $times[$selectedHour] . '.'; + } catch (Exception $exception) { + $message = $exception->getMessage(); + } + } +} +?> + + + + + Electric Kiwi Hour Changer + + + + +
+ + +
+ + + +
+ +
+ +
+ $value): ?> +
+ checked> + +
+ +
+
+ +
+
+

Logged in as . Log out

+ +

This site is not affiliated with Electric Kiwi. It uses Electric Kiwi's API in order to allow you to + update your hour of power. None of your personal information is retained or shared. + Source code +

+
+ +