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