MediaWiki  REL1_19
CSSMin.php
Go to the documentation of this file.
00001 <?php
00028 class CSSMin {
00029 
00030         /* Constants */
00031 
00038         const EMBED_SIZE_LIMIT = 24576;
00039         const URL_REGEX = 'url\(\s*[\'"]?(?P<file>[^\?\)\'"]*)(?P<query>\??[^\)\'"]*)[\'"]?\s*\)';
00040 
00041         /* Protected Static Members */
00042 
00044         protected static $mimeTypes = array(
00045                 'gif' => 'image/gif',
00046                 'jpe' => 'image/jpeg',
00047                 'jpeg' => 'image/jpeg',
00048                 'jpg' => 'image/jpeg',
00049                 'png' => 'image/png',
00050                 'tif' => 'image/tiff',
00051                 'tiff' => 'image/tiff',
00052                 'xbm' => 'image/x-xbitmap',
00053         );
00054 
00055         /* Static Methods */
00056 
00064         public static function getLocalFileReferences( $source, $path = null ) {
00065                 $files = array();
00066                 $rFlags = PREG_OFFSET_CAPTURE | PREG_SET_ORDER;
00067                 if ( preg_match_all( '/' . self::URL_REGEX . '/', $source, $matches, $rFlags ) ) {
00068                         foreach ( $matches as $match ) {
00069                                 $file = ( isset( $path )
00070                                         ? rtrim( $path, '/' ) . '/'
00071                                         : '' ) . "{$match['file'][0]}";
00072 
00073                                 // Only proceed if we can access the file
00074                                 if ( !is_null( $path ) && file_exists( $file ) ) {
00075                                         $files[] = $file;
00076                                 }
00077                         }
00078                 }
00079                 return $files;
00080         }
00081 
00086         protected static function getMimeType( $file ) {
00087                 $realpath = realpath( $file );
00088                 // Try a couple of different ways to get the mime-type of a file, in order of
00089                 // preference
00090                 if (
00091                         $realpath
00092                         && function_exists( 'finfo_file' )
00093                         && function_exists( 'finfo_open' )
00094                         && defined( 'FILEINFO_MIME_TYPE' )
00095                 ) {
00096                         // As of PHP 5.3, this is how you get the mime-type of a file; it uses the Fileinfo
00097                         // PECL extension
00098                         return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
00099                 } elseif ( function_exists( 'mime_content_type' ) ) {
00100                         // Before this was deprecated in PHP 5.3, this was how you got the mime-type of a file
00101                         return mime_content_type( $file );
00102                 } else {
00103                         // Worst-case scenario has happened, use the file extension to infer the mime-type
00104                         $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
00105                         if ( isset( self::$mimeTypes[$ext] ) ) {
00106                                 return self::$mimeTypes[$ext];
00107                         }
00108                 }
00109                 return false;
00110         }
00111 
00122         public static function remap( $source, $local, $remote, $embedData = true ) {
00123                 $pattern = '/((?P<embed>\s*\/\*\s*\@embed\s*\*\/)(?P<pre>[^\;\}]*))?' .
00124                         self::URL_REGEX . '(?P<post>[^;]*)[\;]?/';
00125                 $offset = 0;
00126                 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
00127                         // Skip fully-qualified URLs and data URIs
00128                         $urlScheme = parse_url( $match['file'][0], PHP_URL_SCHEME );
00129                         if ( $urlScheme ) {
00130                                 // Move the offset to the end of the match, leaving it alone
00131                                 $offset = $match[0][1] + strlen( $match[0][0] );
00132                                 continue;
00133                         }
00134                         // URLs with absolute paths like /w/index.php need to be expanded
00135                         // to absolute URLs but otherwise left alone
00136                         if ( $match['file'][0] !== '' && $match['file'][0][0] === '/' ) {
00137                                 // Replace the file path with an expanded (possibly protocol-relative) URL
00138                                 // ...but only if wfExpandUrl() is even available.
00139                                 // This will not be the case if we're running outside of MW
00140                                 $lengthIncrease = 0;
00141                                 if ( function_exists( 'wfExpandUrl' ) ) {
00142                                         $expanded = wfExpandUrl( $match['file'][0], PROTO_RELATIVE );
00143                                         $origLength = strlen( $match['file'][0] );
00144                                         $lengthIncrease = strlen( $expanded ) - $origLength;
00145                                         $source = substr_replace( $source, $expanded,
00146                                                 $match['file'][1], $origLength
00147                                         );
00148                                 }
00149                                 // Move the offset to the end of the match, leaving it alone
00150                                 $offset = $match[0][1] + strlen( $match[0][0] ) + $lengthIncrease;
00151                                 continue;
00152                         }
00153                         // Shortcuts
00154                         $embed = $match['embed'][0];
00155                         $pre = $match['pre'][0];
00156                         $post = $match['post'][0];
00157                         $query = $match['query'][0];
00158                         $url = "{$remote}/{$match['file'][0]}";
00159                         $file = "{$local}/{$match['file'][0]}";
00160                         // bug 27052 - Guard against double slashes, because foo//../bar
00161                         // apparently resolves to foo/bar on (some?) clients
00162                         $url = preg_replace( '#([^:])//+#', '\1/', $url );
00163                         $replacement = false;
00164                         if ( $local !== false && file_exists( $file ) ) {
00165                                 // Add version parameter as a time-stamp in ISO 8601 format,
00166                                 // using Z for the timezone, meaning GMT
00167                                 $url .= '?' . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
00168                                 // Embedding requires a bit of extra processing, so let's skip that if we can
00169                                 if ( $embedData && $embed ) {
00170                                         $type = self::getMimeType( $file );
00171                                         // Detect when URLs were preceeded with embed tags, and also verify file size is
00172                                         // below the limit
00173                                         if (
00174                                                 $type
00175                                                 && $match['embed'][1] > 0
00176                                                 && filesize( $file ) < self::EMBED_SIZE_LIMIT
00177                                         ) {
00178                                                 // Strip off any trailing = symbols (makes browsers freak out)
00179                                                 $data = base64_encode( file_get_contents( $file ) );
00180                                                 // Build 2 CSS properties; one which uses a base64 encoded data URI in place
00181                                                 // of the @embed comment to try and retain line-number integrity, and the
00182                                                 // other with a remapped an versioned URL and an Internet Explorer hack
00183                                                 // making it ignored in all browsers that support data URIs
00184                                                 $replacement = "{$pre}url(data:{$type};base64,{$data}){$post};";
00185                                                 $replacement .= "{$pre}url({$url}){$post}!ie;";
00186                                         }
00187                                 }
00188                                 if ( $replacement === false ) {
00189                                         // Assume that all paths are relative to $remote, and make them absolute
00190                                         $replacement = "{$embed}{$pre}url({$url}){$post};";
00191                                 }
00192                         } elseif ( $local === false ) {
00193                                 // Assume that all paths are relative to $remote, and make them absolute
00194                                 $replacement = "{$embed}{$pre}url({$url}{$query}){$post};";
00195                         }
00196                         if ( $replacement !== false ) {
00197                                 // Perform replacement on the source
00198                                 $source = substr_replace(
00199                                         $source, $replacement, $match[0][1], strlen( $match[0][0] )
00200                                 );
00201                                 // Move the offset to the end of the replacement in the source
00202                                 $offset = $match[0][1] + strlen( $replacement );
00203                                 continue;
00204                         }
00205                         // Move the offset to the end of the match, leaving it alone
00206                         $offset = $match[0][1] + strlen( $match[0][0] );
00207                 }
00208                 return $source;
00209         }
00210 
00217         public static function minify( $css ) {
00218                 return trim(
00219                         str_replace(
00220                                 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
00221                                 array( ';', ':', '{', '{', ',', '}', '}' ),
00222                                 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
00223                         )
00224                 );
00225         }
00226 }