MediaWiki  master
CSSMin.php
Go to the documentation of this file.
1 <?php
30 class CSSMin {
31 
33  private const PLACEHOLDER = "\x7fPLACEHOLDER\x7f";
34 
38  private const DATA_URI_SIZE_LIMIT = 32768;
39 
40  private const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/';
41  private const COMMENT_REGEX = '\/\*.*?\*\/';
42 
44  protected static $mimeTypes = [
45  'gif' => 'image/gif',
46  'jpe' => 'image/jpeg',
47  'jpeg' => 'image/jpeg',
48  'jpg' => 'image/jpeg',
49  'png' => 'image/png',
50  'tif' => 'image/tiff',
51  'tiff' => 'image/tiff',
52  'xbm' => 'image/x-xbitmap',
53  'svg' => 'image/svg+xml',
54  ];
55 
63  public static function getLocalFileReferences( $source, $path ) {
64  $stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source );
65  $path = rtrim( $path, '/' ) . '/';
66  $files = [];
67 
68  $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
69  if ( preg_match_all( '/' . self::getUrlRegex() . '/J', $stripped, $matches, $rFlags ) ) {
70  foreach ( $matches as $match ) {
71  $url = $match['file'][0];
72 
73  // Skip fully-qualified and protocol-relative URLs and data URIs
74  if (
75  substr( $url, 0, 2 ) === '//' ||
76  parse_url( $url, PHP_URL_SCHEME )
77  ) {
78  break;
79  }
80 
81  // Strip trailing anchors - T115436
82  $anchor = strpos( $url, '#' );
83  if ( $anchor !== false ) {
84  $url = substr( $url, 0, $anchor );
85 
86  // '#some-anchors' is not a file
87  if ( $url === '' ) {
88  break;
89  }
90  }
91 
92  $files[] = $path . $url;
93  }
94  }
95  return $files;
96  }
97 
113  public static function encodeImageAsDataURI( $file, $type = null, $ie8Compat = true ) {
114  // Fast-fail for files that definitely exceed the maximum data URI length
115  if ( $ie8Compat && filesize( $file ) >= self::DATA_URI_SIZE_LIMIT ) {
116  return false;
117  }
118 
119  if ( $type === null ) {
121  }
122  if ( !$type ) {
123  return false;
124  }
125 
126  return self::encodeStringAsDataURI( file_get_contents( $file ), $type, $ie8Compat );
127  }
128 
141  public static function encodeStringAsDataURI( $contents, $type, $ie8Compat = true ) {
142  // Try #1: Non-encoded data URI
143 
144  // Remove XML declaration, it's not needed with data URI usage
145  $contents = preg_replace( "/<\\?xml.*?\\?>/", '', $contents );
146  // The regular expression matches ASCII whitespace and printable characters.
147  if ( preg_match( '/^[\r\n\t\x20-\x7e]+$/', $contents ) ) {
148  // Do not base64-encode non-binary files (sane SVGs).
149  // (This often produces longer URLs, but they compress better, yielding a net smaller size.)
150  $encoded = rawurlencode( $contents );
151  // Unencode some things that don't need to be encoded, to make the encoding smaller
152  $encoded = strtr( $encoded, [
153  '%20' => ' ', // Unencode spaces
154  '%2F' => '/', // Unencode slashes
155  '%3A' => ':', // Unencode colons
156  '%3D' => '=', // Unencode equals signs
157  '%0A' => ' ', // Change newlines to spaces
158  '%0D' => ' ', // Change carriage returns to spaces
159  '%09' => ' ', // Change tabs to spaces
160  ] );
161  // Consolidate runs of multiple spaces in a row
162  $encoded = preg_replace( '/ {2,}/', ' ', $encoded );
163  // Remove leading and trailing spaces
164  $encoded = preg_replace( '/^ | $/', '', $encoded );
165 
166  $uri = 'data:' . $type . ',' . $encoded;
167  if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
168  return $uri;
169  }
170  }
171 
172  // Try #2: Encoded data URI
173  $uri = 'data:' . $type . ';base64,' . base64_encode( $contents );
174  if ( !$ie8Compat || strlen( $uri ) < self::DATA_URI_SIZE_LIMIT ) {
175  return $uri;
176  }
177 
178  // A data URI couldn't be produced
179  return false;
180  }
181 
189  public static function serializeStringValue( $value ) {
190  $value = strtr( $value, [ "\0" => "\u{FFFD}", '\\' => '\\\\', '"' => '\\"' ] );
191  $value = preg_replace_callback( '/[\x01-\x1f\x7f]/', function ( $match ) {
192  return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
193  }, $value );
194  return '"' . $value . '"';
195  }
196 
201  public static function getMimeType( $file ) {
202  // Infer the MIME-type from the file extension
203  $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
204  return self::$mimeTypes[$ext] ?? mime_content_type( realpath( $file ) );
205  }
206 
216  public static function buildUrlValue( $url ) {
217  // The list below has been crafted to match URLs such as:
218  // scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s
219  // data:image/png;base64,R0lGODlh/+==
220  if ( preg_match( '!^[\w:@/~.%+;,?&=-]+$!', $url ) ) {
221  return "url($url)";
222  } else {
223  return 'url("' . strtr( $url, [ '\\' => '\\\\', '"' => '\\"' ] ) . '")';
224  }
225  }
226 
238  public static function remap( $source, $local, $remote, $embedData = true ) {
239  // High-level overview:
240  // * For each CSS rule in $source that includes at least one url() value:
241  // * Check for an @embed comment at the start indicating that all URIs should be embedded
242  // * For each url() value:
243  // * Check for an @embed comment directly preceding the value
244  // * If either @embed comment exists:
245  // * Embedding the URL as data: URI, if it's possible / allowed
246  // * Otherwise remap the URL to work in generated stylesheets
247 
248  // Guard against trailing slashes, because "some/remote/../foo.png"
249  // resolves to "some/remote/foo.png" on (some?) clients (T29052).
250  if ( substr( $remote, -1 ) == '/' ) {
251  $remote = substr( $remote, 0, -1 );
252  }
253 
254  // Disallow U+007F DELETE, which is illegal anyway, and which
255  // we use for comment placeholders.
256  $source = str_replace( "\x7f", "?", $source );
257 
258  // Replace all comments by a placeholder so they will not interfere with the remapping.
259  // Warning: This will also catch on anything looking like the start of a comment between
260  // quotation marks (e.g. "foo /* bar").
261  $comments = [];
262 
263  $pattern = '/(?!' . self::EMBED_REGEX . ')(' . self::COMMENT_REGEX . ')/s';
264 
265  $source = preg_replace_callback(
266  $pattern,
267  function ( $match ) use ( &$comments ) {
268  $comments[] = $match[ 0 ];
269  return self::PLACEHOLDER . ( count( $comments ) - 1 ) . 'x';
270  },
271  $source
272  );
273 
274  // Note: This will not correctly handle cases where ';', '{' or '}'
275  // appears in the rule itself, e.g. in a quoted string. You are advised
276  // not to use such characters in file names. We also match start/end of
277  // the string to be consistent in edge-cases ('@import url(…)').
278  $pattern = '/(?:^|[;{])\K[^;{}]*' . self::getUrlRegex() . '[^;}]*(?=[;}]|$)/J';
279 
280  $source = preg_replace_callback(
281  $pattern,
282  function ( $matchOuter ) use ( $local, $remote, $embedData ) {
283  $rule = $matchOuter[0];
284 
285  // Check for global @embed comment and remove it. Allow other comments to be present
286  // before @embed (they have been replaced with placeholders at this point).
287  $embedAll = false;
288  $rule = preg_replace(
289  '/^((?:\s+|' .
290  self::PLACEHOLDER .
291  '(\d+)x)*)' .
292  self::EMBED_REGEX .
293  '\s*/',
294  '$1',
295  $rule,
296  1,
297  $embedAll
298  );
299 
300  // Build two versions of current rule: with remapped URLs
301  // and with embedded data: URIs (where possible).
302  $pattern = '/(?P<embed>' . self::EMBED_REGEX . '\s*|)' . self::getUrlRegex() . '/J';
303 
304  $ruleWithRemapped = preg_replace_callback(
305  $pattern,
306  function ( $match ) use ( $local, $remote ) {
307  $remapped = self::remapOne( $match['file'], $match['query'], $local, $remote, false );
308  return self::buildUrlValue( $remapped );
309  },
310  $rule
311  );
312 
313  if ( $embedData ) {
314  // Remember the occurring MIME types to avoid fallbacks when embedding some files.
315  $mimeTypes = [];
316 
317  $ruleWithEmbedded = preg_replace_callback(
318  $pattern,
319  function ( $match ) use ( $embedAll, $local, $remote, &$mimeTypes ) {
320  $embed = $embedAll || $match['embed'];
321  $embedded = self::remapOne(
322  $match['file'],
323  $match['query'],
324  $local,
325  $remote,
326  $embed
327  );
328 
329  $url = $match['file'] . $match['query'];
330  $file = "{$local}/{$match['file']}";
331  if (
332  !self::isRemoteUrl( $url ) && !self::isLocalUrl( $url )
333  && file_exists( $file )
334  ) {
335  $mimeTypes[ self::getMimeType( $file ) ] = true;
336  }
337 
338  return self::buildUrlValue( $embedded );
339  },
340  $rule
341  );
342 
343  // Are all referenced images SVGs?
344  $needsEmbedFallback = $mimeTypes !== [ 'image/svg+xml' => true ];
345  }
346 
347  if ( !$embedData || $ruleWithEmbedded === $ruleWithRemapped ) {
348  // We're not embedding anything, or we tried to but the file is not embeddable
349  return $ruleWithRemapped;
350  } else {
351  // Use a data URI in place of the @embed comment.
352  return $ruleWithEmbedded;
353  }
354  }, $source );
355 
356  // Re-insert comments
357  $pattern = '/' . self::PLACEHOLDER . '(\d+)x/';
358  $source = preg_replace_callback( $pattern, function ( $match ) use ( &$comments ) {
359  return $comments[ $match[1] ];
360  }, $source );
361 
362  return $source;
363  }
364 
371  protected static function isRemoteUrl( $maybeUrl ) {
372  if ( substr( $maybeUrl, 0, 2 ) === '//' || parse_url( $maybeUrl, PHP_URL_SCHEME ) ) {
373  return true;
374  }
375  return false;
376  }
377 
384  protected static function isLocalUrl( $maybeUrl ) {
385  // Accept "/" (known local)
386  // Accept "/anything" (known local)
387  // Reject "//anything" (known remote)
388  // Reject "" (invalid/uncertain)
389  return $maybeUrl === '/' || ( isset( $maybeUrl[1] ) && $maybeUrl[0] === '/' && $maybeUrl[1] !== '/' );
390  }
391 
396  private static function getUrlRegex() {
397  static $urlRegex;
398  if ( $urlRegex === null ) {
399  $urlRegex = '(' .
400  // Unquoted url
401  'url\‍(\s*(?P<file>[^\s\'"][^\?\‍)]+?)(?P<query>\?[^\‍)]*?|)\s*\‍)' .
402  // Single quoted url
403  '|url\‍(\s*\'(?P<file>[^\?\']+?)(?P<query>\?[^\']*?|)\'\s*\‍)' .
404  // Double quoted url
405  '|url\‍(\s*"(?P<file>[^\?"]+?)(?P<query>\?[^"]*?|)"\s*\‍)' .
406  ')';
407  }
408  return $urlRegex;
409  }
410 
418  private static function resolveUrl( string $base, string $url ) : string {
419  // Net_URL2::resolve() doesn't allow for resolving against server-less URLs.
420  // We need this as for MediaWiki/ResourceLoader, the remote base path may either
421  // be separate (e.g. a separate domain), or simply local (like "/w"). In the
422  // local case, we don't want to needlessly include the server in the output.
423  $isServerless = self::isLocalUrl( $base );
424  if ( $isServerless ) {
425  $base = "https://placeholder.invalid$base";
426  }
427  // Net_URL2::resolve() doesn't allow for protocol-relative URLs, but we want to.
428  $isProtoRelative = substr( $base, 0, 2 ) === '//';
429  if ( $isProtoRelative ) {
430  $base = "https:$base";
431  }
432 
433  $baseUrl = new Net_URL2( $base );
434  $ret = $baseUrl->resolve( $url );
435  if ( $isProtoRelative ) {
436  $ret->setScheme( false );
437  }
438  if ( $isServerless ) {
439  $ret->setScheme( false );
440  $ret->setHost( false );
441  }
442  return $ret->getURL();
443  }
444 
455  public static function remapOne( $file, $query, $local, $remote, $embed ) {
456  // The full URL possibly with query, as passed to the 'url()' value in CSS
457  $url = $file . $query;
458 
459  // Expand local URLs with absolute paths to a full URL (possibly protocol-relative).
460  if ( self::isLocalUrl( $url ) ) {
461  return self::resolveUrl( $remote, $url );
462  }
463 
464  // Pass thru fully-qualified and protocol-relative URLs and data URIs, as well as local URLs if
465  // we can't expand them.
466  // Also skips anchors or the rare `behavior` property specifying application's default behavior
467  if (
468  self::isRemoteUrl( $url ) ||
469  substr( $url, 0, 1 ) === '#'
470  ) {
471  return $url;
472  }
473 
474  // The $remote must have a trailing slash beyond this point for correct path resolution.
475  if ( substr( $remote, -1 ) !== '/' ) {
476  $remote .= '/';
477  }
478 
479  if ( $local === false ) {
480  // CSS specifies a path that is neither a local file path, nor a local URL.
481  // It is probably already a fully-qualitied URL or data URI, but try to expand
482  // it just in case.
483  $url = self::resolveUrl( $remote, $url );
484  } else {
485  // We drop the query part here and instead make the path relative to $remote
486  $url = self::resolveUrl( $remote, $file );
487  // Path to the actual file on the filesystem
488  $localFile = "{$local}/{$file}";
489  if ( file_exists( $localFile ) ) {
490  if ( $embed ) {
491  $data = self::encodeImageAsDataURI( $localFile );
492  if ( $data !== false ) {
493  return $data;
494  }
495  }
496  // Add version parameter as the first five hex digits
497  // of the MD5 hash of the file's contents.
498  $url .= '?' . substr( md5_file( $localFile ), 0, 5 );
499  }
500  // If any of these conditions failed (file missing, we don't want to embed it
501  // or it's not embeddable), return the URL (possibly with ?timestamp part)
502  }
503  return $url;
504  }
505 
512  public static function minify( $css ) {
513  return trim(
514  str_replace(
515  [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}', '( ', ' )', '[ ', ' ]' ],
516  [ ';', ':', '{', '{', ',', '}', '}', '(', ')', '[', ']' ],
517  preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
518  )
519  );
520  }
521 }
CSSMin\isRemoteUrl
static isRemoteUrl( $maybeUrl)
Is this CSS rule referencing a remote URL?
Definition: CSSMin.php:371
CSSMin\buildUrlValue
static buildUrlValue( $url)
Build a CSS 'url()' value for the given URL, quoting parentheses (and other funny characters) and esc...
Definition: CSSMin.php:216
CSSMin\minify
static minify( $css)
Removes whitespace from CSS data.
Definition: CSSMin.php:512
CSSMin\encodeStringAsDataURI
static encodeStringAsDataURI( $contents, $type, $ie8Compat=true)
Encode file contents as a data URI with chosen MIME type.
Definition: CSSMin.php:141
CSSMin\remap
static remap( $source, $local, $remote, $embedData=true)
Remaps CSS URL paths and automatically embeds data URIs for CSS rules or url() values preceded by an ...
Definition: CSSMin.php:238
true
return true
Definition: router.php:90
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
CSSMin\serializeStringValue
static serializeStringValue( $value)
Serialize a string (escape and quote) for use as a CSS string value.
Definition: CSSMin.php:189
$base
$base
Definition: generateLocalAutoload.php:11
CSSMin\encodeImageAsDataURI
static encodeImageAsDataURI( $file, $type=null, $ie8Compat=true)
Encode an image file as a data URI.
Definition: CSSMin.php:113
CSSMin\EMBED_REGEX
const EMBED_REGEX
Definition: CSSMin.php:40
$matches
$matches
Definition: NoLocalSettings.php:24
CSSMin\$mimeTypes
static string[] $mimeTypes
List of common image files extensions and MIME-types.
Definition: CSSMin.php:44
CSSMin\getUrlRegex
static getUrlRegex()
Definition: CSSMin.php:396
CSSMin\isLocalUrl
static isLocalUrl( $maybeUrl)
Is this CSS rule referencing a local URL?
Definition: CSSMin.php:384
CSSMin\getLocalFileReferences
static getLocalFileReferences( $source, $path)
Get a list of local files referenced in a stylesheet (includes non-existent files).
Definition: CSSMin.php:63
CSSMin\resolveUrl
static resolveUrl(string $base, string $url)
Resolve a possibly-relative URL against a base URL.
Definition: CSSMin.php:418
CSSMin\DATA_URI_SIZE_LIMIT
const DATA_URI_SIZE_LIMIT
Internet Explorer data URI length limit.
Definition: CSSMin.php:38
CSSMin\COMMENT_REGEX
const COMMENT_REGEX
Definition: CSSMin.php:41
CSSMin\getMimeType
static getMimeType( $file)
Definition: CSSMin.php:201
CSSMin
Transforms CSS data.
Definition: CSSMin.php:30
$path
$path
Definition: NoLocalSettings.php:25
$source
$source
Definition: mwdoc-filter.php:34
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
CSSMin\remapOne
static remapOne( $file, $query, $local, $remote, $embed)
Remap or embed a CSS URL path.
Definition: CSSMin.php:455
$type
$type
Definition: testCompression.php:52