changing behavior of UrlManipulator + adding session use
This commit is contained in:
parent
3089ed432c
commit
ea18637892
7 changed files with 74 additions and 30 deletions
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
// this is a custom autoloader that will load classes from the current directory
|
||||
function customAutoloader($className) {
|
||||
$filePath = __DIR__ . '/' . str_replace('\\', '/', $className) . '.php';
|
||||
|
||||
|
@ -6,5 +7,5 @@ function customAutoloader($className) {
|
|||
include $filePath;
|
||||
}
|
||||
}
|
||||
|
||||
// register the custom autoloader
|
||||
spl_autoload_register('customAutoloader');
|
|
@ -1,14 +1,15 @@
|
|||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
// require autoloader
|
||||
require('./autoloader.php');
|
||||
|
||||
use urlManipulator\UrlManipulator;
|
||||
|
||||
// handle POST requests
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$urlManipulator = new UrlManipulator();
|
||||
$url = $_POST['url'];
|
||||
$result = $urlManipulator->manipulateUrl($url);
|
||||
|
||||
// send the result as a JSON response
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($result);
|
||||
}
|
11
server/reset_session.php
Normal file
11
server/reset_session.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
// Start the session
|
||||
session_start();
|
||||
|
||||
// Unset all session variables
|
||||
$_SESSION = [];
|
||||
|
||||
// Destroy the session
|
||||
session_destroy();
|
||||
?>
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace urlManipulator;
|
||||
|
||||
// this is a custom AbstractUrlManipulator class that will manipulate a URL
|
||||
abstract class AbstractUrlManipulator
|
||||
{
|
||||
abstract public function manipulateUrl(string $url);
|
||||
abstract protected function addParamToUrl(string $url, string $param, string $value);
|
||||
abstract protected function generateRandomValue();
|
||||
abstract protected function getNewParam(array $existingParams);
|
||||
abstract protected function getNewParam(array $existingParams, string $originalUrl);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
namespace urlManipulator;
|
||||
|
||||
class UrlManipulator extends AbstractUrlManipulator
|
||||
|
@ -7,33 +8,37 @@ class UrlManipulator extends AbstractUrlManipulator
|
|||
|
||||
public function manipulateUrl($url): array
|
||||
{
|
||||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
return ['error' => '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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
<label for="url">Enter a valid URL:</label>
|
||||
<textarea id="url" name="url" rows="4" cols="50" required></textarea>
|
||||
<button type="button" onclick="manipulateUrl()">Manipulate URL</button>
|
||||
<button type="button" onclick="resetSession()">Reset</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
@ -33,3 +33,24 @@ function displayError(error) {
|
|||
resultContainer.empty();
|
||||
resultContainer.append('<p class="error">Error: ' + error + '</p>');
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue