Skip to content

Commit d2705c0

Browse files
committed
Make populate_post_data() return values
1 parent ab4d8fe commit d2705c0

12 files changed

+133
-34
lines changed

Zend/Optimizer/zend_func_infos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ static const func_info_t func_infos[] = {
536536
F1("htmlspecialchars", MAY_BE_STRING),
537537
F1("htmlentities", MAY_BE_STRING),
538538
F1("get_html_translation_table", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_STRING),
539+
F1("populate_post_data", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
539540
F1("bin2hex", MAY_BE_STRING),
540541
F1("hex2bin", MAY_BE_STRING|MAY_BE_FALSE),
541542
#if defined(HAVE_NL_LANGINFO)

ext/standard/basic_functions.stub.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,11 @@ function htmlentities(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE |
22632263
*/
22642264
function get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, string $encoding = "UTF-8"): array {}
22652265

2266-
function populate_post_data(): void {}
2266+
/**
2267+
* @return array<int, array>
2268+
* @refcount 1
2269+
*/
2270+
function populate_post_data(): array {}
22672271

22682272
/* }}} */
22692273

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/html.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,14 +1568,10 @@ PHP_FUNCTION(populate_post_data)
15681568
return;
15691569
}
15701570

1571-
zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_POST]);
1572-
zval_ptr_dtor_nogc(&PG(http_globals)[TRACK_VARS_FILES]);
1573-
array_init(&PG(http_globals)[TRACK_VARS_POST]);
1574-
array_init(&PG(http_globals)[TRACK_VARS_FILES]);
1571+
zval post, files;
1572+
array_init(&post);
1573+
array_init(&files);
15751574
sapi_read_post_data();
1576-
sapi_handle_post(&PG(http_globals)[TRACK_VARS_POST]);
1577-
zend_hash_update(&EG(symbol_table), zend_string_init_interned(ZEND_STRL("_POST"), 1), &PG(http_globals)[TRACK_VARS_POST]);
1578-
zend_hash_update(&EG(symbol_table), zend_string_init_interned(ZEND_STRL("_FILES"), 1), &PG(http_globals)[TRACK_VARS_FILES]);
1579-
Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
1580-
Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
1575+
sapi_handle_post_ex(&post, &files);
1576+
RETURN_ARR(zend_new_pair(&post, &files));
15811577
}

main/SAPI.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,20 @@ static void sapi_run_header_callback(zval *callback)
160160
}
161161
}
162162

163-
SAPI_API void sapi_handle_post(void *arg)
163+
SAPI_API void sapi_handle_post_ex(zval *arg, zval *files)
164164
{
165165
if (SG(request_info).post_entry && SG(request_info).content_type_dup) {
166-
SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg);
166+
SG(request_info).post_entry->post_handler(SG(request_info).content_type_dup, arg, files);
167167
efree(SG(request_info).content_type_dup);
168168
SG(request_info).content_type_dup = NULL;
169169
}
170170
}
171171

172+
SAPI_API void sapi_handle_post(zval *arg)
173+
{
174+
sapi_handle_post_ex(arg, NULL);
175+
}
176+
172177
SAPI_API void sapi_read_post_data(void)
173178
{
174179
sapi_post_entry *post_entry;

main/SAPI.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len,
185185

186186
SAPI_API int sapi_send_headers(void);
187187
SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
188-
SAPI_API void sapi_handle_post(void *arg);
188+
SAPI_API void sapi_handle_post(zval *arg);
189+
SAPI_API void sapi_handle_post_ex(zval *arg, zval *files);
189190
SAPI_API void sapi_read_post_data(void);
190191
SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
191192
SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry);
@@ -272,7 +273,7 @@ struct _sapi_post_entry {
272273
char *content_type;
273274
uint32_t content_type_len;
274275
void (*post_reader)(void);
275-
void (*post_handler)(char *content_type_dup, void *arg);
276+
void (*post_handler)(char *content_type_dup, zval *arg, zval *files);
276277
};
277278

