Simple API and database schema for storing and retrieving OpenStreetMap POI data
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.

80 lines
2.0 KiB

  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: application/json; charset=utf-8');
  13. //
  14. // Composer
  15. require __DIR__ . '/vendor/autoload.php';
  16. date_default_timezone_set($SETTINGS['timezone']);
  17. function sendError($msg) {
  18. header('HTTP/1.1 500 Internal Server Error');
  19. exit(json_encode(["status" => "ERROR", "message" => $msg]));
  20. }
  21. // Database settings
  22. // Also inits database and stuff
  23. use Medoo\Medoo;
  24. $database;
  25. try {
  26. $database = new Medoo([
  27. 'database_type' => $SETTINGS['database']['type'],
  28. 'database_name' => $SETTINGS['database']['name'],
  29. 'server' => $SETTINGS['database']['server'],
  30. 'username' => $SETTINGS['database']['user'],
  31. 'password' => $SETTINGS['database']['password'],
  32. 'charset' => $SETTINGS['database']['charset']
  33. ]);
  34. } catch (Exception $ex) {
  35. sendError("Database error. Try again later. $ex");
  36. }
  37. if (!$SETTINGS['debug']) {
  38. error_reporting(0);
  39. } else {
  40. error_reporting(E_ALL);
  41. ini_set('display_errors', 'On');
  42. }
  43. $VARS;
  44. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  45. $VARS = $_POST;
  46. define("GET", false);
  47. } else {
  48. $VARS = $_GET;
  49. define("GET", true);
  50. }
  51. /**
  52. * Check if the previous database action had a problem.
  53. * @param array $specials int=>string array with special response messages for SQL errors
  54. */
  55. function checkDBError($specials = []) {
  56. global $database;
  57. $errors = $database->error();
  58. if (!is_null($errors[1])) {
  59. foreach ($specials as $code => $text) {
  60. if ($errors[1] == $code) {
  61. sendError($text);
  62. }
  63. }
  64. sendError("A database error occurred: " . $errors[2]);
  65. }
  66. }