3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 require_once('init.php');
12 require_once('ParameterParser.php');
13 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
15 /***************************************************************************
17 * Error handling functions
21 function userError($sMsg)
23 throw new \Exception($sMsg, 400);
27 function exception_handler_json($exception)
29 http_response_code($exception->getCode());
30 header('Content-type: application/json; charset=utf-8');
31 include(CONST_LibDir.'/template/error-json.php');
35 function exception_handler_xml($exception)
37 http_response_code($exception->getCode());
38 header('Content-type: text/xml; charset=utf-8');
39 echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
40 include(CONST_LibDir.'/template/error-xml.php');
44 function shutdown_exception_handler_xml()
46 $error = error_get_last();
47 if ($error !== null && $error['type'] === E_ERROR) {
48 exception_handler_xml(new \Exception($error['message'], 500));
52 function shutdown_exception_handler_json()
54 $error = error_get_last();
55 if ($error !== null && $error['type'] === E_ERROR) {
56 exception_handler_json(new \Exception($error['message'], 500));
61 function set_exception_handler_by_format($sFormat = null)
63 // Multiple calls to register_shutdown_function will cause multiple callbacks
64 // to be executed, we only want the last executed. Thus we don't want to register
65 // one by default without an explicit $sFormat set.
67 if (!isset($sFormat)) {
68 set_exception_handler('exception_handler_json');
69 } elseif ($sFormat == 'xml') {
70 set_exception_handler('exception_handler_xml');
71 register_shutdown_function('shutdown_exception_handler_xml');
73 set_exception_handler('exception_handler_json');
74 register_shutdown_function('shutdown_exception_handler_json');
78 set_exception_handler_by_format();
81 /***************************************************************************
82 * HTTP Reply header setup
85 if (CONST_NoAccessControl) {
86 header('Access-Control-Allow-Origin: *');
87 header('Access-Control-Allow-Methods: OPTIONS,GET');
88 if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
89 header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
92 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
97 header('Content-type: text/html; charset=utf-8');