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