1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
5
|
|
|
* http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Icewind\SMB\Native; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\Exception\Exception; |
11
|
|
|
use Icewind\SMB\Exception\InvalidRequestException; |
12
|
|
|
use Icewind\Streams\File; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
class NativeStream implements File { |
16
|
|
|
/** |
17
|
|
|
* @var resource |
18
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
19
|
|
|
*/ |
20
|
|
|
public $context; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var NativeState |
24
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
25
|
|
|
*/ |
26
|
|
|
protected $state; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var resource |
30
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
31
|
|
|
*/ |
32
|
|
|
protected $handle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $eof = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $url = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Wrap a stream from libsmbclient-php into a regular php stream |
46
|
|
|
* |
47
|
|
|
* @param NativeState $state |
48
|
|
|
* @param resource $smbStream |
49
|
|
|
* @param string $mode |
50
|
|
|
* @param string $url |
51
|
|
|
* @return resource |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public static function wrap($state, $smbStream, $mode, $url) { |
|
|
|
|
54
|
|
|
stream_wrapper_register('nativesmb', NativeStream::class); |
55
|
|
|
$context = stream_context_create([ |
56
|
|
|
'nativesmb' => [ |
57
|
|
|
'state' => $state, |
58
|
|
|
'handle' => $smbStream, |
59
|
|
|
'url' => $url |
60
|
|
|
] |
61
|
|
|
]); |
62
|
|
|
$fh = fopen('nativesmb://', $mode, false, $context); |
63
|
78 |
|
stream_wrapper_unregister('nativesmb'); |
64
|
|
|
return $fh; |
65
|
78 |
|
} |
66
|
|
|
|
67
|
|
|
public function stream_close() { |
68
|
|
|
try { |
69
|
|
|
return $this->state->close($this->handle, $this->url); |
70
|
|
|
} catch (\Exception $e) { |
71
|
38 |
|
return false; |
72
|
38 |
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
38 |
|
public function stream_eof() { |
76
|
38 |
|
return $this->eof; |
77
|
|
|
} |
78
|
|
|
|
79
|
78 |
|
public function stream_flush() { |
80
|
78 |
|
return false; |
81
|
78 |
|
} |
82
|
78 |
|
|
83
|
78 |
|
|
84
|
78 |
|
public function stream_open($path, $mode, $options, &$opened_path) { |
85
|
|
|
$context = stream_context_get_options($this->context); |
86
|
|
|
if (!isset($context['nativesmb']) || !is_array($context['nativesmb'])) { |
87
|
38 |
|
throw new InvalidArgumentException("context not set"); |
88
|
38 |
|
} |
89
|
38 |
|
$state = $context['nativesmb']['state']; |
90
|
38 |
|
if (!$state instanceof NativeState) { |
91
|
|
|
throw new InvalidArgumentException("invalid context set"); |
92
|
38 |
|
} |
93
|
|
|
$this->state = $state; |
94
|
|
|
$handle = $context['nativesmb']['handle']; |
95
|
4 |
|
if (!is_resource($handle)) { |
96
|
4 |
|
throw new InvalidArgumentException("invalid context set"); |
97
|
|
|
} |
98
|
4 |
|
$this->handle = $handle; |
99
|
|
|
$url = $context['nativesmb']['url']; |
100
|
|
|
if (!is_string($url)) { |
101
|
|
|
throw new InvalidArgumentException("invalid context set"); |
102
|
|
|
} |
103
|
|
|
$this->url = $url; |
104
|
36 |
|
return true; |
105
|
|
|
} |
106
|
36 |
|
|
107
|
|
|
public function stream_read($count) { |
108
|
|
|
$result = $this->state->read($this->handle, $count, $this->url); |
109
|
|
|
if (strlen($result) < $count) { |
110
|
|
|
$this->eof = true; |
111
|
|
|
} |
112
|
4 |
|
return $result; |
113
|
4 |
|
} |
114
|
|
|
|
115
|
|
|
public function stream_seek($offset, $whence = SEEK_SET) { |
116
|
|
|
$this->eof = false; |
117
|
|
|
try { |
118
|
|
|
return $this->state->lseek($this->handle, $offset, $whence, $this->url) !== false; |
119
|
|
|
} catch (InvalidRequestException $e) { |
120
|
2 |
|
return false; |
121
|
2 |
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function stream_stat() { |
125
|
2 |
|
try { |
126
|
|
|
return $this->state->stat($this->url); |
127
|
|
|
} catch (Exception $e) { |
128
|
2 |
|
return false; |
129
|
2 |
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function stream_tell() { |
133
|
|
|
return $this->state->lseek($this->handle, 0, SEEK_CUR, $this->url); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function stream_write($data) { |
137
|
|
|
return $this->state->write($this->handle, $data, $this->url); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function stream_truncate($size) { |
141
|
|
|
return $this->state->ftruncate($this->handle, $size, $this->url); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function stream_set_option($option, $arg1, $arg2) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function stream_lock($operation) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.