You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SFTPWriteStream.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 SFTPWriteStream 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 $writePosition = 0;
  39. /** @var bool */
  40. private $eof = false;
  41. private $buffer = '';
  42. public static function register($protocol = 'sftpwrite') {
  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_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 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. return true;
  98. }
  99. public function stream_seek($offset, $whence = SEEK_SET) {
  100. return false;
  101. }
  102. public function stream_tell() {
  103. return $this->writePosition;
  104. }
  105. public function stream_read($count) {
  106. return false;
  107. }
  108. public function stream_write($data) {
  109. $written = strlen($data);
  110. $this->writePosition += $written;
  111. $this->buffer .= $data;
  112. if (strlen($this->buffer) > 64 * 1024) {
  113. if (!$this->stream_flush()) {
  114. return false;
  115. }
  116. }
  117. return $written;
  118. }
  119. public function stream_set_option($option, $arg1, $arg2) {
  120. return false;
  121. }
  122. public function stream_truncate($size) {
  123. return false;
  124. }
  125. public function stream_stat() {
  126. return false;
  127. }
  128. public function stream_lock($operation) {
  129. return false;
  130. }
  131. public function stream_flush() {
  132. $size = strlen($this->buffer);
  133. $packet = pack('Na*N3a*', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer);
  134. if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
  135. return false;
  136. }
  137. $this->internalPosition += $size;
  138. $this->buffer = '';
  139. return $this->sftp->_read_put_responses(1);
  140. }
  141. public function stream_eof() {
  142. return $this->eof;
  143. }
  144. public function stream_close() {
  145. $this->stream_flush();
  146. if (!$this->sftp->_close_handle($this->handle)) {
  147. return false;
  148. }
  149. }
  150. }