QwikClock is an employee time tracking app.
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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * This file contains global settings and utility functions.
  4. */
  5. ob_start(); // allow sending headers after content
  6. // Unicode, solves almost all stupid encoding problems
  7. header('Content-Type: text/html; charset=utf-8');
  8. // Strip PHP version
  9. header('X-Powered-By: PHP');
  10. // Security
  11. header('X-Content-Type-Options: nosniff');
  12. header('X-XSS-Protection: 1; mode=block');
  13. header('X-Frame-Options: "DENY"');
  14. header('Referrer-Policy: "no-referrer, strict-origin-when-cross-origin"');
  15. $SECURE_NONCE = base64_encode(random_bytes(8));
  16. $session_length = 60 * 60; // 1 hour
  17. session_set_cookie_params($session_length, "/", null, false, false);
  18. session_start(); // stick some cookies in it
  19. // renew session cookie
  20. setcookie(session_name(), session_id(), time() + $session_length);
  21. if ($_SESSION['mobile'] === TRUE) {
  22. header("Content-Security-Policy: "
  23. . "default-src 'self';"
  24. . "object-src 'none'; "
  25. . "img-src * data:; "
  26. . "media-src 'self'; "
  27. . "frame-src 'none'; "
  28. . "font-src 'self'; "
  29. . "connect-src *; "
  30. . "style-src 'self' 'unsafe-inline'; "
  31. . "script-src 'self' 'unsafe-inline'");
  32. } else {
  33. header("Content-Security-Policy: "
  34. . "default-src 'self';"
  35. . "object-src 'none'; "
  36. . "img-src * data:; "
  37. . "media-src 'self'; "
  38. . "frame-src 'none'; "
  39. . "font-src 'self'; "
  40. . "connect-src *; "
  41. . "style-src 'self' 'nonce-$SECURE_NONCE'; "
  42. . "script-src 'self' 'nonce-$SECURE_NONCE'");
  43. }
  44. //
  45. // Composer
  46. require __DIR__ . '/vendor/autoload.php';
  47. // Settings file
  48. require __DIR__ . '/settings.php';
  49. // List of alert messages
  50. require __DIR__ . '/lang/messages.php';
  51. // text strings (i18n)
  52. require __DIR__ . '/lang/' . LANGUAGE . ".php";
  53. /**
  54. * Kill off the running process and spit out an error message
  55. * @param string $error error message
  56. */
  57. function sendError($error) {
  58. global $SECURE_NONCE;
  59. die("<!DOCTYPE html>"
  60. . "<meta charset=\"UTF-8\">"
  61. . "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  62. . "<title>Error</title>"
  63. . "<style nonce=\"" . $SECURE_NONCE . "\">"
  64. . "h1 {color: red; font-family: sans-serif; font-size: 20px; margin-bottom: 0px;} "
  65. . "h2 {font-family: sans-serif; font-size: 16px;} "
  66. . "p {font-family: monospace; font-size: 14px; width: 100%; wrap-style: break-word;} "
  67. . "i {font-size: 12px;}"
  68. . "</style>"
  69. . "<h1>A fatal application error has occurred.</h1>"
  70. . "<i>(This isn't your fault.)</i>"
  71. . "<h2>Details:</h2>"
  72. . "<p>" . htmlspecialchars($error) . "</p>");
  73. }
  74. date_default_timezone_set(TIMEZONE);
  75. // Database settings
  76. // Also inits database and stuff
  77. use Medoo\Medoo;
  78. $database;
  79. try {
  80. $database = new Medoo([
  81. 'database_type' => DB_TYPE,
  82. 'database_name' => DB_NAME,
  83. 'server' => DB_SERVER,
  84. 'username' => DB_USER,
  85. 'password' => DB_PASS,
  86. 'charset' => DB_CHARSET
  87. ]);
  88. } catch (Exception $ex) {
  89. //header('HTTP/1.1 500 Internal Server Error');
  90. sendError("Database error. Try again later. $ex");
  91. }
  92. if (!DEBUG) {
  93. error_reporting(0);
  94. } else {
  95. error_reporting(E_ALL);
  96. ini_set('display_errors', 'On');
  97. }
  98. $VARS;
  99. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  100. $VARS = $_POST;
  101. define("GET", false);
  102. } else {
  103. $VARS = $_GET;
  104. define("GET", true);
  105. }
  106. /**
  107. * Checks if a string or whatever is empty.
  108. * @param $str The thingy to check
  109. * @return boolean True if it's empty or whatever.
  110. */
  111. function is_empty($str) {
  112. return (is_null($str) || !isset($str) || $str == '');
  113. }
  114. /**
  115. * I18N string getter. If the key doesn't exist, outputs the key itself.
  116. * @param string $key I18N string key
  117. * @param boolean $echo whether to echo the result or return it (default echo)
  118. */
  119. function lang($key, $echo = true) {
  120. if (array_key_exists($key, STRINGS)) {
  121. $str = STRINGS[$key];
  122. } else {
  123. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  124. $str = $key;
  125. }
  126. if ($echo) {
  127. echo $str;
  128. } else {
  129. return $str;
  130. }
  131. }
  132. /**
  133. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  134. * @param string $key I18N string key
  135. * @param array $replace key-value array of replacements.
  136. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  137. * result will be "hello 123".
  138. * @param boolean $echo whether to echo the result or return it (default echo)
  139. */
  140. function lang2($key, $replace, $echo = true) {
  141. if (array_key_exists($key, STRINGS)) {
  142. $str = STRINGS[$key];
  143. } else {
  144. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  145. $str = $key;
  146. }
  147. foreach ($replace as $find => $repl) {
  148. $str = str_replace("{" . $find . "}", $repl, $str);
  149. }
  150. if ($echo) {
  151. echo $str;
  152. } else {
  153. return $str;
  154. }
  155. }
  156. function dieifnotloggedin() {
  157. if ($_SESSION['loggedin'] != true) {
  158. sendError("Session expired. Please log out and log in again.");
  159. }
  160. }
  161. /**
  162. * Check if the previous database action had a problem.
  163. * @param array $specials int=>string array with special response messages for SQL errors
  164. */
  165. function checkDBError($specials = []) {
  166. global $database;
  167. $errors = $database->error();
  168. if (!is_null($errors[1])) {
  169. foreach ($specials as $code => $text) {
  170. if ($errors[1] == $code) {
  171. sendError($text);
  172. }
  173. }
  174. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  175. }
  176. }
  177. /*
  178. * http://stackoverflow.com/a/20075147
  179. */
  180. if (!function_exists('base_url')) {
  181. function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) {
  182. if (isset($_SERVER['HTTP_HOST'])) {
  183. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  184. $hostname = $_SERVER['HTTP_HOST'];
  185. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  186. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  187. $core = $core[0];
  188. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  189. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  190. $base_url = sprintf($tmplt, $http, $hostname, $end);
  191. } else
  192. $base_url = 'http://localhost/';
  193. if ($parse) {
  194. $base_url = parse_url($base_url);
  195. if (isset($base_url['path']))
  196. if ($base_url['path'] == '/')
  197. $base_url['path'] = '';
  198. }
  199. return $base_url;
  200. }
  201. }
  202. function redirectIfNotLoggedIn() {
  203. if ($_SESSION['loggedin'] !== TRUE) {
  204. header('Location: ' . URL . '/index.php');
  205. die();
  206. }
  207. }