Game server and admin dashboard for TerranQuest.
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.

required.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if ($_SESSION['mobile'] === TRUE) {
  28. header("Content-Security-Policy: "
  29. . "default-src 'self';"
  30. . "object-src 'none'; "
  31. . "img-src * data:; "
  32. . "media-src 'self'; "
  33. . "frame-src 'none'; "
  34. . "font-src 'self'; "
  35. . "connect-src *; "
  36. . "style-src 'self' 'unsafe-inline'; "
  37. . "script-src 'self' 'unsafe-inline'");
  38. } else {
  39. header("Content-Security-Policy: "
  40. . "default-src 'self';"
  41. . "object-src 'none'; "
  42. . "img-src * data:; "
  43. . "media-src 'self'; "
  44. . "frame-src 'none'; "
  45. . "font-src 'self'; "
  46. . "connect-src *; "
  47. . "style-src 'self' 'nonce-$SECURE_NONCE'; "
  48. . "script-src 'self' 'nonce-$SECURE_NONCE'");
  49. }
  50. //
  51. // Composer
  52. require __DIR__ . '/vendor/autoload.php';
  53. // List of alert messages
  54. require __DIR__ . '/langs/messages.php';
  55. $libs = glob(__DIR__ . "/lib/*.lib.php");
  56. foreach ($libs as $lib) {
  57. require_once $lib;
  58. }
  59. $Strings = new Strings($SETTINGS['language']);
  60. /**
  61. * Kill off the running process and spit out an error message
  62. * @param string $error error message
  63. */
  64. function sendError($error) {
  65. global $SECURE_NONCE;
  66. die("<!DOCTYPE html>"
  67. . "<meta charset=\"UTF-8\">"
  68. . "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  69. . "<title>Error</title>"
  70. . "<style nonce=\"" . $SECURE_NONCE . "\">"
  71. . "h1 {color: red; font-family: sans-serif; font-size: 20px; margin-bottom: 0px;} "
  72. . "h2 {font-family: sans-serif; font-size: 16px;} "
  73. . "p {font-family: monospace; font-size: 14px; width: 100%; wrap-style: break-word;} "
  74. . "i {font-size: 12px;}"
  75. . "</style>"
  76. . "<h1>A fatal application error has occurred.</h1>"
  77. . "<i>(This isn't your fault.)</i>"
  78. . "<h2>Details:</h2>"
  79. . "<p>" . htmlspecialchars($error) . "</p>");
  80. }
  81. date_default_timezone_set($SETTINGS['timezone']);
  82. // Database settings
  83. // Also inits database and stuff
  84. use Medoo\Medoo;
  85. $database;
  86. try {
  87. $database = new Medoo([
  88. 'database_type' => $SETTINGS['database']['type'],
  89. 'database_name' => $SETTINGS['database']['name'],
  90. 'server' => $SETTINGS['database']['server'],
  91. 'username' => $SETTINGS['database']['user'],
  92. 'password' => $SETTINGS['database']['password'],
  93. 'charset' => $SETTINGS['database']['charset']
  94. ]);
  95. } catch (Exception $ex) {
  96. //header('HTTP/1.1 500 Internal Server Error');
  97. sendError("Database error. Try again later. $ex");
  98. }
  99. if (!$SETTINGS['debug']) {
  100. error_reporting(0);
  101. } else {
  102. error_reporting(E_ALL);
  103. ini_set('display_errors', 'On');
  104. }
  105. $VARS;
  106. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  107. $VARS = $_POST;
  108. define("GET", false);
  109. } else {
  110. $VARS = $_GET;
  111. define("GET", true);
  112. }
  113. function dieifnotloggedin() {
  114. if ($_SESSION['loggedin'] != true) {
  115. sendError("Session expired. Please log out and log in again.");
  116. }
  117. }
  118. /**
  119. * Check if the previous database action had a problem.
  120. * @param array $specials int=>string array with special response messages for SQL errors
  121. */
  122. function checkDBError($specials = []) {
  123. global $database;
  124. $errors = $database->error();
  125. if (!is_null($errors[1])) {
  126. foreach ($specials as $code => $text) {
  127. if ($errors[1] == $code) {
  128. sendError($text);
  129. }
  130. }
  131. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  132. }
  133. }
  134. function redirectIfNotLoggedIn() {
  135. if ($_SESSION['loggedin'] !== TRUE) {
  136. header('Location: ' . $SETTINGS['url'] . '/index.php');
  137. die();
  138. }
  139. }