From ea186378922f901912d3bde8a9753d9e7363d553 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Mon, 18 Dec 2023 17:08:52 +0200 Subject: [PATCH] changing behavior of UrlManipulator + adding session use --- server/autoloader.php | 3 +- server/index.php | 5 +- server/reset_session.php | 11 ++++ .../urlManipulator/AbstractUrlManipulator.php | 4 +- server/urlManipulator/UrlManipulator.php | 57 +++++++++++-------- web-client/index.html | 3 +- web-client/script.js | 21 +++++++ 7 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 server/reset_session.php diff --git a/server/autoloader.php b/server/autoloader.php index c2df845..46081cc 100644 --- a/server/autoloader.php +++ b/server/autoloader.php @@ -1,4 +1,5 @@ manipulateUrl($url); - + // send the result as a JSON response header('Content-Type: application/json'); echo json_encode($result); } \ No newline at end of file diff --git a/server/reset_session.php b/server/reset_session.php new file mode 100644 index 0000000..a8c2454 --- /dev/null +++ b/server/reset_session.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/server/urlManipulator/AbstractUrlManipulator.php b/server/urlManipulator/AbstractUrlManipulator.php index ea28a8e..5e9e8d4 100644 --- a/server/urlManipulator/AbstractUrlManipulator.php +++ b/server/urlManipulator/AbstractUrlManipulator.php @@ -1,12 +1,12 @@ 'Invalid URL', 'input' => $url]; + // Using Session to store the original URL and used parameters + session_start(); + + // Check if the original URL is stored in the session + if (!isset($_SESSION['originalUrl'])) { + // Store the original URL in the session + $_SESSION['originalUrl'] = $url; + // Initialize the used parameters array in the session + $_SESSION['usedParams'] = []; } - $parsedUrl = parse_url($url); - $existingParams = []; - if (isset($parsedUrl['query'])) { - parse_str($parsedUrl['query'], $existingParams); - } + $originalUrl = $_SESSION['originalUrl']; + $usedParams = $_SESSION['usedParams']; + // Get a new parameter to add to the URL from the predefined array and existing parameters + $newParam = $this->getNewParam($usedParams, $originalUrl); - if (empty($this->paramsArray)) { - return ['error' => 'Params array is full', 'input' => $url]; - } - - $newParam = $this->getNewParam($existingParams); - + // If no new parameter is found, return an error if (!$newParam) { return ['error' => 'Unable to find a unique parameter to add', 'input' => $url]; } $randomValue = $this->generateRandomValue(); + $modifiedUrl = $this->addParamToUrl($originalUrl, $newParam, $randomValue); - $modifiedUrl = $this->addParamToUrl($url, $newParam, $randomValue); + // Store the used parameter in the session + $usedParams[] = $newParam; + $_SESSION['usedParams'] = $usedParams; return [ - 'before' => $url, + 'before' => $originalUrl, 'after' => $modifiedUrl, ]; } @@ -56,19 +61,23 @@ class UrlManipulator extends AbstractUrlManipulator return uniqid(); } - protected function getNewParam(array $existingParams) + protected function getNewParam(array $usedParams, string $originalUrl) { - while (!empty($this->paramsArray)) { - $newParam = array_pop($this->paramsArray); + $parsedUrl = parse_url($originalUrl); + $existingParams = []; - if (!array_key_exists($newParam, $existingParams)) { - return $newParam; + // Extract existing params from the original URL + if (isset($parsedUrl['query'])) { + parse_str($parsedUrl['query'], $existingParams); + } + + // Find the first unused parameter from the predefined array and existing parameters + foreach ($this->paramsArray as $param) { + if (!in_array($param, $usedParams) && !array_key_exists($param, $existingParams)) { + return $param; } } return null; } - - } - diff --git a/web-client/index.html b/web-client/index.html index ca64d0f..db9344b 100644 --- a/web-client/index.html +++ b/web-client/index.html @@ -18,7 +18,8 @@ + - + \ No newline at end of file diff --git a/web-client/script.js b/web-client/script.js index ca6102c..f6af0a9 100644 --- a/web-client/script.js +++ b/web-client/script.js @@ -33,3 +33,24 @@ function displayError(error) { resultContainer.empty(); resultContainer.append('

Error: ' + error + '

'); } + +// Function to reset the session +function resetSession() { + var resultContainer = $('#result-container'); + var textArea = $('#url'); + + $.ajax({ + type: 'POST', + url: '../server/reset_session.php', // Adjust the path accordingly + success: function () { + alert('Session has been reset.'); + resultContainer.empty(); // Clear the error display + textArea.val(''); // Clear the text area + // You can perform additional actions after resetting the session if needed + }, + error: function (xhr, status, error) { + console.error('Error resetting session:', error); + } + }); +} +