From f21ef3a7c86041eae05a0cd4c76e2254d2e1442b Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 30 Apr 2019 12:20:36 +0200 Subject: [PATCH 01/12] Fixed a bug in the FTP Adapter in the way we check if a path is a directory or not --- src/Gaufrette/Adapter/Ftp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 05a228aeb..5f1739c12 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -366,7 +366,7 @@ private function isDir($directory) return true; } - if (!@ftp_chdir($this->getConnection(), $directory)) { + if (!@is_dir('ftp://' . $this->username . ':' . $this->password . '@' . $this->host . ':' . $this->port . $directory)) { return false; } From 0fc519f62af13d1f79f7fb3a560e05ad9faf5db6 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 30 Apr 2019 12:23:20 +0200 Subject: [PATCH 02/12] Improved the way we build the URL to optionnaly add the port --- src/Gaufrette/Adapter/Ftp.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 5f1739c12..0153a0bf2 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -366,7 +366,14 @@ private function isDir($directory) return true; } - if (!@is_dir('ftp://' . $this->username . ':' . $this->password . '@' . $this->host . ':' . $this->port . $directory)) { + // Build the FTP URL that will be used to check if the path is a directory or not + $url = 'ftp://' . $this->username . ':' . $this->password . '@' . $this->host; + + if($this->port) { + $url .= ':' . $this->port; + } + + if (!@is_dir($url . $directory)) { return false; } From 57df9f4ea9e63e046030e63756e35dbd48d68331 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 30 Apr 2019 12:42:37 +0200 Subject: [PATCH 03/12] Improved the way we build the URL to take into consideration SSL situations --- src/Gaufrette/Adapter/Ftp.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 0153a0bf2..00e22c688 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -367,11 +367,9 @@ private function isDir($directory) } // Build the FTP URL that will be used to check if the path is a directory or not - $url = 'ftp://' . $this->username . ':' . $this->password . '@' . $this->host; - - if($this->port) { - $url .= ':' . $this->port; - } + $url = $this->ssl ? 'sftp://' : 'ftp://'; + $url .= $this->username . ':' . $this->password . '@' . $this->host; + $url .= $this->port ? ':' . $this->port : ''; if (!@is_dir($url . $directory)) { return false; From d11a658b6271177dd96ef442b47f64e29d86ee76 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Fri, 14 Jun 2019 16:45:46 +0200 Subject: [PATCH 04/12] Keep chdir as the main method, and fallback on is_dir if there's an exception. Also move the URL creation to a private method. --- src/Gaufrette/Adapter/Ftp.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 00e22c688..3e94af950 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -355,6 +355,17 @@ protected function createDirectory($directory) } } + /** + * @return string + */ + private function createConnectionUrl() { + $url = $this->ssl ? 'sftp://' : 'ftp://'; + $url .= $this->username . ':' . $this->password . '@' . $this->host; + $url .= $this->port ? ':' . $this->port : ''; + + return $url; + } + /** * @param string $directory - full directory path * @@ -366,12 +377,22 @@ private function isDir($directory) return true; } - // Build the FTP URL that will be used to check if the path is a directory or not - $url = $this->ssl ? 'sftp://' : 'ftp://'; - $url .= $this->username . ':' . $this->password . '@' . $this->host; - $url .= $this->port ? ':' . $this->port : ''; + $chDirResult = false; + try { + $chDirResult = ftp_chdir($this->getConnection(), $directory) + } + catch(Exception $e) { + $this->passive = true; + + // Build the FTP URL that will be used to check if the path is a directory or not + $url = $this->createConnectionUrl(); + + if (!@is_dir($url . $directory)) { + return false; + } + } - if (!@is_dir($url . $directory)) { + if (!$chDirResult) { return false; } From 8d7a5b03ebcfe0df6b6fdecd4f65fe4222109bf3 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Fri, 14 Jun 2019 16:49:28 +0200 Subject: [PATCH 05/12] Missing semi-colon --- src/Gaufrette/Adapter/Ftp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 3e94af950..25f0cd43f 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -379,7 +379,7 @@ private function isDir($directory) $chDirResult = false; try { - $chDirResult = ftp_chdir($this->getConnection(), $directory) + $chDirResult = ftp_chdir($this->getConnection(), $directory); } catch(Exception $e) { $this->passive = true; From 32f770aaf6ea36dd7f1c4bf40b530c3468eebe65 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 18 Jun 2019 21:43:07 +0200 Subject: [PATCH 06/12] Applied nicolasmure comments --- src/Gaufrette/Adapter/Ftp.php | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 25f0cd43f..0629dbee6 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -358,7 +358,8 @@ protected function createDirectory($directory) /** * @return string */ - private function createConnectionUrl() { + private function createConnectionUrl() + { $url = $this->ssl ? 'sftp://' : 'ftp://'; $url .= $this->username . ':' . $this->password . '@' . $this->host; $url .= $this->port ? ':' . $this->port : ''; @@ -379,26 +380,35 @@ private function isDir($directory) $chDirResult = false; try { - $chDirResult = ftp_chdir($this->getConnection(), $directory); - } - catch(Exception $e) { - $this->passive = true; + $chDirResult = ftp_chdir($this->getConnection(), $this->directory); + + // change directory again to return in the base directory + ftp_chdir($this->getConnection(), $this->directory); + } catch(\Exception $e) { + + // Cache the actual value of the passive mode + $actualPassiveMode = $this->passive; + $this->passive = true; // Build the FTP URL that will be used to check if the path is a directory or not $url = $this->createConnectionUrl(); if (!@is_dir($url . $directory)) { + + // Revert back the value of the passive mode. + $this->passive = $actualPassiveMode; + return false; } + + // Revert back the value of the passive mode. + $this->passive = $actualPassiveMode; } if (!$chDirResult) { return false; } - // change directory again to return in the base directory - ftp_chdir($this->getConnection(), $this->directory); - return true; } From cb83931039ace979b2476459fc2ca265bdf057c6 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Thu, 20 Jun 2019 09:53:50 +0200 Subject: [PATCH 07/12] Throw an exception if we try to use is_dir when passive mode is not active --- src/Gaufrette/Adapter/Ftp.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 0629dbee6..d690cb8bf 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -386,23 +386,18 @@ private function isDir($directory) ftp_chdir($this->getConnection(), $this->directory); } catch(\Exception $e) { - // Cache the actual value of the passive mode - $actualPassiveMode = $this->passive; - $this->passive = true; - // Build the FTP URL that will be used to check if the path is a directory or not $url = $this->createConnectionUrl(); - if (!@is_dir($url . $directory)) { - - // Revert back the value of the passive mode. - $this->passive = $actualPassiveMode; + // is_dir is only available in passive mode. + // See https://php.net/manual/en/wrappers.ftp.php for more details. + if(!$this->passive) { + throw new \BadFunctionCallException('is_dir can only be used in passive mode.'); + } + if (!@is_dir($url . $directory)) { return false; } - - // Revert back the value of the passive mode. - $this->passive = $actualPassiveMode; } if (!$chDirResult) { From de3eb225d746d7a7edab140f8243e028f5bb676b Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Fri, 21 Jun 2019 08:54:46 +0200 Subject: [PATCH 08/12] Some improvements for readability and simplification of the code --- src/Gaufrette/Adapter/Ftp.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index d690cb8bf..16264da10 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -378,30 +378,27 @@ private function isDir($directory) return true; } - $chDirResult = false; try { + $chDirResult = false; $chDirResult = ftp_chdir($this->getConnection(), $this->directory); // change directory again to return in the base directory ftp_chdir($this->getConnection(), $this->directory); - } catch(\Exception $e) { - - // Build the FTP URL that will be used to check if the path is a directory or not - $url = $this->createConnectionUrl(); - + return $chDirResult; + } catch (\Exception $e) { // is_dir is only available in passive mode. // See https://php.net/manual/en/wrappers.ftp.php for more details. - if(!$this->passive) { - throw new \BadFunctionCallException('is_dir can only be used in passive mode.'); - } - - if (!@is_dir($url . $directory)) { - return false; + if (!$this->passive) { + throw new \RuntimeException( + \sprintf('Not able to determine whether "%s" is a directory or not. Please try again using a passive FTP connection if your backend supports it, by setting the "passive" option of this adapter to true.', $directory), + $e->getCode(), + $e + ); } - } - if (!$chDirResult) { - return false; + // Build the FTP URL that will be used to check if the path is a directory or not + $url = $this->createConnectionUrl(); + return @is_dir($url . $directory); } return true; From 6f31fc205c75282fc2152aedfe098e0c86460337 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Fri, 26 Jul 2019 09:22:46 +0200 Subject: [PATCH 09/12] Removed unused return and unused variable declaration. --- src/Gaufrette/Adapter/Ftp.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 16264da10..910a03279 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -379,7 +379,6 @@ private function isDir($directory) } try { - $chDirResult = false; $chDirResult = ftp_chdir($this->getConnection(), $this->directory); // change directory again to return in the base directory @@ -400,8 +399,6 @@ private function isDir($directory) $url = $this->createConnectionUrl(); return @is_dir($url . $directory); } - - return true; } private function fetchKeys($directory = '', $onlyKeys = true) From 6cc9d881d17e2763b8eed736334350a44ace8245 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 8 Mar 2022 17:48:37 +0100 Subject: [PATCH 10/12] Removed .idea folder from tracking --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d5e9e375a..426ed9245 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ vendor/ .env composer.lock phpunit.xml +.idea From 3712db9917659fc53594d05fc0d099fbcce30126 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 8 Mar 2022 17:48:50 +0100 Subject: [PATCH 11/12] Fixed a typo in the FTP protocol name --- src/Gaufrette/Adapter/Ftp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gaufrette/Adapter/Ftp.php b/src/Gaufrette/Adapter/Ftp.php index 910a03279..904dad171 100644 --- a/src/Gaufrette/Adapter/Ftp.php +++ b/src/Gaufrette/Adapter/Ftp.php @@ -360,7 +360,7 @@ protected function createDirectory($directory) */ private function createConnectionUrl() { - $url = $this->ssl ? 'sftp://' : 'ftp://'; + $url = $this->ssl ? 'ftps://' : 'ftp://'; $url .= $this->username . ':' . $this->password . '@' . $this->host; $url .= $this->port ? ':' . $this->port : ''; From 81283ac4218c771b3a4d3992163903c14c9cbff4 Mon Sep 17 00:00:00 2001 From: Romain Biard Date: Tue, 8 Mar 2022 17:50:21 +0100 Subject: [PATCH 12/12] Resolved conflicts in the .gitignore --- .gitignore | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 426ed9245..77ec81947 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,12 @@ bin/* !bin/tests !bin/tests-all +composer.lock doc/.couscous +.env +.php_cs.cache +phpunit.xml tests/adapters/* !tests/adapters/*.dist vendor/ -.env -composer.lock -phpunit.xml .idea