2017-02-20 04:33:24 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
2018-03-02 23:43:40 +00:00
|
|
|
|
* @license GPL-2.0-or-later
|
2017-02-20 04:33:24 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use Wikimedia\CSS\Parser\Parser as CSSParser;
|
|
|
|
|
use Wikimedia\CSS\Util as CSSUtil;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Content object for sanitized CSS.
|
|
|
|
|
*/
|
|
|
|
|
class TemplateStylesContent extends TextContent {
|
|
|
|
|
|
|
|
|
|
public function __construct( $text, $modelId = 'sanitized-css' ) {
|
|
|
|
|
parent::__construct( $text, $modelId );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle errors from the CSS parser and/or sanitizer
|
|
|
|
|
* @param StatusValue $status Object to add errors to
|
2019-11-06 19:22:50 +00:00
|
|
|
|
* @param array[] $errors Error array
|
2017-02-20 04:33:24 +00:00
|
|
|
|
* @param string $severity Whether to consider errors as 'warning' or 'fatal'
|
|
|
|
|
*/
|
|
|
|
|
protected static function processErrors( StatusValue $status, array $errors, $severity ) {
|
|
|
|
|
if ( $severity !== 'warning' && $severity !== 'fatal' ) {
|
2018-02-06 02:55:05 +00:00
|
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-20 04:33:24 +00:00
|
|
|
|
throw new \InvalidArgumentException( 'Invalid $severity' );
|
2018-02-06 02:55:05 +00:00
|
|
|
|
// @codeCoverageIgnoreEnd
|
2017-02-20 04:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
|
$error[0] = 'templatestyles-error-' . $error[0];
|
|
|
|
|
call_user_func_array( [ $status, $severity ], $error );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sanitize the content
|
|
|
|
|
* @param array $options Options are:
|
|
|
|
|
* - class: (string) Class to prefix selectors with
|
2018-10-17 17:28:43 +00:00
|
|
|
|
* - extraWrapper: (string) Extra simple selector to prefix selectors with
|
2017-02-20 04:33:24 +00:00
|
|
|
|
* - flip: (bool) Have CSSJanus flip the stylesheet.
|
|
|
|
|
* - minify: (bool) Whether to minify. Default true.
|
|
|
|
|
* - novalue: (bool) Don't bother returning the actual stylesheet, just
|
|
|
|
|
* fill the Status with warnings.
|
|
|
|
|
* - severity: (string) Whether to consider errors as 'warning' or 'fatal'
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function sanitize( array $options = [] ) {
|
|
|
|
|
$options += [
|
|
|
|
|
'class' => false,
|
2018-10-17 17:28:43 +00:00
|
|
|
|
'extraWrapper' => null,
|
2017-02-20 04:33:24 +00:00
|
|
|
|
'flip' => false,
|
|
|
|
|
'minify' => true,
|
|
|
|
|
'novalue' => false,
|
|
|
|
|
'severity' => 'warning',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
|
|
|
|
$style = $this->getNativeData();
|
|
|
|
|
$maxSize = TemplateStylesHooks::getConfig()->get( 'TemplateStylesMaxStylesheetSize' );
|
|
|
|
|
if ( $maxSize !== null && strlen( $style ) > $maxSize ) {
|
|
|
|
|
$status->fatal(
|
|
|
|
|
// Status::getWikiText() chokes on the Message::sizeParam if we
|
|
|
|
|
// don't wrap it in a Message ourself.
|
|
|
|
|
wfMessage( 'templatestyles-size-exceeded', $maxSize, Message::sizeParam( $maxSize ) )
|
|
|
|
|
);
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $options['flip'] ) {
|
|
|
|
|
$style = CSSJanus::transform( $style, true, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse it, and collect any errors
|
|
|
|
|
$cssParser = CSSParser::newFromString( $style );
|
|
|
|
|
$stylesheet = $cssParser->parseStylesheet();
|
|
|
|
|
self::processErrors( $status, $cssParser->getParseErrors(), $options['severity'] );
|
|
|
|
|
|
|
|
|
|
// Sanitize it, and collect any errors
|
2018-10-17 17:28:43 +00:00
|
|
|
|
$sanitizer = TemplateStylesHooks::getSanitizer(
|
|
|
|
|
$options['class'] ?: 'mw-parser-output', $options['extraWrapper']
|
|
|
|
|
);
|
2017-02-20 04:33:24 +00:00
|
|
|
|
$sanitizer->clearSanitizationErrors(); // Just in case
|
|
|
|
|
$stylesheet = $sanitizer->sanitize( $stylesheet );
|
|
|
|
|
self::processErrors( $status, $sanitizer->getSanitizationErrors(), $options['severity'] );
|
|
|
|
|
$sanitizer->clearSanitizationErrors();
|
|
|
|
|
|
|
|
|
|
// Stringify it while minifying
|
2017-06-13 15:52:07 +00:00
|
|
|
|
$value = CSSUtil::stringify( $stylesheet, [ 'minify' => $options['minify'] ] );
|
|
|
|
|
|
|
|
|
|
// Sanity check, don't allow "</style" if one somehow sneaks through the sanitizer
|
|
|
|
|
if ( preg_match( '!</style!i', $value ) ) {
|
|
|
|
|
$value = '';
|
|
|
|
|
$status->fatal( 'templatestyles-end-tag-injection' );
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-20 04:33:24 +00:00
|
|
|
|
if ( !$options['novalue'] ) {
|
2017-06-13 15:52:07 +00:00
|
|
|
|
$status->value = $value;
|
2017-02-20 04:33:24 +00:00
|
|
|
|
|
|
|
|
|
// Sanity check, don't allow raw U+007F if one somehow sneaks through the sanitizer
|
|
|
|
|
$status->value = strtr( $status->value, [ "\x7f" => '<27>' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function prepareSave( WikiPage $page, $flags, $parentRevId, User $user ) {
|
|
|
|
|
return $this->sanitize( [ 'novalue' => true, 'severity' => 'fatal' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string CSS wrapped in a <pre> tag.
|
|
|
|
|
*/
|
|
|
|
|
protected function getHtml() {
|
|
|
|
|
$html = "";
|
|
|
|
|
$html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
|
2018-02-11 16:18:13 +00:00
|
|
|
|
$html .= htmlspecialchars( $this->getNativeData(), ENT_NOQUOTES );
|
2017-02-20 04:33:24 +00:00
|
|
|
|
$html .= "\n</pre>\n";
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParserOutput( Title $title, $revId = null,
|
|
|
|
|
ParserOptions $options = null, $generateHtml = true
|
|
|
|
|
) {
|
|
|
|
|
if ( $options === null ) {
|
2018-07-11 16:34:25 +00:00
|
|
|
|
$options = ParserOptions::newCanonical( 'canonical' );
|
2017-02-20 04:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inject our warnings into the resulting ParserOutput
|
|
|
|
|
$po = parent::getParserOutput( $title, $revId, $options, $generateHtml );
|
|
|
|
|
$status = $this->sanitize( [ 'novalue' => true, 'class' => $options->getWrapOutputClass() ] );
|
2018-05-26 21:45:34 +00:00
|
|
|
|
if ( $status->getErrors() ) {
|
|
|
|
|
foreach ( $status->getErrors() as $error ) {
|
|
|
|
|
$po->addWarning(
|
|
|
|
|
Message::newFromSpecifier( array_merge( [ $error['message'] ], $error['params'] ) )->parse()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$po->addTrackingCategory( 'templatestyles-stylesheet-error-category', $title );
|
2017-02-20 04:33:24 +00:00
|
|
|
|
}
|
|
|
|
|
return $po;
|
|
|
|
|
}
|
|
|
|
|
}
|