@@ -25,175 +25,175 @@ |
||
25 | 25 | use phpseclib\Net\SSH2; |
26 | 26 | |
27 | 27 | class SFTPReadStream 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 $readPosition = 0; |
|
42 | - |
|
43 | - /** @var bool */ |
|
44 | - private $eof = false; |
|
45 | - |
|
46 | - private $buffer = ''; |
|
47 | - |
|
48 | - static function register($protocol = 'sftpread') { |
|
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_READ, 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 | - $this->request_chunk(256 * 1024); |
|
109 | - |
|
110 | - return true; |
|
111 | - } |
|
112 | - |
|
113 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
114 | - return false; |
|
115 | - } |
|
116 | - |
|
117 | - public function stream_tell() { |
|
118 | - return $this->readPosition; |
|
119 | - } |
|
120 | - |
|
121 | - public function stream_read($count) { |
|
122 | - if (!$this->eof && strlen($this->buffer) < $count) { |
|
123 | - $chunk = $this->read_chunk(); |
|
124 | - $this->buffer .= $chunk; |
|
125 | - if (!$this->eof) { |
|
126 | - $this->request_chunk(256 * 1024); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - $data = substr($this->buffer, 0, $count); |
|
131 | - $this->buffer = substr($this->buffer, $count); |
|
132 | - if ($this->buffer === false) { |
|
133 | - $this->buffer = ''; |
|
134 | - } |
|
135 | - $this->readPosition += strlen($data); |
|
136 | - |
|
137 | - return $data; |
|
138 | - } |
|
139 | - |
|
140 | - private function request_chunk($size) { |
|
141 | - $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size); |
|
142 | - return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet); |
|
143 | - } |
|
144 | - |
|
145 | - private function read_chunk() { |
|
146 | - $response = $this->sftp->_get_sftp_packet(); |
|
147 | - |
|
148 | - switch ($this->sftp->packet_type) { |
|
149 | - case NET_SFTP_DATA: |
|
150 | - $temp = substr($response, 4); |
|
151 | - $len = strlen($temp); |
|
152 | - $this->internalPosition += $len; |
|
153 | - return $temp; |
|
154 | - case NET_SFTP_STATUS: |
|
155 | - [1 => $status] = unpack('N', substr($response, 0, 4)); |
|
156 | - if ($status == NET_SFTP_STATUS_EOF) { |
|
157 | - $this->eof = true; |
|
158 | - } |
|
159 | - return ''; |
|
160 | - default: |
|
161 | - return ''; |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - public function stream_write($data) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - public function stream_set_option($option, $arg1, $arg2) { |
|
170 | - return false; |
|
171 | - } |
|
172 | - |
|
173 | - public function stream_truncate($size) { |
|
174 | - return false; |
|
175 | - } |
|
176 | - |
|
177 | - public function stream_stat() { |
|
178 | - return false; |
|
179 | - } |
|
180 | - |
|
181 | - public function stream_lock($operation) { |
|
182 | - return false; |
|
183 | - } |
|
184 | - |
|
185 | - public function stream_flush() { |
|
186 | - return false; |
|
187 | - } |
|
188 | - |
|
189 | - public function stream_eof() { |
|
190 | - return $this->eof; |
|
191 | - } |
|
192 | - |
|
193 | - public function stream_close() { |
|
194 | - if (!$this->sftp->_close_handle($this->handle)) { |
|
195 | - return false; |
|
196 | - } |
|
197 | - } |
|
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 $readPosition = 0; |
|
42 | + |
|
43 | + /** @var bool */ |
|
44 | + private $eof = false; |
|
45 | + |
|
46 | + private $buffer = ''; |
|
47 | + |
|
48 | + static function register($protocol = 'sftpread') { |
|
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_READ, 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 | + $this->request_chunk(256 * 1024); |
|
109 | + |
|
110 | + return true; |
|
111 | + } |
|
112 | + |
|
113 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
114 | + return false; |
|
115 | + } |
|
116 | + |
|
117 | + public function stream_tell() { |
|
118 | + return $this->readPosition; |
|
119 | + } |
|
120 | + |
|
121 | + public function stream_read($count) { |
|
122 | + if (!$this->eof && strlen($this->buffer) < $count) { |
|
123 | + $chunk = $this->read_chunk(); |
|
124 | + $this->buffer .= $chunk; |
|
125 | + if (!$this->eof) { |
|
126 | + $this->request_chunk(256 * 1024); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + $data = substr($this->buffer, 0, $count); |
|
131 | + $this->buffer = substr($this->buffer, $count); |
|
132 | + if ($this->buffer === false) { |
|
133 | + $this->buffer = ''; |
|
134 | + } |
|
135 | + $this->readPosition += strlen($data); |
|
136 | + |
|
137 | + return $data; |
|
138 | + } |
|
139 | + |
|
140 | + private function request_chunk($size) { |
|
141 | + $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size); |
|
142 | + return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet); |
|
143 | + } |
|
144 | + |
|
145 | + private function read_chunk() { |
|
146 | + $response = $this->sftp->_get_sftp_packet(); |
|
147 | + |
|
148 | + switch ($this->sftp->packet_type) { |
|
149 | + case NET_SFTP_DATA: |
|
150 | + $temp = substr($response, 4); |
|
151 | + $len = strlen($temp); |
|
152 | + $this->internalPosition += $len; |
|
153 | + return $temp; |
|
154 | + case NET_SFTP_STATUS: |
|
155 | + [1 => $status] = unpack('N', substr($response, 0, 4)); |
|
156 | + if ($status == NET_SFTP_STATUS_EOF) { |
|
157 | + $this->eof = true; |
|
158 | + } |
|
159 | + return ''; |
|
160 | + default: |
|
161 | + return ''; |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + public function stream_write($data) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + public function stream_set_option($option, $arg1, $arg2) { |
|
170 | + return false; |
|
171 | + } |
|
172 | + |
|
173 | + public function stream_truncate($size) { |
|
174 | + return false; |
|
175 | + } |
|
176 | + |
|
177 | + public function stream_stat() { |
|
178 | + return false; |
|
179 | + } |
|
180 | + |
|
181 | + public function stream_lock($operation) { |
|
182 | + return false; |
|
183 | + } |
|
184 | + |
|
185 | + public function stream_flush() { |
|
186 | + return false; |
|
187 | + } |
|
188 | + |
|
189 | + public function stream_eof() { |
|
190 | + return $this->eof; |
|
191 | + } |
|
192 | + |
|
193 | + public function stream_close() { |
|
194 | + if (!$this->sftp->_close_handle($this->handle)) { |
|
195 | + return false; |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | 199 | } |
@@ -64,7 +64,7 @@ |
||
64 | 64 | if (isset($context[$name])) { |
65 | 65 | $context = $context[$name]; |
66 | 66 | } else { |
67 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
67 | + throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set'); |
|
68 | 68 | } |
69 | 69 | if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
70 | 70 | $this->sftp = $context['session']; |
@@ -25,154 +25,154 @@ |
||
25 | 25 | use phpseclib\Net\SSH2; |
26 | 26 | |
27 | 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 | - } |
|
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 | 176 | |
177 | 177 | } |
178 | 178 |
@@ -64,7 +64,7 @@ |
||
64 | 64 | if (isset($context[$name])) { |
65 | 65 | $context = $context[$name]; |
66 | 66 | } else { |
67 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
67 | + throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set'); |
|
68 | 68 | } |
69 | 69 | if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
70 | 70 | $this->sftp = $context['session']; |
@@ -44,441 +44,441 @@ |
||
44 | 44 | * provide access to SFTP servers. |
45 | 45 | */ |
46 | 46 | class SFTP extends \OC\Files\Storage\Common { |
47 | - private $host; |
|
48 | - private $user; |
|
49 | - private $root; |
|
50 | - private $port = 22; |
|
51 | - |
|
52 | - private $auth = []; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var \phpseclib\Net\SFTP |
|
56 | - */ |
|
57 | - protected $client; |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $host protocol://server:port |
|
61 | - * @return array [$server, $port] |
|
62 | - */ |
|
63 | - private function splitHost($host) { |
|
64 | - $input = $host; |
|
65 | - if (strpos($host, '://') === false) { |
|
66 | - // add a protocol to fix parse_url behavior with ipv6 |
|
67 | - $host = 'http://' . $host; |
|
68 | - } |
|
69 | - |
|
70 | - $parsed = parse_url($host); |
|
71 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
72 | - return [$parsed['host'], $parsed['port']]; |
|
73 | - } else if (is_array($parsed)) { |
|
74 | - return [$parsed['host'], 22]; |
|
75 | - } else { |
|
76 | - return [$input, 22]; |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritdoc} |
|
82 | - */ |
|
83 | - public function __construct($params) { |
|
84 | - // Register sftp:// |
|
85 | - Stream::register(); |
|
86 | - |
|
87 | - $parsedHost = $this->splitHost($params['host']); |
|
88 | - |
|
89 | - $this->host = $parsedHost[0]; |
|
90 | - $this->port = $parsedHost[1]; |
|
91 | - |
|
92 | - if (!isset($params['user'])) { |
|
93 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
94 | - } |
|
95 | - $this->user = $params['user']; |
|
96 | - |
|
97 | - if (isset($params['public_key_auth'])) { |
|
98 | - $this->auth[] = $params['public_key_auth']; |
|
99 | - } |
|
100 | - if (isset($params['password']) && $params['password'] !== '') { |
|
101 | - $this->auth[] = $params['password']; |
|
102 | - } |
|
103 | - |
|
104 | - if ($this->auth === []) { |
|
105 | - throw new \UnexpectedValueException('no authentication parameters specified'); |
|
106 | - } |
|
107 | - |
|
108 | - $this->root |
|
109 | - = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
110 | - |
|
111 | - $this->root = '/' . ltrim($this->root, '/'); |
|
112 | - $this->root = rtrim($this->root, '/') . '/'; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the connection. |
|
117 | - * |
|
118 | - * @return \phpseclib\Net\SFTP connected client instance |
|
119 | - * @throws \Exception when the connection failed |
|
120 | - */ |
|
121 | - public function getConnection() { |
|
122 | - if (!is_null($this->client)) { |
|
123 | - return $this->client; |
|
124 | - } |
|
125 | - |
|
126 | - $hostKeys = $this->readHostKeys(); |
|
127 | - $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
128 | - |
|
129 | - // The SSH Host Key MUST be verified before login(). |
|
130 | - $currentHostKey = $this->client->getServerPublicHostKey(); |
|
131 | - if (array_key_exists($this->host, $hostKeys)) { |
|
132 | - if ($hostKeys[$this->host] !== $currentHostKey) { |
|
133 | - throw new \Exception('Host public key does not match known key'); |
|
134 | - } |
|
135 | - } else { |
|
136 | - $hostKeys[$this->host] = $currentHostKey; |
|
137 | - $this->writeHostKeys($hostKeys); |
|
138 | - } |
|
139 | - |
|
140 | - $login = false; |
|
141 | - foreach ($this->auth as $auth) { |
|
142 | - $login = $this->client->login($this->user, $auth); |
|
143 | - if ($login === true) { |
|
144 | - break; |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - if ($login === false) { |
|
149 | - throw new \Exception('Login failed'); |
|
150 | - } |
|
151 | - return $this->client; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * {@inheritdoc} |
|
156 | - */ |
|
157 | - public function test() { |
|
158 | - if ( |
|
159 | - !isset($this->host) |
|
160 | - || !isset($this->user) |
|
161 | - ) { |
|
162 | - return false; |
|
163 | - } |
|
164 | - return $this->getConnection()->nlist() !== false; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * {@inheritdoc} |
|
169 | - */ |
|
170 | - public function getId(){ |
|
171 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
172 | - if ($this->port !== 22) { |
|
173 | - $id .= ':' . $this->port; |
|
174 | - } |
|
175 | - // note: this will double the root slash, |
|
176 | - // we should not change it to keep compatible with |
|
177 | - // old storage ids |
|
178 | - $id .= '/' . $this->root; |
|
179 | - return $id; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @return string |
|
184 | - */ |
|
185 | - public function getHost() { |
|
186 | - return $this->host; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function getRoot() { |
|
193 | - return $this->root; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function getUser() { |
|
200 | - return $this->user; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $path |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - private function absPath($path) { |
|
208 | - return $this->root . $this->cleanPath($path); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @return string|false |
|
213 | - */ |
|
214 | - private function hostKeysPath() { |
|
215 | - try { |
|
216 | - $storage_view = \OCP\Files::getStorage('files_external'); |
|
217 | - if ($storage_view) { |
|
218 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
219 | - $storage_view->getAbsolutePath('') . |
|
220 | - 'ssh_hostKeys'; |
|
221 | - } |
|
222 | - } catch (\Exception $e) { |
|
223 | - } |
|
224 | - return false; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param $keys |
|
229 | - * @return bool |
|
230 | - */ |
|
231 | - protected function writeHostKeys($keys) { |
|
232 | - try { |
|
233 | - $keyPath = $this->hostKeysPath(); |
|
234 | - if ($keyPath && file_exists($keyPath)) { |
|
235 | - $fp = fopen($keyPath, 'w'); |
|
236 | - foreach ($keys as $host => $key) { |
|
237 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
238 | - } |
|
239 | - fclose($fp); |
|
240 | - return true; |
|
241 | - } |
|
242 | - } catch (\Exception $e) { |
|
243 | - } |
|
244 | - return false; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function readHostKeys() { |
|
251 | - try { |
|
252 | - $keyPath = $this->hostKeysPath(); |
|
253 | - if (file_exists($keyPath)) { |
|
254 | - $hosts = array(); |
|
255 | - $keys = array(); |
|
256 | - $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
257 | - if ($lines) { |
|
258 | - foreach ($lines as $line) { |
|
259 | - $hostKeyArray = explode("::", $line, 2); |
|
260 | - if (count($hostKeyArray) === 2) { |
|
261 | - $hosts[] = $hostKeyArray[0]; |
|
262 | - $keys[] = $hostKeyArray[1]; |
|
263 | - } |
|
264 | - } |
|
265 | - return array_combine($hosts, $keys); |
|
266 | - } |
|
267 | - } |
|
268 | - } catch (\Exception $e) { |
|
269 | - } |
|
270 | - return array(); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * {@inheritdoc} |
|
275 | - */ |
|
276 | - public function mkdir($path) { |
|
277 | - try { |
|
278 | - return $this->getConnection()->mkdir($this->absPath($path)); |
|
279 | - } catch (\Exception $e) { |
|
280 | - return false; |
|
281 | - } |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * {@inheritdoc} |
|
286 | - */ |
|
287 | - public function rmdir($path) { |
|
288 | - try { |
|
289 | - $result = $this->getConnection()->delete($this->absPath($path), true); |
|
290 | - // workaround: stray stat cache entry when deleting empty folders |
|
291 | - // see https://github.com/phpseclib/phpseclib/issues/706 |
|
292 | - $this->getConnection()->clearStatCache(); |
|
293 | - return $result; |
|
294 | - } catch (\Exception $e) { |
|
295 | - return false; |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * {@inheritdoc} |
|
301 | - */ |
|
302 | - public function opendir($path) { |
|
303 | - try { |
|
304 | - $list = $this->getConnection()->nlist($this->absPath($path)); |
|
305 | - if ($list === false) { |
|
306 | - return false; |
|
307 | - } |
|
308 | - |
|
309 | - $id = md5('sftp:' . $path); |
|
310 | - $dirStream = array(); |
|
311 | - foreach($list as $file) { |
|
312 | - if ($file !== '.' && $file !== '..') { |
|
313 | - $dirStream[] = $file; |
|
314 | - } |
|
315 | - } |
|
316 | - return IteratorDirectory::wrap($dirStream); |
|
317 | - } catch(\Exception $e) { |
|
318 | - return false; |
|
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * {@inheritdoc} |
|
324 | - */ |
|
325 | - public function filetype($path) { |
|
326 | - try { |
|
327 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
328 | - if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) { |
|
329 | - return 'file'; |
|
330 | - } |
|
331 | - |
|
332 | - if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) { |
|
333 | - return 'dir'; |
|
334 | - } |
|
335 | - } catch (\Exception $e) { |
|
336 | - |
|
337 | - } |
|
338 | - return false; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * {@inheritdoc} |
|
343 | - */ |
|
344 | - public function file_exists($path) { |
|
345 | - try { |
|
346 | - return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
347 | - } catch (\Exception $e) { |
|
348 | - return false; |
|
349 | - } |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * {@inheritdoc} |
|
354 | - */ |
|
355 | - public function unlink($path) { |
|
356 | - try { |
|
357 | - return $this->getConnection()->delete($this->absPath($path), true); |
|
358 | - } catch (\Exception $e) { |
|
359 | - return false; |
|
360 | - } |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * {@inheritdoc} |
|
365 | - */ |
|
366 | - public function fopen($path, $mode) { |
|
367 | - try { |
|
368 | - $absPath = $this->absPath($path); |
|
369 | - switch($mode) { |
|
370 | - case 'r': |
|
371 | - case 'rb': |
|
372 | - if ( !$this->file_exists($path)) { |
|
373 | - return false; |
|
374 | - } |
|
375 | - SFTPReadStream::register(); |
|
376 | - $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
|
377 | - $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context); |
|
378 | - return RetryWrapper::wrap($handle); |
|
379 | - case 'w': |
|
380 | - case 'wb': |
|
381 | - SFTPWriteStream::register(); |
|
382 | - $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
|
383 | - return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context); |
|
384 | - case 'a': |
|
385 | - case 'ab': |
|
386 | - case 'r+': |
|
387 | - case 'w+': |
|
388 | - case 'wb+': |
|
389 | - case 'a+': |
|
390 | - case 'x': |
|
391 | - case 'x+': |
|
392 | - case 'c': |
|
393 | - case 'c+': |
|
394 | - $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
395 | - $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
396 | - return RetryWrapper::wrap($handle); |
|
397 | - } |
|
398 | - } catch (\Exception $e) { |
|
399 | - } |
|
400 | - return false; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * {@inheritdoc} |
|
405 | - */ |
|
406 | - public function touch($path, $mtime=null) { |
|
407 | - try { |
|
408 | - if (!is_null($mtime)) { |
|
409 | - return false; |
|
410 | - } |
|
411 | - if (!$this->file_exists($path)) { |
|
412 | - $this->getConnection()->put($this->absPath($path), ''); |
|
413 | - } else { |
|
414 | - return false; |
|
415 | - } |
|
416 | - } catch (\Exception $e) { |
|
417 | - return false; |
|
418 | - } |
|
419 | - return true; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * @param string $path |
|
424 | - * @param string $target |
|
425 | - * @throws \Exception |
|
426 | - */ |
|
427 | - public function getFile($path, $target) { |
|
428 | - $this->getConnection()->get($path, $target); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * @param string $path |
|
433 | - * @param string $target |
|
434 | - * @throws \Exception |
|
435 | - */ |
|
436 | - public function uploadFile($path, $target) { |
|
437 | - $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * {@inheritdoc} |
|
442 | - */ |
|
443 | - public function rename($source, $target) { |
|
444 | - try { |
|
445 | - if ($this->file_exists($target)) { |
|
446 | - $this->unlink($target); |
|
447 | - } |
|
448 | - return $this->getConnection()->rename( |
|
449 | - $this->absPath($source), |
|
450 | - $this->absPath($target) |
|
451 | - ); |
|
452 | - } catch (\Exception $e) { |
|
453 | - return false; |
|
454 | - } |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * {@inheritdoc} |
|
459 | - */ |
|
460 | - public function stat($path) { |
|
461 | - try { |
|
462 | - $stat = $this->getConnection()->stat($this->absPath($path)); |
|
463 | - |
|
464 | - $mtime = $stat ? $stat['mtime'] : -1; |
|
465 | - $size = $stat ? $stat['size'] : 0; |
|
466 | - |
|
467 | - return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
468 | - } catch (\Exception $e) { |
|
469 | - return false; |
|
470 | - } |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * @param string $path |
|
475 | - * @return string |
|
476 | - */ |
|
477 | - public function constructUrl($path) { |
|
478 | - // Do not pass the password here. We want to use the Net_SFTP object |
|
479 | - // supplied via stream context or fail. We only supply username and |
|
480 | - // hostname because this might show up in logs (they are not used). |
|
481 | - $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
482 | - return $url; |
|
483 | - } |
|
47 | + private $host; |
|
48 | + private $user; |
|
49 | + private $root; |
|
50 | + private $port = 22; |
|
51 | + |
|
52 | + private $auth = []; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var \phpseclib\Net\SFTP |
|
56 | + */ |
|
57 | + protected $client; |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $host protocol://server:port |
|
61 | + * @return array [$server, $port] |
|
62 | + */ |
|
63 | + private function splitHost($host) { |
|
64 | + $input = $host; |
|
65 | + if (strpos($host, '://') === false) { |
|
66 | + // add a protocol to fix parse_url behavior with ipv6 |
|
67 | + $host = 'http://' . $host; |
|
68 | + } |
|
69 | + |
|
70 | + $parsed = parse_url($host); |
|
71 | + if(is_array($parsed) && isset($parsed['port'])) { |
|
72 | + return [$parsed['host'], $parsed['port']]; |
|
73 | + } else if (is_array($parsed)) { |
|
74 | + return [$parsed['host'], 22]; |
|
75 | + } else { |
|
76 | + return [$input, 22]; |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritdoc} |
|
82 | + */ |
|
83 | + public function __construct($params) { |
|
84 | + // Register sftp:// |
|
85 | + Stream::register(); |
|
86 | + |
|
87 | + $parsedHost = $this->splitHost($params['host']); |
|
88 | + |
|
89 | + $this->host = $parsedHost[0]; |
|
90 | + $this->port = $parsedHost[1]; |
|
91 | + |
|
92 | + if (!isset($params['user'])) { |
|
93 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
94 | + } |
|
95 | + $this->user = $params['user']; |
|
96 | + |
|
97 | + if (isset($params['public_key_auth'])) { |
|
98 | + $this->auth[] = $params['public_key_auth']; |
|
99 | + } |
|
100 | + if (isset($params['password']) && $params['password'] !== '') { |
|
101 | + $this->auth[] = $params['password']; |
|
102 | + } |
|
103 | + |
|
104 | + if ($this->auth === []) { |
|
105 | + throw new \UnexpectedValueException('no authentication parameters specified'); |
|
106 | + } |
|
107 | + |
|
108 | + $this->root |
|
109 | + = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
|
110 | + |
|
111 | + $this->root = '/' . ltrim($this->root, '/'); |
|
112 | + $this->root = rtrim($this->root, '/') . '/'; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the connection. |
|
117 | + * |
|
118 | + * @return \phpseclib\Net\SFTP connected client instance |
|
119 | + * @throws \Exception when the connection failed |
|
120 | + */ |
|
121 | + public function getConnection() { |
|
122 | + if (!is_null($this->client)) { |
|
123 | + return $this->client; |
|
124 | + } |
|
125 | + |
|
126 | + $hostKeys = $this->readHostKeys(); |
|
127 | + $this->client = new \phpseclib\Net\SFTP($this->host, $this->port); |
|
128 | + |
|
129 | + // The SSH Host Key MUST be verified before login(). |
|
130 | + $currentHostKey = $this->client->getServerPublicHostKey(); |
|
131 | + if (array_key_exists($this->host, $hostKeys)) { |
|
132 | + if ($hostKeys[$this->host] !== $currentHostKey) { |
|
133 | + throw new \Exception('Host public key does not match known key'); |
|
134 | + } |
|
135 | + } else { |
|
136 | + $hostKeys[$this->host] = $currentHostKey; |
|
137 | + $this->writeHostKeys($hostKeys); |
|
138 | + } |
|
139 | + |
|
140 | + $login = false; |
|
141 | + foreach ($this->auth as $auth) { |
|
142 | + $login = $this->client->login($this->user, $auth); |
|
143 | + if ($login === true) { |
|
144 | + break; |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + if ($login === false) { |
|
149 | + throw new \Exception('Login failed'); |
|
150 | + } |
|
151 | + return $this->client; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * {@inheritdoc} |
|
156 | + */ |
|
157 | + public function test() { |
|
158 | + if ( |
|
159 | + !isset($this->host) |
|
160 | + || !isset($this->user) |
|
161 | + ) { |
|
162 | + return false; |
|
163 | + } |
|
164 | + return $this->getConnection()->nlist() !== false; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * {@inheritdoc} |
|
169 | + */ |
|
170 | + public function getId(){ |
|
171 | + $id = 'sftp::' . $this->user . '@' . $this->host; |
|
172 | + if ($this->port !== 22) { |
|
173 | + $id .= ':' . $this->port; |
|
174 | + } |
|
175 | + // note: this will double the root slash, |
|
176 | + // we should not change it to keep compatible with |
|
177 | + // old storage ids |
|
178 | + $id .= '/' . $this->root; |
|
179 | + return $id; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @return string |
|
184 | + */ |
|
185 | + public function getHost() { |
|
186 | + return $this->host; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function getRoot() { |
|
193 | + return $this->root; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function getUser() { |
|
200 | + return $this->user; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $path |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + private function absPath($path) { |
|
208 | + return $this->root . $this->cleanPath($path); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @return string|false |
|
213 | + */ |
|
214 | + private function hostKeysPath() { |
|
215 | + try { |
|
216 | + $storage_view = \OCP\Files::getStorage('files_external'); |
|
217 | + if ($storage_view) { |
|
218 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
219 | + $storage_view->getAbsolutePath('') . |
|
220 | + 'ssh_hostKeys'; |
|
221 | + } |
|
222 | + } catch (\Exception $e) { |
|
223 | + } |
|
224 | + return false; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param $keys |
|
229 | + * @return bool |
|
230 | + */ |
|
231 | + protected function writeHostKeys($keys) { |
|
232 | + try { |
|
233 | + $keyPath = $this->hostKeysPath(); |
|
234 | + if ($keyPath && file_exists($keyPath)) { |
|
235 | + $fp = fopen($keyPath, 'w'); |
|
236 | + foreach ($keys as $host => $key) { |
|
237 | + fwrite($fp, $host . '::' . $key . "\n"); |
|
238 | + } |
|
239 | + fclose($fp); |
|
240 | + return true; |
|
241 | + } |
|
242 | + } catch (\Exception $e) { |
|
243 | + } |
|
244 | + return false; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function readHostKeys() { |
|
251 | + try { |
|
252 | + $keyPath = $this->hostKeysPath(); |
|
253 | + if (file_exists($keyPath)) { |
|
254 | + $hosts = array(); |
|
255 | + $keys = array(); |
|
256 | + $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
257 | + if ($lines) { |
|
258 | + foreach ($lines as $line) { |
|
259 | + $hostKeyArray = explode("::", $line, 2); |
|
260 | + if (count($hostKeyArray) === 2) { |
|
261 | + $hosts[] = $hostKeyArray[0]; |
|
262 | + $keys[] = $hostKeyArray[1]; |
|
263 | + } |
|
264 | + } |
|
265 | + return array_combine($hosts, $keys); |
|
266 | + } |
|
267 | + } |
|
268 | + } catch (\Exception $e) { |
|
269 | + } |
|
270 | + return array(); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * {@inheritdoc} |
|
275 | + */ |
|
276 | + public function mkdir($path) { |
|
277 | + try { |
|
278 | + return $this->getConnection()->mkdir($this->absPath($path)); |
|
279 | + } catch (\Exception $e) { |
|
280 | + return false; |
|
281 | + } |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * {@inheritdoc} |
|
286 | + */ |
|
287 | + public function rmdir($path) { |
|
288 | + try { |
|
289 | + $result = $this->getConnection()->delete($this->absPath($path), true); |
|
290 | + // workaround: stray stat cache entry when deleting empty folders |
|
291 | + // see https://github.com/phpseclib/phpseclib/issues/706 |
|
292 | + $this->getConnection()->clearStatCache(); |
|
293 | + return $result; |
|
294 | + } catch (\Exception $e) { |
|
295 | + return false; |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * {@inheritdoc} |
|
301 | + */ |
|
302 | + public function opendir($path) { |
|
303 | + try { |
|
304 | + $list = $this->getConnection()->nlist($this->absPath($path)); |
|
305 | + if ($list === false) { |
|
306 | + return false; |
|
307 | + } |
|
308 | + |
|
309 | + $id = md5('sftp:' . $path); |
|
310 | + $dirStream = array(); |
|
311 | + foreach($list as $file) { |
|
312 | + if ($file !== '.' && $file !== '..') { |
|
313 | + $dirStream[] = $file; |
|
314 | + } |
|
315 | + } |
|
316 | + return IteratorDirectory::wrap($dirStream); |
|
317 | + } catch(\Exception $e) { |
|
318 | + return false; |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * {@inheritdoc} |
|
324 | + */ |
|
325 | + public function filetype($path) { |
|
326 | + try { |
|
327 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
328 | + if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) { |
|
329 | + return 'file'; |
|
330 | + } |
|
331 | + |
|
332 | + if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) { |
|
333 | + return 'dir'; |
|
334 | + } |
|
335 | + } catch (\Exception $e) { |
|
336 | + |
|
337 | + } |
|
338 | + return false; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * {@inheritdoc} |
|
343 | + */ |
|
344 | + public function file_exists($path) { |
|
345 | + try { |
|
346 | + return $this->getConnection()->stat($this->absPath($path)) !== false; |
|
347 | + } catch (\Exception $e) { |
|
348 | + return false; |
|
349 | + } |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * {@inheritdoc} |
|
354 | + */ |
|
355 | + public function unlink($path) { |
|
356 | + try { |
|
357 | + return $this->getConnection()->delete($this->absPath($path), true); |
|
358 | + } catch (\Exception $e) { |
|
359 | + return false; |
|
360 | + } |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * {@inheritdoc} |
|
365 | + */ |
|
366 | + public function fopen($path, $mode) { |
|
367 | + try { |
|
368 | + $absPath = $this->absPath($path); |
|
369 | + switch($mode) { |
|
370 | + case 'r': |
|
371 | + case 'rb': |
|
372 | + if ( !$this->file_exists($path)) { |
|
373 | + return false; |
|
374 | + } |
|
375 | + SFTPReadStream::register(); |
|
376 | + $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
|
377 | + $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context); |
|
378 | + return RetryWrapper::wrap($handle); |
|
379 | + case 'w': |
|
380 | + case 'wb': |
|
381 | + SFTPWriteStream::register(); |
|
382 | + $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
|
383 | + return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context); |
|
384 | + case 'a': |
|
385 | + case 'ab': |
|
386 | + case 'r+': |
|
387 | + case 'w+': |
|
388 | + case 'wb+': |
|
389 | + case 'a+': |
|
390 | + case 'x': |
|
391 | + case 'x+': |
|
392 | + case 'c': |
|
393 | + case 'c+': |
|
394 | + $context = stream_context_create(array('sftp' => array('session' => $this->getConnection()))); |
|
395 | + $handle = fopen($this->constructUrl($path), $mode, false, $context); |
|
396 | + return RetryWrapper::wrap($handle); |
|
397 | + } |
|
398 | + } catch (\Exception $e) { |
|
399 | + } |
|
400 | + return false; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * {@inheritdoc} |
|
405 | + */ |
|
406 | + public function touch($path, $mtime=null) { |
|
407 | + try { |
|
408 | + if (!is_null($mtime)) { |
|
409 | + return false; |
|
410 | + } |
|
411 | + if (!$this->file_exists($path)) { |
|
412 | + $this->getConnection()->put($this->absPath($path), ''); |
|
413 | + } else { |
|
414 | + return false; |
|
415 | + } |
|
416 | + } catch (\Exception $e) { |
|
417 | + return false; |
|
418 | + } |
|
419 | + return true; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * @param string $path |
|
424 | + * @param string $target |
|
425 | + * @throws \Exception |
|
426 | + */ |
|
427 | + public function getFile($path, $target) { |
|
428 | + $this->getConnection()->get($path, $target); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * @param string $path |
|
433 | + * @param string $target |
|
434 | + * @throws \Exception |
|
435 | + */ |
|
436 | + public function uploadFile($path, $target) { |
|
437 | + $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE); |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * {@inheritdoc} |
|
442 | + */ |
|
443 | + public function rename($source, $target) { |
|
444 | + try { |
|
445 | + if ($this->file_exists($target)) { |
|
446 | + $this->unlink($target); |
|
447 | + } |
|
448 | + return $this->getConnection()->rename( |
|
449 | + $this->absPath($source), |
|
450 | + $this->absPath($target) |
|
451 | + ); |
|
452 | + } catch (\Exception $e) { |
|
453 | + return false; |
|
454 | + } |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * {@inheritdoc} |
|
459 | + */ |
|
460 | + public function stat($path) { |
|
461 | + try { |
|
462 | + $stat = $this->getConnection()->stat($this->absPath($path)); |
|
463 | + |
|
464 | + $mtime = $stat ? $stat['mtime'] : -1; |
|
465 | + $size = $stat ? $stat['size'] : 0; |
|
466 | + |
|
467 | + return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1); |
|
468 | + } catch (\Exception $e) { |
|
469 | + return false; |
|
470 | + } |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * @param string $path |
|
475 | + * @return string |
|
476 | + */ |
|
477 | + public function constructUrl($path) { |
|
478 | + // Do not pass the password here. We want to use the Net_SFTP object |
|
479 | + // supplied via stream context or fail. We only supply username and |
|
480 | + // hostname because this might show up in logs (they are not used). |
|
481 | + $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
482 | + return $url; |
|
483 | + } |
|
484 | 484 | } |
@@ -64,11 +64,11 @@ discard block |
||
64 | 64 | $input = $host; |
65 | 65 | if (strpos($host, '://') === false) { |
66 | 66 | // add a protocol to fix parse_url behavior with ipv6 |
67 | - $host = 'http://' . $host; |
|
67 | + $host = 'http://'.$host; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | $parsed = parse_url($host); |
71 | - if(is_array($parsed) && isset($parsed['port'])) { |
|
71 | + if (is_array($parsed) && isset($parsed['port'])) { |
|
72 | 72 | return [$parsed['host'], $parsed['port']]; |
73 | 73 | } else if (is_array($parsed)) { |
74 | 74 | return [$parsed['host'], 22]; |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | // Register sftp:// |
85 | 85 | Stream::register(); |
86 | 86 | |
87 | - $parsedHost = $this->splitHost($params['host']); |
|
87 | + $parsedHost = $this->splitHost($params['host']); |
|
88 | 88 | |
89 | 89 | $this->host = $parsedHost[0]; |
90 | 90 | $this->port = $parsedHost[1]; |
@@ -108,8 +108,8 @@ discard block |
||
108 | 108 | $this->root |
109 | 109 | = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; |
110 | 110 | |
111 | - $this->root = '/' . ltrim($this->root, '/'); |
|
112 | - $this->root = rtrim($this->root, '/') . '/'; |
|
111 | + $this->root = '/'.ltrim($this->root, '/'); |
|
112 | + $this->root = rtrim($this->root, '/').'/'; |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -167,15 +167,15 @@ discard block |
||
167 | 167 | /** |
168 | 168 | * {@inheritdoc} |
169 | 169 | */ |
170 | - public function getId(){ |
|
171 | - $id = 'sftp::' . $this->user . '@' . $this->host; |
|
170 | + public function getId() { |
|
171 | + $id = 'sftp::'.$this->user.'@'.$this->host; |
|
172 | 172 | if ($this->port !== 22) { |
173 | - $id .= ':' . $this->port; |
|
173 | + $id .= ':'.$this->port; |
|
174 | 174 | } |
175 | 175 | // note: this will double the root slash, |
176 | 176 | // we should not change it to keep compatible with |
177 | 177 | // old storage ids |
178 | - $id .= '/' . $this->root; |
|
178 | + $id .= '/'.$this->root; |
|
179 | 179 | return $id; |
180 | 180 | } |
181 | 181 | |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | * @return string |
206 | 206 | */ |
207 | 207 | private function absPath($path) { |
208 | - return $this->root . $this->cleanPath($path); |
|
208 | + return $this->root.$this->cleanPath($path); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
@@ -215,8 +215,8 @@ discard block |
||
215 | 215 | try { |
216 | 216 | $storage_view = \OCP\Files::getStorage('files_external'); |
217 | 217 | if ($storage_view) { |
218 | - return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . |
|
219 | - $storage_view->getAbsolutePath('') . |
|
218 | + return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data'). |
|
219 | + $storage_view->getAbsolutePath(''). |
|
220 | 220 | 'ssh_hostKeys'; |
221 | 221 | } |
222 | 222 | } catch (\Exception $e) { |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | if ($keyPath && file_exists($keyPath)) { |
235 | 235 | $fp = fopen($keyPath, 'w'); |
236 | 236 | foreach ($keys as $host => $key) { |
237 | - fwrite($fp, $host . '::' . $key . "\n"); |
|
237 | + fwrite($fp, $host.'::'.$key."\n"); |
|
238 | 238 | } |
239 | 239 | fclose($fp); |
240 | 240 | return true; |
@@ -306,15 +306,15 @@ discard block |
||
306 | 306 | return false; |
307 | 307 | } |
308 | 308 | |
309 | - $id = md5('sftp:' . $path); |
|
309 | + $id = md5('sftp:'.$path); |
|
310 | 310 | $dirStream = array(); |
311 | - foreach($list as $file) { |
|
311 | + foreach ($list as $file) { |
|
312 | 312 | if ($file !== '.' && $file !== '..') { |
313 | 313 | $dirStream[] = $file; |
314 | 314 | } |
315 | 315 | } |
316 | 316 | return IteratorDirectory::wrap($dirStream); |
317 | - } catch(\Exception $e) { |
|
317 | + } catch (\Exception $e) { |
|
318 | 318 | return false; |
319 | 319 | } |
320 | 320 | } |
@@ -366,21 +366,21 @@ discard block |
||
366 | 366 | public function fopen($path, $mode) { |
367 | 367 | try { |
368 | 368 | $absPath = $this->absPath($path); |
369 | - switch($mode) { |
|
369 | + switch ($mode) { |
|
370 | 370 | case 'r': |
371 | 371 | case 'rb': |
372 | - if ( !$this->file_exists($path)) { |
|
372 | + if (!$this->file_exists($path)) { |
|
373 | 373 | return false; |
374 | 374 | } |
375 | 375 | SFTPReadStream::register(); |
376 | 376 | $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
377 | - $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context); |
|
377 | + $handle = fopen('sftpread://'.trim($absPath, '/'), 'r', false, $context); |
|
378 | 378 | return RetryWrapper::wrap($handle); |
379 | 379 | case 'w': |
380 | 380 | case 'wb': |
381 | 381 | SFTPWriteStream::register(); |
382 | 382 | $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]); |
383 | - return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context); |
|
383 | + return fopen('sftpwrite://'.trim($absPath, '/'), 'w', false, $context); |
|
384 | 384 | case 'a': |
385 | 385 | case 'ab': |
386 | 386 | case 'r+': |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | /** |
404 | 404 | * {@inheritdoc} |
405 | 405 | */ |
406 | - public function touch($path, $mtime=null) { |
|
406 | + public function touch($path, $mtime = null) { |
|
407 | 407 | try { |
408 | 408 | if (!is_null($mtime)) { |
409 | 409 | return false; |
@@ -478,7 +478,7 @@ discard block |
||
478 | 478 | // Do not pass the password here. We want to use the Net_SFTP object |
479 | 479 | // supplied via stream context or fail. We only supply username and |
480 | 480 | // hostname because this might show up in logs (they are not used). |
481 | - $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path; |
|
481 | + $url = 'sftp://'.urlencode($this->user).'@'.$this->host.':'.$this->port.$this->root.$path; |
|
482 | 482 | return $url; |
483 | 483 | } |
484 | 484 | } |