3 require_once('init.php');
4 require_once('ParameterParser.php');
5 require_once(CONST_Debug ? 'DebugHtml.php' : 'DebugNone.php');
7 /***************************************************************************
9 * Error handling functions
13 function userError($sMsg)
15 throw new Exception($sMsg, 400);
19 function exception_handler_html($exception)
21 http_response_code($exception->getCode());
22 header('Content-type: text/html; charset=UTF-8');
23 include(CONST_BasePath.'/lib/template/error-html.php');
27 function exception_handler_json($exception)
29 http_response_code($exception->getCode());
30 header('Content-type: application/json; charset=utf-8');
31 include(CONST_BasePath.'/lib/template/error-json.php');
35 function exception_handler_xml($exception)
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');
44 function shutdown_exception_handler_html()
46 $error = error_get_last();
47 if ($error !== null && $error['type'] === E_ERROR) {
48 exception_handler_html(new Exception($error['message'], 500));
52 function shutdown_exception_handler_xml()
54 $error = error_get_last();
55 if ($error !== null && $error['type'] === E_ERROR) {
56 exception_handler_xml(new Exception($error['message'], 500));
60 function shutdown_exception_handler_json()
62 $error = error_get_last();
63 if ($error !== null && $error['type'] === E_ERROR) {
64 exception_handler_json(new Exception($error['message'], 500));
69 function set_exception_handler_by_format($sFormat = null)
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.
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');
84 set_exception_handler('exception_handler_json');
85 register_shutdown_function('shutdown_exception_handler_json');
89 set_exception_handler_by_format();
92 /***************************************************************************
93 * HTTP Reply header setup
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']);
103 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') exit;
105 if (CONST_Debug) header('Content-type: text/html; charset=utf-8');