class FileTransferSSH in Drupal 7
The SSH connection class for the update module.
Hierarchy
- class \FileTransfer
- class \FileTransferSSH implements FileTransferChmodInterface
Expanded class hierarchy of FileTransferSSH
1 string reference to 'FileTransferSSH'
- system_filetransfer_info in modules/
system/ system.module - Implements hook_filetransfer_info().
File
- includes/
filetransfer/ ssh.inc, line 6
View source
class FileTransferSSH extends FileTransfer implements FileTransferChmodInterface {
function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
$this->username = $username;
$this->password = $password;
$this->hostname = $hostname;
$this->port = $port;
parent::__construct($jail);
}
function connect() {
$this->connection = @ssh2_connect($this->hostname, $this->port);
if (!$this->connection) {
throw new FileTransferException('SSH Connection failed to @host:@port', NULL, array(
'@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.');
}
}
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 FileTransferSSH($jail, $username, $password, $hostname, $port);
}
protected function copyFileJailed($source, $destination) {
if (!@ssh2_scp_send($this->connection, $source, $destination)) {
throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array(
'@source' => $source,
'@destination' => $destination,
));
}
}
protected function copyDirectoryJailed($source, $destination) {
if (@(!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination)))) {
throw new FileTransferException('Cannot copy directory @directory.', NULL, array(
'@directory' => $source,
));
}
}
protected function createDirectoryJailed($directory) {
if (@(!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory)))) {
throw new FileTransferException('Cannot create directory @directory.', NULL, array(
'@directory' => $directory,
));
}
}
protected function removeDirectoryJailed($directory) {
if (@(!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory)))) {
throw new FileTransferException('Cannot remove @directory.', NULL, array(
'@directory' => $directory,
));
}
}
protected function removeFileJailed($destination) {
if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
throw new FileTransferException('Cannot remove @directory.', NULL, array(
'@directory' => $destination,
));
}
}
/**
* 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.', NULL, array(
'@path' => $path,
));
}
}
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.', NULL, array(
'@path' => $path,
));
}
}
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.', NULL, array(
'@path' => $path,
));
}
}
/**
* Returns the form to configure the FileTransfer class for SSH.
*/
public function getSettingsForm() {
$form = parent::getSettingsForm();
$form['advanced']['port']['#default_value'] = 22;
return $form;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileTransfer:: |
protected | property | ||
FileTransfer:: |
protected | property | 1 | |
FileTransfer:: |
protected | property | 1 | |
FileTransfer:: |
protected | property | 1 | |
FileTransfer:: |
final protected | function | Checks that the path is inside the jail and throws an exception if not. | |
FileTransfer:: |
final public | function | ||
FileTransfer:: |
final public | function | Copies a directory. | |
FileTransfer:: |
final public | function | Copies a file. | |
FileTransfer:: |
final public | function | Creates a directory. | |
FileTransfer:: |
function | Return the chroot property for this connection. | ||
FileTransfer:: |
final protected | function | Returns a modified path suitable for passing to the server. If a path is a windows path, makes it POSIX compliant by removing the drive letter. If $this->chroot has a value, it is stripped from the path to allow for chroot'd filetransfer systems. | |
FileTransfer:: |
final public | function | Removes a directory. | |
FileTransfer:: |
final public | function | Removes a file. | |
FileTransfer:: |
function | Changes backslashes to slashes, also removes a trailing slash. | ||
FileTransfer:: |
function | Sets the chroot and changes the jail to match the correct path scheme | ||
FileTransfer:: |
function | Implementation of the magic __get() method. | ||
FileTransferSSH:: |
function |
Changes the permissions of the file / directory specified in $path Overrides FileTransferChmodInterface:: |
||
FileTransferSSH:: |
function |
Connect to the server. Overrides FileTransfer:: |
||
FileTransferSSH:: |
protected | function |
Copies a directory. Overrides FileTransfer:: |
|
FileTransferSSH:: |
protected | function |
Copies a file. Overrides FileTransfer:: |
|
FileTransferSSH:: |
protected | function |
Creates a directory. Overrides FileTransfer:: |
|
FileTransferSSH:: |
static | function |
Classes that extend this class must override the factory() static method. Overrides FileTransfer:: |
|
FileTransferSSH:: |
public | function |
Returns the form to configure the FileTransfer class for SSH. Overrides FileTransfer:: |
|
FileTransferSSH:: |
public | function |
WARNING: This is untested. It is not currently used, but should do the trick. Overrides FileTransfer:: |
|
FileTransferSSH:: |
public | function |
Checks if a particular path is a file (not a directory). Overrides FileTransfer:: |
|
FileTransferSSH:: |
protected | function |
Removes a directory. Overrides FileTransfer:: |
|
FileTransferSSH:: |
protected | function |
Removes a file. Overrides FileTransfer:: |
|
FileTransferSSH:: |
function |
The constructor for the UpdateConnection class. This method is also called
from the classes that extend this class and override this method. Overrides FileTransfer:: |