Skip to content
Snippets Groups Projects
Unverified Commit 31442318 authored by Thijs Kinkhorst's avatar Thijs Kinkhorst Committed by GitHub
Browse files

Merge pull request #564 from LukeCarrier/windows-paths

Improve handling of paths containing Windows-style drive letters
parents 68be96da 5f483bb5
No related branches found
No related tags found
No related merge requests found
<?php
use SimpleSAML\Utils\System;
/**
* Configuration of SimpleSAMLphp
......@@ -568,19 +569,7 @@ class SimpleSAML_Configuration implements \SimpleSAML\Utils\ClearableState
assert(is_string($path));
/* Prepend path with basedir if it doesn't start with a slash or a Windows drive letter (e.g. "C:\"). We assume
* getBaseDir ends with a slash.
*/
if ($path[0] !== '/' &&
!(preg_match('@^[a-z]:[\\\\/]@i', $path, $matches) && is_dir($matches[0]))
) {
$path = $this->getBaseDir().$path;
}
// remove trailing slashes
$path = rtrim($path, '/');
return $path;
return System::resolvePath($path, $this->getBaseDir());
}
......
......@@ -122,13 +122,20 @@ class System
$base = $config->getBaseDir();
}
// normalise directory separator
$base = str_replace('\\', '/', $base);
$path = str_replace('\\', '/', $path);
// remove trailing slashes
$base = rtrim($base, '/');
$path = rtrim($path, '/');
// check for absolute path
if (substr($path, 0, 1) === '/') {
// absolute path. */
$ret = '/';
} elseif (static::pathContainsDriveLetter($path)) {
$ret = '';
} else {
// path relative to base
$ret = $base;
......@@ -141,7 +148,7 @@ class System
} elseif ($d === '..') {
$ret = dirname($ret);
} else {
if (substr($ret, -1) !== '/') {
if ($ret && substr($ret, -1) !== '/') {
$ret .= '/';
}
$ret .= $d;
......@@ -215,4 +222,18 @@ class System
opcache_invalidate($filename);
}
}
/**
* Check if the supplied path contains a Windows-style drive letter.
*
* @param string $path
*
* @return bool
*/
private static function pathContainsDriveLetter($path)
{
$letterAsciiValue = ord(strtoupper(substr($path, 0, 1)));
return substr($path, 1, 1) === ':'
&& $letterAsciiValue >= 65 && $letterAsciiValue <= 90;
}
}
......@@ -220,6 +220,9 @@ class Test_SimpleSAML_Configuration extends SimpleSAML\Test\Utils\ClearStateTest
$this->assertEquals($c->resolvePath('slash/'), '/basedir/slash');
$this->assertEquals($c->resolvePath('slash//'), '/basedir/slash');
$this->assertEquals($c->resolvePath('C:\\otherdir'), 'C:/otherdir');
$this->assertEquals($c->resolvePath('C:/otherdir'), 'C:/otherdir');
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment