Projet

Général

Profil

Paste
Télécharger (4,02 ko) Statistiques
| Branche: | Révision:

root / htmltest / includes / filetransfer / ssh.inc @ 85ad3d82

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
    } else {
76
      throw new FileTransferException('Cannot check @path.', NULL, array('@path' => $path));
77
    }
78
  }
79

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

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

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