@@ -111,38 +111,64 @@ public function get_ispconfig_url() {
111111
112112 function json_encode ($ data ) {
113113 if (!function_exists ('json_encode ' )){
114- switch ($ type = gettype ($ data )){
115- case 'NULL ' :
116- return 'null ' ;
117- case 'boolean ' :
118- return ($ data ? 'true ' : 'false ' );
119- case 'integer ' :
120- case 'double ' :
121- case 'float ' :
122- return $ data ;
123- case 'string ' :
124- return '" ' . addslashes ($ data ) . '" ' ;
125- case 'object ' :
126- $ data = get_object_vars ($ data );
127- case 'array ' :
128- $ output_index_count = 0 ;
129- $ output_indexed = array ();
130- $ output_associative = array ();
131- foreach ($ data as $ key => $ value ){
132- $ output_indexed [] = $ this ->json_encode ($ value );
133- $ output_associative [] = $ this ->json_encode ($ key ) . ': ' . $ this ->json_encode ($ value );
134- if ($ output_index_count !== NULL && $ output_index_count ++ !== $ key ){
135- $ output_index_count = NULL ;
136- }
114+ if (is_array ($ data ) || is_object ($ data )){
115+ $ islist = is_array ($ data ) && (empty ($ data ) || array_keys ($ data ) === range (0 ,count ($ data )-1 ));
116+
117+ if ($ islist ){
118+ $ json = '[ ' . implode (', ' , array_map (array ($ this , "json_encode " ), $ data ) ) . '] ' ;
119+ } else {
120+ $ items = Array ();
121+ foreach ( $ data as $ key => $ value ) {
122+ $ items [] = $ this ->json_encode ("$ key " ) . ': ' . $ this ->json_encode ($ value );
137123 }
138- if ($ output_index_count !== NULL ){
139- return '[ ' . implode (', ' , $ output_indexed ) . '] ' ;
140- } else {
141- return '{ ' . implode (', ' , $ output_associative ) . '} ' ;
124+ $ json = '{ ' . implode (', ' , $ items ) . '} ' ;
125+ }
126+ } elseif (is_string ($ data )){
127+ # Escape non-printable or Non-ASCII characters.
128+ # I also put the \\ character first, as suggested in comments on the 'addclashes' page.
129+ $ string = '" ' .addcslashes ($ data , "\\\"\n\r\t/ " .chr (8 ).chr (12 )).'" ' ;
130+ $ json = '' ;
131+ $ len = strlen ($ string );
132+ # Convert UTF-8 to Hexadecimal Codepoints.
133+ for ($ i = 0 ; $ i < $ len ; $ i ++){
134+ $ char = $ string [$ i ];
135+ $ c1 = ord ($ char );
136+
137+ # Single byte;
138+ if ($ c1 <128 ){
139+ $ json .= ($ c1 > 31 ) ? $ char : sprintf ("\\u%04x " , $ c1 );
140+ continue ;
142141 }
143- default :
144- return '' ; // Not supported
142+
143+ # Double byte
144+ $ c2 = ord ($ string [++$ i ]);
145+ if (($ c1 & 32 ) === 0 ){
146+ $ json .= sprintf ("\\u%04x " , ($ c1 - 192 ) * 64 + $ c2 - 128 );
147+ continue ;
148+ }
149+
150+ # Triple
151+ $ c3 = ord ($ string [++$ i ]);
152+ if (($ c1 & 16 ) === 0 ){
153+ $ json .= sprintf ("\\u%04x " , (($ c1 - 224 ) <<12 ) + (($ c2 - 128 ) << 6 ) + ($ c3 - 128 ));
154+ continue ;
155+ }
156+
157+ # Quadruple
158+ $ c4 = ord ($ string [++$ i ]);
159+ if (($ c1 & 8 ) === 0 ){
160+ $ u = (($ c1 & 15 ) << 2 ) + (($ c2 >>4 ) & 3 ) - 1 ;
161+
162+ $ w1 = (54 <<10 ) + ($ u <<6 ) + (($ c2 & 15 ) << 2 ) + (($ c3 >>4 ) & 3 );
163+ $ w2 = (55 <<10 ) + (($ c3 & 15 )<<6 ) + ($ c4 -128 );
164+ $ json .= sprintf ("\\u%04x \\u%04x " , $ w1 , $ w2 );
165+ }
166+ }
167+ } else {
168+ # int, floats, bools, null
169+ $ json = strtolower (var_export ($ data , true ));
145170 }
171+ return $ json ;
146172 } else {
147173 return json_encode ($ data );
148174 }
0 commit comments