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 | 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 | protected function getState() { |
||
59 | |||
60 | /** |
||
61 | * Get the name of the share |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getName() { |
||
68 | |||
69 | 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 | public function dir($path) { |
||
106 | |||
107 | /** |
||
108 | * @param string $path |
||
109 | * @return \Icewind\SMB\IFileInfo |
||
110 | */ |
||
111 | 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 | protected static function mb_basename($path) { |
||
123 | if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) { |
||
124 | return $matches[1]; |
||
125 | } elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) { |
||
126 | return $matches[1]; |
||
127 | } |
||
128 | |||
129 | return ''; |
||
130 | } |
||
131 | |||
132 | private function getStat($path) { |
||
133 | return $this->getState()->stat($this->buildUrl($path)); |
||
134 | } |
||
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 | public function mkdir($path) { |
||
146 | return $this->getState()->mkdir($this->buildUrl($path)); |
||
147 | } |
||
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 | public function rmdir($path) { |
||
159 | return $this->getState()->rmdir($this->buildUrl($path)); |
||
160 | } |
||
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 | public function del($path) { |
||
172 | return $this->getState()->unlink($this->buildUrl($path)); |
||
173 | } |
||
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 | public function rename($from, $to) { |
||
186 | return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to)); |
||
187 | } |
||
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 | public function put($source, $target) { |
||
200 | $sourceHandle = fopen($source, 'rb'); |
||
201 | $targetHandle = $this->getState()->create($this->buildUrl($target)); |
||
202 | |||
203 | while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) { |
||
204 | $this->getState()->write($targetHandle, $data); |
||
205 | } |
||
206 | $this->getState()->close($targetHandle); |
||
207 | 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 | public function get($source, $target) { |
||
223 | if (!$target) { |
||
224 | throw new InvalidPathException('Invalid target path: Filename cannot be empty'); |
||
225 | } |
||
226 | $targetHandle = @fopen($target, 'wb'); |
||
227 | if (!$targetHandle) { |
||
228 | $error = error_get_last(); |
||
229 | if (is_array($error)) { |
||
230 | $reason = $error['message']; |
||
231 | } else { |
||
232 | $reason = 'Unknown error'; |
||
233 | } |
||
234 | throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason); |
||
235 | } |
||
236 | |||
237 | $sourceHandle = $this->getState()->open($this->buildUrl($source), 'r'); |
||
238 | if (!$sourceHandle) { |
||
239 | fclose($targetHandle); |
||
240 | throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading'); |
||
241 | } |
||
242 | |||
243 | while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) { |
||
244 | fwrite($targetHandle, $data); |
||
245 | } |
||
246 | $this->getState()->close($sourceHandle); |
||
247 | return true; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Open a readable stream to a remote file |
||
252 | * |
||
253 | * @param string $source |
||
254 | * @return resource a read only stream with the contents of the remote file |
||
255 | * |
||
256 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
257 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
258 | */ |
||
259 | public function read($source) { |
||
260 | $url = $this->buildUrl($source); |
||
261 | $handle = $this->getState()->open($url, 'r'); |
||
262 | return NativeReadStream::wrap($this->getState(), $handle, 'r', $url); |
||
|
|||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Open a writeable stream to a remote file |
||
267 | * Note: This method will truncate the file to 0bytes first |
||
268 | * |
||
269 | * @param string $source |
||
270 | * @return resource a writeable stream |
||
271 | * |
||
272 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
273 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
274 | */ |
||
275 | public function write($source) { |
||
276 | $url = $this->buildUrl($source); |
||
277 | $handle = $this->getState()->create($url); |
||
278 | return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Open a writeable stream and set the cursor to the end of the stream |
||
283 | * |
||
284 | * @param string $source |
||
285 | * @return resource a writeable stream |
||
286 | * |
||
287 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
288 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
289 | */ |
||
290 | public function append($source) { |
||
291 | $url = $this->buildUrl($source); |
||
292 | $handle = $this->getState()->open($url, "a"); |
||
293 | return NativeWriteStream::wrap($this->getState(), $handle, "a", $url); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Get extended attributes for the path |
||
298 | * |
||
299 | * @param string $path |
||
300 | * @param string $attribute attribute to get the info |
||
301 | * @return string the attribute value |
||
302 | */ |
||
303 | public function getAttribute($path, $attribute) { |
||
304 | return $this->getState()->getxattr($this->buildUrl($path), $attribute); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Set extended attributes for the given path |
||
309 | * |
||
310 | * @param string $path |
||
311 | * @param string $attribute attribute to get the info |
||
312 | * @param string|int $value |
||
313 | * @return mixed the attribute value |
||
314 | */ |
||
315 | public function setAttribute($path, $attribute, $value) { |
||
316 | if ($attribute === 'system.dos_attr.mode' and is_int($value)) { |
||
317 | $value = '0x' . dechex($value); |
||
318 | } |
||
319 | |||
320 | return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Set DOS comaptible node mode |
||
325 | * |
||
326 | * @param string $path |
||
327 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
328 | * @return mixed |
||
329 | */ |
||
330 | public function setMode($path, $mode) { |
||
331 | return $this->setAttribute($path, 'system.dos_attr.mode', $mode); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Start smb notify listener |
||
336 | * Note: This is a blocking call |
||
337 | * |
||
338 | * @param string $path |
||
339 | * @return INotifyHandler |
||
340 | */ |
||
341 | public function notify($path) { |
||
350 | |||
351 | public function __destruct() { |
||
352 | unset($this->state); |
||
353 | } |
||
354 | } |
||
355 |
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: