123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- $final_message = null;
- function success($msg = '') {
- global $final_message;
- if ($msg !== '')
- $final_message = "<p><output><strong>Succès</strong> : <em>" . $msg . "</em></output></p>\n";
- }
- // When the user requests something unexpected
- function userError($msg) {
- global $final_message;
- $final_message = "<p><output><strong>Erreur utilisataire</strong> : <em>" . $msg . "</em></output></p>\n";
- http_response_code(403);
- executePage();
- }
- // When the system did something unexpected
- function serverError($msg) {
- global $final_message;
- $final_message = "<p><output><strong>Server error</strong>: The server encountered an error: <em>" . $msg . "</em></output></p>\n";
- http_response_code(500);
- error_log("Niver internal error: " . strip_tags($msg));
- executePage();
- }
- 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>';
- if (empty($_POST))
- return false;
- if ($requireLogin AND !isset($_SESSION['username']))
- userError("Vous devez être connecté·e pour effectuer cette action.");
- return true;
- }
- 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() { ?>
- <nav>
- <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>
- </nav>
- <?php
- }
- function redirUrl($pageId) {
- $currentPath = '';
- if (SERVICE !== '.') $currentPath .= SERVICE . '/';
- if (PAGE !== 'index') $currentPath .= PAGE;
- return CONF['common']['prefix'] . "/$pageId?redir=$currentPath";
- }
- 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'] . '/');
- }
- }
- // 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.");
- }
- function equalArrays($a, $b) {
- return array_diff($a, $b) === [] AND array_diff($b, $a) === [];
- }
- function linkToDocs($ref, $title) {
- return '<a rel="help" href="' . CONF['common']['docs_prefix'] . $ref . '.html">' . $title . '</a>';
- }
|