1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-16 02:54:33 +02:00

All PHP/doc files have Unix newlines w/ no BOM

This commit is contained in:
Steve Clay
2008-08-29 22:56:34 +00:00
parent 5a09b4943a
commit 79a4e17fb6
48 changed files with 3999 additions and 3998 deletions

50
HISTORY
View File

@@ -1,26 +1,26 @@
Minify Release History
Version 2.0.2 beta (2008-06-24)
* Fast new cache system. Cached files served almost 3x as fast.
* Dropped support of compress encoding (though HTTP_Encoder still supports it)
Version 2.0.1 (2008-05-31)
* E_STRICT compliance (Cache_Lite_File).
Version 2.0.0 (2008-05-22)
* Complete code overhaul. Minify is now a PEAR-style class and toolkit
for building customized minifying file servers.
* Content-Encoding: deflate/gzip/compress, based on request headers
* Expanded CSS and HTML minifiers with test cases
* Easily plug-in 3rd-party minifiers (like Packer)
* Plug-able front end controller allows changing the way files are chosen
* Compression & encoding modules lazy-loaded as needed (304 responses use minimal code)
* Separate utility classes for HTTP encoding and cache control
Version 1.0.1 (2007-05-05)
* Fixed various problems resolving pathnames when hosted on an NFS mount.
* Fixed 'undefined constant' notice.
* Replaced old JSMin library with a much faster custom implementation.
Version 1.0.0 (2007-05-02)
Minify Release History
Version 2.0.2 beta (2008-06-24)
* Fast new cache system. Cached files served almost 3x as fast.
* Dropped support of compress encoding (though HTTP_Encoder still supports it)
Version 2.0.1 (2008-05-31)
* E_STRICT compliance (Cache_Lite_File).
Version 2.0.0 (2008-05-22)
* Complete code overhaul. Minify is now a PEAR-style class and toolkit
for building customized minifying file servers.
* Content-Encoding: deflate/gzip/compress, based on request headers
* Expanded CSS and HTML minifiers with test cases
* Easily plug-in 3rd-party minifiers (like Packer)
* Plug-able front end controller allows changing the way files are chosen
* Compression & encoding modules lazy-loaded as needed (304 responses use minimal code)
* Separate utility classes for HTTP encoding and cache control
Version 1.0.1 (2007-05-05)
* Fixed various problems resolving pathnames when hosted on an NFS mount.
* Fixed 'undefined constant' notice.
* Replaced old JSMin library with a much faster custom implementation.
Version 1.0.0 (2007-05-02)
* First release.

50
LICENSE
View File

@@ -1,25 +1,25 @@
Copyright (c) 2007 Ryan Grove <ryan@wonko.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of this project nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Copyright (c) 2007 Ryan Grove <ryan@wonko.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of this project nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

92
README
View File

@@ -1,46 +1,46 @@
WELCOME TO MINIFY 2.0!
Minify is an HTTP content server. It compresses sources of content
(usually files), combines the result and serves it with appropriate
HTTP headers. These headers can allow clients to perform conditional
GETs (serving content only when clients do not have a valid cache)
or tell clients to cache the file until a given date/time.
More info: http://code.google.com/p/minify/
INSTALLATION AND USAGE: http://code.google.com/p/minify/wiki/UserGuide
UNIT TESTING & EXAMPLES: http://code.google.com/p/minify/wiki/TestingMinify
MINIFY OVERVIEW
Minify works with Minify_Source objects. These usually represent a file
on disk, but a source can also be a string in memory or from a database.
Sources with known "last modified" timestamps allow Minify to implement
server-side caching and conditional GETs.
You configure Minify via a Minify_Controller object. The controller
supplies the sources and default options to serve a request,
determining how it's to be responded to. Several controllers are
supplied with Minify, but it's also fairly easy to write your own. See
the files in /lib/Minify/Controller for examples.
To use an existing controller, you call Minify::serve(), passing it:
1. the name of your controller of choice (without the "Minify_Controller"
prefix) or a custom controller object subclassed from Minify_Controller_Base.
2. a combined array of controller and Minify options. Eg.:
// serve a minified javascript file and tell clients to cache it for a
// year
Minify::serve('Files', array(
'files' => array('/path/to/file1.js', '/path/to/file2.js')
,'setExpires' => (time() + 86400 * 365)
));
The above creates an instance of Minify_Controller_Files, which creates
source objects from the 'files' option, and supplies other default options.
FILE ENCODINGS
Minify *should* work fine with files encoded in UTF-8 or other 8-bit
encodings like ISO 8859/Windows-1252. Leading UTF-8 BOMs are stripped from
all sources to prevent duplication in output files.
WELCOME TO MINIFY 2.0!
Minify is an HTTP content server. It compresses sources of content
(usually files), combines the result and serves it with appropriate
HTTP headers. These headers can allow clients to perform conditional
GETs (serving content only when clients do not have a valid cache)
or tell clients to cache the file until a given date/time.
More info: http://code.google.com/p/minify/
INSTALLATION AND USAGE: http://code.google.com/p/minify/wiki/UserGuide
UNIT TESTING & EXAMPLES: http://code.google.com/p/minify/wiki/TestingMinify
MINIFY OVERVIEW
Minify works with Minify_Source objects. These usually represent a file
on disk, but a source can also be a string in memory or from a database.
Sources with known "last modified" timestamps allow Minify to implement
server-side caching and conditional GETs.
You configure Minify via a Minify_Controller object. The controller
supplies the sources and default options to serve a request,
determining how it's to be responded to. Several controllers are
supplied with Minify, but it's also fairly easy to write your own. See
the files in /lib/Minify/Controller for examples.
To use an existing controller, you call Minify::serve(), passing it:
1. the name of your controller of choice (without the "Minify_Controller"
prefix) or a custom controller object subclassed from Minify_Controller_Base.
2. a combined array of controller and Minify options. Eg.:
// serve a minified javascript file and tell clients to cache it for a
// year
Minify::serve('Files', array(
'files' => array('/path/to/file1.js', '/path/to/file2.js')
,'setExpires' => (time() + 86400 * 365)
));
The above creates an instance of Minify_Controller_Files, which creates
source objects from the 'files' option, and supplies other default options.
FILE ENCODINGS
Minify *should* work fine with files encoded in UTF-8 or other 8-bit
encodings like ISO 8859/Windows-1252. Leading UTF-8 BOMs are stripped from
all sources to prevent duplication in output files.

View File

@@ -1,16 +1,16 @@
<?php
/**
* Configuration for default Minify implementation
* @package Minify
*/
/**
* For best performance, specify your temp directory here.
* Otherwise Minify will have to load extra code to guess.
*/
//$min_cachePath = 'c:\\WINDOWS\Temp';
//$min_cachePath = '/tmp';
<?php
/**
* Configuration for default Minify implementation
* @package Minify
*/
/**
* For best performance, specify your temp directory here.
* Otherwise Minify will have to load extra code to guess.
*/
//$min_cachePath = 'c:\\WINDOWS\Temp';
//$min_cachePath = '/tmp';
/**
@@ -20,29 +20,29 @@
* those changes are seen immediately.
*/
$min_serveOptions['maxAge'] = 1800;
/**
* If you'd like to restrict the "f" option to files within/below
* particular directories below DOCUMENT_ROOT, set this here.
* You will still need to include the directory in the
* f or b GET parameters.
*
* // = DOCUMENT_ROOT
*/
//$min_allowDirs = array('//js', '//css');
/**
* Manually set the path to Minify's lib folder
*/
//$min_libPath = 'lib';
/**
* Set to true to disable the "f" GET parameter for specifying files.
* Only the "g" parameter will be considered.
*/
/**
* If you'd like to restrict the "f" option to files within/below
* particular directories below DOCUMENT_ROOT, set this here.
* You will still need to include the directory in the
* f or b GET parameters.
*
* // = DOCUMENT_ROOT
*/
//$min_allowDirs = array('//js', '//css');
/**
* Manually set the path to Minify's lib folder
*/
//$min_libPath = 'lib';
/**
* Set to true to disable the "f" GET parameter for specifying files.
* Only the "g" parameter will be considered.
*/
$min_groupsOnly = false;
@@ -50,7 +50,7 @@ $min_groupsOnly = false;
* Uncomment to enable debug mode. Files will be combined with no
* minification, and comments will be added to indicate the line #s
* of the original files.
*/
*/
//$min_serveOptions['debug'] = true;

View File

