Completed
Push — master ( c37ee6...eda387 )
by Robin
03:47
created

Server::getShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Wrapped;
9
10
use Icewind\SMB\AbstractServer;
11
use Icewind\SMB\System;
12
13
class Server extends AbstractServer {
14
	/**
15
	 * Check if the smbclient php extension is available
16
	 *
17
	 * @return bool
18
	 */
19
	public static function available(System $system) {
20
		return $system->getSmbclientPath();
21
	}
22
23
	private function getAuthFileArgument() {
24
		if ($this->getAuth()->getUsername()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getAuth()->getUsername() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
			return '--authentication-file=' . System::getFD(3);
26
		} else {
27
			return '';
28
		}
29
	}
30
31
	/**
32
	 * @return \Icewind\SMB\IShare[]
33
	 *
34
	 * @throws \Icewind\SMB\Exception\AuthenticationException
35
	 * @throws \Icewind\SMB\Exception\InvalidHostException
36
	 */
37
	public function listShares() {
38
		$command = sprintf('%s %s %s -L %s',
39
			$this->system->getSmbclientPath(),
40
			$this->getAuthFileArgument(),
41
			$this->getAuth()->getExtraCommandLineArguments(),
42
			escapeshellarg('//' . $this->getHost())
43
		);
44
		$connection = new RawConnection($command);
45
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
46
		$connection->connect();
47
		$output = $connection->readAll();
48
		$parser = new Parser($this->timezoneProvider);
49
50
		if (isset($output[0])) {
51
			$parser->checkConnectionError($output[0]);
52
		}
53
54
		$shareNames = $parser->parseListShares($output);
55
56
		$shares = array();
57
		foreach ($shareNames as $name => $description) {
58
			$shares[] = $this->getShare($name);
59
		}
60
		return $shares;
61
	}
62
63
	/**
64
	 * @param string $name
65
	 * @return \Icewind\SMB\IShare
66
	 */
67
	public function getShare($name) {
68
		return new Share($this, $name, $this->system);
69
	}
70
}
71