Skip to content

Commit eda3870

Browse files
committed
move backends into their own namespace and add support for multiple auth methods
1 parent 2280570 commit eda3870

33 files changed

+750
-375
lines changed

README.md

+23-72
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,50 @@ PHP wrapper for `smbclient` and [`libsmbclient-php`](https://github.com/eduardok
1616
Examples
1717
----
1818

19-
### Upload a file ###
19+
### Connect to a share ###
2020

2121
```php
2222
<?php
23-
use Icewind\SMB\Server;
23+
use Icewind\SMB\ServerFactory;
24+
use Icewind\SMB\BasicAuth;
2425

2526
require('vendor/autoload.php');
2627

27-
$fileToUpload = __FILE__;
28+
$serverFactory = new ServerFactory();
29+
$auth = new BasicAuth('workgroup\test', 'test');
30+
$server = $serverFactory->createServer('localhost', $auth);
2831

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

34-
### Download a file ###
35+
The server factory will automatically pick between the `smbclient` and `libsmbclient-php`
36+
based backend depending on what is available.
37+
38+
### Using kerberos authentication ###
3539

3640
```php
37-
<?php
38-
use Icewind\SMB\Server;
41+
$serverFactory = new ServerFactory();
42+
$auth = new KerberosAuth();
43+
$server = $serverFactory->createServer('localhost', $auth);
44+
```
3945

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

42-
$target = __DIR__ . '/target.txt';
48+
### Upload a file ###
4349

44-
$server = new Server('localhost', 'test', 'test');
45-
$share = $server->getShare('test');
46-
$share->get('example.txt', $target);
50+
```php
51+
$share->put($fileToUpload, 'example.txt');
4752
```
4853

49-
### List shares on the remote server ###
54+
### Download a file ###
5055

5156
```php
52-
<?php
53-
use Icewind\SMB\Server;
57+
$share->get('example.txt', $target);
58+
```
5459

55-
require('vendor/autoload.php');
60+
### List shares on the remote server ###
5661

57-
$server = new Server('localhost', 'test', 'test');
62+
```php
5863
$shares = $server->listShares();
5964

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

6772
```php
68-
<?php
69-
use Icewind\SMB\Server;
70-
71-
require('vendor/autoload.php');
72-
73-
$server = new Server('localhost', 'test', 'test');
74-
$share = $server->getShare('test');
7573
$content = $share->dir('test');
7674

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

8583
```php
86-
<?php
87-
use Icewind\SMB\Server;
88-
89-
require('vendor/autoload.php');
90-
91-
$server = new Server('localhost', 'test', 'test');
92-
$share = $server->getShare('test');
93-
9484
$fh = $share->read('test.txt');
9585
echo fread($fh, 4086);
9686
fclose($fh);
@@ -99,53 +89,14 @@ fclose($fh);
9989
### Using write streams
10090

10191
```php
102-
<?php
103-
use Icewind\SMB\Server;
104-
105-
require('vendor/autoload.php');
106-
107-
$server = new Server('localhost', 'test', 'test');
108-
$share = $server->getShare('test');
109-
11092
$fh = $share->write('test.txt');
11193
fwrite($fh, 'bar');
11294
fclose($fh);
11395
```
11496

115-
### Using libsmbclient-php ###
116-
117-
Install [libsmbclient-php](https://github.com/eduardok/libsmbclient-php)
118-
119-
```php
120-
<?php
121-
use Icewind\SMB\Server;
122-
use Icewind\SMB\NativeServer;
123-
124-
require('vendor/autoload.php');
125-
126-
$fileToUpload = __FILE__;
127-
128-
if (Server::NativeAvailable()) {
129-
$server = new NativeServer('localhost', 'test', 'test');
130-
} else {
131-
echo 'libsmbclient-php not available, falling back to wrapping smbclient';
132-
$server = new Server('localhost', 'test', 'test');
133-
}
134-
$share = $server->getShare('test');
135-
$share->put($fileToUpload, 'example.txt');
136-
```
137-
13897
### Using notify
13998

14099
```php
141-
<?php
142-
use Icewind\SMB\Server;
143-
144-
require('vendor/autoload.php');
145-
146-
$server = new Server('localhost', 'test', 'test');
147-
$share = $server->getShare('test');
148-
149100
$share->notify('')->listen(function (\Icewind\SMB\Change $change) {
150101
echo $change->getCode() . ': ' . $change->getPath() . "\n";
151102
});

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"icewind/streams": ">=0.2.0"
1414
},
1515
"require-dev": {
16-
"phpunit/phpunit": "^4.8"
16+
"phpunit/phpunit": "^5.7"
1717
},
1818
"autoload" : {
1919
"psr-4": {

example.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
require('vendor/autoload.php');
77

88
$host = 'localhost';
9-
$user = 'test';
9+
$user = 'test\test';
1010
$password = 'test';
1111
$share = 'test';
1212

13-
if (Server::NativeAvailable()) {
14-
$server = new NativeServer($host, $user, $password);
15-
} else {
16-
$server = new Server($host, $user, $password);
17-
}
13+
$auth = new \Icewind\SMB\BasicAuth($user, $password);
14+
$serverFactory = new \Icewind\SMB\ServerFactory();
15+
16+
$server = $serverFactory->createServer($host, $auth);
1817

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

src/AbstractServer.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace Icewind\SMB;
23+
24+
25+
abstract class AbstractServer implements IServer {
26+
const LOCALE = 'en_US.UTF-8';
27+
28+
/**
29+
* @var string $host
30+
*/
31+
protected $host;
32+
33+
/**
34+
* @var IAuth $user
35+
*/
36+
protected $auth;
37+
38+
/**
39+
* @var \Icewind\SMB\System
40+
*/
41+
protected $system;
42+
43+
/**
44+
* @var TimeZoneProvider
45+
*/
46+
protected $timezoneProvider;
47+
48+
/**
49+
* @param string $host
50+
* @param IAuth $auth
51+
* @param System $system
52+
* @param TimeZoneProvider $timeZoneProvider
53+
*/
54+
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
55+
$this->host = $host;
56+
$this->auth = $auth;
57+
$this->system = $system;
58+
$this->timezoneProvider = $timeZoneProvider;
59+
}
60+
61+
/**
62+
* @return IAuth
63+
*/
64+
public function getAuth() {
65+
return $this->auth;
66+
}
67+
68+
/**
69+
* return string
70+
*/
71+
public function getHost() {
72+
return $this->host;
73+
}
74+
75+
/**
76+
* @return string
77+
*/
78+
public function getTimeZone() {
79+
return $this->timezoneProvider->get();
80+
}
81+
82+
public function getSystem() {
83+
return $this->system;
84+
}
85+
}

src/BasicAuth.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4+
*
5+
* @license GNU AGPL version 3 or any later version
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
namespace Icewind\SMB;
23+
24+
25+
class BasicAuth implements IAuth {
26+
/** @var string */
27+
private $username;
28+
/** @var string */
29+
private $workgroup;
30+
/** @var string */
31+
private $password;
32+
33+
/**
34+
* BasicAuth constructor.
35+
*
36+
* @param string $username
37+
* @param string $password
38+
*/
39+
public function __construct($username, $password) {
40+
list($workgroup, $username) = $this->splitUser($username);
41+
$this->username = $username;
42+
$this->workgroup = $workgroup;
43+
$this->password = $password;
44+
}
45+
46+
/**
47+
* Split workgroup from username
48+
*
49+
* @param $user
50+
* @return string[] [$workgroup, $user]
51+
*/
52+
private function splitUser($user) {
53+
if (strpos($user, '/')) {
54+
return explode('/', $user, 2);
55+
} elseif (strpos($user, '\\')) {
56+
return explode('\\', $user);
57+
} else {
58+
return array(null, $user);
59+
}
60+
}
61+
62+
public function getUsername() {
63+
return $this->username;
64+
}
65+
66+
public function getWorkgroup() {
67+
return $this->workgroup;
68+
}
69+
70+
public function getPassword() {
71+
return $this->password;
72+
}
73+
74+
public function getExtraCommandLineArguments() {
75+
return ($this->workgroup) ? '-W ' . escapeshellarg($this->workgroup) : '';
76+
}
77+
78+
public function setExtraSmbClientOptions($smbClientState) {
79+
// noop
80+
}
81+
82+
}

0 commit comments

Comments
 (0)