improve support for workgroups/domains

This commit is contained in:
Robin Appelman 2015-08-13 16:17:46 +02:00
commit 33ab10cc4d
4 changed files with 43 additions and 19 deletions

View file

@ -24,12 +24,7 @@ class NativeServer extends Server {
} }
protected function connect() { protected function connect() {
$user = $this->getUser(); $this->state->init($this->getWorkgroup(), $this->getUser(), $this->getPassword());
$workgroup = null;
if (strpos($user, '/')) {
list($workgroup, $user) = explode('/', $user);
}
$this->state->init($workgroup, $user, $this->getPassword());
} }
/** /**

View file

@ -43,15 +43,7 @@ class NativeShare implements IShare {
return; return;
} }
$user = $this->server->getUser(); $this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword());
if (strpos($user, '/')) {
list($workgroup, $user) = explode('/', $user);
} elseif (strpos($user, '\\')) {
list($workgroup, $user) = explode('\\', $user);
} else {
$workgroup = null;
}
$this->state->init($workgroup, $user, $this->server->getPassword());
} }
/** /**

View file

@ -29,6 +29,11 @@ class Server {
*/ */
protected $password; protected $password;
/**
* @var string $workgroup
*/
protected $workgroup;
/** /**
* Check if the smbclient php extension is available * Check if the smbclient php extension is available
* *
@ -45,10 +50,28 @@ class Server {
*/ */
public function __construct($host, $user, $password) { public function __construct($host, $user, $password) {
$this->host = $host; $this->host = $host;
list($workgroup, $user) = $this->splitUser($user);
$this->user = $user; $this->user = $user;
$this->workgroup = $workgroup;
$this->password = $password; $this->password = $password;
} }
/**
* Split workgroup from username
*
* @param $user
* @return string[] [$workgroup, $user]
*/
public function splitUser($user) {
if (strpos($user, '/')) {
return explode('/', $user, 2);
} elseif (strpos($user, '\\')) {
return explode('\\', $user);
} else {
return [null, $user];
}
}
/** /**
* @return string * @return string
*/ */
@ -77,6 +100,13 @@ class Server {
return $this->host; return $this->host;
} }
/**
* @return string
*/
public function getWorkgroup() {
return $this->workgroup;
}
/** /**
* @return \Icewind\SMB\IShare[] * @return \Icewind\SMB\IShare[]
* *
@ -84,7 +114,8 @@ class Server {
* @throws \Icewind\SMB\Exception\InvalidHostException * @throws \Icewind\SMB\Exception\InvalidHostException
*/ */
public function listShares() { public function listShares() {
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' . $workgroupArgument = ($this->workgroup) ? ' -W ' . escapeshellarg($this->workgroup) : '';
$command = Server::CLIENT . $workgroupArgument . ' --authentication-file=/proc/self/fd/3' .
' -gL ' . escapeshellarg($this->getHost()); ' -gL ' . escapeshellarg($this->getHost());
$connection = new RawConnection($command); $connection = new RawConnection($command);
$connection->writeAuthentication($this->getUser(), $this->getPassword()); $connection->writeAuthentication($this->getUser(), $this->getPassword());

View file

@ -57,8 +57,10 @@ class Share implements IShare {
if ($this->connection and $this->connection->isValid()) { if ($this->connection and $this->connection->isValid()) {
return; return;
} }
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s', $workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s',
Server::CLIENT, Server::CLIENT,
$workgroupArgument,
$this->server->getHost(), $this->server->getHost(),
$this->name $this->name
); );
@ -260,8 +262,10 @@ class Share implements IShare {
$source = str_replace('\'', '\'"\'"\'', $source); $source = str_replace('\'', '\'"\'"\'', $source);
// since returned stream is closed by the caller we need to create a new instance // since returned stream is closed by the caller we need to create a new instance
// since we can't re-use the same file descriptor over multiple calls // since we can't re-use the same file descriptor over multiple calls
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'get %s /proc/self/fd/5\'', $workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s -c \'get %s /proc/self/fd/5\'',
Server::CLIENT, Server::CLIENT,
$workgroupArgument,
$this->server->getHost(), $this->server->getHost(),
$this->name, $this->name,
$source $source
@ -288,8 +292,10 @@ class Share implements IShare {
$target = str_replace('\'', '\'"\'"\'', $target); $target = str_replace('\'', '\'"\'"\'', $target);
// since returned stream is closed by the caller we need to create a new instance // since returned stream is closed by the caller we need to create a new instance
// since we can't re-use the same file descriptor over multiple calls // since we can't re-use the same file descriptor over multiple calls
$command = sprintf('%s --authentication-file=/proc/self/fd/3 //%s/%s -c \'put /proc/self/fd/4 %s\'', $workgroupArgument = ($this->server->getWorkgroup()) ? ' -W ' . escapeshellarg($this->server->getWorkgroup()) : '';
$command = sprintf('%s %s --authentication-file=/proc/self/fd/3 //%s/%s -c \'put /proc/self/fd/4 %s\'',
Server::CLIENT, Server::CLIENT,
$workgroupArgument,
$this->server->getHost(), $this->server->getHost(),
$this->name, $this->name,
$target $target