26
26
* @uses url_api.php
27
27
*/
28
28
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 ' );
30
35
31
36
/**
32
37
* Get a chunk of JSON from a given URL.
@@ -44,3 +49,72 @@ function json_url( $p_url, $p_member = null ) {
44
49
return $ t_json ->$ p_member ;
45
50
}
46
51
}
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