MediaWiki  REL1_24
CSSMin.php
Go to the documentation of this file.
00001 <?php
00030 class CSSMin {
00031 
00032     /* Constants */
00033 
00040     const EMBED_SIZE_LIMIT = 24576;
00041     const URL_REGEX = 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*?)(?P<query>\?[^\)\'"]*?|)[\'"]?\s*\)';
00042     const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/';
00043     const COMMENT_REGEX = '\/\*.*?\*\/';
00044 
00045     /* Protected Static Members */
00046 
00048     protected static $mimeTypes = array(
00049         'gif' => 'image/gif',
00050         'jpe' => 'image/jpeg',
00051         'jpeg' => 'image/jpeg',
00052         'jpg' => 'image/jpeg',
00053         'png' => 'image/png',
00054         'tif' => 'image/tiff',
00055         'tiff' => 'image/tiff',
00056         'xbm' => 'image/x-xbitmap',
00057         'svg' => 'image/svg+xml',
00058     );
00059 
00060     /* Static Methods */
00061 
00072     public static function getLocalFileReferences( $source, $path = null ) {
00073         if ( $path === null ) {
00074             return array();
00075         }
00076 
00077         $path = rtrim( $path, '/' ) . '/';
00078         $files = array();
00079 
00080         $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
00081         if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
00082             foreach ( $matches as $match ) {
00083                 $url = $match['file'][0];
00084 
00085                 // Skip fully-qualified and protocol-relative URLs and data URIs
00086                 if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
00087                     break;
00088                 }
00089 
00090                 $file = $path . $url;
00091                 // Skip non-existent files
00092                 if ( file_exists( $file ) ) {
00093                     break;
00094                 }
00095 
00096                 $files[] = $file;
00097             }
00098         }
00099         return $files;
00100     }
00101 
00116     public static function encodeImageAsDataURI( $file, $type = null,
00117         $sizeLimit = self::EMBED_SIZE_LIMIT
00118     ) {
00119         if ( $sizeLimit !== false && filesize( $file ) >= $sizeLimit ) {
00120             return false;
00121         }
00122         if ( $type === null ) {
00123             $type = self::getMimeType( $file );
00124         }
00125         if ( !$type ) {
00126             return false;
00127         }
00128         $data = base64_encode( file_get_contents( $file ) );
00129         return 'data:' . $type . ';base64,' . $data;
00130     }
00131 
00136     public static function getMimeType( $file ) {
00137         $realpath = realpath( $file );
00138         if (
00139             $realpath
00140             && function_exists( 'finfo_file' )
00141             && function_exists( 'finfo_open' )
00142             && defined( 'FILEINFO_MIME_TYPE' )
00143         ) {
00144             return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
00145         }
00146 
00147         // Infer the MIME-type from the file extension
00148         $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
00149         if ( isset( self::$mimeTypes[$ext] ) ) {
00150             return self::$mimeTypes[$ext];
00151         }
00152 
00153         return false;
00154     }
00155 
00165     public static function buildUrlValue( $url ) {
00166         // The list below has been crafted to match URLs such as:
00167         //   scheme://user@domain:port/~user/fi%20le.png?query=yes&really=y+s
00168         //   data:image/png;base64,R0lGODlh/+==
00169         if ( preg_match( '!^[\w\d:@/~.%+;,?&=-]+$!', $url ) ) {
00170             return "url($url)";
00171         } else {
00172             return 'url("' . strtr( $url, array( '\\' => '\\\\', '"' => '\\"' ) ) . '")';
00173         }
00174     }
00175 
00187     public static function remap( $source, $local, $remote, $embedData = true ) {
00188         // High-level overview:
00189         // * For each CSS rule in $source that includes at least one url() value:
00190         //   * Check for an @embed comment at the start indicating that all URIs should be embedded
00191         //   * For each url() value:
00192         //     * Check for an @embed comment directly preceding the value
00193         //     * If either @embed comment exists:
00194         //       * Embedding the URL as data: URI, if it's possible / allowed
00195         //       * Otherwise remap the URL to work in generated stylesheets
00196 
00197         // Guard against trailing slashes, because "some/remote/../foo.png"
00198         // resolves to "some/remote/foo.png" on (some?) clients (bug 27052).
00199         if ( substr( $remote, -1 ) == '/' ) {
00200             $remote = substr( $remote, 0, -1 );
00201         }
00202 
00203         // Replace all comments by a placeholder so they will not interfere with the remapping.
00204         // Warning: This will also catch on anything looking like the start of a comment between
00205         // quotation marks (e.g. "foo /* bar").
00206         $comments = array();
00207         $placeholder = uniqid( '', true );
00208 
00209         $pattern = '/(?!' . CSSMin::EMBED_REGEX . ')(' . CSSMin::COMMENT_REGEX . ')/s';
00210 
00211         $source = preg_replace_callback(
00212             $pattern,
00213             function ( $match ) use ( &$comments, $placeholder ) {
00214                 $comments[] = $match[ 0 ];
00215                 return $placeholder . ( count( $comments ) - 1 ) . 'x';
00216             },
00217             $source
00218         );
00219 
00220         // Note: This will not correctly handle cases where ';', '{' or '}'
00221         // appears in the rule itself, e.g. in a quoted string. You are advised
00222         // not to use such characters in file names. We also match start/end of
00223         // the string to be consistent in edge-cases ('@import url(…)').
00224         $pattern = '/(?:^|[;{])\K[^;{}]*' . CSSMin::URL_REGEX . '[^;}]*(?=[;}]|$)/';
00225 
00226         $source = preg_replace_callback(
00227             $pattern,
00228             function ( $matchOuter ) use ( $local, $remote, $embedData, $placeholder ) {
00229                 $rule = $matchOuter[0];
00230 
00231                 // Check for global @embed comment and remove it. Allow other comments to be present
00232                 // before @embed (they have been replaced with placeholders at this point).
00233                 $embedAll = false;
00234                 $rule = preg_replace( '/^((?:\s+|' . $placeholder . '(\d+)x)*)' . CSSMin::EMBED_REGEX . '\s*/', '$1', $rule, 1, $embedAll );
00235 
00236                 // Build two versions of current rule: with remapped URLs
00237                 // and with embedded data: URIs (where possible).
00238                 $pattern = '/(?P<embed>' . CSSMin::EMBED_REGEX . '\s*|)' . CSSMin::URL_REGEX . '/';
00239 
00240                 $ruleWithRemapped = preg_replace_callback(
00241                     $pattern,
00242                     function ( $match ) use ( $local, $remote ) {
00243                         $remapped = CSSMin::remapOne( $match['file'], $match['query'], $local, $remote, false );
00244 
00245                         return CSSMin::buildUrlValue( $remapped );
00246                     },
00247                     $rule
00248                 );
00249 
00250                 if ( $embedData ) {
00251                     $ruleWithEmbedded = preg_replace_callback(
00252                         $pattern,
00253                         function ( $match ) use ( $embedAll, $local, $remote ) {
00254                             $embed = $embedAll || $match['embed'];
00255                             $embedded = CSSMin::remapOne(
00256                                 $match['file'],
00257                                 $match['query'],
00258                                 $local,
00259                                 $remote,
00260                                 $embed
00261                             );
00262 
00263                             return CSSMin::buildUrlValue( $embedded );
00264                         },
00265                         $rule
00266                     );
00267                 }
00268 
00269                 if ( $embedData && $ruleWithEmbedded !== $ruleWithRemapped ) {
00270                     // Build 2 CSS properties; one which uses a base64 encoded data URI in place
00271                     // of the @embed comment to try and retain line-number integrity, and the
00272                     // other with a remapped an versioned URL and an Internet Explorer hack
00273                     // making it ignored in all browsers that support data URIs
00274                     return "$ruleWithEmbedded;$ruleWithRemapped!ie";
00275                 } else {
00276                     // No reason to repeat twice
00277                     return $ruleWithRemapped;
00278                 }
00279             }, $source );
00280 
00281         // Re-insert comments
00282         $pattern = '/' . $placeholder . '(\d+)x/';
00283         $source = preg_replace_callback( $pattern, function( $match ) use ( &$comments ) {
00284             return $comments[ $match[1] ];
00285         }, $source );
00286 
00287         return $source;
00288 
00289     }
00290 
00301     public static function remapOne( $file, $query, $local, $remote, $embed ) {
00302         // The full URL possibly with query, as passed to the 'url()' value in CSS
00303         $url = $file . $query;
00304 
00305         // Skip fully-qualified and protocol-relative URLs and data URIs
00306         if ( substr( $url, 0, 2 ) === '//' || parse_url( $url, PHP_URL_SCHEME ) ) {
00307             return $url;
00308         }
00309 
00310         // URLs with absolute paths like /w/index.php need to be expanded
00311         // to absolute URLs but otherwise left alone
00312         if ( $url !== '' && $url[0] === '/' ) {
00313             // Replace the file path with an expanded (possibly protocol-relative) URL
00314             // ...but only if wfExpandUrl() is even available.
00315             // This will not be the case if we're running outside of MW
00316             if ( function_exists( 'wfExpandUrl' ) ) {
00317                 return wfExpandUrl( $url, PROTO_RELATIVE );
00318             } else {
00319                 return $url;
00320             }
00321         }
00322 
00323         if ( $local === false ) {
00324             // Assume that all paths are relative to $remote, and make them absolute
00325             return $remote . '/' . $url;
00326         } else {
00327             // We drop the query part here and instead make the path relative to $remote
00328             $url = "{$remote}/{$file}";
00329             // Path to the actual file on the filesystem
00330             $localFile = "{$local}/{$file}";
00331             if ( file_exists( $localFile ) ) {
00332                 // Add version parameter as a time-stamp in ISO 8601 format,
00333                 // using Z for the timezone, meaning GMT
00334                 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $localFile ), -2 ) );
00335                 if ( $embed ) {
00336                     $data = self::encodeImageAsDataURI( $localFile );
00337                     if ( $data !== false ) {
00338                         return $data;
00339                     }
00340                 }
00341             }
00342             // If any of these conditions failed (file missing, we don't want to embed it
00343             // or it's not embeddable), return the URL (possibly with ?timestamp part)
00344             return $url;
00345         }
00346     }
00347 
00354     public static function minify( $css ) {
00355         return trim(
00356             str_replace(
00357                 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
00358                 array( ';', ':', '{', '{', ',', '}', '}' ),
00359                 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
00360             )
00361         );
00362     }
00363 }