1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | |
---|
25 | #_____________________ PHP 5.2 |
---|
26 | if (! function_exists('json_encode')) { |
---|
27 | function json_encode($data) { |
---|
28 | switch (gettype($data)) { |
---|
29 | case 'boolean': |
---|
30 | return ($data ? 'true' : 'false'); |
---|
31 | case 'null': |
---|
32 | case 'NULL': |
---|
33 | return 'null'; |
---|
34 | case 'integer': |
---|
35 | case 'double': |
---|
36 | return $data; |
---|
37 | case 'string': |
---|
38 | return '"'. str_replace(array("\\",'"',"/","\n","\r","\t"), array("\\\\",'\"',"\\/","\\n","\\r","\\t"), $data) .'"'; |
---|
39 | case 'object': |
---|
40 | case 'array': |
---|
41 | if ($data === array()) return '[]'; # empty array |
---|
42 | if (range(0, count($data) - 1) !== array_keys($data) ) { # string keys, unordered, non-incremental keys, .. - whatever, make object |
---|
43 | $out = "\n".'{'; |
---|
44 | foreach($data as $key => $value) { |
---|
45 | $out .= json_encode((string) $key) . ':' . json_encode($value) . ','; |
---|
46 | } |
---|
47 | $out = substr($out, 0, -1) . "\n". '}'; |
---|
48 | }else{ |
---|
49 | # regular array |
---|
50 | $out = "\n".'[' . join("\n".',', array_map('json_encode', $data)) ."\n".']'; |
---|
51 | } |
---|
52 | return $out; |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | class PwgJsonEncoder extends PwgResponseEncoder |
---|
58 | { |
---|
59 | function encodeResponse($response) |
---|
60 | { |
---|
61 | $respClass = strtolower( get_class($response) ); |
---|
62 | if ($respClass=='pwgerror') |
---|
63 | { |
---|
64 | return json_encode( |
---|
65 | array( |
---|
66 | 'stat' => 'fail', |
---|
67 | 'err' => $response->code(), |
---|
68 | 'message' => $response->message(), |
---|
69 | ) |
---|
70 | ); |
---|
71 | } |
---|
72 | parent::flattenResponse($response); |
---|
73 | return json_encode( |
---|
74 | array( |
---|
75 | 'stat' => 'ok', |
---|
76 | 'result' => $response, |
---|
77 | ) |
---|
78 | ); |
---|
79 | } |
---|
80 | |
---|
81 | function getContentType() |
---|
82 | { |
---|
83 | return 'text/plain'; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | ?> |
---|