SSH.php 4.4 KB

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