Passed
Push — master ( 999d66...ed09f6 )
by Robin
03:04
created

NativeShare::getServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 490
	public function __construct($server, $name) {
40 490
		parent::__construct();
41 490
		$this->server = $server;
42 490
		$this->name = $name;
43 490
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 490
	protected function getState() {
51 490
		if ($this->state and $this->state instanceof NativeState) {
52 490
			return $this->state;
53
		}
54
55 490
		$this->state = new NativeState();
56 490
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 490
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65
	public function getName() {
66
		return $this->name;
67
	}
68
69 490
	private function buildUrl($path) {
70 490
		$this->verifyPath($path);
71 490
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 490
		if ($path) {
73 490
			$path = trim($path, '/');
74 490
			$url .= '/';
75 490
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76
		}
77 490
		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 490
	public function dir($path) {
90 490
		$files = [];
91
92 490
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 490
		while ($file = $this->getState()->readdir($dh)) {
94 490
			$name = $file['name'];
95 490
			if ($name !== '.' and $name !== '..') {
96 186
				$fullPath = $path . '/' . $name;
97 186
				$files [] = new NativeFileInfo($this, $fullPath, $name);
98
			}
99
		}
100
101 490
		$this->getState()->closedir($dh);
102 490
		return $files;
103
	}
104
105
	/**
106
	 * @param string $path
107
	 * @return \Icewind\SMB\IFileInfo
108
	 */
109 48
	public function stat($path) {
110 48
		$info = new NativeFileInfo($this, $path, self::mb_basename($path));
111
112
		// trigger attribute loading
113 48
		$info->getSize();
114
115 28
		return $info;
116
	}
117
118
	/**
119
	 * Multibyte unicode safe version of basename()
120
	 *
121
	 * @param string $path
122
	 * @link http://php.net/manual/en/function.basename.php#121405
123
	 * @return string
124
	 */
125 48
	protected static function mb_basename($path) {
126 48
		if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
127 28
			return $matches[1];
128 20
		} elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
129 18
			return $matches[1];
130
		}
131
132 2
		return '';
133
	}
134
135
	/**
136
	 * Create a folder on the share
137
	 *
138
	 * @param string $path
139
	 * @return bool
140
	 *
141
	 * @throws \Icewind\SMB\Exception\NotFoundException
142
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
143
	 */
144 490
	public function mkdir($path) {
145 490
		return $this->getState()->mkdir($this->buildUrl($path));
146
	}
147
148
	/**
149
	 * Remove a folder on the share
150
	 *
151
	 * @param string $path
152
	 * @return bool
153
	 *
154
	 * @throws \Icewind\SMB\Exception\NotFoundException
155
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
156
	 */
157 490
	public function rmdir($path) {
158 490
		return $this->getState()->rmdir($this->buildUrl($path));
159
	}
160
161
	/**
162
	 * Delete a file on the share
163
	 *
164
	 * @param string $path
165
	 * @return bool
166
	 *
167
	 * @throws \Icewind\SMB\Exception\NotFoundException
168
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
169
	 */
170 248
	public function del($path) {
171 248
		return $this->getState()->unlink($this->buildUrl($path));
172
	}
173
174
	/**
175
	 * Rename a remote file
176
	 *
177
	 * @param string $from
178
	 * @param string $to
179
	 * @return bool
180
	 *
181
	 * @throws \Icewind\SMB\Exception\NotFoundException
182
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
183
	 */
184 56
	public function rename($from, $to) {
185 56
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
186
	}
187
188
	/**
189
	 * Upload a local file
190
	 *
191
	 * @param string $source local file
192
	 * @param string $target remove file
193
	 * @return bool
194
	 *
195
	 * @throws \Icewind\SMB\Exception\NotFoundException
196
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
197
	 */
198 208
	public function put($source, $target) {
199 208
		$sourceHandle = fopen($source, 'rb');
200 208
		$targetUrl = $this->buildUrl($target);
201
202 190
		$targetHandle = $this->getState()->create($targetUrl);
203
204 186
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
0 ignored issues
show
Bug introduced by
It seems like $sourceHandle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

204
		while ($data = fread(/** @scrutinizer ignore-type */ $sourceHandle, NativeReadStream::CHUNK_SIZE)) {
Loading history...
205 186
			$this->getState()->write($targetHandle, $data, $targetUrl);
206
		}
207 186
		$this->getState()->close($targetHandle, $targetUrl);
208 186
		return true;
209
	}
210
211
	/**
212
	 * Download a remote file
213
	 *
214
	 * @param string $source remove file
215
	 * @param string $target local file
216
	 * @return bool
217
	 *
218
	 * @throws \Icewind\SMB\Exception\NotFoundException
219
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
220
	 * @throws \Icewind\SMB\Exception\InvalidPathException
221
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
222
	 */
223 90
	public function get($source, $target) {
224 90
		if (!$target) {
225 18
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
226
		}
227
228 72
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
229 68
		if (!$sourceHandle) {
0 ignored issues
show
introduced by
$sourceHandle is of type resource, thus it always evaluated to false.
Loading history...
230
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
231
		}
232
233 68
		$targetHandle = @fopen($target, 'wb');
234 68
		if (!$targetHandle) {
235 2
			$error = error_get_last();
236 2
			if (is_array($error)) {
237 2
				$reason = $error['message'];
238
			} else {
239
				$reason = 'Unknown error';
240
			}
241 2
			$this->getState()->close($sourceHandle, $this->buildUrl($source));
242 2
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
243
		}
244
245 66
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
246 66
			fwrite($targetHandle, $data);
247
		}
248 66
		$this->getState()->close($sourceHandle, $this->buildUrl($source));
249 66
		return true;
250
	}