@@ -1,11 +1,11 @@
<?php
/**
* Groups configuration for default Minify implementation
* @package Minify
*/
return array(
// 'js' => array('//js/file1.js', '//js/file2.js'),
// 'css' => array('//css/file1.css', '//css/file2.css'),
'js' => array('//js/Class.js', '//js/email.js')
<?php
/**
* Groups configuration for default Minify implementation
* @package Minify
*/
return array(
// 'js' => array('//js/file1.js', '//js/file2.js'),
// 'css' => array('//css/file1.css', '//css/file2.css'),
'js' => array('//js/Class.js', '//js/email.js')
);

View File

@@ -1,55 +1,55 @@
<?php
/**
* Front controller for default Minify implementation
*
* DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
*
* @package Minify
*/
define('MINIFY_MIN_DIR', dirname(__FILE__));
// load config
require MINIFY_MIN_DIR . '/config.php';
// setup include path
if (!isset($min_libPath)) {
// default lib path is inside this directory
$min_libPath = MINIFY_MIN_DIR . '/lib';
}
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
// friendly error if lib wasn't in the include_path
if (! (include 'Minify.php')) {
trigger_error(
'Minify: You must add Minify/lib to the include_path or set $min_libPath in config.php'
,E_USER_ERROR
);
<?php
/**
* Front controller for default Minify implementation
*
* DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
*
* @package Minify
*/
define('MINIFY_MIN_DIR', dirname(__FILE__));
// load config
require MINIFY_MIN_DIR . '/config.php';
// setup include path
if (!isset($min_libPath)) {
// default lib path is inside this directory
$min_libPath = MINIFY_MIN_DIR . '/lib';
}
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());
// friendly error if lib wasn't in the include_path
if (! (include 'Minify.php')) {
trigger_error(
'Minify: You must add Minify/lib to the include_path or set $min_libPath in config.php'
,E_USER_ERROR
);
}
Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
if (isset($_GET['g'])) {
Minify::setCache(isset($min_cachePath) ? $min_cachePath : null);
// Groups expects the group key as PATH_INFO
// we want to allow ?g=groupKey
$_SERVER['PATH_INFO'] = '/' . $_GET['g'];
$min_serveOptions['groups'] = (require MINIFY_MIN_DIR . '/groupsConfig.php');
if (preg_match('/&\\d/', $_SERVER['QUERY_STRING'])) {
$min_serveOptions['maxAge'] = 31536000;
}
Minify::serve('Groups', $min_serveOptions);
} elseif (!$min_groupsOnly && isset($_GET['f'])) {
/**
* crude initial implementation hacked onto on Version1 controller
* @todo encapsulate this in a new controller
*/
if (isset($min_cachePath)) {
define('MINIFY_CACHE_DIR', $min_cachePath);
if (isset($_GET['g'])) {
Minify::setCache(isset($min_cachePath) ? $min_cachePath : null);
// Groups expects the group key as PATH_INFO
// we want to allow ?g=groupKey
$_SERVER['PATH_INFO'] = '/' . $_GET['g'];
$min_serveOptions['groups'] = (require MINIFY_MIN_DIR . '/groupsConfig.php');
if (preg_match('/&\\d/', $_SERVER['QUERY_STRING'])) {
$min_serveOptions['maxAge'] = 31536000;
}
Minify::serve('Groups', $min_serveOptions);
} elseif (!$min_groupsOnly && isset($_GET['f'])) {
/**
* crude initial implementation hacked onto on Version1 controller
* @todo encapsulate this in a new controller
*/
if (isset($min_cachePath)) {
define('MINIFY_CACHE_DIR', $min_cachePath);
}
if (isset($min_allowDirs)) {
foreach ((array)$min_allowDirs as $_allowDir) {
@@ -57,16 +57,16 @@ if (isset($_GET['g'])) {
$_SERVER['DOCUMENT_ROOT'] . substr($_allowDir, 1)
);
}
}
// Version1 already does validation. All we want is to prepend "b"
// to each file if it looks right.
$min_base = (isset($_GET['b']) && preg_match('@^[^/.]+(?:/[^/.]+)*$@', $_GET['b']))
? '/' . $_GET['b'] . '/'
: '/';
// Version1 expects ?files=/js/file1.js,/js/file2.js,/js/file3.js
// we want to allow ?f=js/file1.js,js/file2.js,js/file3.js
// or ?b=js&f=file1.js,file2.js,file3.js
$_GET['files'] = $min_base . str_replace(',', ',' . $min_base, $_GET['f']);
Minify::serve('Version1', $min_serveOptions);
}
}
// Version1 already does validation. All we want is to prepend "b"
// to each file if it looks right.
$min_base = (isset($_GET['b']) && preg_match('@^[^/.]+(?:/[^/.]+)*$@', $_GET['b']))
? '/' . $_GET['b'] . '/'
: '/';
// Version1 expects ?files=/js/file1.js,/js/file2.js,/js/file3.js
// we want to allow ?f=js/file1.js,js/file2.js,js/file3.js
// or ?b=js&f=file1.js,file2.js,file3.js
$_GET['files'] = $min_base . str_replace(',', ',' . $min_base, $_GET['f']);
Minify::serve('Version1', $min_serveOptions);
}

View File

@@ -1,262 +1,262 @@
<?php
/**
* Class HTTP_ConditionalGet
* @package Minify
* @subpackage HTTP
*/
/**
* Implement conditional GET via a timestamp or hash of content
*
* E.g. Content from DB with update time:
* <code>
* list($updateTime, $content) = getDbUpdateAndContent();
* $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => $updateTime
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Content from DB with no update time:
* <code>
* $content = getContentFromDB();
* $cg = new HTTP_ConditionalGet(array(
* 'contentHash' => md5($content)
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Static content with some static includes:
* <code>
* // before content
* $cg = new HTTP_ConditionalGet(array(
* 'lastUpdateTime' => max(
* filemtime(__FILE__)
* ,filemtime('/path/to/header.inc')
* ,filemtime('/path/to/footer.inc')
* )
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* </code>
* @package Minify
* @subpackage HTTP
* @author Stephen Clay <steve@mrclay.org>
*/
class HTTP_ConditionalGet {
/**
* Does the client have a valid copy of the requested resource?
*
* You'll want to check this after instantiating the object. If true, do
* not send content, just call sendHeaders() if you haven't already.
*
* @var bool
*/
public $cacheIsValid = null;
/**
* @param array $spec options
*
* 'isPublic': (bool) if true, the Cache-Control header will contain
* "public", allowing proxy caches to cache the content. Otherwise
* "private" will be sent, allowing only browsers to cache. (default false)
*
* 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended.
*
* 'contentHash': (string) if given, only the ETag header can be sent with
* content (only HTTP1.1 clients can conditionally GET). The given string
* should be short with no quote characters and always change when the
* resource changes (recommend md5()). This is not needed/used if
* lastModifiedTime is given.
*
* 'invalidate': (bool) if true, the client cache will be considered invalid
* without testing. Effectively this disables conditional GET.
* (default false)
*
* 'maxAge': (int) if given, this will set the Cache-Control max-age in
* seconds, and also set the Expires header to the equivalent GMT date.
* After the max-age period has passed, the browser will again send a
* conditional GET to revalidate its cache.
*
* @return null
*/
public function __construct($spec) {
$scope = (isset($spec['isPublic']) && $spec['isPublic'])
? 'public'
: 'private';
$maxAge = 0;
// backwards compatibility (can be removed later)
if (isset($spec['setExpires'])
&& is_numeric($spec['setExpires'])
&& ! isset($spec['maxAge'])) {
$spec['maxAge'] = $spec['setExpires'] - $_SERVER['REQUEST_TIME'];
}
if (isset($spec['maxAge'])) {
$maxAge = $spec['maxAge'];
$this->_headers['Expires'] = self::gmtDate(
$_SERVER['REQUEST_TIME'] + $spec['maxAge']
);
}
if (isset($spec['lastModifiedTime'])) {
// base both headers on time
$this->_setLastModified($spec['lastModifiedTime']);
$this->_setEtag($spec['lastModifiedTime'], $scope);
} else {
// hope to use ETag
if (isset($spec['contentHash'])) {
$this->_setEtag($spec['contentHash'], $scope);
}
}
$this->_headers['Cache-Control'] = "max-age={$maxAge}, {$scope}, must-revalidate";
// invalidate cache if disabled, otherwise check
$this->cacheIsValid = (isset($spec['invalidate']) && $spec['invalidate'])
? false
: $this->_isCacheValid();
}
/**
* Get array of output headers to be sent
*
* In the case of 304 responses, this array will only contain the response
* code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified')
*
* Otherwise something like:
* <code>
* array(
* 'Cache-Control' => 'max-age=0, public, must-revalidate'
* ,'ETag' => '"foobar"'
* )
* </code>
*
* @return array
*/
public function getHeaders() {
return $this->_headers;
}
/**
* Set the Content-Length header in bytes
*
* With most PHP configs, as long as you don't flush() output, this method
* is not needed and PHP will buffer all output and set Content-Length for
* you. Otherwise you'll want to call this to let the client know up front.
*
* @param int $bytes
*
* @return int copy of input $bytes
*/
public function setContentLength($bytes) {
return $this->_headers['Content-Length'] = $bytes;
}
/**
* Send headers
*
* @see getHeaders()
*
* Note this doesn't "clear" the headers. Calling sendHeaders() will
* call header() again (but probably have not effect) and getHeaders() will
* still return the headers.
*
* @return null
*/
public function sendHeaders() {
$headers = $this->_headers;
if (array_key_exists('_responseCode', $headers)) {
header($headers['_responseCode']);
unset($headers['_responseCode']);
}
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
}
/**
* Get a GMT formatted date for use in HTTP headers
*
* <code>
* header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
* </code>
*
* @param int $time unix timestamp
*
* @return string
*/
public static function gmtDate($time) {
return gmdate('D, d M Y H:i:s \G\M\T', $time);
}
protected $_headers = array();
protected $_lmTime = null;
protected $_etag = null;
protected function _setEtag($hash, $scope) {
$this->_etag = '"' . $hash
. substr($scope, 0, 3)
. '"';
$this->_headers['ETag'] = $this->_etag;
}
protected function _setLastModified($time) {
$this->_lmTime = (int)$time;
$this->_headers['Last-Modified'] = self::gmtDate($time);
}
/**
* Determine validity of client cache and queue 304 header if valid
*/
protected function _isCacheValid()
{
if (null === $this->_etag) {
// lmTime is copied to ETag, so this condition implies that the
// client received neither ETag nor Last-Modified, so can't
// possibly has a valid cache.
return false;
}
$isValid = ($this->resourceMatchedEtag() || $this->resourceNotModified());
if ($isValid) {
$this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified';
}
return $isValid;
}
protected function resourceMatchedEtag() {
if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
return false;
}
$cachedEtagList = get_magic_quotes_gpc()
? stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])
: $_SERVER['HTTP_IF_NONE_MATCH'];
$cachedEtags = split(',', $cachedEtagList);
foreach ($cachedEtags as $cachedEtag) {
if (trim($cachedEtag) == $this->_etag) {
return true;
}
}
return false;
}
protected function resourceNotModified() {
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
return false;
}
$ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
if (false !== ($semicolon = strrpos($ifModifiedSince, ';'))) {
// IE has tacked on extra data to this header, strip it
$ifModifiedSince = substr($ifModifiedSince, 0, $semicolon);
}
return ($ifModifiedSince == self::gmtDate($this->_lmTime));
}
}
<?php
/**
* Class HTTP_ConditionalGet
* @package Minify
* @subpackage HTTP
*/
/**
* Implement conditional GET via a timestamp or hash of content
*
* E.g. Content from DB with update time:
* <code>
* list($updateTime, $content) = getDbUpdateAndContent();
* $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => $updateTime
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Content from DB with no update time:
* <code>
* $content = getContentFromDB();
* $cg = new HTTP_ConditionalGet(array(
* 'contentHash' => md5($content)
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Static content with some static includes:
* <code>
* // before content
* $cg = new HTTP_ConditionalGet(array(
* 'lastUpdateTime' => max(
* filemtime(__FILE__)
* ,filemtime('/path/to/header.inc')
* ,filemtime('/path/to/footer.inc')
* )
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* </code>
* @package Minify
* @subpackage HTTP
* @author Stephen Clay <steve@mrclay.org>
*/
class HTTP_ConditionalGet {
/**
* Does the client have a valid copy of the requested resource?
*
* You'll want to check this after instantiating the object. If true, do
* not send content, just call sendHeaders() if you haven't already.
*
* @var bool
*/
public $cacheIsValid = null;
/**
* @param array $spec options
*
* 'isPublic': (bool) if true, the Cache-Control header will contain
* "public", allowing proxy caches to cache the content. Otherwise
* "private" will be sent, allowing only browsers to cache. (default false)
*
* 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended.
*
* 'contentHash': (string) if given, only the ETag header can be sent with
* content (only HTTP1.1 clients can conditionally GET). The given string
* should be short with no quote characters and always change when the
* resource changes (recommend md5()). This is not needed/used if
* lastModifiedTime is given.
*
* 'invalidate': (bool) if true, the client cache will be considered invalid
* without testing. Effectively this disables conditional GET.
* (default false)
*
* 'maxAge': (int) if given, this will set the Cache-Control max-age in
* seconds, and also set the Expires header to the equivalent GMT date.
* After the max-age period has passed, the browser will again send a
* conditional GET to revalidate its cache.
*
* @return null
*/
public function __construct($spec) {
$scope = (isset($spec['isPublic']) && $spec['isPublic'])
? 'public'
: 'private';
$maxAge = 0;
// backwards compatibility (can be removed later)
if (isset($spec['setExpires'])
&& is_numeric($spec['setExpires'])
&& ! isset($spec['maxAge'])) {
$spec['maxAge'] = $spec['setExpires'] - $_SERVER['REQUEST_TIME'];
}
if (isset($spec['maxAge'])) {
$maxAge = $spec['maxAge'];
$this->_headers['Expires'] = self::gmtDate(
$_SERVER['REQUEST_TIME'] + $spec['maxAge']
);
}
if (isset($spec['lastModifiedTime'])) {
// base both headers on time
$this->_setLastModified($spec['lastModifiedTime']);
$this->_setEtag($spec['lastModifiedTime'], $scope);
} else {
// hope to use ETag
if (isset($spec['contentHash'])) {
$this->_setEtag($spec['contentHash'], $scope);
}
}
$this->_headers['Cache-Control'] = "max-age={$maxAge}, {$scope}, must-revalidate";
// invalidate cache if disabled, otherwise check
$this->cacheIsValid = (isset($spec['invalidate']) && $spec['invalidate'])
? false
: $this->_isCacheValid();
}
/**
* Get array of output headers to be sent
*
* In the case of 304 responses, this array will only contain the response
* code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified')
*
* Otherwise something like:
* <code>
* array(
* 'Cache-Control' => 'max-age=0, public, must-revalidate'
* ,'ETag' => '"foobar"'
* )
* </code>
*
* @return array
*/
public function getHeaders() {
return $this->_headers;
}
/**
* Set the Content-Length header in bytes
*
* With most PHP configs, as long as you don't flush() output, this method
* is not needed and PHP will buffer all output and set Content-Length for
* you. Otherwise you'll want to call this to let the client know up front.
*
* @param int $bytes
*
* @return int copy of input $bytes
*/
public function setContentLength($bytes) {
return $this->_headers['Content-Length'] = $bytes;
}
/**
* Send headers
*
* @see getHeaders()
*
* Note this doesn't "clear" the headers. Calling sendHeaders() will
* call header() again (but probably have not effect) and getHeaders() will
* still return the headers.
*
* @return null
*/
public function sendHeaders() {
$headers = $this->_headers;
if (array_key_exists('_responseCode', $headers)) {
header($headers['_responseCode']);
unset($headers['_responseCode']);
}
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
}
/**
* Get a GMT formatted date for use in HTTP headers
*
* <code>
* header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
* </code>
*
* @param int $time unix timestamp
*
* @return string
*/
public static function gmtDate($time) {
return gmdate('D, d M Y H:i:s \G\M\T', $time);
}
protected $_headers = array();
protected $_lmTime = null;
protected $_etag = null;
protected function _setEtag($hash, $scope) {
$this->_etag = '"' . $hash
. substr($scope, 0, 3)
. '"';
$this->_headers['ETag'] = $this->_etag;
}
protected function _setLastModified($time) {
$this->_lmTime = (int)$time;
$this->_headers['Last-Modified'] = self::gmtDate($time);
}
/**
* Determine validity of client cache and queue 304 header if valid
*/
protected function _isCacheValid()
{
if (null === $this->_etag) {
// lmTime is copied to ETag, so this condition implies that the
// client received neither ETag nor Last-Modified, so can't
// possibly has a valid cache.
return false;
}
$isValid = ($this->resourceMatchedEtag() || $this->resourceNotModified());
if ($isValid) {
$this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified';
}
return $isValid;
}
protected function resourceMatchedEtag() {
if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
return false;
}
$cachedEtagList = get_magic_quotes_gpc()
? stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])
: $_SERVER['HTTP_IF_NONE_MATCH'];
$cachedEtags = split(',', $cachedEtagList);
foreach ($cachedEtags as $cachedEtag) {
if (trim($cachedEtag) == $this->_etag) {
return true;
}
}
return false;
}
protected function resourceNotModified() {
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
return false;
}
$ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
if (false !== ($semicolon = strrpos($ifModifiedSince, ';'))) {
// IE has tacked on extra data to this header, strip it
$ifModifiedSince = substr($ifModifiedSince, 0, $semicolon);
}
return ($ifModifiedSince == self::gmtDate($this->_lmTime));
}
}

View File

@@ -1,291 +1,291 @@
<?php
/**
* jsmin.php - PHP implementation of Douglas Crockford's JSMin.
*
* This is pretty much a direct port of jsmin.c to PHP with just a few
* PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
* outputs to stdout, this library accepts a string as input and returns another
* string as output.
*
* PHP 5 or higher is required.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @package JSMin
* @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.1.1 (2008-03-02)
* @link http://code.google.com/p/jsmin-php/
*/
class JSMin {
const ORD_LF = 10;
const ORD_SPACE = 32;
protected $a = '';
protected $b = '';
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null;
protected $output = '';
// -- Public Static Methods --------------------------------------------------
public static function minify($js) {
$jsmin = new JSMin($js);
return $jsmin->min();
}
// -- Public Instance Methods ------------------------------------------------
public function __construct($input) {
$this->input = str_replace("\r\n", "\n", $input);
$this->inputLength = strlen($this->input);
}
// -- Protected Instance Methods ---------------------------------------------
protected function action($d) {
switch($d) {
case 1:
$this->output .= $this->a;
case 2:
$this->a = $this->b;
if ($this->a === "'" || $this->a === '"') {
for (;;) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === $this->b) {
break;
}
if (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated string literal.');
}
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
}
}
}
case 3:
$this->b = $this->next();
if ($this->b === '/' && (
$this->a === '(' || $this->a === ',' || $this->a === '=' ||
$this->a === ':' || $this->a === '[' || $this->a === '!' ||
$this->a === '&' || $this->a === '|' || $this->a === '?')) {
$this->output .= $this->a . $this->b;
for (;;) {
$this->a = $this->get();
if ($this->a === '/') {
break;
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression '.
'literal.');
}
$this->output .= $this->a;
}
$this->b = $this->next();
}
}
}
protected function get() {
$c = $this->lookAhead;
$this->lookAhead = null;
if ($c === null) {
if ($this->inputIndex < $this->inputLength) {
$c = $this->input[$this->inputIndex];
$this->inputIndex += 1;
} else {
$c = null;
}
}
if ($c === "\r") {
return "\n";
}
if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
return $c;
}
return ' ';
}
protected function isAlphaNum($c) {
return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
}
protected function min() {
$this->a = "\n";
$this->action(3);
while ($this->a !== null) {
switch ($this->a) {
case ' ':
if ($this->isAlphaNum($this->b)) {
$this->action(1);
} else {
$this->action(2);
}
break;
case "\n":
switch ($this->b) {
case '{':
case '[':
case '(':
case '+':
case '-':
$this->action(1);
break;
case ' ':
$this->action(3);
break;
default:
if ($this->isAlphaNum($this->b)) {
$this->action(1);
}
else {
$this->action(2);
}
}
break;
default:
switch ($this->b) {
case ' ':
if ($this->isAlphaNum($this->a)) {
$this->action(1);
break;
}
$this->action(3);
break;
case "\n":
switch ($this->a) {
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case "'":
$this->action(1);
break;
default:
if ($this->isAlphaNum($this->a)) {
$this->action(1);
}
else {
$this->action(3);
}
}
break;
default:
$this->action(1);
break;
}
}
}
return $this->output;
}
protected function next() {
$c = $this->get();
if ($c === '/') {
switch($this->peek()) {
case '/':
for (;;) {
$c = $this->get();
if (ord($c) <= self::ORD_LF) {
return $c;
}
}
case '*':
$this->get();
for (;;) {
switch($this->get()) {
case '*':
if ($this->peek() === '/') {
$this->get();
return ' ';
}
break;
case null:
throw new JSMinException('Unterminated comment.');
}
}
default:
return $c;
}
}
return $c;
}
protected function peek() {
$this->lookAhead = $this->get();
return $this->lookAhead;
}
}
// -- Exceptions ---------------------------------------------------------------
class JSMinException extends Exception {}
<?php
/**
* jsmin.php - PHP implementation of Douglas Crockford's JSMin.
*
* This is pretty much a direct port of jsmin.c to PHP with just a few
* PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
* outputs to stdout, this library accepts a string as input and returns another
* string as output.
*
* PHP 5 or higher is required.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @package JSMin
* @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version 1.1.1 (2008-03-02)
* @link http://code.google.com/p/jsmin-php/
*/
class JSMin {
const ORD_LF = 10;
const ORD_SPACE = 32;
protected $a = '';
protected $b = '';
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null;
protected $output = '';
// -- Public Static Methods --------------------------------------------------
public static function minify($js) {
$jsmin = new JSMin($js);
return $jsmin->min();
}
// -- Public Instance Methods ------------------------------------------------
public function __construct($input) {
$this->input = str_replace("\r\n", "\n", $input);
$this->inputLength = strlen($this->input);
}
// -- Protected Instance Methods ---------------------------------------------
protected function action($d) {
switch($d) {
case 1:
$this->output .= $this->a;
case 2:
$this->a = $this->b;
if ($this->a === "'" || $this->a === '"') {
for (;;) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === $this->b) {
break;
}
if (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated string literal.');
}
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
}
}
}
case 3:
$this->b = $this->next();
if ($this->b === '/' && (
$this->a === '(' || $this->a === ',' || $this->a === '=' ||
$this->a === ':' || $this->a === '[' || $this->a === '!' ||
$this->a === '&' || $this->a === '|' || $this->a === '?')) {
$this->output .= $this->a . $this->b;
for (;;) {
$this->a = $this->get();
if ($this->a === '/') {
break;
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
} elseif (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated regular expression '.
'literal.');
}
$this->output .= $this->a;
}
$this->b = $this->next();
}
}
}
protected function get() {
$c = $this->lookAhead;
$this->lookAhead = null;
if ($c === null) {
if ($this->inputIndex < $this->inputLength) {
$c = $this->input[$this->inputIndex];
$this->inputIndex += 1;
} else {
$c = null;
}
}
if ($c === "\r") {
return "\n";
}
if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) {
return $c;
}
return ' ';
}
protected function isAlphaNum($c) {
return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1;
}
protected function min() {
$this->a = "\n";
$this->action(3);
while ($this->a !== null) {
switch ($this->a) {
case ' ':
if ($this->isAlphaNum($this->b)) {
$this->action(1);
} else {
$this->action(2);
}
break;
case "\n":
switch ($this->b) {
case '{':
case '[':
case '(':
case '+':
case '-':
$this->action(1);
break;
case ' ':
$this->action(3);
break;
default:
if ($this->isAlphaNum($this->b)) {
$this->action(1);
}
else {
$this->action(2);
}
}
break;
default:
switch ($this->b) {
case ' ':
if ($this->isAlphaNum($this->a)) {
$this->action(1);
break;
}
$this->action(3);
break;
case "\n":
switch ($this->a) {
case '}':
case ']':
case ')':
case '+':
case '-':
case '"':
case "'":
$this->action(1);
break;
default:
if ($this->isAlphaNum($this->a)) {
$this->action(1);
}
else {
$this->action(3);
}
}
break;
default:
$this->action(1);
break;
}
}
}
return $this->output;
}
protected function next() {
$c = $this->get();
if ($c === '/') {
switch($this->peek()) {
case '/':
for (;;) {
$c = $this->get();
if (ord($c) <= self::ORD_LF) {
return $c;
}
}
case '*':
$this->get();
for (;;) {
switch($this->get()) {
case '*':
if ($this->peek() === '/') {
$this->get();
return ' ';
}
break;
case null:
throw new JSMinException('Unterminated comment.');
}
}
default:
return $c;
}
}
return $c;
}
protected function peek() {
$this->lookAhead = $this->get();
return $this->lookAhead;
}
}
// -- Exceptions ---------------------------------------------------------------
class JSMinException extends Exception {}
?>

