Skip to content

Commit 1792dbb

Browse files
committedOct 28, 2012
Fixes #14869: Provide a simple API for outputting JSON
Conflicts: core/json_api.php
1 parent 62a24e9 commit 1792dbb

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed
 

‎core/json_api.php

+75-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
* @uses url_api.php
2727
*/
2828

29-
require_api( 'url_api.php' );
29+
/**
30+
* requires url_api
31+
*/
32+
require_once( 'url_api.php' );
33+
require_once( 'database_api.php' );
34+
require_once( 'lang_api.php' );
3035

3136
/**
3237
* Get a chunk of JSON from a given URL.
@@ -44,3 +49,72 @@ function json_url( $p_url, $p_member = null ) {
4449
return $t_json->$p_member;
4550
}
4651
}
52+
53+
/**
54+
* JSON error handler
55+
*
56+
* <p>Ensures that all necessary headers are set and terminates processing after being invoked.</p>
57+
*/
58+
function json_error_handler( $p_type, $p_error, $p_file, $p_line, $p_context ) {
59+
# flush any language overrides to return to user's natural default
60+
if( function_exists( 'db_is_connected' ) ) {
61+
if( db_is_connected() ) {
62+
lang_push( lang_get_default() );
63+
}
64+
}
65+
66+
# build an appropriate error string
67+
switch( $p_type ) {
68+
case E_WARNING:
69+
$t_error_type = 'SYSTEM WARNING';
70+
$t_error_description = $p_error;
71+
break;
72+
case E_NOTICE:
73+
$t_error_type = 'SYSTEM NOTICE';
74+
$t_error_description = $p_error;
75+
break;
76+
case E_USER_ERROR:
77+
$t_error_type = "APPLICATION ERROR #$p_error";
78+
$t_error_description = error_string( $p_error );
79+
break;
80+
case E_USER_WARNING:
81+
$t_error_type = "APPLICATION WARNING #$p_error";
82+
$t_error_description = error_string( $p_error );
83+
break;
84+
case E_USER_NOTICE:
85+
# used for debugging
86+
$t_error_type = 'DEBUG';
87+
$t_error_description = $p_error;
88+
break;
89+
default:
90+
#shouldn't happen, just display the error just in case
91+
$t_error_type = '';
92+
$t_error_description = $p_error;
93+
}
94+
95+
json_output_raw(array(
96+
'status' => 'ERROR',
97+
'type' => $t_error_type,
98+
'contents' => $t_error_description
99+
));
100+
}
101+
/**
102+
* Outputs the specified contents inside a json response with OK status
103+
*
104+
* <p>Ensures that all necessary headers are set and terminates processing.</p>
105+
* @param string $contents The contents to encode
106+
*/
107+
function json_output_response ( $contents = '') {
108+
109+
json_output_raw( array(
110+
'status' => 'OK',
111+
'contents' => $contents
112+
) );
113+
}
114+
115+
function json_output_raw( $contents ) {
116+
117+
header('Content-Type: application/json');
118+
echo json_encode( $contents );
119+
exit();
120+
}

0 commit comments

Comments
 (0)
Please sign in to comment.