mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Add native implementation using the libsmbclient-php extensions
This commit is contained in:
parent
0520cba7dc
commit
3c67574fff
6 changed files with 460 additions and 9 deletions
66
src/NativeServer.php
Normal file
66
src/NativeServer.php
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Affero General Public License version 3 or
|
||||||
|
* later.
|
||||||
|
* See the COPYING-README file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\SMB;
|
||||||
|
|
||||||
|
class NativeServer extends Server {
|
||||||
|
/**
|
||||||
|
* @var resource
|
||||||
|
*/
|
||||||
|
protected $state;
|
||||||
|
|
||||||
|
protected function connect() {
|
||||||
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
|
||||||
|
$user = $this->getUser();
|
||||||
|
$workgroup = null;
|
||||||
|
if (strpos($user, '/')) {
|
||||||
|
list($workgroup, $user) = explode($user, '/');
|
||||||
|
}
|
||||||
|
$this->state = smbclient_state_new();
|
||||||
|
$result = smbclient_state_init($this->state, $workgroup, $this->getPassword(), $user);
|
||||||
|
if (!$result) {
|
||||||
|
throw new ConnectionError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Icewind\SMB\IShare[]
|
||||||
|
* @throws \Icewind\SMB\AuthenticationException
|
||||||
|
* @throws \Icewind\SMB\InvalidHostException
|
||||||
|
*/
|
||||||
|
public function listShares() {
|
||||||
|
$this->connect();
|
||||||
|
$shares = array();
|
||||||
|
$dh = smbclient_opendir($this->state, 'smb://' . $this->getHost());
|
||||||
|
while ($share = smbclient_readdir($this->state, $dh)) {
|
||||||
|
if ($share['type'] === 'file share') {
|
||||||
|
$shares[] = $this->getShare($share['name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
smbclient_closedir($this->state, $dh);
|
||||||
|
return $shares;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \Icewind\SMB\IShare
|
||||||
|
*/
|
||||||
|
public function getShare($name) {
|
||||||
|
return new NativeShare($this, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct() {
|
||||||
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
smbclient_state_free($this->state);
|
||||||
|
}
|
||||||
|
unset($this->state);
|
||||||
|
}
|
||||||
|
}
|
||||||
282
src/NativeShare.php
Normal file
282
src/NativeShare.php
Normal file
|
|
@ -0,0 +1,282 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Affero General Public License version 3 or
|
||||||
|
* later.
|
||||||
|
* See the COPYING-README file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\SMB;
|
||||||
|
|
||||||
|
require_once 'ErrorCodes.php';
|
||||||
|
|
||||||
|
class NativeShare implements IShare {
|
||||||
|
/**
|
||||||
|
* @var Server $server
|
||||||
|
*/
|
||||||
|
private $server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $name
|
||||||
|
*/
|
||||||
|
private $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var resource $state
|
||||||
|
*/
|
||||||
|
private $state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static $registed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Server $server
|
||||||
|
* @param string $name
|
||||||
|
* @throws ConnectionError
|
||||||
|
*/
|
||||||
|
public function __construct($server, $name) {
|
||||||
|
$this->server = $server;
|
||||||
|
$this->name = $name;
|
||||||
|
self::registerHandlers();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function registerHandlers() {
|
||||||
|
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
|
||||||
|
if (self::$registed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self::$registed = true;
|
||||||
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ConnectionError
|
||||||
|
*/
|
||||||
|
public function connect() {
|
||||||
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $this->server->getUser();
|
||||||
|
$workgroup = null;
|
||||||
|
if (strpos($user, '/')) {
|
||||||
|
list($workgroup, $user) = explode($user, '/');
|
||||||
|
}
|
||||||
|
$this->state = smbclient_state_new();
|
||||||
|
$result = smbclient_state_init($this->state, $workgroup, $this->server->getPassword(), $user);
|
||||||
|
if (!$result) {
|
||||||
|
throw new ConnectionError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the share
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getName() {
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildUrl($path) {
|
||||||
|
$url = 'smb://' . $this->server->getHost() . '/' . $this->name;
|
||||||
|
if ($path) {
|
||||||
|
$path = trim($path, '/');
|
||||||
|
$url .= '/' . $path;
|
||||||
|
}
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function errorHandler($errno, $errstr, $errfile, $errline, array $errcontext) {
|
||||||
|
if (strpos($errstr, 'Path does not exist') or strpos($errstr, 'path doesn\'t exist')) {
|
||||||
|
throw new NotFoundException($errstr);
|
||||||
|
} else if (strpos($errstr, 'already exists')) {
|
||||||
|
throw new AlreadyExistsException($errstr);
|
||||||
|
} else if (strpos($errstr, 'Can\'t write to a directory') or
|
||||||
|
strpos($errstr, 'use rmdir instead') or
|
||||||
|
$errno = 20 // 20: ENOTDIR
|
||||||
|
) {
|
||||||
|
throw new InvalidTypeException($errstr);
|
||||||
|
} else if (strpos($errstr, 'Workgroup not found') or
|
||||||
|
strpos($errstr, 'Workgroup or server not found')
|
||||||
|
) {
|
||||||
|
throw new InvalidHostException($errstr);
|
||||||
|
} else if (strpos($errstr, 'Permission denied')) {
|
||||||
|
throw new AccessDeniedException($errstr);
|
||||||
|
} else {
|
||||||
|
throw new \Exception($errstr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List the content of a remote folder
|
||||||
|
*
|
||||||
|
* Returns a nested array in the format of
|
||||||
|
* [
|
||||||
|
* $name => [
|
||||||
|
* 'size' => $size,
|
||||||
|
* 'type' => $type,
|
||||||
|
* 'time' => $mtime
|
||||||
|
* ],
|
||||||
|
* ...
|
||||||
|
* ]
|
||||||
|
*
|
||||||
|
* @param $path
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
|
public function dir($path) {
|
||||||
|
$this->connect();
|
||||||
|
$files = array();
|
||||||
|
|
||||||
|
$dh = smbclient_opendir($this->state, $this->buildUrl($path));
|
||||||
|
while ($file = smbclient_readdir($this->state, $dh)) {
|
||||||
|
$name = $file['name'];
|
||||||
|
if ($name !== '.' and $name !== '..') {
|
||||||
|
$stat = $this->stat($path . '/' . $name);
|
||||||
|
$files[$name] = array(
|
||||||
|
'type' => ($file['type'] === 'directory') ? 'dir' : 'file',
|
||||||
|
'size' => $stat['size'],
|
||||||
|
'time' => $stat['mtime']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
smbclient_closedir($this->state, $dh);
|
||||||
|
return $files;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function stat($path) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_stat($this->state, $this->buildUrl($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a folder on the share
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function mkdir($path) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_mkdir($this->state, $this->buildUrl($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a folder on the share
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function rmdir($path) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_rmdir($this->state, $this->buildUrl($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a file on the share
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function del($path) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_unlink($this->state, $this->buildUrl($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a remote file
|
||||||
|
*
|
||||||
|
* @param string $from
|
||||||
|
* @param string $to
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function rename($from, $to) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_rename($this->state, $this->buildUrl($from), $this->state, $this->buildUrl($to));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param string $mode
|
||||||
|
* @return resource
|
||||||
|
*/
|
||||||
|
protected function fopen($path, $mode) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_open($this->state, $this->buildUrl($path), $mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return resource
|
||||||
|
*/
|
||||||
|
protected function create($path) {
|
||||||
|
$this->connect();
|
||||||
|
return smbclient_creat($this->state, $this->buildUrl($path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload a local file
|
||||||
|
*
|
||||||
|
* @param string $source local file
|
||||||
|
* @param string $target remove file
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function put($source, $target) {
|
||||||
|
$sourceHandle = fopen($source, 'rb');
|
||||||
|
$targetHandle = $this->create($target);
|
||||||
|
|
||||||
|
while ($data = fread($sourceHandle, 4096)) {
|
||||||
|
smbclient_write($this->state, $targetHandle, $data);
|
||||||
|
}
|
||||||
|
return smbclient_close($this->state, $targetHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download a remote file
|
||||||
|
*
|
||||||
|
* @param string $source remove file
|
||||||
|
* @param string $target local file
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function get($source, $target) {
|
||||||
|
$sourceHandle = $this->fopen($source, 'r');
|
||||||
|
$targetHandle = fopen($target, 'wb');
|
||||||
|
|
||||||
|
while ($data = smbclient_read($this->state, $sourceHandle, 4096)) {
|
||||||
|
fwrite($targetHandle, $data);
|
||||||
|
}
|
||||||
|
return smbclient_close($this->state, $sourceHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a readable stream top a remote file
|
||||||
|
*
|
||||||
|
* @param string $source
|
||||||
|
* @return resource a read only stream with the contents of the remote file
|
||||||
|
*/
|
||||||
|
public function read($source) {
|
||||||
|
$handle = $this->fopen($source, 'r');
|
||||||
|
$context = stream_context_create(array(
|
||||||
|
'nativesmb' => array(
|
||||||
|
'state' => $this->state,
|
||||||
|
'handle' => $handle
|
||||||
|
)
|
||||||
|
));
|
||||||
|
return fopen('nativesmb://dummy', 'r', false, $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Server
|
||||||
|
*/
|
||||||
|
public function getServer() {
|
||||||
|
return $this->server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __destruct() {
|
||||||
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
smbclient_state_free($this->state);
|
||||||
|
}
|
||||||
|
unset($this->state);
|
||||||
|
}
|
||||||
|
}
|
||||||
64
src/NativeStream.php
Normal file
64
src/NativeStream.php
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Affero General Public License version 3 or
|
||||||
|
* later.
|
||||||
|
* See the COPYING-README file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\SMB;
|
||||||
|
|
||||||
|
class NativeStream {
|
||||||
|
public $context;
|
||||||
|
|
||||||
|
private $state;
|
||||||
|
|
||||||
|
private $handle;
|
||||||
|
|
||||||
|
public function stream_close() {
|
||||||
|
return smbclient_close($this->state, $this->handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_eof() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_flush() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||||
|
$context = stream_context_get_options($this->context);
|
||||||
|
if (isset($context['nativesmb'])) {
|
||||||
|
$context = $context['nativesmb'];
|
||||||
|
} else {
|
||||||
|
throw new \Exception('Invalid context');
|
||||||
|
}
|
||||||
|
if (isset($context['state']) and isset($context['handle'])) {
|
||||||
|
$this->state = $context['state'];
|
||||||
|
$this->handle = $context['handle'];
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new \Exception('Invalid context');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_read($count) {
|
||||||
|
return smbclient_read($this->state, $this->handle, $count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||||
|
return smbclient_lseek($this->state, $this->handle, $offset, $whence);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_stat() {
|
||||||
|
return smbclient_fstat($this->state, $this->handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_tell() {
|
||||||
|
return $this->stream_seek(0, SEEK_CUR);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_write($data) {
|
||||||
|
return smbclient_write($this->state, $this->handle, $data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,17 +15,26 @@ class Server {
|
||||||
/**
|
/**
|
||||||
* @var string $host
|
* @var string $host
|
||||||
*/
|
*/
|
||||||
private $host;
|
protected $host;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $user
|
* @var string $user
|
||||||
*/
|
*/
|
||||||
private $user;
|
protected $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $password
|
* @var string $password
|
||||||
*/
|
*/
|
||||||
private $password;
|
protected $password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the smblcient php extension is available
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function NativeAvailable() {
|
||||||
|
return function_exists('smbclient_state_new');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $host
|
* @param string $host
|
||||||
|
|
|
||||||
29
tests/nativeshare.php
Normal file
29
tests/nativeshare.php
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SMB\Test;
|
||||||
|
|
||||||
|
require_once 'share.php';
|
||||||
|
|
||||||
|
class NativeShare extends Share {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Icewind\SMB\NativeShare $share
|
||||||
|
*/
|
||||||
|
protected $share;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
if (!function_exists('smbclient_state_new')) {
|
||||||
|
$this->markTestSkipped('libsmbclient php extension not installed');
|
||||||
|
}
|
||||||
|
$this->config = json_decode(file_get_contents(__DIR__ . '/config.json'));
|
||||||
|
$this->server = new \Icewind\SMB\NativeServer($this->config->host, $this->config->user, $this->config->password);
|
||||||
|
$this->share = new \Icewind\SMB\NativeShare($this->server, $this->config->share);
|
||||||
|
if ($this->config->root) {
|
||||||
|
$this->root = '/' . $this->config->root . '/' . uniqid();
|
||||||
|
} else {
|
||||||
|
$this->root = '/' . uniqid();
|
||||||
|
}
|
||||||
|
$this->share->mkdir($this->root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,24 +6,24 @@ class Share extends \PHPUnit_Framework_TestCase {
|
||||||
/**
|
/**
|
||||||
* @var \Icewind\SMB\Server $server
|
* @var \Icewind\SMB\Server $server
|
||||||
*/
|
*/
|
||||||
private $server;
|
protected $server;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Icewind\SMB\Share $share
|
* @var \Icewind\SMB\Share $share
|
||||||
*/
|
*/
|
||||||
private $share;
|
protected $share;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $root
|
* @var string $root
|
||||||
*/
|
*/
|
||||||
private $root;
|
protected $root;
|
||||||
|
|
||||||
private $config;
|
protected $config;
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
$this->config = json_decode(file_get_contents(__DIR__ . '/config.json'));
|
$this->config = json_decode(file_get_contents(__DIR__ . '/config.json'));
|
||||||
$this->server = new \Icewind\SMB\Server($this->config->host, $this->config->user, $this->config->password);
|
$this->server = new \Icewind\SMB\Server($this->config->host, $this->config->user, $this->config->password);
|
||||||
$this->share = $this->server->getShare($this->config->share);
|
$this->share= new \Icewind\SMB\Share($this->server, $this->config->share);
|
||||||
if ($this->config->root) {
|
if ($this->config->root) {
|
||||||
$this->root = '/' . $this->config->root . '/' . uniqid();
|
$this->root = '/' . $this->config->root . '/' . uniqid();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -243,7 +243,7 @@ class Share extends \PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Icewind\SMB\NotFoundException
|
* @expectedException \Icewind\SMB\InvalidTypeException
|
||||||
*/
|
*/
|
||||||
public function testDelFolder() {
|
public function testDelFolder() {
|
||||||
$this->share->mkdir($this->root . '/foobar');
|
$this->share->mkdir($this->root . '/foobar');
|
||||||
|
|
@ -293,6 +293,7 @@ class Share extends \PHPUnit_Framework_TestCase {
|
||||||
$this->share->put($sourceFile, $this->root . '/foobar');
|
$this->share->put($sourceFile, $this->root . '/foobar');
|
||||||
$fh = $this->share->read($this->root . '/foobar');
|
$fh = $this->share->read($this->root . '/foobar');
|
||||||
$content = stream_get_contents($fh);
|
$content = stream_get_contents($fh);
|
||||||
|
fclose($fh);
|
||||||
$this->share->del($this->root . '/foobar');
|
$this->share->del($this->root . '/foobar');
|
||||||
|
|
||||||
$this->assertEquals(file_get_contents($sourceFile), $content);
|
$this->assertEquals(file_get_contents($sourceFile), $content);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue