rfc1867.c 36 KB

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