mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Restore error handling after using libsmbclient-php
This commit is contained in:
parent
32f54c925b
commit
5f80502017
3 changed files with 68 additions and 12 deletions
|
|
@ -18,14 +18,15 @@ class NativeServer extends Server {
|
||||||
if ($this->state and is_resource($this->state)) {
|
if ($this->state and is_resource($this->state)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
|
|
||||||
$user = $this->getUser();
|
$user = $this->getUser();
|
||||||
$workgroup = null;
|
$workgroup = null;
|
||||||
if (strpos($user, '/')) {
|
if (strpos($user, '/')) {
|
||||||
list($workgroup, $user) = explode($user, '/');
|
list($workgroup, $user) = explode($user, '/');
|
||||||
}
|
}
|
||||||
|
NativeShare::registerErrorHandler();
|
||||||
$this->state = smbclient_state_new();
|
$this->state = smbclient_state_new();
|
||||||
$result = smbclient_state_init($this->state, $workgroup, $user, $this->getPassword());
|
$result = smbclient_state_init($this->state, $workgroup, $user, $this->getPassword());
|
||||||
|
NativeShare::restoreErrorHandler();
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
throw new ConnectionError();
|
throw new ConnectionError();
|
||||||
}
|
}
|
||||||
|
|
@ -39,12 +40,14 @@ class NativeServer extends Server {
|
||||||
public function listShares() {
|
public function listShares() {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$shares = array();
|
$shares = array();
|
||||||
|
NativeShare::registerErrorHandler();
|
||||||
$dh = smbclient_opendir($this->state, 'smb://' . $this->getHost());
|
$dh = smbclient_opendir($this->state, 'smb://' . $this->getHost());
|
||||||
while ($share = smbclient_readdir($this->state, $dh)) {
|
while ($share = smbclient_readdir($this->state, $dh)) {
|
||||||
if ($share['type'] === 'file share') {
|
if ($share['type'] === 'file share') {
|
||||||
$shares[] = $this->getShare($share['name']);
|
$shares[] = $this->getShare($share['name']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
NativeShare::restoreErrorHandler();
|
||||||
smbclient_closedir($this->state, $dh);
|
smbclient_closedir($this->state, $dh);
|
||||||
return $shares;
|
return $shares;
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +62,9 @@ class NativeServer extends Server {
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
if ($this->state and is_resource($this->state)) {
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
NativeShare::registerErrorHandler();
|
||||||
smbclient_state_free($this->state);
|
smbclient_state_free($this->state);
|
||||||
|
NativeShare::restoreErrorHandler();
|
||||||
}
|
}
|
||||||
unset($this->state);
|
unset($this->state);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ class NativeShare implements IShare {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function registerHandlers() {
|
private static function registerHandlers() {
|
||||||
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
|
|
||||||
if (self::$registed) {
|
if (self::$registed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -51,6 +50,14 @@ class NativeShare implements IShare {
|
||||||
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function registerErrorHandler() {
|
||||||
|
set_error_handler(array('Icewind\SMB\NativeShare', 'errorHandler'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function restoreErrorHandler() {
|
||||||
|
restore_error_handler();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ConnectionError
|
* @throws ConnectionError
|
||||||
*/
|
*/
|
||||||
|
|
@ -64,8 +71,10 @@ class NativeShare implements IShare {
|
||||||
if (strpos($user, '/')) {
|
if (strpos($user, '/')) {
|
||||||
list($workgroup, $user) = explode($user, '/');
|
list($workgroup, $user) = explode($user, '/');
|
||||||
}
|
}
|
||||||
|
self::registerErrorHandler();
|
||||||
$this->state = smbclient_state_new();
|
$this->state = smbclient_state_new();
|
||||||
$result = smbclient_state_init($this->state, $workgroup, $user, $this->server->getPassword());
|
$result = smbclient_state_init($this->state, $workgroup, $user, $this->server->getPassword());
|
||||||
|
self::restoreErrorHandler();
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
throw new ConnectionError();
|
throw new ConnectionError();
|
||||||
}
|
}
|
||||||
|
|
@ -130,6 +139,7 @@ class NativeShare implements IShare {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$files = array();
|
$files = array();
|
||||||
|
|
||||||
|
self::registerErrorHandler();
|
||||||
$dh = smbclient_opendir($this->state, $this->buildUrl($path));
|
$dh = smbclient_opendir($this->state, $this->buildUrl($path));
|
||||||
while ($file = smbclient_readdir($this->state, $dh)) {
|
while ($file = smbclient_readdir($this->state, $dh)) {
|
||||||
$name = $file['name'];
|
$name = $file['name'];
|
||||||
|
|
@ -143,12 +153,16 @@ class NativeShare implements IShare {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
smbclient_closedir($this->state, $dh);
|
smbclient_closedir($this->state, $dh);
|
||||||
|
self::restoreErrorHandler();
|
||||||
return $files;
|
return $files;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function stat($path) {
|
protected function stat($path) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_stat($this->state, $this->buildUrl($path));
|
self::registerErrorHandler();
|
||||||
|
$stat = smbclient_stat($this->state, $this->buildUrl($path));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $stat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -159,7 +173,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
public function mkdir($path) {
|
public function mkdir($path) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_mkdir($this->state, $this->buildUrl($path));
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_mkdir($this->state, $this->buildUrl($path));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -170,7 +187,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
public function rmdir($path) {
|
public function rmdir($path) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_rmdir($this->state, $this->buildUrl($path));
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_rmdir($this->state, $this->buildUrl($path));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -181,7 +201,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
public function del($path) {
|
public function del($path) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_unlink($this->state, $this->buildUrl($path));
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_unlink($this->state, $this->buildUrl($path));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -193,7 +216,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
public function rename($from, $to) {
|
public function rename($from, $to) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_rename($this->state, $this->buildUrl($from), $this->state, $this->buildUrl($to));
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_rename($this->state, $this->buildUrl($from), $this->state, $this->buildUrl($to));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -203,7 +229,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
protected function fopen($path, $mode) {
|
protected function fopen($path, $mode) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_open($this->state, $this->buildUrl($path), $mode);
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_open($this->state, $this->buildUrl($path), $mode);
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -212,7 +241,10 @@ class NativeShare implements IShare {
|
||||||
*/
|
*/
|
||||||
protected function create($path) {
|
protected function create($path) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
return smbclient_creat($this->state, $this->buildUrl($path));
|
self::registerErrorHandler();
|
||||||
|
$result = smbclient_creat($this->state, $this->buildUrl($path));
|
||||||
|
self::restoreErrorHandler();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -226,10 +258,13 @@ class NativeShare implements IShare {
|
||||||
$sourceHandle = fopen($source, 'rb');
|
$sourceHandle = fopen($source, 'rb');
|
||||||
$targetHandle = $this->create($target);
|
$targetHandle = $this->create($target);
|
||||||
|
|
||||||
|
self::registerErrorHandler();
|
||||||
while ($data = fread($sourceHandle, 4096)) {
|
while ($data = fread($sourceHandle, 4096)) {
|
||||||
smbclient_write($this->state, $targetHandle, $data);
|
smbclient_write($this->state, $targetHandle, $data);
|
||||||
}
|
}
|
||||||
return smbclient_close($this->state, $targetHandle);
|
smbclient_close($this->state, $targetHandle);
|
||||||
|
restore_error_handler();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -243,10 +278,12 @@ class NativeShare implements IShare {
|
||||||
$sourceHandle = $this->fopen($source, 'r');
|
$sourceHandle = $this->fopen($source, 'r');
|
||||||
$targetHandle = fopen($target, 'wb');
|
$targetHandle = fopen($target, 'wb');
|
||||||
|
|
||||||
|
self::registerErrorHandler();
|
||||||
while ($data = smbclient_read($this->state, $sourceHandle, 4096)) {
|
while ($data = smbclient_read($this->state, $sourceHandle, 4096)) {
|
||||||
fwrite($targetHandle, $data);
|
fwrite($targetHandle, $data);
|
||||||
}
|
}
|
||||||
return smbclient_close($this->state, $sourceHandle);
|
smbclient_close($this->state, $sourceHandle);
|
||||||
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -275,7 +312,9 @@ class NativeShare implements IShare {
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
if ($this->state and is_resource($this->state)) {
|
if ($this->state and is_resource($this->state)) {
|
||||||
|
self::registerErrorHandler();
|
||||||
smbclient_state_free($this->state);
|
smbclient_state_free($this->state);
|
||||||
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
unset($this->state);
|
unset($this->state);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ require_once 'share.php';
|
||||||
|
|
||||||
class NativeShare extends Share {
|
class NativeShare extends Share {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Icewind\SMB\NativeShare $share
|
* @var \Icewind\SMB\NativeShare $share
|
||||||
*/
|
*/
|
||||||
|
|
@ -26,4 +25,17 @@ class NativeShare extends Share {
|
||||||
}
|
}
|
||||||
$this->share->mkdir($this->root);
|
$this->share->mkdir($this->root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRestoreErrorHandler() {
|
||||||
|
$handlerCalled = false;
|
||||||
|
set_error_handler(function () use (&$handlerCalled) {
|
||||||
|
$handlerCalled = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->share->dir($this->root);
|
||||||
|
|
||||||
|
trigger_error('dummy');
|
||||||
|
$this->assertTrue($handlerCalled);
|
||||||
|
restore_error_handler();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue