SFTPWriteStream.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_External\Lib\Storage;
  8. use Icewind\Streams\File;
  9. use phpseclib\Net\SSH2;
  10. class SFTPWriteStream implements File {
  11. /** @var resource */
  12. public $context;
  13. /** @var \phpseclib\Net\SFTP */
  14. private $sftp;
  15. /** @var string */
  16. private $handle;
  17. /** @var int */
  18. private $internalPosition = 0;
  19. /** @var int */
  20. private $writePosition = 0;
  21. /** @var bool */
  22. private $eof = false;
  23. private $buffer = '';
  24. public static function register($protocol = 'sftpwrite') {
  25. if (in_array($protocol, stream_get_wrappers(), true)) {
  26. return false;
  27. }
  28. return stream_wrapper_register($protocol, get_called_class());
  29. }
  30. /**
  31. * Load the source from the stream context and return the context options
  32. *
  33. * @throws \BadMethodCallException
  34. */
  35. protected function loadContext(string $name) {
  36. $context = stream_context_get_options($this->context);
  37. if (isset($context[$name])) {
  38. $context = $context[$name];
  39. } else {
  40. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  41. }
  42. if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) {
  43. $this->sftp = $context['session'];
  44. } else {
  45. throw new \BadMethodCallException('Invalid context, session not set');
  46. }
  47. return $context;
  48. }
  49. public function stream_open($path, $mode, $options, &$opened_path) {
  50. [, $path] = explode('://', $path);
  51. $path = '/' . ltrim($path);
  52. $path = str_replace('//', '/', $path);
  53. $this->loadContext('sftp');
  54. if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) {
  55. return false;
  56. }
  57. $remote_file = $this->sftp->_realpath($path);
  58. if ($remote_file === false) {
  59. return false;
  60. }
  61. $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0);
  62. if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  63. return false;
  64. }
  65. $response = $this->sftp->_get_sftp_packet();
  66. switch ($this->sftp->packet_type) {
  67. case NET_SFTP_HANDLE:
  68. $this->handle = substr($response, 4);
  69. break;
  70. case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  71. $this->sftp->_logError($response);
  72. return false;
  73. default:
  74. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  75. return false;
  76. }
  77. return true;
  78. }
  79. public function stream_seek($offset, $whence = SEEK_SET) {
  80. return false;
  81. }
  82. public function stream_tell() {
  83. return $this->writePosition;
  84. }
  85. public function stream_read($count) {
  86. return false;
  87. }
  88. public function stream_write($data) {
  89. $written = strlen($data);
  90. $this->writePosition += $written;
  91. $this->buffer .= $data;
  92. if (strlen($this->buffer) > 64 * 1024) {
  93. if (!$this->stream_flush()) {
  94. return false;
  95. }
  96. }
  97. return $written;
  98. }
  99. public function stream_set_option($option, $arg1, $arg2) {
  100. return false;
  101. }
  102. public function stream_truncate($size) {
  103. return false;
  104. }
  105. public function stream_stat() {
  106. return false;
  107. }
  108. public function stream_lock($operation) {
  109. return false;
  110. }
  111. public function stream_flush() {
  112. $size = strlen($this->buffer);
  113. $packet = pack('Na*N3a*', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer);
  114. if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
  115. return false;
  116. }
  117. $this->internalPosition += $size;
  118. $this->buffer = '';
  119. return $this->sftp->_read_put_responses(1);
  120. }
  121. public function stream_eof() {
  122. return $this->eof;
  123. }
  124. public function stream_close() {
  125. $this->stream_flush();
  126. if (!$this->sftp->_close_handle($this->handle)) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. }