comparison core/lib/Drupal/Core/FileTransfer/SSH.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\FileTransfer;
4
5 /**
6 * The SSH connection class for the update module.
7 */
8 class SSH extends FileTransfer implements ChmodInterface {
9
10 /**
11 * {@inheritdoc}
12 */
13 public function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
14 $this->username = $username;
15 $this->password = $password;
16 $this->hostname = $hostname;
17 $this->port = $port;
18 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, ['@host' => $this->hostname, '@port' => $this->port]);
28 }
29 if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
30 throw new FileTransferException('The supplied username/password combination was not accepted.');
31 }
32 }
33
34 /**
35 * {@inheritdoc}
36 */
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);
43 }
44
45 /**
46 * {@inheritdoc}
47 */
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]);
51 }
52 }
53
54 /**
55 * {@inheritdoc}
56 */
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]);
60 }
61 }
62
63 /**
64 * {@inheritdoc}
65 */
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]);
69 }
70 }
71
72 /**
73 * {@inheritdoc}
74 */
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]);
78 }
79 }
80
81 /**
82 * {@inheritdoc}
83 */
84 protected function removeFileJailed($destination) {
85 if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
86 throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $destination]);
87 }
88 }
89
90 /**
91 * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
92 *
93 * WARNING: This is untested. It is not currently used, but should do the
94 * trick.
95 */
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') {
101 return TRUE;
102 }
103 return FALSE;
104 }
105 else {
106 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
107 }
108 }
109
110 /**
111 * {@inheritdoc}
112 */
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') {
118 return TRUE;
119 }
120 return FALSE;
121 }
122 else {
123 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
124 }
125 }
126
127 /**
128 * {@inheritdoc}
129 */
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]);
134 }
135 }
136
137 /**
138 * {@inheritdoc}
139 */
140 public function getSettingsForm() {
141 $form = parent::getSettingsForm();
142 $form['advanced']['port']['#default_value'] = 22;
143 return $form;
144 }
145
146 }