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; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\Exception\AuthenticationException; |
11
|
|
|
use Icewind\SMB\Exception\InvalidHostException; |
12
|
|
|
|
13
|
|
|
class Server { |
14
|
|
|
const LOCALE = 'en_US.UTF-8'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string $host |
18
|
|
|
*/ |
19
|
|
|
protected $host; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string $user |
23
|
|
|
*/ |
24
|
|
|
protected $user; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $password |
28
|
|
|
*/ |
29
|
|
|
protected $password; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string $workgroup |
33
|
|
|
*/ |
34
|
|
|
protected $workgroup; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Icewind\SMB\System |
38
|
|
|
*/ |
39
|
|
|
private $system; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TimeZoneProvider |
43
|
|
|
*/ |
44
|
|
|
private $timezoneProvider; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if the smbclient php extension is available |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
2 |
|
public static function NativeAvailable() { |
52
|
|
|
return function_exists('smbclient_state_new'); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $host |
57
|
|
|
* @param string $user |
58
|
|
|
* @param string $password |
59
|
|
|
*/ |
60
|
1040 |
|
public function __construct($host, $user, $password) { |
61
|
1040 |
|
$this->host = $host; |
62
|
1040 |
|
list($workgroup, $user) = $this->splitUser($user); |
63
|
1040 |
|
$this->user = $user; |
64
|
1040 |
|
$this->workgroup = $workgroup; |
65
|
1040 |
|
$this->password = $password; |
66
|
1040 |
|
$this->system = new System(); |
67
|
1040 |
|
$this->timezoneProvider = new TimeZoneProvider($host, $this->system); |
68
|
1040 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Split workgroup from username |
72
|
|
|
* |
73
|
|
|
* @param $user |
74
|
|
|
* @return string[] [$workgroup, $user] |
75
|
|
|
*/ |
76
|
1040 |
|
public function splitUser($user) { |
77
|
1040 |
|
if (strpos($user, '/')) { |
78
|
|
|
return explode('/', $user, 2); |
79
|
1040 |
|
} elseif (strpos($user, '\\')) { |
80
|
|
|
return explode('\\', $user); |
81
|
|
|
} else { |
82
|
1040 |
|
return array(null, $user); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getAuthString() { |
90
|
|
|
return $this->user . '%' . $this->password; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
1040 |
|
public function getUser() { |
97
|
1040 |
|
return $this->user; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1040 |
|
public function getPassword() { |
104
|
1040 |
|
return $this->password; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* return string |
109
|
|
|
*/ |
110
|
1040 |
|
public function getHost() { |
111
|
1040 |
|
return $this->host; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
1024 |
|
public function getWorkgroup() { |
118
|
1024 |
|
return $this->workgroup; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return \Icewind\SMB\IShare[] |
123
|
|
|
* |
124
|
|
|
* @throws \Icewind\SMB\Exception\AuthenticationException |
125
|
|
|
* @throws \Icewind\SMB\Exception\InvalidHostException |
126
|
|
|
*/ |
127
|
20 |
|
public function listShares() { |
128
|
20 |
|
$workgroupArgument = ($this->workgroup) ? ' -W ' . escapeshellarg($this->workgroup) : ''; |
129
|
20 |
|
$command = sprintf('%s %s --authentication-file=%s -gL %s', |
130
|
20 |
|
$this->system->getSmbclientPath(), |
131
|
20 |
|
$workgroupArgument, |
132
|
20 |
|
System::getFD(3), |
133
|
20 |
|
escapeshellarg($this->getHost()) |
134
|
10 |
|
); |
135
|
20 |
|
$connection = new RawConnection($command); |
136
|
20 |
|
$connection->writeAuthentication($this->getUser(), $this->getPassword()); |
137
|
20 |
|
$connection->connect(); |
138
|
20 |
|
$output = $connection->readAll(); |
139
|
20 |
|
$parser = new Parser($this->timezoneProvider); |
140
|
|
|
|
141
|
20 |
|
$parser->checkConnectionError($output[0]); |
142
|
|
|
|
143
|
8 |
|
$shareNames = $parser->parseListShares($output); |
144
|
|
|
|
145
|
8 |
|
$shares = array(); |
146
|
8 |
|
foreach ($shareNames as $name => $description) { |
147
|
8 |
|
$shares[] = $this->getShare($name); |
148
|
4 |
|
} |
149
|
8 |
|
return $shares; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $name |
154
|
|
|
* @return \Icewind\SMB\IShare |
155
|
|
|
*/ |
156
|
1028 |
|
public function getShare($name) { |
157
|
1028 |
|
return new Share($this, $name, $this->system); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getTimeZone() { |
164
|
|
|
return $this->timezoneProvider->get(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|