SFTPWriteStream.php 4.5 KB

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