SFTPReadStream.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 SFTPReadStream 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 $readPosition = 0;
  21. /** @var bool */
  22. private $eof = false;
  23. private $buffer = '';
  24. private bool $pendingRead = false;
  25. private int $size = 0;
  26. public static function register($protocol = 'sftpread') {
  27. if (in_array($protocol, stream_get_wrappers(), true)) {
  28. return false;
  29. }
  30. return stream_wrapper_register($protocol, get_called_class());
  31. }
  32. /**
  33. * Load the source from the stream context and return the context options
  34. *
  35. * @throws \BadMethodCallException
  36. */
  37. protected function loadContext(string $name) {
  38. $context = stream_context_get_options($this->context);
  39. if (isset($context[$name])) {
  40. $context = $context[$name];
  41. } else {
  42. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  43. }
  44. if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) {
  45. $this->sftp = $context['session'];
  46. } else {
  47. throw new \BadMethodCallException('Invalid context, session not set');
  48. }
  49. if (isset($context['size'])) {
  50. $this->size = $context['size'];
  51. }
  52. return $context;
  53. }
  54. public function stream_open($path, $mode, $options, &$opened_path) {
  55. [, $path] = explode('://', $path);
  56. $path = '/' . ltrim($path);
  57. $path = str_replace('//', '/', $path);
  58. $this->loadContext('sftp');
  59. if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) {
  60. return false;
  61. }
  62. $remote_file = $this->sftp->_realpath($path);
  63. if ($remote_file === false) {
  64. return false;
  65. }
  66. $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
  67. if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  68. return false;
  69. }
  70. $response = $this->sftp->_get_sftp_packet();
  71. switch ($this->sftp->packet_type) {
  72. case NET_SFTP_HANDLE:
  73. $this->handle = substr($response, 4);
  74. break;
  75. case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  76. $this->sftp->_logError($response);
  77. return false;
  78. default:
  79. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  80. return false;
  81. }
  82. $this->request_chunk(256 * 1024);
  83. return true;
  84. }
  85. public function stream_seek($offset, $whence = SEEK_SET) {
  86. switch ($whence) {
  87. case SEEK_SET:
  88. $this->seekTo($offset);
  89. break;
  90. case SEEK_CUR:
  91. $this->seekTo($this->readPosition + $offset);
  92. break;
  93. case SEEK_END:
  94. $this->seekTo($this->size + $offset);
  95. break;
  96. }
  97. return true;
  98. }
  99. private function seekTo(int $offset): void {
  100. $this->internalPosition = $offset;
  101. $this->readPosition = $offset;
  102. $this->buffer = '';
  103. $this->request_chunk(256 * 1024);
  104. }
  105. public function stream_tell() {
  106. return $this->readPosition;
  107. }
  108. public function stream_read($count) {
  109. if (!$this->eof && strlen($this->buffer) < $count) {
  110. $chunk = $this->read_chunk();
  111. $this->buffer .= $chunk;
  112. if (!$this->eof) {
  113. $this->request_chunk(256 * 1024);
  114. }
  115. }
  116. $data = substr($this->buffer, 0, $count);
  117. $this->buffer = substr($this->buffer, $count);
  118. $this->readPosition += strlen($data);
  119. return $data;
  120. }
  121. private function request_chunk(int $size) {
  122. if ($this->pendingRead) {
  123. $this->sftp->_get_sftp_packet();
  124. }
  125. $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size);
  126. $this->pendingRead = true;
  127. return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet);
  128. }
  129. private function read_chunk() {
  130. $this->pendingRead = false;
  131. $response = $this->sftp->_get_sftp_packet();
  132. switch ($this->sftp->packet_type) {
  133. case NET_SFTP_DATA:
  134. $temp = substr($response, 4);
  135. $len = strlen($temp);
  136. $this->internalPosition += $len;
  137. return $temp;
  138. case NET_SFTP_STATUS:
  139. [1 => $status] = unpack('N', substr($response, 0, 4));
  140. if ($status == NET_SFTP_STATUS_EOF) {
  141. $this->eof = true;
  142. }
  143. return '';
  144. default:
  145. return '';
  146. }
  147. }
  148. public function stream_write($data) {
  149. return false;
  150. }
  151. public function stream_set_option($option, $arg1, $arg2) {
  152. return false;
  153. }
  154. public function stream_truncate($size) {
  155. return false;
  156. }
  157. public function stream_stat() {
  158. return false;
  159. }
  160. public function stream_lock($operation) {
  161. return false;
  162. }
  163. public function stream_flush() {
  164. return false;
  165. }
  166. public function stream_eof() {
  167. return $this->eof;
  168. }
  169. public function stream_close() {
  170. // we still have a read request incoming that needs to be handled before we can close
  171. if ($this->pendingRead) {
  172. $this->sftp->_get_sftp_packet();
  173. }
  174. if (!$this->sftp->_close_handle($this->handle)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. }