]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/init-website.php
These days the OSM wikipedia tab no longer contains URLs
[nominatim.git] / lib-php / init-website.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 require_once('init.php');
12 require_once('ParameterParser.php');
13 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
14
15 /***************************************************************************
16  *
17  * Error handling functions
18  *
19  */
20
21 function userError($sMsg)
22 {
23     throw new \Exception($sMsg, 400);
24 }
25
26
27 function exception_handler_json($exception)
28 {
29     http_response_code($exception->getCode() == 0 ? 500 : $exception->getCode());
30     header('Content-type: application/json; charset=utf-8');
31     include(CONST_LibDir.'/template/error-json.php');
32     exit();
33 }
34
35 function exception_handler_xml($exception)
36 {
37     http_response_code($exception->getCode() == 0 ? 500 : $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');
41     exit();
42 }
43
44 function shutdown_exception_handler_xml()
45 {
46     $error = error_get_last();
47     if ($error !== null && $error['type'] === E_ERROR) {
48         exception_handler_xml(new \Exception($error['message'], 500));
49     }
50 }
51
52 function shutdown_exception_handler_json()
53 {
54     $error = error_get_last();
55     if ($error !== null && $error['type'] === E_ERROR) {
56         exception_handler_json(new \Exception($error['message'], 500));
57     }
58 }
59
60
61 function set_exception_handler_by_format($sFormat = null)
62 {
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.
66
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');
72     } else {
73         set_exception_handler('exception_handler_json');
74         register_shutdown_function('shutdown_exception_handler_json');
75     }
76 }
77 // set a default
78 set_exception_handler_by_format();
79
80
81 /***************************************************************************
82  * HTTP Reply header setup
83  */
84
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']);
90     }
91 }
92 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
93     exit;
94 }
95
96 if (CONST_Debug) {
97     header('Content-type: text/html; charset=utf-8');
98 }