View File

@@ -1,243 +1,243 @@
<?php
/**
* Class Minify
* @package Minify
*/
/**
* Minify_Source
*/
require_once 'Minify/Source.php';
/**
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
*
* See README for usage instructions (for now).
*
* This library was inspired by {@link mailto:flashkot@mail.ru jscsscomp by Maxim Martynyuk}
* and by the article {@link http://www.hunlock.com/blogs/Supercharged_Javascript "Supercharged JavaScript" by Patrick Hunlock}.
*
* Requires PHP 5.1.0.
* Tested on PHP 5.1.6.
*
* @package Minify
* @author Ryan Grove <ryan@wonko.com>
* @author Stephen Clay <steve@mrclay.org>
* @copyright 2008 Ryan Grove, Stephen Clay. All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://code.google.com/p/minify/
*/
class Minify {
const TYPE_CSS = 'text/css';
const TYPE_HTML = 'text/html';
// there is some debate over the ideal JS Content-Type, but this is the
// Apache default and what Yahoo! uses..
const TYPE_JS = 'application/x-javascript';
/**
* How many hours behind are the file modification times of uploaded files?
*
<?php
/**
* Class Minify
* @package Minify
*/
/**
* Minify_Source
*/
require_once 'Minify/Source.php';
/**
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
*
* See README for usage instructions (for now).
*
* This library was inspired by {@link mailto:flashkot@mail.ru jscsscomp by Maxim Martynyuk}
* and by the article {@link http://www.hunlock.com/blogs/Supercharged_Javascript "Supercharged JavaScript" by Patrick Hunlock}.
*
* Requires PHP 5.1.0.
* Tested on PHP 5.1.6.
*
* @package Minify
* @author Ryan Grove <ryan@wonko.com>
* @author Stephen Clay <steve@mrclay.org>
* @copyright 2008 Ryan Grove, Stephen Clay. All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://code.google.com/p/minify/
*/
class Minify {
const TYPE_CSS = 'text/css';
const TYPE_HTML = 'text/html';
// there is some debate over the ideal JS Content-Type, but this is the
// Apache default and what Yahoo! uses..
const TYPE_JS = 'application/x-javascript';
/**
* How many hours behind are the file modification times of uploaded files?
*
* If you upload files from Windows to a non-Windows server, Windows may report
* incorrect mtimes for the files. Immediately after modifying and uploading a
* file, use the touch command to update the mtime on the server. If the mtime
* jumps ahead by a number of hours, set this variable to that number. If the mtime
* moves back, this should not be needed.
*
* @var int $uploaderHoursBehind
*/
public static $uploaderHoursBehind = 0;
/**
* @see setCache()
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path. (default = '')
* @return null
* @deprecated
*/
public static function useServerCache($path = '')
{
self::setCache($path);
}
/**
* Specify a cache object (with identical interface as Minify_Cache_File) or
* a path to use with Minify_Cache_File.
*
* If not called, Minify will not use a cache and, for each 200 response, will
* need to recombine files, minify and encode the output.
*
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path. (default = '')
*
* @return null
*/
public static function setCache($cache = '')
{
if (is_string($cache)) {
require_once 'Minify/Cache/File.php';
self::$_cache = new Minify_Cache_File($cache);
} else {
self::$_cache = $cache;
}
}
private static $_cache = null;
/**
* Serve a request for a minified file.
*
* Here are the available options and defaults in the base controller:
*
* 'isPublic' : send "public" instead of "private" in Cache-Control
* headers, allowing shared caches to cache the output. (default true)
*
* 'quiet' : set to true to have serve() return an array rather than sending
* any headers/output (default false)
*
* 'encodeOutput' : to disable content encoding, set this to false (default true)
*
* 'encodeMethod' : generally you should let this be determined by
* HTTP_Encoder (leave null), but you can force a particular encoding
* to be returned, by setting this to 'gzip', 'deflate', or '' (no encoding)
*
* 'encodeLevel' : level of encoding compression (0 to 9, default 9)
*
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
* value to remove. (default 'UTF-8')
*
* 'maxAge' : set this to the number of seconds the client should use its cache
* before revalidating with the server. This sets Cache-Control: max-age and the
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching.
* moves back, this should not be needed.
*
* @var int $uploaderHoursBehind
*/
public static $uploaderHoursBehind = 0;
/**
* @see setCache()
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path. (default = '')
* @return null
* @deprecated
*/
public static function useServerCache($path = '')
{
self::setCache($path);
}
/**
* Specify a cache object (with identical interface as Minify_Cache_File) or
* a path to use with Minify_Cache_File.
*
* If not called, Minify will not use a cache and, for each 200 response, will
* need to recombine files, minify and encode the output.
*
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path. (default = '')
*
* @return null
*/
public static function setCache($cache = '')
{
if (is_string($cache)) {
require_once 'Minify/Cache/File.php';
self::$_cache = new Minify_Cache_File($cache);
} else {
self::$_cache = $cache;
}
}
private static $_cache = null;
/**
* Serve a request for a minified file.
*
* Here are the available options and defaults in the base controller:
*
* 'isPublic' : send "public" instead of "private" in Cache-Control
* headers, allowing shared caches to cache the output. (default true)
*
* 'quiet' : set to true to have serve() return an array rather than sending
* any headers/output (default false)
*
* 'encodeOutput' : to disable content encoding, set this to false (default true)
*
* 'encodeMethod' : generally you should let this be determined by
* HTTP_Encoder (leave null), but you can force a particular encoding
* to be returned, by setting this to 'gzip', 'deflate', or '' (no encoding)
*
* 'encodeLevel' : level of encoding compression (0 to 9, default 9)
*
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
* value to remove. (default 'UTF-8')
*
* 'maxAge' : set this to the number of seconds the client should use its cache
* before revalidating with the server. This sets Cache-Control: max-age and the
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching.
*
* 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
* minifier option to enable URI rewriting in CSS files (default true)
*
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses.
* @see Minify_Lines::minify()
*
* 'minifiers' : to override Minify's default choice of minifier function for
* a particular content-type, specify your callback under the key of the
* content-type:
* <code>
* // call customCssMinifier($css) for all CSS minification
* $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
*
* // don't minify Javascript at all
* $options['minifiers'][Minify::TYPE_JS] = '';
* </code>
*
* 'minifierOptions' : to send options to the minifier function, specify your options
* under the key of the content-type. E.g. To send the CSS minifier an option:
* <code>
* // give CSS minifier array('optionName' => 'optionValue') as 2nd argument
* $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
* </code>
*
* 'contentType' : (optional) this is only needed if your file extension is not
* js/css/html. The given content-type will be sent regardless of source file
* extension, so this should not be used in a Groups config with other
* Javascript/CSS files.
*
* Any controller options are documented in that controller's setupSources() method.
*
* @param mixed instance of subclass of Minify_Controller_Base or string name of
* controller. E.g. 'Files'
*
* @param array $options controller/serve options
*
* @return mixed null, or, if the 'quiet' option is set to true, an array
* with keys "success" (bool), "statusCode" (int), "content" (string), and
* "headers" (array).
*/
public static function serve($controller, $options = array()) {
if (is_string($controller)) {
// make $controller into object
$class = 'Minify_Controller_' . $controller;
if (! class_exists($class, false)) {
require_once "Minify/Controller/"
. str_replace('_', '/', $controller) . ".php";
}
$controller = new $class();
}
// set up controller sources and mix remaining options with
// controller defaults
$options = $controller->setupSources($options);
$options = $controller->analyzeSources($options);
self::$_options = $controller->mixInDefaultOptions($options);
// check request validity
if (! $controller->sources) {
// invalid request!
if (! self::$_options['quiet']) {
header(self::$_options['badRequestHeader']);
echo self::$_options['badRequestHeader'];
return;
} else {
list(,$statusCode) = explode(' ', self::$_options['badRequestHeader']);
return array(
'success' => false
,'statusCode' => (int)$statusCode
,'content' => ''
,'headers' => array()
);
}
}
self::$_controller = $controller;
if (self::$_options['debug']) {
self::_setupDebug($controller->sources);
self::$_options['maxAge'] = 0;
}
// check client cache
require_once 'HTTP/ConditionalGet.php';
$cgOptions = array(
'lastModifiedTime' => self::$_options['lastModifiedTime']
,'isPublic' => self::$_options['isPublic']
);
if (self::$_options['maxAge'] > 0) {
$cgOptions['maxAge'] = self::$_options['maxAge'];
}
$cg = new HTTP_ConditionalGet($cgOptions);
if ($cg->cacheIsValid) {
// client's cache is valid
if (! self::$_options['quiet']) {
$cg->sendHeaders();
return;
} else {
return array(
'success' => true
,'statusCode' => 304
,'content' => ''
,'headers' => $cg->getHeaders()
);
}
} else {
// client will need output
$headers = $cg->getHeaders();
unset($cg);
}
// determine encoding
if (self::$_options['encodeOutput']) {
if (self::$_options['encodeMethod'] !== null) {
// controller specifically requested this
$contentEncoding = self::$_options['encodeMethod'];
} else {
// sniff request header
require_once 'HTTP/Encoder.php';
// depending on what the client accepts, $contentEncoding may be
// 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
// getAcceptedEncoding() with false leaves out compress as an option.
list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false);
}
} else {
self::$_options['encodeMethod'] = ''; // identity (no encoding)
*
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses.
* @see Minify_Lines::minify()
*
* 'minifiers' : to override Minify's default choice of minifier function for
* a particular content-type, specify your callback under the key of the
* content-type:
* <code>
* // call customCssMinifier($css) for all CSS minification
* $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
*
* // don't minify Javascript at all
* $options['minifiers'][Minify::TYPE_JS] = '';
* </code>
*
* 'minifierOptions' : to send options to the minifier function, specify your options
* under the key of the content-type. E.g. To send the CSS minifier an option:
* <code>
* // give CSS minifier array('optionName' => 'optionValue') as 2nd argument
* $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
* </code>
*
* 'contentType' : (optional) this is only needed if your file extension is not
* js/css/html. The given content-type will be sent regardless of source file
* extension, so this should not be used in a Groups config with other
* Javascript/CSS files.
*
* Any controller options are documented in that controller's setupSources() method.
*
* @param mixed instance of subclass of Minify_Controller_Base or string name of
* controller. E.g. 'Files'
*
* @param array $options controller/serve options
*
* @return mixed null, or, if the 'quiet' option is set to true, an array
* with keys "success" (bool), "statusCode" (int), "content" (string), and
* "headers" (array).
*/
public static function serve($controller, $options = array()) {
if (is_string($controller)) {
// make $controller into object
$class = 'Minify_Controller_' . $controller;
if (! class_exists($class, false)) {
require_once "Minify/Controller/"
. str_replace('_', '/', $controller) . ".php";
}
$controller = new $class();
}
// set up controller sources and mix remaining options with
// controller defaults
$options = $controller->setupSources($options);
$options = $controller->analyzeSources($options);
self::$_options = $controller->mixInDefaultOptions($options);
// check request validity
if (! $controller->sources) {
// invalid request!
if (! self::$_options['quiet']) {
header(self::$_options['badRequestHeader']);
echo self::$_options['badRequestHeader'];
return;
} else {
list(,$statusCode) = explode(' ', self::$_options['badRequestHeader']);
return array(
'success' => false
,'statusCode' => (int)$statusCode
,'content' => ''
,'headers' => array()
);
}
}
self::$_controller = $controller;
if (self::$_options['debug']) {
self::_setupDebug($controller->sources);
self::$_options['maxAge'] = 0;
}
// check client cache
require_once 'HTTP/ConditionalGet.php';
$cgOptions = array(
'lastModifiedTime' => self::$_options['lastModifiedTime']
,'isPublic' => self::$_options['isPublic']
);
if (self::$_options['maxAge'] > 0) {
$cgOptions['maxAge'] = self::$_options['maxAge'];
}
$cg = new HTTP_ConditionalGet($cgOptions);
if ($cg->cacheIsValid) {
// client's cache is valid
if (! self::$_options['quiet']) {
$cg->sendHeaders();
return;
} else {
return array(
'success' => true
,'statusCode' => 304
,'content' => ''
,'headers' => $cg->getHeaders()
);
}
} else {
// client will need output
$headers = $cg->getHeaders();
unset($cg);
}
// determine encoding
if (self::$_options['encodeOutput']) {
if (self::$_options['encodeMethod'] !== null) {
// controller specifically requested this
$contentEncoding = self::$_options['encodeMethod'];
} else {
// sniff request header
require_once 'HTTP/Encoder.php';
// depending on what the client accepts, $contentEncoding may be
// 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
// getAcceptedEncoding() with false leaves out compress as an option.
list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false);
}
} else {
self::$_options['encodeMethod'] = ''; // identity (no encoding)
}
if (self::$_options['contentType'] === self::TYPE_CSS
@@ -252,184 +252,184 @@ class Minify {
}
}
}
// check server cache
if (null !== self::$_cache) {
// using cache
// the goal is to use only the cache methods to sniff the length and
// output the content, as they do not require ever loading the file into
// memory.
$cacheId = 'minify_' . self::_getCacheId();
$encodingExtension = self::$_options['encodeMethod']
? ('deflate' === self::$_options['encodeMethod']
? '.zd'
: '.zg')
: '';
$fullCacheId = $cacheId . $encodingExtension;
// check cache for valid entry
$cacheIsReady = self::$_cache->isValid($fullCacheId, self::$_options['lastModifiedTime']);
if ($cacheIsReady) {
$cacheContentLength = self::$_cache->getSize($fullCacheId);
} else {
// generate & cache content
$content = self::_combineMinify();
self::$_cache->store($cacheId, $content);
self::$_cache->store($cacheId . '.zd', gzdeflate($content, self::$_options['encodeLevel']));
self::$_cache->store($cacheId . '.zg', gzencode($content, self::$_options['encodeLevel']));
}
} else {
// no cache
$cacheIsReady = false;
$content = self::_combineMinify();
}
if (! $cacheIsReady && self::$_options['encodeMethod']) {
// still need to encode
$content = ('deflate' === self::$_options['encodeMethod'])
? gzdeflate($content, self::$_options['encodeLevel'])
: gzencode($content, self::$_options['encodeLevel']);
}
// add headers
$headers['Content-Length'] = $cacheIsReady
? $cacheContentLength
: strlen($content);
$headers['Content-Type'] = self::$_options['contentTypeCharset']
? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset']
: self::$_options['contentType'];
if (self::$_options['encodeMethod'] !== '') {
$headers['Content-Encoding'] = $contentEncoding;
$headers['Vary'] = 'Accept-Encoding';
}
if (! self::$_options['quiet']) {
// output headers & content
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
if ($cacheIsReady) {
self::$_cache->display($fullCacheId);
} else {
echo $content;
}
} else {
return array(
'success' => true
,'statusCode' => 200
,'content' => $cacheIsReady
? self::$_cache->fetch($fullCacheId)
: $content
,'headers' => $headers
);
}
}
/**
* @var Minify_Controller active controller for current request
*/
protected static $_controller = null;
/**
* @var array options for current request
*/
protected static $_options = null;
/**
* Set up sources to use Minify_Lines
*
* @param array $sources Minify_Source instances
*
* @return null
*/
protected static function _setupDebug($sources)
{
foreach ($sources as $source) {
$source->minifier = array('Minify_Lines', 'minify');
$id = $source->getId();
$source->minifyOptions = array(
'id' => (is_file($id) ? basename($id) : $id)
);
}
}
/**
* Combines sources and minifies the result.
*
* @return string
*/
protected static function _combineMinify() {
$type = self::$_options['contentType']; // ease readability
// when combining scripts, make sure all statements separated
$implodeSeparator = ($type === self::TYPE_JS)
? ';'
: '';
// allow the user to pass a particular array of options to each
// minifier (designated by type). source objects may still override
// these
$defaultOptions = isset(self::$_options['minifierOptions'][$type])
? self::$_options['minifierOptions'][$type]
: array();
// if minifier not set, default is no minification. source objects
// may still override this
$defaultMinifier = isset(self::$_options['minifiers'][$type])
? self::$_options['minifiers'][$type]
: false;
if (Minify_Source::haveNoMinifyPrefs(self::$_controller->sources)) {
// all source have same options/minifier, better performance
// to combine, then minify once
foreach (self::$_controller->sources as $source) {
$pieces[] = $source->getContent();
}
$content = implode($implodeSeparator, $pieces);
if ($defaultMinifier) {
self::$_controller->loadMinifier($defaultMinifier);
$content = call_user_func($defaultMinifier, $content, $defaultOptions);
}
} else {
// minify each source with its own options and minifier, then combine
foreach (self::$_controller->sources as $source) {
// allow the source to override our minifier and options
$minifier = (null !== $source->minifier)
? $source->minifier
: $defaultMinifier;
$options = (null !== $source->minifyOptions)
? array_merge($defaultOptions, $source->minifyOptions)
: $defaultOptions;
if ($defaultMinifier) {
self::$_controller->loadMinifier($minifier);
// get source content and minify it
$pieces[] = call_user_func($minifier, $source->getContent(), $options);
} else {
$pieces[] = $source->getContent();
}
}
$content = implode($implodeSeparator, $pieces);
}
// do any post-processing (esp. for editing build URIs)
if (self::$_options['postprocessorRequire']) {
require_once self::$_options['postprocessorRequire'];
}
if (self::$_options['postprocessor']) {
$content = call_user_func(self::$_options['postprocessor'], $content, $type);
}
return $content;
}
/**
* Make a unique cache id for for this request.
*
* Any settings that could affect output are taken into consideration
*
* @return string
*/
protected static function _getCacheId() {
return md5(serialize(array(
Minify_Source::getDigest(self::$_controller->sources)
,self::$_options['minifiers']
// check server cache
if (null !== self::$_cache) {
// using cache
// the goal is to use only the cache methods to sniff the length and
// output the content, as they do not require ever loading the file into
// memory.
$cacheId = 'minify_' . self::_getCacheId();
$encodingExtension = self::$_options['encodeMethod']
? ('deflate' === self::$_options['encodeMethod']
? '.zd'
: '.zg')
: '';
$fullCacheId = $cacheId . $encodingExtension;
// check cache for valid entry
$cacheIsReady = self::$_cache->isValid($fullCacheId, self::$_options['lastModifiedTime']);
if ($cacheIsReady) {
$cacheContentLength = self::$_cache->getSize($fullCacheId);
} else {
// generate & cache content
$content = self::_combineMinify();
self::$_cache->store($cacheId, $content);
self::$_cache->store($cacheId . '.zd', gzdeflate($content, self::$_options['encodeLevel']));
self::$_cache->store($cacheId . '.zg', gzencode($content, self::$_options['encodeLevel']));
}
} else {
// no cache
$cacheIsReady = false;
$content = self::_combineMinify();
}
if (! $cacheIsReady && self::$_options['encodeMethod']) {
// still need to encode
$content = ('deflate' === self::$_options['encodeMethod'])
? gzdeflate($content, self::$_options['encodeLevel'])
: gzencode($content, self::$_options['encodeLevel']);
}
// add headers
$headers['Content-Length'] = $cacheIsReady
? $cacheContentLength
: strlen($content);
$headers['Content-Type'] = self::$_options['contentTypeCharset']
? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset']
: self::$_options['contentType'];
if (self::$_options['encodeMethod'] !== '') {
$headers['Content-Encoding'] = $contentEncoding;
$headers['Vary'] = 'Accept-Encoding';
}
if (! self::$_options['quiet']) {
// output headers & content
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
if ($cacheIsReady) {
self::$_cache->display($fullCacheId);
} else {
echo $content;
}
} else {
return array(
'success' => true
,'statusCode' => 200
,'content' => $cacheIsReady
? self::$_cache->fetch($fullCacheId)
: $content
,'headers' => $headers
);
}
}
/**
* @var Minify_Controller active controller for current request
*/
protected static $_controller = null;
/**
* @var array options for current request
*/
protected static $_options = null;
/**
* Set up sources to use Minify_Lines
*
* @param array $sources Minify_Source instances
*
* @return null
*/
protected static function _setupDebug($sources)
{
foreach ($sources as $source) {
$source->minifier = array('Minify_Lines', 'minify');
$id = $source->getId();
$source->minifyOptions = array(
'id' => (is_file($id) ? basename($id) : $id)
);
}
}
/**
* Combines sources and minifies the result.
*
* @return string
*/
protected static function _combineMinify() {
$type = self::$_options['contentType']; // ease readability
// when combining scripts, make sure all statements separated
$implodeSeparator = ($type === self::TYPE_JS)
? ';'
: '';
// allow the user to pass a particular array of options to each
// minifier (designated by type). source objects may still override
// these
$defaultOptions = isset(self::$_options['minifierOptions'][$type])
? self::$_options['minifierOptions'][$type]
: array();
// if minifier not set, default is no minification. source objects
// may still override this
$defaultMinifier = isset(self::$_options['minifiers'][$type])
? self::$_options['minifiers'][$type]
: false;
if (Minify_Source::haveNoMinifyPrefs(self::$_controller->sources)) {
// all source have same options/minifier, better performance
// to combine, then minify once
foreach (self::$_controller->sources as $source) {
$pieces[] = $source->getContent();
}
$content = implode($implodeSeparator, $pieces);
if ($defaultMinifier) {
self::$_controller->loadMinifier($defaultMinifier);
$content = call_user_func($defaultMinifier, $content, $defaultOptions);
}
} else {
// minify each source with its own options and minifier, then combine
foreach (self::$_controller->sources as $source) {
// allow the source to override our minifier and options
$minifier = (null !== $source->minifier)
? $source->minifier
: $defaultMinifier;
$options = (null !== $source->minifyOptions)
? array_merge($defaultOptions, $source->minifyOptions)
: $defaultOptions;
if ($defaultMinifier) {
self::$_controller->loadMinifier($minifier);
// get source content and minify it
$pieces[] = call_user_func($minifier, $source->getContent(), $options);
} else {
$pieces[] = $source->getContent();
}
}
$content = implode($implodeSeparator, $pieces);
}
// do any post-processing (esp. for editing build URIs)
if (self::$_options['postprocessorRequire']) {
require_once self::$_options['postprocessorRequire'];
}
if (self::$_options['postprocessor']) {
$content = call_user_func(self::$_options['postprocessor'], $content, $type);
}
return $content;
}
/**
* Make a unique cache id for for this request.
*
* Any settings that could affect output are taken into consideration
*
* @return string
*/
protected static function _getCacheId() {
return md5(serialize(array(
Minify_Source::getDigest(self::$_controller->sources)
,self::$_options['minifiers']
,self::$_options['minifierOptions']
,self::$_options['postprocessor']
)));
}
}
,self::$_options['postprocessor']
)));
}
}

