Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 16 additions & 3 deletions ext/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/ftp/ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/ftp/ftp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function ftp_rawlist(FTP\Connection $ftp, string $directory, bool $recursive = f
* @return array<int, 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 {}

Expand Down
8 changes: 6 additions & 2 deletions ext/ftp/ftp_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
89 changes: 89 additions & 0 deletions ext/ftp/tests/ftp_mlsd_normalize_fact_names.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
--TEST--
ftp_mlsd() optionally normalizes fact names without changing filenames or values
--EXTENSIONS--
ftp
pcntl
--FILE--
<?php
require 'server.inc';

$ftp = ftp_connect('127.0.0.1', $port);
ftp_login($ftp, 'user', 'pass');

$original = ftp_mlsd($ftp, 'normalize');
$explicit = ftp_mlsd($ftp, 'normalize', false);
$normalized = ftp_mlsd($ftp, 'normalize', true);

var_dump($original === $explicit);
var_dump((new ReflectionFunction('ftp_mlsd'))->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)
9 changes: 9 additions & 0 deletions ext/ftp/tests/server.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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'))) {
Expand Down
Loading