Project

General

Profile

Paste
Download (4.03 KB) Statistics
| Branch: | Revision:

root / drupal7 / includes / filetransfer / ssh.inc @ 7dd8ec42

1
<?php
2

    
3
/**
4
 * The SSH connection class for the update module.
5
 */
6
class FileTransferSSH extends FileTransfer implements FileTransferChmodInterface {
7

    
8
  function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
9
    $this->username = $username;
10
    $this->password = $password;
11
    $this->hostname = $hostname;
12
    $this->port = $port;
13
    parent::__construct($jail);
14
  }
15

    
16
  function connect() {
17
    $this->connection = @ssh2_connect($this->hostname, $this->port);
18
    if (!$this->connection) {
19
      throw new FileTransferException('SSH Connection failed to @host:@port', NULL, array('@host' => $this->hostname, '@port' => $this->port));
20
    }
21
    if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
22
      throw new FileTransferException('The supplied username/password combination was not accepted.');
23
    }
24
  }
25

    
26
  static function factory($jail, $settings) {
27
    $username = empty($settings['username']) ? '' : $settings['username'];
28
    $password = empty($settings['password']) ? '' : $settings['password'];
29
    $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
30
    $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port'];
31
    return new FileTransferSSH($jail, $username, $password, $hostname, $port);
32
  }
33

    
34
  protected function copyFileJailed($source, $destination) {
35
    if (!@ssh2_scp_send($this->connection, $source, $destination)) {
36
      throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, array('@source' => $source, '@destination' => $destination));
37
    }
38
  }
39

    
40
  protected function copyDirectoryJailed($source, $destination) {
41
    if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) {
42
      throw new FileTransferException('Cannot copy directory @directory.', NULL, array('@directory' => $source));
43
    }
44
  }
45

    
46
  protected function createDirectoryJailed($directory) {
47
    if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
48
      throw new FileTransferException('Cannot create directory @directory.', NULL, array('@directory' => $directory));
49
    }
50
  }
51

    
52
  protected function removeDirectoryJailed($directory) {
53
    if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
54
      throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $directory));
55
    }
56
  }
57

    
58
  protected function removeFileJailed($destination) {
59
    if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
60
      throw new FileTransferException('Cannot remove @directory.', NULL, array('@directory' => $destination));
61
    }
62
  }
63

    
64
  /**
65
   * WARNING: This is untested.  It is not currently used, but should do the trick.
66
   */
67
  public function isDirectory($path) {
68
    $directory = escapeshellarg($path);
69
    $cmd = "[ -d {$directory} ] && echo 'yes'";
70
    if ($output = @ssh2_exec($this->connection, $cmd)) {
71
      if ($output == 'yes') {
72
        return TRUE;
73
      }
74
      return FALSE;
75
    }
76
    else {
77
      throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
78
    }
79
  }
80

    
81
  public function isFile($path) {
82
    $file = escapeshellarg($path);
83
    $cmd = "[ -f {$file} ] && echo 'yes'";
84
    if ($output = @ssh2_exec($this->connection, $cmd)) {
85
      if ($output == 'yes') {
86
        return TRUE;
87
      }
88
      return FALSE;
89
    }
90
    else {
91
      throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
92
    }
93
  }
94

    
95
  function chmodJailed($path, $mode, $recursive) {
96
    $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path));
97
    if (@!ssh2_exec($this->connection, $cmd)) {
98
      throw new FileTransferException('Cannot change permissions of @path.', NULL, array('@path' => $path));
99
    }
100
  }
101

    
102
  /**
103
   * Returns the form to configure the FileTransfer class for SSH.
104
   */
105
  public function getSettingsForm() {
106
    $form = parent::getSettingsForm();
107
    $form['advanced']['port']['#default_value'] = 22;
108
    return $form;
109
  }
110
}