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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * This file contains global settings and utility functions.
  4. */
  5. ob_start(); // allow sending headers after content
  6. // Unicode, solves almost all stupid encoding problems
  7. header('Content-Type: text/html; charset=utf-8');
  8. // l33t $ecurity h4x
  9. header('X-Content-Type-Options: nosniff');
  10. header('X-XSS-Protection: 1; mode=block');
  11. $session_length = 60 * 60; // 1 hour
  12. session_set_cookie_params($session_length, "/", null, false, true);
  13. session_start(); // stick some cookies in it
  14. //
  15. // Composer
  16. require __DIR__ . '/vendor/autoload.php';
  17. // Settings file
  18. require __DIR__ . '/settings.php';
  19. // List of alert messages
  20. require __DIR__ . '/lang/messages.php';
  21. // text strings (i18n)
  22. require __DIR__ . '/lang/' . LANGUAGE . ".php";
  23. /**
  24. * Kill off the running process and spit out an error message
  25. * @param string $error error message
  26. */
  27. function sendError($error) {
  28. die("<!DOCTYPE html><html><head><title>Error</title></head><body><h1 style='color: red; font-family: sans-serif; font-size:100%;'>" . htmlspecialchars($error) . "</h1></body></html>");
  29. }
  30. date_default_timezone_set(TIMEZONE);
  31. // Database settings
  32. // Also inits database and stuff
  33. use Medoo\Medoo;
  34. $database;
  35. try {
  36. $database = new Medoo([
  37. 'database_type' => DB_TYPE,
  38. 'database_name' => DB_NAME,
  39. 'server' => DB_SERVER,
  40. 'username' => DB_USER,
  41. 'password' => DB_PASS,
  42. 'charset' => DB_CHARSET
  43. ]);
  44. } catch (Exception $ex) {
  45. //header('HTTP/1.1 500 Internal Server Error');
  46. sendError("Database error. Try again later. $ex");
  47. }
  48. if (!DEBUG) {
  49. error_reporting(0);
  50. } else {
  51. error_reporting(E_ALL);
  52. ini_set('display_errors', 'On');
  53. }
  54. $VARS;
  55. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  56. $VARS = $_POST;
  57. define("GET", false);
  58. } else {
  59. $VARS = $_GET;
  60. define("GET", true);
  61. }
  62. /**
  63. * Checks if a string or whatever is empty.
  64. * @param $str The thingy to check
  65. * @return boolean True if it's empty or whatever.
  66. */
  67. function is_empty($str) {
  68. return (is_null($str) || !isset($str) || $str == '');
  69. }
  70. /**
  71. * I18N string getter. If the key doesn't exist, outputs the key itself.
  72. * @param string $key I18N string key
  73. * @param boolean $echo whether to echo the result or return it (default echo)
  74. */
  75. function lang($key, $echo = true) {
  76. if (array_key_exists($key, STRINGS)) {
  77. $str = STRINGS[$key];
  78. } else {
  79. $str = $key;
  80. }
  81. if ($echo) {
  82. echo $str;
  83. } else {
  84. return $str;
  85. }
  86. }
  87. /**
  88. * I18N string getter (with builder). If the key doesn't exist, outputs the key itself.
  89. * @param string $key I18N string key
  90. * @param array $replace key-value array of replacements.
  91. * If the string value is "hello {abc}" and you give ["abc" => "123"], the
  92. * result will be "hello 123".
  93. * @param boolean $echo whether to echo the result or return it (default echo)
  94. */
  95. function lang2($key, $replace, $echo = true) {
  96. if (array_key_exists($key, STRINGS)) {
  97. $str = STRINGS[$key];
  98. } else {
  99. $str = $key;
  100. }
  101. foreach ($replace as $find => $repl) {
  102. $str = str_replace("{" . $find . "}", $repl, $str);
  103. }
  104. if ($echo) {
  105. echo $str;
  106. } else {
  107. return $str;
  108. }
  109. }
  110. function dieifnotloggedin() {
  111. if ($_SESSION['loggedin'] != true) {
  112. sendError("Session expired. Please log out and log in again.");
  113. }
  114. }
  115. /**
  116. * Check if the previous database action had a problem.
  117. * @param array $specials int=>string array with special response messages for SQL errors
  118. */
  119. function checkDBError($specials = []) {
  120. global $database;
  121. $errors = $database->error();
  122. if (!is_null($errors[1])) {
  123. foreach ($specials as $code => $text) {
  124. if ($errors[1] == $code) {
  125. sendError($text);
  126. }
  127. }
  128. sendError("A database error occurred:<br /><code>" . $errors[2] . "</code>");
  129. }
  130. }
  131. /*
  132. * http://stackoverflow.com/a/20075147/2534036
  133. */
  134. if (!function_exists('base_url')) {
  135. function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) {
  136. if (isset($_SERVER['HTTP_HOST'])) {
  137. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  138. $hostname = $_SERVER['HTTP_HOST'];
  139. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  140. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  141. $core = $core[0];
  142. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  143. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  144. $base_url = sprintf($tmplt, $http, $hostname, $end);
  145. } else
  146. $base_url = 'http://localhost/';
  147. if ($parse) {
  148. $base_url = parse_url($base_url);
  149. if (isset($base_url['path']))
  150. if ($base_url['path'] == '/')
  151. $base_url['path'] = '';
  152. }
  153. return $base_url;
  154. }
  155. }
  156. function redirectIfNotLoggedIn() {
  157. if ($_SESSION['loggedin'] !== TRUE) {
  158. header('Location: ' . URL . '/index.php');
  159. die();
  160. }
  161. }