diff --git a/HISTORY.txt b/HISTORY.txt index 0089607f..3ff46c78 100644 --- a/HISTORY.txt +++ b/HISTORY.txt @@ -8,6 +8,9 @@ Version 2.1.5 * more efficient JS minification when using CC/YUIC * Closure Compiler uses cURL when allow_url_fopen is off * Missing file notices when using groups + * Changed README.txt to README.md, styling it to markdown + * Removed local file requires + * Added composer.json for packaging management with composer Version 2.1.4 * Option to minify JS with Closure Compiler API w/ JSMin failover diff --git a/README.md b/README.md new file mode 100644 index 00000000..29b2bec0 --- /dev/null +++ b/README.md @@ -0,0 +1,125 @@ +MINIFY +==== + +*Minify is an HTTP content server. It compresses sources of content +(usually files), combines the result and serves it with appropriate +HTTP headers. These headers can allow clients to perform conditional +GETs (serving content only when clients do not have a valid cache) +and tell clients to cache the file for a period of time.* + +More info: [http://code.google.com/p/minify/][1] + + +---------- + + +Wordpress User +------------- + +These WP plugins integrate Minify into WordPress's style and script hooks to +get you set up faster. + + - [http://wordpress.org/extend/plugins/bwp-minify/][2] + - [http://wordpress.org/extend/plugins/w3-total-cache/][3] + + +---------- + + +Installation +------------ + +**Using composer:** + +NOTE: Check [Composer Starting Guide][4] for a more detailed explanation on how using composer to download and manage this package. + + 1. Download composer.phar from [here][5] or downloading through the console/command line by using this command in your project root folder: `curl -s https://getcomposer.org/installer | php` + 2. Create a ***composer.json*** file in your project root directory + 3. Although Minify is accessible in the packagist repository, this fork is not, so you have to put these lines in your ***composer.json*** file: + + + { + "repositories": [ + { + "name": "minify/minify", + "type": "git", + "url": "https://github.com/tivie/minify.git" + } + ], + "require": { + "minify/minify": "2.1.5" + } + } + + 4. Then run (again in your project root directory) `php composer.phar install` + + +**Zipped Download:** + +You can also download **Minify** zipped directly from github, but this package is prepared for Composer Autoload. + + +---------- + + +Configuration & Usage +--------------------- + +See the MIN.txt file and [http://code.google.com/p/minify/wiki/UserGuide][6] + +Minify also comes with a URI Builder application that can help you write URLs +for use with Minify or configure groups of files. See here for details: + +[http://code.google.com/p/minify/wiki/BuilderApp][7] + +The cookbook also provides some more advanced options for magnification: + +[http://code.google.com/p/minify/wiki/CookBook][8] + + +---------- + + +Upgrading +--------- + +See UPGRADING.txt for instructions. + + + +Unit Testing +------------ + +1. Place the /min_unit_tests/ directory as a child of your DOCUMENT_ROOT +directory: i.e. you will have: */home/example/www/min\_unit\_tests* + +2. To run unit tests, access: *http://example.org/min\_unit\_tests/test_all.php* + +(If you wish, the other test_*.php files can be run to test individual +components with more verbose output.) + +3. Remove */min\_unit\_tests/* from your DOCUMENT_ROOT when you are done. + + +---------- + + +File Encodings +-------------- + +Minify *should* work fine with files encoded in UTF-8 or other 8-bit +encodings like ISO 8859/Windows-1252. By default Minify appends +";charset=utf-8" to the Content-Type headers it sends. + +Leading UTF-8 BOMs are stripped from all sources to prevent +duplication in output files, and files are converted to Unix newlines. + + + [1]: http://code.google.com/p/minify/ + [2]: http://wordpress.org/extend/plugins/bwp-minify/ + [3]: http://wordpress.org/extend/plugins/w3-total-cache/ + [4]: http://getcomposer.org/doc/00-intro.md + [5]: http://getcomposer.org/download/ + [6]: http://code.google.com/p/minify/wiki/UserGuide + [7]: http://code.google.com/p/minify/wiki/BuilderApp + [8]: http://code.google.com/p/minify/wiki/CookBook \ No newline at end of file diff --git a/README.txt b/README.txt deleted file mode 100644 index 54b0690f..00000000 --- a/README.txt +++ /dev/null @@ -1,68 +0,0 @@ -WELCOME TO MINIFY! - -Minify is an HTTP content server. It compresses sources of content -(usually files), combines the result and serves it with appropriate -HTTP headers. These headers can allow clients to perform conditional -GETs (serving content only when clients do not have a valid cache) -and tell clients to cache the file for a period of time. -More info: http://code.google.com/p/minify/ - - -WORDPRESS USER? - -These WP plugins integrate Minify into WordPress's style and script hooks to -get you set up faster. - http://wordpress.org/extend/plugins/bwp-minify/ - http://wordpress.org/extend/plugins/w3-total-cache/ - - -INSTALLATION - -Place the /min/ directory as a child of your DOCUMENT_ROOT -directory: i.e. you will have: /home/example/www/min - -You can see verify that it is working by visiting these two URLs: - http://example.org/min/?f=min/quick-test.js - http://example.org/min/?f=min/quick-test.css - -If your server supports mod_rewrite, this URL should also work: - http://example.org/min/f=min/quick-test.js - -CONFIGURATION & USAGE - -See the MIN.txt file and http://code.google.com/p/minify/wiki/UserGuide - -Minify also comes with a URI Builder application that can help you write URLs -for use with Minify or configure groups of files. See here for details: - http://code.google.com/p/minify/wiki/BuilderApp - -The cookbook also provides some more advanced options for minification: - http://code.google.com/p/minify/wiki/CookBook - -UPGRADING - -See UPGRADING.txt for instructions. - - -UNIT TESTING: - -1. Place the /min_unit_tests/ directory as a child of your DOCUMENT_ROOT -directory: i.e. you will have: /home/example/www/min_unit_tests - -2. To run unit tests, access: http://example.org/min_unit_tests/test_all.php - -(If you wish, the other test_*.php files can be run to test individual -components with more verbose output.) - -3. Remove /min_unit_tests/ from your DOCUMENT_ROOT when you are done. - - -FILE ENCODINGS - -Minify *should* work fine with files encoded in UTF-8 or other 8-bit -encodings like ISO 8859/Windows-1252. By default Minify appends -";charset=utf-8" to the Content-Type headers it sends. - -Leading UTF-8 BOMs are stripped from all sources to prevent -duplication in output files, and files are converted to Unix newlines. - diff --git a/composer.json b/composer.json index b4b2357f..e874dfd2 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,30 @@ { - "name": "oyatel/minify", - "description": "Minify is a PHP5 app that helps you follow several rules for client-side performance. It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers", - "authors": [ - { - "name": "Even Andre Fiskvik", - "email": "eaf@oyatel.com" - } - ], - "require": {}, - "autoload": { - "classmap": ["min/lib/"] + "name": "minify/minify", + "description": "Minify is a PHP5 app that helps you follow several rules for client-side performance. It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers", + "keywords": ["js","javascript"], + "homepage": "http://code.google.com/p/minify/", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Ryan Groove", + "email": "ryan@wonko.com", + "homepage": "http://code.google.com/p/minify/", + "role": "Original Author" + }, + { + "name": "Steve Clay", + "email": "steve@mrclay.org", + "homepage": "http://code.google.com/p/minify/", + "role": "Original Author" + }, + { + "name": "Even Andre Fiskvik", + "email": "eaf@oyatel.com", + "role": "Mantainer, Composer Adaptation" } -} + ], + "autoload": { + "classmap": ["min/lib/"] + } +} \ No newline at end of file diff --git a/min/lib/Minify.php b/min/lib/Minify.php index 2900e43b..c840ab21 100644 --- a/min/lib/Minify.php +++ b/min/lib/Minify.php @@ -155,9 +155,11 @@ public static function setCache($cache = '', $fileLocking = true) * * @param array $options controller/serve options * - * @return mixed null, or, if the 'quiet' option is set to true, an array + * @return null|array if the 'quiet' option is set to true, an array * with keys "success" (bool), "statusCode" (int), "content" (string), and * "headers" (array). + * + * @throws Exception */ public static function serve($controller, $options = array()) { @@ -290,7 +292,7 @@ public static function serve($controller, $options = array()) throw $e; } self::$_cache->store($cacheId, $content); - if (function_exists('gzencode')) { + if (function_exists('gzencode') && self::$_options['encodeMethod']) { self::$_cache->store($cacheId . '.gz', gzencode($content, self::$_options['encodeLevel'])); } } diff --git a/min/lib/Minify/ClosureCompiler.php b/min/lib/Minify/ClosureCompiler.php new file mode 100644 index 00000000..76354adf --- /dev/null +++ b/min/lib/Minify/ClosureCompiler.php @@ -0,0 +1,121 @@ + + * Minify_ClosureCompiler::$jarFile = '/path/to/closure-compiler-20120123.jar'; + * Minify_ClosureCompiler::$tempDir = '/tmp'; + * $code = Minify_ClosureCompiler::minify( + * $code, + * array('compilation_level' => 'SIMPLE_OPTIMIZATIONS') + * ); + * + * --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS + * + * + * + * @todo unit tests, $options docs + * @todo more options support (or should just passthru them all?) + * + * @package Minify + * @author Stephen Clay + * @author Elan Ruusamäe + */ +class Minify_ClosureCompiler { + + /** + * Filepath of the Closure Compiler jar file. This must be set before + * calling minifyJs(). + * + * @var string + */ + public static $jarFile = null; + + /** + * Writable temp directory. This must be set before calling minifyJs(). + * + * @var string + */ + public static $tempDir = null; + + /** + * Filepath of "java" executable (may be needed if not in shell's PATH) + * + * @var string + */ + public static $javaExecutable = 'java'; + + /** + * Minify a Javascript string + * + * @param string $js + * + * @param array $options (verbose is ignored) + * + * @see https://code.google.com/p/closure-compiler/source/browse/trunk/README + * + * @return string + */ + public static function minify($js, $options = array()) + { + self::_prepare(); + if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) { + throw new Exception('Minify_ClosureCompiler : could not create temp file.'); + } + file_put_contents($tmpFile, $js); + exec(self::_getCmd($options, $tmpFile), $output, $result_code); + unlink($tmpFile); + if ($result_code != 0) { + throw new Exception('Minify_ClosureCompiler : Closure Compiler execution failed.'); + } + return implode("\n", $output); + } + + private static function _getCmd($userOptions, $tmpFile) + { + $o = array_merge( + array( + 'charset' => 'utf-8', + 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', + ), + $userOptions + ); + $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile) + . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) + ? " --charset {$o['charset']}" + : ''); + + foreach (array('compilation_level') as $opt) { + if ($o[$opt]) { + $cmd .= " --{$opt} ". escapeshellarg($o[$opt]); + } + } + return $cmd . ' ' . escapeshellarg($tmpFile); + } + + private static function _prepare() + { + if (! is_file(self::$jarFile)) { + throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.'); + } + if (! is_readable(self::$jarFile)) { + throw new Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.'); + } + if (! is_dir(self::$tempDir)) { + throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.'); + } + if (! is_writable(self::$tempDir)) { + throw new Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.'); + } + } +} diff --git a/min/lib/Minify/Controller/MinApp.php b/min/lib/Minify/Controller/MinApp.php index e9dac7d1..4b8c32e8 100644 --- a/min/lib/Minify/Controller/MinApp.php +++ b/min/lib/Minify/Controller/MinApp.php @@ -37,6 +37,10 @@ public function setupSources($options) { $firstMissingResource = null; if (isset($_GET['g'])) { + if (! is_string($_GET['g'])) { + $this->log("GET param 'g' was invalid"); + return $options; + } // add group(s) $this->selectionId .= 'g=' . $_GET['g']; $keys = explode(',', $_GET['g']); @@ -91,6 +95,10 @@ public function setupSources($options) { } } if (! $cOptions['groupsOnly'] && isset($_GET['f'])) { + if (! is_string($_GET['f'])) { + $this->log("GET param 'f' was invalid"); + return $options; + } // try user files // The following restrictions are to limit the URLs that minify will // respond to. @@ -119,7 +127,8 @@ public function setupSources($options) { } if (isset($_GET['b'])) { // check for validity - if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b']) + if (is_string($_GET['b']) + && preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b']) && false === strpos($_GET['b'], '..') && $_GET['b'] !== '.') { // valid base diff --git a/min/lib/Minify/YUICompressor.php b/min/lib/Minify/YUICompressor.php index c5bd8a1e..ef446294 100644 --- a/min/lib/Minify/YUICompressor.php +++ b/min/lib/Minify/YUICompressor.php @@ -134,8 +134,8 @@ private static function _prepare() if (! is_file(self::$jarFile)) { throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.'); } - if (! is_executable(self::$jarFile)) { - throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not executable.'); + if (! is_readable(self::$jarFile)) { + throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.'); } if (! is_dir(self::$tempDir)) { throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.'); diff --git a/min_unit_tests/_test_files/html/before.html b/min_unit_tests/_test_files/html/before.html index 3b06d181..ba612500 100644 --- a/min_unit_tests/_test_files/html/before.html +++ b/min_unit_tests/_test_files/html/before.html @@ -11,57 +11,59 @@ css Zen Garden: The Beauty in CSS Design - - - - - + + + + + css Zen Garden: The Beauty in CSS Design -