diff --git a/index.php b/index.php index 44a8012..5dac7df 100644 --- a/index.php +++ b/index.php @@ -1,17 +1,39 @@ [^\/]+)\/?(?[^\/]+)\/?$/'); +define('SUPPLIER_REGEX', '/^[A-Za-z]\w{0,31}$/'); +define('EVENT_REGEX', '/^\d{4}\-[01]\d\-[0123]\d$/'); +define('ACTION_REGEX', '/^[a-z]{1,16}$/i'); + $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']), '?'); +if (preg_match(REQUEST_REGEX, $requestUrl, $match)) { + $requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null; + $requestEvent = array_key_exists('event', $match) ? $match['event'] : null; +} + +function generateUrl($supplier = null, $event = null) { + global $requestUrl; + + if (is_null($supplier)) + return $requestUrl; + + if (is_null($event)) + return sprintf('%s/%s', $requestUrl, $supplier); + + return sprintf('%s/%s/%s', $requestUrl, $supplier, $event); +} + define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php'); define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php'); if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE; if (!isset($config)) $config = []; -$action = (isset($_REQUEST['action']) and preg_match('/^[a-z]{1,16}$/i', $_REQUEST['action'])) ? $_REQUEST['action'] : null; +$action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null; -$hasSupplier = isset($_REQUEST['supplier']) and !empty($_REQUEST['supplier']) and preg_match('/^[A-Za-z]\w{0,31}$/', $_REQUEST['supplier']); -$supplier = $_REQUEST['supplier']; +$supplier = array_key_exist('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier; +$hasSupplier = is_string($supplier) and preg_match(SUPPLIER_REGEX, $supplier); if ($hasSupplier) { @@ -94,10 +116,11 @@ if ($action === 'config') { } try { + $event = array_key_exist('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent; $hasEvent = ( - isset($_REQUEST['event']) - and preg_match('/^\d{4}\-[01]\d\-[0123]\d$/', $_REQUEST['event']) - and ((new \DateTimeImmutable($_REQUEST['event'])) instanceof \DateTimeImmutable) + is_string($event) + and preg_match(EVENT_REGEX, $event) + and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable) ); } catch (\Exception $exception) { $hasEvent = false; @@ -114,10 +137,9 @@ if (!$isConfig and $hasSupplier) { and ($maxIterations-- > 0) ) $current->add($frequency); $nextEvent = $current->format('Y-m-d'); - header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent)); + header('Location: ' . generateUrl($supplier, $nextEvent)); die(); } else { - $event = $_REQUEST['event']; $current = new \DateTimeImmutable($event); $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']); $previous = $current->sub($frequency); @@ -153,7 +175,7 @@ if (!$isConfig and $hasSupplier) { ); flock($output, LOCK_UN); fclose($output); - header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event)); + header('Location: ' . generateUrl($supplier, $event)); die(); } @@ -210,7 +232,7 @@ if (!$isConfig and $hasSupplier) {