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.2KB

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