PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/fsn-site-central/mediatheque/include/ws_protocols/rest_encoder.php

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