Browse Source

change la gestion de la query string

master
vince vince 3 years ago
parent
commit
2fc4a8a599
1 changed files with 39 additions and 17 deletions
  1. +39
    -17
      index.php

+ 39
- 17
index.php View File

@ -1,17 +1,39 @@
<?php
define('REQUEST_REGEX', '/^\/(?<supplier>[^\/]+)\/?(?<event>[^\/]+)\/?$/');
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) {
<header>
<nav class="navbar navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="<?php echo $hasSupplier ? sprintf('%s?supplier=%s', $requestUrl, $supplier) : $requestUrl; ?>">
<a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-basket d-inline-block align-text-top" viewBox="0 0 16 16">
<path d="M5.757 1.071a.5.5 0 0 1 .172.686L3.383 6h9.234L10.07 1.757a.5.5 0 1 1 .858-.514L13.783 6H15a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1v4.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 13.5V9a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h1.217L5.07 1.243a.5.5 0 0 1 .686-.172zM2 9v4.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V9H2zM1 7v1h14V7H1zm3 3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 4 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 6 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 8 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5z"/>
</svg>
@ -219,7 +241,7 @@ if (!$isConfig and $hasSupplier) {
<?php if ($hasSupplier) : ?>
<span class="navbar-text text-muted">
<?php if ($isConfig) : ?>
<a class="text-reset" href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">Retour</a>
<a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
<?php else : ?>
<?php if ($hasPassword) : ?>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
@ -230,7 +252,7 @@ if (!$isConfig and $hasSupplier) {
<path d="M11 1a2 2 0 0 0-2 2v4a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h5V3a3 3 0 0 1 6 0v4a.5.5 0 0 1-1 0V3a2 2 0 0 0-2-2zM3 8a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V9a1 1 0 0 0-1-1H3z"/>
</svg>
<?php endif; ?>
<a tabindex="-1" class="text-reset" href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">Configuration</a>
<a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">Configuration</a>
<?php endif; ?>
</span>
<?php endif; ?>
@ -259,7 +281,7 @@ if (!$isConfig and $hasSupplier) {
</section>
<section class="container-fluid">
<div class="row g-3">
<form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
<form action="<?php echo generateUrl($supplier); ?>" method="post">
<div class="row mb-3">
<label for="title" class="col-sm-2 col-form-label">Titre</label>
<div class="col-sm-10">
@ -319,20 +341,20 @@ if (!$isConfig and $hasSupplier) {
<div class="btn-group float-end" role="group">
<?php if (isset($previousEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $previousEvent); ?>" title="Événement précédent">
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
</svg>
</a>
<?php endif; ?>
<a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $event); ?>" title="Cet événement">
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
<path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
<path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
</svg>
</a>
<?php if (isset($nextEvent)) : ?>
<a class="btn btn-outline-primary" href="<?php printf('%s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent); ?>" title="Événement suivant">
<a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
</svg>
@ -348,7 +370,7 @@ if (!$isConfig and $hasSupplier) {
</section>
<section class="container-fluid">
<div class="row g-3">
<form class="js-localremember" action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
<form class="js-localremember" action="<?php echo generateUrl($supplier); ?>" method="post">
<div class="row mb-3">
<label for="title" class="col-sm-2 col-form-label">Nom</label>
<div class="col-sm-10">


Loading…
Cancel
Save