From 4a0e6efad661c32c33e66384c221df230e06a040 Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 23 Nov 2023 17:52:20 +0100 Subject: [PATCH] verification des connexions --- src/Transfer/FTP.php | 11 +++++++++++ src/Transfer/Local.php | 12 ++++++++++++ src/Transfer/SFTP.php | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/Transfer/FTP.php b/src/Transfer/FTP.php index 8a38f2c..42c7788 100644 --- a/src/Transfer/FTP.php +++ b/src/Transfer/FTP.php @@ -6,7 +6,14 @@ use Cubist\Util\CommandLine\LFTP; use Cubist\Util\CommandLine\Rclone; class FTP extends Driver { + const SUCCESS = 0; + + const ERROR_PROTOCOL_NOT_READY = 4; + 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); @@ -21,4 +28,8 @@ class FTP extends Driver { { // TODO: Implement copyFile() method. } + + public function checkConnexion() { + return static::ERROR_PROTOCOL_NOT_READY; + } } \ No newline at end of file diff --git a/src/Transfer/Local.php b/src/Transfer/Local.php index a53b628..f0a7830 100644 --- a/src/Transfer/Local.php +++ b/src/Transfer/Local.php @@ -8,6 +8,8 @@ class Local extends Driver { protected $_rootPath = ''; + const ERROR_FOLDER_UNMOUNT = 3; + public function __construct(IServer $server, $rootPath = null) { parent::__construct($server); @@ -43,6 +45,9 @@ class Local extends Driver 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() .' )'); + } $rsync = new Rsync($source, $dest); $rsync->setServer($this->getServer()); $rsync->setBasePath($this->_basePath()); @@ -62,5 +67,12 @@ class Local extends Driver return copy($source, $this->_basePath() . '/' . $dest); } + public function checkConnexion($data = []) { + if(file_exists($this->getRootPath().'status')) { + return true; + } else { + return static::ERROR_FOLDER_UNMOUNT; + } + } } \ No newline at end of file diff --git a/src/Transfer/SFTP.php b/src/Transfer/SFTP.php index 8f25e4b..e2fcdf5 100644 --- a/src/Transfer/SFTP.php +++ b/src/Transfer/SFTP.php @@ -6,7 +6,16 @@ use Cubist\Util\CommandLine\Rclone; class SFTP extends Driver { + const ERROR_MISSING_HOST = 0; + + const ERROR_INVALID_PARAM = 1; + + const ERROR_CONNEXION_FAILED = 2; + 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); @@ -21,4 +30,34 @@ class SFTP extends Driver { { // TODO: Implement copyFile() method. } + + public function checkConnexion($data = []) { + $connection = null; + + $data = [ + 'username' => $data['username'] ?? $this->getServer()->getUsername(), + 'password' => $data['password'] ?? $this->getServer()->getPassword(), + 'host' => $data['host'] ?? $this->getServer()->getHost(), + 'port' => $data['port'] ?? $this->getServer()->getPort(), + ]; + + if(!$data['host']) { + return self::ERROR_MISSING_HOST; + } else { + try { + $connection = ssh2_connect($data['host'], $data['port']); + } catch (\Exception $e) { + return static::ERROR_INVALID_PARAM; + } + + try { + ssh2_auth_password($connection, $data['username'], $data['password']); + ssh2_disconnect($connection); + return true; + } catch (\Exception $e) { + return static::ERROR_CONNEXION_FAILED; + } + + } + } } \ No newline at end of file -- 2.39.5