1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-14 10:05:59 +02:00

+ Solar_Dir for better PHP compat. Moved most of minify.php code to Controller/Version1.php. + a few tests for version1/minify.php

This commit is contained in:
Steve Clay
2008-03-27 16:04:45 +00:00
parent c15acc8782
commit cd0675f490
10 changed files with 353 additions and 103 deletions

View File

@@ -13,9 +13,8 @@
* and by the article "Supercharged JavaScript" by Patrick Hunlock
* <wb@hunlock.com>.
*
* JSMin was originally written by Douglas Crockford <douglas@crockford.com>.
*
* Requires PHP 5.2.1+.
* Requires PHP 5.1.0.
* Tested on PHP 5.1.6.
*
* @package Minify
* @author Ryan Grove <ryan@wonko.com>
@@ -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);
}
}
/**

3
lib/Minify/3rd_party/README vendored Normal file
View File

@@ -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.

View File

@@ -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 <steve@mrclay.org>

View File

@@ -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(

View File

@@ -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(

View File

@@ -0,0 +1,113 @@
<?php
/**
* Class Minify_Controller_Version1
* @package Minify
*/
require_once 'Minify/Controller/Base.php';
/**
* Controller class for emulating version 1 of minify.php
*
* <code>
* Minify::serve('Version1');
* </code>
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
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);
}
}
}
}

View File

@@ -18,4 +18,3 @@ class Minify_Javascript {
}
}
?>

199
lib/Solar/Dir.php Normal file
View File

@@ -0,0 +1,199 @@
<?php
/**
*
* Utility class for static directory methods.
*
* @category Solar
*
* @package Solar
*
* @author Paul M. Jones <pmjones@solarphp.com>
*
* @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';
}
}

18
web/version1/index.html Normal file
View File

@@ -0,0 +1,18 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<title>Test minify.php</title>
</head>
<body>
<h1>Test minify.php</h1>
<ul>
<li><a href="minify.php?files=../test/_test_files/js/before.js">?files=../test/_test_files/js/before.js</a></li>
<li><a href="minify.php?files=../test/_test_files/js/before.js,../test/_test_files/minify/QueryString.js">?files=../test/_test_files/js/before.js,../test/_test_files/minify/QueryString.js</a></li>
<li><a href="minify.php?files=../test/_test_files/css/paths.css">?files=../test/_test_files/css/paths.css</a> (Note that relative URIs in path.css have been rewritten to point to the same location as if paths.css were linked directly.)</li>
<li><a href="minify.php?files=../test/_test_files/css/paths.css,../test/_test_files/css/styles.css">?files=../test/_test_files/css/paths.css,../test/_test_files/css/styles.css</a></li>
</ul>
</body>
</html>

View File

@@ -1,98 +1,6 @@
<?php
//define('MINIFY_BASE_DIR', realpath($_SERVER['DOCUMENT_ROOT'] . '/_3rd_party'));
require '../config.php'; // just to set include_path
require 'Minify.php';
require 'Minify/Controller/Base.php';
if (!defined('MINIFY_BASE_DIR')) {
// files cannot be served above this
define('MINIFY_BASE_DIR', realpath($_SERVER['DOCUMENT_ROOT']));
}
if (!defined('MINIFY_CACHE_DIR')) {
define('MINIFY_CACHE_DIR', sys_get_temp_dir());
}
if (!defined('MINIFY_ENCODING')) {
define('MINIFY_ENCODING', 'utf-8');
}
if (!defined('MINIFY_MAX_FILES')) {
define('MINIFY_MAX_FILES', 16);
}
if (!defined('MINIFY_REWRITE_CSS_URLS')) {
define('MINIFY_REWRITE_CSS_URLS', true);
}
if (!defined('MINIFY_USE_CACHE')) {
define('MINIFY_USE_CACHE', true);
}
class V1Controller extends Minify_Controller_Base {
// setup $this->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());
Minify::serve('Version1');