diff --git a/lib/Minify.php b/lib/Minify.php index e0c069f..35abacf 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -13,9 +13,8 @@ * and by the article "Supercharged JavaScript" by Patrick Hunlock * . * - * JSMin was originally written by Douglas Crockford . - * - * Requires PHP 5.2.1+. + * Requires PHP 5.1.0. + * Tested on PHP 5.1.6. * * @package Minify * @author Ryan Grove @@ -56,9 +55,12 @@ class Minify { * @return null */ public static function useServerCache($path = null) { - self::$_cachePath = (null === $path) - ? sys_get_temp_dir() - : $path; + if (null !== $path) { + self::$_cachePath = $path; + } else { + require_once 'Solar/Dir.php'; + self::$_cachePath = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR); + } } /** diff --git a/lib/Minify/3rd_party/README b/lib/Minify/3rd_party/README new file mode 100644 index 0000000..725d539 --- /dev/null +++ b/lib/Minify/3rd_party/README @@ -0,0 +1,3 @@ +These files are maintained in other projects. If at all possible they +remain unmodified so they can be easily replaced with the latest version +when their parent project has a new release. \ No newline at end of file diff --git a/lib/Minify/CSS.php b/lib/Minify/CSS.php index 9a1683b..05fde8f 100644 --- a/lib/Minify/CSS.php +++ b/lib/Minify/CSS.php @@ -10,9 +10,9 @@ * This is a heavy regex-based removal of whitespace, unnecessary * comments and tokens, and some CSS value minimization, where practical. * Many steps have been taken to avoid breaking comment-based hacks, - * including the ie5/mac filter (and its inversion), but expect hacks - * involving comment tokens in 'content' value strings to break minimization - * badly. A test suite is available. + * including the ie5/mac filter (and its inversion), but expect tricky + * hacks involving comment tokens in 'content' value strings to break + * minimization badly. A test suite is available. * * @package Minify * @author Stephen Clay diff --git a/lib/Minify/Controller/Files.php b/lib/Minify/Controller/Files.php index 22b4020..fb7c8b8 100644 --- a/lib/Minify/Controller/Files.php +++ b/lib/Minify/Controller/Files.php @@ -43,6 +43,10 @@ class Minify_Controller_Files extends Minify_Controller_Base { $sources = array(); foreach ($files as $file) { + if ($file instanceof Minify_Source) { + $sources[] = $file; + continue; + } $file = realpath($file); if (file_exists($file)) { $sources[] = new Minify_Source(array( diff --git a/lib/Minify/Controller/Groups.php b/lib/Minify/Controller/Groups.php index c28313b..820c8de 100644 --- a/lib/Minify/Controller/Groups.php +++ b/lib/Minify/Controller/Groups.php @@ -51,6 +51,10 @@ class Minify_Controller_Groups extends Minify_Controller_Base { } $sources = array(); foreach ($groups[$pi] as $file) { + if ($file instanceof Minify_Source) { + $sources[] = $file; + continue; + } $file = realpath($file); if (file_exists($file)) { $sources[] = new Minify_Source(array( diff --git a/lib/Minify/Controller/Version1.php b/lib/Minify/Controller/Version1.php new file mode 100644 index 0000000..aa59f69 --- /dev/null +++ b/lib/Minify/Controller/Version1.php @@ -0,0 +1,113 @@ + + * Minify::serve('Version1'); + * + * + * @package Minify + * @author Stephen Clay + */ +class Minify_Controller_Version1 extends Minify_Controller_Base { + + /** + * Set up groups of files as sources + * + * @param array $options controller and Minify options + * @return array Minify options + * + */ + public function setupSources($options) { + self::_setupDefines(); + if (MINIFY_USE_CACHE) { + $cacheDir = defined('MINIFY_CACHE_DIR') + ? MINIFY_CACHE_DIR + : null; + Minify::useServerCache($cacheDir); + } + $options['badRequestHeader'] = 'HTTP/1.0 404 Not Found'; + $options['contentTypeCharset'] = MINIFY_ENCODING; + + // The following restrictions are to limit the URLs that minify will + // respond to. Ideally there should be only one way to reference a file. + if (! isset($_GET['files']) + // verify at least one file, files are single comma separated, + // and are all same extension + || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m) + // no "//" (makes URL rewriting easier) + || strpos($_GET['files'], '//') !== false + // no "\" + || strpos($_GET['files'], '\\') !== false + // no "./" + || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files']) + ) { + return $options; + } + $extension = $m[1]; + + $files = explode(',', $_GET['files']); + if (count($files) > MINIFY_MAX_FILES) { + return $options; + } + + // strings for prepending to relative/absolute paths + $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME']) + . DIRECTORY_SEPARATOR; + $prependAbsPaths = $_SERVER['DOCUMENT_ROOT']; + + $sources = array(); + $goodFiles = array(); + $hasBadSource = false; + foreach ($files as $file) { + // prepend appropriate string for abs/rel paths + $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file; + // make sure a real file! + $file = realpath($file); + // don't allow unsafe or duplicate files + if (parent::_fileIsSafe($file, MINIFY_BASE_DIR) + && !in_array($file, $goodFiles)) + { + $goodFiles[] = $file; + $srcOptions = array( + 'filepath' => $file + ); + if ('css' === $extension && MINIFY_REWRITE_CSS_URLS) { + $srcOptions['minifyOptions']['currentPath'] = dirname($file); + } + $this->sources[] = new Minify_Source($srcOptions); + } else { + $hasBadSource = true; + break; + } + } + if ($hasBadSource) { + $this->sources = array(); + } + return $options; + } + + private static function _setupDefines() + { + $defaults = array( + 'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT']) + ,'MINIFY_ENCODING' => 'utf-8' + ,'MINIFY_MAX_FILES' => 16 + ,'MINIFY_REWRITE_CSS_URLS' => true + ,'MINIFY_USE_CACHE' => true + ); + foreach ($defaults as $const => $val) { + if (! defined($const)) { + define($const, $val); + } + } + } +} + diff --git a/lib/Minify/Javascript.php b/lib/Minify/Javascript.php index 1d0bd5b..1f19b9d 100644 --- a/lib/Minify/Javascript.php +++ b/lib/Minify/Javascript.php @@ -18,4 +18,3 @@ class Minify_Javascript { } } -?> \ No newline at end of file diff --git a/lib/Solar/Dir.php b/lib/Solar/Dir.php new file mode 100644 index 0000000..37f7169 --- /dev/null +++ b/lib/Solar/Dir.php @@ -0,0 +1,199 @@ + + * + * @license http://opensource.org/licenses/bsd-license.php BSD + * + * @version $Id: Dir.php 2926 2007-11-09 16:25:44Z pmjones $ + * + */ +class Solar_Dir { + + /** + * + * The OS-specific temporary directory location. + * + * @var string + * + */ + protected static $_tmp; + + /** + * + * Hack for [[php::is_dir() | ]] that checks the include_path. + * + * Use this to see if a directory exists anywhere in the include_path. + * + * {{code: php + * $dir = Solar_Dir::exists('path/to/dir') + * if ($dir) { + * $files = scandir($dir); + * } else { + * echo "Not found in the include-path."; + * } + * }} + * + * @param string $dir Check for this directory in the include_path. + * + * @return mixed If the directory exists in the include_path, returns the + * absolute path; if not, returns boolean false. + * + */ + public static function exists($dir) + { + // no file requested? + $dir = trim($dir); + if (! $dir) { + return false; + } + + // using an absolute path for the file? + // dual check for Unix '/' and Windows '\', + // or Windows drive letter and a ':'. + $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':'); + if ($abs && is_dir($dir)) { + return $dir; + } + + // using a relative path on the file + $path = explode(PATH_SEPARATOR, ini_get('include_path')); + foreach ($path as $base) { + // strip Unix '/' and Windows '\' + $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir; + if (is_dir($target)) { + return $target; + } + } + + // never found it + return false; + } + + /** + * + * "Fixes" a directory string for the operating system. + * + * Use slashes anywhere you need a directory separator. Then run the + * string through fixdir() and the slashes will be converted to the + * proper separator (for example '\' on Windows). + * + * Always adds a final trailing separator. + * + * @param string $dir The directory string to 'fix'. + * + * @return string The "fixed" directory string. + * + */ + public static function fix($dir) + { + $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); + return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + } + + /** + * + * Convenience method for dirname() and higher-level directories. + * + * @param string $file Get the dirname() of this file. + * + * @param int $up Move up in the directory structure this many + * times, default 0. + * + * @return string The dirname() of the file. + * + */ + public static function name($file, $up = 0) + { + $dir = dirname($file); + while ($up --) { + $dir = dirname($dir); + } + return $dir; + } + + /** + * + * Returns the OS-specific directory for temporary files. + * + * @param string $sub Add this subdirectory to the returned temporary + * directory name. + * + * @return string The temporary directory path. + * + */ + public static function tmp($sub = '') + { + // find the tmp dir if needed + if (! Solar_Dir::$_tmp) { + + // use the system if we can + if (function_exists('sys_get_temp_dir')) { + $tmp = sys_get_temp_dir(); + } else { + $tmp = Solar_Dir::_tmp(); + } + + // remove trailing separator and save + Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR); + } + + // do we have a subdirectory request? + $sub = trim($sub); + if ($sub) { + // remove leading and trailing separators, and force exactly + // one trailing separator + $sub = trim($sub, DIRECTORY_SEPARATOR) + . DIRECTORY_SEPARATOR; + } + + return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub; + } + + /** + * + * Returns the OS-specific temporary directory location. + * + * @return string The temp directory path. + * + */ + protected static function _tmp() + { + // non-Windows system? + if (strtolower(substr(PHP_OS, 0, 3)) != 'win') { + $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR']; + if ($tmp) { + return $tmp; + } else { + return '/tmp'; + } + } + + // Windows 'TEMP' + $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP']; + if ($tmp) { + return $tmp; + } + + // Windows 'TMP' + $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP']; + if ($tmp) { + return $tmp; + } + + // Windows 'windir' + $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir']; + if ($tmp) { + return $tmp; + } + + // final fallback for Windows + return getenv('SystemRoot') . '\\temp'; + } +} \ No newline at end of file diff --git a/web/version1/index.html b/web/version1/index.html new file mode 100644 index 0000000..2f09c89 --- /dev/null +++ b/web/version1/index.html @@ -0,0 +1,18 @@ + + + + Test minify.php + + +

Test minify.php

+ + + + + \ No newline at end of file diff --git a/web/version1/minify.php b/web/version1/minify.php index d757176..ed048fc 100644 --- a/web/version1/minify.php +++ b/web/version1/minify.php @@ -1,98 +1,6 @@ sources and return $options - public function setupSources($options) { - $options['badRequestHeader'] = 'HTTP/1.0 404 Not Found'; - $options['contentTypeCharset'] = MINIFY_ENCODING; - - // The following restrictions are to limit the URLs that minify will - // respond to. Ideally there should be only one way to reference a file. - if (! isset($_GET['files']) - // verify at least one file, files are single comma separated, - // and are all same extension - || ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m) - // no "//" (makes URL rewriting easier) - || strpos($_GET['files'], '//') !== false - // no "\" - || strpos($_GET['files'], '\\') !== false - // no "./" - || preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files']) - ) { - return $options; - } - $extension = $m[1]; - - $files = explode(',', $_GET['files']); - if (count($files) > MINIFY_MAX_FILES) { - return $options; - } - - // strings for prepending to relative/absolute paths - $prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME']) - . DIRECTORY_SEPARATOR; - $prependAbsPaths = $_SERVER['DOCUMENT_ROOT']; - - $sources = array(); - $goodFiles = array(); - $hasBadSource = false; - foreach ($files as $file) { - // prepend appropriate string for abs/rel paths - $file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file; - // make sure a real file! - $file = realpath($file); - // don't allow unsafe or duplicate files - if (parent::_fileIsSafe($file, MINIFY_BASE_DIR) - && !in_array($file, $goodFiles)) - { - $goodFiles[] = $file; - $srcOptions = array( - 'filepath' => $file - ); - if ('css' === $extension && MINIFY_REWRITE_CSS_URLS) { - $srcOptions['minifyOptions']['currentPath'] = dirname($file); - } - $this->sources[] = new Minify_Source($srcOptions); - } else { - $hasBadSource = true; - break; - } - } - if ($hasBadSource) { - $this->sources = array(); - } - return $options; - } -} - -if (MINIFY_USE_CACHE) { - Minify::useServerCache(MINIFY_CACHE_DIR); -} -Minify::serve(new V1Controller()); \ No newline at end of file +Minify::serve('Version1');