diff --git a/min/builder/index.php b/min/builder/index.php index 258fa72..35e10a9 100644 --- a/min/builder/index.php +++ b/min/builder/index.php @@ -220,12 +220,13 @@ by Minify. E.g. @import "/min/?g=css2";< $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']; } diff --git a/min/lib/Minify/Controller/Base.php b/min/lib/Minify/Controller/Base.php index 6c83a78..b601d77 100644 --- a/min/lib/Minify/Controller/Base.php +++ b/min/lib/Minify/Controller/Base.php @@ -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 diff --git a/min/lib/Minify/Controller/Files.php b/min/lib/Minify/Controller/Files.php index d70b8cd..29cf712 100644 --- a/min/lib/Minify/Controller/Files.php +++ b/min/lib/Minify/Controller/Files.php @@ -17,9 +17,6 @@ * ) * )); * - * - * As a shortcut, the controller will replace "//" at the beginning - * of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'. * * @package Minify * @author Stephen Clay @@ -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); } } diff --git a/min/lib/Minify/Controller/Groups.php b/min/lib/Minify/Controller/Groups.php index 8bdd251..9fec54c 100644 --- a/min/lib/Minify/Controller/Groups.php +++ b/min/lib/Minify/Controller/Groups.php @@ -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 */ -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); } } diff --git a/min/lib/Minify/Controller/Version1.php b/min/lib/Minify/Controller/Version1.php deleted file mode 100644 index c6409cc..0000000 --- a/min/lib/Minify/Controller/Version1.php +++ /dev/null @@ -1,119 +0,0 @@ - - * Minify::serve('Version1'); - * - * - * @package Minify - * @author Stephen Clay - */ -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); - } - } - } -} -