[XML] Inscrease types strictness

This commit is contained in:
Diogo Cordeiro 2020-04-02 17:22:24 +01:00 committed by Diogo Peralta Cordeiro
parent 02055dee49
commit 7568789f62
5 changed files with 634 additions and 508 deletions

File diff suppressed because it is too large Load Diff

View File

@ -194,7 +194,7 @@ class ApiAction extends Action
* *
* @return void * @return void
*/ */
public function element(string $tag, $attrs = null, $content = null) public function element(string $tag, $attrs = null, ?string $content = null): void
{ {
if (is_bool($content)) { if (is_bool($content)) {
$content = ($content ? "true" : "false"); $content = ($content ? "true" : "false");

View File

@ -1,36 +1,31 @@
<?php <?php
// 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/>.
/** /**
* StatusNet, the distributed open-source microblogging tool
*
* Low-level generator for HTML * Low-level generator for HTML
* *
* PHP version 5 * @package GNUsocial
*
* LICENCE: This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Output * @category Output
* @package StatusNet *
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net> * @author Sarven Capadisli <csarven@status.net>
* @copyright 2008 StatusNet, Inc. * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
if (!defined('GNUSOCIAL')) {
exit(1);
}
// Can include XHTML options but these are too fragile in practice. // Can include XHTML options but these are too fragile in practice.
define('PAGE_TYPE_PREFS', 'text/html'); define('PAGE_TYPE_PREFS', 'text/html');
@ -43,21 +38,17 @@ define('PAGE_TYPE_PREFS', 'text/html');
* been created kind of haphazardly, not with an eye to making a general * been created kind of haphazardly, not with an eye to making a general
* HTML-creation class. * HTML-creation class.
* *
* @category Output
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
* @see Action * @see Action
* @see XMLOutputter * @see XMLOutputter
*
* @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class HTMLOutputter extends XMLOutputter class htmloutputter extends XMLOutputter
{ {
protected $DTD = ['doctype' => 'html', protected $DTD = ['doctype' => 'html',
'spec' => '-//W3C//DTD XHTML 1.0 Strict//EN', 'spec' => '-//W3C//DTD XHTML 1.0 Strict//EN',
'uri' => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd']; 'uri' => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd', ];
/** /**
* Constructor * Constructor
@ -65,9 +56,10 @@ class HTMLOutputter extends XMLOutputter
* Just wraps the XMLOutputter constructor. * Just wraps the XMLOutputter constructor.
* *
* @param string $output URI to output to, default = stdout * @param string $output URI to output to, default = stdout
* @param boolean $indent Whether to indent output, default true * @param bool|null $indent Whether to indent output, if null it defaults to true
*
* @throws ServerException
*/ */
public function __construct($output = 'php://output', $indent = null) public function __construct($output = 'php://output', $indent = null)
{ {
parent::__construct($output, $indent); parent::__construct($output, $indent);
@ -80,15 +72,16 @@ class HTMLOutputter extends XMLOutputter
* *
* Attempts to do content negotiation for language, also. * Attempts to do content negotiation for language, also.
* *
* @param string $type MIME type to use; default is to do negotation. * @param null|string $type MIME type to use; default is to do negotation.
*
* @throws ClientException
* @throws ServerException
* *
* @return void * @return void
* @throws ClientException
* @todo extract content negotiation code to an HTTP module or class.
* *
* @todo extract content negotiation code to an HTTP module or class.
*/ */
public function startHTML(?string $type = null): void
public function startHTML($type = null)
{ {
if (!$type) { if (!$type) {
$httpaccept = isset($_SERVER['HTTP_ACCEPT']) ? $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ?
@ -118,7 +111,7 @@ class HTMLOutputter extends XMLOutputter
} }
$this->extraHeaders(); $this->extraHeaders();
if (preg_match("/.*\/.*xml/", $type)) { if (preg_match('/.*\\/.*xml/', $type)) {
// Required for XML documents // Required for XML documents
$this->startXML(); $this->startXML();
} }
@ -130,7 +123,7 @@ class HTMLOutputter extends XMLOutputter
$attrs = [ $attrs = [
'xmlns' => 'http://www.w3.org/1999/xhtml', 'xmlns' => 'http://www.w3.org/1999/xhtml',
'xml:lang' => $language, 'xml:lang' => $language,
'lang' => $language 'lang' => $language,
]; ];
if (Event::handle('StartHtmlElement', [$this, &$attrs])) { if (Event::handle('StartHtmlElement', [$this, &$attrs])) {
@ -149,7 +142,10 @@ class HTMLOutputter extends XMLOutputter
// Needs to be overloaded // Needs to be overloaded
} }
protected function writeDTD() /**
* @return void
*/
protected function writeDTD(): void
{ {
$this->xw->writeDTD( $this->xw->writeDTD(
$this->DTD['doctype'], $this->DTD['doctype'],
@ -158,13 +154,21 @@ class HTMLOutputter extends XMLOutputter
); );
} }
/**
* @return mixed (array|bool|string)
*/
public function getLanguage() public function getLanguage()
{ {
// FIXME: correct language for interface // FIXME: correct language for interface
return common_language(); return common_language();
} }
public function setDTD($doctype, $spec, $uri) /**
* @param $doctype
* @param $spec
* @param $uri
*/
public function setDTD($doctype, $spec, $uri): void
{ {
$this->DTD = ['doctype' => $doctype, 'spec' => $spec, 'uri' => $uri]; $this->DTD = ['doctype' => $doctype, 'spec' => $spec, 'uri' => $uri];
} }
@ -174,7 +178,7 @@ class HTMLOutputter extends XMLOutputter
* *
* @return void * @return void
*/ */
public function endHTML() public function endHTML(): void
{ {
$this->elementEnd('html'); $this->elementEnd('html');
$this->endXML(); $this->endXML();
@ -191,21 +195,28 @@ class HTMLOutputter extends XMLOutputter
* If $attrs['type'] does not exist it will be set to 'text'. * If $attrs['type'] does not exist it will be set to 'text'.
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of label for the element * @param null|string $label text of label for the element
* @param string $value value of the element, default null * @param null|string $value value of the element, default null
* @param string $instructions instructions for valid input * @param null|string $instructions instructions for valid input
* @param string $name name of the element; if null, the id will be used * @param null|string $name name of the element; if null, the id will be used
* @param bool $required HTML5 required attribute (exclude when false) * @param bool $required HTML5 required attribute (exclude when false)
* @param array $attrs Initial attributes manually set in an array (overwritten by previous options) * @param array $attrs Initial attributes manually set in an array (overwritten by previous options)
* *
* @return void * @return void
*
* @todo add a $maxLength parameter * @todo add a $maxLength parameter
* @todo add a $size parameter * @todo add a $size parameter
* *
*/ */
public function input(
public function input($id, $label, $value = null, $instructions = null, $name = null, $required = false, array $attrs = []) string $id,
{ ?string $label,
?string $value = null,
?string $instructions = null,
?string $name = null,
bool $required = false,
array $attrs = []
): void {
$this->element('label', ['for' => $id], $label); $this->element('label', ['for' => $id], $label);
if (!array_key_exists('type', $attrs)) { if (!array_key_exists('type', $attrs)) {
$attrs['type'] = 'text'; $attrs['type'] = 'text';
@ -239,9 +250,9 @@ class HTMLOutputter extends XMLOutputter
* be used by Action::boolean() * be used by Action::boolean()
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of label for the element * @param null|string $label text of label for the element
* @param bool $checked if the box is checked, default false * @param bool $checked if the box is checked, default false
* @param string $instructions instructions for valid input * @param null|string $instructions instructions for valid input
* @param string $value value of the checkbox, default 'true' * @param string $value value of the checkbox, default 'true'
* @param bool $disabled show the checkbox disabled, default false * @param bool $disabled show the checkbox disabled, default false
* *
@ -249,20 +260,18 @@ class HTMLOutputter extends XMLOutputter
* *
* @todo add a $name parameter * @todo add a $name parameter
*/ */
public function checkbox( public function checkbox(
$id, string $id,
$label, ?string $label,
$checked = false, bool $checked = false,
$instructions = null, ?string $instructions = null,
$value = 'true', string $value = 'true',
$disabled = false bool $disabled = false
) ): void {
{
$attrs = ['name' => $id, $attrs = ['name' => $id,
'type' => 'checkbox', 'type' => 'checkbox',
'class' => 'checkbox', 'class' => 'checkbox',
'id' => $id]; 'id' => $id, ];
if ($value) { if ($value) {
$attrs['value'] = $value; $attrs['value'] = $value;
} }
@ -277,7 +286,7 @@ class HTMLOutputter extends XMLOutputter
$this->element( $this->element(
'label', 'label',
['class' => 'checkbox', ['class' => 'checkbox',
'for' => $id], 'for' => $id, ],
$label $label
); );
$this->text(' '); $this->text(' ');
@ -294,26 +303,24 @@ class HTMLOutputter extends XMLOutputter
* text. (Careful on the overuse of 'value' here.) * text. (Careful on the overuse of 'value' here.)
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of label for the element * @param null|string $label text of label for the element
* @param array $content options array, value => text * @param array $content options array, value => text
* @param string $instructions instructions for valid input * @param null|string $instructions instructions for valid input
* @param bool $blank_select whether to have a blank entry, default false * @param bool $blank_select whether to have a blank entry, default false
* @param string $selected selected value, default null * @param null|string $selected selected value, default null
* *
* @return void * @return void
* *
* @todo add a $name parameter * @todo add a $name parameter
*/ */
public function dropdown( public function dropdown(
$id, string $id,
$label, ?string $label,
$content, array $content,
$instructions = null, ?string $instructions = null,
$blank_select = false, bool $blank_select = false,
$selected = null ?string $selected = null
) ): void {
{
$this->element('label', ['for' => $id], $label); $this->element('label', ['for' => $id], $label);
$this->elementStart('select', ['id' => $id, 'name' => $id]); $this->elementStart('select', ['id' => $id, 'name' => $id]);
if ($blank_select) { if ($blank_select) {
@ -324,7 +331,7 @@ class HTMLOutputter extends XMLOutputter
$this->element( $this->element(
'option', 'option',
['value' => $value, ['value' => $value,
'selected' => 'selected'], 'selected' => 'selected', ],
$option $option
); );
} else { } else {
@ -343,39 +350,37 @@ class HTMLOutputter extends XMLOutputter
* $id is re-used as name * $id is re-used as name
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $value hidden element value, default null * @param null|string $value hidden element value, default null
* @param string $name name, if different than ID * @param null|string $name name, if different than ID
* *
* @return void * @return void
*/ */
public function hidden(string $id, ?string $value = null, ?string $name = null)
public function hidden($id, $value, $name = null)
{ {
$this->element('input', ['name' => $name ?: $id, $this->element('input', ['name' => $name ?: $id,
'type' => 'hidden', 'type' => 'hidden',
'id' => $id, 'id' => $id,
'value' => $value]); 'value' => $value, ]);
} }
/** /**
* output an HTML password input and associated elements * output an HTML password input and associated elements
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of label for the element * @param null|string $label text of label for the element
* @param string $instructions instructions for valid input * @param null|string $instructions instructions for valid input
* *
* @return void * @return void
* *
* @todo add a $name parameter * @todo add a $name parameter
*/ */
public function password(string $id, ?string $label, ?string $instructions = null): void
public function password($id, $label, $instructions = null)
{ {
$this->element('label', ['for' => $id], $label); $this->element('label', ['for' => $id], $label);
$attrs = ['name' => $id, $attrs = ['name' => $id,
'type' => 'password', 'type' => 'password',
'class' => 'password', 'class' => 'password',
'id' => $id]; 'id' => $id, ];
$this->element('input', $attrs); $this->element('input', $attrs);
if ($instructions) { if ($instructions) {
$this->element('p', 'form_guide', $instructions); $this->element('p', 'form_guide', $instructions);
@ -386,24 +391,28 @@ class HTMLOutputter extends XMLOutputter
* output an HTML submit input and associated elements * output an HTML submit input and associated elements
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of the button * @param null|string $label text of the button
* @param string $cls class of the button, default 'submit' * @param string $cls class of the button, default 'submit'
* @param string $name name, if different than ID * @param null|string $name name, if different than ID
* @param string $title title text for the submit button * @param null|string $title title text for the submit button
* *
* @return void * @return void
* *
* @todo add a $name parameter * @todo add a $name parameter
*/ */
public function submit(
public function submit($id, $label, $cls = 'submit', $name = null, $title = null) string $id,
{ ?string $label,
string $cls = 'submit',
?string $name = null,
?string $title = null
): void {
$this->element('input', ['type' => 'submit', $this->element('input', ['type' => 'submit',
'id' => $id, 'id' => $id,
'name' => $name ?: $id, 'name' => $name ?: $id,
'class' => $cls, 'class' => $cls,
'value' => $label, 'value' => $label,
'title' => $title]); 'title' => $title, ]);
} }
/** /**
@ -412,9 +421,11 @@ class HTMLOutputter extends XMLOutputter
* @param string $src relative or absolute script path * @param string $src relative or absolute script path
* @param string $type 'type' attribute value of the tag * @param string $type 'type' attribute value of the tag
* *
* @throws ServerException
*
* @return void * @return void
*/ */
public function script($src, $type = 'text/javascript') public function script(string $src, string $type = 'text/javascript'): void
{ {
if (Event::handle('StartScriptElement', [$this, &$src, &$type])) { if (Event::handle('StartScriptElement', [$this, &$src, &$type])) {
$url = parse_url($src); $url = parse_url($src);
@ -430,8 +441,7 @@ class HTMLOutputter extends XMLOutputter
$server = common_config('javascript', 'sslserver'); $server = common_config('javascript', 'sslserver');
if (empty($server)) { if (empty($server)) {
if (is_string(common_config('site', 'sslserver')) && if (is_string(common_config('site', 'sslserver')) && mb_strlen(common_config('site', 'sslserver')) > 0) {
mb_strlen(common_config('site', 'sslserver')) > 0) {
$server = common_config('site', 'sslserver'); $server = common_config('site', 'sslserver');
} elseif (common_config('site', 'server')) { } elseif (common_config('site', 'server')) {
$server = common_config('site', 'server'); $server = common_config('site', 'server');
@ -476,7 +486,7 @@ class HTMLOutputter extends XMLOutputter
$this->element( $this->element(
'script', 'script',
['type' => $type, ['type' => $type,
'src' => $src], 'src' => $src, ],
' ' ' '
); );
@ -489,11 +499,13 @@ class HTMLOutputter extends XMLOutputter
* *
* @param string $src relative path within the theme directory, or an absolute path * @param string $src relative path within the theme directory, or an absolute path
* @param string $theme 'theme' that contains the stylesheet * @param string $theme 'theme' that contains the stylesheet
* @param string media 'media' attribute of the tag * @param null|string $media
*
* @throws ServerException
* *
* @return void * @return void
*/ */
public function cssLink($src, $theme = null, $media = null) public function cssLink(string $src, ?string $theme = null, ?string $media = null): void
{ {
if (Event::handle('StartCssLinkElement', [$this, &$src, &$theme, &$media])) { if (Event::handle('StartCssLinkElement', [$this, &$src, &$theme, &$media])) {
$url = parse_url($src); $url = parse_url($src);
@ -508,7 +520,7 @@ class HTMLOutputter extends XMLOutputter
$this->element('link', ['rel' => 'stylesheet', $this->element('link', ['rel' => 'stylesheet',
'type' => 'text/css', 'type' => 'text/css',
'href' => $src, 'href' => $src,
'media' => $media]); 'media' => $media, ]);
Event::handle('EndCssLinkElement', [$this, $src, $theme, $media]); Event::handle('EndCssLinkElement', [$this, $src, $theme, $media]);
} }
} }
@ -519,12 +531,11 @@ class HTMLOutputter extends XMLOutputter
* *
* @param string $code code to put in the style tag * @param string $code code to put in the style tag
* @param string $type 'type' attribute value of the tag * @param string $type 'type' attribute value of the tag
* @param string $media 'media' attribute value of the tag * @param null|string $media 'media' attribute value of the tag
* *
* @return void * @return void
*/ */
public function style(string $code, string $type = 'text/css', ?string $media = null)
public function style($code, $type = 'text/css', $media = null)
{ {
if (Event::handle('StartStyleElement', [$this, &$code, &$type, &$media])) { if (Event::handle('StartStyleElement', [$this, &$code, &$type, &$media])) {
$this->elementStart('style', ['type' => $type, 'media' => $media]); $this->elementStart('style', ['type' => $type, 'media' => $media]);
@ -538,33 +549,31 @@ class HTMLOutputter extends XMLOutputter
* output an HTML textarea and associated elements * output an HTML textarea and associated elements
* *
* @param string $id element ID, must be unique on page * @param string $id element ID, must be unique on page
* @param string $label text of label for the element * @param null|string $label text of label for the element
* @param string $content content of the textarea, default none * @param null|string $content content of the textarea, default none
* @param string $instructions instructions for valid input * @param null|string $instructions instructions for valid input
* @param string $name name of textarea; if null, $id will be used * @param null|string $name name of textarea; if null, $id will be used
* @param int $cols number of columns * @param null|int $cols number of columns
* @param int $rows number of rows * @param null|int $rows number of rows
* @param bool $required HTML5 required attribute (exclude when false) * @param bool $required HTML5 required attribute (exclude when false)
* *
* @return void * @return void
*/ */
public function textarea( public function textarea(
$id, string $id,
$label, ?string $label,
$content = null, ?string $content = null,
$instructions = null, ?string $instructions = null,
$name = null, ?string $name = null,
$cols = null, ?int $cols = null,
$rows = null, ?int $rows = null,
$required = false bool $required = false
) ): void {
{
$this->element('label', ['for' => $id], $label); $this->element('label', ['for' => $id], $label);
$attrs = [ $attrs = [
'rows' => 3, 'rows' => 3,
'cols' => 40, 'cols' => 40,
'id' => $id 'id' => $id,
]; ];
$attrs['name'] = is_null($name) ? $id : $name; $attrs['name'] = is_null($name) ? $id : $name;
@ -597,7 +606,7 @@ class HTMLOutputter extends XMLOutputter
* @return void * @return void
* *
*/ */
public function autofocus($id) public function autofocus(string $id): void
{ {
$this->inlineScript( $this->inlineScript(
' $(document).ready(function() {' . ' $(document).ready(function() {' .
@ -616,8 +625,7 @@ class HTMLOutputter extends XMLOutputter
* *
* @return void * @return void
*/ */
public function inlineScript(string $code, string $type = 'text/javascript'): void
public function inlineScript($code, $type = 'text/javascript')
{ {
if (Event::handle('StartInlineScriptElement', [$this, &$code, &$type])) { if (Event::handle('StartInlineScriptElement', [$this, &$code, &$type])) {
$this->elementStart('script', ['type' => $type]); $this->elementStart('script', ['type' => $type]);

View File

@ -1,36 +1,31 @@
<?php <?php
// 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/>.
/** /**
* StatusNet, the distributed open-source microblogging tool
*
* Low-level generator for XML * Low-level generator for XML
* *
* PHP version 5 * @package GNUsocial
*
* LICENCE: This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Output * @category Output
* @package StatusNet *
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net> * @author Sarven Capadisli <csarven@status.net>
* @copyright 2008 StatusNet, Inc. * @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/** /**
* Low-level generator for XML * Low-level generator for XML
@ -39,33 +34,30 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
* advantage is the element() method, which simplifies outputting * advantage is the element() method, which simplifies outputting
* an element. * an element.
* *
* @category Output
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @see Action * @see Action
* @see HTMLOutputter * @see HTMLOutputter
*
* @copyright 2008-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class XMLOutputter class xmloutputter
{ {
/** /**
* Wrapped XMLWriter object, which does most of the heavy lifting * Wrapped XMLWriter object, which does most of the heavy lifting
* for output. * for output.
*/ */
public $xw;
public $xw = null;
/** /**
* Constructor * Constructor
* *
* Initializes the wrapped XMLWriter. * Initializes the wrapped XMLWriter.
* *
* @param string $output URL for outputting, if null it defaults to stdout ('php://output') * @param null|string $output URL for outputting, if null it defaults to stdout ('php://output')
* @param boolean $indent Whether to indent output, default true * @param null|bool $indent Whether to indent output, if null it defaults to true
*
* @throws ServerException
*/ */
public function __construct($output = null, $indent = null) public function __construct($output = null, $indent = null)
{ {
if (is_null($output)) { if (is_null($output)) {
@ -88,11 +80,10 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function startXML(?string $doc = null, ?string $public = null, ?string $system = null): void
public function startXML($doc = null, $public = null, $system = null)
{ {
$this->xw->startDocument('1.0', 'UTF-8'); $this->xw->startDocument('1.0', 'UTF-8');
if ($doc) { if (!is_null($doc)) {
$this->xw->writeDTD($doc, $public, $system); $this->xw->writeDTD($doc, $public, $system);
} }
} }
@ -105,8 +96,7 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function endXML(): void
public function endXML()
{ {
$this->xw->endDocument(); $this->xw->endDocument();
$this->xw->flush(); $this->xw->flush();
@ -128,17 +118,16 @@ class XMLOutputter
* as the class attribute of the element. * as the class attribute of the element.
* *
* @param string $tag Element type or tagname * @param string $tag Element type or tagname
* @param array|string|null $attrs Array of element attributes, as key-value pairs * @param null|array|string $attrs Array of element attributes, as key-value pairs
* @param string|null $content string content of the element * @param null|string $content string content of the element
* *
* @return void * @return void
*/ */
public function element(string $tag, $attrs = null, ?string $content = null): void
public function element(string $tag, $attrs = null, $content = null)
{ {
$this->elementStart($tag, $attrs); $this->elementStart($tag, $attrs);
if (!is_null($content)) { if (!is_null($content)) {
$this->xw->text(strval($content)); $this->xw->text($content);
} }
$this->elementEnd($tag); $this->elementEnd($tag);
} }
@ -153,12 +142,11 @@ class XMLOutputter
* as the class attribute of the element. * as the class attribute of the element.
* *
* @param string $tag Element type or tagname * @param string $tag Element type or tagname
* @param array|string|null $attrs Attributes * @param null|array|string $attrs Attributes
* *
* @return void * @return void
*/ */
public function elementStart(string $tag, $attrs = null): void
public function elementStart(string $tag, $attrs = null)
{ {
$this->xw->startElement($tag); $this->xw->startElement($tag);
if (is_array($attrs)) { if (is_array($attrs)) {
@ -185,12 +173,11 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function elementEnd(string $tag): void
public function elementEnd(string $tag)
{ {
static $empty_tag = ['base', 'meta', 'link', 'hr', static $empty_tag = ['base', 'meta', 'link', 'hr',
'br', 'param', 'img', 'area', 'br', 'param', 'img', 'area',
'input', 'col', 'source']; 'input', 'col', 'source', ];
// XXX: check namespace // XXX: check namespace
if (in_array($tag, $empty_tag)) { if (in_array($tag, $empty_tag)) {
$this->xw->endElement(); $this->xw->endElement();
@ -199,7 +186,15 @@ class XMLOutputter
} }
} }
public function elementNS(array $ns, $tag, $attrs = null, $content = null) /**
* @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
{ {
$this->elementStartNS($ns, $tag, $attrs); $this->elementStartNS($ns, $tag, $attrs);
if (!is_null($content)) { if (!is_null($content)) {
@ -208,7 +203,14 @@ class XMLOutputter
$this->elementEnd($tag); $this->elementEnd($tag);
} }
public function elementStartNS(array $ns, $tag, $attrs = null) /**
* @param array $ns
* @param string $tag
* @param null|array|string $attrs
*
* @return void
*/
public function elementStartNS(array $ns, string $tag, $attrs = null): void
{ {
reset($ns); // array pointer to 0 reset($ns); // array pointer to 0
$uri = key($ns); $uri = key($ns);
@ -232,8 +234,7 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function text(string $txt): void
public function text($txt)
{ {
$this->xw->text($txt); $this->xw->text($txt);
} }
@ -248,8 +249,7 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function raw(string $xml): void
public function raw($xml)
{ {
$this->xw->writeRaw($xml); $this->xw->writeRaw($xml);
} }
@ -261,8 +261,7 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function comment(string $txt): void
public function comment($txt)
{ {
$this->xw->writeComment($txt); $this->xw->writeComment($txt);
} }
@ -272,8 +271,7 @@ class XMLOutputter
* *
* @return void * @return void
*/ */
public function flush(): void
public function flush()
{ {
$this->xw->flush(); $this->xw->flush();
} }

View File

@ -1,65 +1,73 @@
<?php <?php
// 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/>.
/** /**
* StatusNet, the distributed open-source microblogging tool
*
* Generator for in-memory XML * Generator for in-memory XML
* *
* PHP version 5 * @package GNUsocial
*
* LICENCE: This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Output * @category Output
* @package StatusNet *
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @copyright 2009 StatusNet, Inc. * @copyright 2009-2019 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
* @link http://status.net/
*/ */
defined('GNUSOCIAL') || die();
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/** /**
* Create in-memory XML * Create in-memory XML
* *
* @category Output
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
* @see Action * @see Action
* @see HTMLOutputter * @see HTMLOutputter
*
* @copyright 2009-2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
class XMLStringer extends XMLOutputter class xmlstringer extends XMLOutputter
{ {
public function __construct($indent = false) /**
* XMLStringer constructor.
*
* @param bool $indent
*/
public function __construct(bool $indent = false)
{ {
$this->xw = new XMLWriter(); $this->xw = new XMLWriter();
$this->xw->openMemory(); $this->xw->openMemory();
$this->xw->setIndent($indent); $this->xw->setIndent($indent);
} }
public static function estring($tag, $attrs = null, $content = null) /**
* @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
*
* @return string
*/
public static function estring(string $tag, $attrs = null, ?string $content = null): string
{ {
$xs = new XMLStringer(); $xs = new self();
$xs->element($tag, $attrs, $content); $xs->element($tag, $attrs, $content);
return $xs->getString(); return $xs->getString();
} }
// utility for quickly creating XML-strings /**
* Utility for quickly creating XML-strings
*
* @return string
*/
public function getString() public function getString()
{ {
return $this->xw->outputMemory(); return $this->xw->outputMemory();