From 97fed5ae57cae3da14908038943b00c8d4a1db0d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 12 Oct 2008 16:16:25 +0000 Subject: [PATCH] BUGFIX Director::fileExists() fails on windows with absolute paths (#2935) - thanks to ajshort! git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64098 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/Director.php | 19 +++++++++++++++---- tests/control/DirectorTest.php | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/core/control/Director.php b/core/control/Director.php index 49e9acbc4..1cb88b07a 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -460,16 +460,27 @@ class Director { static function getAbsURL($url) { return Director::baseURL() . $url; } - + + /** + * Returns true if a given path is absolute. Works under both *nix and windows + * systems + * + * @param string $path + * @return bool + */ + public static function is_absolute($path) { + if($path[0] == '/' || $path[0] == '\\') return true; + return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1; + } + /** * Given a filesystem reference relative to the site root, return the full file-system path. * * @param string $file * @return string */ - static function getAbsFile($file) { - if($file[0] == '/') return $file; - return Director::baseFolder() . '/' . $file; + public static function getAbsFile($file) { + return self::is_absolute($file) ? $file : Director::baseFolder() . '/' . $file; } /** diff --git a/tests/control/DirectorTest.php b/tests/control/DirectorTest.php index daaea2fa0..89ca7b32a 100644 --- a/tests/control/DirectorTest.php +++ b/tests/control/DirectorTest.php @@ -46,5 +46,27 @@ class DirectorTest extends SapphireTest { $this->assertEquals(Director::protocolAndHost().BASE_URL.'/', Director::absoluteBaseURL(BASE_URL)); $this->assertEquals(Director::protocolAndHost().BASE_URL . '/subfolder/test', Director::absoluteURL('subfolder/test')); } + + /** + * Tests that {@link Director::is_absolute()} works under different environment types + */ + public function testIsAbsolute() { + $expected = array ( + 'C:/something' => true, + 'd:\\' => true, + 'e/' => false, + 's:/directory' => true, + '/var/www' => true, + '\\Something' => true, + 'something/c:' => false, + 'folder' => false, + 'a/c:/' => false + ); + + foreach($expected as $path => $result) { + $this->assertEquals(Director::is_absolute($path), $result, "Test result for $path"); + } + } + } ?> \ No newline at end of file