servnest/fn/common.php

139 lines
3.6 KiB
PHP
Raw Normal View History

2022-05-31 19:12:14 +02:00
<?php
$final_message = null;
function output($code, $msg = '') {
global $final_message;
$shortCode = $code / 100 % 10;
$final_message = match ($shortCode) {
2 => ($msg === '') ? '' : "<p><output><strong>Succès</strong> : <em>" . $msg . "</em></output></p>\n",
4 => "<p><output><strong>Erreur utilisataire</strong> : <em>" . $msg . "</em></output></p>\n",
5 => "<p><output><strong>Server error</strong>: The server encountered an error: <em>" . $msg . "</em></output></p>\n",
};
http_response_code($code);
if ($shortCode === 5)
error_log("Niver internal error: " . strip_tags($msg));
if ($code !== 200)
executePage();
2022-05-31 19:12:14 +02:00
}
function processForm($requireLogin = true) {
if (http_response_code() !== 200)
return false;
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 19:12:14 +02:00
if (empty($_POST))
return false;
2022-05-31 19:12:14 +02:00
if ($requireLogin AND !isset($_SESSION['username']))
output(403, 'Vous devez être connecté·e pour effectuer cette action.');
return true;
2022-05-31 19:12:14 +02:00
}
2022-06-11 23:42:48 +02:00
function insert($table, $values) {
$query = 'INSERT INTO ' . $table . '(';
foreach ($values as $key => $val) {
if ($key === array_key_last($values))
$query .= "$key";
else
$query .= "$key, ";
}
$query .= ") VALUES(";
foreach ($values as $key => $val) {
if ($key === array_key_last($values))
$query .= ":$key";
else
$query .= ":$key, ";
}
$query .= ")";
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare($query);
foreach ($values as $key => $val)
$op->bindValue(":$key", $val);
$op->execute();
}
2022-06-11 23:42:48 +02: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() { ?>
2022-08-11 16:39:31 +02:00
<nav>
<dl>
<?php foreach (PAGES[SERVICE] as $pageId => $page) {
if ($pageId === 'index') continue;
?>
<dt><a href="<?= $pageId ?>"><?= $page['title'] ?></a></dt>
2022-08-11 16:39:31 +02:00
<dd>
<?= $page['description'] ?>
2022-08-11 16:39:31 +02:00
</dd>
<?php } ?>
</dl>
</nav>
<?php
}
function redirUrl($pageId) {
2022-09-13 01:09:40 +02:00
return CONF['common']['prefix'] . '/' . $pageId . '?redir=' . PAGE_URL;
}
2022-06-17 15:45:52 +02:00
function redir() {
if (isset($_GET['redir'])) {
if (preg_match('/^[0-9a-z\/-]{0,128}$/', $_GET['redir']) !== 1)
output(403, 'Wrong character in <code>redir</code>.');
2022-06-17 15:45:52 +02:00
header('Location: ' . CONF['common']['prefix'] . '/' . $_GET['redir']);
} else {
header('Location: ' . CONF['common']['prefix'] . '/');
}
exit();
2022-06-17 15:45:52 +02: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)
output(500, 'Unable to remove directory.');
}
2022-07-20 20:03:45 +02:00
function equalArrays($a, $b) {
return array_diff($a, $b) === [] AND array_diff($b, $a) === [];
}
2022-07-20 20:03:45 +02:00
function linkToDocs($ref, $title) {
return '<a rel="help" href="' . CONF['common']['docs_prefix'] . $ref . '.html">' . $title . '</a>';
}