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

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. $binstack;
  83. try {
  84. $database = new Medoo([
  85. 'database_type' => DB_TYPE,
  86. 'database_name' => DB_NAME,
  87. 'server' => DB_SERVER,
  88. 'username' => DB_USER,
  89. 'password' => DB_PASS,
  90. 'charset' => DB_CHARSET
  91. ]);
  92. $binstack = new Medoo([
  93. 'database_type' => BINSTACK_DB_TYPE,
  94. 'database_name' => BINSTACK_DB_NAME,
  95. 'server' => BINSTACK_DB_SERVER,
  96. 'username' => BINSTACK_DB_USER,
  97. 'password' => BINSTACK_DB_PASS,
  98. 'charset' => BINSTACK_DB_CHARSET
  99. ]);
  100. } catch (Exception $ex) {
  101. //header('HTTP/1.1 500 Internal Server Error');
  102. sendError("Database error. Try again later. $ex");
  103. }
  104. if (!DEBUG) {
  105. error_reporting(0);
  106. } else {
  107. error_reporting(E_ALL);
  108. ini_set('display_errors', 'On');
  109. }
  110. $VARS;
  111. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  112. $VARS = $_POST;
  113. define("GET", false);
  114. } else {
  115. $VARS = $_GET;
  116. define("GET", true);
  117. }
  118. /**
  119. * Checks if a string or whatever is empty.
  120. * @param $str The thingy to check
  121. * @return boolean True if it's empty or whatever.
  122. */
  123. function is_empty($str) {
  124. return (is_null($str) || !isset($str) || $str == '');
  125. }
  126. /**
  127. * I18N string getter. If the key doesn't exist, outputs the key itself.
  128. * @param string $key I18N string key
  129. * @param boolean $echo whether to echo the result or return it (default echo)
  130. */
  131. function lang($key, $echo = true) {
  132. if (array_key_exists($key, STRINGS)) {
  133. $str = STRINGS[$key];
  134. } else {
  135. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  136. $str = $key;
  137. }
  138. if ($echo) {
  139. echo $str;
  140. } else {
  141. return $str;
  142. }
  143. }
  144. /**
  145. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  146. * @param string $key I18N string key
  147. * @param array $replace key-value array of replacements.
  148. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  149. * result will be "hello 123".
  150. * @param boolean $echo whether to echo the result or return it (default echo)
  151. */
  152. function lang2($key, $replace, $echo = true) {
  153. if (array_key_exists($key, STRINGS)) {
  154. $str = STRINGS[$key];
  155. } else {
  156. trigger_error("Language key \"$key\" does not exist in " . LANGUAGE, E_USER_WARNING);
  157. $str = $key;
  158. }
  159. foreach ($replace as $find => $repl) {
  160. $str = str_replace("{" . $find . "}", $repl, $str);
  161. }
  162. if ($echo) {
  163. echo $str;
  164. } else {
  165. return $str;
  166. }
  167. }
  168. function dieifnotloggedin() {
  169. if ($_SESSION['loggedin'] != true) {
  170. sendError("Session expired. Please log out and log in again.");
  171. }
  172. }
  173. /**
  174. * Check if the previous database action had a problem.
  175. * @param array $specials int=>string array with special response messages for SQL errors
  176. */
  177. function checkDBError($specials = []) {
  178. global $database;
  179. $errors = $database->error();
  180. if (!is_null($errors[1])) {
  181. foreach ($specials as $code => $text) {
  182. if ($errors[1] == $code) {
  183. sendError($text);
  184. }
  185. }
  186. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  187. }
  188. }
  189. /*
  190. * http://stackoverflow.com/a/20075147
  191. */
  192. if (!function_exists('base_url')) {
  193. function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) {
  194. if (isset($_SERVER['HTTP_HOST'])) {
  195. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  196. $hostname = $_SERVER['HTTP_HOST'];
  197. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  198. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  199. $core = $core[0];
  200. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  201. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  202. $base_url = sprintf($tmplt, $http, $hostname, $end);
  203. } else
  204. $base_url = 'http://localhost/';
  205. if ($parse) {
  206. $base_url = parse_url($base_url);
  207. if (isset($base_url['path']))
  208. if ($base_url['path'] == '/')
  209. $base_url['path'] = '';
  210. }
  211. return $base_url;
  212. }
  213. }
  214. function redirectIfNotLoggedIn() {
  215. if ($_SESSION['loggedin'] !== TRUE) {
  216. header('Location: ' . URL . '/index.php');
  217. die();
  218. }
  219. }