Friendly, easy, lightweight, self-hostable CAPTCHA service. https://captcheck.netsyms.com
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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * This file contains global settings and utility functions.
  4. */
  5. ob_start(); // allow sending headers after content
  6. //
  7. // Unicode, solves almost all stupid encoding problems
  8. header('Content-Type: application/json; charset=utf-8');
  9. header('X-Content-Type-Options: nosniff');
  10. header('Access-Control-Allow-Origin: *');
  11. $session_length = (60 * 60) * 8; // 1 hour x 8 = 8 hours
  12. session_set_cookie_params($session_length, "/", null, false, true);
  13. session_start(); // stick some cookies in it
  14. // renew session cookie
  15. setcookie(session_name(), session_id(), time() + $session_length);
  16. // Composer
  17. require __DIR__ . '/vendor/autoload.php';
  18. require __DIR__ . '/settings.php';
  19. /**
  20. * Kill off the running process and spit out an error message
  21. * @param string $error error message
  22. * @param string $fault who's fault the error is: "server" (default) or "client".
  23. */
  24. function sendError($error, $fault = "server") {
  25. if ($fault == "server") {
  26. header('HTTP/1.1 500 Internal Server Error');
  27. $code = 500;
  28. } else {
  29. header('HTTP/1.1 400 Bad Request');
  30. $code = 400;
  31. }
  32. die(json_encode(["error" => $error, "code" => $code]));
  33. }
  34. // Database settings
  35. // Also inits database and stuff
  36. use Medoo\Medoo;
  37. $database;
  38. try {
  39. $database = new Medoo([
  40. 'database_type' => DB_TYPE,
  41. 'database_name' => DB_NAME,
  42. 'server' => DB_SERVER,
  43. 'username' => DB_USER,
  44. 'password' => DB_PASS,
  45. 'charset' => DB_CHARSET
  46. ]);
  47. } catch (Exception $ex) {
  48. sendError("Database error. Try again later.");
  49. }
  50. if (!DEBUG) {
  51. error_reporting(0);
  52. } else {
  53. error_reporting(E_ALL);
  54. ini_set('display_errors', 'On');
  55. }
  56. $VARS;
  57. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  58. $VARS = $_POST;
  59. define("GET", false);
  60. } else {
  61. $VARS = $_GET;
  62. define("GET", true);
  63. }
  64. /**
  65. * Checks if a string or whatever is empty.
  66. * @param $str The thingy to check
  67. * @return boolean True if it's empty or whatever.
  68. */
  69. function is_empty($str) {
  70. return (is_null($str) || !isset($str) || $str == '');
  71. }
  72. /*
  73. * http://stackoverflow.com/a/20075147/2534036
  74. */
  75. if (!function_exists('base_url')) {
  76. function base_url($atRoot = FALSE, $atCore = FALSE, $parse = FALSE) {
  77. if (isset($_SERVER['HTTP_HOST'])) {
  78. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  79. $hostname = $_SERVER['HTTP_HOST'];
  80. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  81. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  82. $core = $core[0];
  83. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  84. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  85. $base_url = sprintf($tmplt, $http, $hostname, $end);
  86. } else
  87. $base_url = 'http://localhost/';
  88. if ($parse) {
  89. $base_url = parse_url($base_url);
  90. if (isset($base_url['path']))
  91. if ($base_url['path'] == '/')
  92. $base_url['path'] = '';
  93. }
  94. return $base_url;
  95. }
  96. }
  97. /**
  98. * Attempts to discover the user's IP address.
  99. * @return string IP string or "NOT FOUND".
  100. */
  101. function getUserIP() {
  102. $ip = "";
  103. if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  104. $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
  105. } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  106. $ip = $_SERVER["HTTP_CLIENT_IP"];
  107. } else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  108. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  109. } else if (isset($_SERVER["HTTP_X_FORWARDED"])) {
  110. $ip = $_SERVER["HTTP_X_FORWARDED"];
  111. } else if (isset($_SERVER["HTTP_FORWARDED_FOR"])) {
  112. $ip = $_SERVER["HTTP_FORWARDED_FOR"];
  113. } else if (isset($_SERVER["HTTP_FORWARDED"])) {
  114. $ip = $_SERVER["HTTP_FORWARDED"];
  115. } else if (isset($_SERVER["REMOTE_ADDR"])) {
  116. $ip = $_SERVER["REMOTE_ADDR"];
  117. } else {
  118. $ip = "NOT FOUND";
  119. }
  120. return $ip;
  121. }