xmloutputter.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. private const EMPTY_TAG = ['base', 'meta', 'link', 'hr', 'br', 'param', 'img', 'area', 'input', 'col', 'source'];
  44. /**
  45. * Wrapped XMLWriter object, which does most of the heavy lifting
  46. * for output.
  47. */
  48. public $xw;
  49. /**
  50. * Constructor
  51. *
  52. * Initializes the wrapped XMLWriter.
  53. *
  54. * @param null|string $output URL for outputting, if null it defaults to stdout ('php://output')
  55. * @param null|bool $indent Whether to indent output, if null it defaults to true
  56. *
  57. * @throws ServerException
  58. */
  59. public function __construct($output = null, $indent = null)
  60. {
  61. if (is_null($output)) {
  62. $output = 'php://output';
  63. }
  64. $this->xw = new XMLWriter();
  65. $this->xw->openURI($output);
  66. if (is_null($indent)) {
  67. $indent = common_config('site', 'indent');
  68. }
  69. $this->xw->setIndent($indent);
  70. }
  71. /**
  72. * Start a new XML document
  73. *
  74. * @param string $doc document element
  75. * @param string $public public identifier
  76. * @param string $system system identifier
  77. *
  78. * @return void
  79. */
  80. public function startXML(?string $doc = null, ?string $public = null, ?string $system = null): void
  81. {
  82. $this->xw->startDocument('1.0', 'UTF-8');
  83. if (!is_null($doc)) {
  84. $this->xw->writeDTD($doc, $public, $system);
  85. }
  86. }
  87. /**
  88. * finish an XML document
  89. *
  90. * It's probably a bad idea to continue to use this object
  91. * after calling endXML().
  92. *
  93. * @return void
  94. */
  95. public function endXML(): void
  96. {
  97. $this->xw->endDocument();
  98. $this->xw->flush();
  99. }
  100. /**
  101. * output an XML element
  102. *
  103. * Utility for outputting an XML element. A convenient wrapper
  104. * for a bunch of longer XMLWriter calls. This is best for
  105. * when an element doesn't have any sub-elements; if that's the
  106. * case, use elementStart() and elementEnd() instead.
  107. *
  108. * The $content element will be escaped for XML. If you need
  109. * raw output, use elementStart() and elementEnd() with a call
  110. * to raw() in the middle.
  111. *
  112. * If $attrs is a string instead of an array, it will be treated
  113. * as the class attribute of the element.
  114. *
  115. * @param string $tag Element type or tagname
  116. * @param null|array|string $attrs Array of element attributes, as key-value pairs
  117. * @param null|string $content string content of the element
  118. *
  119. * @return void
  120. */
  121. public function element(string $tag, $attrs = null, ?string $content = null): void
  122. {
  123. $this->elementStart($tag, $attrs);
  124. if (!is_null($content)) {
  125. $this->xw->text($content);
  126. }
  127. $this->elementEnd($tag);
  128. }
  129. /**
  130. * output a start tag for an element
  131. *
  132. * Mostly used for when an element has content that's
  133. * not a simple string.
  134. *
  135. * If $attrs is a string instead of an array, it will be treated
  136. * as the class attribute of the element.
  137. *
  138. * @param string $tag Element type or tagname
  139. * @param null|array|string $attrs Attributes
  140. *
  141. * @return void
  142. */
  143. public function elementStart(string $tag, $attrs = null): void
  144. {
  145. $this->xw->startElement($tag);
  146. if (is_array($attrs)) {
  147. foreach ($attrs as $name => $value) {
  148. if ($value !== null) {
  149. $this->xw->writeAttribute($name, $value);
  150. }
  151. }
  152. return;
  153. }
  154. if (is_string($attrs)) {
  155. $this->xw->writeAttribute('class', $attrs);
  156. }
  157. }
  158. /**
  159. * output an end tag for an element
  160. *
  161. * Used in conjunction with elementStart(). $tag param
  162. * should match the elementStart() param.
  163. *
  164. * For HTML 4 compatibility, this method will force
  165. * a full end element (</tag>) even if the element is
  166. * empty, except for a handful of exception tagnames.
  167. * This is a hack.
  168. *
  169. * @param string $tag Element type or tagname.
  170. *
  171. * @return void
  172. */
  173. public function elementEnd(string $tag): void
  174. {
  175. // XXX: check namespace
  176. if (in_array($tag, self::EMPTY_TAG, true)) {
  177. $this->xw->endElement();
  178. return;
  179. }
  180. $this->xw->fullEndElement();
  181. }
  182. /**
  183. * @param array $ns
  184. * @param string $tag
  185. * @param null|array|string $attrs
  186. * @param null|string $content
  187. *
  188. * @return void
  189. */
  190. public function elementNS(array $ns, string $tag, $attrs = null, ?string $content = null): void
  191. {
  192. $this->elementStartNS($ns, $tag, $attrs);
  193. if (!is_null($content)) {
  194. $this->xw->text($content);
  195. }
  196. $this->elementEnd($tag);
  197. }
  198. /**
  199. * @param array $ns
  200. * @param string $tag
  201. * @param null|array|string $attrs
  202. *
  203. * @return void
  204. */
  205. public function elementStartNS(array $ns, string $tag, $attrs = null): void
  206. {
  207. $keys = array_keys($ns);
  208. $uri = $keys[0];
  209. $prefix = $ns[$uri];
  210. $this->xw->startElementNS($prefix, $tag, $uri);
  211. if (is_array($attrs)) {
  212. foreach ($attrs as $name => $value) {
  213. $this->xw->writeAttribute($name, $value);
  214. }
  215. } elseif (is_string($attrs)) {
  216. $this->xw->writeAttribute('class', $attrs);
  217. }
  218. }
  219. /**
  220. * output plain text
  221. *
  222. * Text will be escaped. If you need it not to be,
  223. * use raw() instead.
  224. *
  225. * @param string $txt Text to output.
  226. *
  227. * @return void
  228. */
  229. public function text(string $txt): void
  230. {
  231. $this->xw->text($txt);
  232. }
  233. /**
  234. * output raw xml
  235. *
  236. * This will spit out its argument verbatim -- no escaping is
  237. * done.
  238. *
  239. * @param string $xml XML to output.
  240. *
  241. * @return void
  242. */
  243. public function raw(string $xml): void
  244. {
  245. $this->xw->writeRaw($xml);
  246. }
  247. /**
  248. * output a comment
  249. *
  250. * @param string $txt text of the comment
  251. *
  252. * @return void
  253. */
  254. public function comment(string $txt): void
  255. {
  256. $this->xw->writeComment($txt);
  257. }
  258. /**
  259. * Flush output buffers
  260. *
  261. * @return void
  262. */
  263. public function flush(): void
  264. {
  265. if (headers_sent()) {
  266. $this->xw->flush();
  267. }
  268. }
  269. }