1 | <?php |
||
19 | class NativeShare extends AbstractShare { |
||
20 | /** |
||
21 | * @var IServer $server |
||
22 | */ |
||
23 | private $server; |
||
24 | |||
25 | /** |
||
26 | * @var string $name |
||
27 | */ |
||
28 | private $name; |
||
29 | |||
30 | /** |
||
31 | * @var NativeState $state |
||
32 | */ |
||
33 | private $state; |
||
34 | |||
35 | /** |
||
36 | * @param IServer $server |
||
37 | * @param string $name |
||
38 | */ |
||
39 | 256 | public function __construct($server, $name) { |
|
44 | |||
45 | /** |
||
46 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
47 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
48 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
49 | */ |
||
50 | 256 | protected function getState() { |
|
59 | |||
60 | /** |
||
61 | * Get the name of the share |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 1 | public function getName() { |
|
68 | |||
69 | 256 | private function buildUrl($path) { |
|
79 | |||
80 | /** |
||
81 | * List the content of a remote folder |
||
82 | * |
||
83 | * @param string $path |
||
84 | * @return \Icewind\SMB\IFileInfo[] |
||
85 | * |
||
86 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
87 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
88 | */ |
||
89 | 256 | public function dir($path) { |
|
106 | |||
107 | /** |
||
108 | * @param string $path |
||
109 | * @return \Icewind\SMB\IFileInfo |
||
110 | */ |
||
111 | 33 | public function stat($path) { |
|
114 | |||
115 | /** |
||
116 | * Multibyte unicode safe version of basename() |
||
117 | * |
||
118 | * @param string $path |
||
119 | * @link http://php.net/manual/en/function.basename.php#121405 |
||
120 | * @return string |
||
121 | */ |
||
122 | 33 | protected static function mb_basename($path) { |
|
131 | |||
132 | 114 | private function getStat($path) { |
|
135 | |||
136 | /** |
||
137 | * Create a folder on the share |
||
138 | * |
||
139 | * @param string $path |
||
140 | * @return bool |
||
141 | * |
||
142 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
143 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
144 | */ |
||
145 | 256 | public function mkdir($path) { |
|
148 | |||
149 | /** |
||
150 | * Remove a folder on the share |
||
151 | * |
||
152 | * @param string $path |
||
153 | * @return bool |
||
154 | * |
||
155 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
156 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
157 | */ |
||
158 | 256 | public function rmdir($path) { |
|
161 | |||
162 | /** |
||
163 | * Delete a file on the share |
||
164 | * |
||
165 | * @param string $path |
||
166 | * @return bool |
||
167 | * |
||
168 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
169 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
170 | */ |
||
171 | 134 | public function del($path) { |
|
174 | |||
175 | /** |
||
176 | * Rename a remote file |
||
177 | * |
||
178 | * @param string $from |
||
179 | * @param string $to |
||
180 | * @return bool |
||
181 | * |
||
182 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
183 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
184 | */ |
||
185 | 28 | public function rename($from, $to) { |
|
188 | |||
189 | /** |
||
190 | * Upload a local file |
||
191 | * |
||
192 | * @param string $source local file |
||
193 | * @param string $target remove file |
||
194 | * @return bool |
||
195 | * |
||
196 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
197 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
198 | */ |
||
199 | 113 | public function put($source, $target) { |
|
200 | 113 | $sourceHandle = fopen($source, 'rb'); |
|
201 | 113 | $targetHandle = $this->getState()->create($this->buildUrl($target)); |
|
202 | |||
203 | 102 | while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) { |
|
204 | 102 | $this->getState()->write($targetHandle, $data); |
|
205 | } |
||
206 | 102 | $this->getState()->close($targetHandle); |
|
207 | 102 | return true; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Download a remote file |
||
212 | * |
||
213 | * @param string $source remove file |
||
214 | * @param string $target local file |
||
215 | * @return bool |
||
216 | * |
||
217 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
218 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
219 | * @throws \Icewind\SMB\Exception\InvalidPathException |
||
220 | * @throws \Icewind\SMB\Exception\InvalidResourceException |
||
221 | */ |
||
222 | 45 | public function get($source, $target) { |
|
223 | 45 | if (!$target) { |
|
224 | 9 | throw new InvalidPathException('Invalid target path: Filename cannot be empty'); |
|
225 | } |
||
226 | |||
227 | 36 | $sourceHandle = $this->getState()->open($this->buildUrl($source), 'r'); |
|
228 | 34 | if (!$sourceHandle) { |
|
229 | throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading'); |
||
230 | } |
||
231 | |||
232 | 34 | $targetHandle = @fopen($target, 'wb'); |
|
233 | 34 | if (!$targetHandle) { |
|
234 | 1 | $error = error_get_last(); |
|
235 | 1 | if (is_array($error)) { |
|
236 | 1 | $reason = $error['message']; |
|
237 | } else { |
||
238 | $reason = 'Unknown error'; |
||
239 | } |
||
240 | 1 | $this->getState()->close($sourceHandle); |
|
241 | 1 | throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason); |
|
242 | } |
||
243 | |||
244 | 33 | while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) { |
|
245 | 33 | fwrite($targetHandle, $data); |
|
246 | } |
||
247 | 33 | $this->getState()->close($sourceHandle); |
|
248 | 33 | return true; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Open a readable stream to a remote file |
||
253 | * |
||
254 | * @param string $source |
||
255 | * @return resource a read only stream with the contents of the remote file |
||
256 | * |
||
257 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
258 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
259 | */ |
||
260 | 29 | public function read($source) { |
|
261 | 29 | $url = $this->buildUrl($source); |
|
262 | 20 | $handle = $this->getState()->open($url, 'r'); |
|
263 | 20 | return NativeReadStream::wrap($this->getState(), $handle, 'r', $url); |
|
|
|||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Open a writeable stream to a remote file |
||
268 | * Note: This method will truncate the file to 0bytes first |
||
269 | * |
||
270 | * @param string $source |
||
271 | * @return resource a writeable stream |
||
272 | * |
||
273 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
274 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
275 | */ |
||
276 | 29 | public function write($source) { |
|
277 | 29 | $url = $this->buildUrl($source); |
|
278 | 20 | $handle = $this->getState()->create($url); |
|
279 | 20 | return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url); |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Open a writeable stream and set the cursor to the end of the stream |
||
284 | * |
||
285 | * @param string $source |
||
286 | * @return resource a writeable stream |
||
287 | * |
||
288 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
289 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
290 | */ |
||
291 | 1 | public function append($source) { |
|
292 | 1 | $url = $this->buildUrl($source); |
|
293 | 1 | $handle = $this->getState()->open($url, "a+"); |
|
294 | 1 | return NativeWriteStream::wrap($this->getState(), $handle, "a", $url); |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get extended attributes for the path |
||
299 | * |
||
300 | * @param string $path |
||
301 | * @param string $attribute attribute to get the info |
||
302 | * @return string the attribute value |
||
303 | */ |
||
304 | public function getAttribute($path, $attribute) { |
||
307 | |||
308 | /** |
||
309 | * Set extended attributes for the given path |
||
310 | * |
||
311 | * @param string $path |
||
312 | * @param string $attribute attribute to get the info |
||
313 | * @param string|int $value |
||
314 | * @return mixed the attribute value |
||
315 | */ |
||
316 | 8 | public function setAttribute($path, $attribute, $value) { |
|
317 | 8 | if ($attribute === 'system.dos_attr.mode' and is_int($value)) { |
|
318 | 8 | $value = '0x' . dechex($value); |
|
319 | } |
||
320 | |||
321 | 8 | return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Set DOS comaptible node mode |
||
326 | * |
||
327 | * @param string $path |
||
328 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
329 | * @return mixed |
||
330 | */ |
||
331 | 8 | public function setMode($path, $mode) { |
|
332 | 8 | return $this->setAttribute($path, 'system.dos_attr.mode', $mode); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Start smb notify listener |
||
337 | * Note: This is a blocking call |
||
338 | * |
||
339 | * @param string $path |
||
340 | * @return INotifyHandler |
||
341 | */ |
||
342 | public function notify($path) { |
||
351 | |||
352 | 256 | public function __destruct() { |
|
353 | 256 | unset($this->state); |
|
354 | 256 | } |
|
355 | } |
||
356 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: