Skip to content
Permalink
Browse files Browse the repository at this point in the history
improve support for workgroups/domains
  • Loading branch information
icewind1991 committed Aug 13, 2015
1 parent 25a7612 commit 33ab10c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
7 changes: 1 addition & 6 deletions src/NativeServer.php
Expand Up @@ -24,12 +24,7 @@ public function __construct($host, $user, $password) {
}

protected function connect() {
$user = $this->getUser();
$workgroup = null;
if (strpos($user, '/')) {
list($workgroup, $user) = explode('/', $user);
}
$this->state->init($workgroup, $user, $this->getPassword());
$this->state->init($this->getWorkgroup(), $this->getUser(), $this->getPassword());
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/NativeShare.php
Expand Up @@ -43,15 +43,7 @@ protected function connect() {
return;
}

$user = $this->server->getUser();
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());
$this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword());
}

/**
Expand Down
33 changes: 32 additions & 1 deletion src/Server.php
Expand Up @@ -29,6 +29,11 @@ class Server {
*/
protected $password;

/**
* @var string $workgroup
*/
protected $workgroup;

/**
* Check if the smbclient php extension is available
*
Expand All @@ -45,10 +50,28 @@ public static function NativeAvailable() {
*/
public function __construct($host, $user, $password) {
$this->host = $host;
list($workgroup, $user) = $this->splitUser($user);
$this->user = $user;
$this->workgroup = $workgroup;
$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
*/
Expand Down Expand Up @@ -77,14 +100,22 @@ public function getHost() {
return $this->host;
}

/**
* @return string
*/
public function getWorkgroup() {
return $this->workgroup;
}

/**
* @return \Icewind\SMB\IShare[]
*
* @throws \Icewind\SMB\Exception\AuthenticationException
* @throws \Icewind\SMB\Exception\InvalidHostException
*/
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());
$connection = new RawConnection($command);
$connection->writeAuthentication($this->getUser(), $this->getPassword());
Expand Down
12 changes: 9 additions & 3 deletions src/Share.php
Expand Up @@ -57,8 +57,10 @@ protected function connect() {
if ($this->connection and $this->connection->isValid()) {
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,
$workgroupArgument,
$this->server->getHost(),
$this->name
);
Expand Down Expand Up @@ -260,8 +262,10 @@ public function read($source) {
$source = str_replace('\'', '\'"\'"\'', $source);
// 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
$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,
$workgroupArgument,
$this->server->getHost(),
$this->name,
$source
Expand All @@ -288,8 +292,10 @@ public function write($target) {
$target = str_replace('\'', '\'"\'"\'', $target);
// 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
$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,
$workgroupArgument,
$this->server->getHost(),
$this->name,
$target
Expand Down

0 comments on commit 33ab10c

Please sign in to comment.