/** * Get the real path (taking ".." and such into account) * * @param string $path Path to resolve * @return string The resolved path */ public function realpath($path) { if (strpos($path, '..') === false) { if (!Folder::isAbsolute($path)) { $path = Folder::addPathElement($this->path, $path); } return $path; } $path = str_replace('/', DIRECTORY_SEPARATOR, trim($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); }
/** * test Adding path elements to a path * * @return void */ public function testAddPathElement() { $expected = DS . 'some' . DS . 'dir' . DS . 'another_path'; $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path'); $this->assertEquals($expected, $result); $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path'); $this->assertEquals($expected, $result); $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path']); $this->assertEquals($expected, $result); $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, ['another_path']); $this->assertEquals($expected, $result); $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another'; $result = Folder::addPathElement(DS . 'some' . DS . 'dir', ['another_path', 'and', 'another']); $this->assertEquals($expected, $result); }