MediaWiki  master
CSSMin.php
Go to the documentation of this file.
1 <?php
30 class CSSMin {
31 
33  const PLACEHOLDER = "\x7fPLACEHOLDER\x7f";
34 
38  const DATA_URI_SIZE_LIMIT = 32768;
39 
40  const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/';
41  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 ) {
120  $type = self::getMimeType( $file );
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\d:@/~.%+;,?&=-]+$!', $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 CSSMin::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  CSSMin::PLACEHOLDER .
291  '(\d+)x)*)' .
292  CSSMin::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>' . CSSMin::EMBED_REGEX . '\s*|)' . self::getUrlRegex() . '/J';
303 
304  $ruleWithRemapped = preg_replace_callback(
305  $pattern,
306  function ( $match ) use ( $local, $remote ) {
307  $remapped = CSSMin::remapOne( $match['file'], $match['query'], $local, $remote, false );
308  return CSSMin::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 = CSSMin::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[ CSSMin::getMimeType( $file ) ] = true;
336  }
337 
338  return CSSMin::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  return isset( $maybeUrl[1] ) && $maybeUrl[0] === '/' && $maybeUrl[1] !== '/';
386  }
387 
392  private static function getUrlRegex() {
393  static $urlRegex;
394  if ( $urlRegex === null ) {
395  $urlRegex = '(' .
396  // Unquoted url
397  'url\(\s*(?P<file>[^\s\'"][^\?\)]+?)(?P<query>\?[^\)]*?|)\s*\)' .
398  // Single quoted url
399  '|url\(\s*\'(?P<file>[^\?\']+?)(?P<query>\?[^\']*?|)\'\s*\)' .
400  // Double quoted url
401  '|url\(\s*"(?P<file>[^\?"]+?)(?P<query>\?[^"]*?|)"\s*\)' .
402  ')';
403  }
404  return $urlRegex;
405  }
406 
417  public static function remapOne( $file, $query, $local, $remote, $embed ) {
418  // The full URL possibly with query, as passed to the 'url()' value in CSS
419  $url = $file . $query;
420 
421  // Expand local URLs with absolute paths like /w/index.php to possibly protocol-relative URL, if
422  // wfExpandUrl() is available. (This will not be the case if we're running outside of MW.)
423  if ( self::isLocalUrl( $url ) && function_exists( 'wfExpandUrl' ) ) {
424  return wfExpandUrl( $url, PROTO_RELATIVE );
425  }
426 
427  // Pass thru fully-qualified and protocol-relative URLs and data URIs, as well as local URLs if
428  // we can't expand them.
429  // Also skips anchors or the rare `behavior` property specifying application's default behavior
430  if (
431  self::isRemoteUrl( $url ) ||
432  self::isLocalUrl( $url ) ||
433  substr( $url, 0, 1 ) === '#'
434  ) {
435  return $url;
436  }
437 
438  if ( $local === false ) {
439  // Assume that all paths are relative to $remote, and make them absolute
440  $url = $remote . '/' . $url;
441  } else {
442  // We drop the query part here and instead make the path relative to $remote
443  $url = "{$remote}/{$file}";
444  // Path to the actual file on the filesystem
445  $localFile = "{$local}/{$file}";
446  if ( file_exists( $localFile ) ) {
447  if ( $embed ) {
448  $data = self::encodeImageAsDataURI( $localFile );
449  if ( $data !== false ) {
450  return $data;
451  }
452  }
453  if ( class_exists( OutputPage::class ) ) {
454  $url = OutputPage::transformFilePath( $remote, $local, $file );
455  } else {
456  // Add version parameter as the first five hex digits
457  // of the MD5 hash of the file's contents.
458  $url .= '?' . substr( md5_file( $localFile ), 0, 5 );
459  }
460  }
461  // If any of these conditions failed (file missing, we don't want to embed it
462  // or it's not embeddable), return the URL (possibly with ?timestamp part)
463  }
464  if ( function_exists( 'wfRemoveDotSegments' ) ) {
465  $url = wfRemoveDotSegments( $url );
466  }
467  return $url;
468  }
469 
476  public static function minify( $css ) {
477  return trim(
478  str_replace(
479  [ '; ', ': ', ' {', '{ ', ', ', '} ', ';}', '( ', ' )', '[ ', ' ]' ],
480  [ ';', ':', '{', '{', ',', '}', '}', '(', ')', '[', ']' ],
481  preg_replace( [ '/\s+/', '/\/\*.*?\*\//s' ], [ ' ', '' ], $css )
482  )
483  );
484  }
485 }
wfRemoveDotSegments
wfRemoveDotSegments( $urlPath)
Remove all dot-segments in the provided URL path.
Definition: GlobalFunctions.php:641
true
return true
Definition: router.php:92
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$matches
$matches
Definition: NoLocalSettings.php:24
PROTO_RELATIVE
const PROTO_RELATIVE
Definition: Defines.php:201
$path
$path
Definition: NoLocalSettings.php:25
$source
$source
Definition: mwdoc-filter.php:34
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491
$type
$type
Definition: testCompression.php:50