|
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\InvalidResourceException; |
|
11
|
|
|
|
|
12
|
|
|
class NativeShare extends AbstractShare { |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Server $server |
|
15
|
|
|
*/ |
|
16
|
|
|
private $server; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string $name |
|
20
|
|
|
*/ |
|
21
|
|
|
private $name; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Icewind\SMB\NativeState $state |
|
25
|
|
|
*/ |
|
26
|
|
|
private $state; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Server $server |
|
30
|
|
|
* @param string $name |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($server, $name) { |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
$this->server = $server; |
|
35
|
|
|
$this->name = $name; |
|
36
|
|
|
$this->state = new NativeState(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws \Icewind\SMB\Exception\ConnectionException |
|
41
|
|
|
* @throws \Icewind\SMB\Exception\AuthenticationException |
|
42
|
|
|
* @throws \Icewind\SMB\Exception\InvalidHostException |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function connect() { |
|
45
|
|
|
if ($this->state and $this->state instanceof NativeShare) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->state->init($this->server->getWorkgroup(), $this->server->getUser(), $this->server->getPassword()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the name of the share |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getName() { |
|
58
|
|
|
return $this->name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function buildUrl($path) { |
|
62
|
|
|
$this->verifyPath($path); |
|
63
|
|
|
$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name); |
|
64
|
|
|
if ($path) { |
|
65
|
|
|
$path = trim($path, '/'); |
|
66
|
|
|
$url .= '/'; |
|
67
|
|
|
$url .= implode('/', array_map('rawurlencode', explode('/', $path))); |
|
68
|
|
|
} |
|
69
|
|
|
return $url; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* List the content of a remote folder |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $path |
|
76
|
|
|
* @return \Icewind\SMB\IFileInfo[] |
|
77
|
|
|
* |
|
78
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
79
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function dir($path) { |
|
82
|
|
|
$this->connect(); |
|
83
|
|
|
$files = array(); |
|
84
|
|
|
|
|
85
|
|
|
$dh = $this->state->opendir($this->buildUrl($path)); |
|
86
|
|
|
while ($file = $this->state->readdir($dh)) { |
|
87
|
|
|
$name = $file['name']; |
|
88
|
|
|
if ($name !== '.' and $name !== '..') { |
|
89
|
|
|
$files [] = new NativeFileInfo($this, $path . '/' . $name, $name); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->state->closedir($dh); |
|
94
|
|
|
return $files; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $path |
|
99
|
|
|
* @return \Icewind\SMB\IFileInfo[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public function stat($path) { |
|
102
|
|
|
return new NativeFileInfo($this, $path, basename($path), $this->getStat($path)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getStat($path) { |
|
106
|
|
|
$this->connect(); |
|
107
|
|
|
return $this->state->stat($this->buildUrl($path)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Create a folder on the share |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $path |
|
114
|
|
|
* @return bool |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
117
|
|
|
* @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function mkdir($path) { |
|
120
|
|
|
$this->connect(); |
|
121
|
|
|
return $this->state->mkdir($this->buildUrl($path)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Remove a folder on the share |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $path |
|
128
|
|
|
* @return bool |
|
129
|
|
|
* |
|
130
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
131
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function rmdir($path) { |
|
134
|
|
|
$this->connect(); |
|
135
|
|
|
return $this->state->rmdir($this->buildUrl($path)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Delete a file on the share |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $path |
|
142
|
|
|
* @return bool |
|
143
|
|
|
* |
|
144
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
145
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function del($path) { |
|
148
|
|
|
$this->connect(); |
|
149
|
|
|
return $this->state->unlink($this->buildUrl($path)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Rename a remote file |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $from |
|
156
|
|
|
* @param string $to |
|
157
|
|
|
* @return bool |
|
158
|
|
|
* |
|
159
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
160
|
|
|
* @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
161
|
|
|
*/ |
|
162
|
|
|
public function rename($from, $to) { |
|
163
|
|
|
$this->connect(); |
|
164
|
|
|
return $this->state->rename($this->buildUrl($from), $this->buildUrl($to)); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Upload a local file |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $source local file |
|
171
|
|
|
* @param string $target remove file |
|
172
|
|
|
* @return bool |
|
173
|
|
|
* |
|
174
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
175
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
176
|
|
|
*/ |
|
177
|
|
|
public function put($source, $target) { |
|
178
|
|
|
$this->connect(); |
|
179
|
|
|
$sourceHandle = fopen($source, 'rb'); |
|
180
|
|
|
$targetHandle = $this->state->create($this->buildUrl($target)); |
|
181
|
|
|
|
|
182
|
|
|
while ($data = fread($sourceHandle, 4096)) { |
|
183
|
|
|
$this->state->write($targetHandle, $data); |
|
184
|
|
|
} |
|
185
|
|
|
$this->state->close($targetHandle); |
|
186
|
|
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Download a remote file |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $source remove file |
|
193
|
|
|
* @param string $target local file |
|
194
|
|
|
* @return bool |
|
195
|
|
|
* |
|
196
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
197
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
198
|
|
|
* @throws \Icewind\SMB\Exception\InvalidResourceException |
|
199
|
|
|
*/ |
|
200
|
|
|
public function get($source, $target) { |
|
201
|
|
|
$targetHandle = @fopen($target, 'wb'); |
|
202
|
|
|
if (!$targetHandle) { |
|
203
|
|
|
$error = error_get_last(); |
|
204
|
|
|
if (is_array($error)) { |
|
205
|
|
|
$reason = $error['message']; |
|
206
|
|
|
} else { |
|
207
|
|
|
$reason = 'Unknown error'; |
|
208
|
|
|
} |
|
209
|
|
|
throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->connect(); |
|
213
|
|
|
$sourceHandle = $this->state->open($this->buildUrl($source), 'r'); |
|
214
|
|
|
if (!$sourceHandle) { |
|
215
|
|
|
fclose($targetHandle); |
|
216
|
|
|
throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
while ($data = $this->state->read($sourceHandle, 4096)) { |
|
220
|
|
|
fwrite($targetHandle, $data); |
|
221
|
|
|
} |
|
222
|
|
|
$this->state->close($sourceHandle); |
|
223
|
|
|
return true; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Open a readable stream top a remote file |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $source |
|
230
|
|
|
* @return resource a read only stream with the contents of the remote file |
|
231
|
|
|
* |
|
232
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
233
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
234
|
|
|
*/ |
|
235
|
|
|
public function read($source) { |
|
236
|
|
|
$this->connect(); |
|
237
|
|
|
$handle = $this->state->open($this->buildUrl($source), 'r'); |
|
238
|
|
|
return NativeStream::wrap($this->state, $handle, 'r'); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Open a readable stream top a remote file |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $source |
|
245
|
|
|
* @return resource a read only stream with the contents of the remote file |
|
246
|
|
|
* |
|
247
|
|
|
* @throws \Icewind\SMB\Exception\NotFoundException |
|
248
|
|
|
* @throws \Icewind\SMB\Exception\InvalidTypeException |
|
249
|
|
|
*/ |
|
250
|
|
|
public function write($source) { |
|
251
|
|
|
$this->connect(); |
|
252
|
|
|
$handle = $this->state->create($this->buildUrl($source)); |
|
253
|
|
|
return NativeStream::wrap($this->state, $handle, 'w'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Get extended attributes for the path |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $path |
|
260
|
|
|
* @param string $attribute attribute to get the info |
|
261
|
|
|
* @return string the attribute value |
|
262
|
|
|
*/ |
|
263
|
|
|
public function getAttribute($path, $attribute) { |
|
264
|
|
|
$this->connect(); |
|
265
|
|
|
|
|
266
|
|
|
$result = $this->state->getxattr($this->buildUrl($path), $attribute); |
|
267
|
|
|
return $result; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Get extended attributes for the path |
|
272
|
|
|
* |
|
273
|
|
|
* @param string $path |
|
274
|
|
|
* @param string $attribute attribute to get the info |
|
275
|
|
|
* @param mixed $value |
|
276
|
|
|
* @return string the attribute value |
|
277
|
|
|
*/ |
|
278
|
|
|
public function setAttribute($path, $attribute, $value) { |
|
279
|
|
|
$this->connect(); |
|
280
|
|
|
|
|
281
|
|
|
if ($attribute === 'system.dos_attr.mode' and is_int($value)) { |
|
282
|
|
|
$value = '0x' . dechex($value); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return $this->state->setxattr($this->buildUrl($path), $attribute, $value); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param string $path |
|
290
|
|
|
* @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
|
291
|
|
|
* @return mixed |
|
292
|
|
|
*/ |
|
293
|
|
|
public function setMode($path, $mode) { |
|
294
|
|
|
return $this->setAttribute($path, 'system.dos_attr.mode', $mode); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
public function __destruct() { |
|
298
|
|
|
unset($this->state); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|