2023-12-12 19:46:11 +00:00
|
|
|
<?php
|
2023-12-18 15:08:52 +00:00
|
|
|
|
2023-12-12 20:25:41 +00:00
|
|
|
namespace urlManipulator;
|
|
|
|
|
|
|
|
class UrlManipulator extends AbstractUrlManipulator
|
2023-12-12 19:46:11 +00:00
|
|
|
{
|
|
|
|
private $paramsArray = ['param1', 'param2', 'param3', 'param4'];
|
2023-12-18 15:08:52 +00:00
|
|
|
|
2023-12-12 20:25:41 +00:00
|
|
|
public function manipulateUrl($url): array
|
2023-12-12 19:46:11 +00:00
|
|
|
{
|
2023-12-18 15:08:52 +00:00
|
|
|
// 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'] = [];
|
2023-12-12 19:46:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
$originalUrl = $_SESSION['originalUrl'];
|
|
|
|
$usedParams = $_SESSION['usedParams'];
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
// Get a new parameter to add to the URL from the predefined array and existing parameters
|
|
|
|
$newParam = $this->getNewParam($usedParams, $originalUrl);
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
// If no new parameter is found, return an error
|
2023-12-12 19:46:11 +00:00
|
|
|
if (!$newParam) {
|
|
|
|
return ['error' => 'Unable to find a unique parameter to add', 'input' => $url];
|
|
|
|
}
|
|
|
|
|
|
|
|
$randomValue = $this->generateRandomValue();
|
2023-12-18 15:08:52 +00:00
|
|
|
$modifiedUrl = $this->addParamToUrl($originalUrl, $newParam, $randomValue);
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
// Store the used parameter in the session
|
|
|
|
$usedParams[] = $newParam;
|
|
|
|
$_SESSION['usedParams'] = $usedParams;
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-12 20:57:42 +00:00
|
|
|
return [
|
2023-12-18 15:08:52 +00:00
|
|
|
'before' => $originalUrl,
|
2023-12-12 20:57:42 +00:00
|
|
|
'after' => $modifiedUrl,
|
|
|
|
];
|
2023-12-12 19:46:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-12 20:25:41 +00:00
|
|
|
protected function addParamToUrl(string $url, string $param, string $value): string
|
2023-12-12 19:46:11 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-12-12 20:25:41 +00:00
|
|
|
protected function generateRandomValue(): string
|
2023-12-12 19:46:11 +00:00
|
|
|
{
|
|
|
|
return uniqid();
|
|
|
|
}
|
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
protected function getNewParam(array $usedParams, string $originalUrl)
|
2023-12-12 20:25:41 +00:00
|
|
|
{
|
2023-12-18 15:08:52 +00:00
|
|
|
$parsedUrl = parse_url($originalUrl);
|
|
|
|
$existingParams = [];
|
|
|
|
|
|
|
|
// Extract existing params from the original URL
|
|
|
|
if (isset($parsedUrl['query'])) {
|
|
|
|
parse_str($parsedUrl['query'], $existingParams);
|
|
|
|
}
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-18 15:08:52 +00:00
|
|
|
// 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;
|
2023-12-12 20:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-12 19:46:11 +00:00
|
|
|
|
2023-12-12 20:57:42 +00:00
|
|
|
return null;
|
2023-12-12 19:46:11 +00:00
|
|
|
}
|
|
|
|
}
|