]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
documentation wording: snipped->snippet
[nominatim.git] / lib / 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_html($exception)
20 {
21     http_response_code($exception->getCode());
22     header('Content-type: text/html; charset=UTF-8');
23     include(CONST_BasePath.'/lib/template/error-html.php');
24     exit();
25 }
26
27 function exception_handler_json($exception)
28 {
29     http_response_code($exception->getCode());
30     header('Content-type: application/json; charset=utf-8');
31     include(CONST_BasePath.'/lib/template/error-json.php');
32     exit();
33 }
34
35 function exception_handler_xml($exception)
36 {
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_BasePath.'/lib/template/error-xml.php');
41     exit();
42 }
43
44 function shutdown_exception_handler_html()
45 {
46     $error = error_get_last();
47     if ($error !== null && $error['type'] === E_ERROR) {
48         exception_handler_html(new Exception($error['message'], 500));
49     }
50 }
51
52 function shutdown_exception_handler_xml()
53 {
54     $error = error_get_last();
55     if ($error !== null && $error['type'] === E_ERROR) {
56         exception_handler_xml(new Exception($error['message'], 500));
57     }
58 }
59
60 function shutdown_exception_handler_json()
61 {
62     $error = error_get_last();
63     if ($error !== null && $error['type'] === E_ERROR) {
64         exception_handler_json(new Exception($error['message'], 500));
65     }
66 }
67
68
69 function set_exception_handler_by_format($sFormat = null)
70 {
71     // Multiple calls to register_shutdown_function will cause multiple callbacks
72     // to be executed, we only want the last executed. Thus we don't want to register
73     // one by default without an explicit $sFormat set.
74
75     if (!isset($sFormat)) {
76         set_exception_handler('exception_handler_html');
77     } elseif ($sFormat == 'html') {
78         set_exception_handler('exception_handler_html');
79         register_shutdown_function('shutdown_exception_handler_html');
80     } elseif ($sFormat == 'xml') {
81         set_exception_handler('exception_handler_xml');
82         register_shutdown_function('shutdown_exception_handler_xml');
83     } else {
84         set_exception_handler('exception_handler_json');
85         register_shutdown_function('shutdown_exception_handler_json');
86     }
87 }
88 // set a default
89 set_exception_handler_by_format();
90
91
92 /***************************************************************************
93  * HTTP Reply header setup
94  */
95
96 if (CONST_NoAccessControl) {
97     header('Access-Control-Allow-Origin: *');
98     header('Access-Control-Allow-Methods: OPTIONS,GET');
99     if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
100         header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
101     }
102 }
103 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
104
105 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');