]> granicus.if.org Git - php/commitdiff
port sendmail to windwos (untested yet)
authorPierre Joye <pierre.php@gmail.com>
Tue, 13 May 2014 04:51:11 +0000 (06:51 +0200)
committerPierre Joye <pierre.php@gmail.com>
Tue, 13 May 2014 04:51:11 +0000 (06:51 +0200)
win32/sendmail.c

index 93e68f4b1a421e277aefaedbcd1c51950795f5db..dd653879c21a89e05b4560c28d4767fed102c149 100644 (file)
@@ -151,46 +151,47 @@ static char *ErrorMessages[] =
  * Returns NULL on error, or the new char* buffer on success.
  * 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
        
-       char *result, *result2;
-       int result_len;
-       zval *replace;
+       zend_string *result, *result2;
+       zval replace;
+       zend_string *regex;
 
        if (!header) {
                return NULL;
        }
 
-       MAKE_STD_ZVAL(replace);
-       ZVAL_STRING(replace, PHP_WIN32_MAIL_UNIFY_REPLACE, 0);
+       ZVAL_STRINGL(&replace, PHP_WIN32_MAIL_UNIFY_REPLACE, strlen(PHP_WIN32_MAIL_UNIFY_REPLACE));
+       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),
-                                                         replace,
+                                                         &replace,
                                                          0,
-                                                         &result_len,
                                                          -1,
                                                          NULL TSRMLS_CC);
+
        if (NULL == result) {
-               FREE_ZVAL(replace);
+               zval_ptr_dtor(&replace);
+               STR_FREE(regex);
                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,
-                                                          result, result_len,
-                                                          replace,
-                                                          0,
-                                                          &result_len,
-                                                          -1,
-                                                          NULL TSRMLS_CC);
-       efree(result);
-       FREE_ZVAL(replace);
-       return result2;
+       result2 = php_pcre_replace(regex,
+                                                          result->val, result->len,
+                                                          &replace,
+                                                         0,
+                                                         -1,
+                                                         NULL TSRMLS_CC);
+       return result;
 #else
        /* In case we don't have PCRE support (for whatever reason...) simply do nothing and return the unmodified header */
        return estrdup(header);
@@ -216,7 +217,7 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
 {
        int ret;
        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;
 
 #ifndef NETWARE
@@ -236,22 +237,22 @@ PHPAPI int TSendMail(char *host, int *error, char **error_message,
        if (headers) {
                char *pos = NULL;
                size_t i;
+               zend_string *headers_trim;
 
                /* 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;
                        return FAILURE;
                }
 
                /* Create a lowercased header for all the searches so we're finally case
                 * insensitive when searching for a pattern. */
-               if (NULL == (headers_lc = estrdup(headers))) {
-                       efree(headers);
+               if (NULL == (headers_lc = STR_COPY(headers_trim))) {
                        *error = OUT_OF_MEMORY;
                        return FAILURE;
                }
-               for (i = 0; i < strlen(headers_lc); i++) {
-                       headers_lc[i] = tolower(headers_lc[i]);
+               for (i = 0; i < headers_lc->len; 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")) {
                RPath = estrdup(INI_STR("sendmail_from"));
        } else if (     headers_lc &&
-                               (pos1 = strstr(headers_lc, "from:")) &&
-                               ((pos1 == headers_lc) || (*(pos1-1) == '\n'))
+                               (pos1 = strstr(headers_lc->val, "from:")) &&
+                               ((pos1 == headers_lc->val) || (*(pos1-1) == '\n'))
        ) {
                /* Real offset is memaddress from the original headers + difference of
                 * string found in the lowercase headrs + 5 characters to jump over   
                 * the from: */
-               pos1 = headers + (pos1 - headers_lc) + 5;
+               pos1 = headers + (pos1 - headers_lc->val) + 5;
                if (NULL == (pos2 = strstr(pos1, "\r\n"))) {
                        RPath = estrndup(pos1, strlen(pos1));
                } else {
                        RPath = estrndup(pos1, pos2 - pos1);
                }
        } else {
-               if (headers) {
-                       efree(headers);
-                       efree(headers_lc);
+               if (headers_lc) {
+                       STR_FREE(headers_lc);
                }
                *error = W32_SM_SENDMAIL_FROM_NOT_SET;
                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"));
                return FAILURE;
        } 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();
                if (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 *server_response = NULL;
        char *stripped_header  = NULL;
-       char *data_cln;
-       int data_cln_len;
+       zend_string *data_cln;
 
        /* check for NULL parameters */
        if (data == NULL)
@@ -617,39 +616,41 @@ static int SendText(char *RPath, char *Subject, char *mailTo, char *mailCc, char
                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
         * We use php_str_to_str() and not php_str_replace_in_subject(), since the latter
         * 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,
-                                       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) {
-               data_cln = estrdup("");
-               data_cln_len = 1;               
+               data_cln = STR_EMPTY_ALLOC();
        }
 
        /* send message contents in 1024 chunks */
        {
-               char c, *e2, *e = data_cln + data_cln_len;
-               p = data_cln;
+               char c, *e2, *e = data_cln->val + data_cln->len;
+               p = data_cln->val;
 
                while (e - p > 1024) {
                        e2 = p + 1024;
                        c = *e2;
                        *e2 = '\0';
                        if ((res = Post(p)) != SUCCESS) {
-                               efree(data_cln);
+                               STR_FREE(data_cln);
                                return(res);
                        }
                        *e2 = c;
                        p = e2;
                }
                if ((res = Post(p)) != SUCCESS) {
-                       efree(data_cln);
+                       STR_FREE(data_cln);
                        return(res);
                }
        }
 
-       efree(data_cln);
+       STR_FREE(data_cln);
 
        /*send termination dot */
        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:")) {
                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);
-               efree(dt);
+               snprintf(header_buffer, MAIL_BUFFER_SIZE, "Date: %s\r\n", dt->val);
+               STR_FREE(dt);
        }
 
        if (!headers_lc || !strstr(headers_lc, "from:")) {