Completed
Pull Request — master (#74)
by Raffael
03:04
created

NativeShare::mb_basename()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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\AbstractShare;
11
use Icewind\SMB\Exception\DependencyException;
12
use Icewind\SMB\Exception\InvalidPathException;
13
use Icewind\SMB\Exception\InvalidResourceException;
14
use Icewind\SMB\INotifyHandler;
15
use Icewind\SMB\IServer;
16
use Icewind\SMB\Wrapped\Server;
17
use Icewind\SMB\Wrapped\Share;
18
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) {
40 256
		parent::__construct();
41 256
		$this->server = $server;
42 256
		$this->name = $name;
43 256
	}
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() {
51 256
		if ($this->state and $this->state instanceof NativeState) {
52 256
			return $this->state;
53
		}
54
55 256
		$this->state = new NativeState();
56 256
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 256
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65 1
	public function getName() {
66 1
		return $this->name;
67
	}
68
69 256
	private function buildUrl($path) {
70 256
		$this->verifyPath($path);
71 256
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 256
		if ($path) {
73 256
			$path = trim($path, '/');
74 256
			$url .= '/';
75 256
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 256
		}
77 256
		return $url;
78
	}
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) {
90 256
		$files = [];
91
92 256
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 256
		while ($file = $this->getState()->readdir($dh)) {
94 256
			$name = $file['name'];
95 256
			if ($name !== '.' and $name !== '..') {
96 102
				$fullPath = $path . '/' . $name;
97 102
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
98 102
					return $this->getStat($fullPath);
99 102
				});
100 102
			}
101 256
		}
102
103 256
		$this->getState()->closedir($dh);
104 256
		return $files;
105
	}
106
107
	/**
108
	 * @param string $path
109
	 * @return \Icewind\SMB\IFileInfo
110
	 */
111 33
	public function stat($path) {
112 33
		return new NativeFileInfo($this, $path, self::mb_basename($path), $this->getStat($path));
113
	}
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) {
123 33
		if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
124 23
			return $matches[1];
125 10
		} elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
126 9
			return $matches[1];
127
		}
128
129 1
		return '';
130
	}
131
132 114
	private function getStat($path) {
133 114
		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 256
	public function mkdir($path) {
146 256
		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 256
	public function rmdir($path) {
159 256
		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 134
	public function del($path) {
172 134
		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 28
	public function rename($from, $to) {
186 28
		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 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 102
		}
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 36
		$targetHandle = @fopen($target, 'wb');
227 36
		if (!$targetHandle) {
228 1
			$error = error_get_last();
229 1
			if (is_array($error)) {
230 1
				$reason = $error['message'];
231 1
			} else {
232
				$reason = 'Unknown error';
233
			}
234 1
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
235
		}
236
237 35
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
238 33
		if (!$sourceHandle) {
239
			fclose($targetHandle);
240
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
241
		}
242
243 33
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
244 33
			fwrite($targetHandle, $data);
245 33
		}
246 33
		$this->getState()->close($sourceHandle);
247 33
		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 29
	public function read($source) {
260 29
		$url = $this->buildUrl($source);
261 20
		$handle = $this->getState()->open($url, 'r');
262 20
		return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 29
	public function write($source) {
276 29
		$url = $this->buildUrl($source);
277 20
		$handle = $this->getState()->create($url);
278 20
		return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 1
	public function append($source) {
291 1
		$url = $this->buildUrl($source);
292 1
		$handle = $this->getState()->open($url, "a");
293 1
		return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 14
	public function getAttribute($path, $attribute) {
304 14
		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 8
	public function setAttribute($path, $attribute, $value) {
316 8
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
317 8
			$value = '0x' . dechex($value);
318 8
		}
319
320 8
		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 8
	public function setMode($path, $mode) {
331 8
		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) {
342
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
343
		// so we use the smbclient based backend for this
344
		if (!Server::available($this->server->getSystem())) {
345
			throw new DependencyException('smbclient not found in path for notify command');
346
		}
347
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
348
		return $share->notify($path);
349
	}
350
351 256
	public function __destruct() {
352 256
		unset($this->state);
353 256
	}
354
}
355