servnest/fn/common.php

79 lines
2 KiB
PHP
Raw Normal View History

2022-05-31 17:12:14 +00:00
<?php
// When the user requests something unexpected
function userError($msg) {
http_response_code(403);
2022-06-11 22:04:18 +00:00
echo "<p><strong>Erreur utilisataire</strong> : <em>" . $msg . "</em></p>\n";
2022-05-31 17:12:14 +00:00
closeHTML();
}
// When the system did something unexpected
function serverError($msg) {
http_response_code(500);
error_log("Niver internal error: " . strip_tags($msg));
2022-06-11 22:04:18 +00:00
echo "<p><strong>Server error</strong>: The server encountered an error: <em>" . $msg . "</em></p>\n";
2022-05-31 17:12:14 +00:00
closeHTML();
}
// For use in pages that first display a form and then process it
function switchToFormProcess($requireLogin = true) {
if (empty($_POST) AND $requireLogin AND !isset($_SESSION['username']))
echo '<p>Ce formulaire ne sera pas accepté car il faut <a class="auth" href="' . redirUrl('auth/login') . '">se connecter</a> avant.</p>';
2022-05-31 17:12:14 +00:00
if (empty($_POST))
closeHTML();
if ($requireLogin AND !isset($_SESSION['username']))
userError("Vous devez être connecté·e pour effectuer cette action.");
}
2022-06-11 21:42:48 +00:00
function query($action, $table, $conditions = [], $column = NULL) {
$query = match ($action) {
'select' => 'SELECT *',
'delete' => 'DELETE',
};
$query .= " FROM $table";
foreach ($conditions as $key => $val) {
if ($key === array_key_first($conditions))
$query .= " WHERE $key = :$key";
else
$query .= " AND $key = :$key";
}
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare($query);
foreach ($conditions as $key => $val)
$op->bindValue(":$key", $val);
$op->execute();
if (isset($column))
return array_column($op->fetchAll(PDO::FETCH_ASSOC), $column);
return $op->fetchAll(PDO::FETCH_ASSOC);
}
function displayIndex() { ?>
<dl>
<?php foreach (DESCRIPTIONS[SERVICE] as $pageId => $pageDesc) {
if ($pageId === 'index') continue;
?>
<dt><a href="<?= $pageId ?>"><?= TITLES[SERVICE][$pageId] ?></a></dt>
<dd>
<?= $pageDesc ?>
</dd>
<?php } ?>
</dl>
<?php
}
function redirUrl($pageId) {
$currentPath = '';
if (SERVICE !== '') $currentPath .= SERVICE . '/';
if (PAGE !== 'index') $currentPath .= PAGE;
return CONF['common']['prefix'] . "/$pageId?redir=$currentPath";
}