278279
/* header_handler() constants */
@@ -288,7 +289,7 @@ struct _sapi_post_entry {
288289
#define SAPI_PHP_VERSION_HEADER "X-Powered-By: PHP/" PHP_VERSION
289290

290291
#define SAPI_POST_READER_FUNC(post_reader) void post_reader(void)
291-
#define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg)
292+
#define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, zval *arg, zval *files)
292293

293294
#define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
294295
#define SAPI_INPUT_FILTER_FUNC(input_filter) unsigned int input_filter(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len)

main/php_variables.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ typedef struct post_var_data {
323323
size_t already_scanned;
324324
} post_var_data_t;
325325

326-
static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
326+
static bool add_post_var(zval *arr, post_var_data_t *var, bool eof, bool store_in_global)
327327
{
328328
char *start, *ksep, *vsep, *val;
329329
size_t klen, vlen;
@@ -364,7 +364,8 @@ static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
364364
vlen = php_url_decode(val, vlen);
365365
}
366366

367-
if (sapi_module.input_filter(PARSE_POST, var->ptr, &val, vlen, &new_vlen)) {
367+
int filter_type = store_in_global ? PARSE_POST : PARSE_STRING;
368+
if (sapi_module.input_filter(filter_type, var->ptr, &val, vlen, &new_vlen)) {
368369
php_register_variable_safe(var->ptr, val, new_vlen, arr);
369370
}
370371
efree(val);
@@ -374,13 +375,13 @@ static bool add_post_var(zval *arr, post_var_data_t *var, bool eof)
374375
return 1;
375376
}
376377

377-
static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof)
378+
static inline int add_post_vars(zval *arr, post_var_data_t *vars, bool eof, bool store_in_global)
378379
{
379380
uint64_t max_vars = PG(max_input_vars);
380381

381382
vars->ptr = ZSTR_VAL(vars->str.s);
382383
vars->end = ZSTR_VAL(vars->str.s) + ZSTR_LEN(vars->str.s);
383-
while (add_post_var(arr, vars, eof)) {
384+
while (add_post_var(arr, vars, eof, store_in_global)) {
384385
if (++vars->cnt > max_vars) {
385386
php_error_docref(NULL, E_WARNING,
386387
"Input variables exceeded %" PRIu64 ". "
@@ -406,6 +407,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
406407
zval *arr = (zval *) arg;
407408
php_stream *s = SG(request_info).request_body;
408409
post_var_data_t post_data;
410+
bool store_in_global = files == NULL;
409411

410412
if (s && SUCCESS == php_stream_rewind(s)) {
411413
memset(&post_data, 0, sizeof(post_data));
@@ -417,7 +419,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
417419
if (len > 0) {
418420
smart_str_appendl(&post_data.str, buf, len);
419421

420-
if (SUCCESS != add_post_vars(arr, &post_data, 0)) {
422+
if (SUCCESS != add_post_vars(arr, &post_data, 0, store_in_global)) {
421423
smart_str_free(&post_data.str);
422424
return;
423425
}
@@ -429,7 +431,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
429431
}
430432

431433
if (post_data.str.s) {
432-
add_post_vars(arr, &post_data, 1);
434+
add_post_vars(arr, &post_data, 1, store_in_global);
433435
smart_str_free(&post_data.str);
434436
}
435437
}

main/rfc1867.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
657657
int skip_upload = 0, anonymous_index = 0;
658658
HashTable *uploaded_files = NULL;
659659
multipart_buffer *mbuff;
660-
zval *array_ptr = (zval *) arg;
660+
zval *array_ptr = arg;
661+
bool store_in_globals = files == NULL;
662+
zval *files_ptr = store_in_globals ? &PG(http_globals)[TRACK_VARS_FILES] : files;
661663
int fd = -1;
662664
zend_llist header;
663665
void *event_extra_data = NULL;
@@ -741,9 +743,9 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
741743
zend_hash_init(uploaded_files, 8, NULL, free_filename, 0);
742744
SG(rfc1867_uploaded_files) = uploaded_files;
743745

744-
if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) != IS_ARRAY) {
746+
if (Z_TYPE_P(files_ptr) != IS_ARRAY) {
745747
/* php_auto_globals_create_files() might have already done that */
746-
array_init(&PG(http_globals)[TRACK_VARS_FILES]);
748+
array_init(files_ptr);
747749
}
748750

749751
zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0);
@@ -849,7 +851,8 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
849851
}
850852
}
851853

852-
if (++count <= PG(max_input_vars) && sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len)) {
854+
int filter_type = store_in_globals ? PARSE_POST : PARSE_STRING;
855+
if (++count <= PG(max_input_vars) && sapi_module.input_filter(filter_type, param, &value, value_len, &new_val_len)) {
853856
if (php_rfc1867_callback != NULL) {
854857
multipart_event_formdata event_formdata;
855858
size_t newlength = new_val_len;
@@ -1131,7 +1134,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
11311134
} else {
11321135
snprintf(lbuf, llen, "%s[name]", param);
11331136
}
1134-
register_http_post_files_variable(lbuf, s, &PG(http_globals)[TRACK_VARS_FILES], 0);
1137+
register_http_post_files_variable(lbuf, s, files_ptr, 0);
11351138
s = NULL;
11361139

11371140
/* Add full path of supplied file for folder uploads via
@@ -1143,7 +1146,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
11431146
} else {
11441147
snprintf(lbuf, llen, "%s[full_path]", param);
11451148
}
1146-
register_http_post_files_variable(lbuf, filename, &PG(http_globals)[TRACK_VARS_FILES], 0);
1149+
register_http_post_files_variable(lbuf, filename, files_ptr, 0);
11471150
efree(filename);
11481151

11491152
/* Possible Content-Type: */
@@ -1163,7 +1166,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
11631166
} else {
11641167
snprintf(lbuf, llen, "%s[type]", param);
11651168
}
1166-
register_http_post_files_variable(lbuf, cd, &PG(http_globals)[TRACK_VARS_FILES], 0);
1169+
register_http_post_files_variable(lbuf, cd, files_ptr, 0);
11671170

11681171
/* Restore Content-Type Header */
11691172
if (s != NULL) {
@@ -1191,7 +1194,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
11911194
} else {
11921195
ZVAL_EMPTY_STRING(&zfilename);
11931196
}
1194-
register_http_post_files_variable_ex(lbuf, &zfilename, &PG(http_globals)[TRACK_VARS_FILES], 1);
1197+
register_http_post_files_variable_ex(lbuf, &zfilename, files_ptr, 1);
11951198
}
11961199

11971200
{
@@ -1229,7 +1232,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
12291232
} else {
12301233
snprintf(lbuf, llen, "%s[error]", param);
12311234
}
1232-
register_http_post_files_variable_ex(lbuf, &error_type, &PG(http_globals)[TRACK_VARS_FILES], 0);
1235+
register_http_post_files_variable_ex(lbuf, &error_type, files_ptr, 0);
12331236

12341237
/* Add $foo[size] */
12351238
if (is_arr_upload) {
@@ -1240,7 +1243,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
12401243
if (size_overflow) {
12411244
ZVAL_STRING(&file_size, file_size_buf);
12421245
}
1243-
register_http_post_files_variable_ex(lbuf, &file_size, &PG(http_globals)[TRACK_VARS_FILES], size_overflow);
1246+
register_http_post_files_variable_ex(lbuf, &file_size, files_ptr, size_overflow);
12441247
}
12451248
efree(param);
12461249
}

