An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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