1 | <?php |
||
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 = []) { |
|
51 | |||
52 | /** |
||
53 | * @throws ConnectException |
||
54 | */ |
||
55 | 1040 | public function connect() { |
|
56 | 1040 | if (is_null($this->getAuthStream())) { |
|
57 | throw new ConnectException('Authentication not set before connecting'); |
||
58 | } |
||
59 | |||
60 | $descriptorSpec = [ |
||
61 | 1040 | 0 => ['pipe', 'r'], // child reads from stdin |
|
62 | 260 | 1 => ['pipe', 'w'], // child writes to stdout |
|
63 | 260 | 2 => ['pipe', 'w'], // child writes to stderr |
|
64 | 1040 | 3 => $this->getAuthStream(), // child reads from fd#3 |
|
65 | 260 | 4 => ['pipe', 'r'], // child reads from fd#4 |
|
66 | 260 | 5 => ['pipe', 'w'] // child writes to fd#5 |
|
67 | 260 | ]; |
|
68 | |||
69 | 1040 | setlocale(LC_ALL, Server::LOCALE); |
|
70 | 1040 | $env = array_merge($this->env, [ |
|
71 | 1040 | 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! |
|
72 | 1040 | 'LC_ALL' => Server::LOCALE, |
|
73 | 1040 | 'LANG' => Server::LOCALE, |
|
74 | 780 | 'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output |
|
75 | 260 | ]); |
|
76 | 1040 | $this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env); |
|
77 | 1040 | if (!$this->isValid()) { |
|
78 | throw new ConnectionException(); |
||
79 | } |
||
80 | 1040 | $this->connected = true; |
|
81 | 1040 | } |
|
82 | |||
83 | /** |
||
84 | * check if the connection is still active |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | 1040 | public function isValid() { |
|
96 | |||
97 | /** |
||
98 | * send input to the process |
||
99 | * |
||
100 | * @param string $input |
||
101 | */ |
||
102 | 1024 | public function write($input) { |
|
106 | |||
107 | /** |
||
108 | * read a line of output |
||
109 | * |
||
110 | * @return string|false |
||
111 | */ |
||
112 | 1040 | public function readLine() { |
|
115 | |||
116 | /** |
||
117 | * read a line of output |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function readError() { |
||
122 | return trim(stream_get_line($this->getErrorStream(), 4086)); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * get all output until the process closes |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 20 | public function readAll() { |
|
131 | 20 | $output = []; |
|
132 | 20 | while ($line = $this->readLine()) { |
|
133 | 20 | $output[] = $line; |
|
134 | 5 | } |
|
135 | 20 | return $output; |
|
136 | } |
||
137 | |||
138 | 1024 | public function getInputStream() { |
|
141 | |||
142 | 1040 | public function getOutputStream() { |
|
145 | |||
146 | public function getErrorStream() { |
||
147 | return $this->pipes[2]; |
||
148 | } |
||
149 | |||
150 | 1040 | public function getAuthStream() { |
|
153 | |||
154 | 64 | public function getFileInputStream() { |
|
155 | 64 | return $this->pipes[4]; |
|
156 | } |
||
157 | |||
158 | 64 | public function getFileOutputStream() { |
|
159 | 64 | return $this->pipes[5]; |
|
160 | } |
||
161 | |||
162 | 1040 | public function writeAuthentication($user, $password) { |
|
170 | |||
171 | 1040 | public function close($terminate = true) { |
|
172 | 1040 | if (!is_resource($this->process)) { |
|
173 | 84 | return; |
|
174 | } |
||
180 | 1040 | ||
181 | 1040 | public function reconnect() { |
|
185 | 256 | ||
186 | 260 | public function __destruct() { |
|
189 | } |
||
190 |