inc/contrib -> inc/lib

This commit is contained in:
Michael Save 2012-04-09 20:52:26 +10:00
parent 69c9a9dd45
commit 874b8cbf00
186 changed files with 6 additions and 6 deletions

View file

@ -0,0 +1,250 @@
<?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>
*/
abstract class Minify_Controller_Base {
/**
* Setup controller sources and set an needed options for Minify::source
*
* You must override this method in your subclass controller to set
* $this->sources. If the request is NOT valid, make sure $this->sources
* is left an empty array. Then strip any controller-specific options from
* $options and return it. To serve files, $this->sources must be an array of
* Minify_Source objects.
*
* @param array $options controller and Minify options
*
* @return array $options Minify::serve options
*/
abstract public function setupSources($options);
/**
* Get default Minify options for this controller.
*
* Override in subclass to change defaults
*
* @return array options for Minify
*/
public function getDefaultMinifyOptions() {
return array(
'isPublic' => true
,'encodeOutput' => function_exists('gzdeflate')
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'utf-8'
,'maxAge' => 1800 // 30 minutes
,'rewriteCssUris' => true
,'bubbleCssImports' => false
,'quiet' => false // serve() will send headers and output
,'debug' => false
// if you override these, the response codes MUST be directly after
// the first space.
,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
,'errorHeader' => 'HTTP/1.0 500 Internal Server Error'
// 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('JSMin', 'minify');
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
return $ret;
}
/**
* Load any code necessary to execute the given minifier callback.
*
* The controller is responsible for loading minification code on demand
* via this method. This built-in function will only load classes for
* static method callbacks where the class isn't already defined. It uses
* the PEAR convention, so, given array('Jimmy_Minifier', 'minCss'), this
* function will include 'Jimmy/Minifier.php'.
*
* 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. Files can also
* be in subdirectories of these directories.
*
* @return bool file is safe
*
* @deprecated use checkAllowDirs, checkNotHidden instead
*/
public static function _fileIsSafe($file, $safeDirs)
{
$pathOk = false;
foreach ((array)$safeDirs as $safeDir) {
if (strpos($file, $safeDir) === 0) {
$pathOk = true;
break;
}
}
$base = basename($file);
if (! $pathOk || ! is_file($file) || $base[0] === '.') {
return false;
}
list($revExt) = explode('.', strrev($base));
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
}
/**
* @param string $file
* @param array $allowDirs
* @param string $uri
* @return bool
* @throws Exception
*/
public static function checkAllowDirs($file, $allowDirs, $uri)
{
foreach ((array)$allowDirs as $allowDir) {
if (strpos($file, $allowDir) === 0) {
return true;
}
}
throw new Exception("File '$file' is outside \$allowDirs. If the path is"
. " resolved via an alias/symlink, look into the \$min_symlinks option."
. " E.g. \$min_symlinks['/" . dirname($uri) . "'] = '" . dirname($file) . "';");
}
/**
* @param string $file
* @throws Exception
*/
public static function checkNotHidden($file)
{
$b = basename($file);
if (0 === strpos($b, '.')) {
throw new Exception("Filename '$b' starts with period (may be hidden)");
}
}
/**
* instances of Minify_Source, which provide content and any individual minification needs.
*
* @var array
*
* @see Minify_Source
*/
public $sources = array();
/**
* Short name to place inside cache id
*
* The setupSources() method may choose to set this, making it easier to
* recognize a particular set of sources/settings in the cache folder. It
* will be filtered and truncated to make the final cache id <= 250 bytes.
*
* @var string
*/
public $selectionId = '';
/**
* 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;
}
/**
* Send message to the Minify logger
*
* @param string $msg
*
* @return null
*/
public function log($msg) {
require_once 'Minify/Logger.php';
Minify_Logger::log($msg);
}
}

View file

@ -0,0 +1,78 @@
<?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 = $options['files'];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
unset($options['files']);
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $realPath
));
} else {
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View file

@ -0,0 +1,93 @@
<?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
*
* 'groups': (required) array mapping PATH_INFO strings to arrays
* of complete file paths. @see Minify_Controller_Groups
*
* @return array Minify options
*/
public function setupSources($options) {
// strip controller options
$groups = $options['groups'];
unset($options['groups']);
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO
$pi = isset($_SERVER['ORIG_PATH_INFO'])
? substr($_SERVER['ORIG_PATH_INFO'], 1)
: (isset($_SERVER['PATH_INFO'])
? substr($_SERVER['PATH_INFO'], 1)
: false
);
if (false === $pi || ! isset($groups[$pi])) {
// no PATH_INFO or not a valid group
$this->log("Missing PATH_INFO or no group set for \"$pi\"");
return $options;
}
$sources = array();
$files = $groups[$pi];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $realPath
));
} else {
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View file

@ -0,0 +1,231 @@
<?php
/**
* Class Minify_Controller_MinApp
* @package Minify
*/
require_once 'Minify/Controller/Base.php';
/**
* Controller class for requests to /min/index.php
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_MinApp extends Minify_Controller_Base {
/**
* Set up groups of files as sources
*
* @param array $options controller and Minify options
*
* @return array Minify options
*/
public function setupSources($options) {
// filter controller options
$cOptions = array_merge(
array(
'allowDirs' => '//'
,'groupsOnly' => false
,'groups' => array()
,'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i' // matched against basename
)
,(isset($options['minApp']) ? $options['minApp'] : array())
);
unset($options['minApp']);
$sources = array();
$this->selectionId = '';
$firstMissingResource = null;
if (isset($_GET['g'])) {
// add group(s)
$this->selectionId .= 'g=' . $_GET['g'];
$keys = explode(',', $_GET['g']);
if ($keys != array_unique($keys)) {
$this->log("Duplicate group key found.");
return $options;
}
$keys = explode(',', $_GET['g']);
foreach ($keys as $key) {
if (! isset($cOptions['groups'][$key])) {
$this->log("A group configuration for \"{$key}\" was not found");
return $options;
}
$files = $cOptions['groups'][$key];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
}
$realpath = realpath($file);
if ($realpath && is_file($realpath)) {
$sources[] = $this->_getFileSource($realpath, $cOptions);
} else {
$this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
if (null === $firstMissingResource) {
$firstMissingResource = basename($file);
continue;
} else {
$secondMissingResource = basename($file);
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
return $options;
}
}
}
if ($sources) {
try {
$this->checkType($sources[0]);
} catch (Exception $e) {
$this->log($e->getMessage());
return $options;
}
}
}
}
if (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
// try user files
// The following restrictions are to limit the URLs that minify will
// respond to.
if (// verify at least one file, files are single comma separated,
// and are all same extension
! preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'], $m)
// no "//"
|| strpos($_GET['f'], '//') !== false
// no "\"
|| strpos($_GET['f'], '\\') !== false
) {
$this->log("GET param 'f' was invalid");
return $options;
}
$ext = ".{$m[1]}";
try {
$this->checkType($m[1]);
} catch (Exception $e) {
$this->log($e->getMessage());
return $options;
}
$files = explode(',', $_GET['f']);
if ($files != array_unique($files)) {
$this->log("Duplicate files were specified");
return $options;
}
if (isset($_GET['b'])) {
// check for validity
if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b'])
&& false === strpos($_GET['b'], '..')
&& $_GET['b'] !== '.') {
// valid base
$base = "/{$_GET['b']}/";
} else {
$this->log("GET param 'b' was invalid");
return $options;
}
} else {
$base = '/';
}
$allowDirs = array();
foreach ((array)$cOptions['allowDirs'] as $allowDir) {
$allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
}
$basenames = array(); // just for cache id
foreach ($files as $file) {
$uri = $base . $file;
$path = $_SERVER['DOCUMENT_ROOT'] . $uri;
$realpath = realpath($path);
if (false === $realpath || ! is_file($realpath)) {
$this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
if (null === $firstMissingResource) {
$firstMissingResource = $uri;
continue;
} else {
$secondMissingResource = $uri;
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
return $options;
}
}
try {
parent::checkNotHidden($realpath);
parent::checkAllowDirs($realpath, $allowDirs, $uri);
} catch (Exception $e) {
$this->log($e->getMessage());
return $options;
}
$sources[] = $this->_getFileSource($realpath, $cOptions);
$basenames[] = basename($realpath, $ext);
}
if ($this->selectionId) {
$this->selectionId .= '_f=';
}
$this->selectionId .= implode(',', $basenames) . $ext;
}
if ($sources) {
if (null !== $firstMissingResource) {
array_unshift($sources, new Minify_Source(array(
'id' => 'missingFile'
// should not cause cache invalidation
,'lastModified' => 0
// due to caching, filename is unreliable.
,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
,'minifier' => ''
)));
}
$this->sources = $sources;
} else {
$this->log("No sources to serve");
}
return $options;
}
/**
* @param string $file
*
* @param array $cOptions
*
* @return Minify_Source
*/
protected function _getFileSource($file, $cOptions)
{
$spec['filepath'] = $file;
if ($cOptions['noMinPattern']
&& preg_match($cOptions['noMinPattern'], basename($file))) {
$spec['minifier'] = '';
}
return new Minify_Source($spec);
}
protected $_type = null;
/**
* Make sure that only source files of a single type are registered
*
* @param string $sourceOrExt
*
* @throws Exception
*/
public function checkType($sourceOrExt)
{
if ($sourceOrExt === 'js') {
$type = Minify::TYPE_JS;
} elseif ($sourceOrExt === 'css') {
$type = Minify::TYPE_CSS;
} elseif ($sourceOrExt->contentType !== null) {
$type = $sourceOrExt->contentType;
} else {
return;
}
if ($this->_type === null) {
$this->_type = $type;
} elseif ($this->_type !== $type) {
throw new Exception('Content-Type mismatch');
}
}
}

View file

@ -0,0 +1,87 @@
<?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) {
if (isset($options['file'])) {
$sourceSpec = array(
'filepath' => $options['file']
);
$f = $options['file'];
} else {
// strip controller options
$sourceSpec = array(
'content' => $options['content']
,'id' => $options['id']
);
$f = $options['id'];
unset($options['content'], $options['id']);
}
// something like "builder,index.php" or "directory,file.html"
$this->selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,');
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('JSMin', 'minify')
);
$this->_loadCssJsMinifiers = true;
unset($options['minifyAll']);
}
$this->sources[] = new Minify_Source($sourceSpec);
$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_once 'Minify/CSS.php';
require_once 'JSMin.php';
}
parent::loadMinifier($minifierCallback); // load Minify/HTML.php
}
}

View file

@ -0,0 +1,118 @@
<?php
/**
* Class Minify_Controller_Version1
* @package Minify
*/
require_once 'Minify/Controller/Base.php';
/**
* Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
*
* <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();
}
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);
}
}
}
}