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_json($exception)
21 http_response_code($exception->getCode());
22 header('Content-type: application/json; charset=utf-8');
23 include(CONST_LibDir.'/template/error-json.php');
27 function exception_handler_xml($exception)
29 http_response_code($exception->getCode());
30 header('Content-type: text/xml; charset=utf-8');
31 echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
32 include(CONST_LibDir.'/template/error-xml.php');
36 function shutdown_exception_handler_xml()
38 $error = error_get_last();
39 if ($error !== null && $error['type'] === E_ERROR) {
40 exception_handler_xml(new Exception($error['message'], 500));
44 function shutdown_exception_handler_json()
46 $error = error_get_last();
47 if ($error !== null && $error['type'] === E_ERROR) {
48 exception_handler_json(new Exception($error['message'], 500));
53 function set_exception_handler_by_format($sFormat = null)
55 // Multiple calls to register_shutdown_function will cause multiple callbacks
56 // to be executed, we only want the last executed. Thus we don't want to register
57 // one by default without an explicit $sFormat set.
59 if (!isset($sFormat)) {
60 set_exception_handler('exception_handler_json');
61 } elseif ($sFormat == 'xml') {
62 set_exception_handler('exception_handler_xml');
63 register_shutdown_function('shutdown_exception_handler_xml');
65 set_exception_handler('exception_handler_json');
66 register_shutdown_function('shutdown_exception_handler_json');
70 set_exception_handler_by_format();
73 /***************************************************************************
74 * HTTP Reply header setup
77 if (CONST_NoAccessControl) {
78 header('Access-Control-Allow-Origin: *');
79 header('Access-Control-Allow-Methods: OPTIONS,GET');
80 if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
81 header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
84 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
89 header('Content-type: text/html; charset=utf-8');