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