SFTPReadStream.php 5.0 KB

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