mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
split connection code
This commit is contained in:
parent
190ac9501d
commit
59bc57cc06
2 changed files with 107 additions and 60 deletions
|
|
@ -8,66 +8,20 @@
|
|||
|
||||
namespace SMB;
|
||||
|
||||
class Connection {
|
||||
|
||||
class Connection extends RawConnection {
|
||||
const DELIMITER = 'smb:';
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
$descriptorSpec = array(
|
||||
0 => array("pipe", "r"),
|
||||
1 => array("pipe", "w")
|
||||
);
|
||||
setlocale(LC_ALL, Server::LOCALE);
|
||||
$this->process = proc_open($command, $descriptorSpec, $this->pipes, null, array(
|
||||
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
|
||||
'LC_ALL' => Server::LOCALE
|
||||
));
|
||||
if (!$this->isValid()) {
|
||||
throw new ConnectionError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 smbclient
|
||||
*
|
||||
* @param string $input
|
||||
*/
|
||||
public function write($input) {
|
||||
fwrite($this->pipes[0], $input);
|
||||
fwrite($this->pipes[0], PHP_EOL); //make sure we have a recognizable delimiter
|
||||
fflush($this->pipes[0]);
|
||||
parent::write($input . PHP_EOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* get all unprocessed output from smbclient
|
||||
* get all unprocessed output from smbclient untill the next prompt
|
||||
*
|
||||
* @throws ConnectionError
|
||||
* @return array
|
||||
|
|
@ -76,15 +30,15 @@ class Connection {
|
|||
if (!$this->isValid()) {
|
||||
throw new ConnectionError();
|
||||
}
|
||||
$line = trim(fgets($this->pipes[1])); //first line is prompt
|
||||
$line = parent::read(); //first line is prompt
|
||||
$this->checkConnectionError($line);
|
||||
|
||||
$output = array();
|
||||
$line = fgets($this->pipes[1]);
|
||||
$line = parent::read();
|
||||
$length = strlen(self::DELIMITER);
|
||||
while (substr($line, 0, $length) !== self::DELIMITER) { //next prompt functions as delimiter
|
||||
$output[] .= $line;
|
||||
$line = fgets($this->pipes[1]);
|
||||
$line = parent::read();
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
|
@ -98,14 +52,16 @@ class Connection {
|
|||
*/
|
||||
private function checkConnectionError($line) {
|
||||
$line = rtrim($line, ')');
|
||||
$authError = 'NT_STATUS_LOGON_FAILURE';
|
||||
if (substr($line, -23) === $authError) {
|
||||
$this->pipes = array(null, null);
|
||||
if (substr($line, -23) === ErrorCodes::LogonFailure) {
|
||||
throw new AuthenticationException();
|
||||
}
|
||||
$addressError = 'NT_STATUS_BAD_NETWORK_NAME';
|
||||
if (substr($line, -26) === $addressError) {
|
||||
$this->pipes = array(null, null);
|
||||
if (substr($line, -26) === ErrorCodes::BadHostName) {
|
||||
throw new InvalidHostException();
|
||||
}
|
||||
if (substr($line, -22) === ErrorCodes::Unsuccessful) {
|
||||
throw new InvalidHostException();
|
||||
}
|
||||
if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
|
||||
throw new InvalidHostException();
|
||||
}
|
||||
}
|
||||
|
|
@ -116,7 +72,6 @@ class Connection {
|
|||
|
||||
public function __destruct() {
|
||||
$this->close();
|
||||
proc_terminate($this->process);
|
||||
proc_close($this->process);
|
||||
parent::__destruct();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue