SECURITY: rate-limit and prevent blocked users from changing email
[lhc/web/wiklou.git] / includes / parser / CoreTagHooks.php
1 <?php
2 /**
3 * Tag hooks provided by MediaWiki core
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Various tag hooks, registered in Parser::firstCallInit()
26 * @ingroup Parser
27 */
28 class CoreTagHooks {
29 /**
30 * @param Parser $parser
31 * @return void
32 */
33 public static function register( $parser ) {
34 global $wgRawHtml;
35 $parser->setHook( 'pre', [ __CLASS__, 'pre' ] );
36 $parser->setHook( 'nowiki', [ __CLASS__, 'nowiki' ] );
37 $parser->setHook( 'gallery', [ __CLASS__, 'gallery' ] );
38 $parser->setHook( 'indicator', [ __CLASS__, 'indicator' ] );
39 if ( $wgRawHtml ) {
40 $parser->setHook( 'html', [ __CLASS__, 'html' ] );
41 }
42 }
43
44 /**
45 * Core parser tag hook function for 'pre'.
46 * Text is treated roughly as 'nowiki' wrapped in an HTML 'pre' tag;
47 * valid HTML attributes are passed on.
48 *
49 * Uses custom html escaping which phan-taint-check won't recognize
50 * hence we suppress the error.
51 * @suppress SecurityCheck-XSS
52 *
53 * @param string $text
54 * @param array $attribs
55 * @param Parser $parser
56 * @return string HTML
57 */
58 public static function pre( $text, $attribs, $parser ) {
59 // Backwards-compatibility hack
60 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
61
62 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
63 // We need to let both '"' and '&' through,
64 // for strip markers and entities respectively.
65 $content = str_replace(
66 [ '>', '<' ],
67 [ '&gt;', '&lt;' ],
68 $content
69 );
70 return Html::rawElement( 'pre', $attribs, $content );
71 }
72
73 /**
74 * Core parser tag hook function for 'html', used only when
75 * $wgRawHtml is enabled.
76 *
77 * This is potentially unsafe and should be used only in very careful
78 * circumstances, as the contents are emitted as raw HTML.
79 *
80 * Uses undocumented extended tag hook return values, introduced in r61913.
81 *
82 * @suppress SecurityCheck-XSS
83 * @param string $content
84 * @param array $attributes
85 * @param Parser $parser
86 * @throws MWException
87 * @return array|string Output of tag hook
88 */
89 public static function html( $content, $attributes, $parser ) {
90 global $wgRawHtml;
91 if ( $wgRawHtml ) {
92 if ( $parser->getOptions()->getAllowUnsafeRawHtml() ) {
93 return [ $content, 'markerType' => 'nowiki' ];
94 } else {
95 // In a system message where raw html is
96 // not allowed (but it is allowed in other
97 // contexts).
98 return Html::rawElement(
99 'span',
100 [ 'class' => 'error' ],
101 // Using ->text() not ->parse() as
102 // a paranoia measure against a loop.
103 wfMessage( 'rawhtml-notallowed' )->escaped()
104 );
105 }
106 } else {
107 throw new MWException( '<html> extension tag encountered unexpectedly' );
108 }
109 }
110
111 /**
112 * Core parser tag hook function for 'nowiki'. Text within this section
113 * gets interpreted as a string of text with HTML-compatible character
114 * references, and wiki markup within it will not be expanded.
115 *
116 * Uses undocumented extended tag hook return values, introduced in r61913.
117 *
118 * Uses custom html escaping which phan-taint-check won't recognize
119 * hence we suppress the error.
120 * @suppress SecurityCheck-XSS
121 *
122 * @param string $content
123 * @param array $attributes
124 * @param Parser $parser
125 * @return array
126 */
127 public static function nowiki( $content, $attributes, $parser ) {
128 $content = strtr( $content, [
129 // lang converter
130 '-{' => '-&#123;',
131 '}-' => '&#125;-',
132 // html tags
133 '<' => '&lt;',
134 '>' => '&gt;'
135 // Note: Both '"' and '&' are not converted.
136 // This allows strip markers and entities through.
137 ] );
138 return [ $content, 'markerType' => 'nowiki' ];
139 }
140
141 /**
142 * Core parser tag hook function for 'gallery'.
143 *
144 * Renders a thumbnail list of the given images, with optional captions.
145 * Full syntax documented on the wiki:
146 *
147 * https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
148 *
149 * @todo break Parser::renderImageGallery out here too.
150 *
151 * @param string $content
152 * @param array $attributes
153 * @param Parser $parser
154 * @return string HTML
155 */
156 public static function gallery( $content, $attributes, $parser ) {
157 return $parser->renderImageGallery( $content, $attributes );
158 }
159
160 /**
161 * XML-style tag for page status indicators: icons (or short text snippets) usually displayed in
162 * the top-right corner of the page, outside of the main content.
163 *
164 * @param string $content
165 * @param array $attributes
166 * @param Parser $parser
167 * @param PPFrame $frame
168 * @return string
169 * @since 1.25
170 */
171 public static function indicator( $content, array $attributes, Parser $parser, PPFrame $frame ) {
172 if ( !isset( $attributes['name'] ) || trim( $attributes['name'] ) === '' ) {
173 return '<span class="error">' .
174 wfMessage( 'invalid-indicator-name' )->inContentLanguage()->parse() .
175 '</span>';
176 }
177
178 $parser->getOutput()->setIndicator(
179 trim( $attributes['name'] ),
180 Parser::stripOuterParagraph( $parser->recursiveTagParseFully( $content, $frame ) )
181 );
182
183 return '';
184 }
185 }