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 | public function __construct($command, array $env = []) { |
||
48 | $this->command = $command; |
||
49 | $this->env = $env; |
||
50 | } |
||
51 | |||
52 | public function connect() { |
||
53 | if (is_null($this->getAuthStream())) { |
||
54 | throw new ConnectException('Authentication not set before connecting'); |
||
55 | } |
||
56 | |||
57 | $descriptorSpec = [ |
||
58 | 0 => ['pipe', 'r'], // child reads from stdin |
||
59 | 1 => ['pipe', 'w'], // child writes to stdout |
||
60 | 2 => ['pipe', 'w'], // child writes to stderr |
||
61 | 3 => $this->getAuthStream(), // child reads from fd#3 |
||
62 | 4 => ['pipe', 'r'], // child reads from fd#4 |
||
63 | 5 => ['pipe', 'w'] // child writes to fd#5 |
||
64 | ]; |
||
65 | |||
66 | setlocale(LC_ALL, Server::LOCALE); |
||
67 | $env = array_merge($this->env, [ |
||
68 | 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! |
||
69 | 'LC_ALL' => Server::LOCALE, |
||
70 | 'LANG' => Server::LOCALE, |
||
71 | 'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output |
||
72 | ]); |
||
73 | $this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env); |
||
74 | if (!$this->isValid()) { |
||
75 | throw new ConnectionException(); |
||
76 | } |
||
77 | $this->connected = true; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * check if the connection is still active |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function isValid() { |
||
86 | if (is_resource($this->process)) { |
||
87 | $status = proc_get_status($this->process); |
||
88 | 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 | public function write($input) { |
||
100 | fwrite($this->getInputStream(), $input); |
||
101 | fflush($this->getInputStream()); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * read a line of output |
||
106 | * |
||
107 | * @return string|false |
||
108 | */ |
||
109 | public function readLine() { |
||
110 | 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() { |
||
121 | |||
122 | /** |
||
123 | * get all output until the process closes |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | public function readAll() { |
||
128 | $output = array(); |
||
129 | while ($line = $this->readLine()) { |
||
130 | $output[] = $line; |
||
131 | } |
||
132 | return $output; |
||
133 | } |
||
134 | |||
135 | public function getInputStream() { |
||
136 | return $this->pipes[0]; |
||
137 | } |
||
138 | |||
139 | public function getOutputStream() { |
||
140 | return $this->pipes[1]; |
||
141 | } |
||
142 | |||
143 | public function getErrorStream() { |
||
146 | |||
147 | public function getAuthStream() { |
||
148 | return $this->authStream; |
||
149 | } |
||
150 | |||
151 | public function getFileInputStream() { |
||
152 | return $this->pipes[4]; |
||
153 | } |
||
154 | |||
155 | public function getFileOutputStream() { |
||
156 | return $this->pipes[5]; |
||
158 | |||
159 | public function writeAuthentication($user, $password) { |
||
167 | |||
168 | public function close($terminate = true) { |
||
189 | |||
190 | public function reconnect() { |
||
194 | |||
195 | public function __destruct() { |
||
198 | } |
||
199 |