SFTPReadStream.php 4.9 KB

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