annotate core/lib/Drupal/Core/FileTransfer/SSH.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\FileTransfer;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * The SSH connection class for the update module.
Chris@0 7 */
Chris@0 8 class SSH extends FileTransfer implements ChmodInterface {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * {@inheritdoc}
Chris@0 12 */
Chris@0 13 public function __construct($jail, $username, $password, $hostname = "localhost", $port = 22) {
Chris@0 14 $this->username = $username;
Chris@0 15 $this->password = $password;
Chris@0 16 $this->hostname = $hostname;
Chris@0 17 $this->port = $port;
Chris@0 18 parent::__construct($jail);
Chris@0 19 }
Chris@0 20
Chris@0 21 /**
Chris@0 22 * {@inheritdoc}
Chris@0 23 */
Chris@0 24 public function connect() {
Chris@0 25 $this->connection = @ssh2_connect($this->hostname, $this->port);
Chris@0 26 if (!$this->connection) {
Chris@0 27 throw new FileTransferException('SSH Connection failed to @host:@port', NULL, ['@host' => $this->hostname, '@port' => $this->port]);
Chris@0 28 }
Chris@0 29 if (!@ssh2_auth_password($this->connection, $this->username, $this->password)) {
Chris@0 30 throw new FileTransferException('The supplied username/password combination was not accepted.');
Chris@0 31 }
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * {@inheritdoc}
Chris@0 36 */
Chris@0 37 public static function factory($jail, $settings) {
Chris@0 38 $username = empty($settings['username']) ? '' : $settings['username'];
Chris@0 39 $password = empty($settings['password']) ? '' : $settings['password'];
Chris@0 40 $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
Chris@0 41 $port = empty($settings['advanced']['port']) ? 22 : $settings['advanced']['port'];
Chris@0 42 return new SSH($jail, $username, $password, $hostname, $port);
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * {@inheritdoc}
Chris@0 47 */
Chris@0 48 protected function copyFileJailed($source, $destination) {
Chris@0 49 if (!@ssh2_scp_send($this->connection, $source, $destination)) {
Chris@0 50 throw new FileTransferException('Cannot copy @source_file to @destination_file.', NULL, ['@source' => $source, '@destination' => $destination]);
Chris@0 51 }
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * {@inheritdoc}
Chris@0 56 */
Chris@0 57 protected function copyDirectoryJailed($source, $destination) {
Chris@0 58 if (@!ssh2_exec($this->connection, 'cp -Rp ' . escapeshellarg($source) . ' ' . escapeshellarg($destination))) {
Chris@0 59 throw new FileTransferException('Cannot copy directory @directory.', NULL, ['@directory' => $source]);
Chris@0 60 }
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * {@inheritdoc}
Chris@0 65 */
Chris@0 66 protected function createDirectoryJailed($directory) {
Chris@0 67 if (@!ssh2_exec($this->connection, 'mkdir ' . escapeshellarg($directory))) {
Chris@0 68 throw new FileTransferException('Cannot create directory @directory.', NULL, ['@directory' => $directory]);
Chris@0 69 }
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 protected function removeDirectoryJailed($directory) {
Chris@0 76 if (@!ssh2_exec($this->connection, 'rm -Rf ' . escapeshellarg($directory))) {
Chris@0 77 throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $directory]);
Chris@0 78 }
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 protected function removeFileJailed($destination) {
Chris@0 85 if (!@ssh2_exec($this->connection, 'rm ' . escapeshellarg($destination))) {
Chris@0 86 throw new FileTransferException('Cannot remove @directory.', NULL, ['@directory' => $destination]);
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Implements Drupal\Core\FileTransfer\FileTransfer::isDirectory().
Chris@0 92 *
Chris@0 93 * WARNING: This is untested. It is not currently used, but should do the
Chris@0 94 * trick.
Chris@0 95 */
Chris@0 96 public function isDirectory($path) {
Chris@0 97 $directory = escapeshellarg($path);
Chris@0 98 $cmd = "[ -d {$directory} ] && echo 'yes'";
Chris@0 99 if ($output = @ssh2_exec($this->connection, $cmd)) {
Chris@0 100 if ($output == 'yes') {
Chris@0 101 return TRUE;
Chris@0 102 }
Chris@0 103 return FALSE;
Chris@0 104 }
Chris@0 105 else {
Chris@0 106 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
Chris@0 107 }
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 */
Chris@0 113 public function isFile($path) {
Chris@0 114 $file = escapeshellarg($path);
Chris@0 115 $cmd = "[ -f {$file} ] && echo 'yes'";
Chris@0 116 if ($output = @ssh2_exec($this->connection, $cmd)) {
Chris@0 117 if ($output == 'yes') {
Chris@0 118 return TRUE;
Chris@0 119 }
Chris@0 120 return FALSE;
Chris@0 121 }
Chris@0 122 else {
Chris@0 123 throw new FileTransferException('Cannot check @path.', NULL, ['@path' => $path]);
Chris@0 124 }
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * {@inheritdoc}
Chris@0 129 */
Chris@0 130 public function chmodJailed($path, $mode, $recursive) {
Chris@0 131 $cmd = sprintf("chmod %s%o %s", $recursive ? '-R ' : '', $mode, escapeshellarg($path));
Chris@0 132 if (@!ssh2_exec($this->connection, $cmd)) {
Chris@0 133 throw new FileTransferException('Cannot change permissions of @path.', NULL, ['@path' => $path]);
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * {@inheritdoc}
Chris@0 139 */
Chris@0 140 public function getSettingsForm() {
Chris@0 141 $form = parent::getSettingsForm();
Chris@0 142 $form['advanced']['port']['#default_value'] = 22;
Chris@0 143 return $form;
Chris@0 144 }
Chris@0 145
Chris@0 146 }