summaryrefslogtreecommitdiff
path: root/ext/ftp
diff options
context:
space:
mode:
authorMark A. Hershberger <mah@debian.(none)>2009-03-25 00:37:27 -0400
committerMark A. Hershberger <mah@debian.(none)>2009-03-25 00:37:27 -0400
commit2d4e5b09576bb4f0ba716cc82cdf29ea04d9184b (patch)
tree41ccc042009cba53e4ce43e727fcba4c1cfbf7f3 /ext/ftp
parentd29a4fd2dd3b5d4cf6e80b602544d7b71d794e76 (diff)
downloadphp-2d4e5b09576bb4f0ba716cc82cdf29ea04d9184b.tar.gz
Imported Upstream version 5.2.2upstream/5.2.2
Diffstat (limited to 'ext/ftp')
-rw-r--r--ext/ftp/ftp.c118
-rw-r--r--ext/ftp/ftp.h6
-rw-r--r--ext/ftp/package.xml2
-rw-r--r--ext/ftp/php_ftp.c4
-rw-r--r--ext/ftp/php_ftp.h4
-rw-r--r--ext/ftp/tests/001.phpt36
-rw-r--r--ext/ftp/tests/002.phpt38
-rw-r--r--ext/ftp/tests/003.phpt43
-rw-r--r--ext/ftp/tests/004.phpt79
-rw-r--r--ext/ftp/tests/005.phpt86
-rw-r--r--ext/ftp/tests/006.phpt100
-rw-r--r--ext/ftp/tests/bug27809.phpt21
-rw-r--r--ext/ftp/tests/bug37799.phpt22
-rw-r--r--ext/ftp/tests/bug39458-2.phpt36
-rw-r--r--ext/ftp/tests/bug39458.phpt35
-rw-r--r--ext/ftp/tests/bug39583-2.phpt34
-rw-r--r--ext/ftp/tests/bug39583.phpt35
-rw-r--r--ext/ftp/tests/bug7216-2.phpt21
-rw-r--r--ext/ftp/tests/bug7216.phpt21
-rw-r--r--ext/ftp/tests/cert.pem48
-rw-r--r--ext/ftp/tests/server.inc258
-rw-r--r--ext/ftp/tests/skipif.inc5
22 files changed, 988 insertions, 64 deletions
diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c
index 08db6a2ff..7d3e6b197 100644
--- a/ext/ftp/ftp.c
+++ b/ext/ftp/ftp.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2006 The PHP Group |
+ | Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: ftp.c,v 1.112.2.4 2006/04/03 09:14:33 tony2001 Exp $ */
+/* $Id: ftp.c,v 1.112.2.4.2.8 2007/03/24 16:25:42 iliaa Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -266,60 +266,57 @@ ftp_login(ftpbuf_t *ftp, const char *user, const char *pass TSRMLS_DC)
}
if (ftp->resp != 334) {
- ftp->use_ssl = 0;
+ return 0;
} else {
ftp->old_ssl = 1;
ftp->use_ssl_for_data = 1;
}
}
- /* now enable ssl if we still need to */
- if (ftp->use_ssl) {
- ctx = SSL_CTX_new(SSLv23_client_method());
- if (ctx == NULL) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create the SSL context");
+ ctx = SSL_CTX_new(SSLv23_client_method());
+ if (ctx == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create the SSL context");
+ return 0;
+ }
+
+ SSL_CTX_set_options(ctx, SSL_OP_ALL);
+
+ ftp->ssl_handle = SSL_new(ctx);
+ if (ftp->ssl_handle == NULL) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create the SSL handle");
+ SSL_CTX_free(ctx);
+ return 0;
+ }
+
+ SSL_set_fd(ftp->ssl_handle, ftp->fd);
+
+ if (SSL_connect(ftp->ssl_handle) <= 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL/TLS handshake failed");
+ SSL_shutdown(ftp->ssl_handle);
+ return 0;
+ }
+
+ ftp->ssl_active = 1;
+
+ if (!ftp->old_ssl) {
+
+ /* set protection buffersize to zero */
+ if (!ftp_putcmd(ftp, "PBSZ", "0")) {
+ return 0;
+ }
+ if (!ftp_getresp(ftp)) {
return 0;
}
- SSL_CTX_set_options(ctx, SSL_OP_ALL);
-
- ftp->ssl_handle = SSL_new(ctx);
- if (ftp->ssl_handle == NULL) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create the SSL handle");
- SSL_CTX_free(ctx);
+ /* enable data conn encryption */
+ if (!ftp_putcmd(ftp, "PROT", "P")) {
return 0;
}
-
- SSL_set_fd(ftp->ssl_handle, ftp->fd);
-
- if (SSL_connect(ftp->ssl_handle) <= 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL/TLS handshake failed");
- SSL_shutdown(ftp->ssl_handle);
+ if (!ftp_getresp(ftp)) {
return 0;
}
- ftp->ssl_active = 1;
-
- if (!ftp->old_ssl) {
-
- /* set protection buffersize to zero */
- if (!ftp_putcmd(ftp, "PBSZ", "0")) {
- return 0;
- }
- if (!ftp_getresp(ftp)) {
- return 0;
- }
-
- /* enable data conn encryption */
- if (!ftp_putcmd(ftp, "PROT", "P")) {
- return 0;
- }
- if (!ftp_getresp(ftp)) {
- return 0;
- }
-
- ftp->use_ssl_for_data = (ftp->resp >= 200 && ftp->resp <=299);
- }
+ ftp->use_ssl_for_data = (ftp->resp >= 200 && ftp->resp <=299);
}
}
#endif
@@ -589,11 +586,7 @@ ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filenam
return 0;
}
- if (!(buffer = emalloc(32 + filename_len + 1))) {
- return 0;
- }
-
- sprintf(buffer, "CHMOD %o %s", mode, filename);
+ spprintf(&buffer, 0, "CHMOD %o %s", mode, filename);
if (!ftp_putcmd(ftp, "SITE", buffer)) {
efree(buffer);
@@ -813,7 +806,7 @@ ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, ftptype_t type,
php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHP cannot handle files greater than 2147483647 bytes.");
goto bail;
}
- sprintf(arg, "%u", resumepos);
+ snprintf(arg, sizeof(arg), "%u", resumepos);
if (!ftp_putcmd(ftp, "REST", arg)) {
goto bail;
}
@@ -910,7 +903,7 @@ ftp_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type, i
php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHP cannot handle files with a size greater than 2147483647 bytes.");
goto bail;
}
- sprintf(arg, "%u", startpos);
+ snprintf(arg, sizeof(arg), "%u", startpos);
if (!ftp_putcmd(ftp, "REST", arg)) {
goto bail;
}
@@ -1103,19 +1096,25 @@ ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const char *args)
int size;
char *data;
+ if (strpbrk(cmd, "\r\n")) {
+ return 0;
+ }
/* build the output buffer */
if (args && args[0]) {
/* "cmd args\r\n\0" */
if (strlen(cmd) + strlen(args) + 4 > FTP_BUFSIZE) {
return 0;
}
- size = sprintf(ftp->outbuf, "%s %s\r\n", cmd, args);
+ if (strpbrk(args, "\r\n")) {
+ return 0;
+ }
+ size = slprintf(ftp->outbuf, sizeof(ftp->outbuf), "%s %s\r\n", cmd, args);
} else {
/* "cmd\r\n\0" */
if (strlen(cmd) + 3 > FTP_BUFSIZE) {
return 0;
}
- size = sprintf(ftp->outbuf, "%s\r\n", cmd);
+ size = slprintf(ftp->outbuf, sizeof(ftp->outbuf), "%s\r\n", cmd);
}
data = ftp->outbuf;
@@ -1441,7 +1440,7 @@ ftp_getdata(ftpbuf_t *ftp TSRMLS_DC)
char eprtarg[INET6_ADDRSTRLEN + sizeof("|x||xxxxx|")];
char out[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &((struct sockaddr_in6*) sa)->sin6_addr, out, sizeof(out));
- sprintf(eprtarg, "|2|%s|%hu|", out, ntohs(((struct sockaddr_in6 *) &addr)->sin6_port));
+ snprintf(eprtarg, sizeof(eprtarg), "|2|%s|%hu|", out, ntohs(((struct sockaddr_in6 *) &addr)->sin6_port));
if (!ftp_putcmd(ftp, "EPRT", eprtarg)) {
goto bail;
@@ -1459,7 +1458,7 @@ ftp_getdata(ftpbuf_t *ftp TSRMLS_DC)
/* send the PORT */
ipbox.ia[0] = ((struct sockaddr_in*) sa)->sin_addr;
ipbox.s[2] = ((struct sockaddr_in*) &addr)->sin_port;
- sprintf(arg, "%u,%u,%u,%u,%u,%u", ipbox.c[0], ipbox.c[1], ipbox.c[2], ipbox.c[3], ipbox.c[4], ipbox.c[5]);
+ snprintf(arg, sizeof(arg), "%u,%u,%u,%u,%u,%u", ipbox.c[0], ipbox.c[1], ipbox.c[2], ipbox.c[3], ipbox.c[4], ipbox.c[5]);
if (!ftp_putcmd(ftp, "PORT", arg)) {
goto bail;
@@ -1614,10 +1613,17 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const char *path TSRMLS_DC)
if (!ftp_putcmd(ftp, cmd, path)) {
goto bail;
}
- if (!ftp_getresp(ftp) || (ftp->resp != 150 && ftp->resp != 125)) {
+ if (!ftp_getresp(ftp) || (ftp->resp != 150 && ftp->resp != 125 && ftp->resp != 226)) {
goto bail;
}
+ /* some servers don't open a ftp-data connection if the directory is empty */
+ if (ftp->resp == 226) {
+ ftp->data = data_close(ftp, data);
+ php_stream_close(tmpstream);
+ return ecalloc(1, sizeof(char**));
+ }
+
/* pull data buffer into tmpfile */
if ((data = data_accept(data, ftp TSRMLS_CC)) == NULL) {
goto bail;
@@ -1709,7 +1715,7 @@ ftp_nb_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, ftptype_t typ
php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHP cannot handle files greater than 2147483648 bytes.");
goto bail;
}
- sprintf(arg, "%u", resumepos);
+ snprintf(arg, sizeof(arg), "%u", resumepos);
if (!ftp_putcmd(ftp, "REST", arg)) {
goto bail;
}
@@ -1827,7 +1833,7 @@ ftp_nb_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type
php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHP cannot handle files with a size greater than 2147483647 bytes.");
goto bail;
}
- sprintf(arg, "%u", startpos);
+ snprintf(arg, sizeof(arg), "%u", startpos);
if (!ftp_putcmd(ftp, "REST", arg)) {
goto bail;
}
diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h
index 2bc501cfe..fcfb72089 100644
--- a/ext/ftp/ftp.h
+++ b/ext/ftp/ftp.h
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2006 The PHP Group |
+ | Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: ftp.h,v 1.43.2.1 2006/01/01 12:50:06 sniper Exp $ */
+/* $Id: ftp.h,v 1.43.2.1.2.2 2007/01/01 09:36:01 sebastian Exp $ */
#ifndef FTP_H
#define FTP_H
@@ -39,7 +39,7 @@
#define FTP_BUFSIZE 4096
typedef enum ftptype {
- FTPTYPE_ASCII,
+ FTPTYPE_ASCII=1,
FTPTYPE_IMAGE
} ftptype_t;
diff --git a/ext/ftp/package.xml b/ext/ftp/package.xml
index c1152cb3e..ecb2637ee 100644
--- a/ext/ftp/package.xml
+++ b/ext/ftp/package.xml
@@ -33,7 +33,7 @@ and more intuitive interface.
<version>5.0.0rc1</version>
<date>2004-03-19</date>
<notes>
-package.xml added to support intallation using pear installer
+package.xml added to support installation using pear installer
</notes>
<filelist>
<file role="doc" name="CREDITS"/>
diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c
index 9274abe88..2b55f8e0d 100644
--- a/ext/ftp/php_ftp.c
+++ b/ext/ftp/php_ftp.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2006 The PHP Group |
+ | Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_ftp.c,v 1.103.2.2.2.1 2006/06/11 20:03:05 bjori Exp $ */
+/* $Id: php_ftp.c,v 1.103.2.2.2.2 2007/01/01 09:36:01 sebastian Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
diff --git a/ext/ftp/php_ftp.h b/ext/ftp/php_ftp.h
index d40a07883..8498bfe5d 100644
--- a/ext/ftp/php_ftp.h
+++ b/ext/ftp/php_ftp.h
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2006 The PHP Group |
+ | Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_ftp.h,v 1.29.2.1 2006/01/01 12:50:06 sniper Exp $ */
+/* $Id: php_ftp.h,v 1.29.2.1.2.1 2007/01/01 09:36:01 sebastian Exp $ */
#ifndef _INCLUDED_FTP_H
#define _INCLUDED_FTP_H
diff --git a/ext/ftp/tests/001.phpt b/ext/ftp/tests/001.phpt
new file mode 100644
index 000000000..38475d3ff
--- /dev/null
+++ b/ext/ftp/tests/001.phpt
@@ -0,0 +1,36 @@
+--TEST--
+FTP login
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+var_dump(ftp_raw($ftp, 'HELP'));
+var_dump(ftp_raw($ftp, 'HELP HELP'));
+
+var_dump(ftp_close($ftp));
+?>
+--EXPECT--
+bool(true)
+array(4) {
+ [0]=>
+ string(55) "214-There is help available for the following commands:"
+ [1]=>
+ string(5) " USER"
+ [2]=>
+ string(5) " HELP"
+ [3]=>
+ string(15) "214 end of list"
+}
+array(1) {
+ [0]=>
+ string(39) "214 Syntax: HELP [<SP> <string>] <CRLF>"
+}
+bool(true)
diff --git a/ext/ftp/tests/002.phpt b/ext/ftp/tests/002.phpt
new file mode 100644
index 000000000..e27113f61
--- /dev/null
+++ b/ext/ftp/tests/002.phpt
@@ -0,0 +1,38 @@
+--TEST--
+FTP login (SSL)
+--SKIPIF--
+<?php
+$ssl = 1;
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$ssl = 1;
+require 'server.inc';
+
+$ftp = ftp_ssl_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+var_dump(ftp_raw($ftp, 'HELP'));
+var_dump(ftp_raw($ftp, 'HELP HELP'));
+
+var_dump(ftp_close($ftp));
+?>
+--EXPECT--
+bool(true)
+array(4) {
+ [0]=>
+ string(55) "214-There is help available for the following commands:"
+ [1]=>
+ string(5) " USER"
+ [2]=>
+ string(5) " HELP"
+ [3]=>
+ string(15) "214 end of list"
+}
+array(1) {
+ [0]=>
+ string(39) "214 Syntax: HELP [<SP> <string>] <CRLF>"
+}
+bool(true)
diff --git a/ext/ftp/tests/003.phpt b/ext/ftp/tests/003.phpt
new file mode 100644
index 000000000..db57ed56b
--- /dev/null
+++ b/ext/ftp/tests/003.phpt
@@ -0,0 +1,43 @@
+--TEST--
+FTP cwd
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_pwd($ftp));
+
+var_dump(ftp_chdir($ftp, 'mydir'));
+var_dump(ftp_pwd($ftp));
+
+var_dump(ftp_chdir($ftp, '/xpto/mydir'));
+var_dump(ftp_pwd($ftp));
+
+var_dump(ftp_cdup($ftp));
+var_dump(ftp_pwd($ftp));
+
+var_dump(ftp_chdir($ftp, '..'));
+var_dump(ftp_pwd($ftp));
+
+var_dump(ftp_close($ftp));
+?>
+--EXPECT--
+bool(true)
+string(1) "/"
+bool(true)
+string(6) "/mydir"
+bool(true)
+string(11) "/xpto/mydir"
+bool(true)
+string(5) "/xpto"
+bool(true)
+string(1) "/"
+bool(true)
diff --git a/ext/ftp/tests/004.phpt b/ext/ftp/tests/004.phpt
new file mode 100644
index 000000000..df6951b09
--- /dev/null
+++ b/ext/ftp/tests/004.phpt
@@ -0,0 +1,79 @@
+--TEST--
+FTP with bogus parameters
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_systype($ftp));
+
+/* some bogus usage */
+var_dump(ftp_alloc($ftp, array()));
+var_dump(ftp_cdup($ftp, 0));
+var_dump(ftp_chdir($ftp, array()));
+var_dump(ftp_chmod($ftp, 0666));
+var_dump(ftp_get($ftp, 1234,12));
+var_dump(ftp_close());
+var_dump(ftp_connect('sfjkfjaksfjkasjf'));
+var_dump(ftp_delete($ftp, array()));
+var_dump(ftp_exec($ftp, array()));
+
+var_dump(ftp_systype($ftp, 0));
+var_dump(ftp_pwd($ftp, array()));
+
+var_dump(ftp_login($ftp));
+var_dump(ftp_login($ftp, 'user', 'bogus'));
+
+var_dump(ftp_quit($ftp));
+?>
+--EXPECTF--
+bool(true)
+string(4) "UNIX"
+
+Warning: ftp_alloc() expects parameter 2 to be long, array given in %s004.php on line 12
+bool(false)
+
+Warning: ftp_cdup() expects exactly 1 parameter, 2 given in %s004.php on line 13
+NULL
+
+Warning: ftp_chdir() expects parameter 2 to be string, array given in %s004.php on line 14
+NULL
+
+Warning: ftp_chmod() expects exactly 3 parameters, 2 given in %s on line %d
+bool(false)
+
+Warning: ftp_get() expects at least 4 parameters, 3 given in %s on line %d
+NULL
+
+Warning: ftp_close() expects exactly 1 parameter, 0 given in %s004.php on line 17
+NULL
+
+Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: %s in %s004.php on line 18
+bool(false)
+
+Warning: ftp_delete() expects parameter 2 to be string, array given in %s004.php on line 19
+NULL
+
+Warning: ftp_exec() expects parameter 2 to be string, array given in %s004.php on line 20
+NULL
+
+Warning: ftp_systype() expects exactly 1 parameter, 2 given in %s004.php on line 22
+NULL
+
+Warning: ftp_pwd() expects exactly 1 parameter, 2 given in %s004.php on line 23
+NULL
+
+Warning: ftp_login() expects exactly 3 parameters, 1 given in %s004.php on line 25
+NULL
+
+Warning: ftp_login(): Not logged in. in %s004.php on line 26
+bool(false)
+bool(true)
diff --git a/ext/ftp/tests/005.phpt b/ext/ftp/tests/005.phpt
new file mode 100644
index 000000000..bbc11e87a
--- /dev/null
+++ b/ext/ftp/tests/005.phpt
@@ -0,0 +1,86 @@
+--TEST--
+FTP with bogus server responses
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bogus = 1;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'anonymous', 'mail@example.com'));
+
+var_dump(ftp_alloc($ftp, 400));
+var_dump(ftp_cdup($ftp));
+var_dump(ftp_chdir($ftp, '~'));
+var_dump(ftp_chmod($ftp, 0666, 'x'));
+var_dump(ftp_delete($ftp, 'x'));
+var_dump(ftp_exec($ftp, 'x'));
+var_dump(ftp_fget($ftp, STDOUT, 'x', 0));
+var_dump(ftp_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));
+var_dump(ftp_get($ftp, 'x', 'y', 0));
+var_dump(ftp_mdtm($ftp, 'x'));
+var_dump(ftp_mkdir($ftp, 'x'));
+var_dump(ftp_nb_continue($ftp));
+var_dump(ftp_nb_fget($ftp, STDOUT, 'x', 0));
+var_dump(ftp_nb_fput($ftp, 'x', fopen(__FILE__, 'r'), 0));
+var_dump(ftp_systype($ftp));
+var_dump(ftp_pwd($ftp));
+var_dump(ftp_size($ftp, ''));
+var_dump(ftp_rmdir($ftp, ''));
+
+?>
+--EXPECTF--
+bool(true)
+bool(false)
+
+Warning: ftp_cdup(): Command not implemented (1). in %s005.php on line 11
+bool(false)
+
+Warning: ftp_chdir(): Command not implemented (2). in %s005.php on line 12
+bool(false)
+
+Warning: ftp_chmod(): Command not implemented (3). in %s005.php on line 13
+bool(false)
+
+Warning: ftp_delete(): Command not implemented (4). in %s005.php on line 14
+bool(false)
+
+Warning: ftp_exec(): Command not implemented (5). in %s005.php on line 15
+bool(false)
+
+Warning: ftp_fget(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 16
+bool(false)
+
+Warning: ftp_fput(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 17
+bool(false)
+
+Warning: ftp_get(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 18
+bool(false)
+int(-1)
+
+Warning: ftp_mkdir(): Command not implemented (7). in %s005.php on line 20
+bool(false)
+
+Warning: ftp_nb_continue(): no nbronous transfer to continue. in %s005.php on line 21
+int(0)
+
+Warning: ftp_nb_fget(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 22
+bool(false)
+
+Warning: ftp_nb_fput(): Mode must be FTP_ASCII or FTP_BINARY in %s005.php on line 23
+bool(false)
+
+Warning: ftp_systype(): Command not implemented (8). in %s005.php on line 24
+bool(false)
+
+Warning: ftp_pwd(): Command not implemented (9). in %s005.php on line 25
+bool(false)
+int(-1)
+
+Warning: ftp_rmdir(): Command not implemented (11). in %s005.php on line 27
+bool(false)
diff --git a/ext/ftp/tests/006.phpt b/ext/ftp/tests/006.phpt
new file mode 100644
index 000000000..4d31be78c
--- /dev/null
+++ b/ext/ftp/tests/006.phpt
@@ -0,0 +1,100 @@
+--TEST--
+FTP with bogus parameters
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$ftp=null;
+
+var_dump(ftp_connect(array()));
+var_dump(ftp_connect('127.0.0.1', 0, -3));
+var_dump(ftp_raw($ftp));
+var_dump(ftp_mkdir($ftp));
+var_dump(ftp_rmdir($ftp));
+var_dump(ftp_nlist($ftp));
+var_dump(ftp_rawlist($ftp));
+var_dump(ftp_fget($ftp));
+var_dump(ftp_nb_fget($ftp));
+var_dump(ftp_nb_get($ftp));
+var_dump(ftp_pasv($ftp));
+var_dump(ftp_nb_continue());
+var_dump(ftp_fput());
+var_dump(ftp_nb_fput($ftp));
+var_dump(ftp_put($ftp));
+var_dump(ftp_nb_put($ftp));
+var_dump(ftp_size($ftp));
+var_dump(ftp_mdtm($ftp));
+var_dump(ftp_rename($ftp));
+var_dump(ftp_site($ftp));
+var_dump(ftp_set_option($ftp));
+var_dump(ftp_get_option($ftp));
+
+?>
+--EXPECTF--
+Warning: ftp_connect() expects parameter 1 to be string, array given in %s006.php on line 4
+NULL
+
+Warning: ftp_connect(): Timeout has to be greater than 0 in %s006.php on line 5
+bool(false)
+
+Warning: ftp_raw() expects exactly 2 parameters, 1 given in %s006.php on line 6
+NULL
+
+Warning: ftp_mkdir() expects exactly 2 parameters, 1 given in %s006.php on line 7
+NULL
+
+Warning: ftp_rmdir() expects exactly 2 parameters, 1 given in %s006.php on line 8
+NULL
+
+Warning: ftp_nlist() expects exactly 2 parameters, 1 given in %s006.php on line 9
+NULL
+
+Warning: ftp_rawlist() expects at least 2 parameters, 1 given in %s006.php on line 10
+NULL
+
+Warning: ftp_fget() expects at least 4 parameters, 1 given in %s006.php on line 11
+NULL
+
+Warning: ftp_nb_fget() expects at least 4 parameters, 1 given in %s006.php on line 12
+NULL
+
+Warning: ftp_nb_get() expects at least 4 parameters, 1 given in %s006.php on line 13
+NULL
+
+Warning: ftp_pasv() expects exactly 2 parameters, 1 given in %s006.php on line 14
+NULL
+
+Warning: ftp_nb_continue() expects exactly 1 parameter, 0 given in %s006.php on line 15
+NULL
+
+Warning: ftp_fput() expects at least 4 parameters, 0 given in %s006.php on line 16
+NULL
+
+Warning: ftp_nb_fput() expects at least 4 parameters, 1 given in %s006.php on line 17
+NULL
+
+Warning: ftp_put() expects at least 4 parameters, 1 given in %s006.php on line 18
+NULL
+
+Warning: ftp_nb_put() expects at least 4 parameters, 1 given in %s006.php on line 19
+NULL
+
+Warning: ftp_size() expects exactly 2 parameters, 1 given in %s006.php on line 20
+NULL
+
+Warning: ftp_mdtm() expects exactly 2 parameters, 1 given in %s006.php on line 21
+NULL
+
+Warning: ftp_rename() expects exactly 3 parameters, 1 given in %s006.php on line 22
+NULL
+
+Warning: ftp_site() expects exactly 2 parameters, 1 given in %s006.php on line 23
+NULL
+
+Warning: ftp_set_option() expects exactly 3 parameters, 1 given in %s006.php on line 24
+NULL
+
+Warning: ftp_get_option() expects exactly 2 parameters, 1 given in %s006.php on line 25
+NULL
diff --git a/ext/ftp/tests/bug27809.phpt b/ext/ftp/tests/bug27809.phpt
new file mode 100644
index 000000000..bcbe03f13
--- /dev/null
+++ b/ext/ftp/tests/bug27809.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Bug #27809: ftp_systype returns null
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug27809=true;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'anonymous', 'IEUser@'));
+var_dump(ftp_systype($ftp));
+
+?>
+--EXPECT--
+bool(true)
+string(6) "OS/400"
diff --git a/ext/ftp/tests/bug37799.phpt b/ext/ftp/tests/bug37799.phpt
new file mode 100644
index 000000000..a47d58678
--- /dev/null
+++ b/ext/ftp/tests/bug37799.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Bug #37799: ftp_ssl_connect() falls back to non-ssl connection
+--SKIPIF--
+<?php
+$ssl = 1;
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug37799=$ssl=1;
+require 'server.inc';
+
+$ftp = ftp_ssl_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+ftp_close($ftp);
+?>
+--EXPECTF--
+Warning: ftp_login(): bogus msg in %sbug37799.php on line 8
+bool(false)
diff --git a/ext/ftp/tests/bug39458-2.phpt b/ext/ftp/tests/bug39458-2.phpt
new file mode 100644
index 000000000..4cd2a4523
--- /dev/null
+++ b/ext/ftp/tests/bug39458-2.phpt
@@ -0,0 +1,36 @@
+--TEST--
+Bug #39458: ftp_nlist() returns false on empty directories (other server behaviour)
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug39458=1;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_nlist($ftp, ''));
+var_dump(ftp_nlist($ftp, 'emptydir'));
+var_dump(ftp_nlist($ftp, 'bogusdir'));
+
+ftp_close($ftp);
+?>
+--EXPECT--
+bool(true)
+array(3) {
+ [0]=>
+ string(5) "file1"
+ [1]=>
+ string(5) "file1"
+ [2]=>
+ string(9) "file
+b0rk"
+}
+array(0) {
+}
+bool(false)
diff --git a/ext/ftp/tests/bug39458.phpt b/ext/ftp/tests/bug39458.phpt
new file mode 100644
index 000000000..5ea345776
--- /dev/null
+++ b/ext/ftp/tests/bug39458.phpt
@@ -0,0 +1,35 @@
+--TEST--
+Bug #39458: ftp_nlist() returns false on empty directories
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_nlist($ftp, ''));
+var_dump(ftp_nlist($ftp, 'emptydir'));
+var_dump(ftp_nlist($ftp, 'bogusdir'));
+
+ftp_close($ftp);
+?>
+--EXPECT--
+bool(true)
+array(3) {
+ [0]=>
+ string(5) "file1"
+ [1]=>
+ string(5) "file1"
+ [2]=>
+ string(9) "file
+b0rk"
+}
+array(0) {
+}
+bool(false)
diff --git a/ext/ftp/tests/bug39583-2.phpt b/ext/ftp/tests/bug39583-2.phpt
new file mode 100644
index 000000000..44921b320
--- /dev/null
+++ b/ext/ftp/tests/bug39583-2.phpt
@@ -0,0 +1,34 @@
+--TEST--
+Bug #39583: FTP always transfers in binary mode
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+$source_file = __FILE__;
+$destination_file = basename(__FILE__);
+
+// upload the file
+$upload = ftp_put($ftp, $destination_file, $source_file, FTP_BINARY);
+
+// check upload status
+if (!$upload) {
+ echo "FTP upload has failed!";
+ } else {
+ echo "Uploaded $source_file as $destination_file";
+ }
+
+// close the FTP stream
+ftp_close($ftp);
+?>
+--EXPECTF--
+bool(true)
+Uploaded %sbug39583-2.php as bug39583-2.php
diff --git a/ext/ftp/tests/bug39583.phpt b/ext/ftp/tests/bug39583.phpt
new file mode 100644
index 000000000..3c73758dc
--- /dev/null
+++ b/ext/ftp/tests/bug39583.phpt
@@ -0,0 +1,35 @@
+--TEST--
+Bug #39583: FTP always transfers in binary mode
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug39583=1;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+$source_file = __FILE__;
+$destination_file = basename(__FILE__);
+
+// upload the file
+$upload = ftp_put($ftp, $destination_file, $source_file, FTP_ASCII);
+
+// check upload status
+if (!$upload) {
+ echo "FTP upload has failed!";
+ } else {
+ echo "Uploaded $source_file as $destination_file";
+ }
+
+// close the FTP stream
+ftp_close($ftp);
+?>
+--EXPECTF--
+bool(true)
+Uploaded %sbug39583.php as bug39583.php
diff --git a/ext/ftp/tests/bug7216-2.phpt b/ext/ftp/tests/bug7216-2.phpt
new file mode 100644
index 000000000..62b6ec84b
--- /dev/null
+++ b/ext/ftp/tests/bug7216-2.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Bug #7216: ftp_mkdir returns nothing (2)
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'anonymous', 'IEUser@'));
+// test for the correct behavior this time
+var_dump(ftp_mkdir($ftp, 'CVS'));
+
+?>
+--EXPECT--
+bool(true)
+string(20) "/path/to/ftproot/CVS"
diff --git a/ext/ftp/tests/bug7216.phpt b/ext/ftp/tests/bug7216.phpt
new file mode 100644
index 000000000..870e02ea6
--- /dev/null
+++ b/ext/ftp/tests/bug7216.phpt
@@ -0,0 +1,21 @@
+--TEST--
+Bug #7216: ftp_mkdir returns nothing
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug7216=true;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'anonymous', 'IEUser@'));
+var_dump(ftp_mkdir($ftp, 'CVS'));
+
+?>
+--EXPECT--
+bool(true)
+string(3) "CVS"
diff --git a/ext/ftp/tests/cert.pem b/ext/ftp/tests/cert.pem
new file mode 100644
index 000000000..94c61ffcc
--- /dev/null
+++ b/ext/ftp/tests/cert.pem
@@ -0,0 +1,48 @@
+-----BEGIN CERTIFICATE REQUEST-----
+MIIBmzCCAQQCAQAwWzELMAkGA1UEBhMCUFQxCzAJBgNVBAgTAkx4MQswCQYDVQQH
+EwJMeDEcMBoGA1UEChMTQSBtaW5oYSBlbXByZXNhLCBTQTEUMBIGA1UECxMLUEhQ
+IFFBIFRlYW0wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAM9mfEOSYwXf58ch
+4NyO1QOU1XMfquz8OVpvMUITABLAevZpeQn6vZPHNyXHFQC0QC8scydK1rAYd2U+
+9K2aPub6ioMjYyjPpAE07l9EAAPUEBlqqsziB/wT8QjWkByyJEkYu+o0Wyjokhfn
+BMPvm52wLWUx9nvUeNDCftnKg1wxAgMBAAGgADANBgkqhkiG9w0BAQQFAAOBgQDD
+s1FeqPxnF2bWj8/dG8MyPaRfOAMVz1UsCZUciXIVG5LSIvR2qnMC3iEYt3s13sEq
+z8VJlNHa8nniE+VFNv093yIu+PlWXMEvb5y5EFqP2AYq3RAT+SJsSxGqIdzPZiKY
+INaktLCZmQ/E1v7/4hFzVRq9ydJI82DVS1nv282Whw==
+-----END CERTIFICATE REQUEST-----
+-----BEGIN CERTIFICATE-----
+MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJQVDEL
+MAkGA1UECBMCTHgxCzAJBgNVBAcTAkx4MRwwGgYDVQQKExNBIG1pbmhhIGVtcHJl
+c2EsIFNBMRQwEgYDVQQLEwtQSFAgUUEgVGVhbTAeFw0wNjExMTkxODIzNTNaFw0w
+NzExMTkxODIzNTNaMFsxCzAJBgNVBAYTAlBUMQswCQYDVQQIEwJMeDELMAkGA1UE
+BxMCTHgxHDAaBgNVBAoTE0EgbWluaGEgZW1wcmVzYSwgU0ExFDASBgNVBAsTC1BI
+UCBRQSBUZWFtMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPZnxDkmMF3+fH
+IeDcjtUDlNVzH6rs/DlabzFCEwASwHr2aXkJ+r2TxzclxxUAtEAvLHMnStawGHdl
+PvStmj7m+oqDI2Moz6QBNO5fRAAD1BAZaqrM4gf8E/EI1pAcsiRJGLvqNFso6JIX
+5wTD75udsC1lMfZ71HjQwn7ZyoNcMQIDAQABo4G2MIGzMB0GA1UdDgQWBBTIga5L
+q+Ub1SWXgNZRYCpq3c8Z+jCBgwYDVR0jBHwweoAUyIGuS6vlG9Ull4DWUWAqat3P
+GfqhX6RdMFsxCzAJBgNVBAYTAlBUMQswCQYDVQQIEwJMeDELMAkGA1UEBxMCTHgx
+HDAaBgNVBAoTE0EgbWluaGEgZW1wcmVzYSwgU0ExFDASBgNVBAsTC1BIUCBRQSBU
+ZWFtggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAe6AA8aC3KDI8
+smd+7XWjaTSp1Q0uMkEZ2PEBzif2I1aPPqw1CQykJ1iDdC/8PJ1yEIezloP2XQoZ
+NjTaCO+uubay03ncoPTZvDUwExN9BYFAYgc2z3tLMHYbA7kM2sIbKys7ZQegLibr
+TSKYQOBeYA/FB9GHECJGU3zBRvYi+Og=
+-----END CERTIFICATE-----
+-----BEGIN RSA PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: DES-EDE3-CBC,928762DB6DE222AD
+
+oOxNUBX0wrqmRqb3IEZMogc1bnVm6JoW6YFjGfHNcIz0jS7UPDhUFDR26y0dYujL
+LEgxOcYo8ItvGcXSRbs+3W7lISbosgkB0DOaKx5jVmOGwUVRergUUSY8rbf93FtP
+27CEvAfsU6do5HmlJ34mYZW1k+onCznlJXJkupQ5jmiily3GwEdr/5mMIVOmXQ6p
+xWkxHySDKyVbR0v4JY3SJLRBuhgofYNG5155PiqZ7KwYY4Aw60eVgINsvJCF9/8b
+kEj+lecHbBdAf7N82320Ga+F+VeFnUl0gWFjoIF9UFCO80+7ZvIGdGlyPkr4zMvt
+TsC1snJQdHg+IlT3sGayYrQANpTG6GPYhn3KEvK5aqq+bPEe5lija0gw34jbPCo+
+TjHR76lToxzubGZODyyF/rjl5KwUbqTCNuv1PX1jTx7n7sCbu+KHpqXMhTHLKtby
++Wh7WAfsVrbIW+P85/mkfhPbPZ2621f9cyStdFGgWU4dHdD00HIGOgAJvUSbC2Au
+oVUoKf2818t1s9aA4ptog04sNi+Ixu+z+3yYNLZj51j4ZX3KuXxLIiQvlvFQ8LQi
+RHGQk3u2W3iNtDKKUQjMPaB2FlVtC7FmtHBCpRmos6ld240DDyucqMdIDTMaqV0+
+sL4X+LIeBM/hP/IquRTuQBHBmgjkN4845ihTUJOanyKx605ANq/roHzXrbIxhR5p
+pcJLCBMLMWgdOCJMZRavSq04iXeNfP6Mk/joVpHS62Ljdfc94BBLfsOKOErA20Nq
+lfvbZqy2tI5IIDoq05S8FU0DYNqq/hyrv9Udo8IAo+WkBOABm0x/WA==
+-----END RSA PRIVATE KEY-----
+ \ No newline at end of file
diff --git a/ext/ftp/tests/server.inc b/ext/ftp/tests/server.inc
new file mode 100644
index 000000000..c101c7c70
--- /dev/null
+++ b/ext/ftp/tests/server.inc
@@ -0,0 +1,258 @@
+<?php
+
+$socket = null;
+$errno = 0;
+$context = stream_context_create(array('ssl' => array('local_cert' => dirname(__FILE__).'/cert.pem', 'passphrase' => 'pass')));
+
+for ($i=0; $i<10 && !$socket; ++$i) {
+ $port = rand(50000, 65535);
+ $socket = stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
+}
+
+if (!$socket) {
+ die("could not start/bind the ftp server\n");
+}
+
+$pid = pcntl_fork();
+
+if ($pid) {
+
+function dump_and_exit($buf)
+{
+ var_dump($buf);
+ fclose($GLOBALS['s']);
+ exit;
+}
+
+function anonymous()
+{
+ return $GLOBALS['user'] === 'anonymous';
+}
+
+/* quick&dirty realpath() like function */
+function change_dir($dir)
+{
+ global $cwd;
+
+ if ($dir[0] == '/') {
+ $cwd = $dir;
+ return;
+ }
+
+ $cwd = "$cwd/$dir";
+
+ do {
+ $old = $cwd;
+ $cwd = preg_replace('@/?[^/]+/\.\.@', '', $cwd);
+ } while ($old != $cwd);
+
+ $cwd = strtr($cwd, array('//' => '/'));
+ if (!$cwd) $cwd = '/';
+}
+
+
+$s = stream_socket_accept($socket);
+if (!$s) die("Error accepting a new connection\n");
+
+fputs($s, "220----- PHP FTP server 0.3 -----\r\n220 Service ready\r\n");
+$buf = fread($s, 2048);
+
+
+function user_auth($buf) {
+ global $user, $s, $ssl, $bug37799;
+
+if (!empty($ssl)) {
+ if ($buf !== "AUTH TLS\r\n") {
+ fputs($s, "500 Syntax error, command unrecognized.\r\n");
+ dump_and_exit($buf);
+ }
+
+ if (empty($bug37799)) {
+ fputs($s, "234 auth type accepted\r\n");
+ } else {
+ fputs($s, "666 dummy\r\n");
+ fputs($s, "666 bogus msg\r\n");
+ exit;
+ }
+
+ if (!stream_socket_enable_crypto($s, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER)) {
+ die("SSLv23 handshake failed.\n");
+ }
+
+ if (!preg_match('/^PBSZ \d+\r\n$/', $buf = fread($s, 2048))) {
+ fputs($s, "501 bogus data\r\n");
+ dump_and_exit($buf);
+ }
+
+ fputs($s, "200 OK\r\n");
+ $buf = fread($s, 2048);
+
+ if ($buf !== "PROT P\r\n") {
+ fputs($s, "504 Wrong protection.\r\n");
+ dump_and_exit($buf);
+ }
+
+ fputs($s, "200 OK\r\n");
+
+ $buf = fread($s, 2048);
+}
+
+if (!preg_match('/^USER (\w+)\r\n$/', $buf, $m)) {
+ fputs($s, "500 Syntax error, command unrecognized.\r\n");
+ dump_and_exit($buf);
+}
+$user = $m[1];
+if ($user !== 'user' && $user !== 'anonymous') {
+ fputs($s, "530 Not logged in.\r\n");
+ fclose($s);
+ exit;
+}
+
+if (anonymous()) {
+ fputs($s, "230 Anonymous user logged in\r\n");
+
+} else {
+ fputs($s, "331 User name ok, need password\r\n");
+
+ if (!preg_match('/^PASS (\w+)\r\n$/', $buf = fread($s, 100), $m)) {
+ fputs($s, "500 Syntax error, command unrecognized.\r\n");
+ dump_and_exit($buf);
+ }
+
+ $pass = $m[1];
+ if ($pass === 'pass') {
+ fputs($s, "230 User logged in\r\n");
+ } else {
+ fputs($s, "530 Not logged in.\r\n");
+ fclose($s);
+ exit;
+ }
+}
+}
+
+user_auth($buf);
+
+$cwd = '/';
+$num_bogus_cmds = 0;
+
+while($buf = fread($s, 4098)) {
+
+ if (!empty($bogus)) {
+ fputs($s, "502 Command not implemented (".$num_bogus_cmds++.").\r\n");
+
+ } else if ($buf === "HELP\r\n") {
+ fputs($s, "214-There is help available for the following commands:\r\n");
+ fputs($s, " USER\r\n");
+ fputs($s, " HELP\r\n");
+ fputs($s, "214 end of list\r\n");
+
+ } elseif ($buf === "HELP HELP\r\n") {
+ fputs($s, "214 Syntax: HELP [<SP> <string>] <CRLF>\r\n");
+
+ } elseif ($buf === "PWD\r\n") {
+ fputs($s, "257 \"$cwd\" is current directory.\r\n");
+
+ } elseif ($buf === "CDUP\r\n") {
+ change_dir('..');
+ fputs($s, "250 CDUP command successful.\r\n");
+
+ } elseif ($buf === "SYST\r\n") {
+ if (isset($bug27809)) {
+ fputs($s, "215 OS/400 is the remote operating system. The TCP/IP version is \"V5R2M0\"\r\n");
+ } else {
+ fputs($s, "215 UNIX Type: L8.\r\n");
+ }
+
+ } elseif ($buf === "TYPE A\r\n") {
+ $ascii = true;
+ fputs($s, "200 OK\r\n");
+
+ } elseif ($buf === "TYPE I\r\n") {
+ $ascii = false;
+ fputs($s, "200 OK\r\n");
+
+ } elseif ($buf === "QUIT\r\n") {
+ break;
+
+ } elseif (preg_match("~^PORT (\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\r\n$~", $buf, $m)) {
+ $host = "$m[1].$m[2].$m[3].$m[4]";
+ $port = ((int)$m[5] << 8) + (int)$m[6];
+ fputs($s, "200 OK.\r\n");
+
+ } elseif (preg_match("~^STOR ([\w/.-]+)\r\n$~", $buf, $m)) {
+ fputs($s, "150 File status okay; about to open data connection\r\n");
+
+ if (!$fs = stream_socket_client("tcp://$host:$port")) {
+ fputs($s, "425 Can't open data connection\r\n");
+ continue;
+ }
+
+ $data = stream_get_contents($fs);
+ $orig = file_get_contents(dirname(__FILE__).'/'.$m[1]);
+
+ if (isset($ascii) && !$ascii && $orig === $data) {
+ fputs($s, "226 Closing data Connection.\r\n");
+
+ } elseif ((!empty($ascii) || isset($bug39583)) && $data === strtr($orig, array("\r\n" => "\n", "\r" => "\n", "\n" => "\r\n"))) {
+ fputs($s, "226 Closing data Connection.\r\n");
+
+ } else {
+ var_dump($data);
+ var_dump($orig);
+ fputs($s, "552 Requested file action aborted.\r\n");
+ }
+ fclose($fs);
+
+ } elseif (preg_match("~^CWD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
+ change_dir($m[1]);
+ fputs($s, "250 CWD command successful.\r\n");
+
+ } elseif (preg_match("~^NLST(?: ([A-Za-z./]+))?\r\n$~", $buf, $m)) {
+
+ if (isset($m[1]) && $m[1] === 'bogusdir') {
+ fputs($s, "250 $m[1]: No such file or directory\r\n");
+ continue;
+ }
+
+ // there are some servers that don't open the ftp-data socket if there's nothing to send
+ if (isset($bug39458) && isset($m[1]) && $m[1] === 'emptydir') {
+ fputs($s, "226 Transfer complete.\r\n");
+ continue;
+ }
+
+ fputs($s, "150 File status okay; about to open data connection\r\n");
+
+ if (!$fs = stream_socket_client("tcp://$host:$port")) {
+ fputs($s, "425 Can't open data connection\r\n");
+ continue;
+ }
+
+ if (empty($m[1]) || $m[1] !== 'emptydir') {
+ fputs($fs, "file1\r\nfile1\r\nfile\nb0rk\r\n");
+ }
+
+ fputs($s, "226 Closing data Connection.\r\n");
+ fclose($fs);
+
+ } elseif (preg_match("~^MKD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
+ if (isset($bug7216)) {
+ fputs($s, "257 OK.\r\n");
+ } else {
+ fputs($s, "257 \"/path/to/ftproot$cwd$m[1]\" created.\r\n");
+ }
+
+ } elseif (preg_match('/^USER /', $buf)) {
+ user_auth($buf);
+
+ } else {
+ fputs($s, "500 Syntax error, command unrecognized.\r\n");
+ dump_and_exit($buf);
+ }
+}
+
+fclose($s);
+exit;
+}
+
+fclose($socket);
+?>
diff --git a/ext/ftp/tests/skipif.inc b/ext/ftp/tests/skipif.inc
new file mode 100644
index 000000000..9ab6ad985
--- /dev/null
+++ b/ext/ftp/tests/skipif.inc
@@ -0,0 +1,5 @@
+<?php
+if (!extension_loaded('ftp')) die('skip ftp extension not loaded');
+if (!function_exists('pcntl_fork')) die('skip fork not available');
+if (!empty($ssl) && !extension_loaded('openssl')) die('skip openssl extension not loaded');
+?>