]> _ Git - cubist_net.git/commitdiff
wip #6668
authorsoufiane <soufiane@cubedesigners.com>
Tue, 30 Jan 2024 13:28:29 +0000 (14:28 +0100)
committersoufiane <soufiane@cubedesigners.com>
Tue, 30 Jan 2024 13:28:29 +0000 (14:28 +0100)
src/Transfer/FTP.php
src/Transfer/SFTP.php

index ddb3e606f275d23ba8facb43587ee99dd78d3a0e..522564fb1c1b332825bff30c909ea51333cf9de4 100644 (file)
@@ -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
index 64163cbe4205c7c62719c2f5c41ce058023bd4fa..4a7ade6911a22624c0732547d74084f234c58012 100644 (file)
@@ -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;
             }