xmloutputter.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Low-level generator for XML
  18. *
  19. * @package GNUsocial
  20. * @category Output
  21. *
  22. * @author Evan Prodromou <evan@status.net>
  23. * @author Sarven Capadisli <csarven@status.net>
  24. * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Low-level generator for XML
  30. *
  31. * This is a thin wrapper around PHP's XMLWriter. The main
  32. * advantage is the element() method, which simplifies outputting
  33. * an element.
  34. *
  35. * @see Action
  36. * @see HTMLOutputter
  37. *
  38. * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. */
  41. class xmloutputter
  42. {
  43. /**
  44. * Wrapped XMLWriter object, which does most of the heavy lifting
  45. * for output.
  46. */
  47. public $xw;
  48. /**
  49. * Constructor
  50. *
  51. * Initializes the wrapped XMLWriter.
  52. *
  53. * @param null|string $output URL for outputting, if null it defaults to stdout ('php://output')
  54. * @param null|bool $indent Whether to indent output, if null it defaults to true
  55. *
  56. * @throws ServerException
  57. */
  58. public function __construct($output = null, $indent = null)
  59. {
  60. if (is_null($output)) {
  61. $output = 'php://output';
  62. }
  63. $this->xw = new XMLWriter();
  64. $this->xw->openURI($output);
  65. if (is_null($indent)) {
  66. $indent = common_config('site', 'indent');
  67. }
  68. $this->xw->setIndent($indent);
  69. }
  70. /**
  71. * Start a new XML document
  72. *
  73. * @param string $doc document element
  74. * @param string $public public identifier
  75. * @param string $system system identifier
  76. *
  77. * @return void
  78. */
  79. public function startXML(?string $doc = null, ?string $public = null, ?string $system = null): void
  80. {
  81. $this->xw->startDocument('1.0', 'UTF-8');
  82. if (!is_null($doc)) {
  83. $this->xw->writeDTD($doc, $public, $system);
  84. }
  85. }
  86. /**
  87. * finish an XML document
  88. *
  89. * It's probably a bad idea to continue to use this object
  90. * after calling endXML().
  91. *
  92. * @return void
  93. */
  94. public function endXML(): void
  95. {
  96. $this->xw->endDocument();
  97. $this->xw->flush();
  98. }
  99. /**
  100. * output an XML element
  101. *
  102. * Utility for outputting an XML element. A convenient wrapper
  103. * for a bunch of longer XMLWriter calls. This is best for
  104. * when an element doesn't have any sub-elements; if that's the
  105. * case, use elementStart() and elementEnd() instead.
  106. *
  107. * The $content element will be escaped for XML. If you need
  108. * raw output, use elementStart() and elementEnd() with a call
  109. * to raw() in the middle.
  110. *
  111. * If $attrs is a string instead of an array, it will be treated
  112. * as the class attribute of the element.
  113. *
  114. * @param string $tag Element type or tagname
  115. * @param null|array|string $attrs Array of element attributes, as key-value pairs
  116. * @param null|string $content string content of the element
  117. *
  118. * @return void
  119. */
  120. public function element(string $tag, $attrs = null, ?string $content = null): void
  121. {
  122. $this->elementStart($tag, $attrs);
  123. if (!is_null($content)) {
  124. $this->xw->text($content);
  125. }
  126. $this->elementEnd($tag);
  127. }
  128. /**
  129. * output a start tag for an element
  130. *
  131. * Mostly used for when an element has content that's
  132. * not a simple string.
  133. *
  134. * If $attrs is a string instead of an array, it will be treated
  135. * as the class attribute of the element.
  136. *
  137. * @param string $tag Element type or tagname
  138. * @param null|array|string $attrs Attributes
  139. *
  140. * @return void
  141. */
  142. public function elementStart(string $tag, $attrs = null): void
  143. {
  144. $this->xw->startElement($tag);
  145. if (is_array($attrs)) {
  146. foreach ($attrs as $name => $value) {
  147. $this->xw->writeAttribute($name, $value);
  148. }
  149. } elseif (is_string($attrs)) {
  150. $this->xw->writeAttribute('class', $attrs);
  151. }
  152. }
  153. /**
  154. * output an end tag for an element
  155. *
  156. * Used in conjunction with elementStart(). $tag param
  157. * should match the elementStart() param.
  158. *
  159. * For HTML 4 compatibility, this method will force
  160. * a full end element (</tag>) even if the element is
  161. * empty, except for a handful of exception tagnames.
  162. * This is a hack.
  163. *
  164. * @param string $tag Element type or tagname.
  165. *
  166. * @return void
  167. */
  168. public function elementEnd(string $tag): void
  169. {
  170. static $empty_tag = ['base', 'meta', 'link', 'hr',
  171. 'br', 'param', 'img', 'area',
  172. 'input', 'col', 'source', ];
  173. // XXX: check namespace
  174. if (in_array($tag, $empty_tag)) {
  175. $this->xw->endElement();
  176. } else {
  177. $this->xw->fullEndElement();
  178. }
  179. }
  180. /**
  181. * @param array $ns
  182. * @param string $tag
  183. * @param null|array|string $attrs
  184. * @param null|string $content
  185. *
  186. * @return void
  187. */
  188. public function elementNS(array $ns, string $tag, $attrs = null, ?string $content = null): void
  189. {
  190. $this->elementStartNS($ns, $tag, $attrs);
  191. if (!is_null($content)) {
  192. $this->xw->text($content);
  193. }
  194. $this->elementEnd($tag);
  195. }
  196. /**
  197. * @param array $ns
  198. * @param string $tag
  199. * @param null|array|string $attrs
  200. *
  201. * @return void
  202. */
  203. public function elementStartNS(array $ns, string $tag, $attrs = null): void
  204. {
  205. reset($ns); // array pointer to 0
  206. $uri = key($ns);
  207. $this->xw->startElementNS($ns[$uri], $tag, $uri);
  208. if (is_array($attrs)) {
  209. foreach ($attrs as $name => $value) {
  210. $this->xw->writeAttribute($name, $value);
  211. }
  212. } elseif (is_string($attrs)) {
  213. $this->xw->writeAttribute('class', $attrs);
  214. }
  215. }
  216. /**
  217. * output plain text
  218. *
  219. * Text will be escaped. If you need it not to be,
  220. * use raw() instead.
  221. *
  222. * @param string $txt Text to output.
  223. *
  224. * @return void
  225. */
  226. public function text(string $txt): void
  227. {
  228. $this->xw->text($txt);
  229. }
  230. /**
  231. * output raw xml
  232. *
  233. * This will spit out its argument verbatim -- no escaping is
  234. * done.
  235. *
  236. * @param string $xml XML to output.
  237. *
  238. * @return void
  239. */
  240. public function raw(string $xml): void
  241. {
  242. $this->xw->writeRaw($xml);
  243. }
  244. /**
  245. * output a comment
  246. *
  247. * @param string $txt text of the comment
  248. *
  249. * @return void
  250. */
  251. public function comment(string $txt): void
  252. {
  253. $this->xw->writeComment($txt);
  254. }
  255. /**
  256. * Flush output buffers
  257. *
  258. * @return void
  259. */
  260. public function flush(): void
  261. {
  262. $this->xw->flush();
  263. }
  264. }