Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RawConnection.php « Wrapped « src « smb « icewind « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4aec674c3da0f0c3ecc592a0ed68f201007fbe2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
 * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Licensed under the MIT license:
 * http://opensource.org/licenses/MIT
 */

namespace Icewind\SMB\Wrapped;

use Icewind\SMB\Exception\ConnectException;
use Icewind\SMB\Exception\ConnectionException;

class RawConnection {
	/**
	 * @var string
	 */
	private $command;

	/**
	 * @var string[]
	 */
	private $env;

	/**
	 * @var resource[] $pipes
	 *
	 * $pipes[0] holds STDIN for smbclient
	 * $pipes[1] holds STDOUT for smbclient
	 * $pipes[3] holds the authfile for smbclient
	 * $pipes[4] holds the stream for writing files
	 * $pipes[5] holds the stream for reading files
	 */
	private $pipes = [];

	/**
	 * @var resource|null $process
	 */
	private $process;

	/**
	 * @var resource|null $authStream
	 */
	private $authStream = null;

	/**
	 * @param string $command
	 * @param array<string, string> $env
	 */
	public function __construct(string $command, array $env = []) {
		$this->command = $command;
		$this->env = $env;
	}

	/**
	 * @throws ConnectException
	 * @psalm-assert resource $this->process
	 */
	public function connect(): void {
		if (is_null($this->getAuthStream())) {
			throw new ConnectException('Authentication not set before connecting');
		}

		$descriptorSpec = [
			0 => ['pipe', 'r'], // child reads from stdin
			1 => ['pipe', 'w'], // child writes to stdout
			2 => ['pipe', 'w'], // child writes to stderr
			3 => $this->getAuthStream(), // child reads from fd#3
			4 => ['pipe', 'r'], // child reads from fd#4
			5 => ['pipe', 'w']  // child writes to fd#5
		];

		setlocale(LC_ALL, Server::LOCALE);
		$env = array_merge($this->env, [
			'CLI_FORCE_INTERACTIVE' => 'y', // Make sure the prompt is displayed
			'CLI_NO_READLINE'       => 1,   // Not all distros build smbclient with readline, disable it to get consistent behaviour
			'LC_ALL'                => Server::LOCALE,
			'LANG'                  => Server::LOCALE,
			'COLUMNS'               => 8192 // prevent smbclient from line-wrapping it's output
		]);
		$this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env);
		if (!$this->isValid()) {
			throw new ConnectionException();
		}
	}

	/**
	 * check if the connection is still active
	 *
	 * @return bool
	 * @psalm-assert-if-true resource $this->process
	 */
	public function isValid(): bool {
		if (is_resource($this->process)) {
			$status = proc_get_status($this->process);
			return $status['running'];
		} else {
			return false;
		}
	}

	/**
	 * send input to the process
	 *
	 * @param string $input
	 * @return int|bool
	 */
	public function write(string $input) {
		$result = @fwrite($this->getInputStream(), $input);
		fflush($this->getInputStream());
		return $result;
	}

	/**
	 * read output till the next prompt
	 *
	 * @return string|false
	 */
	public function readTillPrompt() {
		$output = "";
		do {
			$chunk = $this->readLine('\> ');
			if ($chunk === false) {
				return false;
			}
			$output .= $chunk;
		} while (strlen($chunk) == 4096 && strpos($chunk, "smb:") === false);
		return $output;
	}

	/**
	 * read a line of output
	 *
	 * @return string|false
	 */
	public function readLine(string $end = "\n") {
		return stream_get_line($this->getOutputStream(), 4096, $end);
	}

	/**
	 * read a line of output
	 *
	 * @return string|false
	 */
	public function readError() {
		$line = stream_get_line($this->getErrorStream(), 4086);
		return $line !== false ? trim($line) : false;
	}

	/**
	 * get all output until the process closes
	 *
	 * @return string[]
	 */
	public function readAll(): array {
		$output = [];
		while ($line = $this->readLine()) {
			$output[] = $line;
		}
		return $output;
	}

	/**
	 * @return resource
	 */
	public function getInputStream() {
		return $this->pipes[0];
	}

	/**
	 * @return resource
	 */
	public function getOutputStream() {
		return $this->pipes[1];
	}

	/**
	 * @return resource
	 */
	public function getErrorStream() {
		return $this->pipes[2];
	}

	/**
	 * @return resource|null
	 */
	public function getAuthStream() {
		return $this->authStream;
	}

	/**
	 * @return resource
	 */
	public function getFileInputStream() {
		return $this->pipes[4];
	}

	/**
	 * @return resource
	 */
	public function getFileOutputStream() {
		return $this->pipes[5];
	}

	/**
	 * @param string|null $user
	 * @param string|null $password
	 * @psalm-assert resource $this->authStream
	 */
	public function writeAuthentication(?string $user, ?string $password): void {
		$auth = ($password === null)
			? "username=$user"
			: "username=$user\npassword=$password\n";

		$this->authStream = fopen('php://temp', 'w+');
		fwrite($this->authStream, $auth);
	}

	/**
	 * @param bool $terminate
	 * @psalm-assert null $this->process
	 */
	public function close(bool $terminate = true): void {
		$this->close_process($terminate);
	}

	/**
	 * @param bool $terminate
	 * @psalm-assert null $this->process
	 */
	protected function close_process(bool $terminate = true): void {
		if (!is_resource($this->process)) {
			return;
		}
		if ($terminate) {
			proc_terminate($this->process);
		}
		proc_close($this->process);
		$this->process = null;
	}

	public function reconnect(): void {
		$this->close();
		$this->connect();
	}

	public function __destruct() {
		$this->close();
	}
}