From 8fffe166b65b2cbbfb9fd0e41906a586f6b81d00 Mon Sep 17 00:00:00 2001 From: soufiane Date: Tue, 30 Jan 2024 14:28:29 +0100 Subject: [PATCH] wip #6668 --- src/Transfer/FTP.php | 37 +++++++++++++++++++++++++------------ src/Transfer/SFTP.php | 36 +++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/Transfer/FTP.php b/src/Transfer/FTP.php index ddb3e60..522564f 100644 --- a/src/Transfer/FTP.php +++ b/src/Transfer/FTP.php @@ -4,6 +4,7 @@ namespace Cubist\Net\Transfer; use Cubist\Util\CommandLine\LFTP; use Cubist\Util\CommandLine\Rclone; +use Cubist\Util\Files\Files; class FTP extends Driver { @@ -15,25 +16,31 @@ class FTP extends Driver { const ERROR_PROTOCOL_NOT_READY = 4; + const ERROR_SEND_FILE = 5; + protected $port = 21; + public function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) { + $lftp = new LFTP(); + $lftp->setServer($this->getServer()); + $lftp->setSrc($source); + $lftp->setDest($dest); + $lftp->setMirror($mirror); + $lftp->setDryRun($dryrun); + $lftp->execute(); + return $lftp; + } + public function copy($source, $dest, $mirror = false, $dryrun = false) { if($this->checkConnexion() !== true) { throw new \Exception('Unabled to connect to this server (error code : '. $this->checkConnexion() .' )'); } - $lftp = new LFTP(); - $lftp->setServer($this->getServer()); - $lftp->setSrc($source); - $lftp->setDest($dest); - $lftp->setMirror($mirror); - $lftp->setDryRun($dryrun); - $lftp->execute(); - return $lftp; + $this->synchronizeFiles($source, $dest); } public function copyFile($source, $dest) { - // TODO: Implement copyFile() method. + $this->synchronizeFiles($source, $dest); } public function checkConnexion($data = []) { @@ -57,8 +64,15 @@ class FTP extends Driver { if($ftp) { if (@ftp_login($ftp, $data['username'], $data['password'])) { - ftp_close($ftp); - return true; + try { + $tmp = Files::tempnam(); //on créé un fichier temporaire + $this->copyFile($tmp,self::cleanInstallDir('/'). '/'); + unlink($tmp); //on supprime le fichier + ftp_close($ftp); + return true; + } catch(\Exception $e) { + return static::ERROR_SEND_FILE; + } } else { return static::ERROR_CONNEXION_FAILED; } @@ -66,6 +80,5 @@ class FTP extends Driver { return static::ERROR_CONNEXION_FAILED; } } - } } \ No newline at end of file diff --git a/src/Transfer/SFTP.php b/src/Transfer/SFTP.php index 64163cb..4a7ade6 100644 --- a/src/Transfer/SFTP.php +++ b/src/Transfer/SFTP.php @@ -3,6 +3,7 @@ namespace Cubist\Net\Transfer; use Cubist\Util\CommandLine\Rclone; +use Cubist\Util\Files\Files; class SFTP extends Driver { @@ -12,25 +13,31 @@ class SFTP extends Driver { const ERROR_CONNEXION_FAILED = 2; + const ERROR_SEND_FILE = 5; + protected $port = 22; + public function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) { + $rclone = new Rclone(); + $rclone->setServer($this->getServer()); + $rclone->setSrc($source); + $rclone->setDest(rtrim($this->getServer()->getBasePath(), '/') . '/' . trim($dest, '/') . '/'); + $rclone->setMirror($mirror); + $rclone->setDryRun($dryrun); + $rclone->execute(); + return $rclone; + } + public function copy($source, $dest, $mirror = false, $dryrun = false) { if($this->checkConnexion() !== true) { throw new \Exception('Unabled to connect to this server (error code : '. $this->checkConnexion() .' )'); } - $rclone = new Rclone(); - $rclone->setServer($this->getServer()); - $rclone->setSrc($source); - $rclone->setDest(rtrim($this->getServer()->getBasePath(), '/') . '/' . trim($dest, '/') . '/'); - $rclone->setMirror($mirror); - $rclone->setDryRun($dryrun); - $rclone->execute(); - return $rclone; + $this->synchronizeFiles($source, $dest, $mirror, $dryrun); } public function copyFile($source, $dest) { - // TODO: Implement copyFile() method. + $this->synchronizeFiles($source, $dest); } public function checkConnexion($data = []) { @@ -55,8 +62,15 @@ class SFTP extends Driver { try { ssh2_auth_password($connection, $data['username'], $data['password']); - ssh2_disconnect($connection); - return true; + try { + $tmp = Files::tempnam(); //on créé un fichier temporaire + $this->copyFile($tmp,self::cleanInstallDir('/'). '/'); + unlink($tmp); //on supprime le fichier + ssh2_disconnect($connection); + return true; + } catch(\Exception $e) { + return static::ERROR_SEND_FILE; + } } catch (\Exception $e) { return static::ERROR_CONNEXION_FAILED; } -- 2.39.5