An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 = (CAPTCHA_ENABLED === true ? preg_replace("/http(s)?:\/\//", "", 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__ . '/lang/messages.php';
  56. // text strings (i18n)
  57. require __DIR__ . '/lang/' . LANGUAGE . ".php";
  58. /**
  59. * Kill off the running process and spit out an error message
  60. * @param string $error error message
  61. */
  62. function sendError($error) {
  63. global $SECURE_NONCE;
  64. die("<!DOCTYPE html>"
  65. . "<meta charset=\"UTF-8\">"
  66. . "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  67. . "<title>Error</title>"
  68. . "<style nonce=\"" . $SECURE_NONCE . "\">"
  69. . "h1 {color: red; font-family: sans-serif; font-size: 20px; margin-bottom: 0px;} "
  70. . "h2 {font-family: sans-serif; font-size: 16px;} "
  71. . "p {font-family: monospace; font-size: 14px; width: 100%; wrap-style: break-word;} "
  72. . "i {font-size: 12px;}"
  73. . "</style>"
  74. . "<h1>A fatal application error has occurred.</h1>"
  75. . "<i>(This isn't your fault.)</i>"
  76. . "<h2>Details:</h2>"
  77. . "<p>" . htmlspecialchars($error) . "</p>");
  78. }
  79. date_default_timezone_set(TIMEZONE);
  80. // Database settings
  81. // Also inits database and stuff
  82. use Medoo\Medoo;
  83. $database;
  84. try {
  85. $database = new Medoo([
  86. 'database_type' => DB_TYPE,
  87. 'database_name' => DB_NAME,
  88. 'server' => DB_SERVER,
  89. 'username' => DB_USER,
  90. 'password' => DB_PASS,
  91. 'charset' => DB_CHARSET
  92. ]);
  93. } catch (Exception $ex) {
  94. //header('HTTP/1.1 500 Internal Server Error');
  95. sendError("Database error. Try again later. $ex");
  96. }
  97. if (!DEBUG) {
  98. error_reporting(0);
  99. } else {
  100. error_reporting(E_ALL);
  101. ini_set('display_errors', 'On');
  102. }
  103. $VARS;
  104. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  105. $VARS = $_POST;
  106. define("GET", false);
  107. } else {
  108. $VARS = $_GET;
  109. define("GET", true);
  110. }
  111. /**
  112. * Checks if a string or whatever is empty.
  113. * @param $str The thingy to check
  114. * @return boolean True if it's empty or whatever.
  115. */
  116. function is_empty($str) {
  117. return (is_null($str) || !isset($str) || $str == '');
  118. }
  119. /**
  120. * I18N string getter. If the key doesn't exist, outputs the key itself.
  121. * @param string $key I18N string key
  122. * @param boolean $echo whether to echo the result or return it (default echo)
  123. */
  124. function lang($key, $echo = true) {
  125. if (array_key_exists($key, STRINGS)) {
  126. $str = STRINGS[$key];
  127. } else {
  128. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  129. $str = $key;
  130. }
  131. if ($echo) {
  132. echo $str;
  133. } else {
  134. return $str;
  135. }
  136. }
  137. /**
  138. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  139. * @param string $key I18N string key
  140. * @param array $replace key-value array of replacements.
  141. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  142. * result will be "hello 123".
  143. * @param boolean $echo whether to echo the result or return it (default echo)
  144. */
  145. function lang2($key, $replace, $echo = true) {
  146. if (array_key_exists($key, STRINGS)) {
  147. $str = STRINGS[$key];
  148. } else {
  149. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  150. $str = $key;
  151. }
  152. foreach ($replace as $find => $repl) {
  153. $str = str_replace("{" . $find . "}", $repl, $str);
  154. }
  155. if ($echo) {
  156. echo $str;
  157. } else {
  158. return $str;
  159. }
  160. }
  161. function dieifnotloggedin() {
  162. if ($_SESSION['loggedin'] != true) {
  163. sendError("Session expired. Please log out and log in again.");
  164. }
  165. }
  166. /**
  167. * Check if the previous database action had a problem.
  168. * @param array $specials int=>string array with special response messages for SQL errors
  169. */
  170. function checkDBError($specials = []) {
  171. global $database;
  172. $errors = $database->error();
  173. if (!is_null($errors[1])) {
  174. foreach ($specials as $code => $text) {
  175. if ($errors[1] == $code) {
  176. sendError($text);
  177. }
  178. }
  179. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  180. }
  181. }
  182. /*
  183. * http://stackoverflow.com/a/20075147
  184. */
  185. if (!function_exists('base_url')) {
  186. function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) {
  187. if (isset($_SERVER['HTTP_HOST'])) {
  188. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  189. $hostname = $_SERVER['HTTP_HOST'];
  190. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  191. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  192. $core = $core[0];
  193. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  194. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  195. $base_url = sprintf($tmplt, $http, $hostname, $end);
  196. } else
  197. $base_url = 'http://localhost/';
  198. if ($parse) {
  199. $base_url = parse_url($base_url);
  200. if (isset($base_url['path']))
  201. if ($base_url['path'] == '/')
  202. $base_url['path'] = '';
  203. }
  204. return $base_url;
  205. }
  206. }
  207. function redirectIfNotLoggedIn() {
  208. if ($_SESSION['loggedin'] !== TRUE) {
  209. header('Location: ' . URL . '/index.php');
  210. die();
  211. }
  212. }