]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
67fb52d1a687d654c87255f4c61371b16b994979
[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 }
25
26 function exception_handler_json($exception)
27 {
28     http_response_code($exception->getCode());
29     header('Content-type: application/json; charset=utf-8');
30     include(CONST_BasePath.'/lib/template/error-json.php');
31 }
32
33 function exception_handler_xml($exception)
34 {
35     http_response_code($exception->getCode());
36     header('Content-type: text/xml; charset=utf-8');
37     echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
38     include(CONST_BasePath.'/lib/template/error-xml.php');
39 }
40
41
42 function set_exception_handler_by_format($sFormat = 'html')
43 {
44     if ($sFormat == 'html') {
45         set_exception_handler('exception_handler_html');
46     } elseif ($sFormat == 'xml') {
47         set_exception_handler('exception_handler_xml');
48     } else {
49         set_exception_handler('exception_handler_json');
50     }
51 }
52 // set a default
53 set_exception_handler_by_format();
54
55
56 /***************************************************************************
57  * HTTP Reply header setup
58  */
59
60 if (CONST_NoAccessControl) {
61     header('Access-Control-Allow-Origin: *');
62     header('Access-Control-Allow-Methods: OPTIONS,GET');
63     if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
64         header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
65     }
66 }
67 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
68
69 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');