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
|
<?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;
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
*/
private $pipes;
/**
* @var resource $process
*/
private $process;
public function __construct($command, $env = array()) {
$this->command = $command;
$this->env = $env;
$this->connect();
}
private function connect() {
$descriptorSpec = array(
0 => array('pipe', 'r'), // child reads from stdin
1 => array('pipe', 'w'), // child writes to stdout
2 => array('pipe', 'w'), // child writes to stderr
3 => array('pipe', 'r'), // child reads from fd#3
4 => array('pipe', 'r'), // child reads from fd#4
5 => array('pipe', 'w') // child writes to fd#5
);
setlocale(LC_ALL, Server::LOCALE);
$env = array_merge($this->env, array(
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'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
*/
public function isValid() {
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
*/
public function write($input) {
fwrite($this->getInputStream(), $input);
fflush($this->getInputStream());
}
/**
* read a line of output
*
* @return string
*/
public function readLine() {
return stream_get_line($this->getOutputStream(), 4086, "\n");
}
/**
* read a line of output
*
* @return string
*/
public function readError() {
return trim(stream_get_line($this->getErrorStream(), 4086));
}
/**
* get all output until the process closes
*
* @return array
*/
public function readAll() {
$output = array();
while ($line = $this->readLine()) {
$output[] = $line;
}
return $output;
}
public function getInputStream() {
return $this->pipes[0];
}
public function getOutputStream() {
return $this->pipes[1];
}
public function getErrorStream() {
return $this->pipes[2];
}
public function getAuthStream() {
return $this->pipes[3];
}
public function getFileInputStream() {
return $this->pipes[4];
}
public function getFileOutputStream() {
return $this->pipes[5];
}
public function writeAuthentication($user, $password) {
$auth = ($password === false)
? "username=$user"
: "username=$user\npassword=$password";
if (fwrite($this->getAuthStream(), $auth) === false) {
fclose($this->getAuthStream());
return false;
}
fclose($this->getAuthStream());
return true;
}
public function close($terminate = true) {
if (!is_resource($this->process)) {
return;
}
if ($terminate) {
// if for case that posix_ functions are not available
if (function_exists('posix_kill')) {
$status = proc_get_status($this->process);
$ppid = $status['pid'];
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
foreach($pids as $pid) {
if(is_numeric($pid)) {
//9 is the SIGKILL signal
posix_kill($pid, 9);
}
}
}
proc_terminate($this->process);
}
proc_close($this->process);
}
public function reconnect() {
$this->close();
$this->connect();
}
public function __destruct() {
$this->close();
}
}
|