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 | 520 | public function __construct($command, array $env = []) { |
|
51 | |||
52 | 520 | public function connect() { |
|
53 | 520 | if (is_null($this->getAuthStream())) { |
|
54 | throw new ConnectException('Authentication not set before connecting'); |
||
55 | } |
||
56 | |||
57 | $descriptorSpec = [ |
||
58 | 520 | 0 => ['pipe', 'r'], // child reads from stdin |
|
59 | 260 | 1 => ['pipe', 'w'], // child writes to stdout |
|
60 | 260 | 2 => ['pipe', 'w'], // child writes to stderr |
|
61 | 520 | 3 => $this->getAuthStream(), // child reads from fd#3 |
|
62 | 260 | 4 => ['pipe', 'r'], // child reads from fd#4 |
|
63 | 260 | 5 => ['pipe', 'w'] // child writes to fd#5 |
|
64 | 260 | ]; |
|
65 | |||
66 | 520 | setlocale(LC_ALL, Server::LOCALE); |
|
67 | 520 | $env = array_merge($this->env, [ |
|
68 | 520 | 'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!! |
|
69 | 260 | 'LC_ALL' => Server::LOCALE, |
|
70 | 260 | 'LANG' => Server::LOCALE, |
|
71 | 'COLUMNS' => 8192 // prevent smbclient from line-wrapping it's output |
||
72 | 260 | ]); |
|
73 | 520 | $this->process = proc_open($this->command, $descriptorSpec, $this->pipes, '/', $env); |
|
74 | 520 | if (!$this->isValid()) { |
|
75 | throw new ConnectionException(); |
||
76 | } |
||
77 | 520 | $this->connected = true; |
|
78 | 520 | } |
|
79 | |||
80 | /** |
||
81 | * check if the connection is still active |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 520 | public function isValid() { |
|
93 | |||
94 | /** |
||
95 | * send input to the process |
||
96 | * |
||
97 | * @param string $input |
||
98 | */ |
||
99 | 512 | public function write($input) { |
|
103 | |||
104 | /** |
||
105 | * read a line of output |
||
106 | * |
||
107 | * @return string|false |
||
108 | */ |
||
109 | 520 | public function readLine() { |
|
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 | 10 | public function readAll() { |
|
134 | |||
135 | 512 | public function getInputStream() { |
|
138 | |||
139 | 520 | public function getOutputStream() { |
|
142 | |||
143 | public function getErrorStream() { |
||
146 | |||
147 | 520 | public function getAuthStream() { |
|
150 | |||
151 | 32 | public function getFileInputStream() { |
|
154 | |||
155 | 32 | public function getFileOutputStream() { |
|
158 | |||
159 | 520 | public function writeAuthentication($user, $password) { |
|
167 | |||
168 | 520 | public function close($terminate = true) { |
|
189 | |||
190 | 2 | public function reconnect() { |
|
194 | |||
195 | 520 | public function __destruct() { |
|
198 | } |
||
199 |