3232
3333#include "php_win32_globals.h"
3434
35+ #include "Zend/zend_smart_str.h"
3536#include "ext/pcre/php_pcre.h"
3637#include "ext/standard/php_string.h"
3738#include "ext/date/php_date.h"
@@ -114,7 +115,7 @@ static const char *ErrorMessages[] =
114115static int SendText (char * RPath , const char * Subject , const char * mailTo , const char * data ,
115116 zend_string * headers , zend_string * headers_lc , char * * error_message );
116117static int MailConnect ();
117- static int PostHeader (char * RPath , const char * Subject , const char * mailTo , char * xheaders );
118+ static bool PostHeader (char * RPath , const char * Subject , const char * mailTo , zend_string * xheaders );
118119static bool Post (LPCSTR msg );
119120static int Ack (char * * server_response );
120121static unsigned long GetAddr (LPSTR szHost );
@@ -589,16 +590,17 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
589590 }
590591
591592 /* send message header */
593+ bool PostHeaderIsSuccessful = false;
592594 if (Subject == NULL ) {
593- res = PostHeader (RPath , "No Subject" , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
595+ PostHeaderIsSuccessful = PostHeader (RPath , "No Subject" , mailTo , stripped_header );
594596 } else {
595- res = PostHeader (RPath , Subject , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
597+ PostHeaderIsSuccessful = PostHeader (RPath , Subject , mailTo , stripped_header );
596598 }
597599 if (stripped_header ) {
598600 zend_string_release_ex (stripped_header , false);
599601 }
600- if (res != SUCCESS ) {
601- return ( res ) ;
602+ if (! PostHeaderIsSuccessful ) {
603+ return FAILED_TO_SEND ;
602604 }
603605
604606 /* Escape \n. sequences
@@ -645,14 +647,6 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
645647 return (SUCCESS );
646648}
647649
648- static void addToHeader (char * * header_buffer , const char * specifier , const char * string )
649- {
650- size_t header_buffer_size = strlen (* header_buffer );
651- size_t total_size = header_buffer_size + strlen (specifier ) + strlen (string ) + 1 ;
652- * header_buffer = erealloc (* header_buffer , total_size );
653- snprintf (* header_buffer + header_buffer_size , total_size - header_buffer_size , specifier , string );
654- }
655-
656650//*********************************************************************
657651// Name: PostHeader
658652// Input: 1) return path
@@ -664,64 +658,58 @@ static void addToHeader(char **header_buffer, const char *specifier, const char
664658// Author/Date: jcar 20/9/96
665659// History:
666660//*********************************************************************
667- static int PostHeader (char * RPath , const char * Subject , const char * mailTo , char * xheaders )
661+ static bool PostHeader (char * RPath , const char * Subject , const char * mailTo , zend_string * xheaders )
668662{
669663 /* Print message header according to RFC 822 */
670664 /* Return-path, Received, Date, From, Subject, Sender, To, cc */
671665
672- int res ;
673- char * header_buffer ;
674- char * headers_lc = NULL ;
675- size_t i ;
666+ zend_string * headers_lc = NULL ;
667+ smart_str combined_headers = {0 };
676668
677669 if (xheaders ) {
678- size_t headers_lc_len ;
679-
680- headers_lc = estrdup (xheaders );
681- headers_lc_len = strlen (headers_lc );
682-
683- for (i = 0 ; i < headers_lc_len ; i ++ ) {
684- headers_lc [i ] = tolower (headers_lc [i ]);
685- }
670+ headers_lc = zend_string_tolower (xheaders );
686671 }
687672
688- header_buffer = ecalloc (1 , MAIL_BUFFER_SIZE );
689-
690- if (!xheaders || !strstr (headers_lc , "date:" )) {
673+ if (!xheaders || !strstr (ZSTR_VAL (headers_lc ), "date:" )) {
691674 time_t tNow = time (NULL );
692675 zend_string * dt = php_format_date ("r" , 1 , tNow , 1 );
693676
694- snprintf (header_buffer , MAIL_BUFFER_SIZE , "Date: %s\r\n" , ZSTR_VAL (dt ));
677+ smart_str_appends (& combined_headers , "Date: " );
678+ smart_str_append (& combined_headers , dt );
679+ smart_str_appends (& combined_headers , "\r\n" );
695680 zend_string_free (dt );
696681 }
697682
698- if (!headers_lc || !strstr (headers_lc , "from:" )) {
699- addToHeader (& header_buffer , "From: %s\r\n" , RPath );
683+ if (!headers_lc || !strstr (ZSTR_VAL (headers_lc ), "from:" )) {
684+ smart_str_appends (& combined_headers , "From: " );
685+ smart_str_appends (& combined_headers , RPath );
686+ smart_str_appends (& combined_headers , "\r\n" );
700687 }
701- addToHeader (& header_buffer , "Subject: %s\r\n" , Subject );
688+ smart_str_appends (& combined_headers , "Subject: " );
689+ smart_str_appends (& combined_headers , Subject );
690+ smart_str_appends (& combined_headers , "\r\n" );
702691
703692 /* Only add the To: field from the $to parameter if isn't in the custom headers */
704- if ((headers_lc && (!strstr (headers_lc , "\r\nto:" ) && (strncmp (headers_lc , "to:" , 3 ) != 0 ))) || !headers_lc ) {
705- addToHeader (& header_buffer , "To: %s\r\n" , mailTo );
693+ if (!headers_lc || (!strstr (ZSTR_VAL (headers_lc ), "\r\nto:" ) && (strncmp (ZSTR_VAL (headers_lc ), "to:" , 3 ) != 0 ))) {
694+ smart_str_appends (& combined_headers , "To: " );
695+ smart_str_appends (& combined_headers , mailTo );
696+ smart_str_appends (& combined_headers , "\r\n" );
706697 }
707698 if (xheaders ) {
708- addToHeader (& header_buffer , "%s\r\n" , xheaders );
699+ smart_str_append (& combined_headers , xheaders );
700+ smart_str_appends (& combined_headers , "\r\n" );
709701 }
702+ /* End of headers */
703+ smart_str_appends (& combined_headers , "\r\n" );
704+ zend_string * combined_headers_str = smart_str_extract (& combined_headers );
710705
711706 if (headers_lc ) {
712- efree (headers_lc );
713- }
714- if (!Post (header_buffer )) {
715- efree (header_buffer );
716- return (FAILED_TO_SEND );
707+ zend_string_release_ex (headers_lc , false);
717708 }
718- efree (header_buffer );
719709
720- if (!Post ("\r\n" )) {
721- return (FAILED_TO_SEND );
722- }
723-
724- return (SUCCESS );
710+ bool header_post_status = Post (ZSTR_VAL (combined_headers_str ));
711+ zend_string_efree (combined_headers_str );
712+ return header_post_status ;
725713}
726714
727715
0 commit comments