<?php


namespace Nemundo\App\Ssh\Sftp;


use Nemundo\App\Ssh\AbstractSsh;
use Nemundo\App\Ssh\Type\ConnectionType;
use Nemundo\Core\Debug\Debug;
use Nemundo\Core\Log\LogMessage;
use Nemundo\Dev\Linux\Ssh\SshConfig;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Net\SFTP;


abstract class AbstractSftp extends AbstractSsh
{

    /**
     * @var SFTP
     */
    protected $sftp;


    protected function connect()
    {

        define('NET_SFTP_LOGGING', \phpseclib3\Net\SFTP::LOG_COMPLEX);
        $this->sftp = new \phpseclib3\Net\SFTP($this->connection->host);

        if (!$this->sftp->isConnected()) {

            //define('NET_SFTP_LOGGING', SFTP::LOG_COMPLEX);
            //$this->sftp = new SFTP($this->connection->host);


            if ($this->connection == null) {
                $this->connection = SshConfig::$sshConnction;
            }

           /* if ($this->connection->rsaKey !== null) {

                //$rsa = new RSA();
                //$rsa = new Crypt_RSA();

                $key = PublicKeyLoader::load($this->connection->rsaKey, $password = false);


                //$rsa->loadKey($this->connection->rsaKey);

                if (!$this->sftp->login($this->connection->user, $key)) {
                    (new LogMessage())->writeError('SSH Login fehlgeschlagen');
                    return false;
                }

            } else {

                if (!$this->sftp->login($this->connection->user, $this->connection->password)) {
                    (new LogMessage())->writeError('SSH Login fehlgeschlagen');
                    return false;
                }

            }*/


            if ($this->connection->connectionType == ConnectionType::PASSWORD) {


                if (!$this->sftp->login($this->connection->user, $this->connection->password)) {
                    (new LogMessage())->writeError('SSH Login fehlgeschlagen');
                    return false;
                }


            }



        }

    }




}