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\AlreadyExistsException; |
11
|
|
|
use Icewind\SMB\Exception\ConnectionRefusedException; |
12
|
|
|
use Icewind\SMB\Exception\Exception; |
13
|
|
|
use Icewind\SMB\Exception\ForbiddenException; |
14
|
|
|
use Icewind\SMB\Exception\HostDownException; |
15
|
|
|
use Icewind\SMB\Exception\InvalidTypeException; |
16
|
|
|
use Icewind\SMB\Exception\NoRouteToHostException; |
17
|
|
|
use Icewind\SMB\Exception\NotEmptyException; |
18
|
|
|
use Icewind\SMB\Exception\NotFoundException; |
19
|
|
|
use Icewind\SMB\Exception\TimedOutException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Low level wrapper for libsmbclient-php for error handling |
23
|
|
|
*/ |
24
|
|
|
class NativeState { |
25
|
|
|
/** |
26
|
|
|
* @var resource |
27
|
|
|
*/ |
28
|
|
|
protected $state; |
29
|
|
|
|
30
|
|
|
protected $handlerSet = false; |
31
|
|
|
|
32
|
|
|
protected $connected = false; |
33
|
|
|
|
34
|
506 |
|
protected function handleError($path) { |
35
|
506 |
|
$error = smbclient_state_errno($this->state); |
36
|
|
|
switch ($error) { |
37
|
|
|
// see error.h |
38
|
506 |
|
case 0; |
39
|
506 |
|
return; |
40
|
36 |
|
case 1: |
41
|
36 |
|
case 13: |
42
|
|
|
throw new ForbiddenException($path, $error); |
43
|
36 |
|
case 2: |
44
|
24 |
|
throw new NotFoundException($path, $error); |
45
|
12 |
|
case 17: |
46
|
2 |
|
throw new AlreadyExistsException($path, $error); |
47
|
10 |
|
case 20: |
48
|
2 |
|
throw new InvalidTypeException($path, $error); |
49
|
8 |
|
case 21: |
50
|
6 |
|
throw new InvalidTypeException($path, $error); |
51
|
2 |
|
case 39: |
52
|
2 |
|
throw new NotEmptyException($path, $error); |
53
|
|
|
case 110: |
54
|
|
|
throw new TimedOutException($path, $error); |
55
|
|
|
case 111: |
56
|
|
|
throw new ConnectionRefusedException($path, $error); |
57
|
|
|
case 112: |
58
|
|
|
throw new HostDownException($path, $error); |
59
|
|
|
case 113: |
60
|
|
|
throw new NoRouteToHostException($path, $error); |
61
|
|
|
default: |
62
|
|
|
throw Exception::unknown($path, $error); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
506 |
|
protected function testResult($result, $uri) { |
67
|
506 |
|
if ($result === false or $result === null) { |
68
|
|
|
// smb://host/share/path |
69
|
506 |
|
if (is_string($uri)) { |
70
|
36 |
|
list(, , , , $path) = explode('/', $uri, 5); |
71
|
36 |
|
} else { |
72
|
506 |
|
$path = null; |
73
|
|
|
} |
74
|
506 |
|
$this->handleError($path); |
75
|
506 |
|
} |
76
|
506 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $workGroup |
80
|
|
|
* @param string $user |
81
|
|
|
* @param string $password |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
506 |
|
public function init($workGroup, $user, $password) { |
85
|
506 |
|
if ($this->connected) { |
86
|
506 |
|
return true; |
87
|
|
|
} |
88
|
506 |
|
$this->state = smbclient_state_new(); |
89
|
506 |
|
$result = @smbclient_state_init($this->state, $workGroup, $user, $password); |
90
|
|
|
|
91
|
506 |
|
$this->testResult($result, ''); |
92
|
506 |
|
$this->connected = true; |
93
|
506 |
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $uri |
98
|
|
|
* @return resource |
99
|
|
|
*/ |
100
|
506 |
|
public function opendir($uri) { |
101
|
506 |
|
$result = @smbclient_opendir($this->state, $uri); |
102
|
|
|
|
103
|
506 |
|
$this->testResult($result, $uri); |
104
|
506 |
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param resource $dir |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
506 |
|
public function readdir($dir) { |
112
|
506 |
|
$result = @smbclient_readdir($this->state, $dir); |
113
|
|
|
|
114
|
506 |
|
$this->testResult($result, $dir); |
115
|
506 |
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $dir |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
506 |
|
public function closedir($dir) { |
123
|
506 |
|
$result = smbclient_closedir($this->state, $dir); |
124
|
|
|
|
125
|
506 |
|
$this->testResult($result, $dir); |
126
|
506 |
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $old |
131
|
|
|
* @param string $new |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
36 |
|
public function rename($old, $new) { |
135
|
36 |
|
$result = @smbclient_rename($this->state, $old, $this->state, $new); |
136
|
|
|
|
137
|
36 |
|
$this->testResult($result, $new); |
138
|
32 |
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $uri |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
246 |
|
public function unlink($uri) { |
146
|
246 |
|
$result = @smbclient_unlink($this->state, $uri); |
147
|
|
|
|
148
|
246 |
|
$this->testResult($result, $uri); |
149
|
242 |
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $uri |
154
|
|
|
* @param int $mask |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
506 |
|
public function mkdir($uri, $mask = 0777) { |
158
|
506 |
|
$result = @smbclient_mkdir($this->state, $uri, $mask); |
159
|
|
|
|
160
|
506 |
|
$this->testResult($result, $uri); |
161
|
506 |
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $uri |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
506 |
|
public function rmdir($uri) { |
169
|
506 |
|
$result = @smbclient_rmdir($this->state, $uri); |
170
|
|
|
|
171
|
506 |
|
$this->testResult($result, $uri); |
172
|
506 |
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $uri |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
238 |
|
public function stat($uri) { |
180
|
238 |
|
$result = @smbclient_stat($this->state, $uri); |
181
|
|
|
|
182
|
238 |
|
$this->testResult($result, $uri); |
183
|
236 |
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param resource $file |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function fstat($file) { |
191
|
|
|
$result = @smbclient_fstat($this->state, $file); |
192
|
|
|
|
193
|
|
|
$this->testResult($result, $file); |
194
|
|
|
return $result; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $uri |
199
|
|
|
* @param string $mode |
200
|
|
|
* @param int $mask |
201
|
|
|
* @return resource |
202
|
|
|
*/ |
203
|
106 |
|
public function open($uri, $mode, $mask = 0666) { |
204
|
106 |
|
$result = @smbclient_open($this->state, $uri, $mode, $mask); |
205
|
|
|
|
206
|
106 |
|
$this->testResult($result, $uri); |
207
|
102 |
|
return $result; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $uri |
212
|
|
|
* @param int $mask |
213
|
|
|
* @return resource |
214
|
|
|
*/ |
215
|
246 |
|
public function create($uri, $mask = 0666) { |
216
|
246 |
|
$result = @smbclient_creat($this->state, $uri, $mask); |
217
|
|
|
|
218
|
246 |
|
$this->testResult($result, $uri); |
219
|
242 |
|
return $result; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param resource $file |
224
|
|
|
* @param int $bytes |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
100 |
|
public function read($file, $bytes) { |
228
|
100 |
|
$result = @smbclient_read($this->state, $file, $bytes); |
229
|
|
|
|
230
|
100 |
|
$this->testResult($result, $file); |
231
|
100 |
|
return $result; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param resource $file |
236
|
|
|
* @param string $data |
237
|
|
|
* @param int $length |
238
|
|
|
* @return int |
239
|
|
|
*/ |
240
|
238 |
|
public function write($file, $data, $length = null) { |
241
|
238 |
|
$result = @smbclient_write($this->state, $file, $data, $length); |
242
|
|
|
|
243
|
238 |
|
$this->testResult($result, $file); |
244
|
238 |
|
return $result; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param resource $file |
249
|
|
|
* @param int $offset |
250
|
|
|
* @param int $whence SEEK_SET | SEEK_CUR | SEEK_END |
251
|
|
|
* @return int | bool new file offset as measured from the start of the file on success, false on failure. |
252
|
|
|
*/ |
253
|
2 |
|
public function lseek($file, $offset, $whence = SEEK_SET) { |
254
|
2 |
|
$result = @smbclient_lseek($this->state, $file, $offset, $whence); |
255
|
|
|
|
256
|
2 |
|
$this->testResult($result, $file); |
257
|
2 |
|
return $result; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param resource $file |
262
|
|
|
* @param int $size |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
1 |
|
public function ftruncate($file, $size) { |
266
|
1 |
|
$result = @smbclient_ftruncate($this->state, $file, $size); |
267
|
|
|
|
268
|
1 |
|
$this->testResult($result, $file); |
269
|
1 |
|
return $result; |
270
|
|
|
} |
271
|
|
|
|
272
|
242 |
|
public function close($file) { |
273
|
242 |
|
$result = @smbclient_close($this->state, $file); |
274
|
|
|
|
275
|
242 |
|
$this->testResult($result, $file); |
276
|
242 |
|
return $result; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $uri |
281
|
|
|
* @param string $key |
282
|
|
|
* @return string |
283
|
|
|
*/ |
284
|
28 |
|
public function getxattr($uri, $key) { |
285
|
28 |
|
$result = @smbclient_getxattr($this->state, $uri, $key); |
286
|
|
|
|
287
|
28 |
|
$this->testResult($result, $uri); |
288
|
28 |
|
return $result; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param string $uri |
293
|
|
|
* @param string $key |
294
|
|
|
* @param string $value |
295
|
|
|
* @param int $flags |
296
|
|
|
* @return mixed |
297
|
|
|
*/ |
298
|
16 |
|
public function setxattr($uri, $key, $value, $flags = 0) { |
299
|
16 |
|
$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags); |
300
|
|
|
|
301
|
16 |
|
$this->testResult($result, $uri); |
302
|
16 |
|
return $result; |
303
|
|
|
} |
304
|
|
|
|
305
|
506 |
|
public function __destruct() { |
306
|
506 |
|
if ($this->connected) { |
307
|
506 |
|
smbclient_state_free($this->state); |
308
|
506 |
|
} |
309
|
506 |
|
} |
310
|
|
|
} |
311
|
|
|
|