View File

@@ -1,101 +1,101 @@
<?php
/**
* Class Minify_Build
* @package Minify
*/
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
*/
public $lastModified = 0;
/**
* String to use as ampersand in uri(). Set this to '&' if
* you are not HTML-escaping URIs.
*
* @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
* @return string
*/
public function uri($uri) {
$sep = 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)
{
$max = 0;
foreach ((array)$sources as $source) {
if ($source instanceof Minify_Source) {
$max = max($max, $source->lastModified);
} elseif (is_string($source)) {
if (0 === strpos($source, '//')) {
$source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
}
if (is_file($source)) {
$max = max($max, filemtime($source));
}
}
}
$this->lastModified = $max;
}
}
<?php
/**
* Class Minify_Build
* @package Minify
*/
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
*/
public $lastModified = 0;
/**
* String to use as ampersand in uri(). Set this to '&' if
* you are not HTML-escaping URIs.
*
* @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
* @return string
*/
public function uri($uri) {
$sep = 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)
{
$max = 0;
foreach ((array)$sources as $source) {
if ($source instanceof Minify_Source) {
$max = max($max, $source->lastModified);
} elseif (is_string($source)) {
if (0 === strpos($source, '//')) {
$source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
}
if (is_file($source)) {
$max = max($max, filemtime($source));
}
}
}
$this->lastModified = $max;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php
/**
* Class Minify_CSS
* @package Minify

View File

@@ -1,103 +1,103 @@
<?php
/**
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
public function __construct($path = '')
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
}
$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)
{
return self::_verifiedWrite($this->_path . '/' . $id, $data);
}
/**
* 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 (file_exists($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id (e.g. a filename)
*/
public function display($id)
{
readfile($this->_path . '/' . $id);
}
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
* @return string
*/
public function fetch($id)
{
return file_get_contents($this->_path . '/' . $id);
}
private $_path = null;
/**
* Write data to file and verify its contents
*
* @param string $file path
*
* @param string $data
*
* @return bool success
*/
private static function _verifiedWrite($file, $data)
{
if (! @file_put_contents($file, $data)) {
return false;
}
if (md5($data) !== md5_file($file)) {
@unlink($file);
return false;
}
return true;
}
}
<?php
/**
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
public function __construct($path = '')
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
}
$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)
{
return self::_verifiedWrite($this->_path . '/' . $id, $data);
}
/**
* 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 (file_exists($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id (e.g. a filename)
*/
public function display($id)
{
readfile($this->_path . '/' . $id);
}
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
* @return string
*/
public function fetch($id)
{
return file_get_contents($this->_path . '/' . $id);
}
private $_path = null;
/**
* Write data to file and verify its contents
*
* @param string $file path
*
* @param string $data
*
* @return bool success
*/
private static function _verifiedWrite($file, $data)
{
if (! @file_put_contents($file, $data)) {
return false;
}
if (md5($data) !== md5_file($file)) {
@unlink($file);
return false;
}
return true;
}
}

View File

@@ -1,123 +1,123 @@
<?php
/**
* 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.
*
* @param array $options controller and Minify options
*
* @param array $options Minify options
*/
abstract public function setupSources($options);
/**
* Get default Minify options for this controller.
*
* Override in subclass to change defaults
*
* @return array options for Minify
*/
public function getDefaultMinifyOptions() {
return array(
'isPublic' => true
,'encodeOutput' => true
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'UTF-8'
<?php
/**
* 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.
*
* @param array $options controller and Minify options
*
* @param array $options Minify options
*/
abstract public function setupSources($options);
/**
* Get default Minify options for this controller.
*
* Override in subclass to change defaults
*
* @return array options for Minify
*/
public function getDefaultMinifyOptions() {
return array(
'isPublic' => true
,'encodeOutput' => true
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'UTF-8'
,'maxAge' => 1800 // 30 minutes
,'rewriteCssUris' => true
,'quiet' => false // serve() will send headers and output
,'debug' => false
// 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_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'
*
* If you need code loaded on demand and this doesn't suit you, you'll need
* 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)
{
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
*
* 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
* @return bool file is safe
*/
public static function _fileIsSafe($file, $safeDirs)
{
,'rewriteCssUris' => true
,'quiet' => false // serve() will send headers and output
,'debug' => false
// 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_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'
*
* If you need code loaded on demand and this doesn't suit you, you'll need
* 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)
{
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
*
* 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
* @return bool file is safe
*/
public static function _fileIsSafe($file, $safeDirs)
{
$pathOk = false;
foreach ((array)$safeDirs as $safeDir) {
if (strpos($file, $safeDir) === 0 && file_exists($file)) {
@@ -128,68 +128,68 @@ abstract class Minify_Controller_Base {
if (! $pathOk) {
return false;
}
$base = basename($file);
if ($base[0] === '.') {
return false;
}
list($revExt) = explode('.', strrev($base));
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
}
/*public static function _haveSameExt
/**
* @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)
{
$ret = array_merge(
$this->getDefaultMinifyOptions(), $options
);
if (! isset($options['minifiers'])) {
$options['minifiers'] = array();
}
$ret['minifiers'] = array_merge(
$this->getDefaultMinifers(), $options['minifiers']
);
return $ret;
}
/**
* 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())
{
if ($this->sources) {
if (! isset($options['contentType'])) {
$options['contentType'] = Minify_Source::getContentType($this->sources);
}
// last modified is needed for caching, even if setExpires is set
if (! isset($options['lastModifiedTime'])) {
$max = 0;
foreach ($this->sources as $source) {
$max = max($source->lastModified, $max);
}
$options['lastModifiedTime'] = $max;
}
}
return $options;
}
}
$base = basename($file);
if ($base[0] === '.') {
return false;
}
list($revExt) = explode('.', strrev($base));
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
}
/*public static function _haveSameExt
/**
* @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)
{
$ret = array_merge(
$this->getDefaultMinifyOptions(), $options
);
if (! isset($options['minifiers'])) {
$options['minifiers'] = array();
}
$ret['minifiers'] = array_merge(
$this->getDefaultMinifers(), $options['minifiers']
);
return $ret;
}
/**
* 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())
{
if ($this->sources) {
if (! isset($options['contentType'])) {
$options['contentType'] = Minify_Source::getContentType($this->sources);
}
// last modified is needed for caching, even if setExpires is set
if (! isset($options['lastModifiedTime'])) {
$max = 0;
foreach ($this->sources as $source) {
$max = max($source->lastModified, $max);
}
$options['lastModifiedTime'] = $max;
}
}
return $options;
}
}

View File

@@ -1,71 +1,71 @@
<?php
/**
* Class Minify_Controller_Files
* @package Minify
*/
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(
* 'files' => array(
* '//js/jquery.js'
* ,'//js/plugins.js'
* ,'/home/username/file.js'
* )
* ));
* </code>
*
* 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_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'];
unset($options['files']);
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
// file not found
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}
<?php
/**
* Class Minify_Controller_Files
* @package Minify
*/
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(
* 'files' => array(
* '//js/jquery.js'
* ,'//js/plugins.js'
* ,'/home/username/file.js'
* )
* ));
* </code>
*
* 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_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'];
unset($options['files']);
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
// file not found
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -1,50 +1,50 @@
<?php
/**
* Class Minify_Controller_Groups
* @package Minify
*/
require_once 'Minify/Controller/Base.php';
/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
*
* <code>
* 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
*/
public function setupSources($options) {
// strip controller options
$groups = $options['groups'];
unset($options['groups']);
<?php
/**
* Class Minify_Controller_Groups
* @package Minify
*/
require_once 'Minify/Controller/Base.php';
/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
*
* <code>
* 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
*/
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'])
@@ -57,29 +57,29 @@ class Minify_Controller_Groups extends Minify_Controller_Base {
// no PATH_INFO or not a valid group
return $options;
}
$sources = array();
foreach ((array)$groups[$pi] as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
// file doesn't exist
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}
$sources = array();
foreach ((array)$groups[$pi] as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$file = realpath($file);
if (file_exists($file)) {
$sources[] = new Minify_Source(array(
'filepath' => $file
));
} else {
// file doesn't exist
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -1,80 +1,80 @@
<?php
/**
* Class Minify_Controller_Page
* @package Minify
*/
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)
*
* @todo Add 'file' option to read HTML file.
*/
public function setupSources($options) {
// strip controller options
$sourceSpec = array(
'content' => $options['content']
,'id' => $options['id']
);
unset($options['content'], $options['id']);
if (isset($options['minifyAll'])) {
// this will be the 2nd argument passed to Minify_HTML::minify()
$sourceSpec['minifyOptions'] = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', '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()
*/
public function loadMinifier($minifierCallback)
{
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';
}
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
}
}
<?php
/**
* Class Minify_Controller_Page
* @package Minify
*/
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)
*
* @todo Add 'file' option to read HTML file.
*/
public function setupSources($options) {
// strip controller options
$sourceSpec = array(
'content' => $options['content']
,'id' => $options['id']
);
unset($options['content'], $options['id']);
if (isset($options['minifyAll'])) {
// this will be the 2nd argument passed to Minify_HTML::minify()
$sourceSpec['minifyOptions'] = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', '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()
*/
public function loadMinifier($minifierCallback)
{
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';
}
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
}
}

View File

@@ -1,118 +1,118 @@
<?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
: '';
Minify::setCache($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();
<?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
: '';
Minify::setCache($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;
$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))
{
$goodFiles[] = $file;
$srcOptions = array(
'filepath' => $file
);
$this->sources[] = new Minify_Source($srcOptions);
} else {
$hasBadSource = true;
break;
}
}
if ($hasBadSource) {
$this->sources = array();
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))
{
$goodFiles[] = $file;
$srcOptions = array(
'filepath' => $file
);
$this->sources[] = new Minify_Source($srcOptions);
} else {
$hasBadSource = true;
break;
}
}
if ($hasBadSource) {
$this->sources = array();
}
if (! MINIFY_REWRITE_CSS_URLS) {
$options['rewriteCssUris'] = false;
}
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);
}
}
}
}
}
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

