commit b8595cdca8719675e1984d2f8040013c52a1e87b Author: Kfir Dayan Date: Tue Dec 12 21:46:11 2023 +0200 first commit with client and server diff --git a/server/params.php b/server/params.php new file mode 100644 index 0000000..9b495ed --- /dev/null +++ b/server/params.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/server/process.php b/server/process.php new file mode 100644 index 0000000..dd21e0d --- /dev/null +++ b/server/process.php @@ -0,0 +1,79 @@ + 'Invalid URL', 'input' => $url]; + } + + $parsedUrl = parse_url($url); + $existingParams = []; + if (isset($parsedUrl['query'])) { + parse_str($parsedUrl['query'], $existingParams); + } + + + if (empty($this->paramsArray)) { + return ['error' => 'Params array is full', 'input' => $url]; + } + + $newParam = $this->getNewParam($existingParams); + + if (!$newParam) { + return ['error' => 'Unable to find a unique parameter to add', 'input' => $url]; + } + + $randomValue = $this->generateRandomValue(); + + $modifiedUrl = $this->addParamToUrl($url, $newParam, $randomValue); + + return [ + 'before' => $url, + 'after' => $modifiedUrl, + ]; + } + + private function addParamToUrl($url, $param, $value) + { + $separator = (parse_url($url, PHP_URL_QUERY) == null) ? '?' : '&'; + + if (strpos($url, $param . '=') !== false) { + $modifiedUrl = preg_replace('/(' . $param . '=)[^&]+/', '$1' . urlencode($value), $url); + } else { + $modifiedUrl = $url . $separator . urlencode($param) . '=' . urlencode($value); + } + + return $modifiedUrl; + } + + private function generateRandomValue() + { + return uniqid(); + } + + private function getNewParam($existingParams){ + while (!empty($this->paramsArray)) { + $newParam = array_pop($this->paramsArray); + + if (!array_key_exists($newParam, $existingParams)) { + return $newParam; + } + } + + return null; + } + +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $urlManipulator = new UrlManipulator(); + $url = $_POST['url']; + $result = $urlManipulator->manipulateUrl($url); + + header('Content-Type: application/json'); + echo json_encode($result); +} diff --git a/web-client/index.html b/web-client/index.html new file mode 100644 index 0000000..ca64d0f --- /dev/null +++ b/web-client/index.html @@ -0,0 +1,24 @@ + + + + + + + URL Manipulator + + + + +
+

URL Manipulator

+
+ +
+
+ + + +
+
+ + diff --git a/web-client/script.js b/web-client/script.js new file mode 100644 index 0000000..a67629f --- /dev/null +++ b/web-client/script.js @@ -0,0 +1,35 @@ +function manipulateUrl() { + var url = $('#url').val(); + + $.ajax({ + type: 'POST', + url: '../server/process.php', + data: { url: url }, + dataType: 'json', + success: function (result) { + displayResult(result); + }, + error: function (xhr, status, error) { + displayError(error); + } + }); +} + +function displayResult(result) { + console.log(result); + var resultContainer = $('#result-container'); + resultContainer.empty(); + + if (result.hasOwnProperty('error')) { + resultContainer.append('

' + result.error + '

'); + } else { + // Update the text area with the modified URL + $('#url').val(result.after.replace(/&/g, '&')); + } +} + +function displayError(error) { + var resultContainer = $('#result-container'); + resultContainer.empty(); + resultContainer.append('

Error: ' + error + '

'); +} diff --git a/web-client/style.css b/web-client/style.css new file mode 100644 index 0000000..2694802 --- /dev/null +++ b/web-client/style.css @@ -0,0 +1,25 @@ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f4; + margin: 0; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; +} + +.container { + background-color: #fff; + padding: 20px; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} + +button { + background-color: #4caf50; + color: #fff; + padding: 10px; + border: none; + border-radius: 4px; + cursor: pointer; +}