ssh.inc

File

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