Skip to content

Commit

Permalink
Update isAbsolute() to recognize stream wrapper paths.
Browse files Browse the repository at this point in the history
This has the benefit that the realpath() method is not applied to a
registered stream wrapper in the constructor of the Folder class.
Using the realpath() method will break the stream.

Thank "davalb" for the original patch.
  • Loading branch information
ADmad committed Jun 22, 2014
1 parent 497ecd3 commit 9a1a965
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/Cake/Test/Case/Utility/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,16 @@ public function testIsAbsolute() {
$this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
$this->assertFalse(Folder::isAbsolute('\\path/to/file'));
$this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
$this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
$this->assertFalse(Folder::isAbsolute('://example'));

$this->assertTrue(Folder::isAbsolute('/usr/local'));
$this->assertTrue(Folder::isAbsolute('//path/to/file'));
$this->assertTrue(Folder::isAbsolute('C:\\cake'));
$this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
$this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
$this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
$this->assertTrue(Folder::isAbsolute('http://www.example.com'));
}

/**
Expand Down
24 changes: 23 additions & 1 deletion lib/Cake/Utility/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,29 @@ public static function isWindowsPath($path) {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::isAbsolute
*/
public static function isAbsolute($path) {
return !empty($path) && ($path[0] === '/' || preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) === '\\\\');
if (empty($path)) {
return false;
}

return $path[0] === '/' ||
preg_match('/^[A-Z]:\\\\/i', $path) ||
substr($path, 0, 2) === '\\\\' ||
self::isRegisteredStreamWrapper($path);
}

/**
* Returns true if given $path is a registered stream wrapper.
*
* @param string $path Path to check
* @return boolean true If path is registered stream wrapper.
*/
public static function isRegisteredStreamWrapper($path) {
if (preg_match('/^[A-Z]+(?=:\/\/)/i', $path, $matches) &&
in_array($matches[0], stream_get_wrappers())
) {
return true;
}
return false;
}

/**
Expand Down

0 comments on commit 9a1a965

Please sign in to comment.