]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify.php
1 <?php
2 /**
3  * Class Minify  
4  * @package Minify
5  */
6
7 /**
8  * Minify_Source
9  */
10 require_once 'Minify/Source.php';
11  
12 /**
13  * Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
14  *
15  * See README for usage instructions (for now).
16  *
17  * This library was inspired by {@link mailto:flashkot@mail.ru jscsscomp by Maxim Martynyuk}
18  * and by the article {@link http://www.hunlock.com/blogs/Supercharged_Javascript "Supercharged JavaScript" by Patrick Hunlock}.
19  *
20  * Requires PHP 5.1.0.
21  * Tested on PHP 5.1.6.
22  *
23  * @package Minify
24  * @author Ryan Grove <ryan@wonko.com>
25  * @author Stephen Clay <steve@mrclay.org>
26  * @copyright 2008 Ryan Grove, Stephen Clay. All rights reserved.
27  * @license http://opensource.org/licenses/bsd-license.php  New BSD License
28  * @link http://code.google.com/p/minify/
29  */
30 class Minify {
31     
32     const VERSION = '2.1.3';
33     const TYPE_CSS = 'text/css';
34     const TYPE_HTML = 'text/html';
35     // there is some debate over the ideal JS Content-Type, but this is the
36     // Apache default and what Yahoo! uses..
37     const TYPE_JS = 'application/x-javascript';
38     
39     /**
40      * How many hours behind are the file modification times of uploaded files?
41      * 
42      * If you upload files from Windows to a non-Windows server, Windows may report
43      * incorrect mtimes for the files. Immediately after modifying and uploading a 
44      * file, use the touch command to update the mtime on the server. If the mtime 
45      * jumps ahead by a number of hours, set this variable to that number. If the mtime 
46      * moves back, this should not be needed.
47      *
48      * @var int $uploaderHoursBehind
49      */
50     public static $uploaderHoursBehind = 0;
51     
52     /**
53      * If this string is not empty AND the serve() option 'bubbleCssImports' is
54      * NOT set, then serve() will check CSS files for @import declarations that
55      * appear too late in the combined stylesheet. If found, serve() will prepend
56      * the output with this warning.
57      *
58      * @var string $importWarning
59      */
60     public static $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n";
61     
62     /**
63      * Specify a cache object (with identical interface as Minify_Cache_File) or
64      * a path to use with Minify_Cache_File.
65      * 
66      * If not called, Minify will not use a cache and, for each 200 response, will 
67      * need to recombine files, minify and encode the output.
68      *
69      * @param mixed $cache object with identical interface as Minify_Cache_File or
70      * a directory path, or null to disable caching. (default = '')
71      * 
72      * @param bool $fileLocking (default = true) This only applies if the first
73      * parameter is a string.
74      *
75      * @return null
76      */
77     public static function setCache($cache = '', $fileLocking = true)
78     {
79         if (is_string($cache)) {
80             require_once 'Minify/Cache/File.php';
81             self::$_cache = new Minify_Cache_File($cache, $fileLocking);
82         } else {
83             self::$_cache = $cache;
84         }
85     }
86     
87     /**
88      * Serve a request for a minified file. 
89      * 
90      * Here are the available options and defaults in the base controller:
91      * 
92      * 'isPublic' : send "public" instead of "private" in Cache-Control 
93      * headers, allowing shared caches to cache the output. (default true)
94      * 
95      * 'quiet' : set to true to have serve() return an array rather than sending
96      * any headers/output (default false)
97      * 
98      * 'encodeOutput' : set to false to disable content encoding, and not send
99      * the Vary header (default true)
100      * 
101      * 'encodeMethod' : generally you should let this be determined by 
102      * HTTP_Encoder (leave null), but you can force a particular encoding
103      * to be returned, by setting this to 'gzip' or '' (no encoding)
104      * 
105      * 'encodeLevel' : level of encoding compression (0 to 9, default 9)
106      * 
107      * 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
108      * value to remove. (default 'utf-8')  
109      * 
110      * 'maxAge' : set this to the number of seconds the client should use its cache
111      * before revalidating with the server. This sets Cache-Control: max-age and the
112      * Expires header. Unlike the old 'setExpires' setting, this setting will NOT
113      * prevent conditional GETs. Note this has nothing to do with server-side caching.
114      * 
115      * 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
116      * minifier option to enable URI rewriting in CSS files (default true)
117      * 
118      * 'bubbleCssImports' : If true, all @import declarations in combined CSS
119      * files will be move to the top. Note this may alter effective CSS values
120      * due to a change in order. (default false)
121      * 
122      * 'debug' : set to true to minify all sources with the 'Lines' controller, which
123      * eases the debugging of combined files. This also prevents 304 responses.
124      * @see Minify_Lines::minify()
125      * 
126      * 'minifiers' : to override Minify's default choice of minifier function for 
127      * a particular content-type, specify your callback under the key of the 
128      * content-type:
129      * <code>
130      * // call customCssMinifier($css) for all CSS minification
131      * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
132      * 
133      * // don't minify Javascript at all
134      * $options['minifiers'][Minify::TYPE_JS] = '';
135      * </code>
136      * 
137      * 'minifierOptions' : to send options to the minifier function, specify your options
138      * under the key of the content-type. E.g. To send the CSS minifier an option: 
139      * <code>
140      * // give CSS minifier array('optionName' => 'optionValue') as 2nd argument 
141      * $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
142      * </code>
143      * 
144      * 'contentType' : (optional) this is only needed if your file extension is not 
145      * js/css/html. The given content-type will be sent regardless of source file
146      * extension, so this should not be used in a Groups config with other
147      * Javascript/CSS files.
148      * 
149      * Any controller options are documented in that controller's setupSources() method.
150      * 
151      * @param mixed instance of subclass of Minify_Controller_Base or string name of
152      * controller. E.g. 'Files'
153      * 
154      * @param array $options controller/serve options
155      * 
156      * @return mixed null, or, if the 'quiet' option is set to true, an array
157      * with keys "success" (bool), "statusCode" (int), "content" (string), and
158      * "headers" (array).
159      */
160     public static function serve($controller, $options = array())
161     {
162         if (is_string($controller)) {
163             // make $controller into object
164             $class = 'Minify_Controller_' . $controller;
165             if (! class_exists($class, false)) {
166                 require_once "Minify/Controller/" 
167                     . str_replace('_', '/', $controller) . ".php";    
168             }
169             $controller = new $class();
170         }
171         
172         // set up controller sources and mix remaining options with
173         // controller defaults
174         $options = $controller->setupSources($options);
175         $options = $controller->analyzeSources($options);
176         self::$_options = $controller->mixInDefaultOptions($options);
177         
178         // check request validity
179         if (! $controller->sources) {
180             // invalid request!
181             if (! self::$_options['quiet']) {
182                 header(self::$_options['badRequestHeader']);
183                 echo self::$_options['badRequestHeader'];
184                 return;
185             } else {
186                 list(,$statusCode) = explode(' ', self::$_options['badRequestHeader']);
187                 return array(
188                     'success' => false
189                     ,'statusCode' => (int)$statusCode
190                     ,'content' => ''
191                     ,'headers' => array()
192                 );
193             }
194         }
195         
196         self::$_controller = $controller;
197         
198         if (self::$_options['debug']) {
199             self::_setupDebug($controller->sources);
200             self::$_options['maxAge'] = 0;
201         }
202         
203         // determine encoding
204         if (self::$_options['encodeOutput']) {
205             if (self::$_options['encodeMethod'] !== null) {
206                 // controller specifically requested this
207                 $contentEncoding = self::$_options['encodeMethod'];
208             } else {
209                 // sniff request header
210                 require_once 'HTTP/Encoder.php';
211                 // depending on what the client accepts, $contentEncoding may be 
212                 // 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
213                 // getAcceptedEncoding(false, false) leaves out compress and deflate as options.
214                 list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false);
215             }
216         } else {
217             self::$_options['encodeMethod'] = ''; // identity (no encoding)
218         }
219         
220         // check client cache
221         require_once 'HTTP/ConditionalGet.php';
222         $cgOptions = array(
223             'lastModifiedTime' => self::$_options['lastModifiedTime']
224             ,'isPublic' => self::$_options['isPublic']
225             ,'encoding' => self::$_options['encodeMethod']
226         );
227         if (self::$_options['maxAge'] > 0) {
228             $cgOptions['maxAge'] = self::$_options['maxAge'];
229         }
230         $cg = new HTTP_ConditionalGet($cgOptions);
231         if ($cg->cacheIsValid) {
232             // client's cache is valid
233             if (! self::$_options['quiet']) {
234                 $cg->sendHeaders();
235                 return;
236             } else {
237                 return array(
238                     'success' => true
239                     ,'statusCode' => 304
240                     ,'content' => ''
241                     ,'headers' => $cg->getHeaders()
242                 );
243             }
244         } else {
245             // client will need output
246             $headers = $cg->getHeaders();
247             unset($cg);
248         }
249         
250         if (self::$_options['contentType'] === self::TYPE_CSS
251             && self::$_options['rewriteCssUris']) {
252             reset($controller->sources);
253             while (list($key, $source) = each($controller->sources)) {
254                 if ($source->filepath 
255                     && !isset($source->minifyOptions['currentDir'])
256                     && !isset($source->minifyOptions['prependRelativePath'])
257                 ) {
258                     $source->minifyOptions['currentDir'] = dirname($source->filepath);
259                 }
260             }
261         }
262         
263         // check server cache
264         if (null !== self::$_cache) {
265             // using cache
266             // the goal is to use only the cache methods to sniff the length and 
267             // output the content, as they do not require ever loading the file into
268             // memory.
269             $cacheId = 'minify_' . self::_getCacheId();
270             $fullCacheId = (self::$_options['encodeMethod'])
271                 ? $cacheId . '.gz'
272                 : $cacheId;
273             // check cache for valid entry
274             $cacheIsReady = self::$_cache->isValid($fullCacheId, self::$_options['lastModifiedTime']); 
275             if ($cacheIsReady) {
276                 $cacheContentLength = self::$_cache->getSize($fullCacheId);    
277             } else {
278                 // generate & cache content
279                 $content = self::_combineMinify();
280                 self::$_cache->store($cacheId, $content);
281                 if (function_exists('gzencode')) {
282                     self::$_cache->store($cacheId . '.gz', gzencode($content, self::$_options['encodeLevel']));
283                 }
284             }
285         } else {
286             // no cache
287             $cacheIsReady = false;
288             $content = self::_combineMinify();
289         }
290         if (! $cacheIsReady && self::$_options['encodeMethod']) {
291             // still need to encode
292             $content = gzencode($content, self::$_options['encodeLevel']);
293         }
294         
295         // add headers
296         $headers['Content-Length'] = $cacheIsReady
297             ? $cacheContentLength
298             : strlen($content);
299         $headers['Content-Type'] = self::$_options['contentTypeCharset']
300             ? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset']
301             : self::$_options['contentType'];
302         if (self::$_options['encodeMethod'] !== '') {
303             $headers['Content-Encoding'] = $contentEncoding;
304         }
305         if (self::$_options['encodeOutput']) {
306             $headers['Vary'] = 'Accept-Encoding';
307         }
308
309         if (! self::$_options['quiet']) {
310             // output headers & content
311             foreach ($headers as $name => $val) {
312                 header($name . ': ' . $val);
313             }
314             if ($cacheIsReady) {
315                 self::$_cache->display($fullCacheId);
316             } else {
317                 echo $content;
318             }
319         } else {
320             return array(
321                 'success' => true
322                 ,'statusCode' => 200
323                 ,'content' => $cacheIsReady
324                     ? self::$_cache->fetch($fullCacheId)
325                     : $content
326                 ,'headers' => $headers
327             );
328         }
329     }
330     
331     /**
332      * Return combined minified content for a set of sources
333      *
334      * No internal caching will be used and the content will not be HTTP encoded.
335      * 
336      * @param array $sources array of filepaths and/or Minify_Source objects
337      * 
338      * @param array $options (optional) array of options for serve. By default
339      * these are already set: quiet = true, encodeMethod = '', lastModifiedTime = 0.
340      * 
341      * @return string
342      */
343     public static function combine($sources, $options = array())
344     {
345         $cache = self::$_cache;
346         self::$_cache = null;
347         $options = array_merge(array(
348             'files' => (array)$sources
349             ,'quiet' => true
350             ,'encodeMethod' => ''
351             ,'lastModifiedTime' => 0
352         ), $options);
353         $out = self::serve('Files', $options);
354         self::$_cache = $cache;
355         return $out['content'];
356     }
357     
358     /**
359      * On IIS, create $_SERVER['DOCUMENT_ROOT']
360      * 
361      * @param bool $unsetPathInfo (default false) if true, $_SERVER['PATH_INFO']
362      * will be unset (it is inconsistent with Apache's setting)
363      * 
364      * @return null
365      */
366     public static function setDocRoot($unsetPathInfo = false)
367     {
368         if (isset($_SERVER['SERVER_SOFTWARE'])
369             && 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/')
370         ) {
371             $_SERVER['DOCUMENT_ROOT'] = rtrim(substr(
372                 $_SERVER['PATH_TRANSLATED']
373                 ,0
374                 ,strlen($_SERVER['PATH_TRANSLATED']) - strlen($_SERVER['SCRIPT_NAME'])
375             ), '\\');
376             if ($unsetPathInfo) {
377                 unset($_SERVER['PATH_INFO']);
378             }
379             require_once 'Minify/Logger.php';
380             Minify_Logger::log("setDocRoot() set DOCUMENT_ROOT to \"{$_SERVER['DOCUMENT_ROOT']}\"");
381         }
382     }
383     
384     /**
385      * @var mixed Minify_Cache_* object or null (i.e. no server cache is used)
386      */
387     private static $_cache = null;
388     
389     /**
390      * @var Minify_Controller active controller for current request
391      */
392     protected static $_controller = null;
393     
394     /**
395      * @var array options for current request
396      */
397     protected static $_options = null;
398     
399     /**
400      * Set up sources to use Minify_Lines
401      *
402      * @param array $sources Minify_Source instances
403      *
404      * @return null
405      */
406     protected static function _setupDebug($sources)
407     {
408         foreach ($sources as $source) {
409             $source->minifier = array('Minify_Lines', 'minify');
410             $id = $source->getId();
411             $source->minifyOptions = array(
412                 'id' => (is_file($id) ? basename($id) : $id)
413             );
414         }
415     }
416     
417     /**
418      * Combines sources and minifies the result.
419      *
420      * @return string
421      */
422     protected static function _combineMinify()
423     {
424         $type = self::$_options['contentType']; // ease readability
425         
426         // when combining scripts, make sure all statements separated and
427         // trailing single line comment is terminated
428         $implodeSeparator = ($type === self::TYPE_JS)
429             ? "\n;"
430             : '';
431         // allow the user to pass a particular array of options to each
432         // minifier (designated by type). source objects may still override
433         // these
434         $defaultOptions = isset(self::$_options['minifierOptions'][$type])
435             ? self::$_options['minifierOptions'][$type]
436             : array();
437         // if minifier not set, default is no minification. source objects
438         // may still override this
439         $defaultMinifier = isset(self::$_options['minifiers'][$type])
440             ? self::$_options['minifiers'][$type]
441             : false;
442        
443         if (Minify_Source::haveNoMinifyPrefs(self::$_controller->sources)) {
444             // all source have same options/minifier, better performance
445             // to combine, then minify once
446             foreach (self::$_controller->sources as $source) {
447                 $pieces[] = $source->getContent();
448             }
449             $content = implode($implodeSeparator, $pieces);
450             if ($defaultMinifier) {
451                 self::$_controller->loadMinifier($defaultMinifier);
452                 $content = call_user_func($defaultMinifier, $content, $defaultOptions);    
453             }
454         } else {
455             // minify each source with its own options and minifier, then combine
456             foreach (self::$_controller->sources as $source) {
457                 // allow the source to override our minifier and options
458                 $minifier = (null !== $source->minifier)
459                     ? $source->minifier
460                     : $defaultMinifier;
461                 $options = (null !== $source->minifyOptions)
462                     ? array_merge($defaultOptions, $source->minifyOptions)
463                     : $defaultOptions;
464                 if ($minifier) {
465                     self::$_controller->loadMinifier($minifier);
466                     // get source content and minify it
467                     $pieces[] = call_user_func($minifier, $source->getContent(), $options);     
468                 } else {
469                     $pieces[] = $source->getContent();     
470                 }
471             }
472             $content = implode($implodeSeparator, $pieces);
473         }
474         
475         if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
476             $content = self::_handleCssImports($content);
477         }
478         
479         // do any post-processing (esp. for editing build URIs)
480         if (self::$_options['postprocessorRequire']) {
481             require_once self::$_options['postprocessorRequire'];
482         }
483         if (self::$_options['postprocessor']) {
484             $content = call_user_func(self::$_options['postprocessor'], $content, $type);
485         }
486         return $content;
487     }
488     
489     /**
490      * Make a unique cache id for for this request.
491      * 
492      * Any settings that could affect output are taken into consideration  
493      *
494      * @return string
495      */
496     protected static function _getCacheId()
497     {
498         return md5(serialize(array(
499             Minify_Source::getDigest(self::$_controller->sources)
500             ,self::$_options['minifiers'] 
501             ,self::$_options['minifierOptions']
502             ,self::$_options['postprocessor']
503             ,self::$_options['bubbleCssImports']
504         )));
505     }
506     
507     /**
508      * Bubble CSS @imports to the top or prepend a warning if an
509      * @import is detected not at the top.
510      */
511     protected static function _handleCssImports($css)
512     {
513         if (self::$_options['bubbleCssImports']) {
514             // bubble CSS imports
515             preg_match_all('/@import.*?;/', $css, $imports);\r
516             $css = implode('', $imports[0]) . preg_replace('/@import.*?;/', '', $css);
517         } else if ('' !== self::$importWarning) {
518             // remove comments so we don't mistake { in a comment as a block
519             $noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
520             $lastImportPos = strrpos($noCommentCss, '@import');
521             $firstBlockPos = strpos($noCommentCss, '{');
522             if (false !== $lastImportPos
523                 && false !== $firstBlockPos
524                 && $firstBlockPos < $lastImportPos
525             ) {
526                 // { appears before @import : prepend warning
527                 $css = self::$importWarning . $css;
528             }
529         }
530         return $css;
531     }
532 }