3 require_once('init.php');
4 require_once('ParameterParser.php');
5 require_once('DatabaseError.php');
6 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
8 /***************************************************************************
10 * Error handling functions
15 function chksql($oSql, $sMsg = 'Database request failed')
17 if (!PEAR::isError($oSql)) return $oSql;
19 throw new Nominatim\DatabaseError($sMsg, 500, null, $oSql);
23 function userError($sMsg)
25 throw new Exception($sMsg, 400);
29 function exception_handler_html($exception)
31 http_response_code($exception->getCode());
32 header('Content-type: text/html; charset=UTF-8');
33 include(CONST_BasePath.'/lib/template/error-html.php');
36 function exception_handler_json($exception)
38 http_response_code($exception->getCode());
39 header('Content-type: application/json; charset=utf-8');
40 include(CONST_BasePath.'/lib/template/error-json.php');
43 function exception_handler_xml($exception)
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');
52 function set_exception_handler_by_format($sFormat = 'html')
54 if ($sFormat == 'html') {
55 set_exception_handler('exception_handler_html');
56 } elseif ($sFormat == 'xml') {
57 set_exception_handler('exception_handler_xml');
59 set_exception_handler('exception_handler_json');
63 set_exception_handler_by_format();
66 /***************************************************************************
67 * HTTP Reply header setup
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']);
77 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
79 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');