Passed
Push — master ( 24d0fb...bc411e )
by Roeland
13:34 queued 12s
created

SFTPReadStream::stream_open()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 34
rs 8.9297
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 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) {
0 ignored issues
show
introduced by
The condition $remote_file === false is always false.
Loading history...
87
			return false;
88
		}
89
90
		$packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_OPEN_READ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
91
		if (!$this->sftp->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_OPEN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
			return false;
93
		}
94
95
		$response = $this->sftp->_get_sftp_packet();
96
		switch ($this->sftp->packet_type) {
97
			case NET_SFTP_HANDLE:
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_HANDLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
98
				$this->handle = substr($response, 4);
0 ignored issues
show
Documentation Bug introduced by
It seems like substr($response, 4) of type string is incompatible with the declared type resource of property $handle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
				break;
100
			case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_STATUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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);
0 ignored issues
show
Bug introduced by
$this->handle of type resource is incompatible with the type string expected by parameter $string of strlen(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
		$packet = pack('Na*N3', strlen(/** @scrutinizer ignore-type */ $this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size);
Loading history...
142
		return $this->sftp->_send_sftp_packet(NET_SFTP_READ, $packet);
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_READ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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:
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_DATA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
150
				$temp = substr($response, 4);
151
				$len = strlen($temp);
152
				$this->internalPosition += $len;
153
				return $temp;
154
			case NET_SFTP_STATUS:
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_STATUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
155
				[1 => $status] = unpack('N', substr($response, 0, 4));
156
				if ($status == NET_SFTP_STATUS_EOF) {
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_STATUS_EOF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
157
					$this->eof = true;
158
				}
159
				return '';
160
			default:
161
				return '';
162
		}
163
	}
164
165
	public function stream_write($data) {
166
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Icewind\Streams\File::stream_write() of integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Icewind\Streams\File::stream_stat() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
$this->handle of type resource is incompatible with the type string expected by parameter $handle of phpseclib\Net\SFTP::_close_handle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
		if (!$this->sftp->_close_handle(/** @scrutinizer ignore-type */ $this->handle)) {
Loading history...
195
			return false;
196
		}
197
	}
198
199
}
200