• Stars
    star
    125
  • Rank 285,489 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP wrapper for smbclient

SMB

CI codecov

PHP wrapper for smbclient and libsmbclient-php

  • 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.

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:

  • 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.

The ticket needs to be available in the environment of the php process.

$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.

The setup for such a system is fairly involved and requires roughly the following this

  • 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)

$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.

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.

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

Testing SMB

Use the following steps to check if the library can connect to your SMB share.

  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.

More Repositories

1

files_markdown

Nextcloud markdown editor
TypeScript
286
star
2

files_external_dropbox

External Storage backend for Dropbox
PHP
56
star
3

files_snapshots

Access filesystem snapshots from Nextcloud
JavaScript
32
star
4

files_inotify

Adds support detecting changes in local external storages with occ files_external:notify
PHP
28
star
5

mx-puppet-steam

Matrix <-> Steam puppeting bridge based on mx-puppet-bridge
TypeScript
27
star
6

interceptor

Intercept php includes
PHP
16
star
7

bitbuffer

Reading and writing data types of arbitrary bit length that might not be byte-aligned
Rust
15
star
8

dxt.js

DXT (de)compression in pure javascript
C++
9
star
9

group_default_quota

Set default user quotas for group members
PHP
8
star
10

Streams

A set of generic stream wrappers
PHP
7
star
11

clipboard-sync

Syncronize clipboard between computers
Rust
6
star
12

smb_test

Nextcloud app to debug smb connections
PHP
6
star
13

vbsp

Rust parser for valve bsp files
Rust
6
star
14

prometheus-mdns-rs

mDNS service discovery for prometherus
Rust
5
star
15

reveal

Reveal.js app for ownCloud
JavaScript
5
star
16

xtensa-lx106-rt

Rust
4
star
17

ivory

Writing php extensions in rust made easy
Rust
4
star
18

root_cache_cleaner

Removes duplicate filecache entry from the root filecache.
PHP
4
star
19

vrc-invite-bot

VRChat bot to automate sending invites
Rust
4
star
20

SearchDAV

A sabre/dav plugin to implement rfc5323 SEARCH
PHP
4
star
21

warp-real-ip

Warp filter to get the "real ip" of the remote client
Rust
4
star
22

rss-webhook-trigger

Trigger webhooks from rss feeds
Rust
3
star
23

notify-redis

push filesystem notifications into a redis list
Rust
3
star
24

steam-vent

Interact with the Steam network via rust
Rust
3
star
25

tasproxy

Auto-discovery reverse proxy for tasmota
Rust
3
star
26

vbspview

tf2 map viewer
Rust
2
star
27

files_external_azure

Azure backend for Nextcloud
PHP
2
star
28

php-nginx

Docker image with php using nginx and php-fpm
Dockerfile
2
star
29

blueprint

Automated population of Nextcloud data from templates
PHP
2
star
30

matrix-puppet-signal-docker

Docker container for matrix-puppet-signal
Dockerfile
2
star
31

haze

With a chance of clouds
Rust
2
star
32

files_notify_redis

Process filesystem change notifications pushed to redis
PHP
2
star
33

panic-write

Write panic messages to a core::fmt::Write and then halt
Rust
2
star
34

samba_vfs_notify_redis

vfs plugin to push update notifications to redis
C
2
star
35

archiso-builder

Docker image for building customized archiso images
Dockerfile
2
star
36

files_upload_mtime

Preserve the modified date of files uploaded trough the web interface.
JavaScript
2
star
37

nextcloud-dev

Dev docker image for nextCloud
Shell
1
star
38

Frontpage

frontpage crawler for reddit
JavaScript
1
star
39

contrast-rs

Calculate contrast between two colors
Rust
1
star
40

tf-log-parser

TF2 log parser with a focus on speed and flexibility.
Rust
1
star
41

oc-react-components

Base components for building an ownCloud app with reactjs
JavaScript
1
star
42

searchlight

Fuzzy search for Nextcloud/ownCloud using PostgreSQL
PHP
1
star
43

notify_push-client

A javascript client for notify_push events
TypeScript
1
star
44

mhz19-rs

Accessing MH-Z19 COâ‚‚ sensor over serial bus using rust
Rust
1
star
45

files_slow

Slow down storage backends for performance debugging
PHP
1
star
46

smbclient-builder

Mostly static smbclient builds for ubuntu 20.04
1
star
47

ugc-scaper

Scraper for ugcleague.com
HTML
1
star
48

tasmota-reset

Automatically reset tasmota devices when a sensor stalls
Rust
1
star
49

docker-hub-rss

RSS feed for Docker Hub images
Rust
1
star
50

oc-news-js

Javascript client for the ownCloud news app for node and the browser
JavaScript
1
star
51

files_user_cleanup

Allow cleanup of files of deleted users
PHP
1
star
52

shelve

Quick and easy file hosting
Rust
1
star
53

shortcutd

Global shortcuts using evdev
Rust
1
star
54

taspromto

Publish tasmota state into prometheus
Rust
1
star
55

react_oc_boilerplate

A basic boilerplate to demonstate how to build ownCloud apps using reactjs
JavaScript
1
star
56

vrc-network

TypeScript
1
star
57

userinfo

API to retrieve user information
PHP
1
star
58

db_slow

Slow down datsbase queries for performance debugging
PHP
1
star
59

lockpick

Debug transactional locking conflicts in Nextcloud
PHP
1
star
60

oc-notify

Watch the data directory of an ownCloud installation for changes.
JavaScript
1
star
61

mitemp-rs

Read Xiaomi MI Temperature and Humidity Sensor over BLE
Rust
1
star
62

xray

See what is happening inside the Nextcloud instance
PHP
1
star
63

group_everyone

Adds a virtual "Everyone" group to a Nextcloud instance
PHP
1
star
64

sm-sourcenav

Sourcemod extension for parsing .nav files
Rust
1
star
65

rfc7239

Parser for rfc7239 formatted Forwarded headers
Rust
1
star
66

sourcenav

parsing of SourceEngine .nav files
Rust
1
star
67

wp-news

News client for Windows Phone 8
C#
1
star