1
0

SFTPReadStream.php 5.0 KB

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