Exemple #1
0
 public function __construct($params)
 {
     // The sftp:// scheme has to be manually registered via inclusion of
     // the 'Net/SFTP/Stream.php' file which registers the Net_SFTP_Stream
     // stream wrapper as a side effect.
     // A slightly better way to register the stream wrapper is available
     // since phpseclib 0.3.7 in the form of a static call to
     // Net_SFTP_Stream::register() which will trigger autoloading if
     // necessary.
     // TODO: Call Net_SFTP_Stream::register() instead when phpseclib is
     //       updated to 0.3.7 or higher.
     require_once 'Net/SFTP/Stream.php';
     $this->host = $params['host'];
     $proto = strpos($this->host, '://');
     if ($proto != false) {
         $this->host = substr($this->host, $proto + 3);
     }
     $this->user = $params['user'];
     $this->password = $params['password'];
     $this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
     if ($this->root[0] != '/') {
         $this->root = '/' . $this->root;
     }
     if (substr($this->root, -1, 1) != '/') {
         $this->root .= '/';
     }
     $hostKeys = $this->readHostKeys();
     $this->client = new \Net_SFTP($this->host);
     // 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');
     }
 }
Exemple #2
0
 /**
  * Returns the connection.
  *
  * @return \Net_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 \Net_SFTP($this->host);
     // 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;
 }