From af4ceabc0f55228f669fbc395463f89c25ed96dc Mon Sep 17 00:00:00 2001
From: rahultiwary1 <rahul.tiwary@pfizer.com>
Date: Wed, 25 Dec 2024 16:52:28 +0530
Subject: [PATCH 1/7] Drupal 11 fixes added.

---
 sftp_client.info.yml              |  2 +-
 tests/src/Unit/SftpClientTest.php | 16 +++++-----------
 2 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/sftp_client.info.yml b/sftp_client.info.yml
index 2f79739..265c764 100644
--- a/sftp_client.info.yml
+++ b/sftp_client.info.yml
@@ -3,4 +3,4 @@ description: 'The SFTP client for programmatic use.'
 package: Developer
 type: module
 php: 7.3
-core_version_requirement: ^8 || ^9
+core_version_requirement: ^10 || ^11
diff --git a/tests/src/Unit/SftpClientTest.php b/tests/src/Unit/SftpClientTest.php
index 47fae3b..92a84f9 100644
--- a/tests/src/Unit/SftpClientTest.php
+++ b/tests/src/Unit/SftpClientTest.php
@@ -61,17 +61,11 @@ class SftpClientTest extends UnitTestCase {
     $logger_channel = new LoggerChannel('sftp.client.test');
     $logger_channel->addLogger($this->logger);
 
-    $this->fileSystem = $this
-      ->getMockBuilder(FileSystemInterface::class)
-      ->getMock();
+    $this->fileSystem = $this->createMock(FileSystemInterface::class);
 
-    $this->keyRepository = $this
-      ->getMockBuilder(KeyRepositoryInterface::class)
-      ->getMock();
+    $this->keyRepository = $this->createMock(KeyRepositoryInterface::class);
 
-    $this->loggerChannelFactory = $this
-      ->getMockBuilder(LoggerChannelFactoryInterface::class)
-      ->getMock();
+    $this->loggerChannelFactory = $this->createMock(LoggerChannelFactoryInterface::class);
 
     $this->loggerChannelFactory
       ->method('get')
@@ -92,7 +86,7 @@ class SftpClientTest extends UnitTestCase {
   protected function getSftpClient(Settings $settings, $key = NULL): SftpClient {
     $this->keyRepository
       ->method('getKey')
-      ->willReturn($key ?? $this->getMockBuilder(KeyInterface::class)->getMock());
+      ->willReturn($key ?? $this->createMock(KeyInterface::class));
 
     $sftp_client = new SftpClient(
       $settings,
@@ -447,7 +441,7 @@ class SftpClientTest extends UnitTestCase {
   /**
    * {@inheritdoc}
    */
-  public function providerRetry(): array {
+  public static function providerRetry(): array {
     return [
       [
         5,
-- 
GitLab


From 87ac7a88707dd782f6c07e4fdd6da05dd383543d Mon Sep 17 00:00:00 2001
From: rahultiwary1 <rahul.tiwary@pfizer.com>
Date: Fri, 27 Dec 2024 13:30:17 +0530
Subject: [PATCH 2/7] Upgrade status issue fixed.

---
 composer.json        | 4 ++--
 sftp_client.info.yml | 3 +--
 src/SftpClient.php   | 7 ++++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/composer.json b/composer.json
index 1dd2ae3..528db0f 100644
--- a/composer.json
+++ b/composer.json
@@ -14,9 +14,9 @@
         "phpseclib/phpseclib": "^2.0.29"
     },
     "require-dev": {
-        "drupal/key": "^1.8"
+        "drupal/key": "^1.17"
     },
     "suggest": {
-        "drupal/key": "^1.8"
+        "drupal/key": "^1.17"
     }
 }
diff --git a/sftp_client.info.yml b/sftp_client.info.yml
index 265c764..f2efe77 100644
--- a/sftp_client.info.yml
+++ b/sftp_client.info.yml
@@ -2,5 +2,4 @@ name: 'SFTP Client'
 description: 'The SFTP client for programmatic use.'
 package: Developer
 type: module
-php: 7.3
-core_version_requirement: ^10 || ^11
+core_version_requirement: ^10.3 || ^11
diff --git a/src/SftpClient.php b/src/SftpClient.php
index 3bc0e5b..f702c33 100644
--- a/src/SftpClient.php
+++ b/src/SftpClient.php
@@ -3,6 +3,7 @@
 namespace Drupal\sftp_client;
 
 use Drupal\Core\File\FileSystemInterface;
+use Drupal\Core\File\FileExists;
 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
 use Drupal\Core\Logger\LoggerChannelInterface;
 use Drupal\Core\Site\Settings;
@@ -387,9 +388,9 @@ class SftpClient implements SftpClientInterface {
   /**
    * {@inheritdoc}
    */
-  public function moveFile(string $source, string $destination, int $replace = FileSystemInterface::EXISTS_ERROR): bool {
+  public function moveFile(string $source, string $destination, int $replace = FileExists::Error): bool {
     switch ($replace) {
-      case FileSystemInterface::EXISTS_RENAME:
+      case FileExists::Rename:
         $new_destination = $destination;
         $counter = 0;
 
@@ -400,7 +401,7 @@ class SftpClient implements SftpClientInterface {
         $destination = $new_destination;
         break;
 
-      case FileSystemInterface::EXISTS_REPLACE:
+      case FileExists::Replace:
         if ($this->isFile($destination)) {
           $this->removeFile($destination);
         }
-- 
GitLab


From 487b70bcb20133bf26f01af9dcd62c976bffe4fe Mon Sep 17 00:00:00 2001
From: rahultiwary1 <rahul.tiwary@pfizer.com>
Date: Sun, 29 Dec 2024 13:13:18 +0530
Subject: [PATCH 3/7] Updated key module version.

---
 composer.json | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/composer.json b/composer.json
index 528db0f..6f3e5e0 100644
--- a/composer.json
+++ b/composer.json
@@ -14,9 +14,9 @@
         "phpseclib/phpseclib": "^2.0.29"
     },
     "require-dev": {
-        "drupal/key": "^1.17"
+        "drupal/key": "^1.19"
     },
     "suggest": {
-        "drupal/key": "^1.17"
+        "drupal/key": "^1.19"
     }
 }
-- 
GitLab


From 5c53d302fd9383786ab37bb27da22523178dc975 Mon Sep 17 00:00:00 2001
From: Matthieu SCARSET <m@matthieuscarset.com>
Date: Fri, 24 Jan 2025 11:28:05 +0100
Subject: [PATCH 4/7] feat(D11): #3495972 Upgrade to Drupal 11 - PHP 8.1
 minimum - phpseclib v3 LTS

---
 composer.json                     | 2 +-
 sftp_client.info.yml              | 1 +
 src/SftpClient.php                | 8 ++++----
 tests/src/Unit/SftpClientTest.php | 2 +-
 4 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/composer.json b/composer.json
index 6f3e5e0..49b559c 100644
--- a/composer.json
+++ b/composer.json
@@ -11,7 +11,7 @@
         }
     ],
     "require": {
-        "phpseclib/phpseclib": "^2.0.29"
+        "phpseclib/phpseclib": "^3.0"
     },
     "require-dev": {
         "drupal/key": "^1.19"
diff --git a/sftp_client.info.yml b/sftp_client.info.yml
index f2efe77..dc7cab6 100644
--- a/sftp_client.info.yml
+++ b/sftp_client.info.yml
@@ -2,4 +2,5 @@ name: 'SFTP Client'
 description: 'The SFTP client for programmatic use.'
 package: Developer
 type: module
+php: 8.1
 core_version_requirement: ^10.3 || ^11
diff --git a/src/SftpClient.php b/src/SftpClient.php
index f702c33..64fceff 100644
--- a/src/SftpClient.php
+++ b/src/SftpClient.php
@@ -10,8 +10,8 @@ use Drupal\Core\Site\Settings;
 use Drupal\key\KeyRepositoryInterface;
 use Drupal\sftp_client\Exception\SftpException;
 use Drupal\sftp_client\Exception\SftpLoginException;
-use phpseclib\Crypt\RSA;
-use phpseclib\Net\SFTP;
+use phpseclib3\Crypt\RSA;
+use phpseclib3\Net\SFTP;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LoggerTrait;
 use Psr\Log\LogLevel;
@@ -92,7 +92,7 @@ class SftpClient implements SftpClientInterface {
     Settings $settings,
     FileSystemInterface $file_system,
     LoggerChannelFactoryInterface $logger_factory,
-    ?KeyRepositoryInterface $key_repository
+    ?KeyRepositoryInterface $key_repository,
   ) {
     $this->fileSystem = $file_system;
     $this->sftpSettings = $settings::get(static::SETTING, []);
@@ -516,7 +516,7 @@ class SftpClient implements SftpClientInterface {
    *
    * @link https://www.php.net/manual/en/class.filesystemiterator.php#filesystemiterator.constants.skip-dots
    */
-  protected function listItems(string $path_remote, string $type_constant = NULL, bool $skip_dots = TRUE): \Generator {
+  protected function listItems(string $path_remote, ?string $type_constant = NULL, bool $skip_dots = TRUE): \Generator {
     $list = $this->connect()->rawlist($path_remote);
 
     if (\is_array($list)) {
diff --git a/tests/src/Unit/SftpClientTest.php b/tests/src/Unit/SftpClientTest.php
index 92a84f9..a322961 100644
--- a/tests/src/Unit/SftpClientTest.php
+++ b/tests/src/Unit/SftpClientTest.php
@@ -11,7 +11,7 @@ use Drupal\key\KeyRepositoryInterface;
 use Drupal\sftp_client\Exception\SftpLoginException;
 use Drupal\sftp_client\SftpClient;
 use Drupal\Tests\UnitTestCase;
-use phpseclib\Crypt\RSA;
+use phpseclib3\Crypt\RSA;
 use PHPUnit\Framework\Error\Notice;
 
 /**
-- 
GitLab


From da724ac237fb6da3438e24f9f68129789d0ef12b Mon Sep 17 00:00:00 2001
From: Matthieu SCARSET <m@matthieuscarset.com>
Date: Fri, 24 Jan 2025 11:28:32 +0100
Subject: [PATCH 5/7] fix(D11): Convert 'mode' file attribute to expected
 'permissions' key

---
 src/SftpResource.php | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/SftpResource.php b/src/SftpResource.php
index 6ccfa37..9509861 100644
--- a/src/SftpResource.php
+++ b/src/SftpResource.php
@@ -61,6 +61,11 @@ class SftpResource {
    *   The data to create a resource.
    */
   public function __construct(array $data) {
+    // Convert mode to permission.
+    if (!isset($data['permissions']) && isset($data['mode'])) {
+      $data['permissions'] = \decoct($data['mode']);
+    }
+
     foreach ($this as $key => $value) {
       if (!isset($data[$key])) {
         throw new \InvalidArgumentException(\sprintf('The "%s" must exist in a resource definition!', $key));
-- 
GitLab


From 0edb9795f94fa55c77fd830d934dd2fdf33dfee1 Mon Sep 17 00:00:00 2001
From: Matthieu SCARSET <m@matthieuscarset.com>
Date: Tue, 28 Jan 2025 09:03:22 +0100
Subject: [PATCH 6/7] fix: Wrong type for replace flag in moveFile method

---
 src/SftpClient.php          |  2 +-
 src/SftpClientInterface.php | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/SftpClient.php b/src/SftpClient.php
index 64fceff..7ddeea4 100644
--- a/src/SftpClient.php
+++ b/src/SftpClient.php
@@ -388,7 +388,7 @@ class SftpClient implements SftpClientInterface {
   /**
    * {@inheritdoc}
    */
-  public function moveFile(string $source, string $destination, int $replace = FileExists::Error): bool {
+  public function moveFile(string $source, string $destination, /* FileExists */ object $replace = FileExists::Error): bool {
     switch ($replace) {
       case FileExists::Rename:
         $new_destination = $destination;
diff --git a/src/SftpClientInterface.php b/src/SftpClientInterface.php
index 752163f..da24d35 100644
--- a/src/SftpClientInterface.php
+++ b/src/SftpClientInterface.php
@@ -167,11 +167,11 @@ interface SftpClientInterface {
    *   The path to a remote file.
    * @param string $destination
    *   The path to a new location of a remote file.
-   * @param int $replace
-   *   Replace behavior when the destination file already exists:
-   *   - {@see FileSystemInterface::EXISTS_REPLACE};
-   *   - {@see FileSystemInterface::EXISTS_RENAME};
-   *   - {@see FileSystemInterface::EXISTS_ERROR}.
+   * @param object $replace
+   *   Behavior when the destination file already exists:
+   *   - {@see FileExists::Replace};
+   *   - {@see FileExists::Rename};
+   *   - {@see FileExists::Error}.
    *
    * @return bool
    *   The state of whether a file was successfully moved.
@@ -179,9 +179,9 @@ interface SftpClientInterface {
    * @throws \Drupal\sftp_client\Exception\SftpLoginException
    *   When the connection to SFTP server cannot be established.
    *
-   * @see \Drupal\Core\File\FileSystemInterface
+   * @see \Drupal\Core\File\FileExists
    */
-  public function moveFile(string $source, string $destination, int $replace): bool;
+  public function moveFile(string $source, string $destination, /* FileExists */ object $replace): bool;
 
   /**
    * Removes a remote file.
-- 
GitLab


From b4f84303ecf9786724c808dd2effff86ec97a8ee Mon Sep 17 00:00:00 2001
From: Matthieu Scarset <m@matthieuscarset.com>
Date: Fri, 21 Feb 2025 16:56:54 +0100
Subject: [PATCH 7/7] fix: CAPP-787 MDS Fix phpseclib3 syntax to load RSA key

---
 src/SftpClient.php | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/SftpClient.php b/src/SftpClient.php
index 7ddeea4..811c04e 100644
--- a/src/SftpClient.php
+++ b/src/SftpClient.php
@@ -10,7 +10,7 @@ use Drupal\Core\Site\Settings;
 use Drupal\key\KeyRepositoryInterface;
 use Drupal\sftp_client\Exception\SftpException;
 use Drupal\sftp_client\Exception\SftpLoginException;
-use phpseclib3\Crypt\RSA;
+use phpseclib3\Crypt\PublicKeyLoader;
 use phpseclib3\Net\SFTP;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LoggerTrait;
@@ -226,9 +226,11 @@ class SftpClient implements SftpClientInterface {
     }
 
     if ($this->settings['key_id'] !== FALSE) {
-      $rsa = new RSA();
-      $rsa->loadKey($this->keyRepository->getKey($this->settings['key_id'])->getKeyValue());
-      $rsa->setPassword($this->settings['password']);
+      $key_password = $this->settings['password'];
+      $key_value = $this->keyRepository->getKey($this->settings['key_id'])->getKeyValue();
+
+      /** @var \phpseclib3\Crypt\RSA $rsa */
+      $rsa = PublicKeyLoader::load($key_value, $key_password);
 
       // Convert the password to RSA key.
       /* @see \phpseclib\Net\SFTP::_login_helper() */
-- 
GitLab