3 namespace Drupal\Core\FileTransfer;
6 * The SSH connection class for the update module.
8 class SSH extends FileTransfer implements ChmodInterface {
13 public function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
14 $this->username = $username;
15 $this->password = $password;
16 $this->hostname = $hostname;
18 parent::__construct($jail);
24 public function connect() {
25 $this->connection = @ssh2_connect($this->hostname, $this->port);
26 if (!$this->connection) {
27 throw new FileTransferException('SSH Connection failed to @host:@port', NULL, ['@host' => $this->hostname, '@port' => $this->port]);
29 if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
30 throw new FileTransferException('The supplied username/password combination was not accepted.');
37 public static function factory($jail, $settings) {
38 $username = empty($settings['username']) ? '' : $settings['username'];
39 $password = empty($settings['password']) ? '' : $settings['password'];
40 $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
41 $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port'];
42 return new SSH($jail, $username, $password, $hostname, $port);
48 protected function copyFileJailed($source, $destination) {
49 if (!@ssh2_scp_send($this->connection, $source, $destination)) {
50 throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, ['@source' => $source, '@destination' => $destination]);
57 protected function copyDirectoryJailed($source, $destination) {
58 if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) {
59 throw new FileTransferException('Cannot copy directory @directory.', NULL, ['@directory' => $source]);
66 protected function createDirectoryJailed($directory) {
67 if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
68 throw new FileTransferException('Cannot create directory @directory.', NULL, ['@directory' => $directory]);
75 protected function removeDirectoryJailed($directory) {
76 if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
77 throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $directory]);
84 protected function removeFileJailed($destination) {
85 if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
86 throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $destination]);
91 * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
93 * WARNING: This is untested. It is not currently used, but should do the
96 public function isDirectory($path) {
97 $directory = escapeshellarg($path);
98 $cmd = "[ -d {$directory} ] && echo 'yes'";
99 if ($output = @ssh2_exec($this->connection, $cmd)) {
100 if ($output == 'yes') {
106 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
113 public function isFile($path) {
114 $file = escapeshellarg($path);
115 $cmd = "[ -f {$file} ] && echo 'yes'";
116 if ($output = @ssh2_exec($this->connection, $cmd)) {
117 if ($output == 'yes') {
123 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
130 public function chmodJailed($path, $mode, $recursive) {
131 $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path));
132 if (@!ssh2_exec($this->connection, $cmd)) {
133 throw new FileTransferException('Cannot change permissions of @path.', NULL, ['@path' => $path]);
140 public function getSettingsForm() {
141 $form = parent::getSettingsForm();
142 $form['advanced']['port']['#default_value'] = 22;