login() public méthode

Login
public login ( string $username ) : boolean
$username string
Résultat boolean
 /**
  *
  */
 public function connect()
 {
     $this->ssh = new SSH2($this->configuration['hostname'], $this->configuration['port']);
     $authenticationMethod = $this->configuration[SftpDriver::CONFIG_AUTHENTICATION_METHOD];
     if (static::AUTHENTICATION_PASSWORD === (int) $authenticationMethod) {
         $authentication = $this->configuration['password'];
     } elseif (static::AUTHENTICATION_PUBKEY === (int) $authenticationMethod) {
         $authentication = new RSA();
         if (!empty($this->configuration['privateKeyPassword'])) {
             $authentication->setPassword($this->configuration['privateKeyPassword']);
         }
         $authentication->loadKey(file_get_contents($this->configuration['privateKey']));
     } else {
         throw new \LogicException('Wrong authentication type for phpseclibAdapter', 1476626149);
     }
     $sshConnected = $this->ssh->login($this->configuration['username'], $authentication);
     if ($sshConnected) {
         $this->sftp = new SFTP($this->configuration['hostname'], $this->configuration['port']);
         $sftpConnected = $this->sftp->login($this->configuration['username'], $authentication);
         if ($sftpConnected) {
             $this->info['userId'] = (int) $this->ssh->exec('echo $EUID');
             $this->info['groupIds'] = GeneralUtility::intExplode(' ', $this->ssh->exec('echo ${GROUPS[*]}'), true);
             return true;
         }
     }
     return false;
 }
 /**
  * @return bool
  */
 public function login()
 {
     $password = $this->password;
     $username = $this->username;
     // try to login using ssh key
     if (null === $password) {
         try {
             // try with agent (suppress stupid phpseclib warnings/errors)
             $agent = @new Agent();
         } catch (\Exception $e) {
             $agent = null;
         }
         if ($success = $this->sftp->login($username, $agent)) {
             return true;
         }
         // try with key manually
         $identityFilePath = $_SERVER['HOME'] . '/.ssh/id_rsa';
         if (!is_file($identityFilePath)) {
             $this->io->write(sprintf('Public key file not found in %s', $identityFilePath));
             return false;
         }
         $identityFile = file_get_contents($identityFilePath);
         $key = new RSA();
         $loaded = $key->loadKey($identityFile);
         // first try without keypass
         if (!$loaded || false === ($success = $this->sftp->login($username, $key))) {
             $attempts = 3;
             // now N attempts to load the identity file
             while ($attempts--) {
                 // retry with password
                 $this->keyPassword = $this->keyPassword ?: $this->io->askAndHideAnswer(sprintf('Enter passphrase for %s: ', $identityFilePath));
                 $key->setPassword($this->keyPassword);
                 $loaded = $key->loadKey($identityFile);
                 if (!$loaded) {
                     if ($attempts > 0) {
                         $this->keyPassword = null;
                         $this->io->write('Permission denied, please try again.');
                     }
                 } else {
                     if (false === ($success = $this->sftp->login($username, $key))) {
                         $this->io->write(sprintf('%s@%s: Permission denied (publickey)', $this->username, $this->host));
                     }
                     return $success;
                 }
             }
         }
         return $success;
     }
     // login with given password
     return $this->sftp->login($username, $password);
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function connect()
 {
     $serverConfig = $this->getConfiguration();
     $this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600);
     switch ($serverConfig->getAuthenticationMethod()) {
         case Configuration::AUTH_BY_PASSWORD:
             $result = $this->sftp->login($serverConfig->getUser(), $serverConfig->getPassword());
             break;
         case Configuration::AUTH_BY_IDENTITY_FILE:
             $key = new RSA();
             $key->setPassword($serverConfig->getPassPhrase());
             $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
             $result = $this->sftp->login($serverConfig->getUser(), $key);
             break;
         case Configuration::AUTH_BY_PEM_FILE:
             $key = new RSA();
             $key->loadKey(file_get_contents($serverConfig->getPemFile()));
             $result = $this->sftp->login($serverConfig->getUser(), $key);
             break;
         case Configuration::AUTH_BY_AGENT:
             $key = new Agent();
             $key->startSSHForwarding(null);
             $result = $this->sftp->login($serverConfig->getUser(), $key);
             break;
         default:
             throw new RuntimeException('You need to specify authentication method.');
     }
     if (!$result) {
         throw new RuntimeException('Unable to login with the provided credentials.');
     }
 }
 /**
  * Connects to remote server.
  *
  * @throws \InvalidArgumentException|\RuntimeException
  */
 protected function connect()
 {
     $host = $this->gitEnvironment->getHost();
     $username = $this->gitEnvironment->getUsername();
     $port = $this->gitEnvironment->getPort();
     $password = $this->gitEnvironment->getPassword();
     $privateKey = $this->gitEnvironment->getPrivateKey();
     $privateKeyPassword = $this->gitEnvironment->getPrivateKeyPassword();
     $this->sftp = new SFTP($host, 22);
     if (!$this->sftp) {
         throw new SshLoginException(sprintf('SSH connection failed on "%s:%s"', $host, $port));
     }
     if (isset($username) && $privateKey != null) {
         $key = new RSA();
         //Set Private Key Password
         if ($privateKeyPassword) {
             $key->setPassword($privateKeyPassword);
         }
         $key->loadKey($privateKey);
         //Login using private key
         if (!$this->sftp->login($username, $key)) {
             throw new SshLoginException(sprintf('SFTP authentication failed for user "%s" using private key', $username));
         }
     } else {
         if (!$this->sftp->login($username, $password)) {
             throw new SshLoginException(sprintf('SFTP authentication failed for user "%s" using password', $username));
         }
     }
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  */
 public function connect()
 {
     $this->sftp = new SFTP($this->configuration->getHost(), $this->configuration->getPort());
     switch ($this->configuration->getAuthenticationMethod()) {
         case ServerConfiguration::AUTH_BY_IDENTITY_FILE:
             $key = new RSA();
             $key->loadKey(file_get_contents($this->configuration->getPrivateKey()));
             $result = $this->sftp->login($this->configuration->getUser(), $key);
             break;
         case ServerConfiguration::AUTH_BY_PEM_FILE:
             $key = new RSA();
             $key->loadKey(file_get_contents($this->configuration->getPemFile()));
             $result = $this->sftp->login($this->configuration->getUser(), $key);
             break;
         case ServerConfiguration::AUTH_BY_AGENT:
             $key = new Agent();
             $result = $this->sftp->login($this->configuration->getUser(), $key);
             break;
         default:
             throw new \RuntimeException('You need to specify authentication method.');
     }
     if (!$result) {
         throw new \RuntimeException('Unable to login with the provided credentials.');
     }
 }
Exemple #6
0
 /**
  * @param \Shikiryu\Backup\Backup\BackupAbstract $backup
  * @param array $config
  * @throws \Exception
  */
 public function __construct($backup, $config)
 {
     parent::__construct($backup, $config);
     $this->connection = new LibSFTP($this->host, $this->port);
     if (!$this->connection->login($this->login, $this->password)) {
         throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
     }
 }
Exemple #7
0
 public function setUp()
 {
     parent::setUp();
     $this->scratchDir = uniqid('phpseclib-sftp-scratch-');
     $this->sftp = new SFTP($this->getEnv('SSH_HOSTNAME'));
     $this->assertTrue($this->sftp->login($this->getEnv('SSH_USERNAME'), $this->getEnv('SSH_PASSWORD')));
     $this->assertTrue($this->sftp->mkdir($this->scratchDir));
     $this->assertTrue($this->sftp->chdir($this->scratchDir));
 }
 /**
  * Executes the PullDbViaSsh Task.
  *
  * @return Robo\Result
  */
 public function run()
 {
     // Login to the remote server
     $this->printTaskInfo('Logging into remote server - <info>ssh://' . $this->sshUser . '@' . $this->sshHost . '/</info>');
     $ssh = new SFTP($this->sshHost);
     // Do we use password or a key
     if (file_exists($this->sshKey) && empty($this->sshPass)) {
         $key = new RSA();
         $key->loadKey(file_get_contents($this->sshKey));
         if (!$ssh->login($this->sshUser, $key)) {
             throw new RuntimeException('Failed to login via SSH using Key Based Auth.');
         }
     } else {
         if (!$ssh->login($this->sshUser, $this->sshPass)) {
             throw new RuntimeException('Failed to login via SSH using Password Based Auth.');
         }
     }
     // Create our dump filename
     $dump_name = $this->remoteDbName . '_' . time();
     // Create our dump on the remote server
     $cmd = 'mysqldump ' . '-h' . $this->remoteDbHost . ' -u' . $this->remoteDbUser . ' ' . (empty($this->remoteDbPass) ? '' : '-p' . $this->remoteDbPass) . ' ' . $this->remoteDbName . ' > /tmp/' . $dump_name . '.sql';
     $this->printTaskInfo('Dumping db on remote server - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to create dump on remote server. ' . $results);
     }
     // Compressing dump
     $cmd = 'gzip /tmp/' . $dump_name . '.sql';
     $this->printTaskInfo('Compressing dump on remote server - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to compress dump on remote server. ' . $results);
     }
     // Copy it down locally
     $this->printTaskInfo('Transfering dump to local.');
     $temp_dump_name = tempnam(sys_get_temp_dir(), 'dump');
     $temp_dump = $temp_dump_name . '.sql.gz';
     if (!$ssh->get('/tmp/' . $dump_name . '.sql.gz', $temp_dump)) {
         throw new RuntimeException('Failed to download dump.');
     }
     // Remove the dump from the remote server
     $this->printTaskInfo('Removing dump from remote server - <info>rm /tmp/' . $dump_name . '.sql.gz</info>');
     if (!$ssh->delete('/tmp/' . $dump_name . '.sql.gz')) {
         throw new RuntimeException('Failed to delete dump on remote server.');
     }
     // Import the dump locally
     if (!$this->taskImportSqlDump($temp_dump)->host($this->localDbHost)->user($this->localDbUser)->pass($this->localDbPass)->name($this->localDbName)->run()->wasSuccessful()) {
         throw new RuntimeException('Failed to import dump on local server.');
     }
     $this->printTaskInfo('Deleting dump locally.');
     unlink($temp_dump);
     unlink($temp_dump_name);
     // If we get to here assume everything worked
     return Result::success($this);
 }
 /**
  * {@inheritdoc}
  */
 public function connect()
 {
     if ($this->isConnected()) {
         return true;
     }
     $this->connection = new SFTP($this->hostname);
     return $this->connection->login($this->authenticationUsername, $this->authenticationCredentials);
 }
