1 | <?php |
||
15 | class NativeState { |
||
16 | /** |
||
17 | * @var resource |
||
18 | */ |
||
19 | protected $state; |
||
20 | |||
21 | protected $handlerSet = false; |
||
22 | |||
23 | protected $connected = false; |
||
24 | |||
25 | // todo replace with static once <5.6 support is dropped |
||
26 | // see error.h |
||
27 | private static $exceptionMap = [ |
||
28 | 1 => '\Icewind\SMB\Exception\ForbiddenException', |
||
29 | 2 => '\Icewind\SMB\Exception\NotFoundException', |
||
30 | 13 => '\Icewind\SMB\Exception\ForbiddenException', |
||
31 | 16 => '\Icewind\SMB\Exception\FileInUseException', |
||
32 | 17 => '\Icewind\SMB\Exception\AlreadyExistsException', |
||
33 | 20 => '\Icewind\SMB\Exception\InvalidTypeException', |
||
34 | 21 => '\Icewind\SMB\Exception\InvalidTypeException', |
||
35 | 22 => '\Icewind\SMB\Exception\InvalidArgumentException', |
||
36 | 28 => '\Icewind\SMB\Exception\OutOfSpaceException', |
||
37 | 39 => '\Icewind\SMB\Exception\NotEmptyException', |
||
38 | 110 => '\Icewind\SMB\Exception\TimedOutException', |
||
39 | 111 => '\Icewind\SMB\Exception\ConnectionRefusedException', |
||
40 | 112 => '\Icewind\SMB\Exception\HostDownException', |
||
41 | 113 => '\Icewind\SMB\Exception\NoRouteToHostException' |
||
42 | ]; |
||
43 | |||
44 | protected function handleError($path) { |
||
45 | $error = smbclient_state_errno($this->state); |
||
46 | if ($error === 0) { |
||
47 | return; |
||
48 | } |
||
49 | throw Exception::fromMap(self::$exceptionMap, $error, $path); |
||
50 | } |
||
51 | |||
52 | protected function testResult($result, $uri) { |
||
53 | if ($result === false or $result === null) { |
||
54 | // smb://host/share/path |
||
55 | if (is_string($uri)) { |
||
56 | list(, , , , $path) = explode('/', $uri, 5); |
||
57 | $path = '/' . $path; |
||
58 | } else { |
||
59 | $path = null; |
||
60 | } |
||
61 | $this->handleError($path); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $workGroup |
||
67 | * @param string $user |
||
68 | * @param string $password |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function init($workGroup, $user, $password) { |
||
72 | if ($this->connected) { |
||
73 | return true; |
||
74 | } |
||
75 | $this->state = smbclient_state_new(); |
||
76 | smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false); |
||
77 | $result = @smbclient_state_init($this->state, $workGroup, $user, $password); |
||
78 | |||
79 | $this->testResult($result, ''); |
||
80 | $this->connected = true; |
||
81 | return $result; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param string $uri |
||
86 | * @return resource |
||
87 | */ |
||
88 | public function opendir($uri) { |
||
89 | $result = @smbclient_opendir($this->state, $uri); |
||
90 | |||
91 | $this->testResult($result, $uri); |
||
92 | return $result; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param resource $dir |
||
97 | * @return array |
||
98 | */ |
||
99 | public function readdir($dir) { |
||
100 | $result = @smbclient_readdir($this->state, $dir); |
||
101 | |||
102 | $this->testResult($result, $dir); |
||
103 | return $result; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param $dir |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function closedir($dir) { |
||
111 | $result = smbclient_closedir($this->state, $dir); |
||
112 | |||
113 | $this->testResult($result, $dir); |
||
114 | return $result; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $old |
||
119 | * @param string $new |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function rename($old, $new) { |
||
123 | $result = @smbclient_rename($this->state, $old, $this->state, $new); |
||
124 | |||
125 | $this->testResult($result, $new); |
||
126 | return $result; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $uri |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function unlink($uri) { |
||
134 | $result = @smbclient_unlink($this->state, $uri); |
||
135 | |||
136 | $this->testResult($result, $uri); |
||
137 | return $result; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $uri |
||
142 | * @param int $mask |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function mkdir($uri, $mask = 0777) { |
||
146 | $result = @smbclient_mkdir($this->state, $uri, $mask); |
||
147 | |||
148 | $this->testResult($result, $uri); |
||
149 | return $result; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $uri |
||
154 | * @return bool |
||
155 | */ |
||
156 | public function rmdir($uri) { |
||
157 | $result = @smbclient_rmdir($this->state, $uri); |
||
158 | |||
159 | $this->testResult($result, $uri); |
||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $uri |
||
165 | * @return array |
||
166 | */ |
||
167 | public function stat($uri) { |
||
168 | $result = @smbclient_stat($this->state, $uri); |
||
169 | |||
170 | $this->testResult($result, $uri); |
||
171 | return $result; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param resource $file |
||
176 | * @return array |
||
177 | */ |
||
178 | public function fstat($file) { |
||
184 | |||
185 | /** |
||
186 | * @param string $uri |
||
187 | * @param string $mode |
||
188 | * @param int $mask |
||
189 | * @return resource |
||
190 | */ |
||
191 | public function open($uri, $mode, $mask = 0666) { |
||
192 | $result = @smbclient_open($this->state, $uri, $mode, $mask); |
||
193 | |||
194 | $this->testResult($result, $uri); |
||
195 | return $result; |
||
197 | |||
198 | /** |
||
199 | * @param string $uri |
||
200 | * @param int $mask |
||
201 | * @return resource |
||
202 | */ |
||
203 | public function create($uri, $mask = 0666) { |
||
209 | |||
210 | /** |
||
211 | * @param resource $file |
||
212 | * @param int $bytes |
||
213 | * @return string |
||
214 | */ |
||
215 | public function read($file, $bytes) { |
||
221 | |||
222 | /** |
||
223 | * @param resource $file |
||
224 | * @param string $data |
||
225 | * @param int $length |
||
226 | * @return int |
||
227 | */ |
||
228 | public function write($file, $data, $length = null) { |
||
234 | |||
235 | /** |
||
236 | * @param resource $file |
||
237 | * @param int $offset |
||
238 | * @param int $whence SEEK_SET | SEEK_CUR | SEEK_END |
||
239 | * @return int|bool new file offset as measured from the start of the file on success, false on failure. |
||
240 | */ |
||
241 | public function lseek($file, $offset, $whence = SEEK_SET) { |
||
247 | |||
248 | /** |
||
249 | * @param resource $file |
||
250 | * @param int $size |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function ftruncate($file, $size) { |
||
259 | |||
260 | public function close($file) { |
||
266 | |||
267 | /** |
||
268 | * @param string $uri |
||
269 | * @param string $key |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getxattr($uri, $key) { |
||
278 | |||
279 | /** |
||
280 | * @param string $uri |
||
281 | * @param string $key |
||
282 | * @param string $value |
||
283 | * @param int $flags |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function setxattr($uri, $key, $value, $flags = 0) { |
||
292 | |||
293 | public function __destruct() { |
||
298 | } |
||
299 |