1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
5
|
|
|
* http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Icewind\SMB; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\Exception\ConnectException; |
11
|
|
|
use Icewind\SMB\Exception\ConnectionException; |
12
|
|
|
|
13
|
|
|
class RawConnection { |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $command; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string[] |
21
|
|
|
*/ |
22
|
|
|
private $env; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var resource[] $pipes |
26
|
|
|
* |
27
|
|
|
* $pipes[0] holds STDIN for smbclient |
28
|
|
|
* $pipes[1] holds STDOUT for smbclient |
29
|
|
|
* $pipes[3] holds the authfile for smbclient |
30
|
|
|
* $pipes[4] holds the stream for writing files |
31
|
|
|
* $pipes[5] holds the stream for reading files |
32
|
|
|
*/ |
33
|
|
|
private $pipes; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var resource $process |
37
|
|
|
*/ |
38
|
|
|
private $process; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var resource|null $authStream |
42
|
|
|
*/ |
43
|
|
|
private $authStream = null; |
44
|
|
|
|
45
|
|
|
private $connected = false; |
46
|
|
|
|
47
|
1040 |
|
public function __construct($command, array $env = []) { |
48
|
1040 |
|
$this->command = $command; |
49
|
1040 |
|
$this->env = $env; |
50
|
1040 |
|
} |
51
|
|
|
|
52
|
1040 |
|
public function connect() { |
53
|
1040 |
|
if (is_null($this->getAuthStream())) { |
54
|
|
|
throw new ConnectException('Authentication not set before connecting'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$descriptorSpec = [ |
58
|
1040 |
|
0 => ['pipe', 'r'], // child reads from stdin |
59
|
520 |
|
1 => ['pipe', 'w'], // child writes to stdout |
60
|
520 |
|
2 => ['pipe', 'w'], // child writes to stderr |
61
|
1040 |
|
3 => $this->getAuthStream(), // child reads from fd#3 |
62
|
520 |
|
4 => ['pipe', 'r'], // child reads from fd#4 |
63
|
520 |
|
5 => ['pipe', 'w'] // child writes to fd#5 |
64
|
520 |
|
]; |
65
|
|
|
|
66
|
1040 |
|
setlocale(LC_ALL, Server::LOCALE); |
67
|
1040 |
|
$env = array_merge($this->env, [ |
68
|
1040 |
|
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! |
69
|
520 |
|
'LC_ALL' => Server::LOCALE, |
70
|
520 |
|
'LANG' => Server::LOCALE, |
71
|
|
|
'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output |
72
|
520 |
|
]); |
73
|
1040 |
|
$this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env); |
74
|
1040 |
|
if (!$this->isValid()) { |
75
|
|
|
throw new ConnectionException(); |
76
|
|
|
} |
77
|
1040 |
|
$this->connected = true; |
78
|
1040 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* check if the connection is still active |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
1040 |
|
public function isValid() { |
86
|
1040 |
|
if (is_resource($this->process)) { |
87
|
1040 |
|
$status = proc_get_status($this->process); |
88
|
1040 |
|
return $status['running']; |
89
|
|
|
} else { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* send input to the process |
96
|
|
|
* |
97
|
|
|
* @param string $input |
98
|
|
|
*/ |
99
|
1024 |
|
public function write($input) { |
100
|
1024 |
|
fwrite($this->getInputStream(), $input); |
101
|
1024 |
|
fflush($this->getInputStream()); |
102
|
1024 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* read a line of output |
106
|
|
|
* |
107
|
|
|
* @return string|false |
108
|
|
|
*/ |
109
|
1040 |
|
public function readLine() { |
110
|
1040 |
|
return stream_get_line($this->getOutputStream(), 4086, "\n"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* read a line of output |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function readError() { |
119
|
|
|
return trim(stream_get_line($this->getErrorStream(), 4086)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* get all output until the process closes |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
20 |
|
public function readAll() { |
128
|
20 |
|
$output = array(); |
129
|
20 |
|
while ($line = $this->readLine()) { |
130
|
20 |
|
$output[] = $line; |
131
|
10 |
|
} |
132
|
20 |
|
return $output; |
133
|
|
|
} |
134
|
|
|
|
135
|
1024 |
|
public function getInputStream() { |
136
|
1024 |
|
return $this->pipes[0]; |
137
|
|
|
} |
138
|
|
|
|
139
|
1040 |
|
public function getOutputStream() { |
140
|
1040 |
|
return $this->pipes[1]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getErrorStream() { |
144
|
|
|
return $this->pipes[2]; |
145
|
|
|
} |
146
|
|
|
|
147
|
1040 |
|
public function getAuthStream() { |
148
|
1040 |
|
return $this->authStream; |
149
|
|
|
} |
150
|
|
|
|
151
|
64 |
|
public function getFileInputStream() { |
152
|
64 |
|
return $this->pipes[4]; |
153
|
|
|
} |
154
|
|
|
|
155
|
64 |
|
public function getFileOutputStream() { |
156
|
64 |
|
return $this->pipes[5]; |
157
|
|
|
} |
158
|
|
|
|
159
|
1040 |
|
public function writeAuthentication($user, $password) { |
160
|
1040 |
|
$auth = ($password === false) |
161
|
520 |
|
? "username=$user" |
162
|
1040 |
|
: "username=$user\npassword=$password\n"; |
163
|
|
|
|
164
|
1040 |
|
$this->authStream = fopen('php://temp', 'w+'); |
165
|
1040 |
|
fwrite($this->getAuthStream(), $auth); |
166
|
1040 |
|
} |
167
|
|
|
|
168
|
1040 |
|
public function close($terminate = true) { |
169
|
1040 |
|
if (!is_resource($this->process)) { |
170
|
84 |
|
return; |
171
|
|
|
} |
172
|
1040 |
|
if ($terminate) { |
173
|
|
|
// if for case that posix_ functions are not available |
174
|
1040 |
|
if (function_exists('posix_kill')) { |
175
|
1040 |
|
$status = proc_get_status($this->process); |
176
|
1040 |
|
$ppid = $status['pid']; |
177
|
1040 |
|
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`); |
178
|
1040 |
|
foreach ($pids as $pid) { |
179
|
1040 |
|
if (is_numeric($pid)) { |
180
|
|
|
//9 is the SIGKILL signal |
181
|
1032 |
|
posix_kill($pid, 9); |
182
|
512 |
|
} |
183
|
520 |
|
} |
184
|
520 |
|
} |
185
|
1040 |
|
proc_terminate($this->process); |
186
|
520 |
|
} |
187
|
1040 |
|
proc_close($this->process); |
188
|
1040 |
|
} |
189
|
|
|
|
190
|
4 |
|
public function reconnect() { |
191
|
4 |
|
$this->close(); |
192
|
4 |
|
$this->connect(); |
193
|
4 |
|
} |
194
|
|
|
|
195
|
1040 |
|
public function __destruct() { |
196
|
1040 |
|
$this->close(); |
197
|
1040 |
|
} |
198
|
|
|
} |
199
|
|
|
|