Exemple #10
0
 /**
  * @throws GlSyncFtpException
  */
 private function login()
 {
     if (isset($this->sftp)) {
         return;
     }
     $this->sftp = new SFTP($this->server);
     if (!$this->sftp->login($this->user, $this->password)) {
         throw new GlSyncFtpException('Login Failed');
     }
 }
Exemple #11
0
 /**
  * Connect to remote account.
  *
  * @param array $accountCredentials The list of remote account credentials
  *
  * @return SftpInterface The current instance
  */
 public function connect(array $accountCredentials) : SftpInterface
 {
     /**
      * Note: on a successful login, the default directory
      * is the home directory (e.g., /home/wwwdyn).
      */
     $this->isValidFtpAccountCredentials($accountCredentials) ? $this->appendToStorageRegister($accountCredentials) : $this->logError('AbstractSftp::connect()', 'invalid account credentials', 'E076');
     list($accountHost, $accountUsername, $accountPassword) = [$this->get('account_host'), $this->get('account_username'), $this->get('account_password')];
     $this->setProperty('netSftp', new NetSftp($accountHost));
     $this->netSftp->login($accountUsername, $accountPassword);
     return $this;
 }
Exemple #12
0
 /**
  * Log into the server.
  *
  * @return void
  */
 public function login()
 {
     // Do nothing if already logged in
     if ($this->in) {
         return;
     }
     if ($this->config('key')) {
         // We prefer logging in via keys
         $key = new RSA();
         if ($phrase = $this->config('keyphrase')) {
             $key->setPassword($phrase);
         }
         $key->loadKey(file_get_contents($this->config('key')));
     } else {
         // Password is less preferred, but anyway...
         $key = $this->config('password');
     }
     if (!($this->in = $this->ssh->login($this->config('username'), $key))) {
         throw new Exception('Failed to log in.');
     }
 }