@@ -1,88 +1,88 @@
<?php
/**
* Class Minify_HTML
* @package Minify
*/
/**
* Compress HTML
*
* 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.
*
* A test suite is available.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_HTML {
/**
* "Minify" an HTML page
*
* @param string $html
* @param array $options
* @return string
*/
public static function minify($html, $options = array()) {
if (isset($options['cssMinifier'])) {
self::$_cssMinifier = $options['cssMinifier'];
}
if (isset($options['jsMinifier'])) {
self::$_jsMinifier = $options['jsMinifier'];
}
$html = str_replace("\r\n", "\n", trim($html));
self::$_isXhtml = (false !== strpos($html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
self::$_replacementHash = 'MINIFYHTML' . md5(time());
<?php
/**
* Class Minify_HTML
* @package Minify
*/
/**
* Compress HTML
*
* 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.
*
* A test suite is available.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_HTML {
/**
* "Minify" an HTML page
*
* @param string $html
* @param array $options
* @return string
*/
public static function minify($html, $options = array()) {
if (isset($options['cssMinifier'])) {
self::$_cssMinifier = $options['cssMinifier'];
}
if (isset($options['jsMinifier'])) {
self::$_jsMinifier = $options['jsMinifier'];
}
$html = str_replace("\r\n", "\n", trim($html));
self::$_isXhtml = (false !== strpos($html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
self::$_replacementHash = 'MINIFYHTML' . md5(time());
self::$_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);
// replace STYLEs (and minify) with placeholders
$html = preg_replace_callback(
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
,array('Minify_HTML', '_removeStyleCB')
,$html);
// remove HTML comments (but not IE conditional comments).
$html = preg_replace('/<!--[^\\[][\\s\\S]*?-->/', '', $html);
// replace PREs with placeholders
$html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array('Minify_HTML', '_removePreCB')
, $html);
// replace TEXTAREAs with placeholders
$html = preg_replace_callback(
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array('Minify_HTML', '_removeTaCB')
, $html);
// trim each line.
// @todo take into account attribute values that span multiple lines.
$html = preg_replace('/^\\s+|\\s+$/m', '', $html);
// remove ws around block/undisplayed elements
$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);
// remove ws outside of all elements
$html = preg_replace_callback(
'/>([^<]+)</'
,array('Minify_HTML', '_outsideTagCB')
,$html);
// replace SCRIPTs (and minify) with placeholders
$html = preg_replace_callback(
'/\\s*(<script\\b[^>]*?>)([\\s\\S]*?)<\\/script>\\s*/i'
,array('Minify_HTML', '_removeScriptCB')
,$html);
// replace STYLEs (and minify) with placeholders
$html = preg_replace_callback(
'/\\s*(<style\\b[^>]*?>)([\\s\\S]*?)<\\/style>\\s*/i'
,array('Minify_HTML', '_removeStyleCB')
,$html);
// remove HTML comments (but not IE conditional comments).
$html = preg_replace('/<!--[^\\[][\\s\\S]*?-->/', '', $html);
// replace PREs with placeholders
$html = preg_replace_callback('/\\s*(<pre\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array('Minify_HTML', '_removePreCB')
, $html);
// replace TEXTAREAs with placeholders
$html = preg_replace_callback(
'/\\s*(<textarea\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array('Minify_HTML', '_removeTaCB')
, $html);
// trim each line.
// @todo take into account attribute values that span multiple lines.
$html = preg_replace('/^\\s+|\\s+$/m', '', $html);
// remove ws around block/undisplayed elements
$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);
// remove ws outside of all elements
$html = preg_replace_callback(
'/>([^<]+)</'
,array('Minify_HTML', '_outsideTagCB')
,$html);
// fill placeholders
$html = str_replace(
@@ -90,13 +90,13 @@ class Minify_HTML {
,array_values(self::$_placeholders)
,$html
);
self::$_placeholders = array();
self::$_placeholders = array();
// use newlines before 1st attribute in open tags (to limit line lengths)
$html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $html);
self::$_cssMinifier = self::$_jsMinifier = null;
return $html;
self::$_cssMinifier = self::$_jsMinifier = null;
return $html;
}
protected static function _reservePlace($content)
@@ -104,83 +104,83 @@ class Minify_HTML {
$placeholder = '%' . self::$_replacementHash . count(self::$_placeholders) . '%';
self::$_placeholders[$placeholder] = $content;
return $placeholder;
}
protected static $_isXhtml = false;
}
protected static $_isXhtml = false;
protected static $_replacementHash = null;
protected static $_placeholders = array();
protected static $_cssMinifier = null;
protected static $_jsMinifier = null;
protected static function _outsideTagCB($m)
{
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
}
protected static function _removePreCB($m)
{
return self::_reservePlace($m[1]);
}
protected static function _removeTaCB($m)
{
return self::_reservePlace($m[1]);
}
protected static 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);
// minify
$minifier = self::$_cssMinifier
? self::$_cssMinifier
: 'trim';
$css = call_user_func($minifier, $css);
protected static $_cssMinifier = null;
protected static $_jsMinifier = null;
protected static function _outsideTagCB($m)
{
return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<';
}
protected static function _removePreCB($m)
{
return self::_reservePlace($m[1]);
}
protected static function _removeTaCB($m)
{
return self::_reservePlace($m[1]);
}
protected static 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);
// minify
$minifier = self::$_cssMinifier
? self::$_cssMinifier
: 'trim';
$css = call_user_func($minifier, $css);
return self::_reservePlace(self::_needsCdata($css)
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
: "{$openStyle}{$css}</style>"
);
}
protected static function _removeScriptCB($m)
{
$openScript = $m[1];
$js = $m[2];
// remove HTML comments (and ending "//" if present)
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
// remove CDATA section markers
$js = self::_removeCdata($js);
// minify
$minifier = self::$_jsMinifier
? self::$_jsMinifier
: 'trim';
$js = call_user_func($minifier, $js);
);
}
protected static function _removeScriptCB($m)
{
$openScript = $m[1];
$js = $m[2];
// remove HTML comments (and ending "//" if present)
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
// remove CDATA section markers
$js = self::_removeCdata($js);
// minify
$minifier = self::$_jsMinifier
? self::$_jsMinifier
: 'trim';
$js = call_user_func($minifier, $js);
return self::_reservePlace(self::_needsCdata($js)
? "{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>"
: "{$openScript}{$js}</script>"
);
}
protected static function _removeCdata($str)
{
return (false !== strpos($str, '<![CDATA['))
? str_replace(array('<![CDATA[', ']]>'), '', $str)
: $str;
}
protected static function _needsCdata($str)
{
return (self::$_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
}
}
);
}
protected static function _removeCdata($str)
{
return (false !== strpos($str, '<![CDATA['))
? str_replace(array('<![CDATA[', ']]>'), '', $str)
: $str;
}
protected static function _needsCdata($str)
{
return (self::$_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
}
}

View File

@@ -1,74 +1,74 @@
<?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:
*
* 'preserveComments': (default true) multi-line comments that begin
* with "/*!" will be preserved with newlines before and after to
* enhance readability.
*
* @return string
*/
public static function minify($js, $options = array())
{
if (isset($options['preserveComments'])
&& !$options['preserveComments']) {
return trim(JSMin::minify($js));
}
$ret = '';
while (1) {
list($beforeComment, $comment, $afterComment)
= self::_nextYuiComment($js);
$ret .= trim(JSMin::minify($beforeComment));
if (false === $comment) {
break;
}
$ret .= $comment;
$js = $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
* 1st element and the other two are false.
*/
private static function _nextYuiComment($in)
{
return (
(false !== ($start = strpos($in, '/*!')))
&& (false !== ($end = strpos($in, '*/', $start + 3)))
)
? array(
substr($in, 0, $start)
,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n"
,substr($in, -(strlen($in) - $end - 2))
)
: array($in, false, false);
}
}
<?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:
*
* 'preserveComments': (default true) multi-line comments that begin
* with "/*!" will be preserved with newlines before and after to
* enhance readability.
*
* @return string
*/
public static function minify($js, $options = array())
{
if (isset($options['preserveComments'])
&& !$options['preserveComments']) {
return trim(JSMin::minify($js));
}
$ret = '';
while (1) {
list($beforeComment, $comment, $afterComment)
= self::_nextYuiComment($js);
$ret .= trim(JSMin::minify($beforeComment));
if (false === $comment) {
break;
}
$ret .= $comment;
$js = $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
* 1st element and the other two are false.
*/
private static function _nextYuiComment($in)
{
return (
(false !== ($start = strpos($in, '/*!')))
&& (false !== ($end = strpos($in, '*/', $start + 3)))
)
? array(
substr($in, 0, $start)
,"\n/*" . substr($in, $start + 3, $end - $start - 1) . "\n"
,substr($in, -(strlen($in) - $end - 2))
)
: array($in, false, false);
}
}

View File

@@ -1,124 +1,124 @@
<?php
/**
* Class Minify_Lines
* @package Minify
*/
/**
* Add line numbers in C-style comments for easier debugging of combined content
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
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.
*
* @param string $content
*
* @param array $options available options:
*
* 'id': (optional) string to identify file. E.g. file name/path
*
* @return string
*/
public static function minify($content, $options = array())
{
$id = (isset($options['id']) && $options['id'])
? $options['id']
: '';
if (! $eol = self::_getEol($content)) {
return $content;
}
$lines = explode($eol, $content);
$numLines = count($lines);
// determine left padding
$padTo = strlen($numLines);
$inComment = false;
$i = 0;
$newLines = array();
while (null !== ($line = array_shift($lines))) {
if (('' !== $id) && (0 == $i % 50)) {
array_push($newLines, '', "/* {$id} */", '');
}
++$i;
$newLines[] = self::_addNote($line, $i, $inComment, $padTo);
$inComment = self::_eolInComment($line, $inComment);
}
return implode($eol, $newLines) . $eol;
}
/**
* Determine EOL character sequence
*
* @param string $str file content
*
* @return string EOL char(s) or '' if no EOL could be found
*/
private static function _getEol($str)
{
$r = strpos($str, "\r");
$n = strpos($str, "\n");
if (false === $r && false === $n) {
return '';
}
return ($r !== false)
? ($n == ($r + 1)
? "\r\n"
: "\r")
: "\n";
}
/**
* 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)
{
while (strlen($line)) {
$search = $inComment
? '*/'
: '/*';
$pos = strpos($line, $search);
if (false === $pos) {
return $inComment;
} else {
$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)
{
return $inComment
? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line
: '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
}
}
<?php
/**
* Class Minify_Lines
* @package Minify
*/
/**
* Add line numbers in C-style comments for easier debugging of combined content
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
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.
*
* @param string $content
*
* @param array $options available options:
*
* 'id': (optional) string to identify file. E.g. file name/path
*
* @return string
*/
public static function minify($content, $options = array())
{
$id = (isset($options['id']) && $options['id'])
? $options['id']
: '';
if (! $eol = self::_getEol($content)) {
return $content;
}
$lines = explode($eol, $content);
$numLines = count($lines);
// determine left padding
$padTo = strlen($numLines);
$inComment = false;
$i = 0;
$newLines = array();
while (null !== ($line = array_shift($lines))) {
if (('' !== $id) && (0 == $i % 50)) {
array_push($newLines, '', "/* {$id} */", '');
}
++$i;
$newLines[] = self::_addNote($line, $i, $inComment, $padTo);
$inComment = self::_eolInComment($line, $inComment);
}
return implode($eol, $newLines) . $eol;
}
/**
* Determine EOL character sequence
*
* @param string $str file content
*
* @return string EOL char(s) or '' if no EOL could be found
*/
private static function _getEol($str)
{
$r = strpos($str, "\r");
$n = strpos($str, "\n");
if (false === $r && false === $n) {
return '';
}
return ($r !== false)
? ($n == ($r + 1)
? "\r\n"
: "\r")
: "\n";
}
/**
* 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)
{
while (strlen($line)) {
$search = $inComment
? '*/'
: '/*';
$pos = strpos($line, $search);
if (false === $pos) {
return $inComment;
} else {
$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)
{
return $inComment
? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line
: '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
}
}

View File

@@ -1,177 +1,177 @@
<?php
/**
* Class Minify_Source
* @package 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>
*/
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.
*/
public $minifyOptions = null;
<?php
/**
* Class Minify_Source
* @package 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>
*/
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.
*/
public $minifyOptions = null;
/**
* @var string full path of file
*/
public $filepath = 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'
* (unixtime of last update).
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
* @param array $spec options
*/
public function __construct($spec)
{
if (isset($spec['filepath'])) {
if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
}
$this->filepath = $spec['filepath'];
$this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
// offset for Windows uploaders with out of sync clocks
+ round(Minify::$uploaderHoursBehind * 3600);
} elseif (isset($spec['id'])) {
$this->_id = 'id::' . $spec['id'];
if (isset($spec['content'])) {
$this->_content = $spec['content'];
} else {
$this->_getContentFunc = $spec['getContentFunc'];
}
$this->lastModified = isset($spec['lastModified'])
? $spec['lastModified']
: time();
}
if (isset($spec['minifier'])) {
$this->minifier = $spec['minifier'];
}
if (isset($spec['minifyOptions'])) {
$this->minifyOptions = $spec['minifyOptions'];
}
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
$content = (null !== $this->filepath)
? file_get_contents($this->filepath)
: ((null !== $this->_content)
? $this->_content
: call_user_func($this->_getContentFunc, $this->_id)
);
// remove UTF-8 BOM if present
return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3))
? substr($content, 3)
: $content;
}
/**
* Get id
*
* @return string
*/
public function getId()
{
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)
{
foreach ($sources as $source) {
if (null !== $source->minifier
|| null !== $source->minifyOptions) {
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)
{
foreach ($sources as $source) {
$info[] = array(
$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
*
* @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];
}
}
}
return 'text/plain';
}
protected $_content = null;
protected $_getContentFunc = null;
protected $_id = 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'
* (unixtime of last update).
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
* @param array $spec options
*/
public function __construct($spec)
{
if (isset($spec['filepath'])) {
if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
}
$this->filepath = $spec['filepath'];
$this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
// offset for Windows uploaders with out of sync clocks
+ round(Minify::$uploaderHoursBehind * 3600);
} elseif (isset($spec['id'])) {
$this->_id = 'id::' . $spec['id'];
if (isset($spec['content'])) {
$this->_content = $spec['content'];
} else {
$this->_getContentFunc = $spec['getContentFunc'];
}
$this->lastModified = isset($spec['lastModified'])
? $spec['lastModified']
: time();
}
if (isset($spec['minifier'])) {
$this->minifier = $spec['minifier'];
}
if (isset($spec['minifyOptions'])) {
$this->minifyOptions = $spec['minifyOptions'];
}
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
$content = (null !== $this->filepath)
? file_get_contents($this->filepath)
: ((null !== $this->_content)
? $this->_content
: call_user_func($this->_getContentFunc, $this->_id)
);
// remove UTF-8 BOM if present
return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3))
? substr($content, 3)
: $content;
}
/**
* Get id
*
* @return string
*/
public function getId()
{
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)
{
foreach ($sources as $source) {
if (null !== $source->minifier
|| null !== $source->minifyOptions) {
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)
{
foreach ($sources as $source) {
$info[] = array(
$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
*
* @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];
}
}
}
return 'text/plain';
}
protected $_content = null;
protected $_getContentFunc = null;
protected $_id = null;
}

View File

