Throw the proper exceptions when the connection can't be established

This commit is contained in:
Robin Appelman 2013-03-14 23:23:46 +01:00
commit 2ed803907f
4 changed files with 55 additions and 4 deletions

View file

@ -36,11 +36,25 @@ class Connection {
'CLI_FORCE_INTERACTIVE' => 'y', // Needed or the prompt isn't displayed!!
'LC_ALL' => Server::LOCALE
));
if (!is_resource($this->process)) {
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
*
@ -55,10 +69,16 @@ class Connection {
/**
* get all unprocessed output from smbclient
*
* @throws ConnectionError
* @return array
*/
public function read() {
fgets($this->pipes[1]); //first line is prompt
if (!$this->isValid()) {
throw new ConnectionError();
}
$line = trim(fgets($this->pipes[1])); //first line is prompt
$this->checkConnectionError($line);
$output = array();
$line = fgets($this->pipes[1]);
$length = strlen(self::DELIMITER);
@ -69,6 +89,27 @@ class Connection {
return $output;
}
/**
* check if the first line holds a connection failure
*
* @param $line
* @throws AuthenticationException
* @throws InvalidHostException
*/
private function checkConnectionError($line) {
$line = rtrim($line, ')');
$authError = 'NT_STATUS_LOGON_FAILURE';
if (substr($line, -23) === $authError) {
$this->pipes = array(null, null);
throw new AuthenticationException();
}
$addressError = 'NT_STATUS_BAD_NETWORK_NAME';
if (substr($line, -26) === $addressError) {
$this->pipes = array(null, null);
throw new InvalidHostException();
}
}
public function __destruct() {
proc_close($this->process);
}