Exemple #13
0
 /**
  * @param string      $Username
  * @param string      $File
  * @param null|string $Password
  *
  * @return SFTP
  * @throws ComponentException
  */
 public function loginCredentialKey($Username, $File, $Password = null)
 {
     $this->Username = $Username;
     $this->Key = $File;
     $this->Password = $Password;
     $Key = new RSA();
     if (null !== $Password) {
         $Key->setPassword($Password);
     }
     if (!$Key->loadKey(file_get_contents($File))) {
         throw new ComponentException(__METHOD__ . ': Key failed');
     }
     if (!$this->Connection->login($Username, $Key)) {
         throw new ComponentException(__METHOD__ . ': Login failed');
     }
     return $this;
 }
Exemple #14
0
 /**
  * Returns the connection.
  *
  * @return SFTP connected client instance
  * @throws \Exception when the connection failed
  */
 public function getConnection()
 {
     if (!is_null($this->client)) {
         return $this->client;
     }
     $hostKeys = $this->readHostKeys();
     $this->client = new SFTP($this->host, $this->port);
     // The SSH Host Key MUST be verified before login().
     $currentHostKey = $this->client->getServerPublicHostKey();
     if (array_key_exists($this->host, $hostKeys)) {
         if ($hostKeys[$this->host] != $currentHostKey) {
             throw new \Exception('Host public key does not match known key');
         }
     } else {
         $hostKeys[$this->host] = $currentHostKey;
         $this->writeHostKeys($hostKeys);
     }
     if (!$this->client->login($this->user, $this->password)) {
         throw new \Exception('Login failed');
     }
     return $this->client;
 }
Exemple #15
0
 /**
  * (non-PHPDoc)
  *
  * @see    \phpbu\App\Backup\Sync::sync()
  * @param  \phpbu\App\Backup\Target $target
  * @param  \phpbu\App\Result        $result
  * @throws \phpbu\App\Backup\Sync\Exception
  */
 public function sync(Target $target, Result $result)
 {
     // silence phpseclib
     $old = error_reporting(0);
     $sftp = new phpseclib\Net\SFTP($this->host);
     if (!$sftp->login($this->user, $this->password)) {
         error_reporting($old);
         throw new Exception(sprintf('authentication failed for %s@%s%s', $this->user, $this->host, empty($this->password) ? '' : ' with password ****'));
     }
     error_reporting($old);
     $remoteFilename = $target->getFilename();
     $localFile = $target->getPathname();
     if ('' !== $this->remotePath) {
         $remoteDirs = explode('/', $this->remotePath);
         foreach ($remoteDirs as $dir) {
             if (!$sftp->is_dir($dir)) {
                 $result->debug(sprintf('creating remote dir \'%s\'', $dir));
                 $sftp->mkdir($dir);
             }
             $result->debug(sprintf('change to remote dir \'%s\'', $dir));
             $sftp->chdir($dir);
         }
     }
     $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
     $result->debug(sprintf('last error \'%s\'', $sftp->getLastSFTPError()));
     /** @noinspection PhpInternalEntityUsedInspection */
     if (!$sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) {
         throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $sftp->getLastSFTPError()));
     }
 }
Exemple #16
0
 /**
  * @param SSH2|SFTP $connector
  * @return SSH2|SFTP
  * @throws \Exception
  */
 protected function auth($connector)
 {
     switch ($this->auth) {
         case self::AUTH_KEYFILE:
             $password = new RSA();
             if (!is_null($this->getPassword())) {
                 $password->setPassword($this->getPassword());
             }
             $password->loadKey($this->getKeyfile());
             break;
         case self::AUTH_PASSWORD:
             // break intentionally omitted
         // break intentionally omitted
         default:
             $password = $this->getPassword();
             break;
     }
     if (!isset($password)) {
         $loggedIn = $connector->login($this->username);
     } else {
         $loggedIn = $connector->login($this->username, $password);
     }
     if (!$loggedIn) {
         throw new \Exception(sprintf('SSH authentication (%s) with %s on %s:%s failed!', $this->auth, $this->username, $this->hostname, $this->port));
     }
     return $connector;
 }
Exemple #17
0
 public function createSFTPSession()
 {
     $sftp_url = $this->config['batch_url'];
     $sftp_username = $this->config['sftp_username'];
     $sftp_password = $this->config['sftp_password'];
     $session = new SFTP($sftp_url);
     if (!$session->login($sftp_username, $sftp_password)) {
         throw new \RuntimeException("Failed to SFTP with the username {$sftp_username} and the password {$sftp_password} to the host {$sftp_url}. Check your credentials!");
     }
     return $session;
 }
