mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
remove code duplication for listShares
This commit is contained in:
parent
9c50244e67
commit
b43c129ed9
4 changed files with 58 additions and 55 deletions
|
|
@ -17,6 +17,14 @@ class Connection extends RawConnection {
|
||||||
const DELIMITER = 'smb:';
|
const DELIMITER = 'smb:';
|
||||||
const DELIMITER_LENGTH = 4;
|
const DELIMITER_LENGTH = 4;
|
||||||
|
|
||||||
|
/** @var Parser */
|
||||||
|
private $parser;
|
||||||
|
|
||||||
|
public function __construct($command, Parser $parser, $env = array()) {
|
||||||
|
parent::__construct($command, $env);
|
||||||
|
$this->parser = $parser;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send input to smbclient
|
* send input to smbclient
|
||||||
*
|
*
|
||||||
|
|
@ -42,7 +50,7 @@ class Connection extends RawConnection {
|
||||||
throw new ConnectionException('Connection not valid');
|
throw new ConnectionException('Connection not valid');
|
||||||
}
|
}
|
||||||
$promptLine = $this->readLine(); //first line is prompt
|
$promptLine = $this->readLine(); //first line is prompt
|
||||||
$this->checkConnectionError($promptLine);
|
$this->parser->checkConnectionError($promptLine);
|
||||||
|
|
||||||
$output = array();
|
$output = array();
|
||||||
$line = $this->readLine();
|
$line = $this->readLine();
|
||||||
|
|
@ -90,33 +98,6 @@ class Connection extends RawConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* check if the first line holds a connection failure
|
|
||||||
*
|
|
||||||
* @param $line
|
|
||||||
* @throws AuthenticationException
|
|
||||||
* @throws InvalidHostException
|
|
||||||
* @throws NoLoginServerException
|
|
||||||
*/
|
|
||||||
private function checkConnectionError($line) {
|
|
||||||
$line = rtrim($line, ')');
|
|
||||||
if (substr($line, -23) === ErrorCodes::LogonFailure) {
|
|
||||||
throw new AuthenticationException('Invalid login');
|
|
||||||
}
|
|
||||||
if (substr($line, -26) === ErrorCodes::BadHostName) {
|
|
||||||
throw new InvalidHostException('Invalid hostname');
|
|
||||||
}
|
|
||||||
if (substr($line, -22) === ErrorCodes::Unsuccessful) {
|
|
||||||
throw new InvalidHostException('Connection unsuccessful');
|
|
||||||
}
|
|
||||||
if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
|
|
||||||
throw new InvalidHostException('Connection refused');
|
|
||||||
}
|
|
||||||
if (substr($line, -26) === ErrorCodes::NoLogonServers) {
|
|
||||||
throw new NoLoginServerException('No login server');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function close($terminate = true) {
|
public function close($terminate = true) {
|
||||||
if (is_resource($this->getInputStream())) {
|
if (is_resource($this->getInputStream())) {
|
||||||
$this->write('close' . PHP_EOL);
|
$this->write('close' . PHP_EOL);
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,13 @@ namespace Icewind\SMB;
|
||||||
|
|
||||||
use Icewind\SMB\Exception\AccessDeniedException;
|
use Icewind\SMB\Exception\AccessDeniedException;
|
||||||
use Icewind\SMB\Exception\AlreadyExistsException;
|
use Icewind\SMB\Exception\AlreadyExistsException;
|
||||||
|
use Icewind\SMB\Exception\AuthenticationException;
|
||||||
use Icewind\SMB\Exception\Exception;
|
use Icewind\SMB\Exception\Exception;
|
||||||
use Icewind\SMB\Exception\FileInUseException;
|
use Icewind\SMB\Exception\FileInUseException;
|
||||||
|
use Icewind\SMB\Exception\InvalidHostException;
|
||||||
use Icewind\SMB\Exception\InvalidResourceException;
|
use Icewind\SMB\Exception\InvalidResourceException;
|
||||||
use Icewind\SMB\Exception\InvalidTypeException;
|
use Icewind\SMB\Exception\InvalidTypeException;
|
||||||
|
use Icewind\SMB\Exception\NoLoginServerException;
|
||||||
use Icewind\SMB\Exception\NotEmptyException;
|
use Icewind\SMB\Exception\NotEmptyException;
|
||||||
use Icewind\SMB\Exception\NotFoundException;
|
use Icewind\SMB\Exception\NotFoundException;
|
||||||
|
|
||||||
|
|
@ -72,6 +75,33 @@ class Parser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if the first line holds a connection failure
|
||||||
|
*
|
||||||
|
* @param $line
|
||||||
|
* @throws AuthenticationException
|
||||||
|
* @throws InvalidHostException
|
||||||
|
* @throws NoLoginServerException
|
||||||
|
*/
|
||||||
|
public function checkConnectionError($line) {
|
||||||
|
$line = rtrim($line, ')');
|
||||||
|
if (substr($line, -23) === ErrorCodes::LogonFailure) {
|
||||||
|
throw new AuthenticationException('Invalid login');
|
||||||
|
}
|
||||||
|
if (substr($line, -26) === ErrorCodes::BadHostName) {
|
||||||
|
throw new InvalidHostException('Invalid hostname');
|
||||||
|
}
|
||||||
|
if (substr($line, -22) === ErrorCodes::Unsuccessful) {
|
||||||
|
throw new InvalidHostException('Connection unsuccessful');
|
||||||
|
}
|
||||||
|
if (substr($line, -28) === ErrorCodes::ConnectionRefused) {
|
||||||
|
throw new InvalidHostException('Connection refused');
|
||||||
|
}
|
||||||
|
if (substr($line, -26) === ErrorCodes::NoLogonServers) {
|
||||||
|
throw new NoLoginServerException('No login server');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function parseMode($mode) {
|
public function parseMode($mode) {
|
||||||
$result = 0;
|
$result = 0;
|
||||||
$modeStrings = array(
|
$modeStrings = array(
|
||||||
|
|
@ -135,4 +165,17 @@ class Parser {
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function parseListShares($output) {
|
||||||
|
$shareNames = array();
|
||||||
|
foreach ($output as $line) {
|
||||||
|
if (strpos($line, '|')) {
|
||||||
|
list($type, $name, $description) = explode('|', $line);
|
||||||
|
if (strtolower($type) === 'disk') {
|
||||||
|
$shareNames[$name] = $description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $shareNames;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -135,32 +135,11 @@ class Server {
|
||||||
$connection = new RawConnection($command);
|
$connection = new RawConnection($command);
|
||||||
$connection->writeAuthentication($this->getUser(), $this->getPassword());
|
$connection->writeAuthentication($this->getUser(), $this->getPassword());
|
||||||
$output = $connection->readAll();
|
$output = $connection->readAll();
|
||||||
|
$parser = new Parser($this->timezoneProvider);
|
||||||
|
|
||||||
$line = $output[0];
|
$parser->checkConnectionError($output[0]);
|
||||||
|
|
||||||
$line = rtrim($line, ')');
|
$shareNames = $parser->parseListShares($output);
|
||||||
if (substr($line, -23) === ErrorCodes::LogonFailure) {
|
|
||||||
throw new AuthenticationException();
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
$shareNames = array();
|
|
||||||
foreach ($output as $line) {
|
|
||||||
if (strpos($line, '|')) {
|
|
||||||
list($type, $name, $description) = explode('|', $line);
|
|
||||||
if (strtolower($type) === 'disk') {
|
|
||||||
$shareNames[$name] = $description;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$shares = array();
|
$shares = array();
|
||||||
foreach ($shareNames as $name => $description) {
|
foreach ($shareNames as $name => $description) {
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ class Share extends AbstractShare {
|
||||||
System::getFD(3),
|
System::getFD(3),
|
||||||
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
||||||
);
|
);
|
||||||
$connection = new Connection($command);
|
$connection = new Connection($command, $this->parser);
|
||||||
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
||||||
if (!$connection->isValid()) {
|
if (!$connection->isValid()) {
|
||||||
throw new ConnectionException();
|
throw new ConnectionException();
|
||||||
|
|
@ -276,7 +276,7 @@ class Share extends AbstractShare {
|
||||||
System::getFD(3),
|
System::getFD(3),
|
||||||
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
||||||
);
|
);
|
||||||
$connection = new Connection($command);
|
$connection = new Connection($command, $this->parser);
|
||||||
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
||||||
$connection->write('get ' . $source . ' ' . System::getFD(5));
|
$connection->write('get ' . $source . ' ' . System::getFD(5));
|
||||||
$connection->write('exit');
|
$connection->write('exit');
|
||||||
|
|
@ -305,7 +305,7 @@ class Share extends AbstractShare {
|
||||||
System::getFD(3),
|
System::getFD(3),
|
||||||
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
escapeshellarg('//' . $this->server->getHost() . '/' . $this->name)
|
||||||
);
|
);
|
||||||
$connection = new Connection($command);
|
$connection = new Connection($command, $this->parser);
|
||||||
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
||||||
$fh = $connection->getFileInputStream();
|
$fh = $connection->getFileInputStream();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue