2022-05-31 17:12:14 +00:00
|
|
|
<?php
|
|
|
|
|
2022-09-07 16:44:49 +00:00
|
|
|
$final_message = null;
|
|
|
|
|
2022-06-28 22:30:14 +00:00
|
|
|
function success($msg = '') {
|
2022-09-07 16:44:49 +00:00
|
|
|
global $final_message;
|
2022-06-28 22:30:14 +00:00
|
|
|
if ($msg !== '')
|
2022-09-07 16:44:49 +00:00
|
|
|
$final_message = "<p><output><strong>Succès</strong> : <em>" . $msg . "</em></output></p>\n";
|
2022-06-28 22:30:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:12:14 +00:00
|
|
|
// When the user requests something unexpected
|
|
|
|
function userError($msg) {
|
2022-09-07 16:44:49 +00:00
|
|
|
global $final_message;
|
|
|
|
$final_message = "<p><output><strong>Erreur utilisataire</strong> : <em>" . $msg . "</em></output></p>\n";
|
2022-05-31 17:12:14 +00:00
|
|
|
http_response_code(403);
|
2022-09-07 16:44:49 +00:00
|
|
|
executePage();
|
2022-05-31 17:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the system did something unexpected
|
|
|
|
function serverError($msg) {
|
2022-09-07 16:44:49 +00:00
|
|
|
global $final_message;
|
|
|
|
$final_message = "<p><output><strong>Server error</strong>: The server encountered an error: <em>" . $msg . "</em></output></p>\n";
|
2022-05-31 17:12:14 +00:00
|
|
|
http_response_code(500);
|
|
|
|
error_log("Niver internal error: " . strip_tags($msg));
|
2022-09-07 16:44:49 +00:00
|
|
|
executePage();
|
2022-05-31 17:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 02:21:17 +00:00
|
|
|
function processForm($requireLogin = true) {
|
2022-09-07 16:44:49 +00:00
|
|
|
if (http_response_code() !== 200)
|
|
|
|
return false;
|
2022-06-15 10:42:30 +00:00
|
|
|
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))
|
2022-09-01 02:21:17 +00:00
|
|
|
return false;
|
2022-05-31 17:12:14 +00:00
|
|
|
if ($requireLogin AND !isset($_SESSION['username']))
|
|
|
|
userError("Vous devez être connecté·e pour effectuer cette action.");
|
2022-09-01 02:21:17 +00:00
|
|
|
return true;
|
2022-05-31 17:12:14 +00:00
|
|
|
}
|
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);
|
|
|
|
}
|
2022-06-14 16:21:09 +00:00
|
|
|
|
|
|
|
function displayIndex() { ?>
|
2022-08-11 14:39:31 +00:00
|
|
|
<nav>
|
|
|
|
<dl>
|
2022-06-14 16:21:09 +00:00
|
|
|
<?php foreach (DESCRIPTIONS[SERVICE] as $pageId => $pageDesc) {
|
|
|
|
if ($pageId === 'index') continue;
|
|
|
|
?>
|
2022-08-11 14:39:31 +00:00
|
|
|
<dt><a href="<?= $pageId ?>"><?= TITLES[SERVICE][$pageId] ?></a></dt>
|
|
|
|
<dd>
|
|
|
|
<?= $pageDesc ?>
|
|
|
|
</dd>
|
|
|
|
<?php } ?>
|
|
|
|
</dl>
|
|
|
|
</nav>
|
2022-06-14 16:21:09 +00:00
|
|
|
<?php
|
|
|
|
}
|
2022-06-15 10:42:30 +00:00
|
|
|
|
|
|
|
function redirUrl($pageId) {
|
2022-09-12 23:09:40 +00:00
|
|
|
return CONF['common']['prefix'] . '/' . $pageId . '?redir=' . PAGE_URL;
|
2022-06-15 10:42:30 +00:00
|
|
|
}
|
2022-06-17 13:45:52 +00:00
|
|
|
|
|
|
|
function redir() {
|
|
|
|
if (isset($_GET['redir'])) {
|
|
|
|
if (preg_match('/^[0-9a-z\/-]{0,128}$/', $_GET['redir']) !== 1)
|
|
|
|
userError("Wrong character in <code>redir</code>.");
|
|
|
|
header('Location: ' . CONF['common']['prefix'] . '/' . $_GET['redir']);
|
|
|
|
} else {
|
|
|
|
header('Location: ' . CONF['common']['prefix'] . '/');
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 22:37:06 +00:00
|
|
|
|
|
|
|
// PHP rmdir() only works on empty directories
|
|
|
|
function removeDirectory($dir) {
|
|
|
|
$dirObj = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
$files = new RecursiveIteratorIterator($dirObj, RecursiveIteratorIterator::CHILD_FIRST);
|
|
|
|
foreach ($files as $file)
|
|
|
|
$file->isDir() && !$file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname());
|
|
|
|
if (rmdir($dir) !== true)
|
|
|
|
serverError("Unable to remove directory.");
|
|
|
|
}
|
2022-07-20 18:03:45 +00:00
|
|
|
|
2022-09-03 16:12:49 +00:00
|
|
|
function equalArrays($a, $b) {
|
|
|
|
return array_diff($a, $b) === [] AND array_diff($b, $a) === [];
|
|
|
|
}
|
|
|
|
|
2022-07-20 18:03:45 +00:00
|
|
|
function linkToDocs($ref, $title) {
|
|
|
|
return '<a rel="help" href="' . CONF['common']['docs_prefix'] . $ref . '.html">' . $title . '</a>';
|
|
|
|
}
|