mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
move backends into their own namespace and add support for multiple auth methods
This commit is contained in:
parent
2280570d28
commit
29bdebad42
33 changed files with 752 additions and 377 deletions
144
src/Native/NativeFileInfo.php
Normal file
144
src/Native/NativeFileInfo.php
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
use Icewind\SMB\IFileInfo;
|
||||
|
||||
class NativeFileInfo implements IFileInfo {
|
||||
const MODE_FILE = 0100000;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var NativeShare
|
||||
*/
|
||||
protected $share;
|
||||
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
protected $statCache;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $modeCache;
|
||||
|
||||
/**
|
||||
* @param NativeShare $share
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
* @param array $stat
|
||||
*/
|
||||
public function __construct($share, $path, $name, $stat = null) {
|
||||
$this->share = $share;
|
||||
$this->path = $path;
|
||||
$this->name = $name;
|
||||
$this->statCache = $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPath() {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function stat() {
|
||||
if (is_null($this->statCache)) {
|
||||
$this->statCache = $this->share->getStat($this->getPath());
|
||||
}
|
||||
return $this->statCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() {
|
||||
$stat = $this->stat();
|
||||
return $stat['size'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMTime() {
|
||||
$stat = $this->stat();
|
||||
return $stat['mtime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDirectory() {
|
||||
$stat = $this->stat();
|
||||
return !($stat['mode'] & self::MODE_FILE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function getMode() {
|
||||
if (!$this->modeCache) {
|
||||
$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
|
||||
// parse hex string
|
||||
$this->modeCache = (int)hexdec(substr($attribute, 2));
|
||||
}
|
||||
return $this->modeCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadOnly() {
|
||||
$mode = $this->getMode();
|
||||
return (bool)($mode & IFileInfo::MODE_READONLY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isHidden() {
|
||||
$mode = $this->getMode();
|
||||
return (bool)($mode & IFileInfo::MODE_HIDDEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSystem() {
|
||||
$mode = $this->getMode();
|
||||
return (bool)($mode & IFileInfo::MODE_SYSTEM);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isArchived() {
|
||||
$mode = $this->getMode();
|
||||
return (bool)($mode & IFileInfo::MODE_ARCHIVE);
|
||||
}
|
||||
}
|
||||
101
src/Native/NativeReadStream.php
Normal file
101
src/Native/NativeReadStream.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
/**
|
||||
* Stream optimized for read only usage
|
||||
*/
|
||||
class NativeReadStream extends NativeStream {
|
||||
const CHUNK_SIZE = 1048576; // 1MB chunks
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $readBuffer = null;
|
||||
|
||||
private $bufferSize = 0;
|
||||
|
||||
private $pos = 0;
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
$this->readBuffer = fopen('php://memory', 'r+');
|
||||
|
||||
return parent::stream_open($path, $mode, $options, $opened_path);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||
*
|
||||
* @param \Icewind\SMB\NativeState $state
|
||||
* @param resource $smbStream
|
||||
* @param string $mode
|
||||
* @param string $url
|
||||
* @return resource
|
||||
*/
|
||||
public static function wrap($state, $smbStream, $mode, $url) {
|
||||
stream_wrapper_register('nativesmb', NativeReadStream::class);
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $state,
|
||||
'handle' => $smbStream,
|
||||
'url' => $url
|
||||
)
|
||||
));
|
||||
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||
stream_wrapper_unregister('nativesmb');
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
// php reads 8192 bytes at once
|
||||
// however due to network latency etc, it's faster to read in larger chunks
|
||||
// and buffer the result
|
||||
if (!parent::stream_eof() && $this->bufferSize < $count) {
|
||||
$remaining = $this->readBuffer;
|
||||
$this->readBuffer = fopen('php://memory', 'r+');
|
||||
$this->bufferSize = 0;
|
||||
stream_copy_to_stream($remaining, $this->readBuffer);
|
||||
$this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE));
|
||||
fseek($this->readBuffer, 0);
|
||||
}
|
||||
|
||||
$result = fread($this->readBuffer, $count);
|
||||
$this->bufferSize -= $count;
|
||||
|
||||
$read = strlen($result);
|
||||
$this->pos += $read;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||
$result = parent::stream_seek($offset, $whence);
|
||||
if ($result) {
|
||||
$this->readBuffer = fopen('php://memory', 'r+');
|
||||
$this->bufferSize = 0;
|
||||
$this->pos = parent::stream_tell();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function stream_eof() {
|
||||
return $this->bufferSize <= 0 && parent::stream_eof();
|
||||
}
|
||||
|
||||
public function stream_tell() {
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
public function stream_write($data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function stream_truncate($size) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
71
src/Native/NativeServer.php
Normal file
71
src/Native/NativeServer.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
use Icewind\SMB\AbstractServer;
|
||||
use Icewind\SMB\IAuth;
|
||||
use Icewind\SMB\System;
|
||||
use Icewind\SMB\TimeZoneProvider;
|
||||
|
||||
class NativeServer extends AbstractServer {
|
||||
/**
|
||||
* @var NativeState
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @param IAuth $auth
|
||||
* @param System $system
|
||||
* @param TimeZoneProvider $timeZoneProvider
|
||||
*/
|
||||
public function __construct($host, IAuth $auth, System $system, TimeZoneProvider $timeZoneProvider) {
|
||||
parent::__construct($host, $auth, $system, $timeZoneProvider);
|
||||
$this->state = new NativeState();
|
||||
}
|
||||
|
||||
protected function connect() {
|
||||
$this->state->init($this->getAuth());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Icewind\SMB\IShare[]
|
||||
* @throws \Icewind\SMB\Exception\AuthenticationException
|
||||
* @throws \Icewind\SMB\Exception\InvalidHostException
|
||||
*/
|
||||
public function listShares() {
|
||||
$this->connect();
|
||||
$shares = array();
|
||||
$dh = $this->state->opendir('smb://' . $this->getHost());
|
||||
while ($share = $this->state->readdir($dh)) {
|
||||
if ($share['type'] === 'file share') {
|
||||
$shares[] = $this->getShare($share['name']);
|
||||
}
|
||||
}
|
||||
$this->state->closedir($dh);
|
||||
return $shares;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return \Icewind\SMB\IShare
|
||||
*/
|
||||
public function getShare($name) {
|
||||
return new NativeShare($this, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the smbclient php extension is available
|
||||
*
|
||||
* @param System $system
|
||||
* @return bool
|
||||
*/
|
||||
public static function available(System $system) {
|
||||
return function_exists('smbclient_state_new');
|
||||
}
|
||||
}
|
||||
314
src/Native/NativeShare.php
Normal file
314
src/Native/NativeShare.php
Normal file
|
|
@ -0,0 +1,314 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
use Icewind\SMB\AbstractShare;
|
||||
use Icewind\SMB\Exception\DependencyException;
|
||||
use Icewind\SMB\Exception\InvalidPathException;
|
||||
use Icewind\SMB\Exception\InvalidResourceException;
|
||||
use Icewind\SMB\INotifyHandler;
|
||||
use Icewind\SMB\IServer;
|
||||
use Icewind\SMB\Wrapped\Server;
|
||||
use Icewind\SMB\Wrapped\Share;
|
||||
|
||||
class NativeShare extends AbstractShare {
|
||||
/**
|
||||
* @var IServer $server
|
||||
*/
|
||||
private $server;
|
||||
|
||||
/**
|
||||
* @var string $name
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var NativeState $state
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @param IServer $server
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($server, $name) {
|
||||
parent::__construct();
|
||||
$this->server = $server;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Icewind\SMB\Exception\ConnectionException
|
||||
* @throws \Icewind\SMB\Exception\AuthenticationException
|
||||
* @throws \Icewind\SMB\Exception\InvalidHostException
|
||||
*/
|
||||
protected function getState() {
|
||||
if ($this->state and $this->state instanceof NativeState) {
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
$this->state = new NativeState();
|
||||
$this->state->init($this->server->getAuth());
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the share
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
private function buildUrl($path) {
|
||||
$this->verifyPath($path);
|
||||
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
|
||||
if ($path) {
|
||||
$path = trim($path, '/');
|
||||
$url .= '/';
|
||||
$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the content of a remote folder
|
||||
*
|
||||
* @param string $path
|
||||
* @return \Icewind\SMB\IFileInfo[]
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function dir($path) {
|
||||
$files = array();
|
||||
|
||||
$dh = $this->getState()->opendir($this->buildUrl($path));
|
||||
while ($file = $this->getState()->readdir($dh)) {
|
||||
$name = $file['name'];
|
||||
if ($name !== '.' and $name !== '..') {
|
||||
$files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
|
||||
}
|
||||
}
|
||||
|
||||
$this->getState()->closedir($dh);
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return \Icewind\SMB\IFileInfo
|
||||
*/
|
||||
public function stat($path) {
|
||||
return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
|
||||
}
|
||||
|
||||
public function getStat($path) {
|
||||
return $this->getState()->stat($this->buildUrl($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a folder on the share
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\AlreadyExistsException
|
||||
*/
|
||||
public function mkdir($path) {
|
||||
return $this->getState()->mkdir($this->buildUrl($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a folder on the share
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function rmdir($path) {
|
||||
return $this->getState()->rmdir($this->buildUrl($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a file on the share
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function del($path) {
|
||||
return $this->getState()->unlink($this->buildUrl($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename a remote file
|
||||
*
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\AlreadyExistsException
|
||||
*/
|
||||
public function rename($from, $to) {
|
||||
return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a local file
|
||||
*
|
||||
* @param string $source local file
|
||||
* @param string $target remove file
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function put($source, $target) {
|
||||
$sourceHandle = fopen($source, 'rb');
|
||||
$targetHandle = $this->getState()->create($this->buildUrl($target));
|
||||
|
||||
while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
|
||||
$this->getState()->write($targetHandle, $data);
|
||||
}
|
||||
$this->getState()->close($targetHandle);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a remote file
|
||||
*
|
||||
* @param string $source remove file
|
||||
* @param string $target local file
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
* @throws \Icewind\SMB\Exception\InvalidPathException
|
||||
* @throws \Icewind\SMB\Exception\InvalidResourceException
|
||||
*/
|
||||
public function get($source, $target) {
|
||||
if (!$target) {
|
||||
throw new InvalidPathException('Invalid target path: Filename cannot be empty');
|
||||
}
|
||||
$targetHandle = @fopen($target, 'wb');
|
||||
if (!$targetHandle) {
|
||||
$error = error_get_last();
|
||||
if (is_array($error)) {
|
||||
$reason = $error['message'];
|
||||
} else {
|
||||
$reason = 'Unknown error';
|
||||
}
|
||||
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
|
||||
}
|
||||
|
||||
$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
|
||||
if (!$sourceHandle) {
|
||||
fclose($targetHandle);
|
||||
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
|
||||
}
|
||||
|
||||
while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
|
||||
fwrite($targetHandle, $data);
|
||||
}
|
||||
$this->getState()->close($sourceHandle);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a readable stream top a remote file
|
||||
*
|
||||
* @param string $source
|
||||
* @return resource a read only stream with the contents of the remote file
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function read($source) {
|
||||
$url = $this->buildUrl($source);
|
||||
$handle = $this->getState()->open($url, 'r');
|
||||
return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a readable stream top a remote file
|
||||
*
|
||||
* @param string $source
|
||||
* @return resource a read only stream with the contents of the remote file
|
||||
*
|
||||
* @throws \Icewind\SMB\Exception\NotFoundException
|
||||
* @throws \Icewind\SMB\Exception\InvalidTypeException
|
||||
*/
|
||||
public function write($source) {
|
||||
$url = $this->buildUrl($source);
|
||||
$handle = $this->getState()->create($url);
|
||||
return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extended attributes for the path
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $attribute attribute to get the info
|
||||
* @return string the attribute value
|
||||
*/
|
||||
public function getAttribute($path, $attribute) {
|
||||
return $this->getState()->getxattr($this->buildUrl($path), $attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extended attributes for the path
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $attribute attribute to get the info
|
||||
* @param mixed $value
|
||||
* @return string the attribute value
|
||||
*/
|
||||
public function setAttribute($path, $attribute, $value) {
|
||||
|
||||
if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
|
||||
$value = '0x' . dechex($value);
|
||||
}
|
||||
|
||||
return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
|
||||
* @return mixed
|
||||
*/
|
||||
public function setMode($path, $mode) {
|
||||
return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return INotifyHandler
|
||||
*/
|
||||
public function notify($path) {
|
||||
// php-smbclient does support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
|
||||
// so we use the smbclient based backend for this
|
||||
if (!Server::available($this->server->getSystem())) {
|
||||
throw new DependencyException('smbclient not found in path for notify command');
|
||||
}
|
||||
$share = new Share($this->server, $this->getName());
|
||||
return $share->notify($path);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
unset($this->state);
|
||||
}
|
||||
}
|
||||
309
src/Native/NativeState.php
Normal file
309
src/Native/NativeState.php
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
use Icewind\SMB\Exception\AlreadyExistsException;
|
||||
use Icewind\SMB\Exception\ConnectionRefusedException;
|
||||
use Icewind\SMB\Exception\Exception;
|
||||
use Icewind\SMB\Exception\FileInUseException;
|
||||
use Icewind\SMB\Exception\ForbiddenException;
|
||||
use Icewind\SMB\Exception\HostDownException;
|
||||
use Icewind\SMB\Exception\InvalidArgumentException;
|
||||
use Icewind\SMB\Exception\InvalidTypeException;
|
||||
use Icewind\SMB\Exception\NoRouteToHostException;
|
||||
use Icewind\SMB\Exception\NotEmptyException;
|
||||
use Icewind\SMB\Exception\NotFoundException;
|
||||
use Icewind\SMB\Exception\OutOfSpaceException;
|
||||
use Icewind\SMB\Exception\TimedOutException;
|
||||
use Icewind\SMB\IAuth;
|
||||
|
||||
/**
|
||||
* Low level wrapper for libsmbclient-php with error handling
|
||||
*/
|
||||
class NativeState {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
protected $handlerSet = false;
|
||||
|
||||
protected $connected = false;
|
||||
|
||||
// see error.h
|
||||
const EXCEPTION_MAP = [
|
||||
1 => ForbiddenException::class,
|
||||
2 => NotFoundException::class,
|
||||
13 => ForbiddenException::class,
|
||||
16 => FileInUseException::class,
|
||||
17 => AlreadyExistsException::class,
|
||||
20 => InvalidTypeException::class,
|
||||
21 => InvalidTypeException::class,
|
||||
22 => InvalidArgumentException::class,
|
||||
28 => OutOfSpaceException::class,
|
||||
39 => NotEmptyException::class,
|
||||
110 => TimedOutException::class,
|
||||
111 => ConnectionRefusedException::class,
|
||||
112 => HostDownException::class,
|
||||
113 => NoRouteToHostException::class
|
||||
];
|
||||
|
||||
protected function handleError($path) {
|
||||
$error = smbclient_state_errno($this->state);
|
||||
if ($error === 0) {
|
||||
return;
|
||||
}
|
||||
throw Exception::fromMap(self::EXCEPTION_MAP, $error, $path);
|
||||
}
|
||||
|
||||
protected function testResult($result, $uri) {
|
||||
if ($result === false or $result === null) {
|
||||
// smb://host/share/path
|
||||
if (is_string($uri) && count(explode('/', $uri, 5)) > 4) {
|
||||
list(, , , , $path) = explode('/', $uri, 5);
|
||||
$path = '/' . $path;
|
||||
} else {
|
||||
$path = null;
|
||||
}
|
||||
$this->handleError($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IAuth $auth
|
||||
* @return bool
|
||||
*/
|
||||
public function init(IAuth $auth) {
|
||||
if ($this->connected) {
|
||||
return true;
|
||||
}
|
||||
$this->state = smbclient_state_new();
|
||||
smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false);
|
||||
$auth->setExtraSmbClientOptions($this->state);
|
||||
$result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
|
||||
|
||||
$this->testResult($result, '');
|
||||
$this->connected = true;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @return resource
|
||||
*/
|
||||
public function opendir($uri) {
|
||||
$result = @smbclient_opendir($this->state, $uri);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $dir
|
||||
* @return array
|
||||
*/
|
||||
public function readdir($dir) {
|
||||
$result = @smbclient_readdir($this->state, $dir);
|
||||
|
||||
$this->testResult($result, $dir);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $dir
|
||||
* @return bool
|
||||
*/
|
||||
public function closedir($dir) {
|
||||
$result = smbclient_closedir($this->state, $dir);
|
||||
|
||||
$this->testResult($result, $dir);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $old
|
||||
* @param string $new
|
||||
* @return bool
|
||||
*/
|
||||
public function rename($old, $new) {
|
||||
$result = @smbclient_rename($this->state, $old, $this->state, $new);
|
||||
|
||||
$this->testResult($result, $new);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
public function unlink($uri) {
|
||||
$result = @smbclient_unlink($this->state, $uri);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param int $mask
|
||||
* @return bool
|
||||
*/
|
||||
public function mkdir($uri, $mask = 0777) {
|
||||
$result = @smbclient_mkdir($this->state, $uri, $mask);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
public function rmdir($uri) {
|
||||
$result = @smbclient_rmdir($this->state, $uri);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @return array
|
||||
*/
|
||||
public function stat($uri) {
|
||||
$result = @smbclient_stat($this->state, $uri);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $file
|
||||
* @return array
|
||||
*/
|
||||
public function fstat($file) {
|
||||
$result = @smbclient_fstat($this->state, $file);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $mode
|
||||
* @param int $mask
|
||||
* @return resource
|
||||
*/
|
||||
public function open($uri, $mode, $mask = 0666) {
|
||||
$result = @smbclient_open($this->state, $uri, $mode, $mask);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param int $mask
|
||||
* @return resource
|
||||
*/
|
||||
public function create($uri, $mask = 0666) {
|
||||
$result = @smbclient_creat($this->state, $uri, $mask);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $file
|
||||
* @param int $bytes
|
||||
* @return string
|
||||
*/
|
||||
public function read($file, $bytes) {
|
||||
$result = @smbclient_read($this->state, $file, $bytes);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $file
|
||||
* @param string $data
|
||||
* @param int $length
|
||||
* @return int
|
||||
*/
|
||||
public function write($file, $data, $length = null) {
|
||||
$result = @smbclient_write($this->state, $file, $data, $length);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $file
|
||||
* @param int $offset
|
||||
* @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
|
||||
* @return int|bool new file offset as measured from the start of the file on success, false on failure.
|
||||
*/
|
||||
public function lseek($file, $offset, $whence = SEEK_SET) {
|
||||
$result = @smbclient_lseek($this->state, $file, $offset, $whence);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $file
|
||||
* @param int $size
|
||||
* @return bool
|
||||
*/
|
||||
public function ftruncate($file, $size) {
|
||||
$result = @smbclient_ftruncate($this->state, $file, $size);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function close($file) {
|
||||
$result = @smbclient_close($this->state, $file);
|
||||
|
||||
$this->testResult($result, $file);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
public function getxattr($uri, $key) {
|
||||
$result = @smbclient_getxattr($this->state, $uri, $key);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @param int $flags
|
||||
* @return mixed
|
||||
*/
|
||||
public function setxattr($uri, $key, $value, $flags = 0) {
|
||||
$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
|
||||
|
||||
$this->testResult($result, $uri);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
if ($this->connected) {
|
||||
smbclient_state_free($this->state);
|
||||
}
|
||||
}
|
||||
}
|
||||
127
src/Native/NativeStream.php
Normal file
127
src/Native/NativeStream.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
use Icewind\SMB\Exception\Exception;
|
||||
use Icewind\SMB\Exception\InvalidRequestException;
|
||||
use Icewind\Streams\File;
|
||||
|
||||
class NativeStream implements File {
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
public $context;
|
||||
|
||||
/**
|
||||
* @var NativeState
|
||||
*/
|
||||
protected $state;
|
||||
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
protected $handle;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $eof = false;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||
*
|
||||
* @param \Icewind\SMB\NativeState $state
|
||||
* @param resource $smbStream
|
||||
* @param string $mode
|
||||
* @param string $url
|
||||
* @return resource
|
||||
*/
|
||||
public static function wrap($state, $smbStream, $mode, $url) {
|
||||
stream_wrapper_register('nativesmb', NativeStream::class);
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $state,
|
||||
'handle' => $smbStream,
|
||||
'url' => $url
|
||||
)
|
||||
));
|
||||
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||
stream_wrapper_unregister('nativesmb');
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function stream_close() {
|
||||
return $this->state->close($this->handle);
|
||||
}
|
||||
|
||||
public function stream_eof() {
|
||||
return $this->eof;
|
||||
}
|
||||
|
||||
public function stream_flush() {
|
||||
}
|
||||
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
$context = stream_context_get_options($this->context);
|
||||
$this->state = $context['nativesmb']['state'];
|
||||
$this->handle = $context['nativesmb']['handle'];
|
||||
$this->url = $context['nativesmb']['url'];
|
||||
return true;
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
$result = $this->state->read($this->handle, $count);
|
||||
if (strlen($result) < $count) {
|
||||
$this->eof = true;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||
$this->eof = false;
|
||||
try {
|
||||
return $this->state->lseek($this->handle, $offset, $whence) !== false;
|
||||
} catch (InvalidRequestException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function stream_stat() {
|
||||
try {
|
||||
return $this->state->stat($this->url);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function stream_tell() {
|
||||
return $this->state->lseek($this->handle, 0, SEEK_CUR);
|
||||
}
|
||||
|
||||
public function stream_write($data) {
|
||||
return $this->state->write($this->handle, $data);
|
||||
}
|
||||
|
||||
public function stream_truncate($size) {
|
||||
return $this->state->ftruncate($this->handle, $size);
|
||||
}
|
||||
|
||||
public function stream_set_option($option, $arg1, $arg2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function stream_lock($operation) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
100
src/Native/NativeWriteStream.php
Normal file
100
src/Native/NativeWriteStream.php
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||
* This file is licensed under the Licensed under the MIT license:
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Icewind\SMB\Native;
|
||||
|
||||
/**
|
||||
* Stream optimized for write only usage
|
||||
*/
|
||||
class NativeWriteStream extends NativeStream {
|
||||
const CHUNK_SIZE = 1048576; // 1MB chunks
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $writeBuffer = null;
|
||||
|
||||
private $bufferSize = 0;
|
||||
|
||||
private $pos = 0;
|
||||
|
||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||
$this->writeBuffer = fopen('php://memory', 'r+');
|
||||
|
||||
return parent::stream_open($path, $mode, $options, $opened_path);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||
*
|
||||
* @param \Icewind\SMB\NativeState $state
|
||||
* @param resource $smbStream
|
||||
* @param string $mode
|
||||
* @param string $url
|
||||
* @return resource
|
||||
*/
|
||||
public static function wrap($state, $smbStream, $mode, $url) {
|
||||
stream_wrapper_register('nativesmb', NativeWriteStream::class);
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $state,
|
||||
'handle' => $smbStream,
|
||||
'url' => $url
|
||||
)
|
||||
));
|
||||
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||
stream_wrapper_unregister('nativesmb');
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||
$this->flushWrite();
|
||||
$result = parent::stream_seek($offset, $whence);
|
||||
if ($result) {
|
||||
$this->pos = parent::stream_tell();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function flushWrite() {
|
||||
rewind($this->writeBuffer);
|
||||
$this->state->write($this->handle, stream_get_contents($this->writeBuffer));
|
||||
$this->writeBuffer = fopen('php://memory', 'r+');
|
||||
$this->bufferSize = 0;
|
||||
}
|
||||
|
||||
public function stream_write($data) {
|
||||
$written = fwrite($this->writeBuffer, $data);
|
||||
$this->bufferSize += $written;
|
||||
$this->pos += $written;
|
||||
|
||||
if ($this->bufferSize >= self::CHUNK_SIZE) {
|
||||
$this->flushWrite();
|
||||
}
|
||||
|
||||
return $written;
|
||||
}
|
||||
|
||||
public function stream_close() {
|
||||
$this->flushWrite();
|
||||
return parent::stream_close();
|
||||
}
|
||||
|
||||
public function stream_tell() {
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function stream_truncate($size) {
|
||||
$this->flushWrite();
|
||||
$this->pos = $size;
|
||||
return parent::stream_truncate($size);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue