formatting

This commit is contained in:
Robin Appelman 2025-10-25 17:51:52 +02:00
commit 8af8d79df9
10 changed files with 115 additions and 74 deletions

View file

@ -6,6 +6,7 @@ end_of_line=lf
insert_final_newline=false insert_final_newline=false
indent_style=space indent_style=space
indent_size=4 indent_size=4
insert_final_newline=true
[{composer.lock,.babelrc,.stylelintrc,.eslintrc,jest.config,*.bowerrc,*.jsb3,*.jsb2,*.json}] [{composer.lock,.babelrc,.stylelintrc,.eslintrc,jest.config,*.bowerrc,*.jsb3,*.jsb2,*.json}]
indent_style=space indent_style=space
@ -15,7 +16,7 @@ indent_size=2
indent_style=tab indent_style=tab
tab_width=4 tab_width=4
[{*.module,*.hphp,*.phtml,*.php5,*.php4,*.php,*.inc}] [{*.module,*.hphp,*.phtml,*.php5,*.php4,*.php,*.phpstub,*.inc}]
indent_style=tab indent_style=tab
tab_width=4 tab_width=4

View file

@ -18,7 +18,7 @@ jobs:
- name: Setup PHP - name: Setup PHP
uses: https://github.com/shivammathur/setup-php@v2 uses: https://github.com/shivammathur/setup-php@v2
with: with:
php-version: '8.2' php-version: "8.2"
extensions: apcu extensions: apcu
- name: Composer - name: Composer
run: composer install run: composer install
@ -44,7 +44,9 @@ jobs:
env: env:
ACCOUNT_test: test ACCOUNT_test: test
UID_test: 1000 UID_test: 1000
SAMBA_VOLUME_CONFIG_test: "[test]; path=/tmp; valid users = test; guest ok = no; read only = no; browseable = yes" SAMBA_VOLUME_CONFIG_test:
"[test]; path=/tmp; valid users = test; guest ok = no; read only =
no; browseable = yes"
steps: steps:
- name: Install packages - name: Install packages
@ -71,7 +73,9 @@ jobs:
timeout_minutes: 2 timeout_minutes: 2
max_attempts: 3 max_attempts: 3
retry_on: timeout retry_on: timeout
command: php ./vendor/bin/phpunit tests -c tests/phpunit.xml --coverage-clover=coverage.xml command:
php ./vendor/bin/phpunit tests -c tests/phpunit.xml
--coverage-clover=coverage.xml
env: env:
BACKEND: smbclient BACKEND: smbclient
- name: PHPUnit Tests - libsmbclient - name: PHPUnit Tests - libsmbclient
@ -80,7 +84,9 @@ jobs:
timeout_minutes: 2 timeout_minutes: 2
max_attempts: 3 max_attempts: 3
retry_on: timeout retry_on: timeout
command: php ./vendor/bin/phpunit tests -c tests/phpunit.xml --coverage-clover=coverage.xml command:
php ./vendor/bin/phpunit tests -c tests/phpunit.xml
--coverage-clover=coverage.xml
env: env:
BACKEND: libsmbclient BACKEND: libsmbclient
- uses: https://github.com/codecov/codecov-action@v3 - uses: https://github.com/codecov/codecov-action@v3
@ -89,7 +95,9 @@ jobs:
smb-versions: smb-versions:
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
name: Unit tests - Samba ${{ matrix.server-version }} - smbclient ${{ matrix.client-version }} name:
Unit tests - Samba ${{ matrix.server-version }} - smbclient ${{
matrix.client-version }}
strategy: strategy:
fail-fast: false fail-fast: false
@ -115,7 +123,9 @@ jobs:
env: env:
ACCOUNT_test: test ACCOUNT_test: test
UID_test: 1000 UID_test: 1000
SAMBA_VOLUME_CONFIG_test: "[test]; path=/tmp; valid users = test; guest ok = no; read only = no; browseable = yes" SAMBA_VOLUME_CONFIG_test:
"[test]; path=/tmp; valid users = test; guest ok = no; read only =
no; browseable = yes"
steps: steps:
- name: Setup smbclient - name: Setup smbclient
@ -146,7 +156,9 @@ jobs:
timeout_minutes: 2 timeout_minutes: 2
max_attempts: 3 max_attempts: 3
retry_on: timeout retry_on: timeout
command: php ./vendor/bin/phpunit tests -c tests/phpunit.xml --coverage-clover=coverage.xml command:
php ./vendor/bin/phpunit tests -c tests/phpunit.xml
--coverage-clover=coverage.xml
env: env:
BACKEND: smbclient BACKEND: smbclient
- uses: https://github.com/codecov/codecov-action@v3 - uses: https://github.com/codecov/codecov-action@v3
@ -163,7 +175,9 @@ jobs:
env: env:
ACCOUNT_test: test ACCOUNT_test: test
UID_test: 1000 UID_test: 1000
SAMBA_VOLUME_CONFIG_test: "[test]; path=/tmp; valid users = test; guest ok = no; read only = no; browseable = yes" SAMBA_VOLUME_CONFIG_test:
"[test]; path=/tmp; valid users = test; guest ok = no; read only =
no; browseable = yes"
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@ -182,7 +196,9 @@ jobs:
timeout_minutes: 2 timeout_minutes: 2
max_attempts: 3 max_attempts: 3
retry_on: timeout retry_on: timeout
command: php ./vendor/bin/phpunit tests -c tests/phpunit.xml --coverage-clover=coverage.xml command:
php ./vendor/bin/phpunit tests -c tests/phpunit.xml
--coverage-clover=coverage.xml
env: env:
BACKEND: smbclient BACKEND: smbclient

View file

@ -10,6 +10,6 @@ jobs:
reuse-compliance-check: reuse-compliance-check:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: https://github.com/actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 - uses: https://github.com/actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: REUSE Compliance Check - name: REUSE Compliance Check
uses: https://github.com/fsfe/reuse-action@a46482ca367aef4454a87620aa37c2be4b2f8106 # v3.0.0 uses: https://github.com/fsfe/reuse-action@a46482ca367aef4454a87620aa37c2be4b2f8106 # v3.0.0

View file

@ -2,24 +2,25 @@
- SPDX-FileCopyrightText: 2014 Robin Appelman <robin@icewind.nl> - SPDX-FileCopyrightText: 2014 Robin Appelman <robin@icewind.nl>
- SPDX-License-Identifier: MIT - SPDX-License-Identifier: MIT
--> -->
SMB
=== # SMB
[![CI](https://github.com/icewind1991/SMB/actions/workflows/ci.yaml/badge.svg)](https://github.com/icewind1991/SMB/actions/workflows/ci.yaml) [![CI](https://github.com/icewind1991/SMB/actions/workflows/ci.yaml/badge.svg)](https://github.com/icewind1991/SMB/actions/workflows/ci.yaml)
[![codecov](https://codecov.io/gh/icewind1991/SMB/branch/master/graph/badge.svg?token=eTg0P466k6)](https://codecov.io/gh/icewind1991/SMB) [![codecov](https://codecov.io/gh/icewind1991/SMB/branch/master/graph/badge.svg?token=eTg0P466k6)](https://codecov.io/gh/icewind1991/SMB)
PHP wrapper for `smbclient` and [`libsmbclient-php`](https://github.com/eduardok/libsmbclient-php) PHP wrapper for `smbclient` and
[`libsmbclient-php`](https://github.com/eduardok/libsmbclient-php)
- Reuses a single `smbclient` instance for multiple requests - Reuses a single `smbclient` instance for multiple requests
- Doesn't leak the password to the process list - Doesn't leak the password to the process list
- Simple 1-on-1 mapping of SMB commands - Simple 1-on-1 mapping of SMB commands
- A stream-based api to remove the need for temporary files - A stream-based api to remove the need for temporary files
- Support for using libsmbclient directly trough [`libsmbclient-php`](https://github.com/eduardok/libsmbclient-php) - Support for using libsmbclient directly trough
[`libsmbclient-php`](https://github.com/eduardok/libsmbclient-php)
Examples ## Examples
----
### Connect to a share ### ### Connect to a share
```php ```php
<?php <?php
@ -35,10 +36,10 @@ $server = $serverFactory->createServer('localhost', $auth);
$share = $server->getShare('test'); $share = $server->getShare('test');
``` ```
The server factory will automatically pick between the `smbclient` and `libsmbclient-php` The server factory will automatically pick between the `smbclient` and
based backend depending on what is available. `libsmbclient-php` based backend depending on what is available.
### Using anonymous authentication ### ### Using anonymous authentication
```php ```php
$serverFactory = new ServerFactory(); $serverFactory = new ServerFactory();
@ -46,7 +47,7 @@ $auth = new AnonymousAuth();
$server = $serverFactory->createServer('localhost', $auth); $server = $serverFactory->createServer('localhost', $auth);
``` ```
### Using kerberos authentication ### ### Using kerberos authentication
There are two ways of using kerberos to authenticate against the smb server: There are two ways of using kerberos to authenticate against the smb server:
@ -55,7 +56,8 @@ There are two ways of using kerberos to authenticate against the smb server:
### Using a server ticket ### Using a server ticket
Using a server ticket allows the web server to authenticate against the smb server using an existing machine account. 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. The ticket needs to be available in the environment of the php process.
@ -67,15 +69,18 @@ $server = $serverFactory->createServer('localhost', $auth);
### Re-using a client ticket ### Re-using a client ticket
By re-using a client ticket you can create a single sign-on setup where the user authenticates against By re-using a client ticket you can create a single sign-on setup where the user
the web service using kerberos. And the web server can forward that ticket to the smb server, allowing it authenticates against the web service using kerberos. And the web server can
to act on the behalf of the user without requiring the user to enter his password. 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 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 - The web server is authenticated against kerberos with a machine account
- Delegation is enabled for the web server's 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 - The web server is setup to perform kerberos authentication and save the ticket
in it's environment
- Php has the krb5 extension installed - Php has the krb5 extension installed
- The client authenticates using a ticket with forwarding enabled - The client authenticates using a ticket with forwarding enabled
@ -86,36 +91,36 @@ $auth->setTicket(KerberosTicket::fromEnv());
$server = $serverFactory->createServer('localhost', $auth); $server = $serverFactory->createServer('localhost', $auth);
``` ```
### Upload a file ### ### Upload a file
```php ```php
$share->put($fileToUpload, 'example.txt'); $share->put($fileToUpload, 'example.txt');
``` ```
### Download a file ### ### Download a file
```php ```php
$share->get('example.txt', $target); $share->get('example.txt', $target);
``` ```
### List shares on the remote server ### ### List shares on the remote server
```php ```php
$shares = $server->listShares(); $shares = $server->listShares();
foreach ($shares as $share) { foreach ($shares as $share) {
echo $share->getName() . "\n"; echo $share->getName() . "\n";
} }
``` ```
### List the content of a folder ### ### List the content of a folder
```php ```php
$content = $share->dir('test'); $content = $share->dir('test');
foreach ($content as $info) { foreach ($content as $info) {
echo $info->getName() . "\n"; echo $info->getName() . "\n";
echo "\tsize :" . $info->getSize() . "\n"; echo "\tsize :" . $info->getSize() . "\n";
} }
``` ```
@ -135,20 +140,22 @@ fwrite($fh, 'bar');
fclose($fh); fclose($fh);
``` ```
**Note**: write() will truncate your file to 0bytes. You may open a writeable stream with append() which will point **Note**: write() will truncate your file to 0bytes. You may open a writeable
the cursor to the end of the file or create it if it does not exist yet. (append() is only compatible with libsmbclient-php) 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)
```php ```php
$fh = $share->append('test.txt'); $fh = $share->append('test.txt');
fwrite($fh, 'bar'); fwrite($fh, 'bar');
fclose($fh); fclose($fh);
``` ```
### Using notify ### Using notify
```php ```php
$share->notify('')->listen(function (\Icewind\SMB\Change $change) { $share->notify('')->listen(function (\Icewind\SMB\Change $change) {
echo $change->getCode() . ': ' . $change->getPath() . "\n"; echo $change->getCode() . ': ' . $change->getPath() . "\n";
}); });
``` ```
@ -169,24 +176,30 @@ $options->setMaxProtocol(IOptions::PROTOCOL_SMB3);
$serverFactory = new ServerFactory($options); $serverFactory = new ServerFactory($options);
``` ```
Note, setting the protocol version is not supported with php-smbclient version 1.0.1 or lower. Note, setting the protocol version is not supported with php-smbclient version
1.0.1 or lower.
### Customizing system integration ### Customizing system integration
The `smbclient` backend needs to get various information about the system it's running on to function The `smbclient` backend needs to get various information about the system it's
such as the paths of various binaries or the system timezone. running on to function such as the paths of various binaries or the system
While the default logic for getting this information should work on most systems, it is possible to customize this behaviour. 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`. 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 ## Testing SMB
Use the following steps to check if the library can connect to your SMB share. 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](https://github.com/icewind1991/SMB/archive/master.zip) 1. Clone this repository or download the source as
[zip](https://github.com/icewind1991/SMB/archive/master.zip)
2. Make sure [composer](https://getcomposer.org/) is installed 2. Make sure [composer](https://getcomposer.org/) is installed
3. Run `composer install` in the root of the repository 3. Run `composer install` in the root of the repository
4. Edit `example.php` with the relevant settings for your share. 4. Edit `example.php` with the relevant settings for your share.
5. Run `php example.php` 5. Run `php example.php`
If everything works correctly then the contents of the share should be outputted. If everything works correctly then the contents of the share should be
outputted.

View file

@ -1,7 +1,7 @@
{ {
"host": "skybox.icewind.link", "host": "skybox.icewind.link",
"user": "test", "user": "test",
"password": "test", "password": "test",
"share": "test", "share": "test",
"root": "test" "root": "test"
} }

View file

@ -6,12 +6,23 @@
*/ */
class KRB5CCache { class KRB5CCache {
public function getEntries(): array { return [];} public function getEntries(): array {
public function getName(): string {return "";} return [];
public function initKeytab(string $principal, string $keytab, array $flags = []): void {} }
public function initPassword(string $principal, string $password, array $flags = []): void {} public function getName(): string {
public function isValid(): bool {return false;} return "";
public function open(string $source): void {} }
public function save(string $destination): void {} public function initKeytab(string $principal, string $keytab, array $flags = []): void {
public function setConfig(string $destination): void {} }
public function initPassword(string $principal, string $password, array $flags = []): void {
}
public function isValid(): bool {
return false;
}
public function open(string $source): void {
}
public function save(string $destination): void {
}
public function setConfig(string $destination): void {
}
} }

View file

@ -4,12 +4,12 @@
- SPDX-License-Identifier: MIT - SPDX-License-Identifier: MIT
--> -->
<phpunit bootstrap="bootstrap.php"> <phpunit bootstrap="bootstrap.php">
<testsuite name='SMB'> <testsuite name='SMB'>
<directory suffix='.php'>./</directory> <directory suffix='.php'>./</directory>
</testsuite> </testsuite>
<filter> <filter>
<whitelist processUncoveredFilesFromWhitelist="true"> <whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory> <directory suffix=".php">../src</directory>
</whitelist> </whitelist>
</filter> </filter>
</phpunit> </phpunit>