2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Authors: Andrew Skalski <askalski@chek.com> |
16
| Stefan Esser <sesser@php.net> (resume functions) |
17
+----------------------------------------------------------------------+
20
/* $Id: php_ftp.c,v 1.100.2.2 2005/03/10 23:38:01 iliaa Exp $ */
28
#if defined(NETWARE) && defined(USE_WINSOCK)
33
# include <openssl/ssl.h>
38
#include "ext/standard/info.h"
39
#include "ext/standard/file.h"
45
#define le_ftpbuf_name "FTP Buffer"
48
ZEND_BEGIN_ARG_INFO(third_and_rest_force_ref, 1)
54
function_entry php_ftp_functions[] = {
55
PHP_FE(ftp_connect, NULL)
57
PHP_FE(ftp_ssl_connect, NULL)
59
PHP_FE(ftp_login, NULL)
61
PHP_FE(ftp_cdup, NULL)
62
PHP_FE(ftp_chdir, NULL)
63
PHP_FE(ftp_exec, NULL)
65
PHP_FE(ftp_mkdir, NULL)
66
PHP_FE(ftp_rmdir, NULL)
67
PHP_FE(ftp_chmod, NULL)
68
PHP_FE(ftp_alloc, third_and_rest_force_ref)
69
PHP_FE(ftp_nlist, NULL)
70
PHP_FE(ftp_rawlist, NULL)
71
PHP_FE(ftp_systype, NULL)
72
PHP_FE(ftp_pasv, NULL)
74
PHP_FE(ftp_fget, NULL)
76
PHP_FE(ftp_fput, NULL)
77
PHP_FE(ftp_size, NULL)
78
PHP_FE(ftp_mdtm, NULL)
79
PHP_FE(ftp_rename, NULL)
80
PHP_FE(ftp_delete, NULL)
81
PHP_FE(ftp_site, NULL)
82
PHP_FE(ftp_close, NULL)
83
PHP_FE(ftp_set_option, NULL)
84
PHP_FE(ftp_get_option, NULL)
85
PHP_FE(ftp_nb_fget, NULL)
86
PHP_FE(ftp_nb_get, NULL)
87
PHP_FE(ftp_nb_continue, NULL)
88
PHP_FE(ftp_nb_put, NULL)
89
PHP_FE(ftp_nb_fput, NULL)
90
PHP_FALIAS(ftp_quit, ftp_close, NULL)
94
zend_module_entry php_ftp_module_entry = {
95
STANDARD_MODULE_HEADER,
104
STANDARD_MODULE_PROPERTIES
108
ZEND_GET_MODULE(php_ftp)
111
static void ftp_destructor_ftpbuf(zend_rsrc_list_entry *rsrc TSRMLS_DC)
113
ftpbuf_t *ftp = (ftpbuf_t *)rsrc->ptr;
118
PHP_MINIT_FUNCTION(ftp)
120
le_ftpbuf = zend_register_list_destructors_ex(ftp_destructor_ftpbuf, NULL, le_ftpbuf_name, module_number);
121
REGISTER_LONG_CONSTANT("FTP_ASCII", FTPTYPE_ASCII, CONST_PERSISTENT | CONST_CS);
122
REGISTER_LONG_CONSTANT("FTP_TEXT", FTPTYPE_ASCII, CONST_PERSISTENT | CONST_CS);
123
REGISTER_LONG_CONSTANT("FTP_BINARY", FTPTYPE_IMAGE, CONST_PERSISTENT | CONST_CS);
124
REGISTER_LONG_CONSTANT("FTP_IMAGE", FTPTYPE_IMAGE, CONST_PERSISTENT | CONST_CS);
125
REGISTER_LONG_CONSTANT("FTP_AUTORESUME", PHP_FTP_AUTORESUME, CONST_PERSISTENT | CONST_CS);
126
REGISTER_LONG_CONSTANT("FTP_TIMEOUT_SEC", PHP_FTP_OPT_TIMEOUT_SEC, CONST_PERSISTENT | CONST_CS);
127
REGISTER_LONG_CONSTANT("FTP_AUTOSEEK", PHP_FTP_OPT_AUTOSEEK, CONST_PERSISTENT | CONST_CS);
128
REGISTER_LONG_CONSTANT("FTP_FAILED", PHP_FTP_FAILED, CONST_PERSISTENT | CONST_CS);
129
REGISTER_LONG_CONSTANT("FTP_FINISHED", PHP_FTP_FINISHED, CONST_PERSISTENT | CONST_CS);
130
REGISTER_LONG_CONSTANT("FTP_MOREDATA", PHP_FTP_MOREDATA, CONST_PERSISTENT | CONST_CS);
134
PHP_MINFO_FUNCTION(ftp)
136
php_info_print_table_start();
137
php_info_print_table_row(2, "FTP support", "enabled");
138
php_info_print_table_end();
141
#define XTYPE(xtype, mode) { \
142
if (mode != FTPTYPE_ASCII && mode != FTPTYPE_IMAGE) { \
143
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Mode must be FTP_ASCII or FTP_BINARY"); \
150
/* {{{ proto resource ftp_connect(string host [, int port [, int timeout]])
151
Opens a FTP stream */
152
PHP_FUNCTION(ftp_connect)
158
long timeout_sec = FTP_DEFAULT_TIMEOUT;
160
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
164
if (timeout_sec <= 0) {
165
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
170
if (!(ftp = ftp_open(host, (short)port, timeout_sec TSRMLS_CC))) {
174
/* autoseek for resuming */
175
ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
181
ZEND_REGISTER_RESOURCE(return_value, ftp, le_ftpbuf);
186
/* {{{ proto resource ftp_ssl_connect(string host [, int port [, int timeout]])
187
Opens a FTP-SSL stream */
188
PHP_FUNCTION(ftp_ssl_connect)
194
long timeout_sec = FTP_DEFAULT_TIMEOUT;
196
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &host, &host_len, &port, &timeout_sec) == FAILURE) {
200
if (timeout_sec <= 0) {
201
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
206
if (!(ftp = ftp_open(host, (short)port, timeout_sec TSRMLS_CC))) {
210
/* autoseek for resuming */
211
ftp->autoseek = FTP_DEFAULT_AUTOSEEK;
215
ZEND_REGISTER_RESOURCE(return_value, ftp, le_ftpbuf);
220
/* {{{ proto bool ftp_login(resource stream, string username, string password)
221
Logs into the FTP server */
222
PHP_FUNCTION(ftp_login)
227
int user_len, pass_len;
229
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &z_ftp, &user, &user_len, &pass, &pass_len) == FAILURE) {
233
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
236
if (!ftp_login(ftp, user, pass TSRMLS_CC)) {
237
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
245
/* {{{ proto string ftp_pwd(resource stream)
246
Returns the present working directory */
247
PHP_FUNCTION(ftp_pwd)
253
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
257
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
259
if (!(pwd = ftp_pwd(ftp))) {
260
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
264
RETURN_STRING((char*) pwd, 1);
268
/* {{{ proto bool ftp_cdup(resource stream)
269
Changes to the parent directory */
270
PHP_FUNCTION(ftp_cdup)
275
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
279
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
281
if (!ftp_cdup(ftp)) {
282
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
290
/* {{{ proto bool ftp_chdir(resource stream, string directory)
291
Changes directories */
292
PHP_FUNCTION(ftp_chdir)
299
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
303
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
305
/* change directories */
306
if (!ftp_chdir(ftp, dir)) {
307
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
315
/* {{{ proto bool ftp_exec(resource stream, string command)
316
Requests execution of a program on the FTP server */
317
PHP_FUNCTION(ftp_exec)
324
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
328
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
330
/* execute serverside command */
331
if (!ftp_exec(ftp, cmd)) {
332
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
340
/* {{{ proto array ftp_raw(resource stream, string command)
341
Sends a literal command to the FTP server */
342
PHP_FUNCTION(ftp_raw)
349
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
353
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
355
/* execute arbitrary ftp command */
356
ftp_raw(ftp, cmd, return_value);
360
/* {{{ proto string ftp_mkdir(resource stream, string directory)
361
Creates a directory and returns the absolute path for the new directory or false on error */
362
PHP_FUNCTION(ftp_mkdir)
369
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
373
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
375
/* create directorie */
376
if (NULL == (tmp = ftp_mkdir(ftp, dir))) {
377
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
381
RETURN_STRING(tmp, 0);
385
/* {{{ proto bool ftp_rmdir(resource stream, string directory)
386
Removes a directory */
387
PHP_FUNCTION(ftp_rmdir)
394
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
398
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
400
/* remove directorie */
401
if (!ftp_rmdir(ftp, dir)) {
402
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
410
/* {{{ proto int ftp_chmod(resource stream, int mode, string filename)
411
Sets permissions on a file */
412
PHP_FUNCTION(ftp_chmod)
420
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls", &z_ftp, &mode, &filename, &filename_len) == FAILURE) {
424
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
426
if (!ftp_chmod(ftp, mode, filename, filename_len)) {
427
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
435
/* {{{ proto bool ftp_alloc(resource stream, int size[, &response])
436
Attempt to allocate space on the remote FTP server */
437
PHP_FUNCTION(ftp_alloc)
439
zval *z_ftp, *zresponse = NULL;
442
char *response = NULL;
444
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &z_ftp, &size, &zresponse) == FAILURE) {
448
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
450
ret = ftp_alloc(ftp, size, zresponse ? &response : NULL);
452
zval_dtor(zresponse);
453
ZVAL_STRING(zresponse, response, 0);
464
/* {{{ proto array ftp_nlist(resource stream, string directory)
465
Returns an array of filenames in the given directory */
466
PHP_FUNCTION(ftp_nlist)
470
char **nlist, **ptr, *dir;
473
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &dir, &dir_len) == FAILURE) {
477
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
479
/* get list of files */
480
if (NULL == (nlist = ftp_nlist(ftp, dir TSRMLS_CC))) {
484
array_init(return_value);
485
for (ptr = nlist; *ptr; ptr++) {
486
add_next_index_string(return_value, *ptr, 1);
492
/* {{{ proto array ftp_rawlist(resource stream, string directory [, bool recursive])
493
Returns a detailed listing of a directory as an array of output lines */
494
PHP_FUNCTION(ftp_rawlist)
498
char **llist, **ptr, *dir;
500
zend_bool recursive = 0;
502
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b", &z_ftp, &dir, &dir_len, &recursive) == FAILURE) {
506
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
508
/* get raw directory listing */
509
if (NULL == (llist = ftp_list(ftp, dir, recursive TSRMLS_CC))) {
513
array_init(return_value);
514
for (ptr = llist; *ptr; ptr++) {
515
add_next_index_string(return_value, *ptr, 1);
521
/* {{{ proto string ftp_systype(resource stream)
522
Returns the system type identifier */
523
PHP_FUNCTION(ftp_systype)
529
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
533
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
535
if (NULL == (syst = ftp_syst(ftp))) {
536
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
540
RETURN_STRING((char*) syst, 1);
544
/* {{{ proto bool ftp_fget(resource stream, resource fp, string remote_file, int mode[, int resumepos])
545
Retrieves a file from the FTP server and writes it to an open file */
546
PHP_FUNCTION(ftp_fget)
548
zval *z_ftp, *z_file;
554
long mode, resumepos=0;
556
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrsl|l", &z_ftp, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
560
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
561
php_stream_from_zval(stream, &z_file);
564
/* ignore autoresume if autoseek is switched off */
565
if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
569
if (ftp->autoseek && resumepos) {
570
/* if autoresume is wanted seek to end */
571
if (resumepos == PHP_FTP_AUTORESUME) {
572
php_stream_seek(stream, 0, SEEK_END);
573
resumepos = php_stream_tell(stream);
575
php_stream_seek(stream, resumepos, SEEK_SET);
579
if (!ftp_get(ftp, stream, file, xtype, resumepos TSRMLS_CC)) {
580
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
588
/* {{{ proto int ftp_nb_fget(resource stream, resource fp, string remote_file, int mode[, int resumepos])
589
Retrieves a file from the FTP server asynchronly and writes it to an open file */
590
PHP_FUNCTION(ftp_nb_fget)
592
zval *z_ftp, *z_file;
598
long mode, resumepos=0;
600
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrsl|l", &z_ftp, &z_file, &file, &file_len, &mode, &resumepos) == FAILURE) {
604
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
605
php_stream_from_zval(stream, &z_file);
608
/* ignore autoresume if autoseek is switched off */
609
if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
613
if (ftp->autoseek && resumepos) {
614
/* if autoresume is wanted seek to end */
615
if (resumepos == PHP_FTP_AUTORESUME) {
616
php_stream_seek(stream, 0, SEEK_END);
617
resumepos = php_stream_tell(stream);
619
php_stream_seek(stream, resumepos, SEEK_SET);
624
ftp->direction = 0; /* recv */
625
ftp->closestream = 0; /* do not close */
627
if ((ret = ftp_nb_get(ftp, stream, file, xtype, resumepos TSRMLS_CC)) == PHP_FTP_FAILED) {
628
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
636
/* {{{ proto bool ftp_pasv(resource stream, bool pasv)
637
Turns passive mode on or off */
638
PHP_FUNCTION(ftp_pasv)
644
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &z_ftp, &pasv) == FAILURE) {
648
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
650
if (!ftp_pasv(ftp, pasv ? 1 : 0)) {
658
/* {{{ proto bool ftp_get(resource stream, string local_file, string remote_file, int mode[, int resume_pos])
659
Retrieves a file from the FTP server and writes it to a local file */
660
PHP_FUNCTION(ftp_get)
665
php_stream *outstream;
666
char *local, *remote;
667
int local_len, remote_len;
668
long mode, resumepos=0;
670
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rssl|l", &z_ftp, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
674
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
677
/* ignore autoresume if autoseek is switched off */
678
if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
683
mode = FTPTYPE_IMAGE;
686
if (ftp->autoseek && resumepos) {
687
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
688
if (outstream == NULL) {
689
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
691
if (outstream != NULL) {
692
/* if autoresume is wanted seek to end */
693
if (resumepos == PHP_FTP_AUTORESUME) {
694
php_stream_seek(outstream, 0, SEEK_END);
695
resumepos = php_stream_tell(outstream);
697
php_stream_seek(outstream, resumepos, SEEK_SET);
701
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
704
if (outstream == NULL) {
705
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error opening %s", local);
709
if (!ftp_get(ftp, outstream, remote, xtype, resumepos TSRMLS_CC)) {
710
php_stream_close(outstream);
712
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
716
php_stream_close(outstream);
721
/* {{{ proto int ftp_nb_get(resource stream, string local_file, string remote_file, int mode[, int resume_pos])
722
Retrieves a file from the FTP server nbhronly and writes it to a local file */
723
PHP_FUNCTION(ftp_nb_get)
728
php_stream *outstream;
729
char *local, *remote;
730
int local_len, remote_len, ret;
731
long mode, resumepos=0;
733
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rssl|l", &z_ftp, &local, &local_len, &remote, &remote_len, &mode, &resumepos) == FAILURE) {
737
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
740
/* ignore autoresume if autoseek is switched off */
741
if (!ftp->autoseek && resumepos == PHP_FTP_AUTORESUME) {
745
mode = FTPTYPE_IMAGE;
747
if (ftp->autoseek && resumepos) {
748
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt+" : "rb+", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
749
if (outstream == NULL) {
750
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
752
if (outstream != NULL) {
753
/* if autoresume is wanted seek to end */
754
if (resumepos == PHP_FTP_AUTORESUME) {
755
php_stream_seek(outstream, 0, SEEK_END);
756
resumepos = php_stream_tell(outstream);
758
php_stream_seek(outstream, resumepos, SEEK_SET);
762
outstream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "wt" : "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
765
if (outstream == NULL) {
766
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error opening %s", local);
771
ftp->direction = 0; /* recv */
772
ftp->closestream = 1; /* do close */
774
if ((ret = ftp_nb_get(ftp, outstream, remote, xtype, resumepos TSRMLS_CC)) == PHP_FTP_FAILED) {
775
php_stream_close(outstream);
777
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
778
RETURN_LONG(PHP_FTP_FAILED);
781
if (ret == PHP_FTP_FINISHED) {
782
php_stream_close(outstream);
789
/* {{{ proto int ftp_nb_continue(resource stream)
790
Continues retrieving/sending a file nbronously */
791
PHP_FUNCTION(ftp_nb_continue)
797
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
801
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
804
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no nbronous transfer to continue.");
805
RETURN_LONG(PHP_FTP_FAILED);
808
if (ftp->direction) {
809
ret=ftp_nb_continue_write(ftp TSRMLS_CC);
811
ret=ftp_nb_continue_read(ftp TSRMLS_CC);
814
if (ret != PHP_FTP_MOREDATA && ftp->closestream) {
815
php_stream_close(ftp->stream);
818
if (ret == PHP_FTP_FAILED) {
819
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
826
/* {{{ proto bool ftp_fput(resource stream, string remote_file, resource fp, int mode[, int startpos])
827
Stores a file from an open file to the FTP server */
828
PHP_FUNCTION(ftp_fput)
830
zval *z_ftp, *z_file;
834
long mode, startpos=0;
838
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrl|l", &z_ftp, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
842
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
843
php_stream_from_zval(stream, &z_file);
846
/* ignore autoresume if autoseek is switched off */
847
if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
851
if (ftp->autoseek && startpos) {
852
/* if autoresume is wanted ask for remote size */
853
if (startpos == PHP_FTP_AUTORESUME) {
854
startpos = ftp_size(ftp, remote);
860
php_stream_seek(stream, startpos, SEEK_SET);
864
if (!ftp_put(ftp, remote, stream, xtype, startpos TSRMLS_CC)) {
865
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
873
/* {{{ proto int ftp_nb_fput(resource stream, string remote_file, resource fp, int mode[, int startpos])
874
Stores a file from an open file to the FTP server nbronly */
875
PHP_FUNCTION(ftp_nb_fput)
877
zval *z_ftp, *z_file;
881
long mode, startpos=0;
885
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrl|l", &z_ftp, &remote, &remote_len, &z_file, &mode, &startpos) == FAILURE) {
889
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
890
php_stream_from_zval(stream, &z_file);
893
/* ignore autoresume if autoseek is switched off */
894
if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
898
if (ftp->autoseek && startpos) {
899
/* if autoresume is wanted ask for remote size */
900
if (startpos == PHP_FTP_AUTORESUME) {
901
startpos = ftp_size(ftp, remote);
907
php_stream_seek(stream, startpos, SEEK_SET);
912
ftp->direction = 1; /* send */
913
ftp->closestream = 0; /* do not close */
915
if (((ret = ftp_nb_put(ftp, remote, stream, xtype, startpos TSRMLS_CC)) == PHP_FTP_FAILED)) {
916
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
925
/* {{{ proto bool ftp_put(resource stream, string remote_file, string local_file, int mode[, int startpos])
926
Stores a file on the FTP server */
927
PHP_FUNCTION(ftp_put)
932
char *remote, *local;
933
int remote_len, local_len;
934
long mode, startpos=0;
935
php_stream *instream;
937
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rssl|l", &z_ftp, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
941
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
944
if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) {
948
/* ignore autoresume if autoseek is switched off */
949
if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
953
if (ftp->autoseek && startpos) {
954
/* if autoresume is wanted ask for remote size */
955
if (startpos == PHP_FTP_AUTORESUME) {
956
startpos = ftp_size(ftp, remote);
962
php_stream_seek(instream, startpos, SEEK_SET);
966
if (!ftp_put(ftp, remote, instream, xtype, startpos TSRMLS_CC)) {
967
php_stream_close(instream);
968
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
971
php_stream_close(instream);
978
/* {{{ proto int ftp_nb_put(resource stream, string remote_file, string local_file, int mode[, int startpos])
979
Stores a file on the FTP server */
980
PHP_FUNCTION(ftp_nb_put)
985
char *remote, *local;
986
int remote_len, local_len, ret;
987
long mode, startpos=0;
988
php_stream *instream;
990
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rssl|l", &z_ftp, &remote, &remote_len, &local, &local_len, &mode, &startpos) == FAILURE) {
994
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
997
if (!(instream = php_stream_open_wrapper(local, mode == FTPTYPE_ASCII ? "rt" : "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) {
1001
/* ignore autoresume if autoseek is switched off */
1002
if (!ftp->autoseek && startpos == PHP_FTP_AUTORESUME) {
1006
if (ftp->autoseek && startpos) {
1007
/* if autoresume is wanted ask for remote size */
1008
if (startpos == PHP_FTP_AUTORESUME) {
1009
startpos = ftp_size(ftp, remote);
1015
php_stream_seek(instream, startpos, SEEK_SET);
1020
ftp->direction = 1; /* send */
1021
ftp->closestream = 1; /* do close */
1023
ret = ftp_nb_put(ftp, remote, instream, xtype, startpos TSRMLS_CC);
1025
if (ret != PHP_FTP_MOREDATA) {
1026
php_stream_close(instream);
1029
if (ret == PHP_FTP_FAILED) {
1030
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
1037
/* {{{ proto int ftp_size(resource stream, string filename)
1038
Returns the size of the file, or -1 on error */
1039
PHP_FUNCTION(ftp_size)
1046
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &file, &file_len) == FAILURE) {
1050
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1053
RETURN_LONG(ftp_size(ftp, file));
1057
/* {{{ proto int ftp_mdtm(resource stream, string filename)
1058
Returns the last modification time of the file, or -1 on error */
1059
PHP_FUNCTION(ftp_mdtm)
1066
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &file, &file_len) == FAILURE) {
1070
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1072
/* get file mod time */
1073
RETURN_LONG(ftp_mdtm(ftp, file));
1077
/* {{{ proto bool ftp_rename(resource stream, string src, string dest)
1078
Renames the given file to a new path */
1079
PHP_FUNCTION(ftp_rename)
1084
int src_len, dest_len;
1086
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &z_ftp, &src, &src_len, &dest, &dest_len) == FAILURE) {
1090
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1092
/* rename the file */
1093
if (!ftp_rename(ftp, src, dest)) {
1094
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
1102
/* {{{ proto bool ftp_delete(resource stream, string file)
1104
PHP_FUNCTION(ftp_delete)
1111
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &file, &file_len) == FAILURE) {
1115
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1117
/* delete the file */
1118
if (!ftp_delete(ftp, file)) {
1119
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
1127
/* {{{ proto bool ftp_site(resource stream, string cmd)
1128
Sends a SITE command to the server */
1129
PHP_FUNCTION(ftp_site)
1136
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
1140
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1142
/* send the site command */
1143
if (!ftp_site(ftp, cmd)) {
1144
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", ftp->inbuf);
1152
/* {{{ proto bool ftp_close(resource stream)
1153
Closes the FTP stream */
1154
PHP_FUNCTION(ftp_close)
1159
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_ftp) == FAILURE) {
1163
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1167
RETURN_BOOL(zend_list_delete(Z_LVAL_P(z_ftp)) == SUCCESS);
1171
/* {{{ proto bool ftp_set_option(resource stream, int option, mixed value)
1172
Sets an FTP option */
1173
PHP_FUNCTION(ftp_set_option)
1175
zval *z_ftp, *z_value;
1179
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &z_ftp, &option, &z_value) == FAILURE) {
1183
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1186
case PHP_FTP_OPT_TIMEOUT_SEC:
1187
if (Z_TYPE_P(z_value) != IS_LONG) {
1188
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Option TIMEOUT_SEC expects value of type long, %s given",
1189
zend_zval_type_name(z_value));
1192
if (Z_LVAL_P(z_value) <= 0) {
1193
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Timeout has to be greater than 0");
1196
ftp->timeout_sec = Z_LVAL_P(z_value);
1199
case PHP_FTP_OPT_AUTOSEEK:
1200
if (Z_TYPE_P(z_value) != IS_BOOL) {
1201
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Option AUTOSEEK expects value of type boolean, %s given",
1202
zend_zval_type_name(z_value));
1205
ftp->autoseek = Z_LVAL_P(z_value);
1209
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown option '%ld'", option);
1216
/* {{{ proto mixed ftp_get_option(resource stream, int option)
1217
Gets an FTP option */
1218
PHP_FUNCTION(ftp_get_option)
1224
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &z_ftp, &option) == FAILURE) {
1228
ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);
1231
case PHP_FTP_OPT_TIMEOUT_SEC:
1232
RETURN_LONG(ftp->timeout_sec);
1234
case PHP_FTP_OPT_AUTOSEEK:
1235
RETURN_BOOL(ftp->autoseek);
1238
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown option '%ld'", option);
1245
#endif /* HAVE_FTP */
1251
* indent-tabs-mode: t