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\Exception\AuthenticationException; |
12
|
|
|
use Icewind\SMB\Exception\ConnectException; |
13
|
|
|
use Icewind\SMB\Exception\ConnectionException; |
14
|
|
|
use Icewind\SMB\Exception\ConnectionRefusedException; |
15
|
|
|
use Icewind\SMB\Exception\Exception; |
16
|
|
|
use Icewind\SMB\Exception\InvalidHostException; |
17
|
|
|
use Icewind\SMB\IShare; |
18
|
|
|
use Icewind\SMB\ISystem; |
19
|
|
|
|
20
|
|
|
class Server extends AbstractServer { |
21
|
|
|
/** |
22
|
|
|
* Check if the smbclient php extension is available |
23
|
|
|
* |
24
|
|
|
* @param ISystem $system |
25
|
|
|
* @return bool |
26
|
4 |
|
*/ |
27
|
4 |
|
public static function available(ISystem $system): bool { |
28
|
|
|
return $system->getSmbclientPath() !== null; |
29
|
|
|
} |
30
|
10 |
|
|
31
|
10 |
|
private function getAuthFileArgument(): string { |
32
|
10 |
|
if ($this->getAuth()->getUsername()) { |
33
|
|
|
return '--authentication-file=' . $this->system->getFD(3); |
34
|
|
|
} else { |
35
|
|
|
return ''; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return IShare[] |
41
|
|
|
* |
42
|
|
|
* @throws AuthenticationException |
43
|
|
|
* @throws InvalidHostException |
44
|
|
|
* @throws ConnectException |
45
|
10 |
|
*/ |
46
|
10 |
|
public function listShares(): array { |
47
|
10 |
|
$maxProtocol = $this->options->getMaxProtocol(); |
48
|
10 |
|
$minProtocol = $this->options->getMinProtocol(); |
49
|
10 |
|
$smbClient = $this->system->getSmbclientPath(); |
50
|
10 |
|
if ($smbClient === null) { |
51
|
10 |
|
throw new Exception("Backend not available"); |
52
|
|
|
} |
53
|
10 |
|
$command = sprintf( |
54
|
10 |
|
'%s %s %s %s %s -L %s', |
55
|
10 |
|
$smbClient, |
56
|
10 |
|
$this->getAuthFileArgument(), |
57
|
|
|
$this->getAuth()->getExtraCommandLineArguments(), |
58
|
|
|
$maxProtocol ? "--option='client max protocol=" . $maxProtocol . "'" : "", |
59
|
|
|
$minProtocol ? "--option='client min protocol=" . $minProtocol . "'" : "", |
60
|
10 |
|
escapeshellarg('//' . $this->getHost()) |
61
|
|
|
); |
62
|
10 |
|
$connection = new RawConnection($command); |
63
|
10 |
|
$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword()); |
64
|
6 |
|
$connection->connect(); |
65
|
|
|
if (!$connection->isValid()) { |
66
|
|
|
throw new ConnectionException((string)$connection->readLine()); |
67
|
|
|
} |
68
|
4 |
|
|
69
|
4 |
|
$parser = new Parser($this->timezoneProvider->get($this->host)); |
70
|
|
|
|
71
|
|
|
$output = $connection->readAll(); |
72
|
4 |
|
if (isset($output[0])) { |
73
|
4 |
|
$parser->checkConnectionError($output[0]); |
74
|
|
|
} |
75
|
4 |
|
|
76
|
|
|
// sometimes we get an empty line first |
77
|
|
|
if (count($output) < 2) { |
78
|
|
|
$output = $connection->readAll(); |
79
|
4 |
|
} |
80
|
|
|
|
81
|
4 |
|
if (isset($output[0])) { |
82
|
4 |
|
$parser->checkConnectionError($output[0]); |
83
|
4 |
|
} |
84
|
|
|
if (count($output) === 0) { |
85
|
4 |
|
throw new ConnectionRefusedException(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$shareNames = $parser->parseListShares($output); |
89
|
|
|
|
90
|
|
|
$shares = []; |
91
|
|
|
foreach ($shareNames as $name => $_description) { |
92
|
488 |
|
$shares[] = $this->getShare($name); |
93
|
488 |
|
} |
94
|
|
|
return $shares; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
* @return IShare |
100
|
|
|
*/ |
101
|
|
|
public function getShare(string $name): IShare { |
102
|
|
|
return new Share($this, $name, $this->system); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|