Moved stand-alone libraries to includes/libs.
[lhc/web/wiklou.git] / includes / libs / CSSMin.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 /**
20 * Transforms CSS data
21 *
22 * This class provides minification, URL remapping, URL extracting, and data-URL embedding.
23 *
24 * @author Trevor Parscal
25 */
26 class CSSMin {
27
28 /* Constants */
29
30 /**
31 * Maximum file size to still qualify for in-line embedding as a data-URI
32 *
33 * 24,576 is used because Internet Explorer has a 32,768 byte limit for data URIs, which when base64 encoded will
34 * result in a 1/3 increase in size.
35 */
36 const EMBED_SIZE_LIMIT = 24576;
37
38 /* Static Methods */
39
40 /**
41 * Gets a list of local file paths which are referenced in a CSS style sheet
42 *
43 * @param $source string CSS data to remap
44 * @param $path string File path where the source was read from (optional)
45 * @return array List of local file references
46 */
47 public static function getLocalFileReferences( $source, $path = null ) {
48 $pattern = '/url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)/';
49 $files = array();
50
51 if ( preg_match_all( $pattern, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER ) ) {
52 foreach ( $matches as $match ) {
53 $file = ( isset( $path ) ? rtrim( $path, '/' ) . '/' : '' ) . "{$match['file'][0]}";
54
55 // Only proceed if we can access the file
56 if ( file_exists( $file ) ) {
57 $files[] = $file;
58 }
59 }
60 }
61
62 return $files;
63 }
64
65 /**
66 * Remaps CSS URL paths and automatically embeds data URIs for URL rules preceded by an /* @embed * / comment
67 *
68 * @param $source string CSS data to remap
69 * @param $path string File path where the source was read from
70 * @return string Remapped CSS data
71 */
72 public static function remap( $source, $path, $embed = true ) {
73
74 $pattern = '/((?<embed>\s*\/\*\s*\@embed\s*\*\/)(?<rule>[^\;\}]*))?url\([\'"]?(?<file>[^\?\)\:]*)\??[^\)]*[\'"]?\)(?<extra>[^;]*)[\;]?/';
75 $offset = 0;
76
77 while ( preg_match( $pattern, $source, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
78 // Shortcuts
79 $embed = $match['embed'][0];
80 $rule = $match['rule'][0];
81 $extra = $match['extra'][0];
82 $file = "{$path}/{$match['file'][0]}";
83
84 // Only proceed if we can access the file
85 if ( file_exists( $file ) ) {
86 // Add version parameter as a time-stamp in ISO 8601 format, using Z for the timezone, meaning GMT
87 $url = "{$file}?" . gmdate( 'Y-m-d\TH:i:s\Z', round( filemtime( $file ), -2 ) );
88
89 // Detect when URLs were preceeded with embed tags, and also verify file size is below the limit
90 if ( $embed && $match['embed'][1] > 0 && filesize( $file ) < self::EMBED_SIZE_LIMIT ) {
91 // If we ever get to PHP 5.3, we should use the Fileinfo extension instead of mime_content_type
92 $type = mime_content_type( $file );
93 // Strip off any trailing = symbols (makes browsers freak out)
94 $data = base64_encode( file_get_contents( $file ) );
95 // Build 2 CSS properties; one which uses a base64 encoded data URI in place of the @embed
96 // comment to try and retain line-number integrity , and the other with a remapped an versioned
97 // URL and an Internet Explorer hack making it ignored in all browsers that support data URIs
98 $replacement = "{$rule}url(data:{$type};base64,{$data}){$extra};{$rule}url({$url}){$extra}!ie;";
99 } else {
100 // Build a CSS property with a remapped and versioned URL
101 $replacement = "{$embed}{$rule}url({$url}){$extra};";
102 }
103
104 // Perform replacement on the source
105 $source = substr_replace( $source, $replacement, $match[0][1], strlen( $match[0][0] ) );
106 // Move the offset to the end of the replacement in the source
107 $offset = $match[0][1] + strlen( $replacement );
108 continue;
109 }
110 // Move the offset to the end of the match, leaving it alone
111 $offset = $match[0][1] + strlen( $match[0][0] );
112 }
113
114 return $source;
115 }
116
117 /**
118 * Removes whitespace from CSS data
119 *
120 * @param $source string CSS data to minify
121 * @return string Minified CSS data
122 */
123 public static function minify( $css ) {
124 return trim(
125 str_replace(
126 array( '; ', ': ', ' {', '{ ', ', ', '} ', ';}' ),
127 array( ';', ':', '{', '{', ',', '}', '}' ),
128 preg_replace( array( '/\s+/', '/\/\*.*?\*\//s' ), array( ' ', '' ), $css )
129 )
130 );
131 }
132 }