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