1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-26 23:34:44 +02:00

Fix Files and Groups controllers, cleanup unused stuff

This commit is contained in:
Steve Clay
2014-09-23 11:47:20 -04:00
parent 9716fe802c
commit beb750df3e
6 changed files with 47 additions and 198 deletions

View File

@@ -220,12 +220,13 @@ by Minify. E.g. <code>@import "<span class=minRoot>/min/?</span>g=css2";</code><
<?php
$content = ob_get_clean();
if (empty($min_cachePath)) {
$cache = new Minify_Cache_File('', $min_cacheFileLocking);
} elseif (is_object($min_cachePath)) {
$cache = $min_cachePath;
} else {
if (!isset($min_cachePath)) {
$min_cachePath = '';
}
if (is_string($min_cachePath)) {
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
} else {
$cache = $min_cachePath;
}
$env = new Minify_Env();

View File

@@ -1,6 +1,6 @@
<?php
/**
* Front controller for default Minify implementation
* Sets up MinApp controller and serves files
*
* DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
*
@@ -31,30 +31,28 @@ if (isset($_GET['test'])) {
require "$min_libPath/Minify/Loader.php";
Minify_Loader::register();
// use an environment object to encapsulate all input
$server = $_SERVER;
if ($min_documentRoot) {
$server['DOCUMENT_ROOT'] = $min_documentRoot;
}
$env = new Minify_Env(array(
'server' => $server,
// move these...
'allowDebug' => $min_allowDebugFlag,
'uploaderHoursBehind' => $min_uploaderHoursBehind,
));
// setup cache
if (!isset($min_cachePath)) {
$cache = new Minify_Cache_File('', $min_cacheFileLocking);
} elseif (is_object($min_cachePath)) {
// let type hinting catch type error
$cache = $min_cachePath;
} else {
$min_cachePath = '';
}
if (is_string($min_cachePath)) {
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
} else {
$cache = $min_cachePath;
}
$server = new Minify($cache);
// TODO probably should do this elsewhere...
$min_serveOptions['minifierOptions']['text/css']['docRoot'] = $env->getDocRoot();
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
// auto-add targets to allowDirs
@@ -63,6 +61,7 @@ foreach ($min_symlinks as $uri => $target) {
}
if ($min_allowDebugFlag) {
// TODO get rid of static stuff
$min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
}
@@ -70,6 +69,7 @@ if ($min_errorLogger) {
if (true === $min_errorLogger) {
$min_errorLogger = FirePHP::getInstance(true);
}
// TODO get rid of global state
Minify_Logger::setLogger($min_errorLogger);
}
@@ -89,6 +89,8 @@ if ($env->get('f') || null !== $env->get('g')) {
if (! isset($min_serveController)) {
$sourceFactoryOptions = array();
// translate legacy setting to option for source factory
if (isset($min_serveOptions['minApp']['noMinPattern'])) {
$sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
}

View File

@@ -24,11 +24,6 @@ abstract class Minify_Controller_Base implements Minify_ControllerInterface {
*/
protected $sourceFactory;
/**
* @var string
*/
protected $type;
/**
* @param Minify_Env $env
* @param Minify_Source_Factory $sourceFactory

View File

@@ -17,9 +17,6 @@
* )
* ));
* </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>
@@ -30,13 +27,13 @@ class Minify_Controller_Files extends Minify_Controller_Base {
* Set up file sources
*
* @param array $options controller and Minify options
* @return array Minify options
* @return Minify_ServeConfiguration
*
* Controller options:
*
* 'files': (required) array of complete file paths, or a single path
*/
public function createConfiguration($options) {
public function createConfiguration(array $options) {
// strip controller options
$files = $options['files'];
@@ -50,27 +47,20 @@ class Minify_Controller_Files extends Minify_Controller_Base {
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
if ($file instanceof Minify_SourceInterface) {
$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;
try {
$sources[] = $this->sourceFactory->makeSource(array(
'filepath' => $file,
));
} catch (Minify_Source_FactoryException $e) {
$this->log($e->getMessage());
return new Minify_ServeConfiguration($options);
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
return new Minify_ServeConfiguration($options, $sources);
}
}

View File

@@ -20,13 +20,10 @@
* 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 {
class Minify_Controller_Groups extends Minify_Controller_Files {
/**
* Set up groups of files as sources
@@ -38,54 +35,37 @@ class Minify_Controller_Groups extends Minify_Controller_Base {
*
* @return array Minify options
*/
public function createConfiguration($options) {
public function createConfiguration(array $options) {
// strip controller options
$groups = $options['groups'];
unset($options['groups']);
$server = $this->env->server();
// 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)
$pathInfo = 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])) {
if (false === $pathInfo || ! isset($groups[$pathInfo])) {
// no PATH_INFO or not a valid group
$this->log("Missing PATH_INFO or no group set for \"$pi\"");
return $options;
$this->log("Missing PATH_INFO or no group set for \"$pathInfo\"");
return new Minify_ServeConfiguration($options);
}
$sources = array();
$files = $groups[$pi];
$files = $groups[$pathInfo];
// 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;
$options['files'] = $files;
return parent::createConfiguration($options);
}
}

View File

@@ -1,119 +0,0 @@
<?php
/**
* Class Minify_Controller_Version1
* @package Minify
*/
/**
* 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 createConfiguration($options) {
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
if (isset($_GET['files'])) {
$_GET['files'] = str_replace("\x00", '', (string)$_GET['files']);
}
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;
}
$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'];
$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);
}
}
}
}