class SSH

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/FileTransfer/SSH.php \Drupal\Core\FileTransfer\SSH
  2. 10 core/lib/Drupal/Core/FileTransfer/SSH.php \Drupal\Core\FileTransfer\SSH
  3. 11.x core/lib/Drupal/Core/FileTransfer/SSH.php \Drupal\Core\FileTransfer\SSH

The SSH connection class for the update module.

Hierarchy

Expanded class hierarchy of SSH

6 string references to 'SSH'
core.services.yml in core/core.services.yml
core/core.services.yml
default.services.yml in sites/default/default.services.yml
sites/default/default.services.yml
default.services.yml in core/assets/scaffold/files/default.services.yml
core/assets/scaffold/files/default.services.yml
system_filetransfer_info in core/modules/system/system.module
Implements hook_filetransfer_info().
x0f.php in core/lib/Drupal/Component/Transliteration/data/x0f.php

... See full list

File

core/lib/Drupal/Core/FileTransfer/SSH.php, line 8

Namespace

Drupal\Core\FileTransfer
View source
class SSH extends FileTransfer implements ChmodInterface {
    
    /**
     * {@inheritdoc}
     */
    public function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
        $this->username = $username;
        $this->password = $password;
        $this->hostname = $hostname;
        $this->port = $port;
        parent::__construct($jail);
    }
    
    /**
     * {@inheritdoc}
     */
    public function connect() {
        $this->connection = @ssh2_connect($this->hostname, $this->port);
        if (!$this->connection) {
            throw new FileTransferException('SSH Connection failed to @host:@port', 0, [
                '@host' => $this->hostname,
                '@port' => $this->port,
            ]);
        }
        if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
            throw new FileTransferException('The supplied username/password combination was not accepted.');
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function factory($jail, $settings) {
        $username = empty($settings['username']) ? '' : $settings['username'];
        $password = empty($settings['password']) ? '' : $settings['password'];
        $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
        $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port'];
        return new SSH($jail, $username, $password, $hostname, $port);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function copyFileJailed($source, $destination) {
        if (!@ssh2_scp_send($this->connection, $source, $destination)) {
            throw new FileTransferException('Cannot copy @source_file to @destination_file.', 0, [
                '@source' => $source,
                '@destination' => $destination,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function copyDirectoryJailed($source, $destination) {
        if (@(!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination)))) {
            throw new FileTransferException('Cannot copy directory @directory.', 0, [
                '@directory' => $source,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function createDirectoryJailed($directory) {
        if (@(!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory)))) {
            throw new FileTransferException('Cannot create directory @directory.', 0, [
                '@directory' => $directory,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function removeDirectoryJailed($directory) {
        if (@(!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory)))) {
            throw new FileTransferException('Cannot remove @directory.', 0, [
                '@directory' => $directory,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    protected function removeFileJailed($destination) {
        if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
            throw new FileTransferException('Cannot remove @directory.', 0, [
                '@directory' => $destination,
            ]);
        }
    }
    
    /**
     * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
     *
     * WARNING: This is untested. It is not currently used, but should do the
     * trick.
     */
    public function isDirectory($path) {
        $directory = escapeshellarg($path);
        $cmd = "[ -d {$directory} ] && echo 'yes'";
        if ($output = @ssh2_exec($this->connection, $cmd)) {
            if ($output == 'yes') {
                return TRUE;
            }
            return FALSE;
        }
        else {
            throw new FileTransferException('Cannot check @path.', 0, [
                '@path' => $path,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function isFile($path) {
        $file = escapeshellarg($path);
        $cmd = "[ -f {$file} ] && echo 'yes'";
        if ($output = @ssh2_exec($this->connection, $cmd)) {
            if ($output == 'yes') {
                return TRUE;
            }
            return FALSE;
        }
        else {
            throw new FileTransferException('Cannot check @path.', 0, [
                '@path' => $path,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function chmodJailed($path, $mode, $recursive) {
        $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path));
        if (@(!ssh2_exec($this->connection, $cmd))) {
            throw new FileTransferException('Cannot change permissions of @path.', 0, [
                '@path' => $path,
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSettingsForm() {
        $form = parent::getSettingsForm();
        $form['advanced']['port']['#default_value'] = 22;
        return $form;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FileTransfer::$chrootPath private property Path to connection chroot.
FileTransfer::$connectionHandle private property The instantiated connection object.
FileTransfer::$hostname protected property The hostname for this file transfer.
FileTransfer::$jail protected property Full path to directory where file-transfer is restricted to.
FileTransfer::$password protected property The password for this file transfer. 1
FileTransfer::$port protected property The port for this file transfer. 1
FileTransfer::$username protected property The username for this file transfer. 1
FileTransfer::checkPath final protected function Checks that the path is inside the jail and throws an exception if not.
FileTransfer::chmod final public function Changes the permissions of the specified $path (file or directory).
FileTransfer::copyDirectory final public function Copies a directory.
FileTransfer::copyFile final public function Copies a file.
FileTransfer::createDirectory final public function Creates a directory.
FileTransfer::findChroot public function Returns the chroot property for this connection.
FileTransfer::fixRemotePath final protected function Returns a modified path suitable for passing to the server.
FileTransfer::removeDirectory final public function Removes a directory.
FileTransfer::removeFile final public function Removes a file.
FileTransfer::sanitizePath public function Changes backslashes to slashes, also removes a trailing slash.
FileTransfer::setChroot public function Sets the chroot and changes the jail to match the correct path scheme.
FileTransfer::__get public function Implements the magic __get() method.
FileTransfer::__isset public function
FileTransfer::__set public function
FileTransfer::__unset public function
SSH::chmodJailed public function Changes the permissions of the file / directory specified in $path. Overrides ChmodInterface::chmodJailed
SSH::connect public function Connects to the server. Overrides FileTransfer::connect
SSH::copyDirectoryJailed protected function Copies a directory. Overrides FileTransfer::copyDirectoryJailed
SSH::copyFileJailed protected function Copies a file. Overrides FileTransfer::copyFileJailed
SSH::createDirectoryJailed protected function Creates a directory. Overrides FileTransfer::createDirectoryJailed
SSH::factory public static function Defines a factory method for this class. Overrides FileTransfer::factory
SSH::getSettingsForm public function Returns a form to collect connection settings credentials. Overrides FileTransfer::getSettingsForm
SSH::isDirectory public function Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory(). Overrides FileTransfer::isDirectory
SSH::isFile public function Checks if a particular path is a file (not a directory). Overrides FileTransfer::isFile
SSH::removeDirectoryJailed protected function Removes a directory. Overrides FileTransfer::removeDirectoryJailed
SSH::removeFileJailed protected function Removes a file. Overrides FileTransfer::removeFileJailed
SSH::__construct public function Constructs a Drupal\Core\FileTransfer\FileTransfer object. Overrides FileTransfer::__construct

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.