1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
5 | // +-----------------------------------------------------------------------+ |
---|
6 | // | branch : BSF (Best So Far) |
---|
7 | // | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $ |
---|
8 | // | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $ |
---|
9 | // | last modifier : $Author: rvelices $ |
---|
10 | // | revision : $Rev: 1678 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | |
---|
28 | class PwgXmlWriter |
---|
29 | { |
---|
30 | var $_indent; |
---|
31 | var $_indentStr; |
---|
32 | |
---|
33 | var $_elementStack; |
---|
34 | var $_lastTagOpen; |
---|
35 | var $_indentLevel; |
---|
36 | |
---|
37 | var $_encodedXml; |
---|
38 | |
---|
39 | function PwgXmlWriter() |
---|
40 | { |
---|
41 | $this->_elementStack = array(); |
---|
42 | $this->_lastTagOpen = false; |
---|
43 | $this->_indentLevel = 0; |
---|
44 | |
---|
45 | $this->_encodedXml = ''; |
---|
46 | $this->_indent = true; |
---|
47 | $this->_indentStr = "\t"; |
---|
48 | } |
---|
49 | |
---|
50 | function &getOutput() |
---|
51 | { |
---|
52 | return $this->_encodedXml; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | function start_element($name) |
---|
57 | { |
---|
58 | $this->_end_prev(false); |
---|
59 | if (!empty($this->_elementStack)) |
---|
60 | { |
---|
61 | $this->_eol_indent(); |
---|
62 | } |
---|
63 | $this->_indentLevel++; |
---|
64 | $this->_indent(); |
---|
65 | $this->_output( '<'.$name ); |
---|
66 | $this->_lastTagOpen = true; |
---|
67 | array_push( $this->_elementStack, $name); |
---|
68 | } |
---|
69 | |
---|
70 | function end_element($x) |
---|
71 | { |
---|
72 | $close_tag = $this->_end_prev(true); |
---|
73 | $name = array_pop( $this->_elementStack ); |
---|
74 | if ($close_tag) |
---|
75 | { |
---|
76 | $this->_indentLevel--; |
---|
77 | $this->_indent(); |
---|
78 | // $this->_eol_indent(); |
---|
79 | $this->_output('</'.$name.">"); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | function write_content($value) |
---|
84 | { |
---|
85 | $this->_end_prev(false); |
---|
86 | $value = (string)$value; |
---|
87 | $need_cdata = (strpos($value, "\r")!==false)?true:false; |
---|
88 | if ($need_cdata) |
---|
89 | { |
---|
90 | $this->_output( '<![CDATA[' . $value . ']]>' ); |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | $this->_output( htmlspecialchars( $value ) ); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | function write_attribute($name, $value) |
---|
99 | { |
---|
100 | $this->_output(' '.$name.'="'.$this->encode_attribute($value).'"'); |
---|
101 | } |
---|
102 | |
---|
103 | function encode_attribute($value) |
---|
104 | { |
---|
105 | return htmlspecialchars( (string)$value); |
---|
106 | } |
---|
107 | |
---|
108 | function _end_prev($done) |
---|
109 | { |
---|
110 | $ret = true; |
---|
111 | if ($this->_lastTagOpen) |
---|
112 | { |
---|
113 | if ($done) |
---|
114 | { |
---|
115 | $this->_indentLevel--; |
---|
116 | $this->_output( ' />' ); |
---|
117 | //$this->_eol_indent(); |
---|
118 | $ret = false; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | $this->_output( '>' ); |
---|
123 | } |
---|
124 | $this->_lastTagOpen = false; |
---|
125 | } |
---|
126 | return $ret; |
---|
127 | } |
---|
128 | |
---|
129 | function _eol_indent() |
---|
130 | { |
---|
131 | if ($this->_indent) |
---|
132 | $this->_output("\n"); |
---|
133 | } |
---|
134 | |
---|
135 | function _indent() |
---|
136 | { |
---|
137 | if ($this->_indent and |
---|
138 | $this->_indentLevel > count($this->_elementStack) ) |
---|
139 | { |
---|
140 | $this->_output( |
---|
141 | str_repeat( $this->_indentStr, count($this->_elementStack) ) |
---|
142 | ); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | function _output($raw_content) |
---|
147 | { |
---|
148 | $this->_encodedXml .= $raw_content; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | class PwgRestEncoder extends PwgResponseEncoder |
---|
153 | { |
---|
154 | function encodeResponse($response) |
---|
155 | { |
---|
156 | global $lang_info; |
---|
157 | $respClass = strtolower( get_class($response) ); |
---|
158 | if ($respClass=='pwgerror') |
---|
159 | { |
---|
160 | $ret = '<?xml version="1.0"?> |
---|
161 | <rsp stat="fail"> |
---|
162 | <err code="'.$response->code().'" msg="'.htmlspecialchars($response->message()).'" /> |
---|
163 | </rsp>'; |
---|
164 | return $ret; |
---|
165 | } |
---|
166 | |
---|
167 | //parent::flattenResponse($response); |
---|
168 | |
---|
169 | $this->_writer = new PwgXmlWriter(); |
---|
170 | $this->encode($response); |
---|
171 | $ret = $this->_writer->getOutput(); |
---|
172 | $ret = '<?xml version="1.0" encoding="'.$lang_info['charset'].'" ?> |
---|
173 | <rsp stat="ok"> |
---|
174 | '.$ret.' |
---|
175 | </rsp>'; |
---|
176 | |
---|
177 | return $ret; |
---|
178 | } |
---|
179 | |
---|
180 | function getContentType() |
---|
181 | { |
---|
182 | return 'text/xml'; |
---|
183 | } |
---|
184 | |
---|
185 | function encode_array($data, $itemName, $xml_attributes=array()) |
---|
186 | { |
---|
187 | foreach ($data as $item) |
---|
188 | { |
---|
189 | $this->_writer->start_element( $itemName ); |
---|
190 | $this->encode($item, $xml_attributes); |
---|
191 | $this->_writer->end_element( $itemName ); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | function encode_struct($data, $skip_underscore, $xml_attributes=array()) |
---|
196 | { |
---|
197 | foreach ($data as $name => $value) |
---|
198 | { |
---|
199 | if (is_numeric($name)) |
---|
200 | continue; |
---|
201 | if ($skip_underscore and $name[0]=='_') |
---|
202 | continue; |
---|
203 | if ( is_null($value) ) |
---|
204 | continue; // null means we dont put it |
---|
205 | if ( $name==WS_XML_ATTRIBUTES) |
---|
206 | { |
---|
207 | foreach ($value as $attr_name => $attr_value) |
---|
208 | { |
---|
209 | $this->_writer->write_attribute($attr_name, $attr_value); |
---|
210 | } |
---|
211 | unset($data[$name]); |
---|
212 | } |
---|
213 | else if ( isset($xml_attributes[$name]) ) |
---|
214 | { |
---|
215 | $this->_writer->write_attribute($name, $value); |
---|
216 | unset($data[$name]); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | foreach ($data as $name => $value) |
---|
221 | { |
---|
222 | if (is_numeric($name)) |
---|
223 | continue; |
---|
224 | if ($skip_underscore and $name[0]=='_') |
---|
225 | continue; |
---|
226 | if ( is_null($value) ) |
---|
227 | continue; // null means we dont put it |
---|
228 | if ($name!=WS_XML_CONTENT) |
---|
229 | $this->_writer->start_element($name); |
---|
230 | $this->encode($value); |
---|
231 | if ($name!=WS_XML_CONTENT) |
---|
232 | $this->_writer->end_element($name); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | function encode($data, $xml_attributes=array() ) |
---|
237 | { |
---|
238 | switch (gettype($data)) |
---|
239 | { |
---|
240 | case 'null': |
---|
241 | case 'NULL': |
---|
242 | $this->_writer->write_content(''); |
---|
243 | break; |
---|
244 | case 'boolean': |
---|
245 | $this->_writer->write_content($data ? '1' : '0'); |
---|
246 | break; |
---|
247 | case 'integer': |
---|
248 | case 'double': |
---|
249 | $this->_writer->write_content($data); |
---|
250 | break; |
---|
251 | case 'string': |
---|
252 | $this->_writer->write_content($data); |
---|
253 | break; |
---|
254 | case 'array': |
---|
255 | $is_array = range(0, count($data) - 1) === array_keys($data); |
---|
256 | if ($is_array) |
---|
257 | { |
---|
258 | $this->encode_array($data, 'item' ); |
---|
259 | } |
---|
260 | else |
---|
261 | { |
---|
262 | $this->encode_struct($data, false, $xml_attributes); |
---|
263 | } |
---|
264 | break; |
---|
265 | case 'object': |
---|
266 | switch ( strtolower(get_class($data)) ) |
---|
267 | { |
---|
268 | case 'pwgnamedarray': |
---|
269 | $this->encode_array($data->_content, $data->_itemName, $data->_xmlAttributes); |
---|
270 | break; |
---|
271 | case 'pwgnamedstruct': |
---|
272 | $this->encode_array( array($data->_content), $data->_name, $data->_xmlAttributes); |
---|
273 | break; |
---|
274 | default: |
---|
275 | $this->encode_struct(get_object_vars($data), true); |
---|
276 | break; |
---|
277 | } |
---|
278 | break; |
---|
279 | default: |
---|
280 | trigger_error("Invalid type ". gettype($data)." ".get_class($data), E_USER_WARNING ); |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | ?> |
---|