port sendmail to windwos (untested yet)

This commit is contained in:
Pierre Joye 2014-05-13 06:51:11 +02:00
parent dbc6d0df84
commit d24c5de173

View file

@ -151,46 +151,47 @@ static char *ErrorMessages[] =
* Returns NULL on error, or the new char* buffer on success. * Returns NULL on error, or the new char* buffer on success.
* You have to take care and efree() the buffer on your own. * You have to take care and efree() the buffer on your own.
*/ */
static char *php_win32_mail_trim_header(char *header TSRMLS_DC) static zend_string *php_win32_mail_trim_header(char *header TSRMLS_DC)
{ {
#if HAVE_PCRE || HAVE_BUNDLED_PCRE #if HAVE_PCRE || HAVE_BUNDLED_PCRE
char *result, *result2; zend_string *result, *result2;
int result_len; zval replace;
zval *replace; zend_string *regex;
if (!header) { if (!header) {
return NULL; return NULL;
} }
MAKE_STD_ZVAL(replace); ZVAL_STRINGL(&replace, PHP_WIN32_MAIL_UNIFY_REPLACE, strlen(PHP_WIN32_MAIL_UNIFY_REPLACE));
ZVAL_STRING(replace, PHP_WIN32_MAIL_UNIFY_REPLACE, 0); regex = STR_INIT(PHP_WIN32_MAIL_UNIFY_REPLACE, sizeof(PHP_WIN32_MAIL_UNIFY_REPLACE)-1, 0);
result = php_pcre_replace(PHP_WIN32_MAIL_UNIFY_PATTERN, sizeof(PHP_WIN32_MAIL_UNIFY_PATTERN)-1, //zend_string *php_pcre_replace(zend_string *regex, char *subject, int subject_len, zval *replace_val, int is_callable_replace, int limit, int *replace_count TSRMLS_DC);
result = php_pcre_replace(regex,
header, strlen(header), header, strlen(header),
replace, &replace,
0, 0,
&result_len,
-1, -1,
NULL TSRMLS_CC); NULL TSRMLS_CC);
if (NULL == result) { if (NULL == result) {
FREE_ZVAL(replace); zval_ptr_dtor(&replace);
STR_FREE(regex);
return NULL; return NULL;
} }
ZVAL_STRING(replace, PHP_WIN32_MAIL_RMVDBL_REPLACE, 0); ZVAL_STRING(&replace, PHP_WIN32_MAIL_RMVDBL_PATTERN);
regex = STR_INIT(PHP_WIN32_MAIL_RMVDBL_PATTERN, sizeof(PHP_WIN32_MAIL_RMVDBL_PATTERN)-1, 0);
result2 = php_pcre_replace(PHP_WIN32_MAIL_RMVDBL_PATTERN, sizeof(PHP_WIN32_MAIL_RMVDBL_PATTERN)-1, result2 = php_pcre_replace(regex,
result, result_len, result->val, result->len,
replace, &replace,
0, 0,
&result_len, -1,
-1, NULL TSRMLS_CC);
NULL TSRMLS_CC); return result;
efree(result);
FREE_ZVAL(replace);
return result2;
#else #else
/* In case we don't have PCRE support (for whatever reason...) simply do nothing and return the unmodified header */ /* In case we don't have PCRE support (for whatever reason...) simply do nothing and return the unmodified header */
return estrdup(header); return estrdup(header);
@ -216,7 +217,7 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
{ {
int ret; int ret;
char *RPath = NULL; char *RPath = NULL;
char *headers_lc = NULL; /* headers_lc is only created if we've a header at all */ zend_string *headers_lc = NULL; /* headers_lc is only created if we've a header at all */
char *pos1 = NULL, *pos2 = NULL; char *pos1 = NULL, *pos2 = NULL;
#ifndef NETWARE #ifndef NETWARE
@ -236,22 +237,22 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
if (headers) { if (headers) {
char *pos = NULL; char *pos = NULL;
size_t i; size_t i;
zend_string *headers_trim;
/* Use PCRE to trim the header into the right format */ /* Use PCRE to trim the header into the right format */
if (NULL == (headers = php_win32_mail_trim_header(headers TSRMLS_CC))) { if (NULL == (headers_trim = php_win32_mail_trim_header(headers TSRMLS_CC))) {
*error = W32_SM_PCRE_ERROR; *error = W32_SM_PCRE_ERROR;
return FAILURE; return FAILURE;
} }
/* Create a lowercased header for all the searches so we're finally case /* Create a lowercased header for all the searches so we're finally case
* insensitive when searching for a pattern. */ * insensitive when searching for a pattern. */
if (NULL == (headers_lc = estrdup(headers))) { if (NULL == (headers_lc = STR_COPY(headers_trim))) {
efree(headers);
*error = OUT_OF_MEMORY; *error = OUT_OF_MEMORY;
return FAILURE; return FAILURE;
} }
for (i = 0; i < strlen(headers_lc); i++) { for (i = 0; i < headers_lc->len; i++) {
headers_lc[i] = tolower(headers_lc[i]); headers_lc->val[i] = tolower(headers_lc->val[i]);
} }
} }
@ -261,22 +262,21 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
} else if (INI_STR("sendmail_from")) { } else if (INI_STR("sendmail_from")) {
RPath = estrdup(INI_STR("sendmail_from")); RPath = estrdup(INI_STR("sendmail_from"));
} else if ( headers_lc && } else if ( headers_lc &&
(pos1 = strstr(headers_lc, "from:")) && (pos1 = strstr(headers_lc->val, "from:")) &&
((pos1 == headers_lc) || (*(pos1-1) == '\n')) ((pos1 == headers_lc->val) || (*(pos1-1) == '\n'))
) { ) {
/* Real offset is memaddress from the original headers + difference of /* Real offset is memaddress from the original headers + difference of
* string found in the lowercase headrs + 5 characters to jump over * string found in the lowercase headrs + 5 characters to jump over
* the from: */ * the from: */
pos1 = headers + (pos1 - headers_lc) + 5; pos1 = headers + (pos1 - headers_lc->val) + 5;
if (NULL == (pos2 = strstr(pos1, "\r\n"))) { if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
RPath = estrndup(pos1, strlen(pos1)); RPath = estrndup(pos1, strlen(pos1));
} else { } else {
RPath = estrndup(pos1, pos2 - pos1); RPath = estrndup(pos1, pos2 - pos1);
} }
} else { } else {
if (headers) { if (headers_lc) {
efree(headers); STR_FREE(headers_lc);
efree(headers_lc);
} }
*error = W32_SM_SENDMAIL_FROM_NOT_SET; *error = W32_SM_SENDMAIL_FROM_NOT_SET;
return FAILURE; return FAILURE;
@ -302,7 +302,7 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
MailHost, !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port")); MailHost, !INI_INT("smtp_port") ? 25 : INI_INT("smtp_port"));
return FAILURE; return FAILURE;
} else { } else {
ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers, headers_lc, error_message TSRMLS_CC); ret = SendText(RPath, Subject, mailTo, mailCc, mailBcc, data, headers, headers_lc->val, error_message TSRMLS_CC);
TSMClose(); TSMClose();
if (RPath) { if (RPath) {
efree(RPath); efree(RPath);
@ -387,8 +387,7 @@ static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char
char *tempMailTo, *token, *pos1, *pos2; char *tempMailTo, *token, *pos1, *pos2;
char *server_response = NULL; char *server_response = NULL;
char *stripped_header = NULL; char *stripped_header = NULL;
char *data_cln; zend_string *data_cln;
int data_cln_len;
/* check for NULL parameters */ /* check for NULL parameters */
if (data == NULL) if (data == NULL)
@ -617,39 +616,41 @@ static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char
return (res); return (res);
} }
PHPAPI zend_string *php_str_to_str(char *haystack, int length, char *needle,
int needle_len, char *str, int str_len);
/* Escape \n. sequences /* Escape \n. sequences
* We use php_str_to_str() and not php_str_replace_in_subject(), since the latter * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter
* uses ZVAL as it's parameters */ * uses ZVAL as it's parameters */
data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1, data_cln = php_str_to_str(data, strlen(data), PHP_WIN32_MAIL_DOT_PATTERN, sizeof(PHP_WIN32_MAIL_DOT_PATTERN) - 1,
PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1, &data_cln_len); PHP_WIN32_MAIL_DOT_REPLACE, sizeof(PHP_WIN32_MAIL_DOT_REPLACE) - 1);
if (!data_cln) { if (!data_cln) {
data_cln = estrdup(""); data_cln = STR_EMPTY_ALLOC();
data_cln_len = 1;
} }
/* send message contents in 1024 chunks */ /* send message contents in 1024 chunks */
{ {
char c, *e2, *e = data_cln + data_cln_len; char c, *e2, *e = data_cln->val + data_cln->len;
p = data_cln; p = data_cln->val;
while (e - p > 1024) { while (e - p > 1024) {
e2 = p + 1024; e2 = p + 1024;
c = *e2; c = *e2;
*e2 = '\0'; *e2 = '\0';
if ((res = Post(p)) != SUCCESS) { if ((res = Post(p)) != SUCCESS) {
efree(data_cln); STR_FREE(data_cln);
return(res); return(res);
} }
*e2 = c; *e2 = c;
p = e2; p = e2;
} }
if ((res = Post(p)) != SUCCESS) { if ((res = Post(p)) != SUCCESS) {
efree(data_cln); STR_FREE(data_cln);
return(res); return(res);
} }
} }
efree(data_cln); STR_FREE(data_cln);
/*send termination dot */ /*send termination dot */
if ((res = Post("\r\n.\r\n")) != SUCCESS) if ((res = Post("\r\n.\r\n")) != SUCCESS)
@ -705,10 +706,10 @@ static int PostHeader(char *RPath, char *Subject, char *mailTo, char *xheaders T
if (!xheaders || !strstr(headers_lc, "date:")) { if (!xheaders || !strstr(headers_lc, "date:")) {
time_t tNow = time(NULL); time_t tNow = time(NULL);
char *dt = php_format_date("r", 1, tNow, 1 TSRMLS_CC); zend_string *dt = php_format_date("r", 1, tNow, 1 TSRMLS_CC);
snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", dt); snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", dt->val);
efree(dt); STR_FREE(dt);
} }
if (!headers_lc || !strstr(headers_lc, "from:")) { if (!headers_lc || !strstr(headers_lc, "from:")) {