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 );
@@ -590,16 +591,17 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
590591 }
591592
592593 /* send message header */
594+ bool PostHeaderIsSuccessful = false;
593595 if (Subject == NULL ) {
594- res = PostHeader (RPath , "No Subject" , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
596+ PostHeaderIsSuccessful = PostHeader (RPath , "No Subject" , mailTo , stripped_header );
595597 } else {
596- res = PostHeader (RPath , Subject , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
598+ PostHeaderIsSuccessful = PostHeader (RPath , Subject , mailTo , stripped_header );
597599 }
598600 if (stripped_header ) {
599601 zend_string_release_ex (stripped_header , false);
600602 }
601- if (res != SUCCESS ) {
602- return ( res ) ;
603+ if (! PostHeaderIsSuccessful ) {
604+ return FAILED_TO_SEND ;
603605 }
604606
605607 /* Escape \n. sequences
@@ -646,14 +648,6 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
646648 return (SUCCESS );
647649}
648650
649- static void addToHeader (char * * header_buffer , const char * specifier , const char * string )
650- {
651- size_t header_buffer_size = strlen (* header_buffer );
652- size_t total_size = header_buffer_size + strlen (specifier ) + strlen (string ) + 1 ;
653- * header_buffer = erealloc (* header_buffer , total_size );
654- snprintf (* header_buffer + header_buffer_size , total_size - header_buffer_size , specifier , string );
655- }
656-
657651//*********************************************************************
658652// Name: PostHeader
659653// Input: 1) return path
@@ -665,64 +659,58 @@ static void addToHeader(char **header_buffer, const char *specifier, const char
665659// Author/Date: jcar 20/9/96
666660// History:
667661//*********************************************************************
668- static int PostHeader (char * RPath , const char * Subject , const char * mailTo , char * xheaders )
662+ static bool PostHeader (char * RPath , const char * Subject , const char * mailTo , zend_string * xheaders )
669663{
670664 /* Print message header according to RFC 822 */
671665 /* Return-path, Received, Date, From, Subject, Sender, To, cc */
672666
673- int res ;
674- char * header_buffer ;
675- char * headers_lc = NULL ;
676- size_t i ;
667+ zend_string * headers_lc = NULL ;
668+ smart_str combined_headers = {0 };
677669
678670 if (xheaders ) {
679- size_t headers_lc_len ;
680-
681- headers_lc = estrdup (xheaders );
682- headers_lc_len = strlen (headers_lc );
683-
684- for (i = 0 ; i < headers_lc_len ; i ++ ) {
685- headers_lc [i ] = tolower (headers_lc [i ]);
686- }
671+ headers_lc = zend_string_tolower (xheaders );
687672 }
688673
689- header_buffer = ecalloc (1 , MAIL_BUFFER_SIZE );
690-
691- if (!xheaders || !strstr (headers_lc , "date:" )) {
674+ if (!xheaders || !strstr (ZSTR_VAL (headers_lc ), "date:" )) {
692675 time_t tNow = time (NULL );
693676 zend_string * dt = php_format_date ("r" , 1 , tNow , 1 );
694677
695- snprintf (header_buffer , MAIL_BUFFER_SIZE , "Date: %s\r\n" , ZSTR_VAL (dt ));
678+ smart_str_appends (& combined_headers , "Date: " );
679+ smart_str_append (& combined_headers , dt );
680+ smart_str_appends (& combined_headers , "\r\n" );
696681 zend_string_free (dt );
697682 }
698683
699- if (!headers_lc || !strstr (headers_lc , "from:" )) {
700- addToHeader (& header_buffer , "From: %s\r\n" , RPath );
684+ if (!headers_lc || !strstr (ZSTR_VAL (headers_lc ), "from:" )) {
685+ smart_str_appends (& combined_headers , "From: " );
686+ smart_str_appends (& combined_headers , RPath );
687+ smart_str_appends (& combined_headers , "\r\n" );
701688 }
702- addToHeader (& header_buffer , "Subject: %s\r\n" , Subject );
689+ smart_str_appends (& combined_headers , "Subject: " );
690+ smart_str_appends (& combined_headers , Subject );
691+ smart_str_appends (& combined_headers , "\r\n" );
703692
704693 /* Only add the To: field from the $to parameter if isn't in the custom headers */
705- if ((headers_lc && (!strstr (headers_lc , "\r\nto:" ) && (strncmp (headers_lc , "to:" , 3 ) != 0 ))) || !headers_lc ) {
706- addToHeader (& header_buffer , "To: %s\r\n" , mailTo );
694+ if (!headers_lc || (!strstr (ZSTR_VAL (headers_lc ), "\r\nto:" ) && (strncmp (ZSTR_VAL (headers_lc ), "to:" , 3 ) != 0 ))) {
695+ smart_str_appends (& combined_headers , "To: " );
696+ smart_str_appends (& combined_headers , mailTo );
697+ smart_str_appends (& combined_headers , "\r\n" );
707698 }
708699 if (xheaders ) {
709- addToHeader (& header_buffer , "%s\r\n" , xheaders );
700+ smart_str_append (& combined_headers , xheaders );
701+ smart_str_appends (& combined_headers , "\r\n" );
710702 }
703+ /* End of headers */
704+ smart_str_appends (& combined_headers , "\r\n" );
705+ zend_string * combined_headers_str = smart_str_extract (& combined_headers );
711706
712707 if (headers_lc ) {
713- efree (headers_lc );
714- }
715- if (!Post (header_buffer )) {
716- efree (header_buffer );
717- return (FAILED_TO_SEND );
708+ zend_string_release_ex (headers_lc , false);
718709 }
719- efree (header_buffer );
720710
721- if (!Post ("\r\n" )) {
722- return (FAILED_TO_SEND );
723- }
724-
725- return (SUCCESS );
711+ bool header_post_status = Post (ZSTR_VAL (combined_headers_str ));
712+ zend_string_efree (combined_headers_str );
713+ return header_post_status ;
726714}
727715
728716
0 commit comments