]> git.openstreetmap.org Git - nominatim.git/blob - lib/init-website.php
Merge pull request #1247 from mtmail/exit-with-error-code
[nominatim.git] / lib / init-website.php
1 <?php
2
3 require_once('init.php');
4 require_once('ParameterParser.php');
5 require_once('DatabaseError.php');
6 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
7
8 /***************************************************************************
9  *
10  * Error handling functions
11  *
12  */
13
14
15 function chksql($oSql, $sMsg = 'Database request failed')
16 {
17     if (!PEAR::isError($oSql)) return $oSql;
18
19     throw new Nominatim\DatabaseError($sMsg, 500, null, $oSql);
20 }
21
22
23 function userError($sMsg)
24 {
25     throw new Exception($sMsg, 400);
26 }
27
28
29 function exception_handler_html($exception)
30 {
31     http_response_code($exception->getCode());
32     header('Content-type: text/html; charset=UTF-8');
33     include(CONST_BasePath.'/lib/template/error-html.php');
34 }
35
36 function exception_handler_json($exception)
37 {
38     http_response_code($exception->getCode());
39     header('Content-type: application/json; charset=utf-8');
40     include(CONST_BasePath.'/lib/template/error-json.php');
41 }
42
43 function exception_handler_xml($exception)
44 {
45     http_response_code($exception->getCode());
46     header('Content-type: text/xml; charset=utf-8');
47     echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
48     include(CONST_BasePath.'/lib/template/error-xml.php');
49 }
50
51
52 function set_exception_handler_by_format($sFormat = 'html')
53 {
54     if ($sFormat == 'html') {
55         set_exception_handler('exception_handler_html');
56     } elseif ($sFormat == 'xml') {
57         set_exception_handler('exception_handler_xml');
58     } else {
59         set_exception_handler('exception_handler_json');
60     }
61 }
62 // set a default
63 set_exception_handler_by_format();
64
65
66 /***************************************************************************
67  * HTTP Reply header setup
68  */
69
70 if (CONST_NoAccessControl) {
71     header('Access-Control-Allow-Origin: *');
72     header('Access-Control-Allow-Methods: OPTIONS,GET');
73     if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
74         header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
75     }
76 }
77 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
78
79 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');