An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/

required.php 6.5KB

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