@@ -1,31 +1,31 @@
<?php
/**
* Function min_group_uri()
*
* @package Minify
*/
require_once dirname(__FILE__) . '/lib/Minify/Build.php';
/**
* Get a timestamped URI to a minified resource using the default Minify install
*
* <code>
* <link rel="stylesheet" type="text/css" href="<?php min_group_uri('css'); ?>" />
* <script type="text/javascript" src="<?php min_group_uri('js'); ?>"></script>
* </code>
*
* @param string $group a key of the array in groupsConfig.php
* @param string $ampersand '&' or '&amp;' (default '&amp;')
* @return string
*/
function min_group_uri($group, $ampersand = '&amp;')
{
static $gc = false;
if (false === $gc) {
$gc = (require dirname(__FILE__) . '/groupsConfig.php');
}
$b = new Minify_Build($gc[$group]);
Minify_Build::$ampersand = $ampersand;
return $b->uri('/' . basename(dirname(__FILE__)) . '/?g=' . $group);
}
<?php
/**
* Function min_group_uri()
*
* @package Minify
*/
require_once dirname(__FILE__) . '/lib/Minify/Build.php';
/**
* Get a timestamped URI to a minified resource using the default Minify install
*
* <code>
* <link rel="stylesheet" type="text/css" href="<?php min_group_uri('css'); ?>" />
* <script type="text/javascript" src="<?php min_group_uri('js'); ?>"></script>
* </code>
*
* @param string $group a key of the array in groupsConfig.php
* @param string $ampersand '&' or '&amp;' (default '&amp;')
* @return string
*/
function min_group_uri($group, $ampersand = '&amp;')
{
static $gc = false;
if (false === $gc) {
$gc = (require dirname(__FILE__) . '/groupsConfig.php');
}
$b = new Minify_Build($gc[$group]);
Minify_Build::$ampersand = $ampersand;
return $b->uri('/' . basename(dirname(__FILE__)) . '/?g=' . $group);
}

View File

@@ -1,25 +1,25 @@
<?php
/**
* The goal with this file is to benchmark serving the file doing the absolute
* least operations possible. E.g. we know we'll have to check for the file,
* check its size and the mtimes of it and the src file.
*/
$src = realpath(dirname(__FILE__) . '/../minify/before.js');
$cached = realpath(dirname(__FILE__) . '/../type-map') . '/before.js.zd';
// clearstatcache() takes over 2ms on Athlon 64 X2 5600+! Avoid at all costs!
//clearstatcache();
filemtime($src);
file_exists($cached);
filemtime($cached);
header('Cache-Control: public, max-age=31536000');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + (86400 * 365)));
header('Content-Type: application/x-javascript; charset=utf-8');
header('Content-Encoding: deflate');
header('Content-Length: ' . filesize($cached));
header('Vary: Accept-Encoding');
<?php
/**
* The goal with this file is to benchmark serving the file doing the absolute
* least operations possible. E.g. we know we'll have to check for the file,
* check its size and the mtimes of it and the src file.
*/
$src = realpath(dirname(__FILE__) . '/../minify/before.js');
$cached = realpath(dirname(__FILE__) . '/../type-map') . '/before.js.zd';
// clearstatcache() takes over 2ms on Athlon 64 X2 5600+! Avoid at all costs!
//clearstatcache();
filemtime($src);
file_exists($cached);
filemtime($cached);
header('Cache-Control: public, max-age=31536000');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + (86400 * 365)));
header('Content-Type: application/x-javascript; charset=utf-8');
header('Content-Encoding: deflate');
header('Content-Length: ' . filesize($cached));
header('Vary: Accept-Encoding');
readfile($cached);

View File

@@ -1,15 +1,15 @@
<?php
require '../../config.php';
require 'Minify.php';
// give an explicit path to avoid having to load Solar/Dir.php
Minify::setCache($minifyCachePath);
Minify::serve('Files', array(
'files' => array(
dirname(__FILE__) . '/before.js'
)
,'setExpires' => $_SERVER['REQUEST_TIME'] + 31536000 // 1 yr
));
<?php
require '../../config.php';
require 'Minify.php';
// give an explicit path to avoid having to load Solar/Dir.php
Minify::setCache($minifyCachePath);
Minify::serve('Files', array(
'files' => array(
dirname(__FILE__) . '/before.js'
)
,'setExpires' => $_SERVER['REQUEST_TIME'] + 31536000 // 1 yr
));

View File

@@ -1,15 +1,15 @@
<?php
require '../../config.php';
require 'Minify.php';
// give an explicit path to avoid having to load Solar/Dir.php
Minify::setCache($minifyCachePath);
Minify::serve('Groups', array(
'groups' => array(
'test' => array(dirname(__FILE__) . '/before.js')
)
,'setExpires' => $_SERVER['REQUEST_TIME'] + 31536000 // 1 yr
));
<?php
require '../../config.php';
require 'Minify.php';
// give an explicit path to avoid having to load Solar/Dir.php
Minify::setCache($minifyCachePath);
Minify::serve('Groups', array(
'groups' => array(
'test' => array(dirname(__FILE__) . '/before.js')
)
,'setExpires' => $_SERVER['REQUEST_TIME'] + 31536000 // 1 yr
));

View File

@@ -1,12 +1,12 @@
<?php
require '../../config.php';
define('MINIFY_BASE_DIR', realpath(
dirname(__FILE__) . '/../minify'
));
define('MINIFY_CACHE_DIR', $minifyCachePath);
require 'Minify.php';
Minify::serve('Version1');
<?php
require '../../config.php';
define('MINIFY_BASE_DIR', realpath(
dirname(__FILE__) . '/../minify'
));
define('MINIFY_CACHE_DIR', $minifyCachePath);
require 'Minify.php';
Minify::serve('Version1');

File diff suppressed because it is too large Load Diff

View File

@@ -1,17 +1,17 @@
<?php
/**
* Add the location of Minify's "lib" directory to the include_path. In
* production this could be done via .htaccess or some other method.
*/
ini_set('include_path',
dirname(__FILE__) . '/../min/lib'
. PATH_SEPARATOR . ini_get('include_path')
);
/**
* Set $minifyCachePath to a PHP-writeable path to enable server-side caching
* in all examples and tests.
*/
$minifyCachePath = 'C:/xampp/tmp'; // '';
<?php
/**
* Add the location of Minify's "lib" directory to the include_path. In
* production this could be done via .htaccess or some other method.
*/
ini_set('include_path',
dirname(__FILE__) . '/../min/lib'
. PATH_SEPARATOR . ini_get('include_path')
);
/**
* Set $minifyCachePath to a PHP-writeable path to enable server-side caching
* in all examples and tests.
*/
$minifyCachePath = 'C:/xampp/tmp'; // '';

View File

@@ -1,11 +1,11 @@
<?php
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"
,"{$base}/test space.js"
)
,'css' => array("{$base}/test.css")
);
<?php
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"
,"{$base}/test space.js"
)
,'css' => array("{$base}/test.css")
);
unset($base);

View File

@@ -1,14 +1,14 @@
<?php
require '../../config.php';
require '_groupsSources.php';
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
Minify::serve('Groups', array(
'groups' => $groupsSources
,'setExpires' => time() + 86400 * 365
));
<?php
require '../../config.php';
require '_groupsSources.php';
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
Minify::serve('Groups', array(
'groups' => $groupsSources
,'setExpires' => time() + 86400 * 365
));

View File

@@ -1,11 +1,11 @@
<?php
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"
,"{$base}/test space.js"
)
,'css' => array("{$base}/test.css")
);
<?php
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"
,"{$base}/test space.js"
)
,'css' => array("{$base}/test.css")
);
unset($base);

View File

@@ -1,14 +1,14 @@
<?php
require '../../config.php';
require '_groupsSources.php';
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
Minify::serve('Groups', array(
'groups' => $groupsSources
,'setExpires' => time() + 86400 * 365
));
<?php
require '../../config.php';
require '_groupsSources.php';
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
Minify::serve('Groups', array(
'groups' => $groupsSources
,'setExpires' => time() + 86400 * 365
));

View File

@@ -1,44 +1,44 @@
<?php
/**
* This script will serve a single js/css file in this directory. Here we place
* the front-end-controller logic in user code, then use the "Files" controller
* to minify the file. Alternately, we could have created a custom controller
* with the same logic and passed it to Minify::handleRequest().
*/
require '../../config.php';
/**
* The Files controller only "knows" HTML, CSS, and JS files. Other files
* would only be trim()ed and sent as plain/text.
*/
$serveExtensions = array('css', 'js');
// serve
if (isset($_GET['f'])) {
$filename = basename($_GET['f']); // remove any naughty bits
$filenamePattern = '/[^\'"\\/\\\\]+\\.(?:'
.implode('|', $serveExtensions). ')$/';
if (preg_match($filenamePattern, $filename)
&& file_exists(dirname(__FILE__) . '/../' . $filename)) {
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
// The Files controller can serve an array of files, but here we just
// need one.
Minify::serve('Files', array(
// controller will cast a string to an array for you
'files' => dirname(__FILE__) . '/../' . $filename
));
exit();
}
}
header("HTTP/1.0 404 Not Found");
<?php
/**
* This script will serve a single js/css file in this directory. Here we place
* the front-end-controller logic in user code, then use the "Files" controller
* to minify the file. Alternately, we could have created a custom controller
* with the same logic and passed it to Minify::handleRequest().
*/
require '../../config.php';
/**
* The Files controller only "knows" HTML, CSS, and JS files. Other files
* would only be trim()ed and sent as plain/text.
*/
$serveExtensions = array('css', 'js');
// serve
if (isset($_GET['f'])) {
$filename = basename($_GET['f']); // remove any naughty bits
$filenamePattern = '/[^\'"\\/\\\\]+\\.(?:'
.implode('|', $serveExtensions). ')$/';
if (preg_match($filenamePattern, $filename)
&& file_exists(dirname(__FILE__) . '/../' . $filename)) {
require 'Minify.php';
if ($minifyCachePath) {
Minify::setCache($minifyCachePath);
}
// The Files controller can serve an array of files, but here we just
// need one.
Minify::serve('Files', array(
// controller will cast a string to an array for you
'files' => dirname(__FILE__) . '/../' . $filename
));
exit();
}
}
header("HTTP/1.0 404 Not Found");
echo "HTTP/1.0 404 Not Found";

View File

@@ -1,44 +1,44 @@
<?php
require '../../config.php';
require 'HTTP/ConditionalGet.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
// generate content
$title = 'Last-Modified is known : add Content-Length';
$explain = '
<p>Here, like <a href="./">the first example</a>, we know the Last-Modified time,
but we also want to set the Content-Length to increase cacheability and allow
HTTP persistent connections. Instead of sending headers immediately, we first
generate our content, then use <code>setContentLength(strlen($content))</code>
to add the header. Then finally call <code>sendHeaders()</code> and send the
content.</p>
<p><strong>Note:</strong> This is not required if your PHP config buffers all
output and your script doesn\'t do any incremental flushing of the output
buffer. PHP will generally set Content-Length for you if it can.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);
<?php
require '../../config.php';
require 'HTTP/ConditionalGet.php';
// emulate regularly updating document
$every = 20;
$lastModified = round(time()/$every)*$every - $every;
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lastModified
));
if ($cg->cacheIsValid) {
$cg->sendHeaders();
// we're done
exit();
}
// generate content
$title = 'Last-Modified is known : add Content-Length';
$explain = '
<p>Here, like <a href="./">the first example</a>, we know the Last-Modified time,
but we also want to set the Content-Length to increase cacheability and allow
HTTP persistent connections. Instead of sending headers immediately, we first
generate our content, then use <code>setContentLength(strlen($content))</code>
to add the header. Then finally call <code>sendHeaders()</code> and send the
content.</p>
<p><strong>Note:</strong> This is not required if your PHP config buffers all
output and your script doesn\'t do any incremental flushing of the output
buffer. PHP will generally set Content-Length for you if it can.</p>
<p>This script emulates a document that changes every ' .$every. ' seconds.
<br>This is version: ' . date('r', $lastModified) . '</p>
';
require '_include.php';
$content = get_content(array(
'title' => $title
,'explain' => $explain
));
$cg->setContentLength(strlen($content));
$cg->sendHeaders();
send_slowly($content);

View File

@@ -1,27 +1,27 @@
<?php
require '../../config.php';
require 'HTTP/ConditionalGet.php';
// far expires
$cg = new HTTP_ConditionalGet(array(
'maxAge' => 20
,'lastModifiedTime' => filemtime(__FILE__)
));
$cg->sendHeaders();
// generate, send content
$title = 'Last-Modified + Expires';
$explain = '
<p>Here we set a static "lastModifiedTime" and "maxAge" to 20. The browser
will consider this document fresh for 20 seconds, then revalidate its cache. After
the 304 response, the cache will be good for another 20 seconds. Unless you force
a reload, there will only be 304 responses for this page after the initial download.
';
require '_include.php';
echo get_content(array(
'title' => $title
,'explain' => $explain
));
<?php
require '../../config.php';
require 'HTTP/ConditionalGet.php';
// far expires
$cg = new HTTP_ConditionalGet(array(
'maxAge' => 20
,'lastModifiedTime' => filemtime(__FILE__)
));
$cg->sendHeaders();
// generate, send content
$title = 'Last-Modified + Expires';
$explain = '
<p>Here we set a static "lastModifiedTime" and "maxAge" to 20. The browser
will consider this document fresh for 20 seconds, then revalidate its cache. After
the 304 response, the cache will be good for another 20 seconds. Unless you force
a reload, there will only be 304 responses for this page after the initial download.
';
require '_include.php';
echo get_content(array(
'title' => $title
,'explain' => $explain
));

View File

@@ -1,30 +1,28 @@
<?php
require '../config.php';
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
header('Content-Type: text/plain');
$thisDir = dirname(__FILE__);
/**
* pTest - PHP Unit Tester
* @param mixed $test Condition to test, evaluated as boolean
* @param string $message Descriptive message to output upon test
* @url http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/
*/
function assertTrue($test, $message)
{
static $count;
if (!isset($count)) $count = array('pass'=>0, 'fail'=>0, 'total'=>0);
$mode = $test ? 'pass' : 'fail';
$outMode = $test ? 'PASS' : '!FAIL';
printf("%s: %s (%d of %d tests run so far have %sed)\n",
$outMode, $message, ++$count[$mode], ++$count['total'], $mode);
return (bool)$test;
}
?>
<?php
require '../config.php';
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
header('Content-Type: text/plain');
$thisDir = dirname(__FILE__);
/**
* pTest - PHP Unit Tester
* @param mixed $test Condition to test, evaluated as boolean
* @param string $message Descriptive message to output upon test
* @url http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/
*/
function assertTrue($test, $message)
{
static $count;
if (!isset($count)) $count = array('pass'=>0, 'fail'=>0, 'total'=>0);
$mode = $test ? 'pass' : 'fail';
$outMode = $test ? 'PASS' : '!FAIL';
printf("%s: %s (%d of %d tests run so far have %sed)\n",
$outMode, $message, ++$count[$mode], ++$count['total'], $mode);
return (bool)$test;
}

View File

@@ -1,42 +1,42 @@
<?php
require_once '_inc.php';
require_once 'Minify/CSS.php';
function test_CSS()
{
global $thisDir;
$cssPath = dirname(__FILE__) . '/_test_files/css';
// build test file list
$d = dir($cssPath);
while (false !== ($entry = $d->read())) {
if (preg_match('/^([\w\\-]+)\.css$/', $entry, $m)) {
$list[] = $m[1];
}
}
$d->close();
foreach ($list as $item) {
$options = ($item === 'paths')
? array('prependRelativePath' => '../')
: array();
$src = file_get_contents($cssPath . "/{$item}.css");
$minExpected = file_get_contents($cssPath . "/{$item}.min.css");
$minOutput = Minify_CSS::minify($src, $options);
$passed = assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
if (!$passed) {
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
}
}
}
test_CSS();
<?php
require_once '_inc.php';
require_once 'Minify/CSS.php';
function test_CSS()
{
global $thisDir;
$cssPath = dirname(__FILE__) . '/_test_files/css';
// build test file list
$d = dir($cssPath);
while (false !== ($entry = $d->read())) {
if (preg_match('/^([\w\\-]+)\.css$/', $entry, $m)) {
$list[] = $m[1];
}
}
$d->close();
foreach ($list as $item) {
$options = ($item === 'paths')
? array('prependRelativePath' => '../')
: array();
$src = file_get_contents($cssPath . "/{$item}.css");
$minExpected = file_get_contents($cssPath . "/{$item}.min.css");
$minOutput = Minify_CSS::minify($src, $options);
$passed = assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
if (!$passed) {
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
}
}
}
test_CSS();

View File

