common.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. $final_message = null;
  3. function success($msg = '') {
  4. global $final_message;
  5. if ($msg !== '')
  6. $final_message = "<p><output><strong>Succès</strong> : <em>" . $msg . "</em></output></p>\n";
  7. }
  8. // When the user requests something unexpected
  9. function userError($msg) {
  10. global $final_message;
  11. $final_message = "<p><output><strong>Erreur utilisataire</strong> : <em>" . $msg . "</em></output></p>\n";
  12. http_response_code(403);
  13. executePage();
  14. }
  15. // When the system did something unexpected
  16. function serverError($msg) {
  17. global $final_message;
  18. $final_message = "<p><output><strong>Server error</strong>: The server encountered an error: <em>" . $msg . "</em></output></p>\n";
  19. http_response_code(500);
  20. error_log("Niver internal error: " . strip_tags($msg));
  21. executePage();
  22. }
  23. function processForm($requireLogin = true) {
  24. if (http_response_code() !== 200)
  25. return false;
  26. if (empty($_POST) AND $requireLogin AND !isset($_SESSION['username']))
  27. echo '<p>Ce formulaire ne sera pas accepté car il faut <a class="auth" href="' . redirUrl('auth/login') . '">se connecter</a> avant.</p>';
  28. if (empty($_POST))
  29. return false;
  30. if ($requireLogin AND !isset($_SESSION['username']))
  31. userError("Vous devez être connecté·e pour effectuer cette action.");
  32. return true;
  33. }
  34. function query($action, $table, $conditions = [], $column = NULL) {
  35. $query = match ($action) {
  36. 'select' => 'SELECT *',
  37. 'delete' => 'DELETE',
  38. };
  39. $query .= " FROM $table";
  40. foreach ($conditions as $key => $val) {
  41. if ($key === array_key_first($conditions))
  42. $query .= " WHERE $key = :$key";
  43. else
  44. $query .= " AND $key = :$key";
  45. }
  46. $db = new PDO('sqlite:' . DB_PATH);
  47. $op = $db->prepare($query);
  48. foreach ($conditions as $key => $val)
  49. $op->bindValue(":$key", $val);
  50. $op->execute();
  51. if (isset($column))
  52. return array_column($op->fetchAll(PDO::FETCH_ASSOC), $column);
  53. return $op->fetchAll(PDO::FETCH_ASSOC);
  54. }
  55. function displayIndex() { ?>
  56. <nav>
  57. <dl>
  58. <?php foreach (DESCRIPTIONS[SERVICE] as $pageId => $pageDesc) {
  59. if ($pageId === 'index') continue;
  60. ?>
  61. <dt><a href="<?= $pageId ?>"><?= TITLES[SERVICE][$pageId] ?></a></dt>
  62. <dd>
  63. <?= $pageDesc ?>
  64. </dd>
  65. <?php } ?>
  66. </dl>
  67. </nav>
  68. <?php
  69. }
  70. function redirUrl($pageId) {
  71. $currentPath = '';
  72. if (SERVICE !== '.') $currentPath .= SERVICE . '/';
  73. if (PAGE !== 'index') $currentPath .= PAGE;
  74. return CONF['common']['prefix'] . "/$pageId?redir=$currentPath";
  75. }
  76. function redir() {
  77. if (isset($_GET['redir'])) {
  78. if (preg_match('/^[0-9a-z\/-]{0,128}$/', $_GET['redir']) !== 1)
  79. userError("Wrong character in <code>redir</code>.");
  80. header('Location: ' . CONF['common']['prefix'] . '/' . $_GET['redir']);
  81. } else {
  82. header('Location: ' . CONF['common']['prefix'] . '/');
  83. }
  84. }
  85. // PHP rmdir() only works on empty directories
  86. function removeDirectory($dir) {
  87. $dirObj = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
  88. $files = new RecursiveIteratorIterator($dirObj, RecursiveIteratorIterator::CHILD_FIRST);
  89. foreach ($files as $file)
  90. $file->isDir() && !$file->isLink() ? rmdir($file->getPathname()) : unlink($file->getPathname());
  91. if (rmdir($dir) !== true)
  92. serverError("Unable to remove directory.");
  93. }
  94. function equalArrays($a, $b) {
  95. return array_diff($a, $b) === [] AND array_diff($b, $a) === [];
  96. }
  97. function linkToDocs($ref, $title) {
  98. return '<a rel="help" href="' . CONF['common']['docs_prefix'] . $ref . '.html">' . $title . '</a>';
  99. }