Single-sign-on and self-serve account management. https://netsyms.biz/apps/accounthub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.0KB

  1. <?php
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * This file contains global settings and utility functions.
  7. */
  8. ob_start(); // allow sending headers after content
  9. // Settings file
  10. require __DIR__ . '/settings.php';
  11. // Unicode, solves almost all stupid encoding problems
  12. header('Content-Type: text/html; charset=utf-8');
  13. // Strip PHP version
  14. header('X-Powered-By: PHP');
  15. // Security
  16. header('X-Content-Type-Options: nosniff');
  17. header('X-XSS-Protection: 1; mode=block');
  18. header('X-Frame-Options: "DENY"');
  19. header('Referrer-Policy: "no-referrer, strict-origin-when-cross-origin"');
  20. $SECURE_NONCE = base64_encode(random_bytes(8));
  21. $session_length = 60 * 60 * 1; // 1 hour
  22. ini_set('session.gc_maxlifetime', $session_length);
  23. session_set_cookie_params($session_length, "/", null, false, false);
  24. session_start(); // stick some cookies in it
  25. // renew session cookie
  26. setcookie(session_name(), session_id(), time() + $session_length, "/", false, false);
  27. $captcha_server = ($SETTINGS['captcha']['enabled'] === true ? preg_replace("/http(s)?:\/\//", "", $SETTINGS['captcha']['server']) : "");
  28. if ($_SESSION['mobile'] === TRUE) {
  29. header("Content-Security-Policy: "
  30. . "default-src 'self';"
  31. . "object-src 'none'; "
  32. . "img-src * data:; "
  33. . "media-src 'self'; "
  34. . "frame-src 'none'; "
  35. . "font-src 'self'; "
  36. . "connect-src *; "
  37. . "style-src 'self' 'unsafe-inline' $captcha_server; "
  38. . "script-src 'self' 'unsafe-inline' $captcha_server");
  39. } else {
  40. header("Content-Security-Policy: "
  41. . "default-src 'self';"
  42. . "object-src 'none'; "
  43. . "img-src * data:; "
  44. . "media-src 'self'; "
  45. . "frame-src 'none'; "
  46. . "font-src 'self'; "
  47. . "connect-src *; "
  48. . "style-src 'self' 'nonce-$SECURE_NONCE' $captcha_server; "
  49. . "script-src 'self' 'nonce-$SECURE_NONCE' $captcha_server");
  50. }
  51. //
  52. // Composer
  53. require __DIR__ . '/vendor/autoload.php';
  54. // List of alert messages
  55. require __DIR__ . '/langs/messages.php';
  56. $libs = glob(__DIR__ . "/lib/*.lib.php");
  57. foreach ($libs as $lib) {
  58. require_once $lib;
  59. }
  60. $Strings = new Strings($SETTINGS['language']);
  61. /**
  62. * Kill off the running process and spit out an error message
  63. * @param string $error error message
  64. */
  65. function sendError($error) {
  66. global $SECURE_NONCE;
  67. die("<!DOCTYPE html>"
  68. . "<meta charset=\"UTF-8\">"
  69. . "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  70. . "<title>Error</title>"
  71. . "<style nonce=\"" . $SECURE_NONCE . "\">"
  72. . "h1 {color: red; font-family: sans-serif; font-size: 20px; margin-bottom: 0px;} "
  73. . "h2 {font-family: sans-serif; font-size: 16px;} "
  74. . "p {font-family: monospace; font-size: 14px; width: 100%; wrap-style: break-word;} "
  75. . "i {font-size: 12px;}"
  76. . "</style>"
  77. . "<h1>A fatal application error has occurred.</h1>"
  78. . "<i>(This isn't your fault.)</i>"
  79. . "<h2>Details:</h2>"
  80. . "<p>" . htmlspecialchars($error) . "</p>");
  81. }
  82. date_default_timezone_set($SETTINGS['timezone']);
  83. // Database settings
  84. // Also inits database and stuff
  85. use Medoo\Medoo;
  86. $database;
  87. try {
  88. $database = new Medoo([
  89. 'database_type' => $SETTINGS['database']['type'],
  90. 'database_name' => $SETTINGS['database']['name'],
  91. 'server' => $SETTINGS['database']['server'],
  92. 'username' => $SETTINGS['database']['user'],
  93. 'password' => $SETTINGS['database']['password'],
  94. 'charset' => $SETTINGS['database']['charset']
  95. ]);
  96. } catch (Exception $ex) {
  97. //header('HTTP/1.1 500 Internal Server Error');
  98. sendError("Database error. Try again later. $ex");
  99. }
  100. if (!$SETTINGS['debug']) {
  101. error_reporting(0);
  102. } else {
  103. error_reporting(E_ALL);
  104. ini_set('display_errors', 'On');
  105. }
  106. $VARS;
  107. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  108. $VARS = $_POST;
  109. define("GET", false);
  110. } else {
  111. $VARS = $_GET;
  112. define("GET", true);
  113. }
  114. function dieifnotloggedin() {
  115. if ($_SESSION['loggedin'] != true) {
  116. sendError("Session expired. Please log out and log in again.");
  117. }
  118. }
  119. /**
  120. * Check if the previous database action had a problem.
  121. * @param array $specials int=>string array with special response messages for SQL errors
  122. */
  123. function checkDBError($specials = []) {
  124. global $database;
  125. $errors = $database->error();
  126. if (!is_null($errors[1])) {
  127. foreach ($specials as $code => $text) {
  128. if ($errors[1] == $code) {
  129. sendError($text);
  130. }
  131. }
  132. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  133. }
  134. }
  135. function redirectIfNotLoggedIn() {
  136. if ($_SESSION['loggedin'] !== TRUE) {
  137. header('Location: ' . $SETTINGS['url'] . '/index.php');
  138. die();
  139. }
  140. }
  141. /**
  142. * Check if the client's IP has been doing too many brute-force-friendly
  143. * requests lately.
  144. * Kills the script with a "friendly" error and response code 429
  145. * (Too Many Requests) if the last access time in the DB is too near.
  146. *
  147. * Also updates the rate_limit table with the latest data and purges old rows.
  148. * @global type $database
  149. */
  150. function engageRateLimit() {
  151. global $database;
  152. $delay = date("Y-m-d H:i:s", strtotime("-2 seconds"));
  153. $database->delete('rate_limit', ["lastaction[<]" => $delay]);
  154. if ($database->has('rate_limit', ["AND" => ["ipaddr" => IPUtils::getClientIP()]])) {
  155. http_response_code(429);
  156. // JSONify it so API clients don't scream too loud
  157. die(json_encode(["status" => "ERROR", "msg" => "You're going too fast. Slow down, mkay?"]));
  158. } else {
  159. // Add a record for the IP address
  160. $database->insert('rate_limit', ["ipaddr" => IPUtils::getClientIP(), "lastaction" => date("Y-m-d H:i:s")]);
  161. }
  162. }