/src/php-src/main/rfc1867.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Rasmus Lerdorf <rasmus@php.net> | |
12 | | | Jani Taskinen <jani@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | /* |
17 | | * This product includes software developed by the Apache Group |
18 | | * for use in the Apache HTTP server project (http://www.apache.org/). |
19 | | * |
20 | | */ |
21 | | |
22 | | #include <stdio.h> |
23 | | #include "php.h" |
24 | | #include "php_open_temporary_file.h" |
25 | | #include "zend_globals.h" |
26 | | #include "php_globals.h" |
27 | | #include "php_variables.h" |
28 | | #include "rfc1867.h" |
29 | | #include "zend_smart_string.h" |
30 | | #include "zend_exceptions.h" |
31 | | |
32 | | #ifndef DEBUG_FILE_UPLOAD |
33 | | # define DEBUG_FILE_UPLOAD 0 |
34 | | #endif |
35 | | |
36 | | static int dummy_encoding_translation(void) |
37 | 0 | { |
38 | 0 | return 0; |
39 | 0 | } |
40 | | |
41 | | static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop); |
42 | | static char *php_ap_getword_conf(const zend_encoding *encoding, char *str); |
43 | | |
44 | | static php_rfc1867_encoding_translation_t php_rfc1867_encoding_translation = dummy_encoding_translation; |
45 | | static php_rfc1867_get_detect_order_t php_rfc1867_get_detect_order = NULL; |
46 | | static php_rfc1867_set_input_encoding_t php_rfc1867_set_input_encoding = NULL; |
47 | | static php_rfc1867_getword_t php_rfc1867_getword = php_ap_getword; |
48 | | static php_rfc1867_getword_conf_t php_rfc1867_getword_conf = php_ap_getword_conf; |
49 | | static php_rfc1867_basename_t php_rfc1867_basename = NULL; |
50 | | |
51 | | PHPAPI zend_result (*php_rfc1867_callback)(unsigned int event, void *event_data, void **extra) = NULL; |
52 | | |
53 | | static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, bool override_protection); |
54 | | |
55 | | /* The longest property name we use in an uploaded file array */ |
56 | 0 | #define MAX_SIZE_OF_INDEX sizeof("[full_path]") |
57 | | |
58 | | /* The longest anonymous name */ |
59 | 0 | #define MAX_SIZE_ANONNAME 33 |
60 | | |
61 | | static void normalize_protected_variable(char *varname) /* {{{ */ |
62 | 0 | { |
63 | 0 | char *s = varname, *index = NULL, *indexend = NULL, *p; |
64 | | |
65 | | /* skip leading space */ |
66 | 0 | while (*s == ' ') { |
67 | 0 | s++; |
68 | 0 | } |
69 | | |
70 | | /* and remove it */ |
71 | 0 | if (s != varname) { |
72 | 0 | memmove(varname, s, strlen(s)+1); |
73 | 0 | } |
74 | |
|
75 | 0 | for (p = varname; *p && *p != '['; p++) { |
76 | 0 | switch(*p) { |
77 | 0 | case ' ': |
78 | 0 | case '.': |
79 | 0 | *p = '_'; |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | | /* find index */ |
85 | 0 | index = strchr(varname, '['); |
86 | 0 | if (index) { |
87 | 0 | index++; |
88 | 0 | s = index; |
89 | 0 | } else { |
90 | 0 | return; |
91 | 0 | } |
92 | | |
93 | | /* done? */ |
94 | 0 | while (index) { |
95 | 0 | while (*index == ' ' || *index == '\r' || *index == '\n' || *index=='\t') { |
96 | 0 | index++; |
97 | 0 | } |
98 | 0 | indexend = strchr(index, ']'); |
99 | 0 | indexend = indexend ? indexend + 1 : index + strlen(index); |
100 | |
|
101 | 0 | if (s != index) { |
102 | 0 | memmove(s, index, strlen(index)+1); |
103 | 0 | s += indexend-index; |
104 | 0 | } else { |
105 | 0 | s = indexend; |
106 | 0 | } |
107 | |
|
108 | 0 | if (*s == '[') { |
109 | 0 | s++; |
110 | 0 | index = s; |
111 | 0 | } else { |
112 | 0 | index = NULL; |
113 | 0 | } |
114 | 0 | } |
115 | 0 | *s = '\0'; |
116 | 0 | } |
117 | | /* }}} */ |
118 | | |
119 | | static void add_protected_variable(char *varname) /* {{{ */ |
120 | 0 | { |
121 | 0 | normalize_protected_variable(varname); |
122 | 0 | zend_hash_str_add_empty_element(&PG(rfc1867_protected_variables), varname, strlen(varname)); |
123 | 0 | } |
124 | | /* }}} */ |
125 | | |
126 | | static bool is_protected_variable(char *varname) /* {{{ */ |
127 | 0 | { |
128 | 0 | normalize_protected_variable(varname); |
129 | 0 | return zend_hash_str_exists(&PG(rfc1867_protected_variables), varname, strlen(varname)); |
130 | 0 | } |
131 | | /* }}} */ |
132 | | |
133 | | static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, bool override_protection) /* {{{ */ |
134 | 0 | { |
135 | 0 | if (override_protection || !is_protected_variable(var)) { |
136 | 0 | php_register_variable_safe(var, strval, val_len, track_vars_array); |
137 | 0 | } |
138 | 0 | } |
139 | | /* }}} */ |
140 | | |
141 | | static void safe_php_register_variable_ex(char *var, zval *val, zval *track_vars_array, bool override_protection) /* {{{ */ |
142 | 0 | { |
143 | 0 | if (override_protection || !is_protected_variable(var)) { |
144 | 0 | php_register_variable_ex(var, val, track_vars_array); |
145 | 0 | } |
146 | 0 | } |
147 | | /* }}} */ |
148 | | |
149 | | static void register_http_post_files_variable(char *strvar, char *val, zval *http_post_files, bool override_protection) /* {{{ */ |
150 | 0 | { |
151 | 0 | safe_php_register_variable(strvar, val, strlen(val), http_post_files, override_protection); |
152 | 0 | } |
153 | | /* }}} */ |
154 | | |
155 | | static void register_http_post_files_variable_ex(char *var, zval *val, zval *http_post_files, bool override_protection) /* {{{ */ |
156 | 0 | { |
157 | 0 | safe_php_register_variable_ex(var, val, http_post_files, override_protection); |
158 | 0 | } |
159 | | /* }}} */ |
160 | | |
161 | 0 | static void free_filename(zval *el) { |
162 | 0 | zend_string *filename = Z_STR_P(el); |
163 | 0 | zend_string_release_ex(filename, 0); |
164 | 0 | } |
165 | | |
166 | | PHPAPI void destroy_uploaded_files_hash(void) /* {{{ */ |
167 | 0 | { |
168 | 0 | zval *el; |
169 | |
|
170 | 0 | ZEND_HASH_MAP_FOREACH_VAL(SG(rfc1867_uploaded_files), el) { |
171 | 0 | zend_string *filename = Z_STR_P(el); |
172 | 0 | VCWD_UNLINK(ZSTR_VAL(filename)); |
173 | 0 | } ZEND_HASH_FOREACH_END(); |
174 | 0 | zend_hash_destroy(SG(rfc1867_uploaded_files)); |
175 | 0 | FREE_HASHTABLE(SG(rfc1867_uploaded_files)); |
176 | 0 | SG(rfc1867_uploaded_files) = NULL; |
177 | 0 | } |
178 | | /* }}} */ |
179 | | |
180 | | /* {{{ Following code is based on apache_multipart_buffer.c from libapreq-0.33 package. */ |
181 | | |
182 | 0 | #define FILLUNIT (1024 * 5) |
183 | | |
184 | | typedef struct { |
185 | | |
186 | | /* read buffer */ |
187 | | char *buffer; |
188 | | char *buf_begin; |
189 | | int bufsize; |
190 | | int bytes_in_buffer; |
191 | | |
192 | | /* boundary info */ |
193 | | char *boundary; |
194 | | char *boundary_next; |
195 | | int boundary_next_len; |
196 | | |
197 | | const zend_encoding *input_encoding; |
198 | | const zend_encoding **detect_order; |
199 | | size_t detect_order_size; |
200 | | } multipart_buffer; |
201 | | |
202 | | typedef struct { |
203 | | char *key; |
204 | | char *value; |
205 | | } mime_header_entry; |
206 | | |
207 | | /* |
208 | | * Fill up the buffer with client data. |
209 | | * Returns number of bytes added to buffer. |
210 | | */ |
211 | | static int fill_buffer(multipart_buffer *self) |
212 | 0 | { |
213 | 0 | int bytes_to_read, total_read = 0, actual_read = 0; |
214 | | |
215 | | /* shift the existing data if necessary */ |
216 | 0 | if (self->bytes_in_buffer > 0 && self->buf_begin != self->buffer) { |
217 | 0 | memmove(self->buffer, self->buf_begin, self->bytes_in_buffer); |
218 | 0 | } |
219 | |
|
220 | 0 | self->buf_begin = self->buffer; |
221 | | |
222 | | /* calculate the free space in the buffer */ |
223 | 0 | bytes_to_read = self->bufsize - self->bytes_in_buffer; |
224 | | |
225 | | /* read the required number of bytes */ |
226 | 0 | while (bytes_to_read > 0) { |
227 | |
|
228 | 0 | char *buf = self->buffer + self->bytes_in_buffer; |
229 | |
|
230 | 0 | actual_read = (int)sapi_module.read_post(buf, bytes_to_read); |
231 | | |
232 | | /* update the buffer length */ |
233 | 0 | if (actual_read > 0) { |
234 | 0 | self->bytes_in_buffer += actual_read; |
235 | 0 | SG(read_post_bytes) += actual_read; |
236 | 0 | total_read += actual_read; |
237 | 0 | bytes_to_read -= actual_read; |
238 | 0 | } else { |
239 | 0 | break; |
240 | 0 | } |
241 | 0 | } |
242 | |
|
243 | 0 | return total_read; |
244 | 0 | } |
245 | | |
246 | | /* eof if we are out of bytes, or if we hit the final boundary */ |
247 | | static int multipart_buffer_eof(multipart_buffer *self) |
248 | 0 | { |
249 | 0 | return self->bytes_in_buffer == 0 && fill_buffer(self) < 1; |
250 | 0 | } |
251 | | |
252 | | /* create new multipart_buffer structure */ |
253 | | static multipart_buffer *multipart_buffer_new(char *boundary, int boundary_len) |
254 | 0 | { |
255 | 0 | multipart_buffer *self = (multipart_buffer *) ecalloc(1, sizeof(multipart_buffer)); |
256 | |
|
257 | 0 | int minsize = boundary_len + 6; |
258 | 0 | if (minsize < FILLUNIT) minsize = FILLUNIT; |
259 | |
|
260 | 0 | self->buffer = (char *) ecalloc(1, minsize + 1); |
261 | 0 | self->bufsize = minsize; |
262 | |
|
263 | 0 | spprintf(&self->boundary, 0, "--%s", boundary); |
264 | |
|
265 | 0 | self->boundary_next_len = (int)spprintf(&self->boundary_next, 0, "\n--%s", boundary); |
266 | |
|
267 | 0 | self->buf_begin = self->buffer; |
268 | 0 | self->bytes_in_buffer = 0; |
269 | |
|
270 | 0 | if (php_rfc1867_encoding_translation()) { |
271 | 0 | php_rfc1867_get_detect_order(&self->detect_order, &self->detect_order_size); |
272 | 0 | } else { |
273 | 0 | self->detect_order = NULL; |
274 | 0 | self->detect_order_size = 0; |
275 | 0 | } |
276 | |
|
277 | 0 | self->input_encoding = NULL; |
278 | |
|
279 | 0 | return self; |
280 | 0 | } |
281 | | |
282 | | /* |
283 | | * Gets the next CRLF terminated line from the input buffer. |
284 | | * If it doesn't find a CRLF, and the buffer isn't completely full, returns |
285 | | * NULL; otherwise, returns the beginning of the null-terminated line, |
286 | | * minus the CRLF. |
287 | | * |
288 | | * Note that we really just look for LF terminated lines. This works |
289 | | * around a bug in internet explorer for the macintosh which sends mime |
290 | | * boundaries that are only LF terminated when you use an image submit |
291 | | * button in a multipart/form-data form. |
292 | | */ |
293 | | static char *next_line(multipart_buffer *self) |
294 | 0 | { |
295 | | /* look for LF in the data */ |
296 | 0 | char* line = self->buf_begin; |
297 | 0 | char* ptr = memchr(self->buf_begin, '\n', self->bytes_in_buffer); |
298 | |
|
299 | 0 | if (ptr) { /* LF found */ |
300 | | |
301 | | /* terminate the string, remove CRLF */ |
302 | 0 | if ((ptr - line) > 0 && *(ptr-1) == '\r') { |
303 | 0 | *(ptr-1) = 0; |
304 | 0 | } else { |
305 | 0 | *ptr = 0; |
306 | 0 | } |
307 | | |
308 | | /* bump the pointer */ |
309 | 0 | self->buf_begin = ptr + 1; |
310 | 0 | self->bytes_in_buffer -= (self->buf_begin - line); |
311 | |
|
312 | 0 | } else { /* no LF found */ |
313 | | |
314 | | /* buffer isn't completely full, fail */ |
315 | 0 | if (self->bytes_in_buffer < self->bufsize) { |
316 | 0 | return NULL; |
317 | 0 | } |
318 | | /* return entire buffer as a partial line */ |
319 | 0 | line[self->bufsize] = 0; |
320 | 0 | self->bytes_in_buffer = 0; |
321 | | /* Let fill_buffer() handle the reset of self->buf_begin */ |
322 | 0 | } |
323 | | |
324 | 0 | return line; |
325 | 0 | } |
326 | | |
327 | | /* Returns the next CRLF terminated line from the client */ |
328 | | static char *get_line(multipart_buffer *self) |
329 | 0 | { |
330 | 0 | char* ptr = next_line(self); |
331 | |
|
332 | 0 | if (!ptr) { |
333 | 0 | fill_buffer(self); |
334 | 0 | ptr = next_line(self); |
335 | 0 | } |
336 | |
|
337 | 0 | return ptr; |
338 | 0 | } |
339 | | |
340 | | /* Free header entry */ |
341 | | static void php_free_hdr_entry(mime_header_entry *h) |
342 | 0 | { |
343 | 0 | if (h->key) { |
344 | 0 | efree(h->key); |
345 | 0 | } |
346 | 0 | if (h->value) { |
347 | 0 | efree(h->value); |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | | /* finds a boundary */ |
352 | | static int find_boundary(multipart_buffer *self, char *boundary) |
353 | 0 | { |
354 | 0 | char *line; |
355 | | |
356 | | /* loop through lines */ |
357 | 0 | while( (line = get_line(self)) ) |
358 | 0 | { |
359 | | /* finished if we found the boundary */ |
360 | 0 | if (!strcmp(line, boundary)) { |
361 | 0 | return 1; |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | /* didn't find the boundary */ |
366 | 0 | return 0; |
367 | 0 | } |
368 | | |
369 | | /* parse headers */ |
370 | | static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header) |
371 | 0 | { |
372 | 0 | char *line; |
373 | 0 | mime_header_entry entry = {0}; |
374 | 0 | smart_string buf_value = {0}; |
375 | 0 | char *key = NULL; |
376 | | |
377 | | /* didn't find boundary, abort */ |
378 | 0 | if (!find_boundary(self, self->boundary)) { |
379 | 0 | return 0; |
380 | 0 | } |
381 | | |
382 | | /* get lines of text, or CRLF_CRLF */ |
383 | | |
384 | 0 | while ((line = get_line(self)) && line[0] != '\0') { |
385 | | /* add header to table */ |
386 | 0 | char *value = NULL; |
387 | |
|
388 | 0 | if (php_rfc1867_encoding_translation()) { |
389 | 0 | self->input_encoding = zend_multibyte_encoding_detector((const unsigned char *) line, strlen(line), self->detect_order, self->detect_order_size); |
390 | 0 | } |
391 | | |
392 | | /* space in the beginning means same header */ |
393 | 0 | if (!isspace((unsigned char)line[0])) { |
394 | 0 | value = strchr(line, ':'); |
395 | 0 | } |
396 | |
|
397 | 0 | if (value) { |
398 | 0 | if (buf_value.c && key) { |
399 | | /* new entry, add the old one to the list */ |
400 | 0 | smart_string_0(&buf_value); |
401 | 0 | entry.key = key; |
402 | 0 | entry.value = buf_value.c; |
403 | 0 | zend_llist_add_element(header, &entry); |
404 | 0 | buf_value.c = NULL; |
405 | 0 | key = NULL; |
406 | 0 | } |
407 | |
|
408 | 0 | *value = '\0'; |
409 | 0 | do { value++; } while (isspace((unsigned char)*value)); |
410 | |
|
411 | 0 | key = estrdup(line); |
412 | 0 | smart_string_appends(&buf_value, value); |
413 | 0 | } else if (buf_value.c) { /* If no ':' on the line, add to previous line */ |
414 | 0 | smart_string_appends(&buf_value, line); |
415 | 0 | } else { |
416 | 0 | continue; |
417 | 0 | } |
418 | 0 | } |
419 | |
|
420 | 0 | if (buf_value.c && key) { |
421 | | /* add the last one to the list */ |
422 | 0 | smart_string_0(&buf_value); |
423 | 0 | entry.key = key; |
424 | 0 | entry.value = buf_value.c; |
425 | 0 | zend_llist_add_element(header, &entry); |
426 | 0 | } |
427 | |
|
428 | 0 | return 1; |
429 | 0 | } |
430 | | |
431 | | static char *php_mime_get_hdr_value(zend_llist header, char *key) |
432 | 0 | { |
433 | 0 | mime_header_entry *entry; |
434 | |
|
435 | 0 | if (key == NULL) { |
436 | 0 | return NULL; |
437 | 0 | } |
438 | | |
439 | 0 | entry = zend_llist_get_first(&header); |
440 | 0 | while (entry) { |
441 | 0 | if (!strcasecmp(entry->key, key)) { |
442 | 0 | return entry->value; |
443 | 0 | } |
444 | 0 | entry = zend_llist_get_next(&header); |
445 | 0 | } |
446 | | |
447 | 0 | return NULL; |
448 | 0 | } |
449 | | |
450 | | static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop) |
451 | 0 | { |
452 | 0 | char *pos = *line, quote; |
453 | 0 | char *res; |
454 | |
|
455 | 0 | while (*pos && *pos != stop) { |
456 | 0 | if ((quote = *pos) == '"' || quote == '\'') { |
457 | 0 | ++pos; |
458 | 0 | while (*pos && *pos != quote) { |
459 | 0 | if (*pos == '\\' && pos[1] && pos[1] == quote) { |
460 | 0 | pos += 2; |
461 | 0 | } else { |
462 | 0 | ++pos; |
463 | 0 | } |
464 | 0 | } |
465 | 0 | if (*pos) { |
466 | 0 | ++pos; |
467 | 0 | } |
468 | 0 | } else ++pos; |
469 | 0 | } |
470 | 0 | if (*pos == '\0') { |
471 | 0 | res = estrdup(*line); |
472 | 0 | *line += strlen(*line); |
473 | 0 | return res; |
474 | 0 | } |
475 | | |
476 | 0 | res = estrndup(*line, pos - *line); |
477 | |
|
478 | 0 | while (*pos == stop) { |
479 | 0 | ++pos; |
480 | 0 | } |
481 | |
|
482 | 0 | *line = pos; |
483 | 0 | return res; |
484 | 0 | } |
485 | | |
486 | | static char *substring_conf(char *start, int len, char quote) |
487 | 0 | { |
488 | 0 | char *result = emalloc(len + 1); |
489 | 0 | char *resp = result; |
490 | 0 | int i; |
491 | |
|
492 | 0 | for (i = 0; i < len && start[i] != quote; ++i) { |
493 | 0 | if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) { |
494 | 0 | *resp++ = start[++i]; |
495 | 0 | } else { |
496 | 0 | *resp++ = start[i]; |
497 | 0 | } |
498 | 0 | } |
499 | |
|
500 | 0 | *resp = '\0'; |
501 | 0 | return result; |
502 | 0 | } |
503 | | |
504 | | static char *php_ap_getword_conf(const zend_encoding *encoding, char *str) |
505 | 0 | { |
506 | 0 | while (*str && isspace((unsigned char)*str)) { |
507 | 0 | ++str; |
508 | 0 | } |
509 | |
|
510 | 0 | if (!*str) { |
511 | 0 | return estrdup(""); |
512 | 0 | } |
513 | | |
514 | 0 | if (*str == '"' || *str == '\'') { |
515 | 0 | char quote = *str; |
516 | |
|
517 | 0 | str++; |
518 | 0 | return substring_conf(str, (int)strlen(str), quote); |
519 | 0 | } else { |
520 | 0 | char *strend = str; |
521 | |
|
522 | 0 | while (*strend && !isspace((unsigned char)*strend)) { |
523 | 0 | ++strend; |
524 | 0 | } |
525 | 0 | return substring_conf(str, strend - str, 0); |
526 | 0 | } |
527 | 0 | } |
528 | | |
529 | | static char *php_ap_basename(const zend_encoding *encoding, char *path) |
530 | 0 | { |
531 | 0 | char *s = strrchr(path, '\\'); |
532 | 0 | char *s2 = strrchr(path, '/'); |
533 | |
|
534 | 0 | if (s && s2) { |
535 | 0 | if (s > s2) { |
536 | 0 | ++s; |
537 | 0 | } else { |
538 | 0 | s = ++s2; |
539 | 0 | } |
540 | 0 | return s; |
541 | 0 | } else if (s) { |
542 | 0 | return ++s; |
543 | 0 | } else if (s2) { |
544 | 0 | return ++s2; |
545 | 0 | } |
546 | 0 | return path; |
547 | 0 | } |
548 | | |
549 | | /* |
550 | | * Search for a string in a fixed-length byte string. |
551 | | * If partial is true, partial matches are allowed at the end of the buffer. |
552 | | * Returns NULL if not found, or a pointer to the start of the first match. |
553 | | */ |
554 | | static void *php_ap_memstr(char *haystack, int haystacklen, char *needle, int needlen, int partial) |
555 | 0 | { |
556 | 0 | int len = haystacklen; |
557 | 0 | char *ptr = haystack; |
558 | | |
559 | | /* iterate through first character matches */ |
560 | 0 | while( (ptr = memchr(ptr, needle[0], len)) ) { |
561 | | |
562 | | /* calculate length after match */ |
563 | 0 | len = haystacklen - (ptr - (char *)haystack); |
564 | | |
565 | | /* done if matches up to capacity of buffer */ |
566 | 0 | if (memcmp(needle, ptr, needlen < len ? needlen : len) == 0 && (partial || len >= needlen)) { |
567 | 0 | break; |
568 | 0 | } |
569 | | |
570 | | /* next character */ |
571 | 0 | ptr++; len--; |
572 | 0 | } |
573 | |
|
574 | 0 | return ptr; |
575 | 0 | } |
576 | | |
577 | | /* read until a boundary condition */ |
578 | | static size_t multipart_buffer_read(multipart_buffer *self, char *buf, size_t bytes, int *end) |
579 | 0 | { |
580 | 0 | size_t len, max; |
581 | 0 | char *bound; |
582 | | |
583 | | /* fill buffer if needed */ |
584 | 0 | if (bytes > (size_t)self->bytes_in_buffer) { |
585 | 0 | fill_buffer(self); |
586 | 0 | } |
587 | | |
588 | | /* look for a potential boundary match, only read data up to that point */ |
589 | 0 | if ((bound = php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 1))) { |
590 | 0 | max = bound - self->buf_begin; |
591 | 0 | if (end && php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 0)) { |
592 | 0 | *end = 1; |
593 | 0 | } |
594 | 0 | } else { |
595 | 0 | max = self->bytes_in_buffer; |
596 | 0 | } |
597 | | |
598 | | /* maximum number of bytes we are reading */ |
599 | 0 | len = max < bytes-1 ? max : bytes-1; |
600 | | |
601 | | /* if we read any data... */ |
602 | 0 | if (len > 0) { |
603 | | |
604 | | /* copy the data */ |
605 | 0 | memcpy(buf, self->buf_begin, len); |
606 | 0 | buf[len] = 0; |
607 | |
|
608 | 0 | if (bound && len > 0 && buf[len-1] == '\r') { |
609 | 0 | buf[--len] = 0; |
610 | 0 | } |
611 | | |
612 | | /* update the buffer */ |
613 | 0 | self->bytes_in_buffer -= (int)len; |
614 | 0 | self->buf_begin += len; |
615 | 0 | } |
616 | |
|
617 | 0 | return len; |
618 | 0 | } |
619 | | |
620 | | /* |
621 | | XXX: this is horrible memory-usage-wise, but we only expect |
622 | | to do this on small pieces of form data. |
623 | | */ |
624 | | static char *multipart_buffer_read_body(multipart_buffer *self, size_t *len) |
625 | 0 | { |
626 | 0 | char buf[FILLUNIT], *out=NULL; |
627 | 0 | size_t total_bytes=0, read_bytes=0; |
628 | |
|
629 | 0 | while((read_bytes = multipart_buffer_read(self, buf, sizeof(buf), NULL))) { |
630 | 0 | out = erealloc(out, total_bytes + read_bytes + 1); |
631 | 0 | memcpy(out + total_bytes, buf, read_bytes); |
632 | 0 | total_bytes += read_bytes; |
633 | 0 | } |
634 | |
|
635 | 0 | if (out) { |
636 | 0 | out[total_bytes] = '\0'; |
637 | 0 | } |
638 | 0 | *len = total_bytes; |
639 | |
|
640 | 0 | return out; |
641 | 0 | } |
642 | | /* }}} */ |
643 | | |
644 | | /* |
645 | | * The combined READER/HANDLER |
646 | | * |
647 | | */ |
648 | | |
649 | | SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) |
650 | 0 | { |
651 | 0 | char *boundary, *s = NULL, *boundary_end = NULL, *start_arr = NULL, *array_index = NULL; |
652 | 0 | char *lbuf = NULL, *abuf = NULL; |
653 | 0 | zend_string *temp_filename = NULL; |
654 | 0 | int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0; |
655 | 0 | size_t array_len = 0; |
656 | 0 | int64_t total_bytes = 0, max_file_size = 0; |
657 | 0 | int skip_upload = 0, anonymous_index = 0; |
658 | 0 | HashTable *uploaded_files = NULL; |
659 | 0 | multipart_buffer *mbuff; |
660 | 0 | zval *array_ptr = (zval *) arg; |
661 | 0 | bool throw_exceptions = SG(request_parse_body_context).throw_exceptions; |
662 | 0 | int fd = -1; |
663 | 0 | zend_llist header; |
664 | 0 | void *event_extra_data = NULL; |
665 | 0 | unsigned int llen = 0; |
666 | 0 | zend_long upload_cnt = REQUEST_PARSE_BODY_OPTION_GET(max_file_uploads, zend_ini_long_literal("max_file_uploads")); |
667 | 0 | zend_long body_parts_cnt = REQUEST_PARSE_BODY_OPTION_GET(max_multipart_body_parts, zend_ini_long_literal("max_multipart_body_parts")); |
668 | 0 | zend_long post_max_size = REQUEST_PARSE_BODY_OPTION_GET(post_max_size, SG(post_max_size)); |
669 | 0 | zend_long max_input_vars = REQUEST_PARSE_BODY_OPTION_GET(max_input_vars, PG(max_input_vars)); |
670 | 0 | zend_long upload_max_filesize = REQUEST_PARSE_BODY_OPTION_GET(upload_max_filesize, PG(upload_max_filesize)); |
671 | 0 | const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding(); |
672 | 0 | php_rfc1867_getword_t getword; |
673 | 0 | php_rfc1867_getword_conf_t getword_conf; |
674 | 0 | php_rfc1867_basename_t _basename; |
675 | 0 | zend_long count = 0; |
676 | |
|
677 | 0 | #define EMIT_WARNING_OR_ERROR(...) do { \ |
678 | 0 | if (throw_exceptions) { \ |
679 | 0 | zend_throw_exception_ex(zend_ce_request_parse_body_exception, 0, __VA_ARGS__); \ |
680 | 0 | } else { \ |
681 | 0 | php_error_docref(NULL, E_WARNING, __VA_ARGS__); \ |
682 | 0 | } \ |
683 | 0 | } while (0) |
684 | |
|
685 | 0 | if (php_rfc1867_encoding_translation() && internal_encoding) { |
686 | 0 | getword = php_rfc1867_getword; |
687 | 0 | getword_conf = php_rfc1867_getword_conf; |
688 | 0 | _basename = php_rfc1867_basename; |
689 | 0 | } else { |
690 | 0 | getword = php_ap_getword; |
691 | 0 | getword_conf = php_ap_getword_conf; |
692 | 0 | _basename = php_ap_basename; |
693 | 0 | } |
694 | |
|
695 | 0 | if (post_max_size > 0 && SG(request_info).content_length > post_max_size) { |
696 | 0 | EMIT_WARNING_OR_ERROR("POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes", SG(request_info).content_length, post_max_size); |
697 | 0 | return; |
698 | 0 | } |
699 | | |
700 | 0 | if (body_parts_cnt < 0) { |
701 | 0 | body_parts_cnt = max_input_vars + upload_cnt; |
702 | 0 | } |
703 | 0 | int body_parts_limit = body_parts_cnt; |
704 | | |
705 | | /* Get the boundary */ |
706 | 0 | boundary = strstr(content_type_dup, "boundary"); |
707 | 0 | if (!boundary) { |
708 | 0 | int content_type_len = (int)strlen(content_type_dup); |
709 | 0 | char *content_type_lcase = estrndup(content_type_dup, content_type_len); |
710 | |
|
711 | 0 | zend_str_tolower(content_type_lcase, content_type_len); |
712 | 0 | boundary = strstr(content_type_lcase, "boundary"); |
713 | 0 | if (boundary) { |
714 | 0 | boundary = content_type_dup + (boundary - content_type_lcase); |
715 | 0 | } |
716 | 0 | efree(content_type_lcase); |
717 | 0 | } |
718 | |
|
719 | 0 | if (!boundary || !(boundary = strchr(boundary, '='))) { |
720 | 0 | EMIT_WARNING_OR_ERROR("Missing boundary in multipart/form-data POST data"); |
721 | 0 | return; |
722 | 0 | } |
723 | | |
724 | 0 | boundary++; |
725 | 0 | boundary_len = (int)strlen(boundary); |
726 | |
|
727 | 0 | if (boundary[0] == '"') { |
728 | 0 | boundary++; |
729 | 0 | boundary_end = strchr(boundary, '"'); |
730 | 0 | if (!boundary_end) { |
731 | 0 | EMIT_WARNING_OR_ERROR("Invalid boundary in multipart/form-data POST data"); |
732 | 0 | return; |
733 | 0 | } |
734 | 0 | } else { |
735 | | /* search for the end of the boundary */ |
736 | 0 | boundary_end = strpbrk(boundary, ",;"); |
737 | 0 | } |
738 | 0 | if (boundary_end) { |
739 | 0 | boundary_end[0] = '\0'; |
740 | 0 | boundary_len = boundary_end-boundary; |
741 | 0 | } |
742 | | |
743 | | /* Boundaries larger than FILLUNIT-strlen("\r\n--") characters lead to |
744 | | * erroneous parsing */ |
745 | 0 | if (boundary_len > FILLUNIT-strlen("\r\n--")) { |
746 | 0 | sapi_module.sapi_error(E_WARNING, "Boundary too large in multipart/form-data POST data"); |
747 | 0 | return; |
748 | 0 | } |
749 | | |
750 | | /* Initialize the buffer */ |
751 | 0 | mbuff = multipart_buffer_new(boundary, boundary_len); |
752 | | |
753 | | /* Initialize $_FILES[] */ |
754 | 0 | zend_hash_init(&PG(rfc1867_protected_variables), 8, NULL, NULL, 0); |
755 | |
|
756 | 0 | ALLOC_HASHTABLE(uploaded_files); |
757 | 0 | zend_hash_init(uploaded_files, 8, NULL, free_filename, 0); |
758 | 0 | SG(rfc1867_uploaded_files) = uploaded_files; |
759 | |
|
760 | 0 | if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) != IS_ARRAY) { |
761 | | /* php_auto_globals_create_files() might have already done that */ |
762 | 0 | array_init(&PG(http_globals)[TRACK_VARS_FILES]); |
763 | 0 | } |
764 | |
|
765 | 0 | zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0); |
766 | |
|
767 | 0 | if (php_rfc1867_callback != NULL) { |
768 | 0 | multipart_event_start event_start; |
769 | |
|
770 | 0 | event_start.content_length = SG(request_info).content_length; |
771 | 0 | if (php_rfc1867_callback(MULTIPART_EVENT_START, &event_start, &event_extra_data) == FAILURE) { |
772 | 0 | goto fileupload_done; |
773 | 0 | } |
774 | 0 | } |
775 | | |
776 | 0 | while (!multipart_buffer_eof(mbuff)) |
777 | 0 | { |
778 | 0 | char buff[FILLUNIT]; |
779 | 0 | char *cd = NULL, *param = NULL, *filename = NULL, *tmp = NULL; |
780 | 0 | size_t blen = 0, wlen = 0; |
781 | 0 | zend_off_t offset; |
782 | |
|
783 | 0 | zend_llist_clean(&header); |
784 | |
|
785 | 0 | if (!multipart_buffer_headers(mbuff, &header)) { |
786 | 0 | goto fileupload_done; |
787 | 0 | } |
788 | | |
789 | 0 | if ((cd = php_mime_get_hdr_value(header, "Content-Disposition"))) { |
790 | 0 | char *pair = NULL; |
791 | 0 | int end = 0; |
792 | |
|
793 | 0 | if (--body_parts_cnt < 0) { |
794 | 0 | EMIT_WARNING_OR_ERROR("Multipart body parts limit exceeded %d. To increase the limit change max_multipart_body_parts in php.ini.", body_parts_limit); |
795 | 0 | goto fileupload_done; |
796 | 0 | } |
797 | | |
798 | 0 | while (isspace((unsigned char)*cd)) { |
799 | 0 | ++cd; |
800 | 0 | } |
801 | |
|
802 | 0 | while (*cd && (pair = getword(mbuff->input_encoding, &cd, ';'))) |
803 | 0 | { |
804 | 0 | char *key = NULL, *word = pair; |
805 | |
|
806 | 0 | while (isspace((unsigned char)*cd)) { |
807 | 0 | ++cd; |
808 | 0 | } |
809 | |
|
810 | 0 | if (strchr(pair, '=')) { |
811 | 0 | key = getword(mbuff->input_encoding, &pair, '='); |
812 | |
|
813 | 0 | if (!strcasecmp(key, "name")) { |
814 | 0 | if (param) { |
815 | 0 | efree(param); |
816 | 0 | } |
817 | 0 | param = getword_conf(mbuff->input_encoding, pair); |
818 | 0 | if (mbuff->input_encoding && internal_encoding) { |
819 | 0 | unsigned char *new_param; |
820 | 0 | size_t new_param_len; |
821 | 0 | if ((size_t)-1 != zend_multibyte_encoding_converter(&new_param, &new_param_len, (unsigned char *)param, strlen(param), internal_encoding, mbuff->input_encoding)) { |
822 | 0 | efree(param); |
823 | 0 | param = (char *)new_param; |
824 | 0 | } |
825 | 0 | } |
826 | 0 | } else if (!strcasecmp(key, "filename")) { |
827 | 0 | if (filename) { |
828 | 0 | efree(filename); |
829 | 0 | } |
830 | 0 | filename = getword_conf(mbuff->input_encoding, pair); |
831 | 0 | if (mbuff->input_encoding && internal_encoding) { |
832 | 0 | unsigned char *new_filename; |
833 | 0 | size_t new_filename_len; |
834 | 0 | if ((size_t)-1 != zend_multibyte_encoding_converter(&new_filename, &new_filename_len, (unsigned char *)filename, strlen(filename), internal_encoding, mbuff->input_encoding)) { |
835 | 0 | efree(filename); |
836 | 0 | filename = (char *)new_filename; |
837 | 0 | } |
838 | 0 | } |
839 | 0 | } |
840 | 0 | } |
841 | 0 | if (key) { |
842 | 0 | efree(key); |
843 | 0 | } |
844 | 0 | efree(word); |
845 | 0 | } |
846 | | |
847 | | /* Normal form variable, safe to read all data into memory */ |
848 | 0 | if (!filename && param) { |
849 | 0 | size_t value_len; |
850 | 0 | char *value = multipart_buffer_read_body(mbuff, &value_len); |
851 | 0 | size_t new_val_len; /* Dummy variable */ |
852 | |
|
853 | 0 | if (!value) { |
854 | 0 | value = estrdup(""); |
855 | 0 | value_len = 0; |
856 | 0 | } |
857 | |
|
858 | 0 | if (mbuff->input_encoding && internal_encoding) { |
859 | 0 | unsigned char *new_value; |
860 | 0 | size_t new_value_len; |
861 | 0 | if ((size_t)-1 != zend_multibyte_encoding_converter(&new_value, &new_value_len, (unsigned char *)value, value_len, internal_encoding, mbuff->input_encoding)) { |
862 | 0 | efree(value); |
863 | 0 | value = (char *)new_value; |
864 | 0 | value_len = new_value_len; |
865 | 0 | } |
866 | 0 | } |
867 | |
|
868 | 0 | if (++count <= max_input_vars && sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len)) { |
869 | 0 | if (php_rfc1867_callback != NULL) { |
870 | 0 | multipart_event_formdata event_formdata; |
871 | 0 | size_t newlength = new_val_len; |
872 | |
|
873 | 0 | event_formdata.post_bytes_processed = SG(read_post_bytes); |
874 | 0 | event_formdata.name = param; |
875 | 0 | event_formdata.value = &value; |
876 | 0 | event_formdata.length = new_val_len; |
877 | 0 | event_formdata.newlength = &newlength; |
878 | 0 | if (php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data) == FAILURE) { |
879 | 0 | efree(param); |
880 | 0 | efree(value); |
881 | 0 | continue; |
882 | 0 | } |
883 | 0 | new_val_len = newlength; |
884 | 0 | } |
885 | 0 | safe_php_register_variable(param, value, new_val_len, array_ptr, 0); |
886 | 0 | } else { |
887 | 0 | if (count == max_input_vars + 1) { |
888 | 0 | EMIT_WARNING_OR_ERROR("Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", max_input_vars); |
889 | 0 | } |
890 | |
|
891 | 0 | if (php_rfc1867_callback != NULL) { |
892 | 0 | multipart_event_formdata event_formdata; |
893 | |
|
894 | 0 | event_formdata.post_bytes_processed = SG(read_post_bytes); |
895 | 0 | event_formdata.name = param; |
896 | 0 | event_formdata.value = &value; |
897 | 0 | event_formdata.length = value_len; |
898 | 0 | event_formdata.newlength = NULL; |
899 | 0 | php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data); |
900 | 0 | } |
901 | 0 | } |
902 | | |
903 | 0 | if (!strcasecmp(param, "MAX_FILE_SIZE")) { |
904 | 0 | max_file_size = strtoll(value, NULL, 10); |
905 | 0 | } |
906 | |
|
907 | 0 | efree(param); |
908 | 0 | efree(value); |
909 | 0 | continue; |
910 | 0 | } |
911 | | |
912 | | /* If file_uploads=off, skip the file part */ |
913 | 0 | if (!PG(file_uploads)) { |
914 | 0 | skip_upload = 1; |
915 | 0 | } else if (upload_cnt <= 0) { |
916 | 0 | skip_upload = 1; |
917 | 0 | if (upload_cnt == 0) { |
918 | 0 | --upload_cnt; |
919 | 0 | EMIT_WARNING_OR_ERROR("Maximum number of allowable file uploads has been exceeded"); |
920 | 0 | } |
921 | 0 | } |
922 | | |
923 | | /* Return with an error if the posted data is garbled */ |
924 | 0 | if (!param && !filename) { |
925 | 0 | EMIT_WARNING_OR_ERROR("File Upload Mime headers garbled"); |
926 | 0 | goto fileupload_done; |
927 | 0 | } |
928 | | |
929 | 0 | if (!param) { |
930 | 0 | param = emalloc(MAX_SIZE_ANONNAME); |
931 | 0 | snprintf(param, MAX_SIZE_ANONNAME, "%u", anonymous_index++); |
932 | 0 | } |
933 | | |
934 | | /* New Rule: never repair potential malicious user input */ |
935 | 0 | if (!skip_upload) { |
936 | 0 | long c = 0; |
937 | 0 | tmp = param; |
938 | |
|
939 | 0 | while (*tmp) { |
940 | 0 | if (*tmp == '[') { |
941 | 0 | c++; |
942 | 0 | } else if (*tmp == ']') { |
943 | 0 | c--; |
944 | 0 | if (tmp[1] && tmp[1] != '[') { |
945 | 0 | skip_upload = 1; |
946 | 0 | break; |
947 | 0 | } |
948 | 0 | } |
949 | 0 | if (c < 0) { |
950 | 0 | skip_upload = 1; |
951 | 0 | break; |
952 | 0 | } |
953 | 0 | tmp++; |
954 | 0 | } |
955 | | /* Brackets should always be closed */ |
956 | 0 | if(c != 0) { |
957 | 0 | skip_upload = 1; |
958 | 0 | } |
959 | 0 | } |
960 | |
|
961 | 0 | total_bytes = cancel_upload = 0; |
962 | 0 | temp_filename = NULL; |
963 | 0 | fd = -1; |
964 | |
|
965 | 0 | if (!skip_upload && php_rfc1867_callback != NULL) { |
966 | 0 | multipart_event_file_start event_file_start; |
967 | |
|
968 | 0 | event_file_start.post_bytes_processed = SG(read_post_bytes); |
969 | 0 | event_file_start.name = param; |
970 | 0 | event_file_start.filename = &filename; |
971 | 0 | if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data) == FAILURE) { |
972 | 0 | temp_filename = NULL; |
973 | 0 | efree(param); |
974 | 0 | efree(filename); |
975 | 0 | continue; |
976 | 0 | } |
977 | 0 | } |
978 | | |
979 | 0 | if (skip_upload) { |
980 | 0 | efree(param); |
981 | 0 | efree(filename); |
982 | 0 | continue; |
983 | 0 | } |
984 | | |
985 | 0 | if (filename[0] == '\0') { |
986 | | #if DEBUG_FILE_UPLOAD |
987 | | sapi_module.sapi_error(E_NOTICE, "No file uploaded"); |
988 | | #endif |
989 | 0 | cancel_upload = PHP_UPLOAD_ERROR_D; |
990 | 0 | } |
991 | |
|
992 | 0 | offset = 0; |
993 | 0 | end = 0; |
994 | |
|
995 | 0 | if (!cancel_upload) { |
996 | | /* only bother to open temp file if we have data */ |
997 | 0 | blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end); |
998 | | #if DEBUG_FILE_UPLOAD |
999 | | if (blen > 0) { |
1000 | | #else |
1001 | | /* in non-debug mode we have no problem with 0-length files */ |
1002 | 0 | { |
1003 | 0 | #endif |
1004 | 0 | fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK); |
1005 | 0 | upload_cnt--; |
1006 | 0 | if (fd == -1) { |
1007 | 0 | EMIT_WARNING_OR_ERROR("File upload error - unable to create a temporary file"); |
1008 | 0 | cancel_upload = PHP_UPLOAD_ERROR_E; |
1009 | 0 | } |
1010 | 0 | } |
1011 | 0 | } |
1012 | |
|
1013 | 0 | while (!cancel_upload && (blen > 0)) |
1014 | 0 | { |
1015 | 0 | if (php_rfc1867_callback != NULL) { |
1016 | 0 | multipart_event_file_data event_file_data; |
1017 | |
|
1018 | 0 | event_file_data.post_bytes_processed = SG(read_post_bytes); |
1019 | 0 | event_file_data.offset = offset; |
1020 | 0 | event_file_data.data = buff; |
1021 | 0 | event_file_data.length = blen; |
1022 | 0 | event_file_data.newlength = &blen; |
1023 | 0 | if (php_rfc1867_callback(MULTIPART_EVENT_FILE_DATA, &event_file_data, &event_extra_data) == FAILURE) { |
1024 | 0 | cancel_upload = PHP_UPLOAD_ERROR_X; |
1025 | 0 | continue; |
1026 | 0 | } |
1027 | 0 | } |
1028 | | |
1029 | 0 | if (upload_max_filesize > 0 && (zend_long)(total_bytes+blen) > upload_max_filesize) { |
1030 | | #if DEBUG_FILE_UPLOAD |
1031 | | sapi_module.sapi_error(E_NOTICE, "upload_max_filesize of " ZEND_LONG_FMT " bytes exceeded - file [%s=%s] not saved", upload_max_filesize, param, filename); |
1032 | | #endif |
1033 | 0 | cancel_upload = PHP_UPLOAD_ERROR_A; |
1034 | 0 | } else if (max_file_size && ((zend_long)(total_bytes+blen) > max_file_size)) { |
1035 | | #if DEBUG_FILE_UPLOAD |
1036 | | sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of %" PRId64 " bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename); |
1037 | | #endif |
1038 | 0 | cancel_upload = PHP_UPLOAD_ERROR_B; |
1039 | 0 | } else if (blen > 0) { |
1040 | | #ifdef PHP_WIN32 |
1041 | | wlen = write(fd, buff, (unsigned int)blen); |
1042 | | #else |
1043 | 0 | wlen = write(fd, buff, blen); |
1044 | 0 | #endif |
1045 | |
|
1046 | 0 | if (wlen == (size_t)-1) { |
1047 | | /* write failed */ |
1048 | | #if DEBUG_FILE_UPLOAD |
1049 | | char errstr[256]; |
1050 | | sapi_module.sapi_error(E_NOTICE, "write() failed - %s", php_socket_strerror_s(errno, errstr, sizeof(errstr))); |
1051 | | #endif |
1052 | 0 | cancel_upload = PHP_UPLOAD_ERROR_F; |
1053 | 0 | } else if (wlen < blen) { |
1054 | | #if DEBUG_FILE_UPLOAD |
1055 | | sapi_module.sapi_error(E_NOTICE, "Only %zd bytes were written, expected to write %zd", wlen, blen); |
1056 | | #endif |
1057 | 0 | cancel_upload = PHP_UPLOAD_ERROR_F; |
1058 | 0 | } else { |
1059 | 0 | total_bytes += wlen; |
1060 | 0 | } |
1061 | 0 | offset += wlen; |
1062 | 0 | } |
1063 | | |
1064 | | /* read data for next iteration */ |
1065 | 0 | blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end); |
1066 | 0 | } |
1067 | |
|
1068 | 0 | if (fd != -1) { /* may not be initialized if file could not be created */ |
1069 | 0 | close(fd); |
1070 | 0 | } |
1071 | |
|
1072 | 0 | if (!cancel_upload && !end) { |
1073 | | #if DEBUG_FILE_UPLOAD |
1074 | | sapi_module.sapi_error(E_NOTICE, "Missing mime boundary at the end of the data for file %s", filename[0] != '\0' ? filename : ""); |
1075 | | #endif |
1076 | 0 | cancel_upload = PHP_UPLOAD_ERROR_C; |
1077 | 0 | } |
1078 | | #if DEBUG_FILE_UPLOAD |
1079 | | if (filename[0] != '\0' && total_bytes == 0 && !cancel_upload) { |
1080 | | EMIT_WARNING_OR_ERROR("Uploaded file size 0 - file [%s=%s] not saved", param, filename); |
1081 | | cancel_upload = 5; |
1082 | | } |
1083 | | #endif |
1084 | 0 | if (php_rfc1867_callback != NULL) { |
1085 | 0 | multipart_event_file_end event_file_end; |
1086 | |
|
1087 | 0 | event_file_end.post_bytes_processed = SG(read_post_bytes); |
1088 | 0 | event_file_end.temp_filename = temp_filename ? ZSTR_VAL(temp_filename) : NULL; |
1089 | 0 | event_file_end.cancel_upload = cancel_upload; |
1090 | 0 | if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data) == FAILURE) { |
1091 | 0 | cancel_upload = PHP_UPLOAD_ERROR_X; |
1092 | 0 | } |
1093 | 0 | } |
1094 | |
|
1095 | 0 | if (cancel_upload) { |
1096 | 0 | if (temp_filename) { |
1097 | 0 | if (cancel_upload != PHP_UPLOAD_ERROR_E) { /* file creation failed */ |
1098 | 0 | unlink(ZSTR_VAL(temp_filename)); |
1099 | 0 | } |
1100 | 0 | zend_string_release_ex(temp_filename, 0); |
1101 | 0 | } |
1102 | 0 | temp_filename = NULL; |
1103 | 0 | } else { |
1104 | 0 | zend_hash_add_ptr(SG(rfc1867_uploaded_files), temp_filename, temp_filename); |
1105 | 0 | } |
1106 | | |
1107 | | /* is_arr_upload is true when name of file upload field |
1108 | | * ends in [.*] |
1109 | | * start_arr is set to point to 1st [ */ |
1110 | 0 | is_arr_upload = (start_arr = strchr(param,'[')) && (param[strlen(param)-1] == ']'); |
1111 | |
|
1112 | 0 | if (is_arr_upload) { |
1113 | 0 | array_len = strlen(start_arr); |
1114 | 0 | if (array_index) { |
1115 | 0 | efree(array_index); |
1116 | 0 | } |
1117 | 0 | array_index = estrndup(start_arr + 1, array_len - 2); |
1118 | 0 | } |
1119 | | |
1120 | | /* Add $foo_name */ |
1121 | 0 | if (llen < strlen(param) + MAX_SIZE_OF_INDEX + 1) { |
1122 | 0 | llen = (int)strlen(param); |
1123 | 0 | lbuf = (char *) safe_erealloc(lbuf, llen, 1, MAX_SIZE_OF_INDEX + 1); |
1124 | 0 | llen += MAX_SIZE_OF_INDEX + 1; |
1125 | 0 | } |
1126 | |
|
1127 | 0 | if (is_arr_upload) { |
1128 | 0 | if (abuf) efree(abuf); |
1129 | 0 | abuf = estrndup(param, strlen(param)-array_len); |
1130 | 0 | snprintf(lbuf, llen, "%s_name[%s]", abuf, array_index); |
1131 | 0 | } else { |
1132 | 0 | snprintf(lbuf, llen, "%s_name", param); |
1133 | 0 | } |
1134 | | |
1135 | | /* Pursuant to RFC 7578, strip any path components in the |
1136 | | * user-supplied file name: |
1137 | | * > If a "filename" parameter is supplied ... do not use |
1138 | | * > directory path information that may be present." |
1139 | | */ |
1140 | 0 | s = _basename(internal_encoding, filename); |
1141 | 0 | if (!s) { |
1142 | 0 | s = filename; |
1143 | 0 | } |
1144 | | |
1145 | | /* Add $foo[name] */ |
1146 | 0 | if (is_arr_upload) { |
1147 | 0 | snprintf(lbuf, llen, "%s[name][%s]", abuf, array_index); |
1148 | 0 | } else { |
1149 | 0 | snprintf(lbuf, llen, "%s[name]", param); |
1150 | 0 | } |
1151 | 0 | register_http_post_files_variable(lbuf, s, &PG(http_globals)[TRACK_VARS_FILES], 0); |
1152 | 0 | s = NULL; |
1153 | | |
1154 | | /* Add full path of supplied file for folder uploads via |
1155 | | * <input type="file" name="files" multiple webkitdirectory> |
1156 | | */ |
1157 | | /* Add $foo[full_path] */ |
1158 | 0 | if (is_arr_upload) { |
1159 | 0 | snprintf(lbuf, llen, "%s[full_path][%s]", abuf, array_index); |
1160 | 0 | } else { |
1161 | 0 | snprintf(lbuf, llen, "%s[full_path]", param); |
1162 | 0 | } |
1163 | 0 | register_http_post_files_variable(lbuf, filename, &PG(http_globals)[TRACK_VARS_FILES], 0); |
1164 | 0 | efree(filename); |
1165 | | |
1166 | | /* Possible Content-Type: */ |
1167 | 0 | if (cancel_upload || !(cd = php_mime_get_hdr_value(header, "Content-Type"))) { |
1168 | 0 | cd = ""; |
1169 | 0 | } else { |
1170 | | /* fix for Opera 6.01 */ |
1171 | 0 | s = strchr(cd, ';'); |
1172 | 0 | if (s != NULL) { |
1173 | 0 | *s = '\0'; |
1174 | 0 | } |
1175 | 0 | } |
1176 | | |
1177 | | /* Add $foo[type] */ |
1178 | 0 | if (is_arr_upload) { |
1179 | 0 | snprintf(lbuf, llen, "%s[type][%s]", abuf, array_index); |
1180 | 0 | } else { |
1181 | 0 | snprintf(lbuf, llen, "%s[type]", param); |
1182 | 0 | } |
1183 | 0 | register_http_post_files_variable(lbuf, cd, &PG(http_globals)[TRACK_VARS_FILES], 0); |
1184 | | |
1185 | | /* Restore Content-Type Header */ |
1186 | 0 | if (s != NULL) { |
1187 | 0 | *s = ';'; |
1188 | 0 | } |
1189 | 0 | s = ""; |
1190 | |
|
1191 | 0 | { |
1192 | | /* store temp_filename as-is (in case upload_tmp_dir |
1193 | | * contains escapable characters. escape only the variable name.) */ |
1194 | 0 | zval zfilename; |
1195 | | |
1196 | | /* Initialize variables */ |
1197 | 0 | add_protected_variable(param); |
1198 | | |
1199 | | /* Add $foo[tmp_name] */ |
1200 | 0 | if (is_arr_upload) { |
1201 | 0 | snprintf(lbuf, llen, "%s[tmp_name][%s]", abuf, array_index); |
1202 | 0 | } else { |
1203 | 0 | snprintf(lbuf, llen, "%s[tmp_name]", param); |
1204 | 0 | } |
1205 | 0 | add_protected_variable(lbuf); |
1206 | 0 | if (temp_filename) { |
1207 | 0 | ZVAL_STR_COPY(&zfilename, temp_filename); |
1208 | 0 | } else { |
1209 | 0 | ZVAL_EMPTY_STRING(&zfilename); |
1210 | 0 | } |
1211 | 0 | register_http_post_files_variable_ex(lbuf, &zfilename, &PG(http_globals)[TRACK_VARS_FILES], 1); |
1212 | 0 | } |
1213 | |
|
1214 | 0 | { |
1215 | 0 | zval file_size, error_type; |
1216 | 0 | int size_overflow = 0; |
1217 | 0 | char file_size_buf[65]; |
1218 | |
|
1219 | 0 | ZVAL_LONG(&error_type, cancel_upload); |
1220 | | |
1221 | | /* Add $foo[error] */ |
1222 | 0 | if (cancel_upload) { |
1223 | 0 | ZVAL_LONG(&file_size, 0); |
1224 | 0 | } else { |
1225 | 0 | if (total_bytes > ZEND_LONG_MAX) { |
1226 | | #ifdef PHP_WIN32 |
1227 | | if (_i64toa_s(total_bytes, file_size_buf, 65, 10)) { |
1228 | | file_size_buf[0] = '0'; |
1229 | | file_size_buf[1] = '\0'; |
1230 | | } |
1231 | | #else |
1232 | 0 | { |
1233 | 0 | int __len = snprintf(file_size_buf, 65, "%" PRId64, total_bytes); |
1234 | 0 | file_size_buf[__len] = '\0'; |
1235 | 0 | } |
1236 | 0 | #endif |
1237 | 0 | size_overflow = 1; |
1238 | |
|
1239 | 0 | } else { |
1240 | 0 | ZVAL_LONG(&file_size, total_bytes); |
1241 | 0 | } |
1242 | 0 | } |
1243 | |
|
1244 | 0 | if (is_arr_upload) { |
1245 | 0 | snprintf(lbuf, llen, "%s[error][%s]", abuf, array_index); |
1246 | 0 | } else { |
1247 | 0 | snprintf(lbuf, llen, "%s[error]", param); |
1248 | 0 | } |
1249 | 0 | register_http_post_files_variable_ex(lbuf, &error_type, &PG(http_globals)[TRACK_VARS_FILES], 0); |
1250 | | |
1251 | | /* Add $foo[size] */ |
1252 | 0 | if (is_arr_upload) { |
1253 | 0 | snprintf(lbuf, llen, "%s[size][%s]", abuf, array_index); |
1254 | 0 | } else { |
1255 | 0 | snprintf(lbuf, llen, "%s[size]", param); |
1256 | 0 | } |
1257 | 0 | if (size_overflow) { |
1258 | 0 | ZVAL_STRING(&file_size, file_size_buf); |
1259 | 0 | } |
1260 | 0 | register_http_post_files_variable_ex(lbuf, &file_size, &PG(http_globals)[TRACK_VARS_FILES], size_overflow); |
1261 | 0 | } |
1262 | 0 | efree(param); |
1263 | 0 | } |
1264 | 0 | } |
1265 | | |
1266 | 0 | fileupload_done: |
1267 | 0 | if (php_rfc1867_callback != NULL) { |
1268 | 0 | multipart_event_end event_end; |
1269 | |
|
1270 | 0 | event_end.post_bytes_processed = SG(read_post_bytes); |
1271 | 0 | php_rfc1867_callback(MULTIPART_EVENT_END, &event_end, &event_extra_data); |
1272 | 0 | } |
1273 | |
|
1274 | 0 | if (lbuf) efree(lbuf); |
1275 | 0 | if (abuf) efree(abuf); |
1276 | 0 | if (array_index) efree(array_index); |
1277 | 0 | zend_hash_destroy(&PG(rfc1867_protected_variables)); |
1278 | 0 | zend_llist_destroy(&header); |
1279 | 0 | if (mbuff->boundary_next) efree(mbuff->boundary_next); |
1280 | 0 | if (mbuff->boundary) efree(mbuff->boundary); |
1281 | 0 | if (mbuff->buffer) efree(mbuff->buffer); |
1282 | 0 | if (mbuff) efree(mbuff); |
1283 | |
|
1284 | 0 | #undef EMIT_WARNING_OR_ERROR |
1285 | 0 | } |
1286 | | /* }}} */ |
1287 | | |
1288 | | SAPI_API void php_rfc1867_set_multibyte_callbacks( |
1289 | | php_rfc1867_encoding_translation_t encoding_translation, |
1290 | | php_rfc1867_get_detect_order_t get_detect_order, |
1291 | | php_rfc1867_set_input_encoding_t set_input_encoding, |
1292 | | php_rfc1867_getword_t getword, |
1293 | | php_rfc1867_getword_conf_t getword_conf, |
1294 | | php_rfc1867_basename_t basename) /* {{{ */ |
1295 | 0 | { |
1296 | 0 | php_rfc1867_encoding_translation = encoding_translation; |
1297 | 0 | php_rfc1867_get_detect_order = get_detect_order; |
1298 | 0 | php_rfc1867_set_input_encoding = set_input_encoding; |
1299 | 0 | php_rfc1867_getword = getword; |
1300 | 0 | php_rfc1867_getword_conf = getword_conf; |
1301 | 0 | php_rfc1867_basename = basename; |
1302 | 0 | } |
1303 | | /* }}} */ |