SFTPReadStream.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_External\Lib\Storage;
  26. use Icewind\Streams\File;
  27. use phpseclib\Net\SSH2;
  28. class SFTPReadStream implements File {
  29. /** @var resource */
  30. public $context;
  31. /** @var \phpseclib\Net\SFTP */
  32. private $sftp;
  33. /** @var string */
  34. private $handle;
  35. /** @var int */
  36. private $internalPosition = 0;
  37. /** @var int */
  38. private $readPosition = 0;
  39. /** @var bool */
  40. private $eof = false;
  41. private $buffer = '';
  42. private bool $pendingRead = false;
  43. private int $size = 0;
  44. public static function register($protocol = 'sftpread') {
  45. if (in_array($protocol, stream_get_wrappers(), true)) {
  46. return false;
  47. }
  48. return stream_wrapper_register($protocol, get_called_class());
  49. }
  50. /**
  51. * Load the source from the stream context and return the context options
  52. *
  53. * @param string $name
  54. * @throws \BadMethodCallException
  55. */
  56. protected function loadContext($name) {
  57. $context = stream_context_get_options($this->context);
  58. if (isset($context[$name])) {
  59. $context = $context[$name];
  60. } else {
  61. throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
  62. }
  63. if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) {
  64. $this->sftp = $context['session'];
  65. } else {
  66. throw new \BadMethodCallException('Invalid context, session not set');
  67. }
  68. if (isset($context['size'])) {
  69. $this->size = $context['size'];
  70. }
  71. return $context;
  72. }
  73. public function stream_open($path, $mode, $options, &$opened_path) {
  74. [, $path] = explode('://', $path);
  75. $path = '/' . ltrim($path);
  76. $path = str_replace('//', '/', $path);
  77. $this->loadContext('sftp');
  78. if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) {
  79. return false;
  80. }
  81. $remote_file = $this->sftp->_realpath($path);
  82. if ($remote_file === false) {
  83. return false;
  84. }
  85. $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
  86. if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
  87. return false;
  88. }
  89. $response = $this->sftp->_get_sftp_packet();
  90. switch ($this->sftp->packet_type) {
  91. case NET_SFTP_HANDLE:
  92. $this->handle = substr($response, 4);
  93. break;
  94. case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
  95. $this->sftp->_logError($response);
  96. return false;
  97. default:
  98. user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
  99. return false;
  100. }
  101. $this->request_chunk(256 * 1024);
  102. return true;
  103. }
  104. public function stream_seek($offset, $whence = SEEK_SET) {
  105. switch ($whence) {
  106. case SEEK_SET:
  107. $this->seekTo($offset);
  108. break;
  109. case SEEK_CUR:
  110. $this->seekTo($this->readPosition + $offset);
  111. break;
  112. case SEEK_END:
  113. $this->seekTo($this->size + $offset);
  114. break;
  115. }
  116. return true;
  117. }
  118. private function seekTo(int $offset): void {
  119. $this->internalPosition = $offset;
  120. $this->readPosition = $offset;
  121. $this->buffer = '';
  122. $this->request_chunk(256 * 1024);
  123. }
  124. public function stream_tell() {
  125. return $this->readPosition;
  126. }
  127. public function stream_read($count) {
  128. if (!$this->eof && strlen($this->buffer) < $count) {
  129. $chunk = $this->read_chunk();
  130. $this->buffer .= $chunk;
  131. if (!$this->eof) {
  132. $this->request_chunk(256 * 1024);
  133. }
  134. }
  135. $data = substr($this->buffer, 0, $count);
  136. $this->buffer = substr($this->buffer, $count);
  137. $this->readPosition += strlen($data);
  138. return $data;
  139. }
  140. private function request_chunk($size) {
  141. if ($this->pendingRead) {
  142. $this->sftp->_get_sftp_packet();
  143. }
  144. $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size);
  145. $this->pendingRead = true;
  146. return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet);
  147. }
  148. private function read_chunk() {
  149. $this->pendingRead = false;
  150. $response = $this->sftp->_get_sftp_packet();
  151. switch ($this->sftp->packet_type) {
  152. case NET_SFTP_DATA:
  153. $temp = substr($response, 4);
  154. $len = strlen($temp);
  155. $this->internalPosition += $len;
  156. return $temp;
  157. case NET_SFTP_STATUS:
  158. [1 => $status] = unpack('N', substr($response, 0, 4));
  159. if ($status == NET_SFTP_STATUS_EOF) {
  160. $this->eof = true;
  161. }
  162. return '';
  163. default:
  164. return '';
  165. }
  166. }
  167. public function stream_write($data) {
  168. return false;
  169. }
  170. public function stream_set_option($option, $arg1, $arg2) {
  171. return false;
  172. }
  173. public function stream_truncate($size) {
  174. return false;
  175. }
  176. public function stream_stat() {
  177. return false;
  178. }
  179. public function stream_lock($operation) {
  180. return false;
  181. }
  182. public function stream_flush() {
  183. return false;
  184. }
  185. public function stream_eof() {
  186. return $this->eof;
  187. }
  188. public function stream_close() {
  189. // we still have a read request incoming that needs to be handled before we can close
  190. if ($this->pendingRead) {
  191. $this->sftp->_get_sftp_packet();
  192. }
  193. if (!$this->sftp->_close_handle($this->handle)) {
  194. return false;
  195. }
  196. return true;
  197. }
  198. }