251
252
	/**
253
	 * Open a readable stream to a remote file
254
	 *
255
	 * @param string $source
256
	 * @return resource a read only stream with the contents of the remote file
257
	 *
258
	 * @throws \Icewind\SMB\Exception\NotFoundException
259
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
260
	 */
261 58
	public function read($source) {
262 58
		$url = $this->buildUrl($source);
263 40
		$handle = $this->getState()->open($url, 'r');
264 40
		return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
265
	}
266
267
	/**
268
	 * Open a writeable stream to a remote file
269
	 * Note: This method will truncate the file to 0bytes first
270
	 *
271
	 * @param string $source
272
	 * @return resource a writeable stream
273
	 *
274
	 * @throws \Icewind\SMB\Exception\NotFoundException
275
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
276
	 */
277 58
	public function write($source) {
278 58
		$url = $this->buildUrl($source);
279 40
		$handle = $this->getState()->create($url);
280 40
		return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
281
	}
282
283
	/**
284
	 * Open a writeable stream and set the cursor to the end of the stream
285
	 *
286
	 * @param string $source
287
	 * @return resource a writeable stream
288
	 *
289
	 * @throws \Icewind\SMB\Exception\NotFoundException
290
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
291
	 */
292 2
	public function append($source) {
293 2
		$url = $this->buildUrl($source);
294 2
		$handle = $this->getState()->open($url, "a+");
295 2
		return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
296
	}
297
298
	/**
299
	 * Get extended attributes for the path
300
	 *
301
	 * @param string $path
302
	 * @param string $attribute attribute to get the info
303
	 * @return string the attribute value
304
	 */
305 208
	public function getAttribute($path, $attribute) {
306 208
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
307
	}
308
309
	/**
310
	 * Set extended attributes for the given path
311
	 *
312
	 * @param string $path
313
	 * @param string $attribute attribute to get the info
314
	 * @param string|int $value
315
	 * @return mixed the attribute value
316
	 */
317
	public function setAttribute($path, $attribute, $value) {
318
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
319
			$value = '0x' . dechex($value);
320
		}
321
322
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
323
	}
324
325
	/**
326
	 * Set DOS comaptible node mode
327
	 *
328
	 * @param string $path
329
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
330
	 * @return mixed
331
	 */
332
	public function setMode($path, $mode) {
333
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
334
	}
335
336
	/**
337
	 * Start smb notify listener
338
	 * Note: This is a blocking call
339
	 *
340
	 * @param string $path
341
	 * @return INotifyHandler
342
	 */
343
	public function notify($path) {
344
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
345
		// so we use the smbclient based backend for this
346
		if (!Server::available($this->server->getSystem())) {
347
			throw new DependencyException('smbclient not found in path for notify command');
348
		}
349
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
350
		return $share->notify($path);
351
	}
352
353
	public function getServer(): IServer {
354
		return $this->server;
355
	}
356
357 490
	public function __destruct() {
358 490
		unset($this->state);
359 490
	}
360
}
361