@@ -1,59 +1,59 @@
<?php
require_once '_inc.php';
require_once 'Minify/HTML.php';
require_once 'Minify/CSS.php';
require_once 'Minify/Javascript.php';
function test_HTML()
{
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/html/before.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before.min.html');
$time = microtime(true);
$minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
));
$time = microtime(true) - $time;
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
if ($passed) {
echo "\n---Source: ", strlen($src), " bytes\n"
, "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n";
} else {
echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n"
, "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n"
, "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n";
}
}
$src = file_get_contents($thisDir . '/_test_files/html/before2.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before2.min.html');
$time = microtime(true);
$minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
));
$time = microtime(true) - $time;
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
if ($passed) {
echo "\n---Source: ", strlen($src), " bytes\n"
, "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n";
} else {
echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n"
, "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n"
, "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n";
}
}
}
test_HTML();
<?php
require_once '_inc.php';
require_once 'Minify/HTML.php';
require_once 'Minify/CSS.php';
require_once 'Minify/Javascript.php';
function test_HTML()
{
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/html/before.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before.min.html');
$time = microtime(true);
$minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
));
$time = microtime(true) - $time;
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
if ($passed) {
echo "\n---Source: ", strlen($src), " bytes\n"
, "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n";
} else {
echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n"
, "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n"
, "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n";
}
}
$src = file_get_contents($thisDir . '/_test_files/html/before2.html');
$minExpected = file_get_contents($thisDir . '/_test_files/html/before2.min.html');
$time = microtime(true);
$minOutput = Minify_HTML::minify($src, array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
));
$time = microtime(true) - $time;
$passed = assertTrue($minExpected === $minOutput, 'Minify_HTML');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
if ($passed) {
echo "\n---Source: ", strlen($src), " bytes\n"
, "---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n\n";
} else {
echo "\n---Output: ", strlen($minOutput), " bytes (", round($time * 1000), " ms)\n\n{$minOutput}\n\n"
, "---Expected: ", strlen($minExpected), " bytes\n\n{$minExpected}\n\n"
, "---Source: ", strlen($src), " bytes\n\n{$src}\n\n\n";
}
}
}
test_HTML();

View File

@@ -1,117 +1,117 @@
<?php
require_once '_inc.php';
require_once 'HTTP/ConditionalGet.php';
function test_HTTP_ConditionalGet()
{
global $thisDir;
$lmTime = time() - 900;
$gmtTime = gmdate('D, d M Y H:i:s \G\M\T', $lmTime);
$tests = array(
array(
'desc' => 'client has valid If-Modified-Since'
,'inm' => null
,'ims' => $gmtTime
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'client has valid If-Modified-Since with trailing semicolon'
,'inm' => null
,'ims' => $gmtTime . ';'
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'client has valid ETag'
,'inm' => "\"badEtagFoo\", \"{$lmTime}pri\""
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'no conditional get'
,'inm' => null
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
,array(
'desc' => 'client has invalid ETag'
,'inm' => '"' . ($lmTime - 300) . 'pri"'
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
,array(
'desc' => 'client has invalid If-Modified-Since'
,'inm' => null
,'ims' => gmdate('D, d M Y H:i:s \G\M\T', $lmTime - 300)
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
);
foreach ($tests as $test) {
// setup env
if (null === $test['inm']) {
unset($_SERVER['HTTP_IF_NONE_MATCH']);
} else {
$_SERVER['HTTP_IF_NONE_MATCH'] = get_magic_quotes_gpc()
? addslashes($test['inm'])
: $test['inm'];;
}
if (null === $test['ims']) {
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = $test['ims'];
}
$exp = $test['exp'];
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lmTime
));
$ret = $cg->getHeaders();
$ret['isValid'] = $cg->cacheIsValid;
$passed = assertTrue($exp == $ret, 'HTTP_ConditionalGet : ' . $test['desc']);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- INM = {$test['inm']} / IMS = {$test['ims']}\n";
echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
}
}
}
<?php
require_once '_inc.php';
require_once 'HTTP/ConditionalGet.php';
function test_HTTP_ConditionalGet()
{
global $thisDir;
$lmTime = time() - 900;
$gmtTime = gmdate('D, d M Y H:i:s \G\M\T', $lmTime);
$tests = array(
array(
'desc' => 'client has valid If-Modified-Since'
,'inm' => null
,'ims' => $gmtTime
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'client has valid If-Modified-Since with trailing semicolon'
,'inm' => null
,'ims' => $gmtTime . ';'
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'client has valid ETag'
,'inm' => "\"badEtagFoo\", \"{$lmTime}pri\""
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'_responseCode' => 'HTTP/1.0 304 Not Modified'
,'isValid' => true
)
)
,array(
'desc' => 'no conditional get'
,'inm' => null
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
,array(
'desc' => 'client has invalid ETag'
,'inm' => '"' . ($lmTime - 300) . 'pri"'
,'ims' => null
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
,array(
'desc' => 'client has invalid If-Modified-Since'
,'inm' => null
,'ims' => gmdate('D, d M Y H:i:s \G\M\T', $lmTime - 300)
,'exp' => array(
'Last-Modified' => $gmtTime
,'ETag' => "\"{$lmTime}pri\""
,'Cache-Control' => 'max-age=0, private, must-revalidate'
,'isValid' => false
)
)
);
foreach ($tests as $test) {
// setup env
if (null === $test['inm']) {
unset($_SERVER['HTTP_IF_NONE_MATCH']);
} else {
$_SERVER['HTTP_IF_NONE_MATCH'] = get_magic_quotes_gpc()
? addslashes($test['inm'])
: $test['inm'];;
}
if (null === $test['ims']) {
unset($_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = $test['ims'];
}
$exp = $test['exp'];
$cg = new HTTP_ConditionalGet(array(
'lastModifiedTime' => $lmTime
));
$ret = $cg->getHeaders();
$ret['isValid'] = $cg->cacheIsValid;
$passed = assertTrue($exp == $ret, 'HTTP_ConditionalGet : ' . $test['desc']);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- INM = {$test['inm']} / IMS = {$test['ims']}\n";
echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
}
}
}
test_HTTP_ConditionalGet();

View File

@@ -1,224 +1,224 @@
<?php
require_once '_inc.php';
require_once 'HTTP/Encoder.php';
function test_HTTP_Encoder()
{
global $thisDir;
$methodTests = array(
array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'recognize "x-gzip" as gzip'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0.5'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'gzip w/ non-zero q'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0'
,'exp' => array('compress', 'compress')
,'desc' => 'gzip w/ zero q'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE6 w/o "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
,'ae' => 'gzip, deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'IE6 w/ "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE5.5'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.25'
,'ae' => 'gzip,deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'Opera identifying as IE6'
)
);
foreach ($methodTests as $test) {
$_SERVER['HTTP_USER_AGENT'] = $test['ua'];
$_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae'];
$exp = $test['exp'];
$ret = HTTP_Encoder::getAcceptedEncoding();
$passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n";
echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
}
}
$variedContent = file_get_contents($thisDir . '/_test_files/html/before.html')
. file_get_contents($thisDir . '/_test_files/css/subsilver.css')
. file_get_contents($thisDir . '/../examples/jquery-1.2.3.js');
$variedLength = strlen($variedContent);
$encodingTests = array(
array('method' => 'deflate', 'inv' => 'gzinflate', 'exp' => 32157)
,array('method' => 'gzip', 'inv' => '_gzdecode', 'exp' => 32175)
,array('method' => 'compress', 'inv' => 'gzuncompress', 'exp' => 32211)
);
foreach ($encodingTests as $test) {
$e = new HTTP_Encoder(array(
'content' => $variedContent
,'method' => $test['method']
));
$e->encode(9);
$ret = strlen($e->getContent());
// test uncompression
$roundTrip = @call_user_func($test['inv'], $e->getContent());
$desc = "HTTP_Encoder : {$test['method']} : uncompress possible";
$passed = assertTrue($variedContent == $roundTrip, $desc);
// test expected compressed size
$desc = "HTTP_Encoder : {$test['method']} : compressed to "
. sprintf('%4.2f%% of original', $ret/$variedLength*100);
$passed = assertTrue(abs($ret - $test['exp']) < 100, $desc);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- {$test['method']}: expected bytes: "
, "{$test['exp']}. Returned: {$ret} "
, "(off by ". abs($ret - $test['exp']) . " bytes)\n\n";
}
}
}
test_HTTP_Encoder();
function _gzdecode($data)
{
$filename = $error = '';
return _phpman_gzdecode($data, $filename, $error);
}
// http://www.php.net/manual/en/function.gzdecode.php#82930
function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null)
{
$len = strlen($data);
if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
$error = "Not in GZIP format.";
return null; // Not GZIP format (See RFC 1952)
}
$method = ord(substr($data,2,1)); // Compression method
$flags = ord(substr($data,3,1)); // Flags
if ($flags & 31 != $flags) {
$error = "Reserved bits not allowed.";
return null;
}
// NOTE: $mtime may be negative (PHP integer limitations)
$mtime = unpack("V", substr($data,4,4));
$mtime = $mtime[1];
$xfl = substr($data,8,1);
$os = substr($data,8,1);
$headerlen = 10;
$extralen = 0;
$extra = "";
if ($flags & 4) {
// 2-byte length prefixed EXTRA data in header
if ($len - $headerlen - 2 < 8) {
return false; // invalid
}
$extralen = unpack("v",substr($data,8,2));
$extralen = $extralen[1];
if ($len - $headerlen - 2 - $extralen < 8) {
return false; // invalid
}
$extra = substr($data,10,$extralen);
$headerlen += 2 + $extralen;
}
$filenamelen = 0;
$filename = "";
if ($flags & 8) {
// C-style string
if ($len - $headerlen - 1 < 8) {
return false; // invalid
}
$filenamelen = strpos(substr($data,$headerlen),chr(0));
if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
return false; // invalid
}
$filename = substr($data,$headerlen,$filenamelen);
$headerlen += $filenamelen + 1;
}
$commentlen = 0;
$comment = "";
if ($flags & 16) {
// C-style string COMMENT data in header
if ($len - $headerlen - 1 < 8) {
return false; // invalid
}
$commentlen = strpos(substr($data,$headerlen),chr(0));
if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
return false; // Invalid header format
}
$comment = substr($data,$headerlen,$commentlen);
$headerlen += $commentlen + 1;
}
$headercrc = "";
if ($flags & 2) {
// 2-bytes (lowest order) of CRC32 on header present
if ($len - $headerlen - 2 < 8) {
return false; // invalid
}
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
$headercrc = unpack("v", substr($data,$headerlen,2));
$headercrc = $headercrc[1];
if ($headercrc != $calccrc) {
$error = "Header checksum failed.";
return false; // Bad header CRC
}
$headerlen += 2;
}
// GZIP FOOTER
$datacrc = unpack("V",substr($data,-8,4));
$datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF);
$isize = unpack("V",substr($data,-4));
$isize = $isize[1];
// decompression:
$bodylen = $len-$headerlen-8;
if ($bodylen < 1) {
// IMPLEMENTATION BUG!
return null;
}
$body = substr($data,$headerlen,$bodylen);
$data = "";
if ($bodylen > 0) {
switch ($method) {
case 8:
// Currently the only supported compression method:
$data = gzinflate($body,$maxlength);
break;
default:
$error = "Unknown compression method.";
return false;
}
} // zero-byte body content is allowed
// Verifiy CRC32
$crc = sprintf("%u",crc32($data));
$crcOK = $crc == $datacrc;
$lenOK = $isize == strlen($data);
if (!$lenOK || !$crcOK) {
$error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
return false;
}
return $data;
<?php
require_once '_inc.php';
require_once 'HTTP/Encoder.php';
function test_HTTP_Encoder()
{
global $thisDir;
$methodTests = array(
array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'recognize "x-gzip" as gzip'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0.5'
,'exp' => array('gzip', 'x-gzip')
,'desc' => 'gzip w/ non-zero q'
)
,array(
'ua' => 'Any browser'
,'ae' => 'compress, x-gzip;q=0'
,'exp' => array('compress', 'compress')
,'desc' => 'gzip w/ zero q'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE6 w/o "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
,'ae' => 'gzip, deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'IE6 w/ "enhanced security"'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)'
,'ae' => 'gzip, deflate'
,'exp' => array('', '')
,'desc' => 'IE5.5'
)
,array(
'ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.25'
,'ae' => 'gzip,deflate'
,'exp' => array('deflate', 'deflate')
,'desc' => 'Opera identifying as IE6'
)
);
foreach ($methodTests as $test) {
$_SERVER['HTTP_USER_AGENT'] = $test['ua'];
$_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae'];
$exp = $test['exp'];
$ret = HTTP_Encoder::getAcceptedEncoding();
$passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n";
echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
}
}
$variedContent = file_get_contents($thisDir . '/_test_files/html/before.html')
. file_get_contents($thisDir . '/_test_files/css/subsilver.css')
. file_get_contents($thisDir . '/../examples/jquery-1.2.3.js');
$variedLength = strlen($variedContent);
$encodingTests = array(
array('method' => 'deflate', 'inv' => 'gzinflate', 'exp' => 32157)
,array('method' => 'gzip', 'inv' => '_gzdecode', 'exp' => 32175)
,array('method' => 'compress', 'inv' => 'gzuncompress', 'exp' => 32211)
);
foreach ($encodingTests as $test) {
$e = new HTTP_Encoder(array(
'content' => $variedContent
,'method' => $test['method']
));
$e->encode(9);
$ret = strlen($e->getContent());
// test uncompression
$roundTrip = @call_user_func($test['inv'], $e->getContent());
$desc = "HTTP_Encoder : {$test['method']} : uncompress possible";
$passed = assertTrue($variedContent == $roundTrip, $desc);
// test expected compressed size
$desc = "HTTP_Encoder : {$test['method']} : compressed to "
. sprintf('%4.2f%% of original', $ret/$variedLength*100);
$passed = assertTrue(abs($ret - $test['exp']) < 100, $desc);
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n--- {$test['method']}: expected bytes: "
, "{$test['exp']}. Returned: {$ret} "
, "(off by ". abs($ret - $test['exp']) . " bytes)\n\n";
}
}
}
test_HTTP_Encoder();
function _gzdecode($data)
{
$filename = $error = '';
return _phpman_gzdecode($data, $filename, $error);
}
// http://www.php.net/manual/en/function.gzdecode.php#82930
function _phpman_gzdecode($data, &$filename='', &$error='', $maxlength=null)
{
$len = strlen($data);
if ($len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
$error = "Not in GZIP format.";
return null; // Not GZIP format (See RFC 1952)
}
$method = ord(substr($data,2,1)); // Compression method
$flags = ord(substr($data,3,1)); // Flags
if ($flags & 31 != $flags) {
$error = "Reserved bits not allowed.";
return null;
}
// NOTE: $mtime may be negative (PHP integer limitations)
$mtime = unpack("V", substr($data,4,4));
$mtime = $mtime[1];
$xfl = substr($data,8,1);
$os = substr($data,8,1);
$headerlen = 10;
$extralen = 0;
$extra = "";
if ($flags & 4) {
// 2-byte length prefixed EXTRA data in header
if ($len - $headerlen - 2 < 8) {
return false; // invalid
}
$extralen = unpack("v",substr($data,8,2));
$extralen = $extralen[1];
if ($len - $headerlen - 2 - $extralen < 8) {
return false; // invalid
}
$extra = substr($data,10,$extralen);
$headerlen += 2 + $extralen;
}
$filenamelen = 0;
$filename = "";
if ($flags & 8) {
// C-style string
if ($len - $headerlen - 1 < 8) {
return false; // invalid
}
$filenamelen = strpos(substr($data,$headerlen),chr(0));
if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
return false; // invalid
}
$filename = substr($data,$headerlen,$filenamelen);
$headerlen += $filenamelen + 1;
}
$commentlen = 0;
$comment = "";
if ($flags & 16) {
// C-style string COMMENT data in header
if ($len - $headerlen - 1 < 8) {
return false; // invalid
}
$commentlen = strpos(substr($data,$headerlen),chr(0));
if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
return false; // Invalid header format
}
$comment = substr($data,$headerlen,$commentlen);
$headerlen += $commentlen + 1;
}
$headercrc = "";
if ($flags & 2) {
// 2-bytes (lowest order) of CRC32 on header present
if ($len - $headerlen - 2 < 8) {
return false; // invalid
}
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
$headercrc = unpack("v", substr($data,$headerlen,2));
$headercrc = $headercrc[1];
if ($headercrc != $calccrc) {
$error = "Header checksum failed.";
return false; // Bad header CRC
}
$headerlen += 2;
}
// GZIP FOOTER
$datacrc = unpack("V",substr($data,-8,4));
$datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF);
$isize = unpack("V",substr($data,-4));
$isize = $isize[1];
// decompression:
$bodylen = $len-$headerlen-8;
if ($bodylen < 1) {
// IMPLEMENTATION BUG!
return null;
}
$body = substr($data,$headerlen,$bodylen);
$data = "";
if ($bodylen > 0) {
switch ($method) {
case 8:
// Currently the only supported compression method:
$data = gzinflate($body,$maxlength);
break;
default:
$error = "Unknown compression method.";
return false;
}
} // zero-byte body content is allowed
// Verifiy CRC32
$crc = sprintf("%u",crc32($data));
$crcOK = $crc == $datacrc;
$lenOK = $isize == strlen($data);
if (!$lenOK || !$crcOK) {
$error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
return false;
}
return $data;
}

