diff --git a/NEWS b/NEWS index a1b658ea9d27..21712fab1a1e 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,9 @@ PHP NEWS . Use recommended TYPE, HELP order for OpenMetrics metadata. (Marcel Hernandez) +- FTP: + . Added optional fact name normalization to ftp_mlsd(). + - GD: . Fixed imagecolormatch() and imagejpeg() ValueError messages. (lacatoire) diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index c6fd158ddaed..a769050a4953 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -644,7 +644,7 @@ char** ftp_mlsd(ftpbuf_t *ftp, const char *path, const size_t path_len) return ftp_genlist(ftp, "MLSD", sizeof("MLSD")-1, path, path_len); } -zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input) +zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input, bool normalize_fact_names) { zval zstr; const char *end = input + strlen(input); @@ -677,8 +677,21 @@ zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input) return FAILURE; } - ZVAL_STRINGL(&zstr, eq + 1, semi - eq - 1); - zend_hash_str_update(ht, input, eq - input, &zstr); + if (normalize_fact_names) { + zend_string *key = zend_string_init(input, eq - input, false); + zend_str_tolower(ZSTR_VAL(key), ZSTR_LEN(key)); + + /* The pathname is stored under "name", not supplied as a fact. */ + if (!zend_string_equals_literal(key, "name")) { + ZVAL_STRINGL(&zstr, eq + 1, semi - eq - 1); + /* As with repeated exact keys, the last fact wins. */ + zend_hash_update(ht, key, &zstr); + } + zend_string_release(key); + } else { + ZVAL_STRINGL(&zstr, eq + 1, semi - eq - 1); + zend_hash_str_update(ht, input, eq - input, &zstr); + } input = semi + 1; } diff --git a/ext/ftp/ftp.h b/ext/ftp/ftp.h index 1c8d45460ca6..cf6dd65ac387 100644 --- a/ext/ftp/ftp.h +++ b/ext/ftp/ftp.h @@ -161,7 +161,7 @@ char** ftp_list(ftpbuf_t *ftp, const char *path, const size_t path_len, int recu /* populates a hashtable with the facts contained in one line of * an MLSD response. */ -zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input); +zend_result ftp_mlsd_parse_line(HashTable *ht, const char *input, bool normalize_fact_names); /* returns a NULL-terminated array of lines returned by the ftp * MLSD command for the given path or NULL on error. the return diff --git a/ext/ftp/ftp.stub.php b/ext/ftp/ftp.stub.php index 6560dd893018..256d4c8e62b5 100644 --- a/ext/ftp/ftp.stub.php +++ b/ext/ftp/ftp.stub.php @@ -99,7 +99,7 @@ function ftp_rawlist(FTP\Connection $ftp, string $directory, bool $recursive = f * @return array|false * @refcount 1 */ - function ftp_mlsd(FTP\Connection $ftp, string $directory): array|false {} + function ftp_mlsd(FTP\Connection $ftp, string $directory, bool $normalize_fact_names = false): array|false {} function ftp_systype(FTP\Connection $ftp): string|false {} diff --git a/ext/ftp/ftp_arginfo.h b/ext/ftp/ftp_arginfo.h index 420fc3553ea6..3778b129f1ac 100644 --- a/ext/ftp/ftp_arginfo.h +++ b/ext/ftp/ftp_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit ftp.stub.php instead. - * Stub hash: 29606d7114a0698b8ae231173a624b17c196ffec */ + * Stub hash: ac7e05423e8c2ee530d8b2b56dc5d36621452806 */ #include "zend_attributes.h" #include "zend_constants.h" @@ -77,7 +77,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_rawlist, 0, 2, MAY_BE_ARRAY| ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, recursive, _IS_BOOL, 0, "false") ZEND_END_ARG_INFO() -#define arginfo_ftp_mlsd arginfo_ftp_nlist +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_ftp_mlsd, 0, 2, MAY_BE_ARRAY|MAY_BE_FALSE) + ZEND_ARG_OBJ_INFO(0, ftp, FTP\\Connection, 0) + ZEND_ARG_TYPE_INFO(0, directory, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, normalize_fact_names, _IS_BOOL, 0, "false") +ZEND_END_ARG_INFO() #define arginfo_ftp_systype arginfo_ftp_pwd diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index 3f22485c39a9..ec8f459db4c6 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -518,8 +518,9 @@ PHP_FUNCTION(ftp_mlsd) char **llist, **ptr, *dir; size_t dir_len; zval entry; + bool normalize_fact_names = false; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &z_ftp, php_ftp_ce, &dir, &dir_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|b", &z_ftp, php_ftp_ce, &dir, &dir_len, &normalize_fact_names) == FAILURE) { RETURN_THROWS(); } GET_FTPBUF(ftp, z_ftp); @@ -532,7 +533,7 @@ PHP_FUNCTION(ftp_mlsd) array_init(return_value); for (ptr = llist; *ptr; ptr++) { array_init(&entry); - if (ftp_mlsd_parse_line(Z_ARRVAL_P(&entry), *ptr) == SUCCESS) { + if (ftp_mlsd_parse_line(Z_ARRVAL_P(&entry), *ptr, normalize_fact_names) == SUCCESS) { zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &entry); } else { zval_ptr_dtor(&entry); diff --git a/ext/ftp/tests/ftp_mlsd_normalize_fact_names.phpt b/ext/ftp/tests/ftp_mlsd_normalize_fact_names.phpt new file mode 100644 index 000000000000..e5ec956d30cb --- /dev/null +++ b/ext/ftp/tests/ftp_mlsd_normalize_fact_names.phpt @@ -0,0 +1,89 @@ +--TEST-- +ftp_mlsd() optionally normalizes fact names without changing filenames or values +--EXTENSIONS-- +ftp +pcntl +--FILE-- +getParameters()[2]->getName()); +var_dump(array_keys($original[0])); +var_dump($normalized[0]); +var_dump($normalized[1]); +var_dump($original[2]['name'], $original[2]['Name'], $normalized[2]['name']); +var_dump($original[3]['name'], $normalized[3]['name']); +var_dump($original[4]['Type'], $original[4]['type'], $normalized[4]['type']); +var_dump($normalized[5]['name'], bin2hex(array_keys($normalized[5])[1]), $normalized[5]["x.\xC4foo"]); +var_dump(ftp_mlsd($ftp, 'emptydir', true)); +var_dump(ftp_mlsd($ftp, 'no_exists/', true)); +$warnings = []; +set_error_handler(static function (int $severity, string $message) use (&$warnings): bool { + $warnings[] = $message; + return true; +}); +$malformed = ftp_mlsd($ftp, '.', true); +restore_error_handler(); +var_dump(count($malformed), count($warnings)); + +ftp_close($ftp); +?> +--EXPECT-- +bool(true) +string(20) "normalize_fact_names" +array(5) { + [0]=> + string(4) "name" + [1]=> + string(4) "Type" + [2]=> + string(4) "SIZE" + [3]=> + string(6) "Modify" + [4]=> + string(9) "UNIX.mode" +} +array(5) { + ["name"]=> + string(19) "mixed Case file.txt" + ["type"]=> + string(4) "FiLe" + ["size"]=> + string(5) "51574" + ["modify"]=> + string(14) "20240605091325" + ["unix.mode"]=> + string(4) "0644" +} +array(3) { + ["name"]=> + string(9) "lower.txt" + ["type"]=> + string(3) "DIR" + ["size"]=> + string(1) "5" +} +string(10) "actual.txt" +string(5) "wrong" +string(10) "actual.txt" +string(5) "wrong" +string(11) "actual2.txt" +string(5) "THIRD" +string(6) "SeCoNd" +string(5) "THIRD" +string(12) "nonascii.txt" +string(12) "782ec4666f6f" +string(5) "VaLuE" +array(0) { +} +bool(false) +int(4) +int(3) diff --git a/ext/ftp/tests/server.inc b/ext/ftp/tests/server.inc index 4a4ee958fc64..a1736123b91d 100644 --- a/ext/ftp/tests/server.inc +++ b/ext/ftp/tests/server.inc @@ -488,6 +488,15 @@ if ($pid) { fputs($s, "250 Rename successful\r\n"); }elseif (preg_match('/^MLSD no_exists\//', $buf, $matches)) { fputs($s, "425 Error establishing connection\r\n"); + }elseif ($buf === "MLSD normalize\r\n") { + with_data_connection(function ($fs) { + fputs($fs, "Type=FiLe;SIZE=51574;Modify=20240605091325;UNIX.mode=0644; mixed Case file.txt\r\n"); + fputs($fs, "type=DIR;size=5; lower.txt\r\n"); + fputs($fs, "Name=wrong;Type=file; actual.txt\r\n"); + fputs($fs, "name=wrong;Type=file; actual2.txt\r\n"); + fputs($fs, "Type=FIRST;type=SeCoNd;Type=THIRD; collision.txt\r\n"); + fputs($fs, "X.\xC4FOO=VaLuE; nonascii.txt\r\n"); + }); }elseif (preg_match("~^MLSD(?: ([A-Za-z./]+))?\r\n$~", $buf, $m)) { if(isset($m[1]) && (($m[1] === 'bogusdir') || ($m[1] === '/bogusdir'))) {