Fixed bug #15383: [Unit tests] Add tests for t3lib_div::validEmail
[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 // we remove BACK_PATH from $filename, so make it relative to TYPO3_mainDir
155 $filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
156 // if compression is enabled
157 if ($fileOptions['compress']) {
158 $filesAfterCompression[$this->compressCssFile($filename)] = $fileOptions;
159 } else {
160 $filesAfterCompression[$filename] = $fileOptions;
161 }
162 }
163 return $filesAfterCompression;
164 }
165
166 /**
167 * Compresses a CSS file
168 *
169 * Options:
170 * baseDirectories If set, only include files below one of the base directories
171 *
172 * removes comments and whitespaces
173 * Adopted from http://drupal.org/files/issues/minify_css.php__1.txt
174 *
175 * @param string $filename Source filename, relative to requested page
176 * @return string Compressed filename, relative to requested page
177 */
178 public function compressCssFile($filename) {
179 // generate the unique name of the file
180 $filenameAbsolute = t3lib_div::resolveBackPath(PATH_typo3 . substr($filename, strlen($GLOBALS['BACK_PATH'])));
181 $unique = $filenameAbsolute . filemtime($filenameAbsolute) . filesize($filenameAbsolute);
182
183 $pathinfo = pathinfo($filename);
184 $targetFile = $this->targetDirectory . $pathinfo['filename'] . '-' . md5($unique) . '.css';
185 // only create it, if it doesn't exist, yet
186 if (!file_exists(PATH_site . $targetFile) || ($this->createGzipped && !file_exists(PATH_site . $targetFile . '.gzip'))) {
187 $contents = t3lib_div::getUrl($filenameAbsolute);
188 // Perform some safe CSS optimizations.
189 $contents = str_replace("\r", '', $contents); // Strip any and all carriage returns.
190 // Match and process strings, comments and everything else, one chunk at a time.
191 // To understand this regex, read: "Mastering Regular Expressions 3rd Edition" chapter 6.
192 $contents = preg_replace_callback('%
193 # One-regex-to-rule-them-all! - version: 20100220_0100
194 # Group 1: Match a double quoted string.
195 ("[^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+") | # or...
196 # Group 2: Match a single quoted string.
197 (\'[^\'\\\\]*+(?:\\\\.[^\'\\\\]*+)*+\') | # or...
198 # Group 3: Match a regular non-MacIE5-hack comment.
199 (/\*[^\\\\*]*+\*++(?:[^\\\\*/][^\\\\*]*+\*++)*+/) | # or...
200 # Group 4: Match a MacIE5-type1 comment.
201 (/\*(?:[^*\\\\]*+\**+(?!/))*+\\\\[^*]*+\*++(?:[^*/][^*]*+\*++)*+/(?<!\\\\\*/)) | # or...
202 # Group 5: Match a MacIE5-type2 comment.
203 (/\*[^*]*\*+(?:[^/*][^*]*\*+)*/(?<=\\\\\*/)) # folllowed by...
204 # Group 6: Match everything up to final closing regular comment
205 ([^/]*+(?:(?!\*)/[^/]*+)*?)
206 # Group 7: Match final closing regular comment
207 (/\*[^/]++(?:(?<!\*)/(?!\*)[^/]*+)*+/(?<=(?<!\\\\)\*/)) | # or...
208 # Group 8: Match regular non-string, non-comment text.
209 ([^"\'/]*+(?:(?!/\*)/[^"\'/]*+)*+)
210 %Ssx', array('self','compressCssPregCallback'), $contents); // Do it!
211 $contents = preg_replace('/^\s++/', '', $contents); // Strip leading whitespace.
212 $contents = preg_replace('/[ \t]*+\n\s*+/S', "\n", $contents); // Consolidate multi-lines space.
213 $contents = preg_replace('/(?<!\s)\s*+$/S', "\n", $contents); // Ensure file ends in newline.
214 // we have to fix relative paths, if we aren't working on a file in our target directory
215 if (!is_int(strpos($filename, $this->targetDirectory))) {
216 $filenameRelativeToMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
217 $contents = $this->cssFixRelativeUrlPaths($contents, dirname($filenameRelativeToMainDir) . '/');
218 }
219 $this->writeFileAndCompressed($targetFile, $contents);
220 }
221
222 return $GLOBALS['BACK_PATH'] . '../' . $this->returnFileReference($targetFile);
223 }
224
225 /**
226 * Callback function for preg_replace
227 *
228 * @see compressCssFile
229 * @param array $matches
230 * @return string the compressed string
231 */
232 public static function compressCssPregCallback($matches) {
233 if ($matches[1]) { // Group 1: Double quoted string.
234 return $matches[1]; // Return the string unmodified.
235 } elseif ($matches[2]) { // Group 2: Single quoted string.
236 return $matches[2]; // Return the string unmodified.
237 } elseif ($matches[3]) { // Group 3: Regular non-MacIE5-hack comment.
238 return "\n"; // Return single space.
239 } elseif ($matches[4]) { // Group 4: MacIE5-hack-type-1 comment.
240 return "\n/*\\T1*/\n"; // Return minimal MacIE5-hack-type-1 comment.
241 }
242 elseif ($matches[5]) { // Group 5,6,7: MacIE5-hack-type-2 comment
243 $matches[6] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[6]); // Clean pre-punctuation.
244 $matches[6] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[6]); // Clean post-punctuation.
245 $matches[6] = preg_replace('/;?\}/S', "}\n", $matches[6]); // Add a touch of formatting.
246 return "\n/*T2\\*/" . $matches[6] . "\n/*T2E*/\n"; // Minify and reassemble composite type2 comment.
247 } elseif (isset($matches[8])) { // Group 8: Non-string, non-comment. Safe to clean whitespace here.
248 $matches[8] = preg_replace('/^\s++/', '', $matches[8]); // Strip all leading whitespace.
249 $matches[8] = preg_replace('/\s++$/', '', $matches[8]); // Strip all trailing whitespace.
250 $matches[8] = preg_replace('/\s{2,}+/', ' ', $matches[8]); // Consolidate multiple whitespace.
251 $matches[8] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[8]); // Clean pre-punctuation.
252 $matches[8] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[8]); // Clean post-punctuation.
253 $matches[8] = preg_replace('/;?\}/S', "}\n", $matches[8]); // Add a touch of formatting.
254 return $matches[8];
255 }
256 return $matches[0] . "\n/* ERROR! Unexpected _proccess_css_minify() parameter */\n"; // never get here
257 }
258
259 /**
260 * Compress multiple javascript files
261 *
262 * @param array $jsFiles The files to compress (array key = filename), relative to requested page
263 * @return array The js files after compression (array key = new filename), relative to requested page
264 */
265 public function compressJsFiles(array $jsFiles) {
266 $filesAfterCompression = array();
267 foreach ($jsFiles as $filename => $fileOptions) {
268 // we remove BACK_PATH from $filename, so make it relative to TYPO3_mainDir
269 $filenameFromMainDir = substr($filename, strlen($GLOBALS['BACK_PATH']));
270 // if compression is enabled
271 if ($fileOptions['compress']) {
272 $filesAfterCompression[$this->compressJsFile($filename)] = $fileOptions;
273 } else {
274 $filesAfterCompression[$filename] = $fileOptions;
275 }
276 }
277 return $filesAfterCompression;
278 }
279
280 /**
281 * Compresses a javascript file
282 *
283 * Options:
284 * baseDirectories If set, only include files below one of the base directories
285 *
286 * @param string $filename Source filename, relative to requested page
287 * @return string Filename of the compressed file, relative to requested page
288 */
289 public function compressJsFile($filename) {
290 // generate the unique name of the file
291 $filenameAbsolute = t3lib_div::resolveBackPath(PATH_typo3 . substr($filename, strlen($GLOBALS['BACK_PATH'])));
292 $unique = $filenameAbsolute . filemtime($filenameAbsolute) . filesize($filenameAbsolute);
293
294 $pathinfo = pathinfo($filename);
295 $targetFile = $this->targetDirectory . $pathinfo['filename'] . '-' . md5($unique) . '.js';
296 // only create it, if it doesn't exist, yet
297 if (!file_exists(PATH_site . $targetFile) || ($this->createGzipped && !file_exists(PATH_site . $targetFile . '.gzip'))) {
298 $contents = t3lib_div::getUrl($filenameAbsolute);
299 $this->writeFileAndCompressed($targetFile, $contents);
300 }
301 return $GLOBALS['BACK_PATH'] . '../' . $this->returnFileReference($targetFile);
302 }
303
304 /**
305 * Decides whether a CSS file comes from one of the baseDirectories
306 *
307 * @param string $filename Filename
308 * @return boolean File belongs to a skin or not
309 */
310 protected function checkBaseDirectory($filename, array $baseDirectories) {
311 foreach ($baseDirectories as $baseDirectory) {
312 // check, if $filename starts with $skinStylesheetDirectory
313 if (t3lib_div::isFirstPartOfStr($filename, $baseDirectory)) {
314 return TRUE;
315 }
316 }
317 return FALSE;
318 }
319
320 /**
321 * Fixes the relative paths inside of url() references in CSS files
322 *
323 * @param string $contents Data to process
324 * @param string $oldDir Directory of the originial file, relative to TYPO3_mainDir
325 * @return string Processed data
326 */
327 protected function cssFixRelativeUrlPaths($contents, $oldDir) {
328 $matches = array();
329
330 preg_match_all('/url(\(\s*["\']?([^"\']+)["\']?\s*\))/iU', $contents, $matches);
331 foreach ($matches[2] as $matchCount => $match) {
332 // remove '," or white-spaces around
333 $match = preg_replace('/[\"\'\s]/', '', $match);
334
335 // we must not rewrite paths containing ":", e.g. data URIs (see RFC 2397)
336 if (strpos($match, ':') === FALSE) {
337 $newPath = t3lib_div::resolveBackPath('../../' . TYPO3_mainDir . $oldDir . $match);
338 $contents = str_replace($matches[1][$matchCount], '(\'' . $newPath . '\')', $contents);
339 }
340 }
341 return $contents;
342 }
343
344 /**
345 * Writes $contents into file $filename together with a gzipped version into $filename.gz
346 *
347 * @param string $filename Target filename
348 * @param strings $contents File contents
349 * @return void
350 */
351 protected function writeFileAndCompressed($filename, $contents) {
352 // write uncompressed file
353 t3lib_div::writeFile(PATH_site . $filename, $contents);
354
355 if ($this->createGzipped) {
356 // create compressed version
357 t3lib_div::writeFile(PATH_site . $filename . '.gzip', gzencode($contents, $this->gzipCompressionLevel));
358 }
359 }
360
361 /**
362 * Decides whether a client can deal with gzipped content or not and returns the according file name,
363 * based on HTTP_ACCEPT_ENCODING
364 *
365 * @param string $filename File name
366 * @return string $filename suffixed with '.gzip' or not - dependent on HTTP_ACCEPT_ENCODING
367 */
368 protected function returnFileReference($filename) {
369 // if the client accepts gzip and we can create gzipped files, we give him compressed versions
370 if ($this->createGzipped && strpos(t3lib_div::getIndpEnv('HTTP_ACCEPT_ENCODING'), 'gzip') !== FALSE) {
371 return $filename . '.gzip';
372 } else {
373 return $filename;
374 }
375 }
376 }
377 ?>