Skip to content

Commit

Permalink
Allow empty connection root for FTP connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Jan 29, 2022
1 parent e280a45 commit c222d21
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"require-dev": {
"ext-fileinfo": "*",
"ext-ftp": "*",
"phpunit/phpunit": "^8.5 || ^9.4",
"phpstan/phpstan": "^0.12.26",
"phpseclib/phpseclib": "^2.0",
Expand Down
31 changes: 24 additions & 7 deletions src/Ftp/FtpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use League\MimeTypeDetection\MimeTypeDetector;
use Throwable;

use function ftp_chdir;
use function ftp_pwd;

class FtpAdapter implements FilesystemAdapter
{
private const SYSTEM_TYPE_WINDOWS = 'windows';
Expand Down Expand Up @@ -77,6 +80,8 @@ class FtpAdapter implements FilesystemAdapter
*/
private $mimeTypeDetector;

private ?string $rootDirectory = null;

public function __construct(
FtpConnectionOptions $connectionOptions,
FtpConnectionProvider $connectionProvider = null,
Expand Down Expand Up @@ -111,14 +116,15 @@ private function connection()
start:
if ( ! $this->hasFtpConnection()) {
$this->connection = $this->connectionProvider->createConnection($this->connectionOptions);
$this->rootDirectory = $this->resolveConnectionRoot($this->connection);
}

if ($this->connectivityChecker->isConnected($this->connection) === false) {
$this->connection = false;
goto start;
}

ftp_chdir($this->connection, $this->connectionOptions->root());
ftp_chdir($this->connection, $this->rootDirectory);

return $this->connection;
}
Expand Down Expand Up @@ -434,15 +440,15 @@ private function normalizeUnixObject(string $item, string $base): StorageAttribu
$isDirectory = $this->listingItemIsDirectory($permissions);
$permissions = $this->normalizePermissions($permissions);
$path = $base === '' ? $name : rtrim($base, '/') . '/' . $name;
$lastModified = $this->connectionOptions->timestampsOnUnixListingsEnabled()
? $this->normalizeUnixTimestamp($month, $day, $timeOrYear)
: null;
$lastModified = $this->connectionOptions->timestampsOnUnixListingsEnabled() ? $this->normalizeUnixTimestamp(
$month,
$day,
$timeOrYear
) : null;

if ($isDirectory) {
return new DirectoryAttributes(
$path,
$this->visibilityConverter->inverseForDirectory($permissions),
$lastModified
$path, $this->visibilityConverter->inverseForDirectory($permissions), $lastModified
);
}

Expand Down Expand Up @@ -622,4 +628,15 @@ private function hasFtpConnection(): bool
{
return $this->connection instanceof \FTP\Connection || is_resource($this->connection);
}

private function resolveConnectionRoot($connection): string
{
$root = $this->connectionOptions->root();

if ($root !== '') {
ftp_chdir($connection, $root);
}

return ftp_pwd($connection);
}
}

0 comments on commit c222d21

Please sign in to comment.