source: trunk/include/ws_protocols/rest_encoder.php @ 2299

Last change on this file since 2299 was 2299, checked in by plg, 16 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
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
25class PwgXmlWriter
26{
27  var $_indent;
28  var $_indentStr;
29
30  var $_elementStack;
31  var $_lastTagOpen;
32  var $_indentLevel;
33
34  var $_encodedXml;
35
36  function PwgXmlWriter()
37  {
38    $this->_elementStack = array();
39    $this->_lastTagOpen = false;
40    $this->_indentLevel = 0;
41
42    $this->_encodedXml = '';
43    $this->_indent = true;
44    $this->_indentStr = "\t";
45  }
46
47  function &getOutput()
48  {
49    return $this->_encodedXml;
50  }
51
52
53  function start_element($name)
54  {
55    $this->_end_prev(false);
56    if (!empty($this->_elementStack))
57    {
58      $this->_eol_indent();
59    }
60    $this->_indentLevel++;
61    $this->_indent();
62    $this->_output( '<'.$name );
63    $this->_lastTagOpen = true;
64    array_push( $this->_elementStack, $name);
65  }
66
67  function end_element($x)
68  {
69    $close_tag = $this->_end_prev(true);
70    $name = array_pop( $this->_elementStack );
71    if ($close_tag)
72    {
73      $this->_indentLevel--;
74      $this->_indent();
75//      $this->_eol_indent();
76      $this->_output('</'.$name.">");
77    }
78  }
79
80  function write_content($value)
81  {
82    $this->_end_prev(false);
83    $value = (string)$value;
84    $this->_output( htmlspecialchars( $value ) );
85  }
86
87  function write_cdata($value)
88  {
89    $this->_end_prev(false);
90    $value = (string)$value;
91    $this->_output(
92      '<![CDATA['
93      . str_replace(']]>', ']]&gt;', $value)
94      . ']]>' );
95  }
96
97  function write_attribute($name, $value)
98  {
99    $this->_output(' '.$name.'="'.$this->encode_attribute($value).'"');
100  }
101
102  function encode_attribute($value)
103  {
104    return htmlspecialchars( (string)$value);
105  }
106
107  function _end_prev($done)
108  {
109    $ret = true;
110    if ($this->_lastTagOpen)
111    {
112      if ($done)
113      {
114        $this->_indentLevel--;
115        $this->_output( ' />' );
116        //$this->_eol_indent();
117        $ret = false;
118      }
119      else
120      {
121        $this->_output( '>' );
122      }
123      $this->_lastTagOpen = false;
124    }
125    return $ret;
126  }
127
128  function _eol_indent()
129  {
130    if ($this->_indent)
131      $this->_output("\n");
132  }
133
134  function _indent()
135  {
136    if ($this->_indent and
137        $this->_indentLevel > count($this->_elementStack) )
138    {
139      $this->_output(
140        str_repeat( $this->_indentStr, count($this->_elementStack) )
141       );
142    }
143  }
144
145  function _output($raw_content)
146  {
147    $this->_encodedXml .= $raw_content;
148  }
149}
150
151class PwgRestEncoder extends PwgResponseEncoder
152{
153  function encodeResponse($response)
154  {
155    global $lang_info;
156    $respClass = strtolower( get_class($response) );
157    if ($respClass=='pwgerror')
158    {
159      $ret = '<?xml version="1.0"?>
160<rsp stat="fail">
161        <err code="'.$response->code().'" msg="'.htmlspecialchars($response->message()).'" />
162</rsp>';
163      return $ret;
164    }
165
166    $this->_writer = new PwgXmlWriter();
167    $this->encode($response);
168    $ret = $this->_writer->getOutput();
169    $ret = '<?xml version="1.0" encoding="'.get_pwg_charset().'" ?>
170<rsp stat="ok">
171'.$ret.'
172</rsp>';
173
174    return $ret;
175  }
176
177  function getContentType()
178  {
179    return 'text/xml';
180  }
181
182  function encode_array($data, $itemName, $xml_attributes=array())
183  {
184    foreach ($data as $item)
185    {
186      $this->_writer->start_element( $itemName );
187      $this->encode($item, $xml_attributes);
188      $this->_writer->end_element( $itemName );
189    }
190  }
191
192  function encode_struct($data, $skip_underscore, $xml_attributes=array())
193  {
194    foreach ($data as $name => $value)
195    {
196      if (is_numeric($name))
197        continue;
198      if ($skip_underscore and $name[0]=='_')
199        continue;
200      if ( is_null($value) )
201        continue; // null means we dont put it
202      if ( $name==WS_XML_ATTRIBUTES)
203      {
204        foreach ($value as $attr_name => $attr_value)
205        {
206          $this->_writer->write_attribute($attr_name, $attr_value);
207        }
208        unset($data[$name]);
209      }
210      else if ( isset($xml_attributes[$name]) )
211      {
212        $this->_writer->write_attribute($name, $value);
213        unset($data[$name]);
214      }
215    }
216
217    foreach ($data as $name => $value)
218    {
219      if (is_numeric($name))
220        continue;
221      if ($skip_underscore and $name[0]=='_')
222        continue;
223      if ( is_null($value) )
224        continue; // null means we dont put it
225      if ($name!=WS_XML_CONTENT)
226        $this->_writer->start_element($name);
227      $this->encode($value);
228      if ($name!=WS_XML_CONTENT)
229        $this->_writer->end_element($name);
230    }
231  }
232
233  function encode($data, $xml_attributes=array() )
234  {
235    switch (gettype($data))
236    {
237      case 'null':
238      case 'NULL':
239        $this->_writer->write_content('');
240        break;
241      case 'boolean':
242        $this->_writer->write_content($data ? '1' : '0');
243        break;
244      case 'integer':
245      case 'double':
246        $this->_writer->write_content($data);
247        break;
248      case 'string':
249        $this->_writer->write_content($data);
250        break;
251      case 'array':
252        $is_array = range(0, count($data) - 1) === array_keys($data);
253        if ($is_array)
254        {
255          $this->encode_array($data, 'item' );
256        }
257        else
258        {
259          $this->encode_struct($data, false, $xml_attributes);
260        }
261        break;
262      case 'object':
263        switch ( strtolower(get_class($data)) )
264        {
265          case 'pwgnamedarray':
266            $this->encode_array($data->_content, $data->_itemName, $data->_xmlAttributes);
267            break;
268          case 'pwgnamedstruct':
269            $this->encode_array( array($data->_content), $data->_name, $data->_xmlAttributes);
270            break;
271          default:
272            $this->encode_struct(get_object_vars($data), true);
273            break;
274        }
275        break;
276      default:
277        trigger_error("Invalid type ". gettype($data)." ".get_class($data), E_USER_WARNING );
278    }
279  }
280}
281
282?>
Note: See TracBrowser for help on using the repository browser.