View File

@@ -1,37 +1,37 @@
<?php
require_once '_inc.php';
require_once 'Minify/Javascript.php';
function test_Javascript()
{
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
$minOutput = Minify_Javascript::minify($src);
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
//$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before_noComments.min.js');
$minOutput = Minify_Javascript::minify($src, array(
'preserveComments' => false
));
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
}
test_Javascript();
<?php
require_once '_inc.php';
require_once 'Minify/Javascript.php';
function test_Javascript()
{
global $thisDir;
$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before.min.js');
$minOutput = Minify_Javascript::minify($src);
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
//$src = file_get_contents($thisDir . '/_test_files/js/before.js');
$minExpected = file_get_contents($thisDir . '/_test_files/js/before_noComments.min.js');
$minOutput = Minify_Javascript::minify($src, array(
'preserveComments' => false
));
$passed = assertTrue($minExpected == $minOutput, 'Minify_Javascript');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n";
echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n";
echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n";
}
}
test_Javascript();

View File

@@ -1,33 +1,33 @@
<?php
require_once '_inc.php';
require_once 'Minify.php';
function test_Lines()
{
global $thisDir;
$exp = file_get_contents("{$thisDir}/_test_files/minify/lines_output.js");
$ret = Minify::serve('Files', array(
'debug' => true
,'quiet' => true
,'encodeOutput' => false
,'files' => array(
"{$thisDir}/_test_files/minify/email.js"
,"{$thisDir}/_test_files/minify/QueryString.js"
,"{$thisDir}/_test_files/js/before.js"
)
));
$passed = assertTrue($exp === $ret['content'], 'Minify_Lines');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($ret['content']). " bytes\n\n{$ret['content']}\n\n";
if (!$passed) {
echo "---Expected: " .strlen($exp). " bytes\n\n{$exp}\n\n\n";
}
}
}
<?php
require_once '_inc.php';
require_once 'Minify.php';
function test_Lines()
{
global $thisDir;
$exp = file_get_contents("{$thisDir}/_test_files/minify/lines_output.js");
$ret = Minify::serve('Files', array(
'debug' => true
,'quiet' => true
,'encodeOutput' => false
,'files' => array(
"{$thisDir}/_test_files/minify/email.js"
,"{$thisDir}/_test_files/minify/QueryString.js"
,"{$thisDir}/_test_files/js/before.js"
)
));
$passed = assertTrue($exp === $ret['content'], 'Minify_Lines');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\n---Output: " .strlen($ret['content']). " bytes\n\n{$ret['content']}\n\n";
if (!$passed) {
echo "---Expected: " .strlen($exp). " bytes\n\n{$exp}\n\n\n";
}
}
}
test_Lines();

View File

@@ -1,101 +1,101 @@
<?php
// currently these only test serve() when passed the 'quiet' options
require_once '_inc.php';
require_once 'Minify.php';
function test_Minify()
{
global $thisDir;
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
$tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
$lastModified = $_SERVER['REQUEST_TIME'] - 86400;
// Test 304 response
// simulate conditional headers
$_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
$expected = array (
'success' => true
,'statusCode' => 304
,'content' => '',
'headers' => array(
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=1800, public, must-revalidate',
'_responseCode' => 'HTTP/1.0 304 Not Modified',
)
);
$output = Minify::serve('Files', array(
'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
,'quiet' => true
,'lastModifiedTime' => $lastModified
,'encodeOutput' => false
));
$passed = assertTrue($expected === $output, 'Minify : 304 response');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
assertTrue(
//! class_exists('Cache_Lite_File', false)
! class_exists('HTTP_Encoder', false)
&& ! class_exists('Minify_CSS', false)
&& ! class_exists('Minify_Cache', false)
,'Encoder.php, CSS.php, Cache.php not loaded'
);
// Test minifying JS and serving with Expires header
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
$lastModified = filemtime($minifyTestPath . '/minified.js');
$expected = array(
'success' => true
,'statusCode' => 200
// Minify_Javascript always converts to \n line endings
,'content' => $content
,'headers' => array (
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=86400, public, must-revalidate',
'Content-Length' => strlen($content),
'Content-Type' => 'application/x-javascript; charset=UTF-8',
)
);
$output = Minify::serve('Files', array(
'files' => array(
$minifyTestPath . '/email.js'
,$minifyTestPath . '/QueryString.js'
)
,'quiet' => true
,'maxAge' => 86400
,'encodeOutput' => false
));
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
// Test minifying CSS and responding with Etag/Last-Modified
// needed to expose E_STRICT warning in Cache_Lite_File
Minify::setCache();
// don't allow conditional headers
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
<?php
// currently these only test serve() when passed the 'quiet' options
require_once '_inc.php';
require_once 'Minify.php';
function test_Minify()
{
global $thisDir;
$minifyTestPath = dirname(__FILE__) . '/_test_files/minify';
$tomorrow = $_SERVER['REQUEST_TIME'] + 86400;
$lastModified = $_SERVER['REQUEST_TIME'] - 86400;
// Test 304 response
// simulate conditional headers
$_SERVER['HTTP_IF_NONE_MATCH'] = "\"{$lastModified}pub\"";
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = gmdate('D, d M Y H:i:s \G\M\T', $lastModified);
$expected = array (
'success' => true
,'statusCode' => 304
,'content' => '',
'headers' => array(
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $_SERVER['REQUEST_TIME'] + 1800),
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=1800, public, must-revalidate',
'_responseCode' => 'HTTP/1.0 304 Not Modified',
)
);
$output = Minify::serve('Files', array(
'files' => $thisDir . '/_test_files/css/styles.css' // controller casts to array
,'quiet' => true
,'lastModifiedTime' => $lastModified
,'encodeOutput' => false
));
$passed = assertTrue($expected === $output, 'Minify : 304 response');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
assertTrue(
//! class_exists('Cache_Lite_File', false)
! class_exists('HTTP_Encoder', false)
&& ! class_exists('Minify_CSS', false)
&& ! class_exists('Minify_Cache', false)
,'Encoder.php, CSS.php, Cache.php not loaded'
);
// Test minifying JS and serving with Expires header
$content = preg_replace('/\\r\\n?/', "\n", file_get_contents($minifyTestPath . '/minified.js'));
$lastModified = filemtime($minifyTestPath . '/minified.js');
$expected = array(
'success' => true
,'statusCode' => 200
// Minify_Javascript always converts to \n line endings
,'content' => $content
,'headers' => array (
'Expires' => gmdate('D, d M Y H:i:s \G\M\T', $tomorrow),
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=86400, public, must-revalidate',
'Content-Length' => strlen($content),
'Content-Type' => 'application/x-javascript; charset=UTF-8',
)
);
$output = Minify::serve('Files', array(
'files' => array(
$minifyTestPath . '/email.js'
,$minifyTestPath . '/QueryString.js'
)
,'quiet' => true
,'maxAge' => 86400
,'encodeOutput' => false
));
$passed = assertTrue($expected === $output, 'Minify : JS and Expires');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
// Test minifying CSS and responding with Etag/Last-Modified
// needed to expose E_STRICT warning in Cache_Lite_File
Minify::setCache();
// don't allow conditional headers
unset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_IF_MODIFIED_SINCE']);
$pathToWebTest = str_replace(
DIRECTORY_SEPARATOR
@@ -107,37 +107,37 @@ function test_Minify()
,$pathToWebTest
,file_get_contents($minifyTestPath . '/minified.css')
);
$expected = array(
'success' => true
,'statusCode' => 200
,'content' => $expectedContent
,'headers' => array (
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=0, public, must-revalidate',
'Content-Length' => strlen($expectedContent),
'Content-Type' => 'text/css; charset=UTF-8',
)
);
$output = Minify::serve('Files', array(
'files' => array(
$thisDir . '/_test_files/css/styles.css'
,$thisDir . '/_test_files/css/subsilver.css'
)
,'quiet' => true
,'lastModifiedTime' => $lastModified
,'encodeOutput' => false
$expected = array(
'success' => true
,'statusCode' => 200
,'content' => $expectedContent
,'headers' => array (
'Last-Modified' => gmdate('D, d M Y H:i:s \G\M\T', $lastModified),
'ETag' => "\"{$lastModified}pub\"",
'Cache-Control' => 'max-age=0, public, must-revalidate',
'Content-Length' => strlen($expectedContent),
'Content-Type' => 'text/css; charset=UTF-8',
)
);
$output = Minify::serve('Files', array(
'files' => array(
$thisDir . '/_test_files/css/styles.css'
,$thisDir . '/_test_files/css/subsilver.css'
)
,'quiet' => true
,'lastModifiedTime' => $lastModified
,'encodeOutput' => false
,'maxAge' => false
));
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
}
test_Minify();
));
$passed = assertTrue($expected === $output, 'Minify : CSS and Etag/Last-Modified');
if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
echo "\nOutput: " .var_export($output, 1). "\n\n";
if (! $passed) {
echo "\n\n\n\n---Expected: " .var_export($expected, 1). "\n\n";
}
}
}
test_Minify();

View File

@@ -1,35 +1,35 @@
<?php
require_once '_inc.php';
require_once 'Minify/Build.php';
function test_Minify_Build()
{
global $thisDir;
$file1 = $thisDir . '/_test_files/css/paths.css';
$file2 = $thisDir . '/_test_files/css/styles.css';
$maxTime = max(filemtime($file1), filemtime($file2));
$b = new Minify_Build($file1);
assertTrue($b->lastModified == filemtime($file1)
,'Minify_Build : single file path');
$b = new Minify_Build(array($file1, $file2));
assertTrue($maxTime == $b->lastModified
,'Minify_Build : multiple file paths');
$b = new Minify_Build(array(
$file1
,new Minify_Source(array('filepath' => $file2))
));
assertTrue($maxTime == $b->lastModified
,'Minify_Build : file path and a Minify_Source');
assertTrue($b->uri('/path') == "/path?{$maxTime}"
,'Minify_Build : uri() with no querystring');
assertTrue($b->uri('/path?hello') == "/path?hello&amp;{$maxTime}"
,'Minify_Build : uri() with existing querystring');
}
<?php
require_once '_inc.php';
require_once 'Minify/Build.php';
function test_Minify_Build()
{
global $thisDir;
$file1 = $thisDir . '/_test_files/css/paths.css';
$file2 = $thisDir . '/_test_files/css/styles.css';
$maxTime = max(filemtime($file1), filemtime($file2));
$b = new Minify_Build($file1);
assertTrue($b->lastModified == filemtime($file1)
,'Minify_Build : single file path');
$b = new Minify_Build(array($file1, $file2));
assertTrue($maxTime == $b->lastModified
,'Minify_Build : multiple file paths');
$b = new Minify_Build(array(
$file1
,new Minify_Source(array('filepath' => $file2))
));
assertTrue($maxTime == $b->lastModified
,'Minify_Build : file path and a Minify_Source');
assertTrue($b->uri('/path') == "/path?{$maxTime}"
,'Minify_Build : uri() with no querystring');
assertTrue($b->uri('/path?hello') == "/path?hello&amp;{$maxTime}"
,'Minify_Build : uri() with existing querystring');
}
test_Minify_Build();

View File

@@ -1,10 +1,10 @@
<?php
require 'test_Minify.php';
require 'test_Javascript.php';
require 'test_CSS.php';
require 'test_Lines.php';
require 'test_HTML.php';
require 'test_Minify_Build.php';
require 'test_HTTP_Encoder.php';
require 'test_HTTP_ConditionalGet.php';
<?php
require 'test_Minify.php';
require 'test_Javascript.php';
require 'test_CSS.php';
require 'test_Lines.php';
require 'test_HTML.php';
require 'test_Minify_Build.php';
require 'test_HTTP_Encoder.php';
require 'test_HTTP_ConditionalGet.php';

View File

@@ -1,32 +1,32 @@
<?php
if (isset($_FILES['subject']['name'])) {
require '../../min/lib/HTTP/Encoder.php';
$he = new HTTP_Encoder(array(
'content' => file_get_contents($_FILES['subject']['tmp_name'])
,'method' => $_POST['method']
));
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=\"{$_FILES['subject']['name']}."
. ($_POST['method'] == 'deflate'
? 'zd'
: ($_POST['method'] == 'gzip'
? 'zg'
: 'zc'
)
) . '"');
$he->encode(9);
echo $he->getContent();
exit();
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p>Encode <input type="file" name="subject" /><br />
as <input type="submit" name="method" value="deflate" />
<input type="submit" name="method" value="gzip" />
<input type="submit" name="method" value="compress" />
</p>
<?php
if (isset($_FILES['subject']['name'])) {
require '../../min/lib/HTTP/Encoder.php';
$he = new HTTP_Encoder(array(
'content' => file_get_contents($_FILES['subject']['tmp_name'])
,'method' => $_POST['method']
));
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header("Content-Disposition: attachment; filename=\"{$_FILES['subject']['name']}."
. ($_POST['method'] == 'deflate'
? 'zd'
: ($_POST['method'] == 'gzip'
? 'zg'
: 'zc'
)
) . '"');
$he->encode(9);
echo $he->getContent();
exit();
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p>Encode <input type="file" name="subject" /><br />
as <input type="submit" name="method" value="deflate" />
<input type="submit" name="method" value="gzip" />
<input type="submit" name="method" value="compress" />
</p>
</form>

View File

@@ -1,50 +1,53 @@
<?php
if (isset($_FILES['subject']['name'])
&& preg_match('/\\.(js|css|html)$/', $_FILES['subject']['name'], $m)
) {
ini_set('include_path',
dirname(__FILE__) . '/../../min/lib'
. PATH_SEPARATOR . ini_get('include_path')
);
// eh why not
require 'Minify/HTML.php';
require 'Minify/CSS.php';
require 'Minify/Javascript.php';
$arg2 = null;
switch ($m[1]) {
case 'js':
$type = 'Javascript';
break;
case 'css':
$type = 'CSS';
break;
case 'html':
$type = 'HTML';
$arg2 = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
);
}
$func = array('Minify_' . $type, 'minify');
$out = call_user_func($func, file_get_contents($_FILES['subject']['tmp_name']), $arg2);
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'
. preg_replace('/\\.(\w+)$/', '.min.$1', $_FILES['subject']['name'])
. '"');
echo $out;
exit();
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p>Minify <input type="file" name="subject" /><br />
<input type="submit" name="method" value="Go!" />
</p>
<?php
if (isset($_FILES['subject']['name'])
&& preg_match('/\\.(js|css|x?html?)$/', $_FILES['subject']['name'], $m)
) {
ini_set('include_path',
dirname(__FILE__) . '/../../min/lib'
. PATH_SEPARATOR . ini_get('include_path')
);
// eh why not
require 'Minify/HTML.php';
require 'Minify/CSS.php';
require 'Minify/Javascript.php';
$arg2 = null;
switch ($m[1]) {
case 'js':
$type = 'Javascript';
break;
case 'css':
$type = 'CSS';
break;
case 'html': // fallthrough
case 'htm': // fallthrough
case 'xhtml':
$type = 'HTML';
$arg2 = array(
'cssMinifier' => array('Minify_CSS', 'minify')
,'jsMinifier' => array('Minify_Javascript', 'minify')
);
}
$func = array('Minify_' . $type, 'minify');
$out = call_user_func($func, file_get_contents($_FILES['subject']['tmp_name']), $arg2);
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'
. preg_replace('/\\.(\w+)$/', '.min.$1', $_FILES['subject']['name'])
. '"');
//@unlink($_FILES['subject']['tmp_name']);
echo $out;
exit();
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p>Minify <input type="file" name="subject" /><br />
<input type="submit" name="method" value="Go!" />
</p>
</form>

View File

@@ -1,6 +1,6 @@
<?php
require '../config.php'; // just to set include_path
require 'Minify.php';
Minify::serve('Version1');
<?php
require '../config.php'; // just to set include_path
require 'Minify.php';
Minify::serve('Version1');