SSH.php 4.37 KB
Newer Older
1 2
<?php

3 4
namespace Drupal\Core\FileTransfer;

5 6 7
/**
 * The SSH connection class for the update module.
 */
8
class SSH extends FileTransfer implements ChmodInterface {
9

10
  /**
11
   * {@inheritdoc}
12
   */
13
  function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
14 15 16 17 18
    $this->username = $username;
    $this->password = $password;
    $this->hostname = $hostname;
    $this->port = $port;
    parent::__construct($jail);
19 20
  }

21
  /**
22
   * {@inheritdoc}
23
   */
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, array('@host' => $this->hostname, '@port' => $this->port));
28 29 30 31 32 33
    }
    if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
      throw new FileTransferException('The supplied username/password combination was not accepted.');
    }
  }

34
  /**
35
   * {@inheritdoc}
36
   */
37
  static function factory($jail, $settings) {
38 39 40 41
    $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'];
42
    return new SSH($jail, $username, $password, $hostname, $port);
43 44
  }

45
  /**
46
   * {@inheritdoc}
47
   */
48
  protected function copyFileJailed($source, $destination) {
49 50 51 52 53
    if (!@ssh2_scp_send($this->connection, $source, $destination)) {
      throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array('@source' => $source, '@destination' => $destination));
    }
  }

54
  /**
55
   * {@inheritdoc}
56
   */
57 58
  protected function copyDirectoryJailed($source, $destination) {
    if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) {
59 60 61 62
      throw new FileTransferException('Cannot copy directory @directory.', NULL, array('@directory' => $source));
    }
  }

63
  /**
64
   * {@inheritdoc}
65
   */
66 67
  protected function createDirectoryJailed($directory) {
    if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
68 69 70 71
      throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
    }
  }

72
  /**
73
   * {@inheritdoc}
74
   */
75 76
  protected function removeDirectoryJailed($directory) {
    if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
77 78 79
      throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory));
    }
  }
80

81
  /**
82
   * {@inheritdoc}
83
   */
84
  protected function removeFileJailed($destination) {
85 86 87 88
    if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
      throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination));
    }
  }
89

90
  /**
91 92 93 94
   * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
   *
   * WARNING: This is untested. It is not currently used, but should do the
   * trick.
95 96 97 98 99 100 101 102 103
   */
  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;
104 105
    }
    else {
106 107 108
      throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
    }
  }
109

110
  /**
111
   * {@inheritdoc}
112
   */
113 114 115 116 117 118 119 120
  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;
121 122
    }
    else {
123 124 125 126
      throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
    }
  }

127
  /**
128
   * {@inheritdoc}
129
   */
130 131 132 133 134 135
  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));
    }
  }
136 137

  /**
138
   * {@inheritdoc}
139 140 141 142 143 144
   */
  public function getSettingsForm() {
    $form = parent::getSettingsForm();
    $form['advanced']['port']['#default_value'] = 22;
    return $form;
  }
145

146
}