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