Skip to content

Commit

Permalink
move backends into their own namespace and add support for multiple a…
Browse files Browse the repository at this point in the history
…uth methods
  • Loading branch information
icewind1991 committed Mar 13, 2018
1 parent 2280570 commit eda3870
Show file tree
Hide file tree
Showing 33 changed files with 750 additions and 375 deletions.
95 changes: 23 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,50 @@ PHP wrapper for `smbclient` and [`libsmbclient-php`](https://github.com/eduardok
Examples
----

### Upload a file ###
### Connect to a share ###

```php
<?php
use Icewind\SMB\Server;
use Icewind\SMB\ServerFactory;
use Icewind\SMB\BasicAuth;

require('vendor/autoload.php');

$fileToUpload = __FILE__;
$serverFactory = new ServerFactory();
$auth = new BasicAuth('workgroup\test', 'test');
$server = $serverFactory->createServer('localhost', $auth);

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');
$share->put($fileToUpload, 'example.txt');
```

### Download a file ###
The server factory will automatically pick between the `smbclient` and `libsmbclient-php`
based backend depending on what is available.

### Using kerberos authentication ###

```php
<?php
use Icewind\SMB\Server;
$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$server = $serverFactory->createServer('localhost', $auth);
```

require('vendor/autoload.php');
Note that this requires a valid kerberos ticket to already be available for php

$target = __DIR__ . '/target.txt';
### Upload a file ###

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');
$share->get('example.txt', $target);
```php
$share->put($fileToUpload, 'example.txt');
```

### List shares on the remote server ###
### Download a file ###

```php
<?php
use Icewind\SMB\Server;
$share->get('example.txt', $target);
```

require('vendor/autoload.php');
### List shares on the remote server ###

$server = new Server('localhost', 'test', 'test');
```php
$shares = $server->listShares();

foreach ($shares as $share) {
Expand All @@ -65,13 +70,6 @@ foreach ($shares as $share) {
### List the content of a folder ###

```php
<?php
use Icewind\SMB\Server;

require('vendor/autoload.php');

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');
$content = $share->dir('test');

foreach ($content as $info) {
Expand All @@ -83,14 +81,6 @@ foreach ($content as $info) {
### Using read streams

```php
<?php
use Icewind\SMB\Server;

require('vendor/autoload.php');

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');

$fh = $share->read('test.txt');
echo fread($fh, 4086);
fclose($fh);
Expand All @@ -99,53 +89,14 @@ fclose($fh);
### Using write streams

```php
<?php
use Icewind\SMB\Server;

require('vendor/autoload.php');

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');

$fh = $share->write('test.txt');
fwrite($fh, 'bar');
fclose($fh);
```

### Using libsmbclient-php ###

Install [libsmbclient-php](https://github.com/eduardok/libsmbclient-php)

```php
<?php
use Icewind\SMB\Server;
use Icewind\SMB\NativeServer;

require('vendor/autoload.php');

$fileToUpload = __FILE__;

if (Server::NativeAvailable()) {
$server = new NativeServer('localhost', 'test', 'test');
} else {
echo 'libsmbclient-php not available, falling back to wrapping smbclient';
$server = new Server('localhost', 'test', 'test');
}
$share = $server->getShare('test');
$share->put($fileToUpload, 'example.txt');
```

### Using notify

```php
<?php
use Icewind\SMB\Server;

require('vendor/autoload.php');

$server = new Server('localhost', 'test', 'test');
$share = $server->getShare('test');

$share->notify('')->listen(function (\Icewind\SMB\Change $change) {
echo $change->getCode() . ': ' . $change->getPath() . "\n";
});
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"icewind/streams": ">=0.2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^5.7"
},
"autoload" : {
"psr-4": {
Expand Down
11 changes: 5 additions & 6 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
require('vendor/autoload.php');

$host = 'localhost';
$user = 'test';
$user = 'test\test';
$password = 'test';
$share = 'test';

if (Server::NativeAvailable()) {
$server = new NativeServer($host, $user, $password);
} else {
$server = new Server($host, $user, $password);
}
$auth = new \Icewind\SMB\BasicAuth($user, $password);
$serverFactory = new \Icewind\SMB\ServerFactory();

$server = $serverFactory->createServer($host, $auth);

$share = $server->getShare($share);

Expand Down
85 changes: 85 additions & 0 deletions src/AbstractServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;


abstract class AbstractServer implements IServer {
const LOCALE = 'en_US.UTF-8';

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

/**
* @var IAuth $user
*/
protected $auth;

/**
* @var \Icewind\SMB\System
*/
protected $system;

/**
* @var TimeZoneProvider
*/
protected $timezoneProvider;

/**
* @param string $host
* @param IAuth $auth
* @param System $system
* @param TimeZoneProvider $timeZoneProvider
*/
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
$this->host = $host;
$this->auth = $auth;
$this->system = $system;
$this->timezoneProvider = $timeZoneProvider;
}

/**
* @return IAuth
*/
public function getAuth() {
return $this->auth;
}

/**
* return string
*/
public function getHost() {
return $this->host;
}

/**
* @return string
*/
public function getTimeZone() {
return $this->timezoneProvider->get();
}

public function getSystem() {
return $this->system;
}
}
82 changes: 82 additions & 0 deletions src/BasicAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Icewind\SMB;


class BasicAuth implements IAuth {
/** @var string */
private $username;
/** @var string */
private $workgroup;
/** @var string */
private $password;

/**
* BasicAuth constructor.
*
* @param string $username
* @param string $password
*/
public function __construct($username, $password) {
list($workgroup, $username) = $this->splitUser($username);
$this->username = $username;
$this->workgroup = $workgroup;
$this->password = $password;
}

/**
* Split workgroup from username
*
* @param $user
* @return string[] [$workgroup, $user]
*/
private function splitUser($user) {
if (strpos($user, '/')) {
return explode('/', $user, 2);
} elseif (strpos($user, '\\')) {
return explode('\\', $user);
} else {
return array(null, $user);
}
}

public function getUsername() {
return $this->username;
}

public function getWorkgroup() {
return $this->workgroup;
}

public function getPassword() {
return $this->password;
}

public function getExtraCommandLineArguments() {
return ($this->workgroup) ? '-W ' . escapeshellarg($this->workgroup) : '';
}

public function setExtraSmbClientOptions($smbClientState) {
// noop
}

}
Loading

0 comments on commit eda3870

Please sign in to comment.