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