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
|
<?php
|
||||||
|
// this is a custom autoloader that will load classes from the current directory
|
||||||
function customAutoloader($className) {
|
function customAutoloader($className) {
|
||||||
$filePath = __DIR__ . '/' . str_replace('\\', '/', $className) . '.php';
|
$filePath = __DIR__ . '/' . str_replace('\\', '/', $className) . '.php';
|
||||||
|
|
||||||
|
@ -6,5 +7,5 @@ function customAutoloader($className) {
|
||||||
include $filePath;
|
include $filePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// register the custom autoloader
|
||||||
spl_autoload_register('customAutoloader');
|
spl_autoload_register('customAutoloader');
|
|
@ -1,14 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
ini_set('display_errors', 1);
|
// require autoloader
|
||||||
require('./autoloader.php');
|
require('./autoloader.php');
|
||||||
|
|
||||||
use urlManipulator\UrlManipulator;
|
use urlManipulator\UrlManipulator;
|
||||||
|
|
||||||
|
// handle POST requests
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
$urlManipulator = new UrlManipulator();
|
$urlManipulator = new UrlManipulator();
|
||||||
$url = $_POST['url'];
|
$url = $_POST['url'];
|
||||||
$result = $urlManipulator->manipulateUrl($url);
|
$result = $urlManipulator->manipulateUrl($url);
|
||||||
|
// send the result as a JSON response
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode($result);
|
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
|
<?php
|
||||||
|
|
||||||
namespace urlManipulator;
|
namespace urlManipulator;
|
||||||
|
// this is a custom AbstractUrlManipulator class that will manipulate a URL
|
||||||
abstract class AbstractUrlManipulator
|
abstract class AbstractUrlManipulator
|
||||||
{
|
{
|
||||||
abstract public function manipulateUrl(string $url);
|
abstract public function manipulateUrl(string $url);
|
||||||
abstract protected function addParamToUrl(string $url, string $param, string $value);
|
abstract protected function addParamToUrl(string $url, string $param, string $value);
|
||||||
abstract protected function generateRandomValue();
|
abstract protected function generateRandomValue();
|
||||||
abstract protected function getNewParam(array $existingParams);
|
abstract protected function getNewParam(array $existingParams, string $originalUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,39 +1,44 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace urlManipulator;
|
namespace urlManipulator;
|
||||||
|
|
||||||
class UrlManipulator extends AbstractUrlManipulator
|
class UrlManipulator extends AbstractUrlManipulator
|
||||||
{
|
{
|
||||||
private $paramsArray = ['param1', 'param2', 'param3', 'param4'];
|
private $paramsArray = ['param1', 'param2', 'param3', 'param4'];
|
||||||
|
|
||||||
public function manipulateUrl($url): array
|
public function manipulateUrl($url): array
|
||||||
{
|
{
|
||||||
if (!filter_var($url, FILTER_VALIDATE_URL)) {
|
// Using Session to store the original URL and used parameters
|
||||||
return ['error' => 'Invalid URL', 'input' => $url];
|
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);
|
$originalUrl = $_SESSION['originalUrl'];
|
||||||
$existingParams = [];
|
$usedParams = $_SESSION['usedParams'];
|
||||||
if (isset($parsedUrl['query'])) {
|
|
||||||
parse_str($parsedUrl['query'], $existingParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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)) {
|
// If no new parameter is found, return an error
|
||||||
return ['error' => 'Params array is full', 'input' => $url];
|
|
||||||
}
|
|
||||||
|
|
||||||
$newParam = $this->getNewParam($existingParams);
|
|
||||||
|
|
||||||
if (!$newParam) {
|
if (!$newParam) {
|
||||||
return ['error' => 'Unable to find a unique parameter to add', 'input' => $url];
|
return ['error' => 'Unable to find a unique parameter to add', 'input' => $url];
|
||||||
}
|
}
|
||||||
|
|
||||||
$randomValue = $this->generateRandomValue();
|
$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 [
|
return [
|
||||||
'before' => $url,
|
'before' => $originalUrl,
|
||||||
'after' => $modifiedUrl,
|
'after' => $modifiedUrl,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -56,19 +61,23 @@ class UrlManipulator extends AbstractUrlManipulator
|
||||||
return uniqid();
|
return uniqid();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getNewParam(array $existingParams)
|
protected function getNewParam(array $usedParams, string $originalUrl)
|
||||||
{
|
{
|
||||||
while (!empty($this->paramsArray)) {
|
$parsedUrl = parse_url($originalUrl);
|
||||||
$newParam = array_pop($this->paramsArray);
|
$existingParams = [];
|
||||||
|
|
||||||
if (!array_key_exists($newParam, $existingParams)) {
|
// Extract existing params from the original URL
|
||||||
return $newParam;
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,8 @@
|
||||||
<label for="url">Enter a valid URL:</label>
|
<label for="url">Enter a valid URL:</label>
|
||||||
<textarea id="url" name="url" rows="4" cols="50" required></textarea>
|
<textarea id="url" name="url" rows="4" cols="50" required></textarea>
|
||||||
<button type="button" onclick="manipulateUrl()">Manipulate URL</button>
|
<button type="button" onclick="manipulateUrl()">Manipulate URL</button>
|
||||||
|
<button type="button" onclick="resetSession()">Reset</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -33,3 +33,24 @@ function displayError(error) {
|
||||||
resultContainer.empty();
|
resultContainer.empty();
|
||||||
resultContainer.append('<p class="error">Error: ' + error + '</p>');
|
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