V 3.5 RC 1

This commit is contained in:
Romain Neutron
2011-12-05 00:23:28 +01:00
parent 6f1ee368aa
commit 4c5b7eb658
5563 changed files with 466984 additions and 985416 deletions

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Build
* Class Minify_Build
* @package Minify
*/
@@ -9,42 +9,42 @@ require_once 'Minify/Source.php';
/**
* Maintain a single last modification time for a group of Minify sources to
* allow use of far off Expires headers in Minify.
*
*
* <code>
* // in config file
* $groupSources = array(
* 'js' => array('file1.js', 'file2.js')
* ,'css' => array('file1.css', 'file2.css', 'file3.css')
* )
*
*
* // during HTML generation
* $jsBuild = new Minify_Build($groupSources['js']);
* $cssBuild = new Minify_Build($groupSources['css']);
*
*
* $script = "<script type='text/javascript' src='"
* . $jsBuild->uri('/min.php/js') . "'></script>";
* $link = "<link rel='stylesheet' type='text/css' href='"
* . $cssBuild->uri('/min.php/css') . "'>";
*
*
* // in min.php
* Minify::serve('Groups', array(
* 'groups' => $groupSources
* ,'setExpires' => (time() + 86400 * 365)
* ));
* </code>
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Build {
/**
* Last modification time of all files in the build
*
* @var int
*
* @var int
*/
public $lastModified = 0;
/**
* String to use as ampersand in uri(). Set this to '&' if
* you are not HTML-escaping URIs.
@@ -52,20 +52,20 @@ class Minify_Build {
* @var string
*/
public static $ampersand = '&amp;';
/**
* Get a time-stamped URI
*
*
* <code>
* echo $b->uri('/site.js');
* // outputs "/site.js?1678242"
*
*
* echo $b->uri('/scriptaculous.js?load=effects');
* // outputs "/scriptaculous.js?load=effects&amp1678242"
* </code>
*
* @param string $uri
* @param boolean $forceAmpersand (default = false) Force the use of ampersand to
* @param boolean $forceAmpersand (default = false) Force the use of ampersand to
* append the timestamp to the URI.
* @return string
*/
@@ -73,17 +73,18 @@ class Minify_Build {
$sep = ($forceAmpersand || strpos($uri, '?') !== false)
? self::$ampersand
: '?';
return "{$uri}{$sep}{$this->lastModified}";
}
/**
/**
* Create a build object
*
*
* @param array $sources array of Minify_Source objects and/or file paths
*
*
* @return null
*/
public function __construct($sources)
public function __construct($sources)
{
$max = 0;
foreach ((array)$sources as $source) {

View File

@@ -1,322 +1,83 @@
<?php
/**
* Class Minify_CSS
* Class Minify_CSS
* @package Minify
*/
/**
* Compress CSS
* Minify CSS
*
* This class uses Minify_CSS_Compressor and Minify_CSS_UriRewriter to
* minify CSS and rewrite relative URIs.
*
* 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 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>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
*/
class Minify_CSS {
/**
* Minify a CSS string
*
*
* @param string $css
*
*
* @param array $options available options:
*
*
* 'preserveComments': (default true) multi-line comments that begin
* with "/*!" will be preserved with newlines before and after to
* enhance readability.
*
*
* 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations
*
*
* 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to
* the desired files. For this to work, the files *must* exist and be
* visible by the PHP process.
*
* @return string
*/
public static function minify($css, $options = array())
{
if (isset($options['preserveComments'])
&& !$options['preserveComments']) {
return self::_minify($css, $options);
}
require_once 'Minify/CommentPreserver.php';
// recursive calls don't preserve comments
$options['preserveComments'] = false;
return Minify_CommentPreserver::process(
$css
,array('Minify_CSS', 'minify')
,array($options)
);
}
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options To enable URL rewriting, set the value
* for key 'prependRelativePath'.
*
* @return string
*/
protected static function _minify($css, $options)
{
$css = str_replace("\r\n", "\n", $css);
// preserve empty comment after '>'
// http://www.webdevout.net/css-hacks#in_css-selectors
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
// preserve empty comment between property and value
// http://css-discuss.incutio.com/?page=BoxModelHack
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
// apply callback to all valid comments (and strip out surrounding ws
self::$_inHack = false;
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
,array('Minify_CSS', '_commentCB'), $css);
// remove ws around { } and last semicolon in declaration block
$css = preg_replace('/\\s*{\\s*/', '{', $css);
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
// remove ws surrounding semicolons
$css = preg_replace('/\\s*;\\s*/', ';', $css);
// remove ws around urls
$css = preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
\\s*
\\) # )
/x', 'url($1)', $css);
// remove ws between rules and colons
$css = preg_replace('/
\\s*
([{;]) # 1 = beginning of block or rule separator
\\s*
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
\\s*
:
\\s*
(\\b|[#\'"]) # 3 = first character of a value
/x', '$1$2:$3', $css);
// remove ws in selectors
$css = preg_replace_callback('/
(?: # non-capture
\\s*
[^~>+,\\s]+ # selector part
\\s*
[,>+~] # combinators
)+
\\s*
[^~>+,\\s]+ # selector part
{ # open declaration block
/x'
,array('Minify_CSS', '_selectorsCB'), $css);
// minimize hex colors
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
, '$1#$2$3$4$5', $css);
// remove spaces between font families
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
,array('Minify_CSS', '_fontFamilyCB'), $css);
$css = preg_replace('/@import\\s+url/', '@import url', $css);
// replace any ws involving newlines with a single newline
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
// separate common descendent selectors w/ newlines (to limit line lengths)
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
// Use newline after 1st numeric value (to limit line lengths).
$css = preg_replace('/
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
\\s+
/x'
,"$1\n", $css);
$rewrite = false;
if (isset($options['prependRelativePath'])) {
self::$_tempPrepend = $options['prependRelativePath'];
$rewrite = true;
} elseif (isset($options['currentDir'])) {
self::$_tempCurrentDir = $options['currentDir'];
$rewrite = true;
}
if ($rewrite) {
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array('Minify_CSS', '_urlCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array('Minify_CSS', '_urlCB'), $css);
}
self::$_tempPrepend = self::$_tempCurrentDir = '';
return trim($css);
}
/**
* Replace what looks like a set of selectors
*
* @param array $m regex matches
*
* 'symlinks': (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.:
* <code>
* array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* @return string
*/
protected static function _selectorsCB($m)
public static function minify($css, $options = array())
{
// remove ws around the combinators
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
}
/**
* @var bool Are we "in" a hack?
*
* I.e. are some browsers targetted until the next comment?
*/
protected static $_inHack = false;
/**
* @var string string to be prepended to relative URIs
*/
protected static $_tempPrepend = '';
/**
* @var string directory of this stylesheet for rewriting purposes
*/
protected static $_tempCurrentDir = '';
/**
* Process a comment and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected static function _commentCB($m)
{
$m = $m[1];
// $m is the comment content w/o the surrounding tokens,
// but the return value will replace the entire comment.
if ($m === 'keep') {
return '/**/';
}
if ($m === '" "') {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*" "*/';
}
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*";}}/* */';
}
if (self::$_inHack) {
// inversion: feeding only to one browser
if (preg_match('@
^/ # comment started like /*/
\\s*
(\\S[\\s\\S]+?) # has at least some non-ws content
\\s*
/\\* # ends like /*/ or /**/
@x', $m, $n)) {
// end hack mode after this comment, but preserve the hack and comment content
self::$_inHack = false;
return "/*/{$n[1]}/**/";
}
}
if (substr($m, -1) === '\\') { // comment ends like \*/
// begin hack mode and preserve hack
self::$_inHack = true;
return '/*\\*/';
}
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
// begin hack mode and preserve hack
self::$_inHack = true;
return '/*/*/';
}
if (self::$_inHack) {
// a regular comment ends hack mode but should be preserved
self::$_inHack = false;
return '/**/';
}
return ''; // remove all other comments
}
protected static function _urlCB($m)
{
$isImport = (0 === strpos($m[0], '@import'));
if ($isImport) {
$quote = $m[1];
$url = $m[2];
require_once 'Minify/CSS/Compressor.php';
if (isset($options['preserveComments'])
&& !$options['preserveComments']) {
$css = Minify_CSS_Compressor::process($css, $options);
} else {
// is url()
// $m[1] is either quoted or not
$quote = ($m[1][0] === "'" || $m[1][0] === '"')
? $m[1][0]
: '';
$url = ($quote === '')
? $m[1]
: substr($m[1], 1, strlen($m[1]) - 2);
require_once 'Minify/CommentPreserver.php';
$css = Minify_CommentPreserver::process(
$css
,array('Minify_CSS_Compressor', 'process')
,array($options)
);
}
if ('/' !== $url[0]) {
if (strpos($url, '//') > 0) {
// probably starts with protocol, do not alter
} else {
// relative URI, rewrite!
if (self::$_tempPrepend) {
$url = self::$_tempPrepend . $url;
} else {
// rewrite absolute url from scratch!
// prepend path with current dir separator (OS-independent)
$path = self::$_tempCurrentDir
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
// strip doc root
$path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
// fix to absolute URL
$url = strtr($path, DIRECTORY_SEPARATOR, '/');
// remove /./ and /../ where possible
$url = str_replace('/./', '/', $url);
// inspired by patch from Oleg Cherniy
do {
$url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, -1, $changed);
} while ($changed);
}
}
if (! isset($options['currentDir']) && ! isset($options['prependRelativePath'])) {
return $css;
}
require_once 'Minify/CSS/UriRewriter.php';
if (isset($options['currentDir'])) {
return Minify_CSS_UriRewriter::rewrite(
$css
,$options['currentDir']
,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
,isset($options['symlinks']) ? $options['symlinks'] : array()
);
} else {
return Minify_CSS_UriRewriter::prepend(
$css
,$options['prependRelativePath']
);
}
return $isImport
? "@import {$quote}{$url}{$quote}"
: "url({$quote}{$url}{$quote})";
}
/**
* Process a font-family listing and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected static function _fontFamilyCB($m)
{
$m[1] = preg_replace('/
\\s*
(
"[^"]+" # 1 = family in double qutoes
|\'[^\']+\' # or 1 = family in single quotes
|[\\w\\-]+ # or 1 = unquoted family
)
\\s*
/x', '$1', $m[1]);
return 'font-family:' . $m[1] . $m[2];
}
}

View File

@@ -0,0 +1,256 @@
<?php
/**
* Class Minify_CSS_Compressor
* @package Minify
*/
/**
* Compress CSS
*
* 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 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>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
*/
class Minify_CSS_Compressor {
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options (currently ignored)
*
* @return string
*/
public static function process($css, $options = array())
{
$obj = new Minify_CSS_Compressor($options);
return $obj->_process($css);
}
/**
* @var array options
*/
protected $_options = null;
/**
* @var bool Are we "in" a hack?
*
* I.e. are some browsers targetted until the next comment?
*/
protected $_inHack = false;
/**
* Constructor
*
* @param array $options (currently ignored)
*
* @return null
*/
private function __construct($options) {
$this->_options = $options;
}
/**
* Minify a CSS string
*
* @param string $css
*
* @return string
*/
protected function _process($css)
{
$css = str_replace("\r\n", "\n", $css);
// preserve empty comment after '>'
// http://www.webdevout.net/css-hacks#in_css-selectors
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
// preserve empty comment between property and value
// http://css-discuss.incutio.com/?page=BoxModelHack
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
// apply callback to all valid comments (and strip out surrounding ws
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
,array($this, '_commentCB'), $css);
// remove ws around { } and last semicolon in declaration block
$css = preg_replace('/\\s*{\\s*/', '{', $css);
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
// remove ws surrounding semicolons
$css = preg_replace('/\\s*;\\s*/', ';', $css);
// remove ws around urls
$css = preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
\\s*
\\) # )
/x', 'url($1)', $css);
// remove ws between rules and colons
$css = preg_replace('/
\\s*
([{;]) # 1 = beginning of block or rule separator
\\s*
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
\\s*
:
\\s*
(\\b|[#\'"]) # 3 = first character of a value
/x', '$1$2:$3', $css);
// remove ws in selectors
$css = preg_replace_callback('/
(?: # non-capture
\\s*
[^~>+,\\s]+ # selector part
\\s*
[,>+~] # combinators
)+
\\s*
[^~>+,\\s]+ # selector part
{ # open declaration block
/x'
,array($this, '_selectorsCB'), $css);
// minimize hex colors
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
, '$1#$2$3$4$5', $css);
// remove spaces between font families
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
,array($this, '_fontFamilyCB'), $css);
$css = preg_replace('/@import\\s+url/', '@import url', $css);
// replace any ws involving newlines with a single newline
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
// separate common descendent selectors w/ newlines (to limit line lengths)
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
// Use newline after 1st numeric value (to limit line lengths).
$css = preg_replace('/
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
\\s+
/x'
,"$1\n", $css);
// prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/
$css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css);
return trim($css);
}
/**
* Replace what looks like a set of selectors
*
* @param array $m regex matches
*
* @return string
*/
protected function _selectorsCB($m)
{
// remove ws around the combinators
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
}
/**
* Process a comment and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected function _commentCB($m)
{
$hasSurroundingWs = (trim($m[0]) !== $m[1]);
$m = $m[1];
// $m is the comment content w/o the surrounding tokens,
// but the return value will replace the entire comment.
if ($m === 'keep') {
return '/**/';
}
if ($m === '" "') {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*" "*/';
}
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*";}}/* */';
}
if ($this->_inHack) {
// inversion: feeding only to one browser
if (preg_match('@
^/ # comment started like /*/
\\s*
(\\S[\\s\\S]+?) # has at least some non-ws content
\\s*
/\\* # ends like /*/ or /**/
@x', $m, $n)) {
// end hack mode after this comment, but preserve the hack and comment content
$this->_inHack = false;
return "/*/{$n[1]}/**/";
}
}
if (substr($m, -1) === '\\') { // comment ends like \*/
// begin hack mode and preserve hack
$this->_inHack = true;
return '/*\\*/';
}
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
// begin hack mode and preserve hack
$this->_inHack = true;
return '/*/*/';
}
if ($this->_inHack) {
// a regular comment ends hack mode but should be preserved
$this->_inHack = false;
return '/**/';
}
// Issue 107: if there's any surrounding whitespace, it may be important, so
// replace the comment with a single space
return $hasSurroundingWs // remove all other comments
? ' '
: '';
}
/**
* Process a font-family listing and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected function _fontFamilyCB($m)
{
$m[1] = preg_replace('/
\\s*
(
"[^"]+" # 1 = family in double qutoes
|\'[^\']+\' # or 1 = family in single quotes
|[\\w\\-]+ # or 1 = unquoted family
)
\\s*
/x', '$1', $m[1]);
return 'font-family:' . $m[1] . $m[2];
}
}

View File

@@ -1,75 +1,155 @@
<?php
/**
* Class Minify_CSS_UriRewriter
* Class Minify_CSS_UriRewriter
* @package Minify
*/
/**
* Rewrite file-relative URIs as root-relative in CSS files
*
* @todo: prepend() method
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_CSS_UriRewriter {
/**
* Defines which class to call as part of callbacks, change this
* if you extend Minify_CSS_UriRewriter
* @var string
*/
protected static $className = 'Minify_CSS_UriRewriter';
/**
* rewrite() and rewriteRelative() append debugging information here
* @var string
*/
public static $debugText = '';
/**
* Rewrite file relative URIs as root relative in CSS files
*
*
* @param string $css
*
*
* @param string $currentDir The directory of the current CSS file.
*
* @param string $docRoot The document root of the web site in which
* @param string $docRoot The document root of the web site in which
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
*
*
* @param array $symlinks (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.:
* <code>
* array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* @return string
*/
public static function rewrite($css, $currentDir, $docRoot = null)
public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array())
{
self::$_docRoot = $docRoot
? $docRoot
: $_SERVER['DOCUMENT_ROOT'];
self::$_docRoot = realpath(self::$_docRoot);
self::$_currentDir = realpath($currentDir);
// remove ws around urls
$css = preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = URI (really just a bunch of non right parenthesis)
\\s*
\\) # )
/x', 'url($1)', $css);
self::$_docRoot = self::_realpath(
$docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT']
);
self::$_currentDir = self::_realpath($currentDir);
self::$_symlinks = array();
// normalize symlinks
foreach ($symlinks as $link => $target) {
$link = ($link === '//')
? self::$_docRoot
: str_replace('//', self::$_docRoot . '/', $link);
$link = strtr($link, '/', DIRECTORY_SEPARATOR);
self::$_symlinks[$link] = self::_realpath($target);
}
self::$debugText .= "docRoot : " . self::$_docRoot . "\n"
. "currentDir : " . self::$_currentDir . "\n";
if (self::$_symlinks) {
self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n";
}
self::$debugText .= "\n";
$css = self::_trimUrls($css);
// rewrite
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array('Minify_CSS_UriRewriter', '_uriCB'), $css);
,array(self::$className, '_processUriCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array('Minify_CSS_UriRewriter', '_uriCB'), $css);
,array(self::$className, '_processUriCB'), $css);
return $css;
}
/**
* Prepend a path to relative URIs in CSS files
*
* @param string $css
*
* @param string $path The path to prepend.
*
* @return string
*/
public static function prepend($css, $path)
{
self::$_prependPath = $path;
$css = self::_trimUrls($css);
// append
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array(self::$className, '_processUriCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array(self::$className, '_processUriCB'), $css);
self::$_prependPath = null;
return $css;
}
/**
* @var string directory of this stylesheet
*/
private static $_currentDir = '';
/**
* @var string DOC_ROOT
*/
private static $_docRoot = '';
private static function _uriCB($m)
/**
* @var array directory replacements to map symlink targets back to their
* source (within the document root) E.g. '/var/www/symlink' => '/var/realpath'
*/
private static $_symlinks = array();
/**
* @var string path to prepend
*/
private static $_prependPath = null;
private static function _trimUrls($css)
{
return preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = URI (assuming does not contain ")")
\\s*
\\) # )
/x', 'url($1)', $css);
}
private static function _processUriCB($m)
{
// $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
$isImport = ($m[0][0] === '@');
// determine URI and the quote character (if any)
if ($isImport) {
$quoteChar = $m[1];
$uri = $m[2];
} else {
// is url()
// $m[1] is either quoted or not
$quoteChar = ($m[1][0] === "'" || $m[1][0] === '"')
? $m[1][0]
@@ -78,30 +158,116 @@ class Minify_CSS_UriRewriter {
? $m[1]
: substr($m[1], 1, strlen($m[1]) - 2);
}
if ('/' !== $uri[0]) {
if (strpos($uri, '//') > 0) {
// probably starts with protocol, do not alter
} else {
// it's a file relative URI!
// prepend path with current dir separator (OS-independent)
$path = strtr(self::$_currentDir, '/', DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
// strip doc root
$path = substr($path, strlen(self::$_docRoot));
// fix to root-relative URI
$uri = strtr($path, DIRECTORY_SEPARATOR, '/');
// remove /./ and /../ where possible
$uri = str_replace('/./', '/', $uri);
// inspired by patch from Oleg Cherniy
do {
$uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, -1, $changed);
} while ($changed);
// analyze URI
if ('/' !== $uri[0] // root-relative
&& false === strpos($uri, '//') // protocol (non-data)
&& 0 !== strpos($uri, 'data:') // data protocol
) {
// URI is file-relative: rewrite depending on options
$uri = (self::$_prependPath !== null)
? (self::$_prependPath . $uri)
: self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
}
return $isImport
? "@import {$quoteChar}{$uri}{$quoteChar}"
: "url({$quoteChar}{$uri}{$quoteChar})";
}
/**
* Rewrite a file relative URI as root relative
*
* <code>
* Minify_CSS_UriRewriter::rewriteRelative(
* '../img/hello.gif'
* , '/home/user/www/css' // path of CSS file
* , '/home/user/www' // doc root
* );
* // returns '/img/hello.gif'
*
* // example where static files are stored in a symlinked directory
* Minify_CSS_UriRewriter::rewriteRelative(
* 'hello.gif'
* , '/var/staticFiles/theme'
* , '/home/user/www'
* , array('/home/user/www/static' => '/var/staticFiles')
* );
* // returns '/static/theme/hello.gif'
* </code>
*
* @param string $uri file relative URI
*
* @param string $realCurrentDir realpath of the current file's directory.
*
* @param string $realDocRoot realpath of the site document root.
*
* @param array $symlinks (default = array()) If the file is stored in
* a symlink-ed directory, provide an array of link paths to
* real target paths, where the link paths "appear" to be within the document
* root. E.g.:
* <code>
* array('/home/foo/www/not/real/path' => '/real/target/path') // unix
* array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows
* </code>
*
* @return string
*/
public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
{
// prepend path with current dir separator (OS-independent)
$path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
self::$debugText .= "file-relative URI : {$uri}\n"
. "path prepended : {$path}\n";
// "unresolve" a symlink back to doc root
foreach ($symlinks as $link => $target) {
if (0 === strpos($path, $target)) {
// replace $target with $link
$path = $link . substr($path, strlen($target));
self::$debugText .= "symlink unresolved : {$path}\n";
break;
}
}
if ($isImport) {
return "@import {$quoteChar}{$uri}{$quoteChar}";
} else {
return "url({$quoteChar}{$uri}{$quoteChar})";
// strip doc root
$path = substr($path, strlen($realDocRoot));
self::$debugText .= "docroot stripped : {$path}\n";
// fix to root-relative URI
$uri = strtr($path, '/\\', '//');
// remove /./ and /../ where possible
$uri = str_replace('/./', '/', $uri);
// inspired by patch from Oleg Cherniy
do {
$uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
} while ($changed);
self::$debugText .= "traversals removed : {$uri}\n\n";
return $uri;
}
/**
* Get realpath with any trailing slash removed. If realpath() fails,
* just remove the trailing slash.
*
* @param string $path
*
* @return mixed path with no trailing slash
*/
protected static function _realpath($path)
{
$realPath = realpath($path);
if ($realPath !== false) {
$path = $realPath;
}
return rtrim($path, '/\\');
}
}

View File

@@ -0,0 +1,132 @@
<?php
/**
* Class Minify_Cache_APC
* @package Minify
*/
/**
* APC-based cache class for Minify
*
* <code>
* Minify::setCache(new Minify_Cache_APC());
* </code>
*
* @package Minify
* @author Chris Edwards
**/
class Minify_Cache_APC {
/**
* Create a Minify_Cache_APC object, to be passed to
* Minify::setCache().
*
*
* @param int $expire seconds until expiration (default = 0
* meaning the item will not get an expiration date)
*
* @return null
*/
public function __construct($expire = 0)
{
$this->_exp = $expire;
}
/**
* Write data to cache.
*
* @param string $id cache id
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
return apc_store($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id
*
* @return int size in bytes
*/
public function getSize($id)
{
return $this->_fetch($id)
? strlen($this->_data)
: false;
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id
*/
public function display($id)
{
echo $this->_fetch($id)
? $this->_data
: '';
}
/**
* Fetch the cached content
*
* @param string $id cache id
*
* @return string
*/
public function fetch($id)
{
return $this->_fetch($id)
? $this->_data
: '';
}
private $_exp = null;
// cache of most recently fetched id
private $_lm = null;
private $_data = null;
private $_id = null;
/**
* Fetch data and timestamp from apc, store in instance
*
* @param string $id
*
* @return bool success
*/
private function _fetch($id)
{
if ($this->_id === $id) {
return true;
}
$ret = apc_fetch($id);
if (false === $ret) {
$this->_id = null;
return false;
}
list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id;
return true;
}
}

View File

@@ -1,76 +1,79 @@
<?php
/**
* Class Minify_Cache_File
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
public function __construct($path = '', $fileLocking = false)
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
}
}
$this->_locking = $fileLocking;
$this->_path = $path;
}
/**
* Write data to cache.
*
* @param string $id cache id (e.g. a filename)
*
*
* @param string $data
*
*
* @return bool success
*/
public function store($id, $data)
{
$flag = $this->_locking
? LOCK_EX
: null;
if (is_file($this->_path . '/' . $id)) {
@unlink($this->_path . '/' . $id);
}
if (! @file_put_contents($this->_path . '/' . $id, $data, $flag)) {
return false;
$flag = $this->_locking
? LOCK_EX
: null;
if (is_file($this->_path . '/' . $id)) {
@unlink($this->_path . '/' . $id);
}
// write control
if ($data !== $this->fetch($id)) {
@unlink($file);
return false;
}
if (! @file_put_contents($this->_path . '/' . $id, $data, $flag)) {
return false;
}
// write control
if ($data !== $this->fetch($id)) {
@unlink($file);
return false;
}
return true;
}
/**
* Get the size of a cache entry
*
* @param string $id cache id (e.g. a filename)
*
*
* @return int size in bytes
*/
public function getSize($id)
{
return filesize($this->_path . '/' . $id);
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id (e.g. a filename)
*
*
* @param int $srcMtime mtime of the original source file(s)
*
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$file = $this->_path . '/' . $id;
return (is_file($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
@@ -78,38 +81,49 @@ class Minify_Cache_File {
*/
public function display($id)
{
if ($this->_locking) {
$fp = fopen($this->_path . '/' . $id, 'rb');
flock($fp, LOCK_SH);
fpassthru($fp);
flock($fp, LOCK_UN);
fclose($fp);
} else {
readfile($this->_path . '/' . $id);
}
if ($this->_locking) {
$fp = fopen($this->_path . '/' . $id, 'rb');
flock($fp, LOCK_SH);
fpassthru($fp);
flock($fp, LOCK_UN);
fclose($fp);
} else {
readfile($this->_path . '/' . $id);
}
}
/**
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
*
* @return string
*/
public function fetch($id)
{
if ($this->_locking) {
$fp = fopen($this->_path . '/' . $id, 'rb');
flock($fp, LOCK_SH);
$ret = stream_get_contents($fp);
flock($fp, LOCK_UN);
fclose($fp);
return $ret;
} else {
return file_get_contents($this->_path . '/' . $id);
if ($this->_locking) {
$fp = fopen($this->_path . '/' . $id, 'rb');
flock($fp, LOCK_SH);
$ret = stream_get_contents($fp);
flock($fp, LOCK_UN);
fclose($fp);
return $ret;
} else {
return file_get_contents($this->_path . '/' . $id);
}
}
/**
* Fetch the cache path used
*
* @return string
*/
public function getPath()
{
return $this->_path;
}
private $_path = null;
private $_locking = null;
private $_locking = null;
}

View File

@@ -6,7 +6,7 @@
/**
* Memcache-based cache class for Minify
*
*
* <code>
* // fall back to disk caching if memcache can't connect
* $memcache = new Memcache;
@@ -18,16 +18,16 @@
* </code>
**/
class Minify_Cache_Memcache {
/**
* Create a Minify_Cache_Memcache object, to be passed to
* Create a Minify_Cache_Memcache object, to be passed to
* Minify::setCache().
*
* @param Memcache $memcache already-connected instance
*
*
* @param int $expire seconds until expiration (default = 0
* meaning the item will not get an expiration date)
*
*
* @return null
*/
public function __construct($memcache, $expire = 0)
@@ -35,27 +35,27 @@ class Minify_Cache_Memcache {
$this->_mc = $memcache;
$this->_exp = $expire;
}
/**
* Write data to cache.
*
* @param string $id cache id
*
*
* @param string $data
*
*
* @return bool success
*/
public function store($id, $data)
{
return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id
*
*
* @return int size in bytes
*/
public function getSize($id)
@@ -64,21 +64,21 @@ class Minify_Cache_Memcache {
? strlen($this->_data)
: false;
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id
*
*
* @param int $srcMtime mtime of the original source file(s)
*
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
}
/**
* Send the cached content to output
*
@@ -90,12 +90,12 @@ class Minify_Cache_Memcache {
? $this->_data
: '';
}
/**
/**
* Fetch the cached content
*
* @param string $id cache id
*
*
* @return string
*/
public function fetch($id)
@@ -104,20 +104,20 @@ class Minify_Cache_Memcache {
? $this->_data
: '';
}
private $_mc = null;
private $_exp = null;
// cache of most recently fetched id
private $_lm = null;
private $_data = null;
private $_id = null;
/**
/**
* Fetch data and timestamp from memcache, store in instance
*
*
* @param string $id
*
*
* @return bool success
*/
private function _fetch($id)
@@ -128,10 +128,12 @@ class Minify_Cache_Memcache {
$ret = $this->_mc->get($id);
if (false === $ret) {
$this->_id = null;
return false;
}
list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id;
return true;
}
}

View File

@@ -1,42 +1,42 @@
<?php
/**
* Class Minify_CommentPreserver
* Class Minify_CommentPreserver
* @package Minify
*/
/**
* Process a string in pieces preserving C-style comments that begin with "/*!"
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_CommentPreserver {
/**
* String to be prepended to each preserved comment
*
* @var string
*/
public static $prepend = "\n";
/**
* String to be appended to each preserved comment
*
* @var string
*/
public static $append = "\n";
/**
* Process a string outside of C-style comments that begin with "/*!"
*
* On each non-empty string outside these comments, the given processor
* function will be called. The first "!" will be removed from the
* preserved comments, and the comments will be surrounded by
* On each non-empty string outside these comments, the given processor
* function will be called. The first "!" will be removed from the
* preserved comments, and the comments will be surrounded by
* Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
*
*
* @param string $content
* @param callback $processor function
* @param array $args array of extra arguments to pass to the processor
* @param array $args array of extra arguments to pass to the processor
* function (default = array())
* @return string
*/
@@ -48,7 +48,7 @@ class Minify_CommentPreserver {
if ('' !== $beforeComment) {
$callArgs = $args;
array_unshift($callArgs, $beforeComment);
$ret .= call_user_func_array($processor, $callArgs);
$ret .= call_user_func_array($processor, $callArgs);
}
if (false === $comment) {
break;
@@ -56,17 +56,18 @@ class Minify_CommentPreserver {
$ret .= $comment;
$content = $afterComment;
}
return $ret;
}
/**
* Extract comments that YUI Compressor preserves.
*
*
* @param string $in input
*
*
* @return array 3 elements are returned. If a YUI comment is found, the
* 2nd element is the comment and the 1st and 2nd are the surrounding
* strings. If no comment is found, the entire string is returned as the
* strings. If no comment is found, the entire string is returned as the
* 1st element and the other two are false.
*/
private static function _nextComment($in)
@@ -85,6 +86,7 @@ class Minify_CommentPreserver {
$ret[] = (0 === $endChars)
? ''
: substr($in, -$endChars);
return $ret;
}
}

View File

@@ -1,41 +1,39 @@
<?php
/**
* Class Minify_Controller_Base
* Class Minify_Controller_Base
* @package Minify
*/
/**
* Base class for Minify controller
*
*
* The controller class validates a request and uses it to create sources
* for minification and set options like contentType. It's also responsible
* for loading minifier code upon request.
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*
* @todo add static function to ease setting currentPath for CSS files
* (see line 83 of Version1.php)
*/
abstract class Minify_Controller_Base {
/**
* Setup controller sources
*
* You must override this method in your subclass controller to set
* $this->sources. If the request is NOT valid, make sure $this->sources
* is left an empty array. Then strip any controller-specific options from
* $options and return it.
*
* Setup controller sources and set an needed options for Minify::source
*
* You must override this method in your subclass controller to set
* $this->sources. If the request is NOT valid, make sure $this->sources
* is left an empty array. Then strip any controller-specific options from
* $options and return it. To serve files, $this->sources must be an array of
* Minify_Source objects.
*
* @param array $options controller and Minify options
*
* @param array $options Minify options
*
* return array $options Minify::serve options
*/
abstract public function setupSources($options);
/**
* Get default Minify options for this controller.
*
*
* Override in subclass to change defaults
*
* @return array options for Minify
@@ -47,52 +45,54 @@ abstract class Minify_Controller_Base {
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'UTF-8'
,'contentTypeCharset' => 'utf-8'
,'maxAge' => 1800 // 30 minutes
,'rewriteCssUris' => true
,'bubbleCssImports' => false
,'quiet' => false // serve() will send headers and output
,'debug' => false
// if you override this, the response code MUST be directly after
// if you override this, the response code MUST be directly after
// the first space.
,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
// callback function to see/modify content of all sources
,'postprocessor' => null
// file to require to load preprocessor
,'postprocessorRequire' => null
);
}
}
/**
* Get default minifiers for this controller.
*
*
* Override in subclass to change defaults
*
* @return array minifier callbacks for common types
*/
public function getDefaultMinifers() {
$ret[Minify::TYPE_JS] = array('Minify_Javascript', 'minify');
$ret[Minify::TYPE_JS] = array('JSMin', 'minify');
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
return $ret;
}
/**
* Load any code necessary to execute the given minifier callback.
*
*
* The controller is responsible for loading minification code on demand
* via this method. This built-in function will only load classes for
* static method callbacks where the class isn't already defined. It uses
* the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
* function will include 'Jimmy/Minifier.php'
*
* the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
* function will include 'Jimmy/Minifier.php'.
*
* If you need code loaded on demand and this doesn't suit you, you'll need
* to override this function in your subclass.
* to override this function in your subclass.
* @see Minify_Controller_Page::loadMinifier()
*
*
* @param callback $minifierCallback callback of minifier function
*
*
* @return null
*/
public function loadMinifier($minifierCallback)
@@ -100,20 +100,23 @@ abstract class Minify_Controller_Base {
if (is_array($minifierCallback)
&& is_string($minifierCallback[0])
&& !class_exists($minifierCallback[0], false)) {
require str_replace('_', '/', $minifierCallback[0]) . '.php';
}
}
/**
* Is a user-given file within an allowable directory, existing,
* and having an extension js/css/html/txt
*
* and having an extension js/css/html/txt ?
*
* This is a convenience function for controllers that have to accept
* user-given paths
*
* @param string $file full file path (already processed by realpath())
* @param array $safeDirs directories where files are safe to serve
*
* @param array $safeDirs directories where files are safe to serve. Files can also
* be in subdirectories of these directories.
*
* @return bool file is safe
*/
public static function _fileIsSafe($file, $safeDirs)
@@ -130,22 +133,23 @@ abstract class Minify_Controller_Base {
return false;
}
list($revExt) = explode('.', strrev($base));
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
}
/**
* @var array instances of Minify_Source, which provide content and
* any individual minification needs.
*
*
* @see Minify_Source
*/
public $sources = array();
/**
* Mix in default controller options with user-given options
*
*
* @param array $options user options
*
*
* @return array mixed options
*/
public final function mixInDefaultOptions($options)
@@ -159,18 +163,19 @@ abstract class Minify_Controller_Base {
$ret['minifiers'] = array_merge(
$this->getDefaultMinifers(), $options['minifiers']
);
return $ret;
}
/**
* Analyze sources (if there are any) and set $options 'contentType'
* Analyze sources (if there are any) and set $options 'contentType'
* and 'lastModifiedTime' if they already aren't.
*
*
* @param array $options options for Minify
*
*
* @return array options for Minify
*/
public final function analyzeSources($options = array())
public final function analyzeSources($options = array())
{
if ($this->sources) {
if (! isset($options['contentType'])) {
@@ -183,8 +188,19 @@ abstract class Minify_Controller_Base {
$max = max($source->lastModified, $max);
}
$options['lastModifiedTime'] = $max;
}
}
}
return $options;
}
/**
* Send message to the Minify logger
* @param string $msg
* @return null
*/
protected function log($msg) {
require_once 'Minify/Logger.php';
Minify_Logger::log($msg);
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Controller_Files
* Class Minify_Controller_Files
* @package Minify
*/
@@ -8,7 +8,7 @@ require_once 'Minify/Controller/Base.php';
/**
* Controller class for minifying a set of files
*
*
* E.g. the following would serve the minified Javascript for a site
* <code>
* Minify::serve('Files', array(
@@ -19,7 +19,7 @@ require_once 'Minify/Controller/Base.php';
* )
* ));
* </code>
*
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
@@ -27,22 +27,29 @@ require_once 'Minify/Controller/Base.php';
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Files extends Minify_Controller_Base {
/**
* Set up file sources
*
*
* @param array $options controller and Minify options
* @return array Minify options
*
*
* Controller options:
*
*
* 'files': (required) array of complete file paths, or a single path
*/
public function setupSources($options) {
// strip controller options
$files = (array)$options['files'];
$files = $options['files'];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
unset($options['files']);
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
@@ -52,19 +59,21 @@ class Minify_Controller_Files extends Minify_Controller_Base {
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
'filepath' => $realPath
));
} else {
// file not found
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Controller_Groups
* Class Minify_Controller_Groups
* @package Minify
*/
@@ -9,56 +9,66 @@ require_once 'Minify/Controller/Base.php';
/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
*
*
* <code>
* Minify::serve('Groups', array(
* Minify::serve('Groups', array(
* 'groups' => array(
* 'css' => array('//css/type.css', '//css/layout.css')
* ,'js' => array('//js/jquery.js', '//js/site.js')
* )
* ));
* </code>
*
*
* If the above code were placed in /serve.php, it would enable the URLs
* /serve.php/js and /serve.php/css
*
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Groups extends Minify_Controller_Base {
/**
* Set up groups of files as sources
*
*
* @param array $options controller and Minify options
* @return array Minify options
*
*
* Controller options:
*
*
* 'groups': (required) array mapping PATH_INFO strings to arrays
* of complete file paths. @see Minify_Controller_Groups
* of complete file paths. @see Minify_Controller_Groups
*/
public function setupSources($options) {
// strip controller options
$groups = $options['groups'];
unset($options['groups']);
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO
$pi = isset($_SERVER['ORIG_PATH_INFO'])
? substr($_SERVER['ORIG_PATH_INFO'], 1)
? substr($_SERVER['ORIG_PATH_INFO'], 1)
: (isset($_SERVER['PATH_INFO'])
? substr($_SERVER['PATH_INFO'], 1)
? substr($_SERVER['PATH_INFO'], 1)
: false
);
if (false === $pi || ! isset($groups[$pi])) {
// no PATH_INFO or not a valid group
$this->log("Missing PATH_INFO or no group set for \"$pi\"");
return $options;
}
$sources = array();
foreach ((array)$groups[$pi] as $file) {
$files = $groups[$pi];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
@@ -66,19 +76,21 @@ class Minify_Controller_Groups extends Minify_Controller_Base {
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
'filepath' => $realPath
));
} else {
// file doesn't exist
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Controller_MinApp
* Class Minify_Controller_MinApp
* @package Minify
*/
@@ -8,18 +8,18 @@ require_once 'Minify/Controller/Base.php';
/**
* Controller class for requests to /min/index.php
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_MinApp 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) {
// filter controller options
@@ -28,7 +28,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
'allowDirs' => '//'
,'groupsOnly' => false
,'groups' => array()
,'maxFiles' => 10
,'maxFiles' => 10
)
,(isset($options['minApp']) ? $options['minApp'] : array())
);
@@ -37,9 +37,19 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
if (isset($_GET['g'])) {
// try groups
if (! isset($cOptions['groups'][$_GET['g']])) {
$this->log("A group configuration for \"{$_GET['g']}\" was not set");
return $options;
}
foreach ((array)$cOptions['groups'][$_GET['g']] as $file) {
$files = $cOptions['groups'][$_GET['g']];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
@@ -51,9 +61,10 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
if (is_file($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
));
} else {
// file doesn't exist
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
@@ -61,7 +72,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
// try user files
// 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 (// verify at least one file, files are single comma separated,
if (// verify at least one file, files are single comma separated,
// and are all same extension
! preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'])
// no "//"
@@ -71,11 +82,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
// no "./"
|| preg_match('/(?:^|[^\\.])\\.\\//', $_GET['f'])
) {
$this->log("GET param 'f' invalid (see MinApp.php line 63)");
return $options;
}
$files = explode(',', $_GET['f']);
if (count($files) > $cOptions['maxFiles'] || $files != array_unique($files)) {
// too many or duplicate files
$this->log("Too many or duplicate files specified");
return $options;
}
if (isset($_GET['b'])) {
@@ -84,8 +98,10 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
&& false === strpos($_GET['b'], '..')
&& $_GET['b'] !== '.') {
// valid base
$base = "/{$_GET['b']}/";
$base = "/{$_GET['b']}/";
} else {
$this->log("GET param 'b' invalid (see MinApp.php line 84)");
return $options;
}
} else {
@@ -96,21 +112,29 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
$allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
}
foreach ($files as $file) {
$file = realpath($_SERVER['DOCUMENT_ROOT'] . $base . $file);
// don't allow unsafe or duplicate files
if (parent::_fileIsSafe($file, $allowDirs)) {
$path = $_SERVER['DOCUMENT_ROOT'] . $base . $file;
$file = realpath($path);
if (false === $file) {
$this->log("Path \"{$path}\" failed realpath()");
return $options;
} elseif (! parent::_fileIsSafe($file, $allowDirs)) {
$this->log("Path \"{$path}\" failed Minify_Controller_Base::_fileIsSafe()");
return $options;
} else {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
// unsafe file
return $options;
}
}
}
if ($sources) {
$this->sources = $sources;
} else {
$this->log("No sources to serve");
}
return $options;
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Controller_Page
* Class Minify_Controller_Page
* @package Minify
*/
@@ -8,30 +8,30 @@ require_once 'Minify/Controller/Base.php';
/**
* Controller class for serving a single HTML page
*
*
* @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Page extends Minify_Controller_Base {
/**
* Set up source of HTML content
*
*
* @param array $options controller and Minify options
* @return array Minify options
*
*
* Controller options:
*
*
* 'content': (required) HTML markup
*
*
* 'id': (required) id of page (string for use in server-side caching)
*
*
* 'lastModifiedTime': timestamp of when this content changed. This
* is recommended to allow both server and client-side caching.
*
* 'minifyAll': should all CSS and Javascript blocks be individually
* minified? (default false)
*
* 'minifyAll': should all CSS and Javascript blocks be individually
* minified? (default false)
*
* @todo Add 'file' option to read HTML file.
*/
@@ -52,22 +52,20 @@ class Minify_Controller_Page extends Minify_Controller_Base {
// this will be the 2nd argument passed to Minify_HTML::minify()
$sourceSpec['minifyOptions'] = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
,'jsMinifier' => array('JSMin', 'minify')
);
$this->_loadCssJsMinifiers = true;
unset($options['minifyAll']);
}
$this->sources[] = new Minify_Source($sourceSpec);
// may not be needed
//$options['minifier'] = array('Minify_HTML', 'minify');
$options['contentType'] = Minify::TYPE_HTML;
return $options;
}
protected $_loadCssJsMinifiers = false;
/**
* @see Minify_Controller_Base::loadMinifier()
*/
@@ -76,8 +74,8 @@ class Minify_Controller_Page extends Minify_Controller_Base {
if ($this->_loadCssJsMinifiers) {
// Minify will not call for these so we must manually load
// them when Minify/HTML.php is called for.
require 'Minify/CSS.php';
require 'Minify/Javascript.php';
require_once 'Minify/CSS.php';
require_once 'JSMin.php';
}
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Controller_Version1
* Class Minify_Controller_Version1
* @package Minify
*/
@@ -8,22 +8,22 @@ 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();
@@ -39,7 +39,7 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
// 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,
// 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)
@@ -52,33 +52,33 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
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;
$allowDirs = isset($options['allowDirs'])
? $options['allowDirs']
: MINIFY_BASE_DIR;
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, $allowDirs)
&& !in_array($file, $goodFiles))
if (parent::_fileIsSafe($file, $allowDirs)
&& !in_array($file, $goodFiles))
{
$goodFiles[] = $file;
$srcOptions = array(
@@ -96,9 +96,10 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
if (! MINIFY_REWRITE_CSS_URLS) {
$options['rewriteCssUris'] = false;
}
return $options;
}
private static function _setupDefines()
{
$defaults = array(

View File

@@ -1,23 +1,23 @@
<?php
/**
* Class Minify_HTML
* Class Minify_HTML
* @package Minify
*/
/**
* Compress HTML
*
* This is a heavy regex-based removal of whitespace, unnecessary comments and
* This is a heavy regex-based removal of whitespace, unnecessary comments and
* tokens. IE conditional comments are preserved. There are also options to have
* STYLE and SCRIPT blocks compressed by callback functions.
*
* STYLE and SCRIPT blocks compressed by callback functions.
*
* A test suite is available.
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_HTML {
/**
* "Minify" an HTML page
*
@@ -27,185 +27,222 @@ class Minify_HTML {
*
* 'cssMinifier' : (optional) callback function to process content of STYLE
* elements.
*
*
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
*
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*
*
* @return string
*/
public static function minify($html, $options = array()) {
$min = new Minify_HTML($html, $options);
return $min->process();
}
/**
* Create a minifier object
*
* @param string $html
*
* @param array $options
*
* 'cssMinifier' : (optional) callback function to process content of STYLE
* elements.
*
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*
* @return null
*/
public function __construct($html, $options = array())
{
$this->_html = str_replace("\r\n", "\n", trim($html));
if (isset($options['xhtml'])) {
$this->_isXhtml = (bool)$options['xhtml'];
}
if (isset($options['cssMinifier'])) {
self::$_cssMinifier = $options['cssMinifier'];
$this->_cssMinifier = $options['cssMinifier'];
}
if (isset($options['jsMinifier'])) {
self::$_jsMinifier = $options['jsMinifier'];
$this->_jsMinifier = $options['jsMinifier'];
}
$html = str_replace("\r\n", "\n", trim($html));
self::$_isXhtml = (
isset($options['xhtml'])
? (bool)$options['xhtml']
: (false !== strpos($html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'))
);
self::$_replacementHash = 'MINIFYHTML' . md5(time());
self::$_placeholders = array();
}
/**
* Minify the markeup given in the constructor
*
* @return string
*/
public function process()
{
if ($this->_isXhtml === null) {
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
}
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
$this->_placeholders = array();
// replace SCRIPTs (and minify) with placeholders
$html = preg_replace_callback(
'/\\s*(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>\\s*/i'
,array('Minify_HTML', '_removeScriptCB')
,$html);
$this->_html = preg_replace_callback(
'/(\\s*)(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
,array($this, '_removeScriptCB')
,$this->_html);
// replace STYLEs (and minify) with placeholders
$html = preg_replace_callback(
$this->_html = preg_replace_callback(
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
,array('Minify_HTML', '_removeStyleCB')
,$html);
,array($this, '_removeStyleCB')
,$this->_html);
// remove HTML comments (not containing IE conditional comments).
$html = preg_replace_callback(
$this->_html = preg_replace_callback(
'/<!--([\\s\\S]*?)-->/'
,array('Minify_HTML', '_commentCB')
,$html);
,array($this, '_commentCB')
,$this->_html);
// replace PREs with placeholders
$html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array('Minify_HTML', '_removePreCB')
, $html);
$this->_html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array($this, '_removePreCB')
,$this->_html);
// replace TEXTAREAs with placeholders
$html = preg_replace_callback(
$this->_html = preg_replace_callback(
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array('Minify_HTML', '_removeTaCB')
, $html);
,array($this, '_removeTextareaCB')
,$this->_html);
// trim each line.
// @todo take into account attribute values that span multiple lines.
$html = preg_replace('/^\\s+|\\s+$/m', '', $html);
$this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
// remove ws around block/undisplayed elements
$html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
$this->_html = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body'
.'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form'
.'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta'
.'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)'
.'|ul)\\b[^>]*>)/i', '$1', $html);
.'|ul)\\b[^>]*>)/i', '$1', $this->_html);
// remove ws outside of all elements
$html = preg_replace_callback(
$this->_html = preg_replace_callback(
'/>([^<]+)</'
,array('Minify_HTML', '_outsideTagCB')
,$html);
,array($this, '_outsideTagCB')
,$this->_html);
// use newlines before 1st attribute in open tags (to limit line lengths)
$html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $html);
$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
// fill placeholders
$html = str_replace(
array_keys(self::$_placeholders)
,array_values(self::$_placeholders)
,$html
$this->_html = str_replace(
array_keys($this->_placeholders)
,array_values($this->_placeholders)
,$this->_html
);
self::$_placeholders = array();
self::$_cssMinifier = self::$_jsMinifier = null;
return $html;
return $this->_html;
}
protected static function _commentCB($m)
protected function _commentCB($m)
{
return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
? $m[0]
: '';
}
protected static function _reservePlace($content)
protected function _reservePlace($content)
{
$placeholder = '%' . self::$_replacementHash . count(self::$_placeholders) . '%';
self::$_placeholders[$placeholder] = $content;
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
$this->_placeholders[$placeholder] = $content;
return $placeholder;
}
protected static $_isXhtml = false;
protected static $_replacementHash = null;
protected static $_placeholders = array();
protected static $_cssMinifier = null;
protected static $_jsMinifier = null;
protected $_isXhtml = null;
protected $_replacementHash = null;
protected $_placeholders = array();
protected $_cssMinifier = null;
protected $_jsMinifier = null;
protected static function _outsideTagCB($m)
protected function _outsideTagCB($m)
{
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
}
protected static function _removePreCB($m)
protected function _removePreCB($m)
{
return self::_reservePlace($m[1]);
}
protected static function _removeTaCB($m)
{
return self::_reservePlace($m[1]);
return $this->_reservePlace($m[1]);
}
protected static function _removeStyleCB($m)
protected function _removeTextareaCB($m)
{
return $this->_reservePlace($m[1]);
}
protected function _removeStyleCB($m)
{
$openStyle = $m[1];
$css = $m[2];
// remove HTML comments
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
// remove CDATA section markers
$css = self::_removeCdata($css);
$css = $this->_removeCdata($css);
// minify
$minifier = self::$_cssMinifier
? self::$_cssMinifier
$minifier = $this->_cssMinifier
? $this->_cssMinifier
: 'trim';
$css = call_user_func($minifier, $css);
return self::_reservePlace(self::_needsCdata($css)
return $this->_reservePlace($this->_needsCdata($css)
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
: "{$openStyle}{$css}</style>"
);
}
protected static function _removeScriptCB($m)
protected function _removeScriptCB($m)
{
$openScript = $m[1];
$js = $m[2];
$openScript = $m[2];
$js = $m[3];
// whitespace surrounding? preserve at least one space
$ws1 = ($m[1] === '') ? '' : ' ';
$ws2 = ($m[4] === '') ? '' : ' ';
// remove HTML comments (and ending "//" if present)
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
// remove CDATA section markers
$js = self::_removeCdata($js);
$js = $this->_removeCdata($js);
// minify
$minifier = self::$_jsMinifier
? self::$_jsMinifier
: 'trim';
$minifier = $this->_jsMinifier
? $this->_jsMinifier
: 'trim';
$js = call_user_func($minifier, $js);
return self::_reservePlace(self::_needsCdata($js)
? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>"
: "{$openScript}{$js}</script>"
return $this->_reservePlace($this->_needsCdata($js)
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
);
}
protected static function _removeCdata($str)
protected function _removeCdata($str)
{
return (false !== strpos($str, '<![CDATA['))
? str_replace(array('<![CDATA[', ']]>'), '', $str)
: $str;
}
protected static function _needsCdata($str)
protected function _needsCdata($str)
{
return (self::$_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
}
}

View File

@@ -1,47 +1,48 @@
<?php
/**
* Class Minify_ImportProcessor
* Class Minify_ImportProcessor
* @package Minify
*/
/**
* Linearize a CSS/JS file by including content specified by CSS import
* declarations. In CSS files, relative URIs are fixed.
*
* @imports will be processed regardless of where they appear in the source
*
* @imports will be processed regardless of where they appear in the source
* files; i.e. @imports commented out or in string content will still be
* processed!
*
*
* This has a unit test but should be considered "experimental".
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_ImportProcessor {
public static $filesIncluded = array();
public static function process($file)
{
self::$filesIncluded = array();
self::$_isCss = (strtolower(substr($file, -4)) === '.css');
$obj = new Minify_ImportProcessor(dirname($file));
return $obj->_getContent($file);
}
// allows callback funcs to know the current directory
private $_currentDir = null;
// allows _importCB to write the fetched content back to the obj
private $_importedContent = '';
private static $_isCss = null;
private function __construct($currentDir)
{
$this->_currentDir = $currentDir;
}
private function _getContent($file)
{
$file = realpath($file);
@@ -54,14 +55,14 @@ class Minify_ImportProcessor {
}
self::$filesIncluded[] = realpath($file);
$this->_currentDir = dirname($file);
// remove UTF-8 BOM if present
if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) {
$content = substr($content, 3);
}
// ensure uniform EOLs
$content = str_replace("\r\n", "\n", $content);
// process @imports
$content = preg_replace_callback(
'/
@@ -77,7 +78,7 @@ class Minify_ImportProcessor {
,array($this, '_importCB')
,$content
);
if (self::$_isCss) {
// rewrite remaining relative URIs
$content = preg_replace_callback(
@@ -86,15 +87,15 @@ class Minify_ImportProcessor {
,$content
);
}
return $this->_importedContent . $content;
}
private function _importCB($m)
{
$url = $m[1];
$mediaList = preg_replace('/\\s+/', '', $m[2]);
if (strpos($url, '://') > 0) {
// protocol, leave in place for CSS, comment for JS
return self::$_isCss
@@ -108,7 +109,7 @@ class Minify_ImportProcessor {
. strtr($url, '/', DIRECTORY_SEPARATOR);
} else {
// relative to current path
$file = $this->_currentDir . DIRECTORY_SEPARATOR
$file = $this->_currentDir . DIRECTORY_SEPARATOR
. strtr($url, '/', DIRECTORY_SEPARATOR);
}
$obj = new Minify_ImportProcessor(dirname($file));
@@ -119,11 +120,12 @@ class Minify_ImportProcessor {
? $m[0]
: "/* Minify_ImportProcessor could not fetch '{$file}' */";;
}
return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList))
? $content
: "@media {$mediaList} {\n{$content}\n}\n";
}
private function _urlCB($m)
{
// $m[1] is either quoted or not
@@ -138,20 +140,21 @@ class Minify_ImportProcessor {
// probably starts with protocol, do not alter
} else {
// prepend path with current dir separator (OS-independent)
$path = $this->_currentDir
$path = $this->_currentDir
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
// strip doc root
$path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT'])));
// fix to absolute URL
$url = strtr($path, DIRECTORY_SEPARATOR, '/');
$url = strtr($path, '/\\', '//');
// remove /./ and /../ where possible
$url = str_replace('/./', '/', $url);
// inspired by patch from Oleg Cherniy
do {
$url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, -1, $changed);
$url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, 1, $changed);
} while ($changed);
}
}
return "url({$quote}{$url}{$quote})";
}
}

View File

@@ -1,31 +0,0 @@
<?php
/**
* Class Minify_Javascript
* @package Minify
*/
require 'JSMin.php';
/**
* Compress Javascript using Ryan Grove's JSMin class
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Javascript {
/**
* Minify a Javascript string
*
* @param string $js
*
* @param array $options available options (none currently)
*
* @return string
*/
public static function minify($js, $options = array())
{
return trim(JSMin::minify($js));
}
}

View File

@@ -1,6 +1,6 @@
<?php
/**
* Class Minify_Lines
* Class Minify_Lines
* @package Minify
*/
@@ -9,6 +9,7 @@
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Adam Pedersen (Issue 55 fix)
*/
class Minify_Lines {
@@ -16,18 +17,24 @@ class Minify_Lines {
* Add line numbers in C-style comments
*
* This uses a very basic parser easily fooled by comment tokens inside
* strings or regexes, but, otherwise, generally clean code will not be
* mangled.
*
* strings or regexes, but, otherwise, generally clean code will not be
* mangled. URI rewriting can also be performed.
*
* @param string $content
*
*
* @param array $options available options:
*
*
* 'id': (optional) string to identify file. E.g. file name/path
*
* @return string
*
* 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to
* the desired files, and prepend a comment with debugging information about
* this process.
*
* @return string
*/
public static function minify($content, $options = array())
public static function minify($content, $options = array())
{
$id = (isset($options['id']) && $options['id'])
? $options['id']
@@ -48,17 +55,34 @@ class Minify_Lines {
$newLines[] = self::_addNote($line, $i, $inComment, $padTo);
$inComment = self::_eolInComment($line, $inComment);
}
return implode("\n", $newLines) . "\n";
$content = implode("\n", $newLines) . "\n";
// check for desired URI rewriting
if (isset($options['currentDir'])) {
require_once 'Minify/CSS/UriRewriter.php';
Minify_CSS_UriRewriter::$debugText = '';
$content = Minify_CSS_UriRewriter::rewrite(
$content
,$options['currentDir']
,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
,isset($options['symlinks']) ? $options['symlinks'] : array()
);
$content = "/* Minify_CSS_UriRewriter::\$debugText\n\n"
. Minify_CSS_UriRewriter::$debugText . "*/\n"
. $content;
}
return $content;
}
/**
* Is the parser within a C-style comment at the end of this line?
*
* @param string $line current line of code
*
*
* @param bool $inComment was the parser in a comment at the
* beginning of the line?
*
*
* @return bool
*/
private static function _eolInComment($line, $inComment)
@@ -71,25 +95,32 @@ class Minify_Lines {
if (false === $pos) {
return $inComment;
} else {
$inComment = ! $inComment;
if ($pos == 0
|| ($inComment
? substr($line, $pos, 3)
: substr($line, $pos-1, 3)) != '*/*')
{
$inComment = ! $inComment;
}
$line = substr($line, $pos + 2);
}
}
return $inComment;
}
/**
* Prepend a comment (or note) to the given line
*
* @param string $line current line of code
*
* @param string $note content of note/comment
*
*
* @param bool $inComment was the parser in a comment at the
* beginning of the line?
*
* @param int $padTo minimum width of comment
*
*
* @return string
*/
private static function _addNote($line, $note, $inComment, $padTo)

View File

@@ -0,0 +1,45 @@
<?php
/**
* Class Minify_Logger
* @package Minify
*/
/**
* Message logging class
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Logger {
/**
* Set logger object.
*
* The object should have a method "log" that accepts a value as 1st argument and
* an optional string label as the 2nd.
*
* @param mixed $obj or a "falsey" value to disable
* @return null
*/
public static function setLogger($obj = null) {
self::$_logger = $obj
? $obj
: null;
}
/**
* Pass a message to the logger (if set)
*
* @param string $msg message to log
* @return null
*/
public static function log($msg, $label = 'Minify') {
if (! self::$_logger) return;
self::$_logger->log($msg, $label);
}
/**
* @var mixed logger object (like FirePHP) or null (i.e. no logger available)
*/
private static $_logger = null;
}

View File

@@ -4,21 +4,27 @@
*
* To use this class you must first download the PHP port of Packer
* and place the file "class.JavaScriptPacker.php" in /lib (or your
* include_path).
* include_path).
* @link http://joliclic.free.fr/php/javascript-packer/en/
*
* Be aware that, as long as HTTP encoding is used, scripts minified
* with Minify_Javascript (JSMin) will provide better client-side
* performance, as they need not be unpacked in client-side code.
*
* @package Minify
* Be aware that, as long as HTTP encoding is used, scripts minified with JSMin
* will provide better client-side performance, as they need not be unpacked in
* client-side code.
*
* @package Minify
*/
require 'class.JavaScriptPacker.php';
if (false === (@include 'class.JavaScriptPacker.php')) {
trigger_error(
'The script "class.JavaScriptPacker.php" is required. Please see: http:'
.'//code.google.com/p/minify/source/browse/trunk/min/lib/Minify/Packer.php'
,E_USER_ERROR
);
}
/**
* Minify Javascript using Dean Edward's Packer
*
*
* @package Minify
*/
class Minify_Packer {
@@ -26,6 +32,7 @@ class Minify_Packer {
{
// @todo: set encoding options based on $options :)
$packer = new JavascriptPacker($code, 'Normal', true, false);
return trim($packer->pack());
}
}

View File

@@ -1,15 +1,15 @@
<?php
/**
* Class Minify_Source
* Class Minify_Source
* @package Minify
*/
/**
* A content source to be minified by Minify.
*
/**
* A content source to be minified by Minify.
*
* This allows per-source minification options and the mixing of files with
* content from other sources.
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
@@ -19,12 +19,12 @@ class Minify_Source {
* @var int time of last modification
*/
public $lastModified = null;
/**
* @var callback minifier function specifically for this source.
*/
public $minifier = null;
/**
* @var array minification options specific to this source.
*/
@@ -34,15 +34,20 @@ class Minify_Source {
* @var string full path of file
*/
public $filepath = null;
/**
* @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
*/
public $contentType = null;
/**
* Create a Minify_Source
*
*
* In the $spec array(), you can either provide a 'filepath' to an existing
* file (existence will not be checked!) or give 'id' (unique string for
* the content), 'content' (the string content) and 'lastModified'
* file (existence will not be checked!) or give 'id' (unique string for
* the content), 'content' (the string content) and 'lastModified'
* (unixtime of last update).
*
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
@@ -54,6 +59,17 @@ class Minify_Source {
if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
}
$segments = explode('.', $spec['filepath']);
$ext = strtolower(array_pop($segments));
switch ($ext) {
case 'js' : $this->contentType = 'application/x-javascript';
break;
case 'css' : $this->contentType = 'text/css';
break;
case 'htm' : // fallthrough
case 'html' : $this->contentType = 'text/html';
break;
}
$this->filepath = $spec['filepath'];
$this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
@@ -70,6 +86,9 @@ class Minify_Source {
? $spec['lastModified']
: time();
}
if (isset($spec['contentType'])) {
$this->contentType = $spec['contentType'];
}
if (isset($spec['minifier'])) {
$this->minifier = $spec['minifier'];
}
@@ -77,7 +96,7 @@ class Minify_Source {
$this->minifyOptions = $spec['minifyOptions'];
}
}
/**
* Get content
*
@@ -96,7 +115,7 @@ class Minify_Source {
? substr($content, 3)
: $content;
}
/**
* Get id
*
@@ -106,12 +125,12 @@ class Minify_Source {
{
return $this->_id;
}
/**
* Verifies a single minification call can handle all sources
*
* @param array $sources Minify_Source instances
*
*
* @return bool true iff there no sources with specific minifier preferences.
*/
public static function haveNoMinifyPrefs($sources)
@@ -122,14 +141,15 @@ class Minify_Source {
return false;
}
}
return true;
}
/**
* Get unique string for a set of sources
*
* @param array $sources Minify_Source instances
*
*
* @return string
*/
public static function getDigest($sources)
@@ -139,37 +159,30 @@ class Minify_Source {
$source->_id, $source->minifier, $source->minifyOptions
);
}
return md5(serialize($info));
}
/**
* Guess content type from the first filename extension available
*
* This is called if the user doesn't pass in a 'contentType' options
*
* Get content type from a group of sources
*
* This is called if the user doesn't pass in a 'contentType' options
*
* @param array $sources Minify_Source instances
*
*
* @return string content type. e.g. 'text/css'
*/
public static function getContentType($sources)
{
$exts = array(
'css' => Minify::TYPE_CSS
,'js' => Minify::TYPE_JS
,'html' => Minify::TYPE_HTML
);
foreach ($sources as $source) {
if (null !== $source->filepath) {
$segments = explode('.', $source->filepath);
$ext = array_pop($segments);
if (isset($exts[$ext])) {
return $exts[$ext];
}
if ($source->contentType !== null) {
return $source->contentType;
}
}
return 'text/plain';
}
protected $_content = null;
protected $_getContentFunc = null;
protected $_id = null;

View File

@@ -1,17 +1,17 @@
<?php
/**
* Class Minify_YUICompressor
* Class Minify_YUICompressor
* @package Minify
*/
/**
* Compress Javascript/CSS using the YUI Compressor
*
*
* You must set $jarFile and $tempDir before calling the minify functions.
* Also, depending on your shell's environment, you may need to specify
* the full path to java in $javaExecutable or use putenv() to setup the
* Java environment.
*
*
* <code>
* Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar';
* Minify_YUICompressor::$tempDir = '/tmp';
@@ -20,9 +20,9 @@
* ,array('nomunge' => true, 'line-break' => 1000)
* );
* </code>
*
*
* @todo unit tests, $options docs
*
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
@@ -35,7 +35,7 @@ class Minify_YUICompressor {
* @var string
*/
public static $jarFile = null;
/**
* Writable temp directory. This must be set before calling minifyJs()
* or minifyCss().
@@ -43,46 +43,46 @@ class Minify_YUICompressor {
* @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 http://www.julienlecomte.net/yuicompressor/README
*
* @return string
*
* @return string
*/
public static function minifyJs($js, $options = array())
{
return self::_minify('js', $js, $options);
}
/**
* Minify a CSS string
*
*
* @param string $css
*
*
* @param array $options (verbose is ignored)
*
*
* @see http://www.julienlecomte.net/yuicompressor/README
*
* @return string
*
* @return string
*/
public static function minifyCss($css, $options = array())
{
return self::_minify('css', $css, $options);
}
private static function _minify($type, $content, $options)
{
self::_prepare();
@@ -92,9 +92,10 @@ class Minify_YUICompressor {
file_put_contents($tmpFile, $content);
exec(self::_getCmd($options, $type, $tmpFile), $output);
unlink($tmpFile);
return implode("\n", $output);
}
private static function _getCmd($userOptions, $type, $tmpFile)
{
$o = array_merge(
@@ -110,25 +111,26 @@ class Minify_YUICompressor {
);
$cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
. " --type {$type}"
. (preg_match('/^[a-zA-Z\\-]+$/', $o['charset'])
? " --charset {$o['charset']}"
. (preg_match('/^[a-zA-Z\\-]+$/', $o['charset'])
? " --charset {$o['charset']}"
: '')
. (is_numeric($o['line-break']) && $o['line-break'] >= 0
? ' --line-break ' . (int)$o['line-break']
: '');
if ($type === 'js') {
foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
$cmd .= $o[$opt]
$cmd .= $o[$opt]
? " --{$opt}"
: '';
}
}
return $cmd . ' ' . escapeshellarg($tmpFile);
}
private static function _prepare()
{
if (! is_file(self::$jarFile)
if (! is_file(self::$jarFile)
|| ! is_dir(self::$tempDir)
|| ! is_writable(self::$tempDir)
) {