| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Low-level generator for XML | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |  * @package   GNUsocial | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |  * @category  Output | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2009-08-25 18:19:04 -04:00
										 |  |  |  * @author    Evan Prodromou <evan@status.net> | 
					
						
							|  |  |  |  * @author    Sarven Capadisli <csarven@status.net> | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |  * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  | defined('GNUSOCIAL') || die(); | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Low-level generator for XML | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This is a thin wrapper around PHP's XMLWriter. The main | 
					
						
							|  |  |  |  * advantage is the element() method, which simplifies outputting | 
					
						
							|  |  |  |  * an element. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @see      Action | 
					
						
							|  |  |  |  * @see      HTMLOutputter | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  | class xmloutputter | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Wrapped XMLWriter object, which does most of the heavy lifting | 
					
						
							|  |  |  |      * for output. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public $xw; | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Constructor | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Initializes the wrapped XMLWriter. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |      * @param null|string $output URL for outputting, if null it defaults to stdout ('php://output') | 
					
						
							|  |  |  |      * @param null|bool   $indent Whether to indent output, if null it defaults to true | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws ServerException | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |     public function __construct($output = null, $indent = null) | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-16 23:58:04 +02:00
										 |  |  |         if (is_null($output)) { | 
					
						
							|  |  |  |             $output = 'php://output'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |         $this->xw = new XMLWriter(); | 
					
						
							|  |  |  |         $this->xw->openURI($output); | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |         if (is_null($indent)) { | 
					
						
							| 
									
										
										
										
											2009-12-05 01:03:04 -05:00
										 |  |  |             $indent = common_config('site', 'indent'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |         $this->xw->setIndent($indent); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Start a new XML document | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |      * @param string $doc    document element | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |      * @param string $public public identifier | 
					
						
							|  |  |  |      * @param string $system system identifier | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function startXML(?string $doc = null, ?string $public = null, ?string $system = null): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-01-15 14:21:47 -05:00
										 |  |  |         $this->xw->startDocument('1.0', 'UTF-8'); | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |         if (!is_null($doc)) { | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |             $this->xw->writeDTD($doc, $public, $system); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * finish an XML document | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * It's probably a bad idea to continue to use this object | 
					
						
							|  |  |  |      * after calling endXML(). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function endXML(): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->endDocument(); | 
					
						
							|  |  |  |         $this->xw->flush(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * output an XML element | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Utility for outputting an XML element. A convenient wrapper | 
					
						
							|  |  |  |      * for a bunch of longer XMLWriter calls. This is best for | 
					
						
							| 
									
										
										
										
											2009-11-09 20:01:46 +01:00
										 |  |  |      * when an element doesn't have any sub-elements; if that's the | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |      * case, use elementStart() and elementEnd() instead. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * The $content element will be escaped for XML. If you need | 
					
						
							|  |  |  |      * raw output, use elementStart() and elementEnd() with a call | 
					
						
							|  |  |  |      * to raw() in the middle. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2009-01-13 11:47:01 -05:00
										 |  |  |      * If $attrs is a string instead of an array, it will be treated | 
					
						
							|  |  |  |      * as the class attribute of the element. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |      * @param string            $tag     Element type or tagname | 
					
						
							|  |  |  |      * @param null|array|string $attrs   Array of element attributes, as key-value pairs | 
					
						
							|  |  |  |      * @param null|string       $content string content of the element | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function element(string $tag, $attrs = null, ?string $content = null): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->elementStart($tag, $attrs); | 
					
						
							|  |  |  |         if (!is_null($content)) { | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |             $this->xw->text($content); | 
					
						
							| 
									
										
										
										
											2014-11-27 13:36:29 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $this->elementEnd($tag); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * output a start tag for an element | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Mostly used for when an element has content that's | 
					
						
							|  |  |  |      * not a simple string. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2009-01-13 11:47:01 -05:00
										 |  |  |      * If $attrs is a string instead of an array, it will be treated | 
					
						
							|  |  |  |      * as the class attribute of the element. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |      * @param string            $tag   Element type or tagname | 
					
						
							|  |  |  |      * @param null|array|string $attrs Attributes | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function elementStart(string $tag, $attrs = null): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->startElement($tag); | 
					
						
							|  |  |  |         if (is_array($attrs)) { | 
					
						
							|  |  |  |             foreach ($attrs as $name => $value) { | 
					
						
							|  |  |  |                 $this->xw->writeAttribute($name, $value); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |         } elseif (is_string($attrs)) { | 
					
						
							| 
									
										
										
										
											2014-11-27 13:36:29 +01:00
										 |  |  |             $this->xw->writeAttribute('class', $attrs); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * output an end tag for an element | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Used in conjunction with elementStart(). $tag param | 
					
						
							|  |  |  |      * should match the elementStart() param. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * For HTML 4 compatibility, this method will force | 
					
						
							|  |  |  |      * a full end element (</tag>) even if the element is | 
					
						
							|  |  |  |      * empty, except for a handful of exception tagnames. | 
					
						
							|  |  |  |      * This is a hack. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $tag Element type or tagname. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function elementEnd(string $tag): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |         static $empty_tag = ['base', 'meta', 'link', 'hr', | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |             'br', 'param', 'img', 'area', | 
					
						
							|  |  |  |             'input', 'col', 'source', ]; | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |         // XXX: check namespace
 | 
					
						
							|  |  |  |         if (in_array($tag, $empty_tag)) { | 
					
						
							|  |  |  |             $this->xw->endElement(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $this->xw->fullEndElement(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array             $ns | 
					
						
							|  |  |  |      * @param string            $tag | 
					
						
							|  |  |  |      * @param null|array|string $attrs | 
					
						
							|  |  |  |      * @param null|string       $content | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function elementNS(array $ns, string $tag, $attrs = null, ?string $content = null): void | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->elementStartNS($ns, $tag, $attrs); | 
					
						
							|  |  |  |         if (!is_null($content)) { | 
					
						
							|  |  |  |             $this->xw->text($content); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->elementEnd($tag); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param array             $ns | 
					
						
							|  |  |  |      * @param string            $tag | 
					
						
							|  |  |  |      * @param null|array|string $attrs | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function elementStartNS(array $ns, string $tag, $attrs = null): void | 
					
						
							| 
									
										
										
										
											2019-05-06 23:58:45 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         reset($ns); // array pointer to 0
 | 
					
						
							|  |  |  |         $uri = key($ns); | 
					
						
							|  |  |  |         $this->xw->startElementNS($ns[$uri], $tag, $uri); | 
					
						
							|  |  |  |         if (is_array($attrs)) { | 
					
						
							|  |  |  |             foreach ($attrs as $name => $value) { | 
					
						
							|  |  |  |                 $this->xw->writeAttribute($name, $value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } elseif (is_string($attrs)) { | 
					
						
							|  |  |  |             $this->xw->writeAttribute('class', $attrs); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * output plain text | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Text will be escaped. If you need it not to be, | 
					
						
							|  |  |  |      * use raw() instead. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $txt Text to output. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function text(string $txt): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->text($txt); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * output raw xml | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This will spit out its argument verbatim -- no escaping is | 
					
						
							|  |  |  |      * done. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $xml XML to output. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function raw(string $xml): void | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->writeRaw($xml); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-14 13:57:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * output a comment | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $txt text of the comment | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function comment(string $txt): void | 
					
						
							| 
									
										
										
										
											2009-01-14 13:57:29 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->writeComment($txt); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-01-21 14:23:02 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Flush output buffers | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-02 17:22:24 +01:00
										 |  |  |     public function flush(): void | 
					
						
							| 
									
										
										
										
											2011-01-21 14:23:02 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->xw->flush(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-13 10:56:50 -05:00
										 |  |  | } |