--- main/rfc1867.c.orig Thu Feb 17 21:52:52 2005 +++ main/rfc1867.c Thu Feb 17 21:56:11 2005 @@ -31,6 +31,8 @@ #include "php_globals.h" #include "php_variables.h" #include "rfc1867.h" +#include "ext/standard/md5.h" +#include "ext/standard/sha1.h" #undef DEBUG_FILE_UPLOAD @@ -772,6 +774,13 @@ char *temp_filename=NULL, *lbuf=NULL, *abuf=NULL; int boundary_len=0, total_bytes=0, cancel_upload=0, is_arr_upload=0, array_len=0, max_file_size=0, skip_upload=0; zval *http_post_files=NULL; HashTable *uploaded_files=NULL; + int compute_md5=0, compute_sha1=0; + PHP_MD5_CTX md5_context; + unsigned char md5_digest[16]; + char md5_string[33]; + PHP_SHA1_CTX sha1_context; + unsigned char sha1_digest[20]; + char sha1_string[41]; #if HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING) int str_len = 0, num_vars = 0, num_vars_max = 2*10, *len_list = NULL; char **val_list = NULL; @@ -908,6 +917,10 @@ #endif if (!strcasecmp(param, "MAX_FILE_SIZE")) { max_file_size = atol(value); + } else if (!strcasecmp(param, "COMPUTE_MD5")) { + compute_md5 = atol(value); + } else if (!strcasecmp(param, "COMPUTE_SHA1")) { + compute_sha1 = atol(value); } efree(param); @@ -981,6 +994,14 @@ cancel_upload = UPLOAD_ERROR_D; } + if (compute_md5) { + PHP_MD5Init(&md5_context); + } + + if (compute_sha1) { + PHP_SHA1Init(&sha1_context); + } + while (!cancel_upload && (blen = multipart_buffer_read(mbuff, buff, sizeof(buff) TSRMLS_CC))) { if (PG(upload_max_filesize) > 0 && total_bytes > PG(upload_max_filesize)) { @@ -991,6 +1012,13 @@ cancel_upload = UPLOAD_ERROR_B; } else if (blen > 0) { wlen = fwrite(buff, 1, blen, fp); + + if (compute_md5) { + PHP_MD5Update(&md5_context, buff, blen); + } + if (compute_sha1) { + PHP_SHA1Update(&sha1_context, buff, blen); + } if (wlen < blen) { sapi_module.sapi_error(E_WARNING, "Only %d bytes were written, expected to write %d", wlen, blen); @@ -1004,6 +1032,15 @@ fclose(fp); } + if (compute_md5) { + PHP_MD5Final(md5_digest, &md5_context); + make_digest(md5_string, md5_digest); + } + if (compute_sha1) { + PHP_SHA1Final(sha1_digest, &sha1_context); + make_digest(sha1_string, sha1_digest); + } + #ifdef DEBUG_FILE_UPLOAD if(strlen(filename) > 0 && total_bytes == 0 && !cancel_upload) { sapi_module.sapi_error(E_WARNING, "Uploaded file size 0 - file [%s=%s] not saved", param, filename); @@ -1158,6 +1195,26 @@ } add_protected_variable(lbuf TSRMLS_CC); register_http_post_files_variable(lbuf, temp_filename, http_post_files, 1 TSRMLS_CC); + + /* Add $foo[md5] */ + if (!cancel_upload && compute_md5) { + if (is_arr_upload) { + sprintf(lbuf, "%s[md5][%s]", abuf, array_index); + } else { + sprintf(lbuf, "%s[md5]", param); + } + register_http_post_files_variable(lbuf, md5_string, http_post_files, 0 TSRMLS_CC); + } + + /* Add $foo[sha1] */ + if (!cancel_upload && compute_sha1) { + if (is_arr_upload) { + sprintf(lbuf, "%s[sha1][%s]", abuf, array_index); + } else { + sprintf(lbuf, "%s[sha1]", param); + } + register_http_post_files_variable(lbuf, sha1_string, http_post_files, 0 TSRMLS_CC); + } PG(magic_quotes_gpc) = magic_quotes_gpc;