SFTPWriteStream.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @param string $name
  34. * @throws \BadMethodCallException
  35. */
  36. protected function loadContext($name) {
  37. $context = stream_context_get_options($this->context);
  38. if (isset($context[$name])) {
  39. $context = $context[$name];
  40. } else {
  41. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  42. }
  43. if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) {
  44. $this->sftp = $context['session'];
  45. } else {
  46. throw new \BadMethodCallException('Invalid context, session not set');
  47. }
  48. return $context;
  49. }
  50. public function stream_open($path, $mode, $options, &$opened_path) {
  51. [, $path] = explode('://', $path);
  52. $path = '/' . ltrim($path);
  53. $path = str_replace('//', '/', $path);
  54. $this->loadContext('sftp');
  55. if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) {
  56. return false;
  57. }
  58. $remote_file = $this->sftp->_realpath($path);
  59. if ($remote_file === false) {
  60. return false;
  61. }
  62. $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0);
  63. if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  64. return false;
  65. }
  66. $response = $this->sftp->_get_sftp_packet();
  67. switch ($this->sftp->packet_type) {
  68. case NET_SFTP_HANDLE:
  69. $this->handle = substr($response, 4);
  70. break;
  71. case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  72. $this->sftp->_logError($response);
  73. return false;
  74. default:
  75. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  76. return false;
  77. }
  78. return true;
  79. }
  80. public function stream_seek($offset, $whence = SEEK_SET) {
  81. return false;
  82. }
  83. public function stream_tell() {
  84. return $this->writePosition;
  85. }
  86. public function stream_read($count) {
  87. return false;
  88. }
  89. public function stream_write($data) {
  90. $written = strlen($data);
  91. $this->writePosition += $written;
  92. $this->buffer .= $data;
  93. if (strlen($this->buffer) > 64 * 1024) {
  94. if (!$this->stream_flush()) {
  95. return false;
  96. }
  97. }
  98. return $written;
  99. }
  100. public function stream_set_option($option, $arg1, $arg2) {
  101. return false;
  102. }
  103. public function stream_truncate($size) {
  104. return false;
  105. }
  106. public function stream_stat() {
  107. return false;
  108. }
  109. public function stream_lock($operation) {
  110. return false;
  111. }
  112. public function stream_flush() {
  113. $size = strlen($this->buffer);
  114. $packet = pack('Na*N3a*', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer);
  115. if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
  116. return false;
  117. }
  118. $this->internalPosition += $size;
  119. $this->buffer = '';
  120. return $this->sftp->_read_put_responses(1);
  121. }
  122. public function stream_eof() {
  123. return $this->eof;
  124. }
  125. public function stream_close() {
  126. $this->stream_flush();
  127. if (!$this->sftp->_close_handle($this->handle)) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. }