1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-15 02:24:13 +02:00

Makes sure all file/dir paths are normalized to prepare for comparing.

Particularly the system docRoot and currentDir values passed to the URI
rewriter and the allowDirs check.
This commit is contained in:
Steve Clay
2016-05-12 13:29:54 -04:00
parent 7aac6792bc
commit de39966e9c
6 changed files with 50 additions and 17 deletions

View File

@@ -56,6 +56,11 @@ class Minify {
*/
protected $controller = null;
/**
* @var Minify_Env
*/
protected $env;
/**
* @var Minify_SourceInterface[]
*/
@@ -218,6 +223,8 @@ class Minify {
*/
public function serve(Minify_ControllerInterface $controller, $options = array())
{
$this->env = $controller->getEnv();
$options = array_merge($this->getDefaultOptions(), $options);
$config = $controller->createConfiguration($options);
@@ -499,7 +506,7 @@ class Minify {
protected function setupUriRewrites()
{
foreach($this->sources as $key => $source) {
$file = $source->getFilePath();
$file = $this->env->normalizePath($source->getFilePath());
$minifyOptions = $source->getMinifierOptions();
if ($file

View File

@@ -104,7 +104,7 @@ class App extends Container {
if (empty($config->documentRoot)) {
return $app->env->getDocRoot();
}
return rtrim($config->documentRoot, '/\\');
return $app->env->normalizePath($config->documentRoot);
};
$this->env = function (App $app) {

View File

@@ -69,4 +69,12 @@ abstract class Minify_Controller_Base implements Minify_ControllerInterface {
trigger_error(__METHOD__ . ' is deprecated in Minify 3.0.', E_USER_DEPRECATED);
$this->logger->info($msg);
}
/**
* {@inheritdoc}
*/
public function getEnv()
{
return $this->env;
}
}

View File

@@ -12,4 +12,11 @@ interface Minify_ControllerInterface {
* @return Minify_ServeConfiguration
*/
public function createConfiguration(array $options);
/**
* Get the Env component
*
* @return Minify_Env
*/
public function getEnv();
}

View File

@@ -33,6 +33,7 @@ class Minify_Env {
} else {
$this->server['DOCUMENT_ROOT'] = rtrim($this->server['DOCUMENT_ROOT'], '/\\');
}
$this->server['DOCUMENT_ROOT'] = $this->normalizePath($this->server['DOCUMENT_ROOT']);
$this->get = $options['get'];
$this->post = $options['post'];
$this->cookie = $options['cookie'];
@@ -76,6 +77,29 @@ class Minify_Env {
return isset($this->post[$key]) ? $this->post[$key] : $default;
}
/**
* turn windows-style slashes into unix-style,
* remove trailing slash
* and lowercase drive letter
*
* @param string $path absolute path
*
* @return string
*/
public function normalizePath($path)
{
$realpath = realpath($path);
if ($realpath) {
$path = $realpath;
}
$path = str_replace('\\', '/', $path);
$path = rtrim($path, '/');
if (substr($path, 1, 1) === ':') {
$path = lcfirst($path);
}
return $path;
}
protected $server = null;
protected $get = null;
protected $post = null;

View File

@@ -110,20 +110,6 @@ class Minify_Source_Factory {
return $realpath;
}
/**
* turn windows-style slashes into unix-style,
* remove trailing slash
* and lowercase drive letter
*
* @param string $path absolute path
*
* @return string
*/
public function getNormalizedPath($path)
{
return lcfirst(rtrim(str_replace('\\', '/', $path), '/'));
}
/**
* @param mixed $spec
*
@@ -159,8 +145,9 @@ class Minify_Source_Factory {
if ($this->options['checkAllowDirs']) {
$allowDirs = (array)$this->options['allowDirs'];
$inAllowedDir = false;
$filePath = $this->env->normalizePath($spec['filepath']);
foreach ($allowDirs as $allowDir) {
if (strpos($this->getNormalizedPath($spec['filepath']), $this->getNormalizedPath($allowDir)) === 0) {
if (strpos($filePath, $this->env->normalizePath($allowDir)) === 0) {
$inAllowedDir = true;
}
}