Plateforme web de commande de panier bio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

545 lines
22 KiB

<?php
define('DEFAULT_TITLE', 'Mon panier bio');
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;
if (!is_null($requestEvent))
$requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
if (!is_null($requestSupplier))
$requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
}
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);
}
function findNext($start, $frequency, $excludes = [], $maxIterations = 1000, $direction = +1) {
$current = clone $start;
$frequency = \DateInterval::createFromDateString($frequency);
do {
while (
($current->getTimestamp() < $now->getTimestamp())
and ($maxIterations-- > 0)
) {
if ($direction === abs($direction)) $current->add($frequency);
else $current->sub($frequency);
}
$nextEvent = $current->format('Y-m-d');
} while (
in_array($nextEvent, $excludes)
and ($maxIterations > 0)
);
return $current;
}
function findPrevious($start, $frequency, $excludes = [], $maxIterations = 1000) {
return findNext($start, $frequency, $excludes, $maxIterations, -1);
}
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(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;
$supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
$hasSupplier = is_string($supplier) and preg_match(SUPPLIER_REGEX, $supplier);
if ($hasSupplier) {
if (!isset($config[$supplier]))
$config[$supplier] = [];
$config[$supplier] = array_merge(
[
'title' => '',
'subtitle' => '<small class="text-muted text-nowrap d-block d-sm-inline">%date%</small>',
'description' => '',
'choices' => [],
'start' => 'now 00:00:00',
'frequency' => '1 day',
'password' => '',
'excludes' => [],
],
$config[$supplier]
);
$hasPassword = !empty($config[$supplier]['password']);
if ($action === 'config') {
if ($hasPassword) {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
header('HTTP/1.0 401 Unauthorized');
printf('Cette configuration est protégée par mot de passe !');
exit;
} elseif (
($_SERVER['PHP_AUTH_USER'] !== $supplier)
or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
) {
header('HTTP/1.0 403 Forbidden');
printf('Cette configuration est protégée par mot de passe !');
exit;
}
}
foreach (array_keys($config[$supplier]) as $key)
if (isset($_REQUEST[$key]))
$config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
}
if (empty($config[$supplier]['start']))
$config[$supplier]['start'] = 'now 00:00:00';
foreach (['choices', 'excludes'] as $key) {
if (is_string($config[$supplier][$key]))
$config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);
if (!is_array($config[$supplier][$key]))
$config[$supplier][$key] = [];
$config[$supplier][$key] = array_filter(
$config[$supplier][$key],
function ($choice) {
return is_string($choice) and !empty(trim($choice));
}
);
$config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
}
}
$isConfig = false;
if ($action === 'config') {
$output = fopen(CONFIG_FILE, 'w+');
if ($output) {
if (flock($output, LOCK_EX)) {
fwrite($output, '<?php' . PHP_EOL);
fprintf(
$output,
'$config = %s;' . PHP_EOL,
var_export($config, true)
);
flock($output, LOCK_UN);
}
fclose($output);
}
$isConfig = true;
}
try {
$event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
$hasEvent = (
is_string($event)
and preg_match(EVENT_REGEX, $event)
and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
);
} catch (\Exception $exception) {
$hasEvent = false;
}
if (!$isConfig and $hasSupplier) {
$start = new \DateTime($config[$supplier]['start']);
if (!$hasEvent) {
$now = new \DateTime('now');
$next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes']);
$nextEvent = $next->format('Y-m-d');
header('Location: ' . generateUrl($supplier, $nextEvent));
die();
} else {
$current = new \DateTimeImmutable($event);
$previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes']);
$previousEvent = $previous->format('Y-m-d');
if (false and !array_key_exists($previousEvent, $data[$supplier]))
unset($previousEvent);
$next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes']);
$nextEvent = $next->format('Y-m-d');
if (false and !array_key_exists($nextEvent, $data[$supplier]))
unset($nextEvent);
}
switch ($action) {
case 'insert' :
case 'delete' :
$isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
$output = fopen(DATA_FILE, 'a+');
if (!$output) break;
if (!flock($output, LOCK_EX)) break;
if ($isBeginning)
fwrite($output, '<?php' . PHP_EOL);
$item = [];
foreach (['name', 'choice', 'action'] as $field)
$item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
$item['timestamp'] = time();
$item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
fprintf(
$output,
'$data[%s][%s][] = %s;' . PHP_EOL,
var_export($supplier, true),
var_export($event, true),
str_replace(PHP_EOL, '', var_export($item, true))
);
flock($output, LOCK_UN);
fclose($output);
header('Location: ' . generateUrl($supplier, $event));
die();
}
if (!isset($data)) $data = [];
if (file_exists(DATA_FILE)) include DATA_FILE;
$items = [];
$allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
usort($allItems, function ($a, $b) {
$a = intval($a['timestamp']);
$b = intval($b['timestamp']);
if ($a === $b)
return 0;
return ($a < $b) ? -1 : 1;
});
foreach ($allItems as $item) {
if ($item['action'] === 'insert') {
$items[] = $item;
} elseif ($item['action'] === 'delete') {
foreach ($items as $index => $prevItem)
if ($prevItem['hash'] === $item['hash'])
unset($items[$index]);
}
}
$date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
foreach (['title', 'subtitle', 'description'] as $key) {
while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
$config[$supplier][$key] = str_replace(
$match[0],
${$match[1]},
$config[$supplier][$key]
);
}
if (empty($config[$supplier]['title']))
$config[$supplier]['title'] = $supplier;
$stats = [];
foreach ($items as $item)
if (!empty($item['choice']))
$stats[$item['choice']] += 1;
}
?><!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?php if ($hasSupplier) : ?><?php echo strip_tags($config[$supplier]['title']); ?><?php if (!$isConfig) : ?> — <?php echo strip_tags($config[$supplier]['subtitle']); ?><?php endif; ?><?php else : ?><?php echo DEFAULT_TITLE; ?><?php endif; ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<header>
<nav class="navbar navbar-dark bg-dark">
<div class="container-fluid">
<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>
<?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
</a>
<?php if ($hasSupplier) : ?>
<span class="navbar-text text-muted">
<?php if ($isConfig) : ?>
<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">
<path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2zM5 8h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z"/>
</svg>
<?php else : ?>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
<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?action=config', generateUrl($supplier)); ?>">Configuration</a>
<?php endif; ?>
</span>
<?php endif; ?>
</div>
</nav>
</header>
<main>
<?php if (!$hasSupplier) : ?>
<section class="container-fluid">
<div class="row my-3">
<div class="col">
<div class="alert alert-danger" role="alert">
Pas de fournisseur !
</div>
</div>
</div>
</section>
<?php else : ?>
<?php if ($isConfig) : ?>
<section class="container-fluid">
<div class="row my-3 g-3">
<div class="col">
<h1>Configuration</h1>
</div>
</div>
</section>
<section class="container-fluid">
<div class="row g-3">
<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">
<input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
<div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
</div>
</div>
<div class="row mb-3">
<label for="description" class="col-sm-2 col-form-label">Description</label>
<div class="col-sm-10">
<textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
<div class="form-text">La description affichée sous le titre.</div>
</div>
</div>
<div class="row mb-3">
<label for="choices" class="col-sm-2 col-form-label">Choix</label>
<div class="col-sm-10">
<textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
<div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
</div>
</div>
<div class="row mb-3">
<label for="start" class="col-sm-2 col-form-label">Début</label>
<div class="col-sm-10">
<input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
<div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
</div>
</div>
<div class="row mb-3">
<label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
<div class="form-text">La fréquence des événements dans le format <a class="text-reset" href="https://www.php.net/manual/fr/datetime.formats.relative.php" target="_blank">décrit sur cette page</a>.</div>
</div>
</div>
<div class="row mb-3">
<label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
<div class="col-sm-10">
<textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['excludes']); ?></textarea>
<div class="form-text">Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'<a href="https://fr.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> : <kbd>AAAA-MM-JJ</kbd></div>
</div>
</div>
<div class="row mb-3">
<label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
<div class="form-text">Ce mot de passe sera demandé pour accéder à la configuration la prochaine fois. Le nom d'utilisateur est le fournisseur courant (en l'occurrence <kbd><?php echo $supplier; ?></kbd>).</div>
</div>
</div>
<div class="row">
<div class="col mb-3">
<button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
</div>
</div>
</form>
</div>
</section>
<?php else : ?>
<section class="container-fluid">
<div class="row my-3">
<div class="col">
<h1>
<div class="btn-group float-end" role="group">
<?php if (isset($previousEvent)) : ?>
<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 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 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>
</a>
<?php endif; ?>
</div>
<?php echo $config[$supplier]['title']; ?>
<?php echo $config[$supplier]['subtitle']; ?>
</h1>
<?php if (!empty($config[$supplier]['description'])) : ?>
<p class="lead"><?php echo $config[$supplier]['description']; ?></p>
<?php endif; ?>
</div>
</div>
</section>
<section class="container-fluid">
<div class="row g-3">
<form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
<div class="row my-3">
<label for="title" class="col-sm-2 col-form-label">Nom</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="name" required placeholder="Nom" />
</div>
</div>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<div class="row mb-3">
<label for="title" class="col-sm-2 col-form-label">Choix</label>
<div class="col-sm-10">
<div class="btn-group" role="group">
<?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
<input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
<label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endif; ?>
<div class="row">
<div class="col mb-3">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<?php if (empty($config[$supplier]['choices'])) : ?>
<input type="hidden" name="choice" value="" />
<?php endif; ?>
<button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
</div>
</div>
</form>
</div>
</section>
<section class="container-fluid">
<div class="row my-3">
<?php if (!empty($items)) : ?>
<div class="col-12">
<div class="table-responsive">
<table class="table table-striped table-hover align-middle">
<thead>
<tr>
<th scope="col">
Nom
</th>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<th scope="col">
Choix
</th>
<?php endif; ?>
<th scope="col">
&nbsp;
</th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $item) : ?>
<tr>
<td>
<?php echo $item['name']; ?>
</td>
<?php if (!empty($config[$supplier]['choices'])) : ?>
<td>
<?php if (!empty($item['choice'])) : ?>
<?php echo $item['choice']; ?>
<?php endif; ?>
</td>
<?php endif; ?>
<td>
<form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
<input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
<input type="hidden" name="event" value="<?php echo $event; ?>" />
<input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
<input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
<button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
<div class="col-12">
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
Commandes
<span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
</li>
<?php foreach ($stats as $choice => $count) : ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?php echo $choice; ?>
<span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</section>
<?php endif; ?>
<?php endif; ?>
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<?php if ($isConfig) : ?>
<script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
<script>
document.querySelectorAll('.js-ckeditor').forEach(function (element) {
ClassicEditor.create(element).catch(error => { console.error(error); });
});
</script>
<?php endif; ?>
<script>
document.querySelectorAll('.js-localremember').forEach(function (form) {
const fields = [ 'name', 'choice' ];
form.addEventListener('submit', function (event) {
fields.forEach(function (field) {
window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
});
});
fields.forEach(function (field) {
if (
(form.elements[field].value === '')
&& (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
) {
form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
}
});
});
</script>
</body>
</html>