/** * testIsAbsolute method * * @return void */ public function testIsAbsolute() { $this->assertFalse(Folder::isAbsolute('path/to/file')); $this->assertFalse(Folder::isAbsolute('cake/')); $this->assertFalse(Folder::isAbsolute('path\\to\\file')); $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')); }
/** * Get the real path (taking ".." and such into account) * * @param string $path Path to resolve * @return string The resolved path */ public function realpath($path) { $path = str_replace('/', DIRECTORY_SEPARATOR, trim($path)); if (strpos($path, '..') === false) { if (!Folder::isAbsolute($path)) { $path = Folder::addPathElement($this->_path, $path); } return $path; } $parts = explode(DIRECTORY_SEPARATOR, $path); $newparts = []; $newpath = ''; if ($path[0] === DIRECTORY_SEPARATOR) { $newpath = DIRECTORY_SEPARATOR; } while (($part = array_shift($parts)) !== null) { if ($part === '.' || $part === '') { continue; } if ($part === '..') { if (!empty($newparts)) { array_pop($newparts); continue; } return false; } $newparts[] = $part; } $newpath .= implode(DIRECTORY_SEPARATOR, $newparts); return Folder::slashTerm($newpath); }