/src/php-src/ext/standard/ftp_fopen_wrapper.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Rasmus Lerdorf <rasmus@php.net> | |
14 | | | Jim Winstead <jimw@php.net> | |
15 | | | Hartmut Holzgraefe <hholzgra@php.net> | |
16 | | | Sara Golemon <pollita@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "php.h" |
21 | | #include "php_globals.h" |
22 | | #include "php_network.h" |
23 | | #include "php_ini.h" |
24 | | #include "zend_exceptions.h" |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #include <errno.h> |
29 | | #include <sys/types.h> |
30 | | #include <sys/stat.h> |
31 | | #include <fcntl.h> |
32 | | |
33 | | #ifdef PHP_WIN32 |
34 | | #include <winsock2.h> |
35 | | #define O_RDONLY _O_RDONLY |
36 | | #include "win32/param.h" |
37 | | #else |
38 | | #include <sys/param.h> |
39 | | #endif |
40 | | |
41 | | #include "php_standard.h" |
42 | | #include "ext/uri/php_uri.h" |
43 | | |
44 | | #include <sys/types.h> |
45 | | #ifdef HAVE_SYS_SOCKET_H |
46 | | #include <sys/socket.h> |
47 | | #endif |
48 | | |
49 | | #ifdef PHP_WIN32 |
50 | | #include <winsock2.h> |
51 | | #else |
52 | | #include <netinet/in.h> |
53 | | #include <netdb.h> |
54 | | #ifdef HAVE_ARPA_INET_H |
55 | | #include <arpa/inet.h> |
56 | | #endif |
57 | | #endif |
58 | | |
59 | | #if defined(PHP_WIN32) || defined(__riscos__) |
60 | | #undef AF_UNIX |
61 | | #endif |
62 | | |
63 | | #if defined(AF_UNIX) |
64 | | #include <sys/un.h> |
65 | | #endif |
66 | | |
67 | | #include "php_fopen_wrappers.h" |
68 | | |
69 | | #define FTPS_ENCRYPT_DATA 1 |
70 | 0 | #define GET_FTP_RESULT(stream) get_ftp_result((stream), tmp_line, sizeof(tmp_line)) |
71 | | |
72 | | typedef struct _php_ftp_dirstream_data { |
73 | | php_stream *datastream; |
74 | | php_stream *controlstream; |
75 | | php_stream *dirstream; |
76 | | } php_ftp_dirstream_data; |
77 | | |
78 | | /* {{{ get_ftp_result */ |
79 | | static inline int get_ftp_result(php_stream *stream, char *buffer, size_t buffer_size) |
80 | 0 | { |
81 | 0 | buffer[0] = '\0'; /* in case read fails to read anything */ |
82 | 0 | while (php_stream_gets(stream, buffer, buffer_size-1) && |
83 | 0 | !(isdigit((int) buffer[0]) && isdigit((int) buffer[1]) && |
84 | 0 | isdigit((int) buffer[2]) && buffer[3] == ' ')); |
85 | 0 | return strtol(buffer, NULL, 10); |
86 | 0 | } |
87 | | /* }}} */ |
88 | | |
89 | | /* {{{ php_stream_ftp_stream_stat */ |
90 | | static int php_stream_ftp_stream_stat(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb) |
91 | 0 | { |
92 | | /* For now, we return with a failure code to prevent the underlying |
93 | | * file's details from being used instead. */ |
94 | 0 | return -1; |
95 | 0 | } |
96 | | /* }}} */ |
97 | | |
98 | | /* {{{ php_stream_ftp_stream_close */ |
99 | | static int php_stream_ftp_stream_close(php_stream_wrapper *wrapper, php_stream *stream) |
100 | 0 | { |
101 | 0 | php_stream *controlstream = stream->wrapperthis; |
102 | 0 | int ret = 0; |
103 | |
|
104 | 0 | if (controlstream) { |
105 | 0 | if (strpbrk(stream->mode, "wa+")) { |
106 | 0 | char tmp_line[512]; |
107 | 0 | int result; |
108 | | |
109 | | /* For write modes close data stream first to signal EOF to server */ |
110 | 0 | result = GET_FTP_RESULT(controlstream); |
111 | 0 | if (result != 226 && result != 250) { |
112 | 0 | php_error_docref(NULL, E_WARNING, "FTP server error %d:%s", result, tmp_line); |
113 | 0 | ret = EOF; |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | 0 | php_stream_write_string(controlstream, "QUIT\r\n"); |
118 | 0 | php_stream_close(controlstream); |
119 | 0 | stream->wrapperthis = NULL; |
120 | 0 | } |
121 | |
|
122 | 0 | return ret; |
123 | 0 | } |
124 | | /* }}} */ |
125 | | |
126 | | /* {{{ php_ftp_fopen_connect */ |
127 | | static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, |
128 | | zend_string **opened_path, php_stream_context *context, php_stream **preuseid, |
129 | | php_uri **presource, int *puse_ssl, int *puse_ssl_on_data) |
130 | 0 | { |
131 | 0 | php_stream *stream = NULL, *reuseid = NULL; |
132 | 0 | php_uri *resource = NULL; |
133 | 0 | int result, use_ssl, use_ssl_on_data = 0; |
134 | 0 | char tmp_line[512]; |
135 | 0 | char *transport; |
136 | 0 | int transport_len; |
137 | |
|
138 | 0 | const php_uri_parser *uri_parser = php_stream_context_get_uri_parser("ftp", context); |
139 | 0 | if (uri_parser == NULL) { |
140 | 0 | zend_value_error("%s(): Provided stream context has invalid value for the \"uri_parser_class\" option", get_active_function_name()); |
141 | 0 | return NULL; |
142 | 0 | } |
143 | | |
144 | 0 | resource = php_uri_parse_to_struct(uri_parser, path, strlen(path), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
145 | 0 | if (resource == NULL || resource->path == NULL) { |
146 | 0 | if (resource && presource) { |
147 | 0 | *presource = resource; |
148 | 0 | } |
149 | 0 | return NULL; |
150 | 0 | } |
151 | | |
152 | 0 | use_ssl = resource->scheme && (ZSTR_LEN(resource->scheme) > 3) && ZSTR_VAL(resource->scheme)[3] == 's'; |
153 | | |
154 | | /* use port 21 if one wasn't specified */ |
155 | 0 | if (resource->port == 0) |
156 | 0 | resource->port = 21; |
157 | |
|
158 | 0 | transport_len = (int)spprintf(&transport, 0, "tcp://%s:" ZEND_LONG_FMT, ZSTR_VAL(resource->host), resource->port); |
159 | 0 | stream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, NULL, NULL); |
160 | 0 | efree(transport); |
161 | 0 | if (stream == NULL) { |
162 | 0 | result = 0; /* silence */ |
163 | 0 | goto connect_errexit; |
164 | 0 | } |
165 | | |
166 | 0 | php_stream_context_set(stream, context); |
167 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0); |
168 | | |
169 | | /* Start talking to ftp server */ |
170 | 0 | result = GET_FTP_RESULT(stream); |
171 | 0 | if (result > 299 || result < 200) { |
172 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
173 | 0 | goto connect_errexit; |
174 | 0 | } |
175 | | |
176 | 0 | if (use_ssl) { |
177 | | |
178 | | /* send the AUTH TLS request name */ |
179 | 0 | php_stream_write_string(stream, "AUTH TLS\r\n"); |
180 | | |
181 | | /* get the response */ |
182 | 0 | result = GET_FTP_RESULT(stream); |
183 | 0 | if (result != 234) { |
184 | | /* AUTH TLS not supported try AUTH SSL */ |
185 | 0 | php_stream_write_string(stream, "AUTH SSL\r\n"); |
186 | | |
187 | | /* get the response */ |
188 | 0 | result = GET_FTP_RESULT(stream); |
189 | 0 | if (result != 334) { |
190 | 0 | php_stream_wrapper_log_error(wrapper, options, "Server doesn't support FTPS."); |
191 | 0 | goto connect_errexit; |
192 | 0 | } else { |
193 | | /* we must reuse the old SSL session id */ |
194 | | /* if we talk to an old ftpd-ssl */ |
195 | 0 | reuseid = stream; |
196 | 0 | } |
197 | 0 | } else { |
198 | | /* encrypt data etc */ |
199 | | |
200 | |
|
201 | 0 | } |
202 | |
|
203 | 0 | } |
204 | | |
205 | 0 | if (use_ssl) { |
206 | 0 | if (php_stream_xport_crypto_setup(stream, |
207 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 |
208 | 0 | || php_stream_xport_crypto_enable(stream, 1) < 0) { |
209 | 0 | php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode"); |
210 | 0 | php_stream_close(stream); |
211 | 0 | stream = NULL; |
212 | 0 | goto connect_errexit; |
213 | 0 | } |
214 | | |
215 | | /* set PBSZ to 0 */ |
216 | 0 | php_stream_write_string(stream, "PBSZ 0\r\n"); |
217 | | |
218 | | /* ignore the response */ |
219 | 0 | result = GET_FTP_RESULT(stream); |
220 | | |
221 | | /* set data connection protection level */ |
222 | 0 | #if FTPS_ENCRYPT_DATA |
223 | 0 | php_stream_write_string(stream, "PROT P\r\n"); |
224 | | |
225 | | /* get the response */ |
226 | 0 | result = GET_FTP_RESULT(stream); |
227 | 0 | use_ssl_on_data = (result >= 200 && result<=299) || reuseid; |
228 | | #else |
229 | | php_stream_write_string(stream, "PROT C\r\n"); |
230 | | |
231 | | /* get the response */ |
232 | | result = GET_FTP_RESULT(stream); |
233 | | #endif |
234 | 0 | } |
235 | | |
236 | 0 | #define PHP_FTP_CNTRL_CHK(val, val_len, err_msg) { \ |
237 | 0 | unsigned char *s = (unsigned char *) val, *e = (unsigned char *) s + val_len; \ |
238 | 0 | while (s < e) { \ |
239 | 0 | if (iscntrl(*s)) { \ |
240 | 0 | php_stream_wrapper_log_error(wrapper, options, err_msg, val); \ |
241 | 0 | goto connect_errexit; \ |
242 | 0 | } \ |
243 | 0 | s++; \ |
244 | 0 | } \ |
245 | 0 | } |
246 | | |
247 | | /* send the user name */ |
248 | 0 | if (resource->user != NULL) { |
249 | 0 | ZSTR_LEN(resource->user) = php_raw_url_decode(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user)); |
250 | |
|
251 | 0 | PHP_FTP_CNTRL_CHK(ZSTR_VAL(resource->user), ZSTR_LEN(resource->user), "Invalid login %s") |
252 | | |
253 | 0 | php_stream_printf(stream, "USER %s\r\n", ZSTR_VAL(resource->user)); |
254 | 0 | } else { |
255 | 0 | php_stream_write_string(stream, "USER anonymous\r\n"); |
256 | 0 | } |
257 | | |
258 | | /* get the response */ |
259 | 0 | result = GET_FTP_RESULT(stream); |
260 | | |
261 | | /* if a password is required, send it */ |
262 | 0 | if (result >= 300 && result <= 399) { |
263 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_REQUIRED, tmp_line, 0); |
264 | |
|
265 | 0 | if (resource->password != NULL) { |
266 | 0 | ZSTR_LEN(resource->password) = php_raw_url_decode(ZSTR_VAL(resource->password), ZSTR_LEN(resource->password)); |
267 | |
|
268 | 0 | PHP_FTP_CNTRL_CHK(ZSTR_VAL(resource->password), ZSTR_LEN(resource->password), "Invalid password %s") |
269 | | |
270 | 0 | php_stream_printf(stream, "PASS %s\r\n", ZSTR_VAL(resource->password)); |
271 | 0 | } else { |
272 | | /* if the user has configured who they are, |
273 | | send that as the password */ |
274 | 0 | if (FG(from_address)) { |
275 | 0 | php_stream_printf(stream, "PASS %s\r\n", FG(from_address)); |
276 | 0 | } else { |
277 | 0 | php_stream_write_string(stream, "PASS anonymous\r\n"); |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | /* read the response */ |
282 | 0 | result = GET_FTP_RESULT(stream); |
283 | |
|
284 | 0 | if (result > 299 || result < 200) { |
285 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); |
286 | 0 | } else { |
287 | 0 | php_stream_notify_info(context, PHP_STREAM_NOTIFY_AUTH_RESULT, tmp_line, result); |
288 | 0 | } |
289 | 0 | } |
290 | 0 | if (result > 299 || result < 200) { |
291 | 0 | goto connect_errexit; |
292 | 0 | } |
293 | | |
294 | 0 | if (puse_ssl) { |
295 | 0 | *puse_ssl = use_ssl; |
296 | 0 | } |
297 | 0 | if (puse_ssl_on_data) { |
298 | 0 | *puse_ssl_on_data = use_ssl_on_data; |
299 | 0 | } |
300 | 0 | if (preuseid) { |
301 | 0 | *preuseid = reuseid; |
302 | 0 | } |
303 | 0 | if (presource) { |
304 | 0 | *presource = resource; |
305 | 0 | } |
306 | |
|
307 | 0 | return stream; |
308 | | |
309 | 0 | connect_errexit: |
310 | 0 | php_uri_struct_free(resource); |
311 | |
|
312 | 0 | if (stream) { |
313 | 0 | php_stream_close(stream); |
314 | 0 | } |
315 | |
|
316 | 0 | return NULL; |
317 | 0 | } |
318 | | /* }}} */ |
319 | | |
320 | | /* {{{ php_fopen_do_pasv */ |
321 | | static unsigned short php_fopen_do_pasv(php_stream *stream, char *ip, size_t ip_size, char **phoststart) |
322 | 0 | { |
323 | 0 | char tmp_line[512]; |
324 | 0 | int result, i; |
325 | 0 | unsigned short portno; |
326 | 0 | char *tpath, *ttpath, *hoststart=NULL; |
327 | |
|
328 | 0 | #ifdef HAVE_IPV6 |
329 | | /* We try EPSV first, needed for IPv6 and works on some IPv4 servers */ |
330 | 0 | php_stream_write_string(stream, "EPSV\r\n"); |
331 | 0 | result = GET_FTP_RESULT(stream); |
332 | | |
333 | | /* check if we got a 229 response */ |
334 | 0 | if (result != 229) { |
335 | 0 | #endif |
336 | | /* EPSV failed, let's try PASV */ |
337 | 0 | php_stream_write_string(stream, "PASV\r\n"); |
338 | 0 | result = GET_FTP_RESULT(stream); |
339 | | |
340 | | /* make sure we got a 227 response */ |
341 | 0 | if (result != 227) { |
342 | 0 | return 0; |
343 | 0 | } |
344 | | |
345 | | /* parse pasv command (129, 80, 95, 25, 13, 221) */ |
346 | 0 | tpath = tmp_line; |
347 | | /* skip over the "227 Some message " part */ |
348 | 0 | for (tpath += 4; *tpath && !isdigit((int) *tpath); tpath++); |
349 | 0 | if (!*tpath) { |
350 | 0 | return 0; |
351 | 0 | } |
352 | | /* skip over the host ip, to get the port */ |
353 | 0 | hoststart = tpath; |
354 | 0 | for (i = 0; i < 4; i++) { |
355 | 0 | for (; isdigit((int) *tpath); tpath++); |
356 | 0 | if (*tpath != ',') { |
357 | 0 | return 0; |
358 | 0 | } |
359 | 0 | *tpath='.'; |
360 | 0 | tpath++; |
361 | 0 | } |
362 | 0 | tpath[-1] = '\0'; |
363 | 0 | memcpy(ip, hoststart, ip_size); |
364 | 0 | ip[ip_size-1] = '\0'; |
365 | 0 | hoststart = ip; |
366 | | |
367 | | /* pull out the MSB of the port */ |
368 | 0 | portno = (unsigned short) strtoul(tpath, &ttpath, 10) * 256; |
369 | 0 | if (ttpath == NULL) { |
370 | | /* didn't get correct response from PASV */ |
371 | 0 | return 0; |
372 | 0 | } |
373 | 0 | tpath = ttpath; |
374 | 0 | if (*tpath != ',') { |
375 | 0 | return 0; |
376 | 0 | } |
377 | 0 | tpath++; |
378 | | /* pull out the LSB of the port */ |
379 | 0 | portno += (unsigned short) strtoul(tpath, &ttpath, 10); |
380 | 0 | #ifdef HAVE_IPV6 |
381 | 0 | } else { |
382 | | /* parse epsv command (|||6446|) */ |
383 | 0 | for (i = 0, tpath = tmp_line + 4; *tpath; tpath++) { |
384 | 0 | if (*tpath == '|') { |
385 | 0 | i++; |
386 | 0 | if (i == 3) |
387 | 0 | break; |
388 | 0 | } |
389 | 0 | } |
390 | 0 | if (i < 3) { |
391 | 0 | return 0; |
392 | 0 | } |
393 | | /* pull out the port */ |
394 | 0 | portno = (unsigned short) strtoul(tpath + 1, &ttpath, 10); |
395 | 0 | } |
396 | 0 | #endif |
397 | 0 | if (ttpath == NULL) { |
398 | | /* didn't get correct response from EPSV/PASV */ |
399 | 0 | return 0; |
400 | 0 | } |
401 | | |
402 | 0 | if (phoststart) { |
403 | 0 | *phoststart = hoststart; |
404 | 0 | } |
405 | |
|
406 | 0 | return portno; |
407 | 0 | } |
408 | | /* }}} */ |
409 | | |
410 | | /* {{{ php_fopen_url_wrap_ftp */ |
411 | | php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, const char *path, const char *mode, |
412 | | int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) |
413 | 0 | { |
414 | 0 | php_stream *stream = NULL, *datastream = NULL; |
415 | 0 | php_uri *resource = NULL; |
416 | 0 | char tmp_line[512]; |
417 | 0 | char ip[sizeof("123.123.123.123")]; |
418 | 0 | unsigned short portno; |
419 | 0 | char *hoststart = NULL; |
420 | 0 | int result = 0, use_ssl, use_ssl_on_data=0; |
421 | 0 | php_stream *reuseid=NULL; |
422 | 0 | size_t file_size = 0; |
423 | 0 | zval *tmpzval; |
424 | 0 | bool allow_overwrite = false; |
425 | 0 | int8_t read_write = 0; |
426 | 0 | char *transport; |
427 | 0 | int transport_len; |
428 | 0 | zend_string *error_message = NULL; |
429 | |
|
430 | 0 | tmp_line[0] = '\0'; |
431 | |
|
432 | 0 | if (strpbrk(mode, "r+")) { |
433 | 0 | read_write = 1; /* Open for reading */ |
434 | 0 | } |
435 | 0 | if (strpbrk(mode, "wa+")) { |
436 | 0 | if (read_write) { |
437 | 0 | php_stream_wrapper_log_error(wrapper, options, "FTP does not support simultaneous read/write connections"); |
438 | 0 | return NULL; |
439 | 0 | } |
440 | 0 | if (strchr(mode, 'a')) { |
441 | 0 | read_write = 3; /* Open for Appending */ |
442 | 0 | } else { |
443 | 0 | read_write = 2; /* Open for writing */ |
444 | 0 | } |
445 | 0 | } |
446 | 0 | if (!read_write) { |
447 | | /* No mode specified? */ |
448 | 0 | php_stream_wrapper_log_error(wrapper, options, "Unknown file open mode"); |
449 | 0 | return NULL; |
450 | 0 | } |
451 | | |
452 | 0 | if (context && |
453 | 0 | (tmpzval = php_stream_context_get_option(context, "ftp", "proxy")) != NULL) { |
454 | 0 | if (read_write == 1) { |
455 | | /* Use http wrapper to proxy ftp request */ |
456 | 0 | return php_stream_url_wrap_http(wrapper, path, mode, options, opened_path, context STREAMS_CC); |
457 | 0 | } else { |
458 | | /* ftp proxy is read-only */ |
459 | 0 | php_stream_wrapper_log_error(wrapper, options, "FTP proxy may only be used in read mode"); |
460 | 0 | return NULL; |
461 | 0 | } |
462 | 0 | } |
463 | | |
464 | 0 | stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data); |
465 | 0 | if (!stream) { |
466 | 0 | goto errexit; |
467 | 0 | } |
468 | | |
469 | | /* set the connection to be binary */ |
470 | 0 | php_stream_write_string(stream, "TYPE I\r\n"); |
471 | 0 | result = GET_FTP_RESULT(stream); |
472 | 0 | if (result > 299 || result < 200) |
473 | 0 | goto errexit; |
474 | | |
475 | | /* find out the size of the file (verifying it exists) */ |
476 | 0 | php_stream_printf(stream, "SIZE %s\r\n", ZSTR_VAL(resource->path)); |
477 | | |
478 | | /* read the response */ |
479 | 0 | result = GET_FTP_RESULT(stream); |
480 | 0 | if (read_write == 1) { |
481 | | /* Read Mode */ |
482 | 0 | char *sizestr; |
483 | | |
484 | | /* when reading file, it must exist */ |
485 | 0 | if (result > 299 || result < 200) { |
486 | 0 | errno = ENOENT; |
487 | 0 | goto errexit; |
488 | 0 | } |
489 | | |
490 | 0 | sizestr = strchr(tmp_line, ' '); |
491 | 0 | if (sizestr) { |
492 | 0 | sizestr++; |
493 | 0 | file_size = atoi(sizestr); |
494 | 0 | php_stream_notify_file_size(context, file_size, tmp_line, result); |
495 | 0 | } |
496 | 0 | } else if (read_write == 2) { |
497 | | /* when writing file (but not appending), it must NOT exist, unless a context option exists which allows it */ |
498 | 0 | if (context && (tmpzval = php_stream_context_get_option(context, "ftp", "overwrite")) != NULL) { |
499 | 0 | allow_overwrite = zend_is_true(tmpzval); |
500 | 0 | } |
501 | 0 | if (result <= 299 && result >= 200) { |
502 | 0 | if (allow_overwrite) { |
503 | | /* Context permits overwriting file, |
504 | | so we just delete whatever's there in preparation */ |
505 | 0 | php_stream_printf(stream, "DELE %s\r\n", ZSTR_VAL(resource->path)); |
506 | 0 | result = GET_FTP_RESULT(stream); |
507 | 0 | if (result >= 300 || result <= 199) { |
508 | 0 | goto errexit; |
509 | 0 | } |
510 | 0 | } else { |
511 | 0 | php_stream_wrapper_log_error(wrapper, options, "Remote file already exists and overwrite context option not specified"); |
512 | 0 | errno = EEXIST; |
513 | 0 | goto errexit; |
514 | 0 | } |
515 | 0 | } |
516 | 0 | } |
517 | | |
518 | | /* set up the passive connection */ |
519 | 0 | portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart); |
520 | |
|
521 | 0 | if (!portno) { |
522 | 0 | goto errexit; |
523 | 0 | } |
524 | | |
525 | | /* Send RETR/STOR command */ |
526 | 0 | if (read_write == 1) { |
527 | | /* set resume position if applicable */ |
528 | 0 | if (context && |
529 | 0 | (tmpzval = php_stream_context_get_option(context, "ftp", "resume_pos")) != NULL && |
530 | 0 | Z_TYPE_P(tmpzval) == IS_LONG && |
531 | 0 | Z_LVAL_P(tmpzval) > 0) { |
532 | 0 | php_stream_printf(stream, "REST " ZEND_LONG_FMT "\r\n", Z_LVAL_P(tmpzval)); |
533 | 0 | result = GET_FTP_RESULT(stream); |
534 | 0 | if (result < 300 || result > 399) { |
535 | 0 | php_stream_wrapper_log_error(wrapper, options, "Unable to resume from offset " ZEND_LONG_FMT, Z_LVAL_P(tmpzval)); |
536 | 0 | goto errexit; |
537 | 0 | } |
538 | 0 | } |
539 | | |
540 | | /* retrieve file */ |
541 | 0 | memcpy(tmp_line, "RETR", sizeof("RETR")); |
542 | 0 | } else if (read_write == 2) { |
543 | | /* Write new file */ |
544 | 0 | memcpy(tmp_line, "STOR", sizeof("STOR")); |
545 | 0 | } else { |
546 | | /* Append */ |
547 | 0 | memcpy(tmp_line, "APPE", sizeof("APPE")); |
548 | 0 | } |
549 | 0 | php_stream_printf(stream, "%s %s\r\n", tmp_line, (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
550 | | |
551 | | /* open the data channel */ |
552 | 0 | if (hoststart == NULL) { |
553 | 0 | hoststart = ZSTR_VAL(resource->host); |
554 | 0 | } |
555 | 0 | transport_len = (int)spprintf(&transport, 0, "tcp://%s:%d", hoststart, portno); |
556 | 0 | datastream = php_stream_xport_create(transport, transport_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, NULL, NULL, context, &error_message, NULL); |
557 | 0 | efree(transport); |
558 | 0 | if (datastream == NULL) { |
559 | 0 | tmp_line[0]='\0'; |
560 | 0 | goto errexit; |
561 | 0 | } |
562 | | |
563 | 0 | result = GET_FTP_RESULT(stream); |
564 | 0 | if (result != 150 && result != 125) { |
565 | | /* Could not retrieve or send the file |
566 | | * this data will only be sent to us after connection on the data port was initiated. |
567 | | */ |
568 | 0 | php_stream_close(datastream); |
569 | 0 | datastream = NULL; |
570 | 0 | goto errexit; |
571 | 0 | } |
572 | | |
573 | 0 | php_stream_context_set(datastream, context); |
574 | 0 | php_stream_notify_progress_init(context, 0, file_size); |
575 | |
|
576 | 0 | if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream, |
577 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 || |
578 | 0 | php_stream_xport_crypto_enable(datastream, 1) < 0)) { |
579 | |
|
580 | 0 | php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode"); |
581 | 0 | php_stream_close(datastream); |
582 | 0 | datastream = NULL; |
583 | 0 | tmp_line[0]='\0'; |
584 | 0 | goto errexit; |
585 | 0 | } |
586 | | |
587 | | /* remember control stream */ |
588 | 0 | datastream->wrapperthis = stream; |
589 | |
|
590 | 0 | php_uri_struct_free(resource); |
591 | 0 | return datastream; |
592 | | |
593 | 0 | errexit: |
594 | 0 | if (resource) { |
595 | 0 | php_uri_struct_free(resource); |
596 | 0 | } |
597 | 0 | if (stream) { |
598 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
599 | 0 | php_stream_close(stream); |
600 | 0 | } |
601 | 0 | if (tmp_line[0] != '\0') |
602 | 0 | php_stream_wrapper_log_error(wrapper, options, "FTP server reports %s", tmp_line); |
603 | |
|
604 | 0 | if (error_message) { |
605 | 0 | php_stream_wrapper_log_error(wrapper, options, "Failed to set up data channel: %s", ZSTR_VAL(error_message)); |
606 | 0 | zend_string_release(error_message); |
607 | 0 | } |
608 | 0 | return NULL; |
609 | 0 | } |
610 | | /* }}} */ |
611 | | |
612 | | /* {{{ php_ftp_dirsteam_read */ |
613 | | static ssize_t php_ftp_dirstream_read(php_stream *stream, char *buf, size_t count) |
614 | 0 | { |
615 | 0 | php_stream_dirent *ent = (php_stream_dirent *)buf; |
616 | 0 | php_stream *innerstream; |
617 | 0 | size_t tmp_len; |
618 | 0 | zend_string *basename; |
619 | |
|
620 | 0 | innerstream = ((php_ftp_dirstream_data *)stream->abstract)->datastream; |
621 | |
|
622 | 0 | if (count != sizeof(php_stream_dirent)) { |
623 | 0 | return -1; |
624 | 0 | } |
625 | | |
626 | 0 | if (php_stream_eof(innerstream)) { |
627 | 0 | return 0; |
628 | 0 | } |
629 | | |
630 | 0 | if (!php_stream_get_line(innerstream, ent->d_name, sizeof(ent->d_name), &tmp_len)) { |
631 | 0 | return -1; |
632 | 0 | } |
633 | | |
634 | 0 | basename = php_basename(ent->d_name, tmp_len, NULL, 0); |
635 | |
|
636 | 0 | tmp_len = MIN(sizeof(ent->d_name), ZSTR_LEN(basename) - 1); |
637 | 0 | memcpy(ent->d_name, ZSTR_VAL(basename), tmp_len); |
638 | 0 | ent->d_name[tmp_len - 1] = '\0'; |
639 | 0 | zend_string_release_ex(basename, 0); |
640 | 0 | ent->d_type = DT_UNKNOWN; |
641 | | |
642 | | /* Trim off trailing whitespace characters */ |
643 | 0 | while (tmp_len > 0 && |
644 | 0 | (ent->d_name[tmp_len - 1] == '\n' || ent->d_name[tmp_len - 1] == '\r' || |
645 | 0 | ent->d_name[tmp_len - 1] == '\t' || ent->d_name[tmp_len - 1] == ' ')) { |
646 | 0 | ent->d_name[--tmp_len] = '\0'; |
647 | 0 | } |
648 | |
|
649 | 0 | return sizeof(php_stream_dirent); |
650 | 0 | } |
651 | | /* }}} */ |
652 | | |
653 | | /* {{{ php_ftp_dirstream_close */ |
654 | | static int php_ftp_dirstream_close(php_stream *stream, int close_handle) |
655 | 0 | { |
656 | 0 | php_ftp_dirstream_data *data = stream->abstract; |
657 | | |
658 | | /* close control connection */ |
659 | 0 | if (data->controlstream) { |
660 | 0 | php_stream_close(data->controlstream); |
661 | 0 | data->controlstream = NULL; |
662 | 0 | } |
663 | | /* close data connection */ |
664 | 0 | php_stream_close(data->datastream); |
665 | 0 | data->datastream = NULL; |
666 | |
|
667 | 0 | efree(data); |
668 | 0 | stream->abstract = NULL; |
669 | |
|
670 | 0 | return 0; |
671 | 0 | } |
672 | | /* }}} */ |
673 | | |
674 | | /* ftp dirstreams only need to support read and close operations, |
675 | | They can't be rewound because the underlying ftp stream can't be rewound. */ |
676 | | static const php_stream_ops php_ftp_dirstream_ops = { |
677 | | NULL, /* write */ |
678 | | php_ftp_dirstream_read, /* read */ |
679 | | php_ftp_dirstream_close, /* close */ |
680 | | NULL, /* flush */ |
681 | | "ftpdir", |
682 | | NULL, /* rewind */ |
683 | | NULL, /* cast */ |
684 | | NULL, /* stat */ |
685 | | NULL /* set option */ |
686 | | }; |
687 | | |
688 | | /* {{{ php_stream_ftp_opendir */ |
689 | | static php_stream * php_stream_ftp_opendir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, |
690 | | zend_string **opened_path, php_stream_context *context STREAMS_DC) |
691 | 0 | { |
692 | 0 | php_stream *stream, *reuseid, *datastream = NULL; |
693 | 0 | php_ftp_dirstream_data *dirsdata; |
694 | 0 | php_uri *resource = NULL; |
695 | 0 | int result = 0, use_ssl, use_ssl_on_data = 0; |
696 | 0 | char *hoststart = NULL, tmp_line[512]; |
697 | 0 | char ip[sizeof("123.123.123.123")]; |
698 | 0 | unsigned short portno; |
699 | |
|
700 | 0 | tmp_line[0] = '\0'; |
701 | |
|
702 | 0 | stream = php_ftp_fopen_connect(wrapper, path, mode, options, opened_path, context, &reuseid, &resource, &use_ssl, &use_ssl_on_data); |
703 | 0 | if (!stream) { |
704 | 0 | goto opendir_errexit; |
705 | 0 | } |
706 | | |
707 | | /* set the connection to be ascii */ |
708 | 0 | php_stream_write_string(stream, "TYPE A\r\n"); |
709 | 0 | result = GET_FTP_RESULT(stream); |
710 | 0 | if (result > 299 || result < 200) |
711 | 0 | goto opendir_errexit; |
712 | | |
713 | | // tmp_line isn't relevant after the php_fopen_do_pasv(). |
714 | 0 | tmp_line[0] = '\0'; |
715 | | |
716 | | /* set up the passive connection */ |
717 | 0 | portno = php_fopen_do_pasv(stream, ip, sizeof(ip), &hoststart); |
718 | |
|
719 | 0 | if (!portno) { |
720 | 0 | goto opendir_errexit; |
721 | 0 | } |
722 | | |
723 | | /* open the data channel */ |
724 | 0 | if (hoststart == NULL) { |
725 | 0 | hoststart = ZSTR_VAL(resource->host); |
726 | 0 | } |
727 | |
|
728 | 0 | datastream = php_stream_sock_open_host(hoststart, portno, SOCK_STREAM, 0, 0); |
729 | 0 | if (datastream == NULL) { |
730 | 0 | goto opendir_errexit; |
731 | 0 | } |
732 | | |
733 | 0 | php_stream_printf(stream, "NLST %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
734 | |
|
735 | 0 | result = GET_FTP_RESULT(stream); |
736 | 0 | if (result != 150 && result != 125) { |
737 | | /* Could not retrieve or send the file |
738 | | * this data will only be sent to us after connection on the data port was initiated. |
739 | | */ |
740 | 0 | php_stream_close(datastream); |
741 | 0 | datastream = NULL; |
742 | 0 | goto opendir_errexit; |
743 | 0 | } |
744 | | |
745 | 0 | php_stream_context_set(datastream, context); |
746 | 0 | if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream, |
747 | 0 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL) < 0 || |
748 | 0 | php_stream_xport_crypto_enable(datastream, 1) < 0)) { |
749 | |
|
750 | 0 | php_stream_wrapper_log_error(wrapper, options, "Unable to activate SSL mode"); |
751 | 0 | php_stream_close(datastream); |
752 | 0 | datastream = NULL; |
753 | 0 | goto opendir_errexit; |
754 | 0 | } |
755 | | |
756 | 0 | php_uri_struct_free(resource); |
757 | |
|
758 | 0 | dirsdata = emalloc(sizeof *dirsdata); |
759 | 0 | dirsdata->datastream = datastream; |
760 | 0 | dirsdata->controlstream = stream; |
761 | 0 | dirsdata->dirstream = php_stream_alloc(&php_ftp_dirstream_ops, dirsdata, 0, mode); |
762 | |
|
763 | 0 | return dirsdata->dirstream; |
764 | | |
765 | 0 | opendir_errexit: |
766 | 0 | if (resource) { |
767 | 0 | php_uri_struct_free(resource); |
768 | 0 | } |
769 | 0 | if (stream) { |
770 | 0 | php_stream_notify_error(context, PHP_STREAM_NOTIFY_FAILURE, tmp_line, result); |
771 | 0 | php_stream_close(stream); |
772 | 0 | } |
773 | 0 | if (tmp_line[0] != '\0') { |
774 | 0 | php_stream_wrapper_log_error(wrapper, options, "FTP server reports %s", tmp_line); |
775 | 0 | } |
776 | 0 | return NULL; |
777 | 0 | } |
778 | | /* }}} */ |
779 | | |
780 | | /* {{{ php_stream_ftp_url_stat */ |
781 | | static int php_stream_ftp_url_stat(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context) |
782 | 0 | { |
783 | 0 | php_stream *stream = NULL; |
784 | 0 | php_uri *resource = NULL; |
785 | 0 | int result; |
786 | 0 | char tmp_line[512]; |
787 | | |
788 | | /* If ssb is NULL then someone is misbehaving */ |
789 | 0 | if (!ssb) return -1; |
790 | | |
791 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
792 | 0 | if (!stream) { |
793 | 0 | goto stat_errexit; |
794 | 0 | } |
795 | | |
796 | 0 | ssb->sb.st_mode = 0644; /* FTP won't give us a valid mode, so approximate one based on being readable */ |
797 | 0 | php_stream_printf(stream, "CWD %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); /* If we can CWD to it, it's a directory (maybe a link, but we can't tell) */ |
798 | 0 | result = GET_FTP_RESULT(stream); |
799 | 0 | if (result < 200 || result > 299) { |
800 | 0 | ssb->sb.st_mode |= S_IFREG; |
801 | 0 | } else { |
802 | 0 | ssb->sb.st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH; |
803 | 0 | } |
804 | |
|
805 | 0 | php_stream_write_string(stream, "TYPE I\r\n"); /* we need this since some servers refuse to accept SIZE command in ASCII mode */ |
806 | |
|
807 | 0 | result = GET_FTP_RESULT(stream); |
808 | |
|
809 | 0 | if(result < 200 || result > 299) { |
810 | 0 | goto stat_errexit; |
811 | 0 | } |
812 | | |
813 | 0 | php_stream_printf(stream, "SIZE %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
814 | 0 | result = GET_FTP_RESULT(stream); |
815 | 0 | if (result < 200 || result > 299) { |
816 | | /* Failure either means it doesn't exist |
817 | | or it's a directory and this server |
818 | | fails on listing directory sizes */ |
819 | 0 | if (ssb->sb.st_mode & S_IFDIR) { |
820 | 0 | ssb->sb.st_size = 0; |
821 | 0 | } else { |
822 | 0 | goto stat_errexit; |
823 | 0 | } |
824 | 0 | } else { |
825 | 0 | ssb->sb.st_size = atoi(tmp_line + 4); |
826 | 0 | } |
827 | | |
828 | 0 | php_stream_printf(stream, "MDTM %s\r\n", (resource->path != NULL ? ZSTR_VAL(resource->path) : "/")); |
829 | 0 | result = GET_FTP_RESULT(stream); |
830 | 0 | if (result == 213) { |
831 | 0 | char *p = tmp_line + 4; |
832 | 0 | int n; |
833 | 0 | struct tm tm, tmbuf, *gmt; |
834 | 0 | time_t stamp; |
835 | |
|
836 | 0 | while ((size_t)(p - tmp_line) < sizeof(tmp_line) && !isdigit(*p)) { |
837 | 0 | p++; |
838 | 0 | } |
839 | |
|
840 | 0 | if ((size_t)(p - tmp_line) > sizeof(tmp_line)) { |
841 | 0 | goto mdtm_error; |
842 | 0 | } |
843 | | |
844 | 0 | n = sscanf(p, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec); |
845 | 0 | if (n != 6) { |
846 | 0 | goto mdtm_error; |
847 | 0 | } |
848 | | |
849 | 0 | tm.tm_year -= 1900; |
850 | 0 | tm.tm_mon--; |
851 | 0 | tm.tm_isdst = -1; |
852 | | |
853 | | /* figure out the GMT offset */ |
854 | 0 | stamp = time(NULL); |
855 | 0 | gmt = php_gmtime_r(&stamp, &tmbuf); |
856 | 0 | if (!gmt) { |
857 | 0 | goto mdtm_error; |
858 | 0 | } |
859 | 0 | gmt->tm_isdst = -1; |
860 | | |
861 | | /* apply the GMT offset */ |
862 | 0 | tm.tm_sec += (long)(stamp - mktime(gmt)); |
863 | 0 | tm.tm_isdst = gmt->tm_isdst; |
864 | |
|
865 | 0 | ssb->sb.st_mtime = mktime(&tm); |
866 | 0 | } else { |
867 | | /* error or unsupported command */ |
868 | 0 | mdtm_error: |
869 | 0 | ssb->sb.st_mtime = -1; |
870 | 0 | } |
871 | | |
872 | 0 | ssb->sb.st_ino = 0; /* Unknown values */ |
873 | 0 | ssb->sb.st_dev = 0; |
874 | 0 | ssb->sb.st_uid = 0; |
875 | 0 | ssb->sb.st_gid = 0; |
876 | 0 | ssb->sb.st_atime = -1; |
877 | 0 | ssb->sb.st_ctime = -1; |
878 | |
|
879 | 0 | ssb->sb.st_nlink = 1; |
880 | 0 | ssb->sb.st_rdev = -1; |
881 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
882 | 0 | ssb->sb.st_blksize = 4096; /* Guess since FTP won't expose this information */ |
883 | 0 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
884 | 0 | ssb->sb.st_blocks = (int)((4095 + ssb->sb.st_size) / ssb->sb.st_blksize); /* emulate ceil */ |
885 | 0 | #endif |
886 | 0 | #endif |
887 | 0 | php_stream_close(stream); |
888 | 0 | php_uri_struct_free(resource); |
889 | 0 | return 0; |
890 | | |
891 | 0 | stat_errexit: |
892 | 0 | if (resource) { |
893 | 0 | php_uri_struct_free(resource); |
894 | 0 | } |
895 | 0 | if (stream) { |
896 | 0 | php_stream_close(stream); |
897 | 0 | } |
898 | 0 | return -1; |
899 | 0 | } |
900 | | /* }}} */ |
901 | | |
902 | | /* {{{ php_stream_ftp_unlink */ |
903 | | static int php_stream_ftp_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) |
904 | 0 | { |
905 | 0 | php_stream *stream = NULL; |
906 | 0 | php_uri *resource = NULL; |
907 | 0 | int result; |
908 | 0 | char tmp_line[512]; |
909 | |
|
910 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
911 | 0 | if (!stream) { |
912 | 0 | if (options & REPORT_ERRORS) { |
913 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url); |
914 | 0 | } |
915 | 0 | goto unlink_errexit; |
916 | 0 | } |
917 | | |
918 | 0 | if (resource->path == NULL) { |
919 | 0 | if (options & REPORT_ERRORS) { |
920 | 0 | php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", url); |
921 | 0 | } |
922 | 0 | goto unlink_errexit; |
923 | 0 | } |
924 | | |
925 | | /* Attempt to delete the file */ |
926 | 0 | php_stream_printf(stream, "DELE %s\r\n", ZSTR_VAL(resource->path)); |
927 | |
|
928 | 0 | result = GET_FTP_RESULT(stream); |
929 | 0 | if (result < 200 || result > 299) { |
930 | 0 | if (options & REPORT_ERRORS) { |
931 | 0 | php_error_docref(NULL, E_WARNING, "Error Deleting file: %s", tmp_line); |
932 | 0 | } |
933 | 0 | goto unlink_errexit; |
934 | 0 | } |
935 | | |
936 | 0 | php_uri_struct_free(resource); |
937 | 0 | php_stream_close(stream); |
938 | 0 | return 1; |
939 | | |
940 | 0 | unlink_errexit: |
941 | 0 | if (resource) { |
942 | 0 | php_uri_struct_free(resource); |
943 | 0 | } |
944 | 0 | if (stream) { |
945 | 0 | php_stream_close(stream); |
946 | 0 | } |
947 | 0 | return 0; |
948 | 0 | } |
949 | | /* }}} */ |
950 | | |
951 | | /* {{{ php_stream_ftp_rename */ |
952 | | static int php_stream_ftp_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context) |
953 | 0 | { |
954 | 0 | php_stream *stream = NULL; |
955 | 0 | php_uri *resource_from = NULL, *resource_to = NULL; |
956 | 0 | int result; |
957 | 0 | char tmp_line[512]; |
958 | |
|
959 | 0 | const php_uri_parser *uri_parser = php_stream_context_get_uri_parser("ftp", context); |
960 | 0 | if (uri_parser == NULL) { |
961 | 0 | zend_value_error("%s(): Provided stream context has invalid value for the \"uri_parser_class\" option", get_active_function_name()); |
962 | 0 | return 0; |
963 | 0 | } |
964 | | |
965 | 0 | resource_from = php_uri_parse_to_struct(uri_parser, url_from, strlen(url_from), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
966 | 0 | if (!resource_from) { |
967 | 0 | return 0; |
968 | 0 | } |
969 | | |
970 | 0 | resource_to = php_uri_parse_to_struct(uri_parser, url_to, strlen(url_to), PHP_URI_COMPONENT_READ_MODE_RAW, true); |
971 | 0 | if (!resource_to) { |
972 | 0 | goto rename_errexit; |
973 | 0 | } |
974 | | |
975 | | /* Must be same scheme (ftp/ftp or ftps/ftps), same host, and same port |
976 | | (or a 21/0 0/21 combination which is also "same") |
977 | | Also require paths to/from */ |
978 | 0 | if (!resource_from->scheme || |
979 | 0 | !resource_to->scheme || |
980 | 0 | !zend_string_equals(resource_from->scheme, resource_to->scheme) || |
981 | 0 | !resource_from->host || |
982 | 0 | !resource_to->host || |
983 | 0 | !zend_string_equals(resource_from->host, resource_to->host) || |
984 | 0 | (resource_from->port != resource_to->port && |
985 | 0 | resource_from->port * resource_to->port != 0 && |
986 | 0 | resource_from->port + resource_to->port != 21) || |
987 | 0 | !resource_from->path || |
988 | 0 | !resource_to->path) { |
989 | 0 | goto rename_errexit; |
990 | 0 | } |
991 | | |
992 | 0 | stream = php_ftp_fopen_connect(wrapper, url_from, "r", 0, NULL, context, NULL, NULL, NULL, NULL); |
993 | 0 | if (!stream) { |
994 | 0 | if (options & REPORT_ERRORS) { |
995 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s", ZSTR_VAL(resource_from->host)); |
996 | 0 | } |
997 | 0 | goto rename_errexit; |
998 | 0 | } |
999 | | |
1000 | | /* Rename FROM */ |
1001 | 0 | php_stream_printf(stream, "RNFR %s\r\n", ZSTR_VAL(resource_from->path)); |
1002 | |
|
1003 | 0 | result = GET_FTP_RESULT(stream); |
1004 | 0 | if (result < 300 || result > 399) { |
1005 | 0 | if (options & REPORT_ERRORS) { |
1006 | 0 | php_error_docref(NULL, E_WARNING, "Error Renaming file: %s", tmp_line); |
1007 | 0 | } |
1008 | 0 | goto rename_errexit; |
1009 | 0 | } |
1010 | | |
1011 | | /* Rename TO */ |
1012 | 0 | php_stream_printf(stream, "RNTO %s\r\n", ZSTR_VAL(resource_to->path)); |
1013 | |
|
1014 | 0 | result = GET_FTP_RESULT(stream); |
1015 | 0 | if (result < 200 || result > 299) { |
1016 | 0 | if (options & REPORT_ERRORS) { |
1017 | 0 | php_error_docref(NULL, E_WARNING, "Error Renaming file: %s", tmp_line); |
1018 | 0 | } |
1019 | 0 | goto rename_errexit; |
1020 | 0 | } |
1021 | | |
1022 | 0 | php_uri_struct_free(resource_from); |
1023 | 0 | php_uri_struct_free(resource_to); |
1024 | 0 | php_stream_close(stream); |
1025 | 0 | return 1; |
1026 | | |
1027 | 0 | rename_errexit: |
1028 | 0 | php_uri_struct_free(resource_from); |
1029 | 0 | if (resource_to) { |
1030 | 0 | php_uri_struct_free(resource_to); |
1031 | 0 | } |
1032 | 0 | if (stream) { |
1033 | 0 | php_stream_close(stream); |
1034 | 0 | } |
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | | /* }}} */ |
1038 | | |
1039 | | /* {{{ php_stream_ftp_mkdir */ |
1040 | | static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context) |
1041 | 0 | { |
1042 | 0 | php_stream *stream = NULL; |
1043 | 0 | php_uri *resource = NULL; |
1044 | 0 | int result, recursive = options & PHP_STREAM_MKDIR_RECURSIVE; |
1045 | 0 | char tmp_line[512]; |
1046 | |
|
1047 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
1048 | 0 | if (!stream) { |
1049 | 0 | if (options & REPORT_ERRORS) { |
1050 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url); |
1051 | 0 | } |
1052 | 0 | goto mkdir_errexit; |
1053 | 0 | } |
1054 | | |
1055 | 0 | if (resource->path == NULL) { |
1056 | 0 | if (options & REPORT_ERRORS) { |
1057 | 0 | php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", url); |
1058 | 0 | } |
1059 | 0 | goto mkdir_errexit; |
1060 | 0 | } |
1061 | | |
1062 | 0 | if (!recursive) { |
1063 | 0 | php_stream_printf(stream, "MKD %s\r\n", ZSTR_VAL(resource->path)); |
1064 | 0 | result = GET_FTP_RESULT(stream); |
1065 | 0 | } else { |
1066 | | /* we look for directory separator from the end of string, thus hopefully reducing our work load */ |
1067 | 0 | char *p, *e, *buf; |
1068 | |
|
1069 | 0 | buf = estrndup(ZSTR_VAL(resource->path), ZSTR_LEN(resource->path)); |
1070 | 0 | e = buf + ZSTR_LEN(resource->path); |
1071 | | |
1072 | | /* find a top level directory we need to create */ |
1073 | 0 | while ((p = strrchr(buf, '/'))) { |
1074 | 0 | *p = '\0'; |
1075 | 0 | php_stream_printf(stream, "CWD %s\r\n", strlen(buf) ? buf : "/"); |
1076 | 0 | result = GET_FTP_RESULT(stream); |
1077 | 0 | if (result >= 200 && result <= 299) { |
1078 | 0 | *p = '/'; |
1079 | 0 | break; |
1080 | 0 | } |
1081 | 0 | } |
1082 | |
|
1083 | 0 | php_stream_printf(stream, "MKD %s\r\n", strlen(buf) ? buf : "/"); |
1084 | 0 | result = GET_FTP_RESULT(stream); |
1085 | |
|
1086 | 0 | if (result >= 200 && result <= 299) { |
1087 | 0 | if (!p) { |
1088 | 0 | p = buf; |
1089 | 0 | } |
1090 | | /* create any needed directories if the creation of the 1st directory worked */ |
1091 | 0 | while (p != e) { |
1092 | 0 | if (*p == '\0' && *(p + 1) != '\0') { |
1093 | 0 | *p = '/'; |
1094 | 0 | php_stream_printf(stream, "MKD %s\r\n", buf); |
1095 | 0 | result = GET_FTP_RESULT(stream); |
1096 | 0 | if (result < 200 || result > 299) { |
1097 | 0 | if (options & REPORT_ERRORS) { |
1098 | 0 | php_error_docref(NULL, E_WARNING, "%s", tmp_line); |
1099 | 0 | } |
1100 | 0 | break; |
1101 | 0 | } |
1102 | 0 | } |
1103 | 0 | ++p; |
1104 | 0 | } |
1105 | 0 | } |
1106 | |
|
1107 | 0 | efree(buf); |
1108 | 0 | } |
1109 | |
|
1110 | 0 | php_uri_struct_free(resource); |
1111 | 0 | php_stream_close(stream); |
1112 | |
|
1113 | 0 | if (result < 200 || result > 299) { |
1114 | | /* Failure */ |
1115 | 0 | return 0; |
1116 | 0 | } |
1117 | | |
1118 | 0 | return 1; |
1119 | | |
1120 | 0 | mkdir_errexit: |
1121 | 0 | if (resource) { |
1122 | 0 | php_uri_struct_free(resource); |
1123 | 0 | } |
1124 | 0 | if (stream) { |
1125 | 0 | php_stream_close(stream); |
1126 | 0 | } |
1127 | 0 | return 0; |
1128 | 0 | } |
1129 | | /* }}} */ |
1130 | | |
1131 | | /* {{{ php_stream_ftp_rmdir */ |
1132 | | static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) |
1133 | 0 | { |
1134 | 0 | php_stream *stream = NULL; |
1135 | 0 | php_uri *resource = NULL; |
1136 | 0 | int result; |
1137 | 0 | char tmp_line[512]; |
1138 | |
|
1139 | 0 | stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, context, NULL, &resource, NULL, NULL); |
1140 | 0 | if (!stream) { |
1141 | 0 | if (options & REPORT_ERRORS) { |
1142 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s", url); |
1143 | 0 | } |
1144 | 0 | goto rmdir_errexit; |
1145 | 0 | } |
1146 | | |
1147 | 0 | if (resource->path == NULL) { |
1148 | 0 | if (options & REPORT_ERRORS) { |
1149 | 0 | php_error_docref(NULL, E_WARNING, "Invalid path provided in %s", url); |
1150 | 0 | } |
1151 | 0 | goto rmdir_errexit; |
1152 | 0 | } |
1153 | | |
1154 | 0 | php_stream_printf(stream, "RMD %s\r\n", ZSTR_VAL(resource->path)); |
1155 | 0 | result = GET_FTP_RESULT(stream); |
1156 | |
|
1157 | 0 | if (result < 200 || result > 299) { |
1158 | 0 | if (options & REPORT_ERRORS) { |
1159 | 0 | php_error_docref(NULL, E_WARNING, "%s", tmp_line); |
1160 | 0 | } |
1161 | 0 | goto rmdir_errexit; |
1162 | 0 | } |
1163 | | |
1164 | 0 | php_uri_struct_free(resource); |
1165 | 0 | php_stream_close(stream); |
1166 | |
|
1167 | 0 | return 1; |
1168 | | |
1169 | 0 | rmdir_errexit: |
1170 | 0 | if (resource) { |
1171 | 0 | php_uri_struct_free(resource); |
1172 | 0 | } |
1173 | 0 | if (stream) { |
1174 | 0 | php_stream_close(stream); |
1175 | 0 | } |
1176 | 0 | return 0; |
1177 | 0 | } |
1178 | | /* }}} */ |
1179 | | |
1180 | | static const php_stream_wrapper_ops ftp_stream_wops = { |
1181 | | php_stream_url_wrap_ftp, |
1182 | | php_stream_ftp_stream_close, /* stream_close */ |
1183 | | php_stream_ftp_stream_stat, |
1184 | | php_stream_ftp_url_stat, /* stat_url */ |
1185 | | php_stream_ftp_opendir, /* opendir */ |
1186 | | "ftp", |
1187 | | php_stream_ftp_unlink, /* unlink */ |
1188 | | php_stream_ftp_rename, /* rename */ |
1189 | | php_stream_ftp_mkdir, /* mkdir */ |
1190 | | php_stream_ftp_rmdir, /* rmdir */ |
1191 | | NULL |
1192 | | }; |
1193 | | |
1194 | | PHPAPI const php_stream_wrapper php_stream_ftp_wrapper = { |
1195 | | &ftp_stream_wops, |
1196 | | NULL, |
1197 | | 1 /* is_url */ |
1198 | | }; |