]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/init-website.php
Merge pull request #2390 from lonvia/responsible-disclosure
[nominatim.git] / lib-php / init-website.php
1 <?php
2
3 require_once('init.php');
4 require_once('ParameterParser.php');
5 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
6
7 /***************************************************************************
8  *
9  * Error handling functions
10  *
11  */
12
13 function userError($sMsg)
14 {
15     throw new Exception($sMsg, 400);
16 }
17
18
19 function exception_handler_json($exception)
20 {
21     http_response_code($exception->getCode());
22     header('Content-type: application/json; charset=utf-8');
23     include(CONST_LibDir.'/template/error-json.php');
24     exit();
25 }
26
27 function exception_handler_xml($exception)
28 {
29     http_response_code($exception->getCode());
30     header('Content-type: text/xml; charset=utf-8');
31     echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
32     include(CONST_LibDir.'/template/error-xml.php');
33     exit();
34 }
35
36 function shutdown_exception_handler_xml()
37 {
38     $error = error_get_last();
39     if ($error !== null && $error['type'] === E_ERROR) {
40         exception_handler_xml(new Exception($error['message'], 500));
41     }
42 }
43
44 function shutdown_exception_handler_json()
45 {
46     $error = error_get_last();
47     if ($error !== null && $error['type'] === E_ERROR) {
48         exception_handler_json(new Exception($error['message'], 500));
49     }
50 }
51
52
53 function set_exception_handler_by_format($sFormat = null)
54 {
55     // Multiple calls to register_shutdown_function will cause multiple callbacks
56     // to be executed, we only want the last executed. Thus we don't want to register
57     // one by default without an explicit $sFormat set.
58
59     if (!isset($sFormat)) {
60         set_exception_handler('exception_handler_json');
61     } elseif ($sFormat == 'xml') {
62         set_exception_handler('exception_handler_xml');
63         register_shutdown_function('shutdown_exception_handler_xml');
64     } else {
65         set_exception_handler('exception_handler_json');
66         register_shutdown_function('shutdown_exception_handler_json');
67     }
68 }
69 // set a default
70 set_exception_handler_by_format();
71
72
73 /***************************************************************************
74  * HTTP Reply header setup
75  */
76
77 if (CONST_NoAccessControl) {
78     header('Access-Control-Allow-Origin: *');
79     header('Access-Control-Allow-Methods: OPTIONS,GET');
80     if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
81         header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
82     }
83 }
84 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
85
86 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');