Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_positionmap
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_compressor.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Steffen Gebert (steffen@steffen-gebert.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Compressor
30 * This merges and compresses CSS and JavaScript files of the TYPO3 Backend.
31 *
32 * @author Steffen Gebert <steffen@steffen-gebert.de>
33 * @package TYPO3
34 * @subpackage t3lib
35 * $Id$
36 */
37 class t3lib_Compressor {
38
39 protected $targetDirectory = 'typo3temp/compressor/';
40
41 // gzipped versions are only created if $TYPO3_CONF_VARS[TYPO3_MODE]['compressionLevel'] is set
42 protected $createGzipped = FALSE;
43 // default compression level is -1
44 protected $gzipCompressionLevel = -1;
45
46 /**
47 * Constructor
48 */
49 public function __construct() {
50
51 // we check for existance of our targetDirectory
52 if (!is_dir(PATH_site . $this->targetDirectory)) {
53 t3lib_div::mkdir(PATH_site . $this->targetDirectory);
54 }
55
56 // decide whether we should create gzipped versions or not
57 $compressionLevel = $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['compressionLevel'];
58 // we need zlib for gzencode()
59 if (extension_loaded('zlib') && $compressionLevel) {
60 $this->createGzipped = TRUE;
61 // $compressionLevel can also be TRUE
62 if (t3lib_div::testInt($compressionLevel)) {
63 $this->gzipCompressionLevel = intval($compressionLevel);
64 }
65 }
66 }
67
68 /**
69 * Concatenates the cssFiles
70 *
71 * Options:
72 * baseDirectories If set, only include files below one of the base directories
73 *
74 * @param array $cssFiles CSS files to process
75 * @param array $options Additional options
76 * @return array CSS files
77 */
78 public function concatenateCssFiles(array $cssFiles, $options = array()) {
79
80 $filesToInclude = array();
81 foreach ($cssFiles as $filename => $fileOptions) {
82 // we remove BACK_PATH from $filename, so make it relative to TYPO3_mainDir
83 $filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
84 // if $options['baseDirectories'] set, we only include files below these directories
85 if ((!isset($options['baseDirectories'])
86 || $this->checkBaseDirectory($filenameFromMainDir, array_merge($options['baseDirectories'], array($this->targetDirectory))))
87 && ($fileOptions['media'] === 'all')
88 ) {
89
90 $filesToInclude[] = $filenameFromMainDir;
91 // remove the file from the incoming file array
92 unset($cssFiles[$filename]);
93 }
94 }
95
96 if (count($filesToInclude)) {
97 $targetFile = $this->createMergedCssFile($filesToInclude);
98 $concatenatedOptions = array(
99 'rel' => 'stylesheet',
100 'media' => 'all',
101 'compress' => TRUE,
102 );
103 $targetFileRelative = $GLOBALS['BACK_PATH'] . '../' . $targetFile;
104 // place the merged stylesheet on top of the stylesheets
105 $cssFiles = array_merge(array($targetFileRelative => $concatenatedOptions), $cssFiles);
106 }
107 return $cssFiles;
108 }
109
110 /**
111 * Creates a merged CSS file
112 *
113 * @param array $filesToInclude Files which should be merged, paths relative to TYPO3_mainDir
114 * @return mixed Filename of the merged file
115 */
116 protected function createMergedCssFile(array $filesToInclude) {
117 // we add up the filenames, filemtimes and filsizes to later build a checksum over
118 // it and include it in the temporary file name
119 $unique = '';
120
121 foreach ($filesToInclude as $filename) {
122 $filepath = t3lib_div::resolveBackPath(PATH_typo3 . $filename);
123 $unique .= $filename . filemtime($filepath) . filesize($filepath);
124 }
125 $targetFile = $this->targetDirectory . 'merged-' . md5($unique) . '.css';
126
127 // if the file doesn't already exist, we create it
128 if (!file_exists(PATH_site . $targetFile)) {
129 $concatenated = '';
130 // concatenate all the files together
131 foreach ($filesToInclude as $filename) {
132 $contents = t3lib_div::getUrl(t3lib_div::resolveBackPath(PATH_typo3 . $filename));
133 // only fix paths if files aren't already in typo3temp (already processed)
134 if (!t3lib_div::isFirstPartOfStr($filename, $this->targetDirectory)) {
135 $concatenated .= $this->cssFixRelativeUrlPaths($contents, dirname($filename) . '/');
136 } else {
137 $concatenated .= $contents;
138 }
139 }
140 t3lib_div::writeFile(PATH_site . $targetFile, $concatenated);
141 }
142 return $targetFile;
143 }
144
145 /**
146 * Compress multiple css files
147 *
148 * @param array $cssFiles The files to compress (array key = filename), relative to requested page
149 * @return array The CSS files after compression (array key = new filename), relative to requested page
150 */
151 public function compressCssFiles(array $cssFiles) {
152 $filesAfterCompression = array();
153 foreach ($cssFiles as $filename => $fileOptions) {
154 // if compression is enabled
155 if ($fileOptions['compress']) {
156 $filesAfterCompression[$this->compressCssFile($filename)] = $fileOptions;
157 } else {
158 $filesAfterCompression[$filename] = $fileOptions;
159 }
160 }
161 return $filesAfterCompression;
162 }
163
164 /**
165 * Compresses a CSS file
166 *
167 * Options:
168 * baseDirectories If set, only include files below one of the base directories
169 *
170 * removes comments and whitespaces
171 * Adopted from http://drupal.org/files/issues/minify_css.php__1.txt
172 *
173 * @param string $filename Source filename, relative to requested page
174 * @return string Compressed filename, relative to requested page
175 */
176 public function compressCssFile($filename) {
177 // generate the unique name of the file
178 $filenameAbsolute = t3lib_div::resolveBackPath(PATH_typo3 . substr($filename, strlen($GLOBALS['BACK_PATH'])));
179 $unique = $filenameAbsolute . filemtime($filenameAbsolute) . filesize($filenameAbsolute);
180
181 $pathinfo = pathinfo($filename);
182 $targetFile = $this->targetDirectory . $pathinfo['filename'] . '-' . md5($unique) . '.css';
183 // only create it, if it doesn't exist, yet
184 if (!file_exists(PATH_site . $targetFile) || ($this->createGzipped && !file_exists(PATH_site . $targetFile . '.gzip'))) {
185 $contents = t3lib_div::getUrl($filenameAbsolute);
186 // Perform some safe CSS optimizations.
187 $contents = str_replace("\r", '', $contents); // Strip any and all carriage returns.
188 // Match and process strings, comments and everything else, one chunk at a time.
189 // To understand this regex, read: "Mastering Regular Expressions 3rd Edition" chapter 6.
190 $contents = preg_replace_callback('%
191 # One-regex-to-rule-them-all! - version: 20100220_0100
192 # Group 1: Match a double quoted string.
193 ("[^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+") | # or...
194 # Group 2: Match a single quoted string.
195 (\'[^\'\\\\]*+(?:\\\\.[^\'\\\\]*+)*+\') | # or...
196 # Group 3: Match a regular non-MacIE5-hack comment.
197 (/\*[^\\\\*]*+\*++(?:[^\\\\*/][^\\\\*]*+\*++)*+/) | # or...
198 # Group 4: Match a MacIE5-type1 comment.
199 (/\*(?:[^*\\\\]*+\**+(?!/))*+\\\\[^*]*+\*++(?:[^*/][^*]*+\*++)*+/(?<!\\\\\*/)) | # or...
200 # Group 5: Match a MacIE5-type2 comment.
201 (/\*[^*]*\*+(?:[^/*][^*]*\*+)*/(?<=\\\\\*/)) # folllowed by...
202 # Group 6: Match everything up to final closing regular comment
203 ([^/]*+(?:(?!\*)/[^/]*+)*?)
204 # Group 7: Match final closing regular comment
205 (/\*[^/]++(?:(?<!\*)/(?!\*)[^/]*+)*+/(?<=(?<!\\\\)\*/)) | # or...
206 # Group 8: Match regular non-string, non-comment text.
207 ([^"\'/]*+(?:(?!/\*)/[^"\'/]*+)*+)
208 %Ssx', array('self', 'compressCssPregCallback'), $contents); // Do it!
209 $contents = preg_replace('/^\s++/', '', $contents); // Strip leading whitespace.
210 $contents = preg_replace('/[ \t]*+\n\s*+/S', "\n", $contents); // Consolidate multi-lines space.
211 $contents = preg_replace('/(?<!\s)\s*+$/S', "\n", $contents); // Ensure file ends in newline.
212 // we have to fix relative paths, if we aren't working on a file in our target directory
213 if (!is_int(strpos($filename, $this->targetDirectory))) {
214 $filenameRelativeToMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
215 $contents = $this->cssFixRelativeUrlPaths($contents, dirname($filenameRelativeToMainDir) . '/');
216 }
217 $this->writeFileAndCompressed($targetFile, $contents);
218 }
219
220 return $GLOBALS['BACK_PATH'] . '../' . $this->returnFileReference($targetFile);
221 }
222
223 /**
224 * Callback function for preg_replace
225 *
226 * @see compressCssFile
227 * @param array $matches
228 * @return string the compressed string
229 */
230 public static function compressCssPregCallback($matches) {
231 if ($matches[1]) { // Group 1: Double quoted string.
232 return $matches[1]; // Return the string unmodified.
233 } elseif ($matches[2]) { // Group 2: Single quoted string.
234 return $matches[2]; // Return the string unmodified.
235 } elseif ($matches[3]) { // Group 3: Regular non-MacIE5-hack comment.
236 return "\n"; // Return single space.
237 } elseif ($matches[4]) { // Group 4: MacIE5-hack-type-1 comment.
238 return "\n/*\\T1*/\n"; // Return minimal MacIE5-hack-type-1 comment.
239 }
240 elseif ($matches[5]) { // Group 5,6,7: MacIE5-hack-type-2 comment
241 $matches[6] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[6]); // Clean pre-punctuation.
242 $matches[6] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[6]); // Clean post-punctuation.
243 $matches[6] = preg_replace('/;?\}/S', "}\n", $matches[6]); // Add a touch of formatting.
244 return "\n/*T2\\*/" . $matches[6] . "\n/*T2E*/\n"; // Minify and reassemble composite type2 comment.
245 } elseif (isset($matches[8])) { // Group 8: Non-string, non-comment. Safe to clean whitespace here.
246 $matches[8] = preg_replace('/^\s++/', '', $matches[8]); // Strip all leading whitespace.
247 $matches[8] = preg_replace('/\s++$/', '', $matches[8]); // Strip all trailing whitespace.
248 $matches[8] = preg_replace('/\s{2,}+/', ' ', $matches[8]); // Consolidate multiple whitespace.
249 $matches[8] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[8]); // Clean pre-punctuation.
250 $matches[8] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[8]); // Clean post-punctuation.
251 $matches[8] = preg_replace('/;?\}/S', "}\n", $matches[8]); // Add a touch of formatting.
252 return $matches[8];
253 }
254 return $matches[0] . "\n/* ERROR! Unexpected _proccess_css_minify() parameter */\n"; // never get here
255 }
256
257 /**
258 * Compress multiple javascript files
259 *
260 * @param array $jsFiles The files to compress (array key = filename), relative to requested page
261 * @return array The js files after compression (array key = new filename), relative to requested page
262 */
263 public function compressJsFiles(array $jsFiles) {
264 $filesAfterCompression = array();
265 foreach ($jsFiles as $filename => $fileOptions) {
266 // we remove BACK_PATH from $filename, so make it relative to TYPO3_mainDir
267 $filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
268 // if compression is enabled
269 if ($fileOptions['compress']) {
270 $filesAfterCompression[$this->compressJsFile($filename)] = $fileOptions;
271 } else {
272 $filesAfterCompression[$filename] = $fileOptions;
273 }
274 }
275 return $filesAfterCompression;
276 }
277
278 /**
279 * Compresses a javascript file
280 *
281 * Options:
282 * baseDirectories If set, only include files below one of the base directories
283 *
284 * @param string $filename Source filename, relative to requested page
285 * @return string Filename of the compressed file, relative to requested page
286 */
287 public function compressJsFile($filename) {
288 // generate the unique name of the file
289 $filenameAbsolute = t3lib_div::resolveBackPath(PATH_typo3 . substr($filename, strlen($GLOBALS['BACK_PATH'])));
290 $unique = $filenameAbsolute . filemtime($filenameAbsolute) . filesize($filenameAbsolute);
291
292 $pathinfo = pathinfo($filename);
293 $targetFile = $this->targetDirectory . $pathinfo['filename'] . '-' . md5($unique) . '.js';
294 // only create it, if it doesn't exist, yet
295 if (!file_exists(PATH_site . $targetFile) || ($this->createGzipped && !file_exists(PATH_site . $targetFile . '.gzip'))) {
296 $contents = t3lib_div::getUrl($filenameAbsolute);
297 $this->writeFileAndCompressed($targetFile, $contents);
298 }
299 return $GLOBALS['BACK_PATH'] . '../' . $this->returnFileReference($targetFile);
300 }
301
302 /**
303 * Decides whether a CSS file comes from one of the baseDirectories
304 *
305 * @param string $filename Filename
306 * @return boolean File belongs to a skin or not
307 */
308 protected function checkBaseDirectory($filename, array $baseDirectories) {
309 foreach ($baseDirectories as $baseDirectory) {
310 // check, if $filename starts with $skinStylesheetDirectory
311 if (t3lib_div::isFirstPartOfStr($filename, $baseDirectory)) {
312 return TRUE;
313 }
314 }
315 return FALSE;
316 }
317
318 /**
319 * Fixes the relative paths inside of url() references in CSS files
320 *
321 * @param string $contents Data to process
322 * @param string $oldDir Directory of the originial file, relative to TYPO3_mainDir
323 * @return string Processed data
324 */
325 protected function cssFixRelativeUrlPaths($contents, $oldDir) {
326 $matches = array();
327
328 preg_match_all('/url(\(\s*["\']?([^"\']+)["\']?\s*\))/iU', $contents, $matches);
329 foreach ($matches[2] as $matchCount => $match) {
330 // remove '," or white-spaces around
331 $match = preg_replace('/[\"\'\s]/', '', $match);
332
333 // we must not rewrite paths containing ":", e.g. data URIs (see RFC 2397)
334 if (strpos($match, ':') === FALSE) {
335 $newPath = t3lib_div::resolveBackPath('../../' . TYPO3_mainDir . $oldDir . $match);
336 $contents = str_replace($matches[1][$matchCount], '(\'' . $newPath . '\')', $contents);
337 }
338 }
339 return $contents;
340 }
341
342 /**
343 * Writes $contents into file $filename together with a gzipped version into $filename.gz
344 *
345 * @param string $filename Target filename
346 * @param strings $contents File contents
347 * @return void
348 */
349 protected function writeFileAndCompressed($filename, $contents) {
350 // write uncompressed file
351 t3lib_div::writeFile(PATH_site . $filename, $contents);
352
353 if ($this->createGzipped) {
354 // create compressed version
355 t3lib_div::writeFile(PATH_site . $filename . '.gzip', gzencode($contents, $this->gzipCompressionLevel));
356 }
357 }
358
359 /**
360 * Decides whether a client can deal with gzipped content or not and returns the according file name,
361 * based on HTTP_ACCEPT_ENCODING
362 *
363 * @param string $filename File name
364 * @return string $filename suffixed with '.gzip' or not - dependent on HTTP_ACCEPT_ENCODING
365 */
366 protected function returnFileReference($filename) {
367 // if the client accepts gzip and we can create gzipped files, we give him compressed versions
368 if ($this->createGzipped && strpos(t3lib_div::getIndpEnv('HTTP_ACCEPT_ENCODING'), 'gzip') !== FALSE) {
369 return $filename . '.gzip';
370 } else {
371 return $filename;
372 }
373 }
374 }
375
376 ?>