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

SFTPWriteStream::stream_read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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) {
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_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_TRUNCATE, 0);
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_OPEN_WRITE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant OCA\Files_External\Lib\S...\NET_SFTP_OPEN_TRUNCATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant OCA\Files_External\Lib\S...ge\NET_SFTP_OPEN_CREATE 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
		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;
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_read() of string.

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...
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;
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...
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;
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...
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);
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

156
		$packet = pack('Na*N3a*', strlen(/** @scrutinizer ignore-type */ $this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size, $this->buffer);
Loading history...
157
		if (!$this->sftp->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
0 ignored issues
show
Bug introduced by
The constant OCA\Files_External\Lib\Storage\NET_SFTP_WRITE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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)) {
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

172
		if (!$this->sftp->_close_handle(/** @scrutinizer ignore-type */ $this->handle)) {
Loading history...
173
			return false;
174
		}
175
	}
176
177
}
178
179