1 | <?php |
||
28 | class NativeState { |
||
29 | /** |
||
30 | * @var resource |
||
31 | */ |
||
32 | protected $state; |
||
33 | |||
34 | protected $handlerSet = false; |
||
35 | |||
36 | protected $connected = false; |
||
37 | |||
38 | // see error.h |
||
39 | const EXCEPTION_MAP = [ |
||
40 | 1 => ForbiddenException::class, |
||
41 | 2 => NotFoundException::class, |
||
42 | 13 => ForbiddenException::class, |
||
43 | 16 => FileInUseException::class, |
||
44 | 17 => AlreadyExistsException::class, |
||
45 | 20 => InvalidTypeException::class, |
||
46 | 21 => InvalidTypeException::class, |
||
47 | 22 => InvalidArgumentException::class, |
||
48 | 28 => OutOfSpaceException::class, |
||
49 | 39 => NotEmptyException::class, |
||
50 | 110 => TimedOutException::class, |
||
51 | 111 => ConnectionRefusedException::class, |
||
52 | 112 => HostDownException::class, |
||
53 | 113 => NoRouteToHostException::class |
||
54 | ]; |
||
55 | |||
56 | 255 | protected function handleError($path) { |
|
57 | 255 | $error = smbclient_state_errno($this->state); |
|
58 | 255 | if ($error === 0) { |
|
59 | 255 | return; |
|
60 | } |
||
61 | 19 | throw Exception::fromMap(self::EXCEPTION_MAP, $error, $path); |
|
62 | } |
||
63 | |||
64 | 255 | protected function testResult($result, $uri) { |
|
65 | 255 | if ($result === false or $result === null) { |
|
66 | // smb://host/share/path |
||
67 | 255 | if (is_string($uri) && count(explode('/', $uri, 5)) > 4) { |
|
68 | 19 | list(, , , , $path) = explode('/', $uri, 5); |
|
69 | 19 | $path = '/' . $path; |
|
70 | } else { |
||
71 | 255 | $path = null; |
|
72 | } |
||
73 | 255 | $this->handleError($path); |
|
74 | } |
||
75 | 255 | } |
|
76 | |||
77 | /** |
||
78 | * @param IAuth $auth |
||
79 | * @return bool |
||
80 | */ |
||
81 | 255 | public function init(IAuth $auth) { |
|
82 | 255 | if ($this->connected) { |
|
83 | return true; |
||
84 | } |
||
85 | 255 | $this->state = smbclient_state_new(); |
|
86 | 255 | smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false); |
|
87 | 255 | $auth->setExtraSmbClientOptions($this->state); |
|
88 | 255 | $result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword()); |
|
89 | |||
90 | 255 | $this->testResult($result, ''); |
|
91 | 255 | $this->connected = true; |
|
92 | 255 | return $result; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $uri |
||
97 | * @return resource |
||
98 | */ |
||
99 | 255 | public function opendir($uri) { |
|
100 | 255 | $result = @smbclient_opendir($this->state, $uri); |
|
101 | |||
102 | 255 | $this->testResult($result, $uri); |
|
103 | 255 | return $result; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param resource $dir |
||
108 | * @return array |
||
109 | */ |
||
110 | 255 | public function readdir($dir) { |
|
111 | 255 | $result = @smbclient_readdir($this->state, $dir); |
|
112 | |||
113 | 255 | $this->testResult($result, $dir); |
|
114 | 255 | return $result; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param $dir |
||
119 | * @return bool |
||
120 | */ |
||
121 | 255 | public function closedir($dir) { |
|
122 | 255 | $result = smbclient_closedir($this->state, $dir); |
|
123 | |||
124 | 255 | $this->testResult($result, $dir); |
|
125 | 255 | return $result; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $old |
||
130 | * @param string $new |
||
131 | * @return bool |
||
132 | */ |
||
133 | 19 | public function rename($old, $new) { |
|
134 | 19 | $result = @smbclient_rename($this->state, $old, $this->state, $new); |
|
135 | |||
136 | 19 | $this->testResult($result, $new); |
|
137 | 16 | return $result; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $uri |
||
142 | * @return bool |
||
143 | */ |
||
144 | 124 | public function unlink($uri) { |
|
145 | 124 | $result = @smbclient_unlink($this->state, $uri); |
|
146 | |||
147 | 124 | $this->testResult($result, $uri); |
|
148 | 122 | return $result; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $uri |
||
153 | * @param int $mask |
||
154 | * @return bool |
||
155 | */ |
||
156 | 255 | public function mkdir($uri, $mask = 0777) { |
|
157 | 255 | $result = @smbclient_mkdir($this->state, $uri, $mask); |
|
158 | |||
159 | 255 | $this->testResult($result, $uri); |
|
160 | 255 | return $result; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $uri |
||
165 | * @return bool |
||
166 | */ |
||
167 | 255 | public function rmdir($uri) { |
|
168 | 255 | $result = @smbclient_rmdir($this->state, $uri); |
|
169 | |||
170 | 255 | $this->testResult($result, $uri); |
|
171 | 255 | return $result; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $uri |
||
176 | * @return array |
||
177 | */ |
||
178 | 121 | public function stat($uri) { |
|
179 | 121 | $result = @smbclient_stat($this->state, $uri); |
|
180 | |||
181 | 121 | $this->testResult($result, $uri); |
|
182 | 120 | return $result; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param resource $file |
||
187 | * @return array |
||
188 | */ |
||
189 | public function fstat($file) { |
||
195 | |||
196 | /** |
||
197 | * @param string $uri |
||
198 | * @param string $mode |
||
199 | * @param int $mask |
||
200 | * @return resource |
||
201 | */ |
||
202 | 54 | public function open($uri, $mode, $mask = 0666) { |
|
203 | 54 | $result = @smbclient_open($this->state, $uri, $mode, $mask); |
|
204 | |||
205 | 54 | $this->testResult($result, $uri); |
|
208 | |||
209 | /** |
||
210 | * @param string $uri |
||
211 | * @param int $mask |
||
212 | * @return resource |
||
213 | */ |
||
214 | 124 | public function create($uri, $mask = 0666) { |
|
220 | |||
221 | /** |
||
222 | * @param resource $file |
||
223 | * @param int $bytes |
||
224 | * @return string |
||
225 | */ |
||
226 | 51 | public function read($file, $bytes) { |
|
232 | |||
233 | /** |
||
234 | * @param resource $file |
||
235 | * @param string $data |
||
236 | * @param int $length |
||
237 | * @return int |
||
238 | */ |
||
239 | 122 | public function write($file, $data, $length = null) { |
|
245 | |||
246 | /** |
||
247 | * @param resource $file |
||
248 | * @param int $offset |
||
249 | * @param int $whence SEEK_SET | SEEK_CUR | SEEK_END |
||
250 | * @return int|bool new file offset as measured from the start of the file on success, false on failure. |
||
251 | */ |
||
252 | 1 | public function lseek($file, $offset, $whence = SEEK_SET) { |
|
253 | 1 | $result = @smbclient_lseek($this->state, $file, $offset, $whence); |
|
254 | |||
255 | 1 | $this->testResult($result, $file); |
|
256 | 1 | return $result; |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param resource $file |
||
261 | * @param int $size |
||
262 | * @return bool |
||
263 | */ |
||
264 | 1 | public function ftruncate($file, $size) { |
|
265 | 1 | $result = @smbclient_ftruncate($this->state, $file, $size); |
|
266 | |||
267 | 1 | $this->testResult($result, $file); |
|
268 | 1 | return $result; |
|
269 | } |
||
270 | |||
271 | 122 | public function close($file) { |
|
277 | |||
278 | /** |
||
279 | * @param string $uri |
||
280 | * @param string $key |
||
281 | * @return string |
||
282 | */ |
||
283 | 14 | public function getxattr($uri, $key) { |
|
289 | |||
290 | /** |
||
291 | * @param string $uri |
||
292 | * @param string $key |
||
293 | * @param string $value |
||
294 | * @param int $flags |
||
295 | * @return mixed |
||
296 | */ |
||
297 | 8 | public function setxattr($uri, $key, $value, $flags = 0) { |
|
303 | |||
304 | 255 | public function __destruct() { |
|
305 | 255 | if ($this->connected) { |
|
306 | 255 | smbclient_state_free($this->state); |
|
309 | } |
||
310 |