]> _ Git - cubist_net.git/commitdiff
verification des connexions
authorsoufiane <soufiane@cubedesigners.com>
Thu, 23 Nov 2023 16:52:20 +0000 (17:52 +0100)
committersoufiane <soufiane@cubedesigners.com>
Thu, 23 Nov 2023 16:52:20 +0000 (17:52 +0100)
src/Transfer/FTP.php
src/Transfer/Local.php
src/Transfer/SFTP.php

index 8a38f2cfa2769ce2b046ca45c2bf9fbfd002eb6a..42c778897ec6933e36c606450c5fad1a2076e7d1 100644 (file)
@@ -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
index a53b628570e45503d5dc567b2254f61cc4f66f1b..f0a78308cbe15bc0831573f826ba3b89c973ce3a 100644 (file)
@@ -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
index 8f25e4b4f946ceae366cb80826d40603f26f7373..e2fcdf526e14e38a4a94f9ca3119a34ebd7898ff 100644 (file)
@@ -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