Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.45% |
21 / 22 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CdupCommand | |
95.45% |
21 / 22 |
|
50.00% |
1 / 2 |
7 | |
0.00% |
0 / 1 |
| run | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| normalizePath | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 | |||
| 1 | <?php |
| 2 | namespace Apie\FtpServer\Commands; |
| 3 | |
| 4 | use Apie\ApieFileSystem\ApieFilesystem; |
| 5 | use Apie\Core\Context\ApieContext; |
| 6 | use Apie\FtpServer\FtpConstants; |
| 7 | use React\Socket\ConnectionInterface; |
| 8 | |
| 9 | class CdupCommand implements CommandInterface |
| 10 | { |
| 11 | public function run(ApieContext $apieContext, string $arg = ''): ApieContext |
| 12 | { |
| 13 | $filesystem = $apieContext->getContext(ApieFilesystem::class); |
| 14 | $conn = $apieContext->getContext(ConnectionInterface::class); |
| 15 | $pwd = trim($apieContext->getContext(FtpConstants::CURRENT_PWD), '/') . '/..'; |
| 16 | if ($pwd === '/..') { |
| 17 | $conn->write("550 Already at /\r\n"); |
| 18 | return $apieContext; |
| 19 | } |
| 20 | assert($filesystem instanceof ApieFilesystem); |
| 21 | $child = $filesystem->visit($pwd); |
| 22 | assert($child !== null); |
| 23 | $conn->write("250 Directory successfully changed.\r\n"); |
| 24 | return $apieContext |
| 25 | ->withContext(FtpConstants::CURRENT_FOLDER, $child) |
| 26 | ->withContext(FtpConstants::CURRENT_PWD, $this->normalizePath($pwd)); |
| 27 | } |
| 28 | |
| 29 | private function normalizePath($path) |
| 30 | { |
| 31 | $parts = explode('/', $path); |
| 32 | $stack = []; |
| 33 | |
| 34 | foreach ($parts as $part) { |
| 35 | if ($part === '' || $part === '.') { |
| 36 | continue; |
| 37 | } |
| 38 | if ($part === '..') { |
| 39 | array_pop($stack); |
| 40 | } else { |
| 41 | $stack[] = $part; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return implode('/', $stack); |
| 46 | } |
| 47 | |
| 48 | } |