2017 © Pedro Peláez
 

library smb

php wrapper for smbclient and libsmbclient-php

image

icewind/smb

php wrapper for smbclient and libsmbclient-php

  • Friday, July 13, 2018
  • by icewind1991
  • Repository
  • 7 Watchers
  • 62 Stars
  • 23,614 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 24 Forks
  • 18 Open issues
  • 23 Versions
  • 20 % Grown

The README.md

Moved to https://codeberg.org/icewind/SMB

SMB

CI codecov, (*1)

PHP wrapper for smbclient and libsmbclient-php, (*2)

  • Reuses a single smbclient instance for multiple requests
  • Doesn't leak the password to the process list
  • Simple 1-on-1 mapping of SMB commands
  • A stream-based api to remove the need for temporary files
  • Support for using libsmbclient directly trough libsmbclient-php

Examples

Connect to a share

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

require('vendor/autoload.php');

$serverFactory = new ServerFactory();
$auth = new BasicAuth('user', 'workgroup', 'password');
$server = $serverFactory->createServer('localhost', $auth);

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

The server factory will automatically pick between the smbclient and libsmbclient-php based backend depending on what is available., (*3)

Using anonymous authentication

$serverFactory = new ServerFactory();
$auth = new AnonymousAuth();
$server = $serverFactory->createServer('localhost', $auth);

Using kerberos authentication

There are two ways of using kerberos to authenticate against the smb server:, (*4)

  • Using a ticket from the php server
  • Re-using a ticket send by the client

Using a server ticket

Using a server ticket allows the web server to authenticate against the smb server using an existing machine account., (*5)

The ticket needs to be available in the environment of the php process., (*6)

$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$server = $serverFactory->createServer('localhost', $auth);

Re-using a client ticket

By re-using a client ticket you can create a single sign-on setup where the user authenticates against the web service using kerberos. And the web server can forward that ticket to the smb server, allowing it to act on the behalf of the user without requiring the user to enter his password., (*7)

The setup for such a system is fairly involved and requires roughly the following this, (*8)

  • The web server is authenticated against kerberos with a machine account
  • Delegation is enabled for the web server's machine account
  • The web server is setup to perform kerberos authentication and save the ticket in it's environment
  • Php has the krb5 extension installed
  • The client authenticates using a ticket with forwarding enabled
$serverFactory = new ServerFactory();
$auth = new KerberosAuth();
$auth->setTicket(KerberosTicket::fromEnv());
$server = $serverFactory->createServer('localhost', $auth);

Upload a file

$share->put($fileToUpload, 'example.txt');

Download a file

$share->get('example.txt', $target);

List shares on the remote server

$shares = $server->listShares();

foreach ($shares as $share) {
    echo $share->getName() . "\n";
}

List the content of a folder

$content = $share->dir('test');

foreach ($content as $info) {
    echo $info->getName() . "\n";
    echo "\tsize :" . $info->getSize() . "\n";
}

Using read streams

$fh = $share->read('test.txt');
echo fread($fh, 4086);
fclose($fh);

Using write streams

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

Note: write() will truncate your file to 0bytes. You may open a writeable stream with append() which will point the cursor to the end of the file or create it if it does not exist yet. (append() is only compatible with libsmbclient-php), (*9)

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

Using notify

$share->notify('')->listen(function (\Icewind\SMB\Change $change) {
    echo $change->getCode() . ': ' . $change->getPath() . "\n";
});

Changing network timeouts

$options = new Options();
$options->setTimeout(5);
$serverFactory = new ServerFactory($options);

Setting protocol version

$options = new Options();
$options->setMinProtocol(IOptions::PROTOCOL_SMB2);
$options->setMaxProtocol(IOptions::PROTOCOL_SMB3);
$serverFactory = new ServerFactory($options);

Note, setting the protocol version is not supported with php-smbclient version 1.0.1 or lower., (*10)

Customizing system integration

The smbclient backend needs to get various information about the system it's running on to function such as the paths of various binaries or the system timezone. While the default logic for getting this information should work on most systems, it is possible to customize this behaviour., (*11)

In order to customize the integration you provide a custom implementation of ITimezoneProvider and/or ISystem and pass them as arguments to the ServerFactory., (*12)

Testing SMB

Use the following steps to check if the library can connect to your SMB share., (*13)

  1. Clone this repository or download the source as zip
  2. Make sure composer is installed
  3. Run composer install in the root of the repository
  4. Edit example.php with the relevant settings for your share.
  5. Run php example.php

If everything works correctly then the contents of the share should be outputted., (*14)

The Versions

13/07 2018

dev-master

9999999-dev

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

24/05 2018

v3.0.0

3.0.0.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2018

dev-stable2

dev-stable2

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2018

v2.0.5

2.0.5.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

19/01 2018

v2.0.4

2.0.4.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

18/10 2017

v2.0.3

2.0.3.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

16/08 2017

v2.0.2

2.0.2.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

24/04 2017

v2.0.1

2.0.1.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

13/12 2016

v2.0.0

2.0.0.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

08/12 2016

dev-notifyhandler

dev-notifyhandler

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

01/12 2016

v1.1.2

1.1.2.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

13/06 2016

v1.1.1

1.1.1.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2016

v1.1.0

1.1.0.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

17/03 2016

v1.0.8

1.0.8.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

17/03 2016

v1.0.7

1.0.7.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

16/03 2016

v1.0.6

1.0.6.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

20/01 2016

v1.0.5

1.0.5.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

17/08 2015

v1.0.4

1.0.4.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

13/08 2015

v1.0.3a

1.0.3.0-alpha

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

13/08 2015

v1.0.3

1.0.3.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

24/06 2015

1.0.2

1.0.2.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

20/04 2015

v1.0.1

1.0.1.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires

10/04 2015

v1.0.0

1.0.0.0

php wrapper for smbclient and libsmbclient-php

  Sources   Download

MIT

The Requires

 

The Development Requires