41 const URL_REGEX =
'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*?)(?P<query>\?[^\)\'"]*?|)[\'"]?\s*\)';
49 'jpe' =>
'image/jpeg',
50 'jpeg' =>
'image/jpeg',
51 'jpg' =>
'image/jpeg',
53 'tif' =>
'image/tiff',
54 'tiff' =>
'image/tiff',
55 'xbm' =>
'image/x-xbitmap',
56 'svg' =>
'image/svg+xml',
72 if (
$path ===
null ) {
79 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
80 if ( preg_match_all(
'/' . self::URL_REGEX .
'/',
$source,
$matches, $rFlags ) ) {
82 $url = $match[
'file'][0];
85 if ( substr( $url, 0, 2 ) ===
'//' || parse_url( $url, PHP_URL_SCHEME ) ) {
91 if ( file_exists(
$file ) ) {
116 if ( $sizeLimit !==
false && filesize(
$file ) >= $sizeLimit ) {
119 if (
$type ===
null ) {
125 $data = base64_encode( file_get_contents(
$file ) );
126 return 'data:' .
$type .
';base64,' . $data;
134 $realpath = realpath(
$file );
139 && function_exists(
'finfo_file' )
140 && function_exists(
'finfo_open' )
141 && defined(
'FILEINFO_MIME_TYPE' )
145 return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
146 } elseif ( function_exists(
'mime_content_type' ) ) {
148 return mime_content_type(
$file );
151 $ext = strtolower( pathinfo(
$file, PATHINFO_EXTENSION ) );
152 if ( isset( self::$mimeTypes[
$ext] ) ) {
153 return self::$mimeTypes[
$ext];
170 if ( preg_match(
'!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
173 return 'url("' . strtr( $url,
array(
'\\' =>
'\\\\',
'"' =>
'\\"' ) ) .
'")';
187 public static function remap(
$source, $local, $remote, $embedData =
true ) {
199 if ( substr( $remote, -1 ) ==
'/' ) {
200 $remote = substr( $remote, 0, -1 );
207 return preg_replace_callback( $pattern,
function ( $matchOuter ) use ( $local, $remote, $embedData ) {
208 $rule = $matchOuter[0];
212 $rule = preg_replace(
'/^(\s*)' .
CSSMin::EMBED_REGEX .
'\s*/',
'$1', $rule, 1, $embedAll );
217 $ruleWithRemapped = preg_replace_callback( $pattern,
function ( $match ) use ( $local, $remote ) {
218 $remapped =
CSSMin::remapOne( $match[
'file'], $match[
'query'], $local, $remote,
false );
223 $ruleWithEmbedded = preg_replace_callback( $pattern,
function ( $match ) use ( $embedAll, $local, $remote ) {
224 $embed = $embedAll || $match[
'embed'];
225 $embedded =
CSSMin::remapOne( $match[
'file'], $match[
'query'], $local, $remote, $embed );
230 if ( $embedData && $ruleWithEmbedded !== $ruleWithRemapped ) {
235 return "$ruleWithEmbedded;$ruleWithRemapped!ie";
238 return $ruleWithRemapped;
258 if ( substr( $url, 0, 2 ) ===
'//' || parse_url( $url, PHP_URL_SCHEME ) ) {
264 if ( $url !==
'' && $url[0] ===
'/' ) {
268 if ( function_exists(
'wfExpandUrl' ) ) {
275 if ( $local ===
false ) {
277 return $remote .
'/' . $url;
280 $url =
"{$remote}/{$file}";
282 $localFile =
"{$local}/{$file}";
283 if ( file_exists( $localFile ) ) {
286 $url .=
'?' . gmdate(
'Y-m-d\TH:i:s\Z', round( filemtime( $localFile ), -2 ) );
289 if ( $data !==
false ) {
309 array(
'; ',
': ',
' {',
'{ ',
', ',
'} ',
';}' ),
310 array(
';',
':',
'{',
'{',
',',
'}',
'}' ),
311 preg_replace(
array(
'/\s+/',
'/\/\*.*?\*\//s' ),
array(
' ',
'' ),
$css )