Exemple #18
0
 /**
  * Executes the SftpSync Task.
  *
  * @return Robo\Result
  */
 public function run()
 {
     // Tell the world whats happening
     $this->printTaskInfo('Logging into server - ' . '<info>' . 'sftp://' . $this->sftpUser . ':' . (empty($this->sftpPass) ? $this->sftpKey : $this->sftpPass) . '@' . $this->sftpHost . '</info>');
     // Intialise our sftp connection
     $sftp = new SFTP($this->sftpHost);
     // Do we use password or a key
     if (file_exists($this->sftpKey) && empty($this->sshPass)) {
         $key = new RSA();
         $key->loadKey(file_get_contents($this->sshKey));
         if (!$sftp->login($this->sshUser, $key)) {
             return Result::error($this, 'Failed to login via SFTP using Key Based Auth.');
         }
     } else {
         if (!$sftp->login($this->sftpUser, $this->sftpPass)) {
             return Result::error($this, 'Failed to login via SFTP using Password Based Auth.');
         }
     }
     // Check to see if a .htaccess file exists
     if ($sftp->stat($this->remotePath . '/.htaccess')) {
         // It does so lets rename it, just in case it messes with out helper script
         $this->printTaskInfo('Renaming .htaccess file');
         if (!$sftp->rename($this->remotePath . '/.htaccess', $this->remotePath . '/disabled.htaccess')) {
             return Result::error($this, 'Failed to rename .htaccess file');
         }
     }
     // Upload helper script
     $this->printTaskInfo('Uploading sftp helper script.');
     if (!$sftp->put($this->remotePath . '/sftp-upload-helper.php', $this->sftp_upload_helper())) {
         return Result::error($this, 'UPLOAD OF HELPER SCRIPT FAILED');
     }
     // Get the local and remote file arrays
     $this->printTaskInfo('Get a list of files on the local and remote servers.');
     $local_files = $this->get_local_file_hashes($this->localPath);
     $remote_files = $this->get_remote_files();
     // Delete helper script
     $this->printTaskInfo('Deleting sftp helper script.');
     if (!$sftp->delete($this->remotePath . '/sftp-upload-helper.php')) {
         return Result::error($this, 'FAILED TO DELETE HELPER SCRIPT');
     }
     // Rename htaccess file back
     if ($sftp->stat($this->remotePath . '/disabled.htaccess')) {
         // It does so lets rename it, just in case it messes with out helper script
         $this->printTaskInfo('Renaming .htaccess file back to original');
         if (!$sftp->rename($this->remotePath . '/disabled.htaccess', $this->remotePath . '/.htaccess')) {
             return Result::error($this, 'Failed to rename .htaccess file back to original. OH SNAP... better fix this ASAP!');
         }
     }
     $this->printTaskInfo('Comparing files between local and remote servers.');
     // Create some arrays
     $files_to_ignore = [];
     $files_to_upload = [];
     $files_to_delete = [];
     $folders_to_create = [];
     $folders_to_delete = [];
     // Merge in our own ignores
     $files_to_ignore = array_merge($files_to_ignore, $this->ignore);
     // Remove any double ups in our ignore array
     $files_to_ignore = array_unique($files_to_ignore);
     // Remove a few extra items
     foreach ($files_to_ignore as $key => $value) {
         // We don't want to ignore the vendor dir
         if ($value == './vendor') {
             unset($files_to_ignore[$key]);
         }
         // We can't ignore everything
         if ($value == './') {
             unset($files_to_ignore[$key]);
         }
     }
     // Loop through the local files array looking for files that
     // don't exist or are different on the remote server.
     // ie: Files to upload
     foreach ($local_files as $path => $hash) {
         if (isset($remote_files[$path])) {
             if ($hash != $remote_files[$path]) {
                 if (!in_array($path, $files_to_ignore)) {
                     $files_to_upload[] = $path;
                 }
             }
         } else {
             if (!in_array($path, $files_to_ignore)) {
                 if ($hash == 'dir') {
                     $folders_to_create[] = $path;
                 } else {
                     $files_to_upload[] = $path;
                 }
             }
         }
     }
     // Loop through the remote files array looking for
     // files that don't exist on the local server.
     // ie: Files to delete
     foreach ($remote_files as $path => $hash) {
         if (!isset($local_files[$path])) {
             if (!in_array($path, $files_to_ignore)) {
                 if ($hash == 'dir') {
                     $folders_to_delete[] = $path;
                 } else {
                     $files_to_delete[] = $path;
                 }
             }
         }
     }
     // We need to delete the children first
     $folders_to_delete = array_reverse($folders_to_delete);
     // Perform a double check of our files to ignore array
     foreach ($files_to_ignore as $path) {
         foreach ($files_to_upload as $key => $file) {
             if (strpos($file, $path) !== false) {
                 unset($files_to_upload[$key]);
             }
         }
         foreach ($files_to_delete as $key => $file) {
             if (strpos($file, $path) !== false) {
                 unset($files_to_delete[$key]);
             }
         }
         foreach ($folders_to_create as $key => $file) {
             if (strpos($file, $path) !== false) {
                 unset($folders_to_create[$key]);
             }
         }
         foreach ($folders_to_delete as $key => $file) {
             if (strpos($file, $path) !== false) {
                 unset($folders_to_delete[$key]);
             }
         }
     }
     // Check the dry run option
     if (!$this->dryRun) {
         // Create any needed folders
         foreach ($folders_to_create as $file) {
             $remotepath = str_replace('//', '/', $this->remotePath . substr($file, 1));
             if (!$sftp->mkdir($remotepath)) {
                 return Result::error($this, 'FAILED TO CREATE FOLDER: ' . $remotepath);
             }
             $this->printTaskInfo('Folder Created: ' . $file);
         }
         // Upload our files
         foreach ($files_to_upload as $file) {
             $this->printTaskInfo('Uploading: ' . $file);
             $localpath = str_replace('//', '/', $this->localPath . substr($file, 1));
             $remotepath = str_replace('//', '/', $this->remotePath . substr($file, 1));
             if (!$sftp->put($remotepath, $localpath, NET_SFTP_LOCAL_FILE)) {
                 return Result::error($this, 'FAILED TO UPLOAD FILE: ' . $file);
             }
         }
         // Do we want to delete all the files?
         $delete_all = false;
         if (count($files_to_delete) > 0) {
             print_r($files_to_delete);
             do {
                 $answer = $this->ask('Do you want to delete all these files? (yes|no)');
             } while ($answer != 'yes' && $answer != 'no' && $answer != 'y' && $answer != 'n');
             if ($answer == 'yes' || $answer == 'y') {
                 $delete_all = true;
             }
         }
         // Loop through our files to delete.
         foreach ($files_to_delete as $file) {
             $remotepath = str_replace('//', '/', $this->remotePath . substr($file, 1));
             if ($delete_all) {
                 if (!$sftp->delete($remotepath)) {
                     return Result::error($this, 'FAILED TO DELETE FILE: ' . $file);
                 } else {
                     $this->printTaskInfo('Deleted: ' . $file);
                 }
             } else {
                 do {
                     $answer = $this->ask('Do you really want to delete? (yes|no)' . $remotepath);
                 } while ($answer != 'yes' && $answer != 'no' && $answer != 'y' && $answer != 'n');
                 if ($answer == 'yes' || $answer == 'y') {
                     if (!$sftp->delete($remotepath)) {
                         return Result::error($this, 'FAILED TO DELETE FILE: ' . $file);
                     }
                     $this->printTaskInfo('Deleted: ' . $file);
                 }
             }
         }
         // Same again but for folders
         $delete_all_folders = false;
         if (count($folders_to_delete) > 0) {
             print_r($folders_to_delete);
             do {
                 $answer = $this->ask('Do you want to delete all these folders? (yes|no)');
             } while ($answer != 'yes' && $answer != 'no' && $answer != 'y' && $answer != 'n');
             if ($answer == 'yes' || $answer == 'y') {
                 $delete_all_folders = true;
             }
         }
         foreach ($folders_to_delete as $file) {
             $remotepath = str_replace('//', '/', $this->remotePath . substr($file, 1));
             if ($delete_all_folders) {
                 if (!$sftp->rmdir($remotepath)) {
                     return Result::error($this, 'FAILED TO DELETE FOLDER: ' . $file);
                 }
                 $this->printTaskInfo('Deleted Folder: ' . $file);
             } else {
                 do {
                     $answer = $this->ask('Do you really want to delete? (yes|no)' . $remotepath);
                 } while ($answer != 'yes' && $answer != 'no' && $answer != 'y' && $answer != 'n');
                 if ($answer == 'yes' || $answer == 'y') {
                     if (!$sftp->rmdir($remotepath)) {
                         return Result::error($this, 'FAILED TO DELETE FOLDER: ' . $file);
                     }
                     $this->printTaskInfo('Deleted Folder: ' . $file);
                 }
             }
         }
         $this->printTaskInfo('The remote server has been synced :)');
     } else {
         $this->printTaskInfo('Files that would have been uploaded: ');
         print_r($files_to_upload);
         $this->printTaskInfo('Files that would have been deleted: ');
         print_r($files_to_delete);
         $this->printTaskInfo('Folders that would have been created: ');
         print_r($folders_to_create);
         $this->printTaskInfo('Folders that would have been deleted: ');
         print_r($folders_to_delete);
     }
     // If we get to here we assume everything worked
     return Result::success($this);
 }
 /**
  * Executes the PushDbViaSsh Task.
  *
  * @return Robo\Result
  */
 public function run()
 {
     // Login to the remote server
     $this->printTaskInfo('Logging into remote server - <info>ssh://' . $this->sshUser . '@' . $this->sshHost . '/</info>');
     $ssh = new SFTP($this->sshHost);
     // Do we use password or a key
     if (file_exists($this->sshKey) && empty($this->sshPass)) {
         $key = new RSA();
         $key->loadKey(file_get_contents($this->sshKey));
         if (!$ssh->login($this->sshUser, $key)) {
             throw new RuntimeException('Failed to login via SSH using Key Based Auth.');
         }
     } else {
         if (!$ssh->login($this->sshUser, $this->sshPass)) {
             throw new RuntimeException('Failed to login via SSH using Password Based Auth.');
         }
     }
     // Create our dump filename
     $dump_name = tempnam(sys_get_temp_dir(), 'dump');
     // Create our dump locally
     $cmd = 'mysqldump' . ' -h' . $this->localDbHost . ' -u' . $this->localDbUser . ' ' . (empty($this->localDbPass) ? '' : '-p' . $this->localDbPass) . ' ' . $this->localDbName . ' > ' . $dump_name;
     $this->printTaskInfo('Dumping db on local server - <info>' . $cmd . '</info>');
     if (!$this->taskExec($cmd)->run()->wasSuccessful()) {
         throw new RuntimeException('Failed to create dump locally.' . 'HINT: Is the `mysqldump` binary in your "PATH"?');
     }
     // Compress the dump
     $this->printTaskInfo('Compressing dump on local server - <info>' . $cmd . '</info>');
     if ($fp_out = gzopen($dump_name . '.gz', 'wb9')) {
         if ($fp_in = fopen($dump_name, 'rb')) {
             while (!feof($fp_in)) {
                 gzwrite($fp_out, fread($fp_in, 1024 * 512));
             }
             fclose($fp_in);
         } else {
             throw new RuntimeException('Failed to open source dump file for reading.');
         }
         gzclose($fp_out);
     } else {
         throw new RuntimeException('Failed to open destination compressed dump file for writing.');
     }
     // Copy it up
     $this->printTaskInfo('Transfering dump to remote.');
     $dump_name_remote = '/tmp/' . $this->remoteDbName . '-' . time() . '.sql';
     if (!$ssh->put($dump_name_remote . '.gz', $dump_name, SFTP::SOURCE_LOCAL_FILE)) {
         throw new RuntimeException('Failed to upload db dump.');
     }
     // Decompress dump on remote
     $cmd = 'gzip -d ' . $dump_name_remote . '.gz';
     $this->printTaskInfo('Decompressing dump on remote server - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to decompress dump on remote.');
     }
     // Import db remotely
     $cmd = 'mysql' . ' -h' . $this->remoteDbHost . ' -u' . $this->remoteDbUser . ' ' . (empty($this->remoteDbPass) ? '' : '-p' . $this->remoteDbPass) . ' ' . $this->remoteDbName . ' < ' . $dump_name_remote;
     $this->printTaskInfo('Importing dump remotely - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to import dump on remote.');
     }
     // Delete dump from remote server
     $this->printTaskInfo('Removing dump from remote server. - <info>' . $dump_name_remote . '</info>');
     if (!$ssh->delete($dump_name_remote)) {
         return Result::error($this, 'Failed to delete dump on remote.');
     }
     // Remove the dump from the local server
     $this->printTaskInfo('Removing dump from local server. - <info>' . $dump_name . '</info>');
     if (!unlink($dump_name)) {
         return Result::error($this, 'Failed to delete dump from local.');
     }
     // If we get to here assume everything worked
     return Result::success($this);
 }