sapi/fpm/tests/put_multipart.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ EOT;
2323

2424
$code = <<<'EOT'
2525
<?php
26-
populate_post_data();
26+
[$_POST, $_FILES] = populate_post_data();
2727
$file_path = __DIR__ . '/put_multipart_uploaded_file.txt';
2828
move_uploaded_file($_FILES[0]['tmp_name'], $file_path);
2929
$file_content = file_get_contents($file_path);

sapi/fpm/tests/put_urlencoded.phpt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
PUT x-www-form-urlencoded
3+
--EXTENSIONS--
4+
zend_test
5+
--SKIPIF--
6+
<?php include "skipif.inc"; ?>
7+
--FILE--
8+
<?php
9+
10+
require_once "tester.inc";
11+
12+
$cfg = <<<EOT
13+
[global]
14+
error_log = {{FILE:LOG}}
15+
[unconfined]
16+
listen = {{ADDR}}
17+
pm = dynamic
18+
pm.max_children = 5
19+
pm.start_servers = 1
20+
pm.min_spare_servers = 1
21+
pm.max_spare_servers = 3
22+
EOT;
23+
24+
$code = <<<'EOT'
25+
<?php
26+
[$_POST, $_FILES] = populate_post_data();
27+
echo json_encode([
28+
'post' => $_POST,
29+
'files' => $_FILES,
30+
], JSON_PRETTY_PRINT);
31+
EOT;
32+
33+
$tester = new FPM\Tester($cfg, $code);
34+
$tester->start();
35+
$tester->expectLogStartNotices();
36+
echo $tester
37+
->request(
38+
method: 'PUT',
39+
headers: ['CONTENT_TYPE' => 'application/x-www-form-urlencoded'],
40+
stdin: 'foo=foo&bar[]=1&bar[]=2'
41+
)
42+
->getBody();
43+
$tester->terminate();
44+
$tester->expectLogTerminatingNotices();
45+
$tester->close();
46+
47+
?>
48+
--EXPECTF--
49+
{
50+
"post": {
51+
"foo": "foo",
52+
"bar": [
53+
"1",
54+
"2"
55+
]
56+
},
57+
"files": []
58+
}
59+
--CLEAN--
60+
<?php
61+
require_once "tester.inc";
62+
FPM\Tester::clean();
63+
$file_path = __DIR__ . '/put_multipart_uploaded_file.txt';
64+
@unlink($file_path);
65+
?>

test.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<form enctype="multipart/form-data" method="post">
5+
<label>
6+
Name:
7+
<input type="text" name="name">
8+
</label>
9+
<label>
10+
File:
11+
<input type="file" name="file">
12+
</label>
13+
<button type="submit">Submit</button>
14+
</form>
15+
16+
<pre><?php var_dump($_POST, $_FILES); ?></pre>
17+
</body>
18+
</html>

test_put.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
[$_POST, $_FILES] = populate_post_data();
4+
var_dump($_POST, $_FILES);

0 commit comments

Comments
 (0)