@@ -31,155 +31,155 @@ |
||
31 | 31 | use phpseclib\Net\SSH2; |
32 | 32 | |
33 | 33 | class SFTPWriteStream implements File { |
34 | - /** @var resource */ |
|
35 | - public $context; |
|
36 | - |
|
37 | - /** @var \phpseclib\Net\SFTP */ |
|
38 | - private $sftp; |
|
39 | - |
|
40 | - /** @var resource */ |
|
41 | - private $handle; |
|
42 | - |
|
43 | - /** @var int */ |
|
44 | - private $internalPosition = 0; |
|
45 | - |
|
46 | - /** @var int */ |
|
47 | - private $writePosition = 0; |
|
48 | - |
|
49 | - /** @var bool */ |
|
50 | - private $eof = false; |
|
51 | - |
|
52 | - private $buffer = ''; |
|
53 | - |
|
54 | - public static function register($protocol = 'sftpwrite') { |
|
55 | - if (in_array($protocol, stream_get_wrappers(), true)) { |
|
56 | - return false; |
|
57 | - } |
|
58 | - return stream_wrapper_register($protocol, get_called_class()); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Load the source from the stream context and return the context options |
|
63 | - * |
|
64 | - * @param string $name |
|
65 | - * @return array |
|
66 | - * @throws \BadMethodCallException |
|
67 | - */ |
|
68 | - protected function loadContext($name) { |
|
69 | - $context = stream_context_get_options($this->context); |
|
70 | - if (isset($context[$name])) { |
|
71 | - $context = $context[$name]; |
|
72 | - } else { |
|
73 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
74 | - } |
|
75 | - if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
|
76 | - $this->sftp = $context['session']; |
|
77 | - } else { |
|
78 | - throw new \BadMethodCallException('Invalid context, session not set'); |
|
79 | - } |
|
80 | - return $context; |
|
81 | - } |
|
82 | - |
|
83 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
84 | - [, $path] = explode('://', $path); |
|
85 | - $path = '/' . ltrim($path); |
|
86 | - $path = str_replace('//', '/', $path); |
|
87 | - |
|
88 | - $this->loadContext('sftp'); |
|
89 | - |
|
90 | - if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) { |
|
91 | - return false; |
|
92 | - } |
|
93 | - |
|
94 | - $remote_file = $this->sftp->_realpath($path); |
|
95 | - if ($remote_file === false) { |
|
96 | - return false; |
|
97 | - } |
|
98 | - |
|
99 | - $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0); |
|
100 | - if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { |
|
101 | - return false; |
|
102 | - } |
|
103 | - |
|
104 | - $response = $this->sftp->_get_sftp_packet(); |
|
105 | - switch ($this->sftp->packet_type) { |
|
106 | - case NET_SFTP_HANDLE: |
|
107 | - $this->handle = substr($response, 4); |
|
108 | - break; |
|
109 | - case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED |
|
110 | - $this->sftp->_logError($response); |
|
111 | - return false; |
|
112 | - default: |
|
113 | - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); |
|
114 | - return false; |
|
115 | - } |
|
116 | - |
|
117 | - return true; |
|
118 | - } |
|
119 | - |
|
120 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
121 | - return false; |
|
122 | - } |
|
123 | - |
|
124 | - public function stream_tell() { |
|
125 | - return $this->writePosition; |
|
126 | - } |
|
127 | - |
|
128 | - public function stream_read($count) { |
|
129 | - return false; |
|
130 | - } |
|
131 | - |
|
132 | - public function stream_write($data) { |
|
133 | - $written = strlen($data); |
|
134 | - $this->writePosition += $written; |
|
135 | - |
|
136 | - $this->buffer .= $data; |
|
137 | - |
|
138 | - if (strlen($this->buffer) > 64 * 1024) { |
|
139 | - if (!$this->stream_flush()) { |
|
140 | - return false; |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - return $written; |
|
145 | - } |
|
146 | - |
|
147 | - public function stream_set_option($option, $arg1, $arg2) { |
|
148 | - return false; |
|
149 | - } |
|
150 | - |
|
151 | - public function stream_truncate($size) { |
|
152 | - return false; |
|
153 | - } |
|
154 | - |
|
155 | - public function stream_stat() { |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - public function stream_lock($operation) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - public function stream_flush() { |
|
164 | - $size = strlen($this->buffer); |
|
165 | - $packet = pack('Na*N3a*', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer); |
|
166 | - if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) { |
|
167 | - return false; |
|
168 | - } |
|
169 | - $this->internalPosition += $size; |
|
170 | - $this->buffer = ''; |
|
171 | - |
|
172 | - return $this->sftp->_read_put_responses(1); |
|
173 | - } |
|
174 | - |
|
175 | - public function stream_eof() { |
|
176 | - return $this->eof; |
|
177 | - } |
|
178 | - |
|
179 | - public function stream_close() { |
|
180 | - $this->stream_flush(); |
|
181 | - if (!$this->sftp->_close_handle($this->handle)) { |
|
182 | - return false; |
|
183 | - } |
|
184 | - } |
|
34 | + /** @var resource */ |
|
35 | + public $context; |
|
36 | + |
|
37 | + /** @var \phpseclib\Net\SFTP */ |
|
38 | + private $sftp; |
|
39 | + |
|
40 | + /** @var resource */ |
|
41 | + private $handle; |
|
42 | + |
|
43 | + /** @var int */ |
|
44 | + private $internalPosition = 0; |
|
45 | + |
|
46 | + /** @var int */ |
|
47 | + private $writePosition = 0; |
|
48 | + |
|
49 | + /** @var bool */ |
|
50 | + private $eof = false; |
|
51 | + |
|
52 | + private $buffer = ''; |
|
53 | + |
|
54 | + public static function register($protocol = 'sftpwrite') { |
|
55 | + if (in_array($protocol, stream_get_wrappers(), true)) { |
|
56 | + return false; |
|
57 | + } |
|
58 | + return stream_wrapper_register($protocol, get_called_class()); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Load the source from the stream context and return the context options |
|
63 | + * |
|
64 | + * @param string $name |
|
65 | + * @return array |
|
66 | + * @throws \BadMethodCallException |
|
67 | + */ |
|
68 | + protected function loadContext($name) { |
|
69 | + $context = stream_context_get_options($this->context); |
|
70 | + if (isset($context[$name])) { |
|
71 | + $context = $context[$name]; |
|
72 | + } else { |
|
73 | + throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
74 | + } |
|
75 | + if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
|
76 | + $this->sftp = $context['session']; |
|
77 | + } else { |
|
78 | + throw new \BadMethodCallException('Invalid context, session not set'); |
|
79 | + } |
|
80 | + return $context; |
|
81 | + } |
|
82 | + |
|
83 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
84 | + [, $path] = explode('://', $path); |
|
85 | + $path = '/' . ltrim($path); |
|
86 | + $path = str_replace('//', '/', $path); |
|
87 | + |
|
88 | + $this->loadContext('sftp'); |
|
89 | + |
|
90 | + if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) { |
|
91 | + return false; |
|
92 | + } |
|
93 | + |
|
94 | + $remote_file = $this->sftp->_realpath($path); |
|
95 | + if ($remote_file === false) { |
|
96 | + return false; |
|
97 | + } |
|
98 | + |
|
99 | + $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0); |
|
100 | + if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { |
|
101 | + return false; |
|
102 | + } |
|
103 | + |
|
104 | + $response = $this->sftp->_get_sftp_packet(); |
|
105 | + switch ($this->sftp->packet_type) { |
|
106 | + case NET_SFTP_HANDLE: |
|
107 | + $this->handle = substr($response, 4); |
|
108 | + break; |
|
109 | + case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED |
|
110 | + $this->sftp->_logError($response); |
|
111 | + return false; |
|
112 | + default: |
|
113 | + user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); |
|
114 | + return false; |
|
115 | + } |
|
116 | + |
|
117 | + return true; |
|
118 | + } |
|
119 | + |
|
120 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
121 | + return false; |
|
122 | + } |
|
123 | + |
|
124 | + public function stream_tell() { |
|
125 | + return $this->writePosition; |
|
126 | + } |
|
127 | + |
|
128 | + public function stream_read($count) { |
|
129 | + return false; |
|
130 | + } |
|
131 | + |
|
132 | + public function stream_write($data) { |
|
133 | + $written = strlen($data); |
|
134 | + $this->writePosition += $written; |
|
135 | + |
|
136 | + $this->buffer .= $data; |
|
137 | + |
|
138 | + if (strlen($this->buffer) > 64 * 1024) { |
|
139 | + if (!$this->stream_flush()) { |
|
140 | + return false; |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + return $written; |
|
145 | + } |
|
146 | + |
|
147 | + public function stream_set_option($option, $arg1, $arg2) { |
|
148 | + return false; |
|
149 | + } |
|
150 | + |
|
151 | + public function stream_truncate($size) { |
|
152 | + return false; |
|
153 | + } |
|
154 | + |
|
155 | + public function stream_stat() { |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + public function stream_lock($operation) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + public function stream_flush() { |
|
164 | + $size = strlen($this->buffer); |
|
165 | + $packet = pack('Na*N3a*', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer); |
|
166 | + if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) { |
|
167 | + return false; |
|
168 | + } |
|
169 | + $this->internalPosition += $size; |
|
170 | + $this->buffer = ''; |
|
171 | + |
|
172 | + return $this->sftp->_read_put_responses(1); |
|
173 | + } |
|
174 | + |
|
175 | + public function stream_eof() { |
|
176 | + return $this->eof; |
|
177 | + } |
|
178 | + |
|
179 | + public function stream_close() { |
|
180 | + $this->stream_flush(); |
|
181 | + if (!$this->sftp->_close_handle($this->handle)) { |
|
182 | + return false; |
|
183 | + } |
|
184 | + } |
|
185 | 185 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | if (isset($context[$name])) { |
71 | 71 | $context = $context[$name]; |
72 | 72 | } else { |
73 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
73 | + throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set'); |
|
74 | 74 | } |
75 | 75 | if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
76 | 76 | $this->sftp = $context['session']; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | |
83 | 83 | public function stream_open($path, $mode, $options, &$opened_path) { |
84 | 84 | [, $path] = explode('://', $path); |
85 | - $path = '/' . ltrim($path); |
|
85 | + $path = '/'.ltrim($path); |
|
86 | 86 | $path = str_replace('//', '/', $path); |
87 | 87 | |
88 | 88 | $this->loadContext('sftp'); |
@@ -31,177 +31,177 @@ |
||
31 | 31 | use phpseclib\Net\SSH2; |
32 | 32 | |
33 | 33 | class SFTPReadStream implements File { |
34 | - /** @var resource */ |
|
35 | - public $context; |
|
36 | - |
|
37 | - /** @var \phpseclib\Net\SFTP */ |
|
38 | - private $sftp; |
|
39 | - |
|
40 | - /** @var resource */ |
|
41 | - private $handle; |
|
42 | - |
|
43 | - /** @var int */ |
|
44 | - private $internalPosition = 0; |
|
45 | - |
|
46 | - /** @var int */ |
|
47 | - private $readPosition = 0; |
|
48 | - |
|
49 | - /** @var bool */ |
|
50 | - private $eof = false; |
|
51 | - |
|
52 | - private $buffer = ''; |
|
53 | - |
|
54 | - public static function register($protocol = 'sftpread') { |
|
55 | - if (in_array($protocol, stream_get_wrappers(), true)) { |
|
56 | - return false; |
|
57 | - } |
|
58 | - return stream_wrapper_register($protocol, get_called_class()); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Load the source from the stream context and return the context options |
|
63 | - * |
|
64 | - * @param string $name |
|
65 | - * @return array |
|
66 | - * @throws \BadMethodCallException |
|
67 | - */ |
|
68 | - protected function loadContext($name) { |
|
69 | - $context = stream_context_get_options($this->context); |
|
70 | - if (isset($context[$name])) { |
|
71 | - $context = $context[$name]; |
|
72 | - } else { |
|
73 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
74 | - } |
|
75 | - if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
|
76 | - $this->sftp = $context['session']; |
|
77 | - } else { |
|
78 | - throw new \BadMethodCallException('Invalid context, session not set'); |
|
79 | - } |
|
80 | - return $context; |
|
81 | - } |
|
82 | - |
|
83 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
84 | - [, $path] = explode('://', $path); |
|
85 | - $path = '/' . ltrim($path); |
|
86 | - $path = str_replace('//', '/', $path); |
|
87 | - |
|
88 | - $this->loadContext('sftp'); |
|
89 | - |
|
90 | - if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) { |
|
91 | - return false; |
|
92 | - } |
|
93 | - |
|
94 | - $remote_file = $this->sftp->_realpath($path); |
|
95 | - if ($remote_file === false) { |
|
96 | - return false; |
|
97 | - } |
|
98 | - |
|
99 | - $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0); |
|
100 | - if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { |
|
101 | - return false; |
|
102 | - } |
|
103 | - |
|
104 | - $response = $this->sftp->_get_sftp_packet(); |
|
105 | - switch ($this->sftp->packet_type) { |
|
106 | - case NET_SFTP_HANDLE: |
|
107 | - $this->handle = substr($response, 4); |
|
108 | - break; |
|
109 | - case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED |
|
110 | - $this->sftp->_logError($response); |
|
111 | - return false; |
|
112 | - default: |
|
113 | - user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); |
|
114 | - return false; |
|
115 | - } |
|
116 | - |
|
117 | - $this->request_chunk(256 * 1024); |
|
118 | - |
|
119 | - return true; |
|
120 | - } |
|
121 | - |
|
122 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
123 | - return false; |
|
124 | - } |
|
125 | - |
|
126 | - public function stream_tell() { |
|
127 | - return $this->readPosition; |
|
128 | - } |
|
129 | - |
|
130 | - public function stream_read($count) { |
|
131 | - if (!$this->eof && strlen($this->buffer) < $count) { |
|
132 | - $chunk = $this->read_chunk(); |
|
133 | - $this->buffer .= $chunk; |
|
134 | - if (!$this->eof) { |
|
135 | - $this->request_chunk(256 * 1024); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - $data = substr($this->buffer, 0, $count); |
|
140 | - $this->buffer = substr($this->buffer, $count); |
|
141 | - if ($this->buffer === false) { |
|
142 | - $this->buffer = ''; |
|
143 | - } |
|
144 | - $this->readPosition += strlen($data); |
|
145 | - |
|
146 | - return $data; |
|
147 | - } |
|
148 | - |
|
149 | - private function request_chunk($size) { |
|
150 | - $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size); |
|
151 | - return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet); |
|
152 | - } |
|
153 | - |
|
154 | - private function read_chunk() { |
|
155 | - $response = $this->sftp->_get_sftp_packet(); |
|
156 | - |
|
157 | - switch ($this->sftp->packet_type) { |
|
158 | - case NET_SFTP_DATA: |
|
159 | - $temp = substr($response, 4); |
|
160 | - $len = strlen($temp); |
|
161 | - $this->internalPosition += $len; |
|
162 | - return $temp; |
|
163 | - case NET_SFTP_STATUS: |
|
164 | - [1 => $status] = unpack('N', substr($response, 0, 4)); |
|
165 | - if ($status == NET_SFTP_STATUS_EOF) { |
|
166 | - $this->eof = true; |
|
167 | - } |
|
168 | - return ''; |
|
169 | - default: |
|
170 | - return ''; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - public function stream_write($data) { |
|
175 | - return false; |
|
176 | - } |
|
177 | - |
|
178 | - public function stream_set_option($option, $arg1, $arg2) { |
|
179 | - return false; |
|
180 | - } |
|
181 | - |
|
182 | - public function stream_truncate($size) { |
|
183 | - return false; |
|
184 | - } |
|
185 | - |
|
186 | - public function stream_stat() { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - public function stream_lock($operation) { |
|
191 | - return false; |
|
192 | - } |
|
193 | - |
|
194 | - public function stream_flush() { |
|
195 | - return false; |
|
196 | - } |
|
197 | - |
|
198 | - public function stream_eof() { |
|
199 | - return $this->eof; |
|
200 | - } |
|
201 | - |
|
202 | - public function stream_close() { |
|
203 | - if (!$this->sftp->_close_handle($this->handle)) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - } |
|
34 | + /** @var resource */ |
|
35 | + public $context; |
|
36 | + |
|
37 | + /** @var \phpseclib\Net\SFTP */ |
|
38 | + private $sftp; |
|
39 | + |
|
40 | + /** @var resource */ |
|
41 | + private $handle; |
|
42 | + |
|
43 | + /** @var int */ |
|
44 | + private $internalPosition = 0; |
|
45 | + |
|
46 | + /** @var int */ |
|
47 | + private $readPosition = 0; |
|
48 | + |
|
49 | + /** @var bool */ |
|
50 | + private $eof = false; |
|
51 | + |
|
52 | + private $buffer = ''; |
|
53 | + |
|
54 | + public static function register($protocol = 'sftpread') { |
|
55 | + if (in_array($protocol, stream_get_wrappers(), true)) { |
|
56 | + return false; |
|
57 | + } |
|
58 | + return stream_wrapper_register($protocol, get_called_class()); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Load the source from the stream context and return the context options |
|
63 | + * |
|
64 | + * @param string $name |
|
65 | + * @return array |
|
66 | + * @throws \BadMethodCallException |
|
67 | + */ |
|
68 | + protected function loadContext($name) { |
|
69 | + $context = stream_context_get_options($this->context); |
|
70 | + if (isset($context[$name])) { |
|
71 | + $context = $context[$name]; |
|
72 | + } else { |
|
73 | + throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
74 | + } |
|
75 | + if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
|
76 | + $this->sftp = $context['session']; |
|
77 | + } else { |
|
78 | + throw new \BadMethodCallException('Invalid context, session not set'); |
|
79 | + } |
|
80 | + return $context; |
|
81 | + } |
|
82 | + |
|
83 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
84 | + [, $path] = explode('://', $path); |
|
85 | + $path = '/' . ltrim($path); |
|
86 | + $path = str_replace('//', '/', $path); |
|
87 | + |
|
88 | + $this->loadContext('sftp'); |
|
89 | + |
|
90 | + if (!($this->sftp->bitmap & SSH2::MASK_LOGIN)) { |
|
91 | + return false; |
|
92 | + } |
|
93 | + |
|
94 | + $remote_file = $this->sftp->_realpath($path); |
|
95 | + if ($remote_file === false) { |
|
96 | + return false; |
|
97 | + } |
|
98 | + |
|
99 | + $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0); |
|
100 | + if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) { |
|
101 | + return false; |
|
102 | + } |
|
103 | + |
|
104 | + $response = $this->sftp->_get_sftp_packet(); |
|
105 | + switch ($this->sftp->packet_type) { |
|
106 | + case NET_SFTP_HANDLE: |
|
107 | + $this->handle = substr($response, 4); |
|
108 | + break; |
|
109 | + case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED |
|
110 | + $this->sftp->_logError($response); |
|
111 | + return false; |
|
112 | + default: |
|
113 | + user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS'); |
|
114 | + return false; |
|
115 | + } |
|
116 | + |
|
117 | + $this->request_chunk(256 * 1024); |
|
118 | + |
|
119 | + return true; |
|
120 | + } |
|
121 | + |
|
122 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
123 | + return false; |
|
124 | + } |
|
125 | + |
|
126 | + public function stream_tell() { |
|
127 | + return $this->readPosition; |
|
128 | + } |
|
129 | + |
|
130 | + public function stream_read($count) { |
|
131 | + if (!$this->eof && strlen($this->buffer) < $count) { |
|
132 | + $chunk = $this->read_chunk(); |
|
133 | + $this->buffer .= $chunk; |
|
134 | + if (!$this->eof) { |
|
135 | + $this->request_chunk(256 * 1024); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + $data = substr($this->buffer, 0, $count); |
|
140 | + $this->buffer = substr($this->buffer, $count); |
|
141 | + if ($this->buffer === false) { |
|
142 | + $this->buffer = ''; |
|
143 | + } |
|
144 | + $this->readPosition += strlen($data); |
|
145 | + |
|
146 | + return $data; |
|
147 | + } |
|
148 | + |
|
149 | + private function request_chunk($size) { |
|
150 | + $packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size); |
|
151 | + return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet); |
|
152 | + } |
|
153 | + |
|
154 | + private function read_chunk() { |
|
155 | + $response = $this->sftp->_get_sftp_packet(); |
|
156 | + |
|
157 | + switch ($this->sftp->packet_type) { |
|
158 | + case NET_SFTP_DATA: |
|
159 | + $temp = substr($response, 4); |
|
160 | + $len = strlen($temp); |
|
161 | + $this->internalPosition += $len; |
|
162 | + return $temp; |
|
163 | + case NET_SFTP_STATUS: |
|
164 | + [1 => $status] = unpack('N', substr($response, 0, 4)); |
|
165 | + if ($status == NET_SFTP_STATUS_EOF) { |
|
166 | + $this->eof = true; |
|
167 | + } |
|
168 | + return ''; |
|
169 | + default: |
|
170 | + return ''; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + public function stream_write($data) { |
|
175 | + return false; |
|
176 | + } |
|
177 | + |
|
178 | + public function stream_set_option($option, $arg1, $arg2) { |
|
179 | + return false; |
|
180 | + } |
|
181 | + |
|
182 | + public function stream_truncate($size) { |
|
183 | + return false; |
|
184 | + } |
|
185 | + |
|
186 | + public function stream_stat() { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + public function stream_lock($operation) { |
|
191 | + return false; |
|
192 | + } |
|
193 | + |
|
194 | + public function stream_flush() { |
|
195 | + return false; |
|
196 | + } |
|
197 | + |
|
198 | + public function stream_eof() { |
|
199 | + return $this->eof; |
|
200 | + } |
|
201 | + |
|
202 | + public function stream_close() { |
|
203 | + if (!$this->sftp->_close_handle($this->handle)) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + } |
|
207 | 207 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | if (isset($context[$name])) { |
71 | 71 | $context = $context[$name]; |
72 | 72 | } else { |
73 | - throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set'); |
|
73 | + throw new \BadMethodCallException('Invalid context, "'.$name.'" options not set'); |
|
74 | 74 | } |
75 | 75 | if (isset($context['session']) and $context['session'] instanceof \phpseclib\Net\SFTP) { |
76 | 76 | $this->sftp = $context['session']; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | |
83 | 83 | public function stream_open($path, $mode, $options, &$opened_path) { |
84 | 84 | [, $path] = explode('://', $path); |
85 | - $path = '/' . ltrim($path); |
|
85 | + $path = '/'.ltrim($path); |
|
86 | 86 | $path = str_replace('//', '/', $path); |
87 | 87 | |
88 | 88 | $this->loadContext('sftp'); |