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 );
@@ -588,16 +589,17 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
588589 }
589590
590591 /* send message header */
592+ bool PostHeaderIsSuccessful = false;
591593 if (Subject == NULL ) {
592- res = PostHeader (RPath , "No Subject" , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
594+ PostHeaderIsSuccessful = PostHeader (RPath , "No Subject" , mailTo , stripped_header );
593595 } else {
594- res = PostHeader (RPath , Subject , mailTo , stripped_header ? ZSTR_VAL ( stripped_header ) : NULL );
596+ PostHeaderIsSuccessful = PostHeader (RPath , Subject , mailTo , stripped_header );
595597 }
596598 if (stripped_header ) {
597599 zend_string_release_ex (stripped_header , false);
598600 }
599- if (res != SUCCESS ) {
600- return ( res ) ;
601+ if (! PostHeaderIsSuccessful ) {
602+ return FAILED_TO_SEND ;
601603 }
602604
603605 /* Escape \n. sequences
@@ -644,14 +646,6 @@ static int SendText(char *RPath, const char *Subject, const char *mailTo, const
644646 return (SUCCESS );
645647}
646648
647- static void addToHeader (char * * header_buffer , const char * specifier , const char * string )
648- {
649- size_t header_buffer_size = strlen (* header_buffer );
650- size_t total_size = header_buffer_size + strlen (specifier ) + strlen (string ) + 1 ;
651- * header_buffer = erealloc (* header_buffer , total_size );
652- snprintf (* header_buffer + header_buffer_size , total_size - header_buffer_size , specifier , string );
653- }
654-
655649//*********************************************************************
656650// Name: PostHeader
657651// Input: 1) return path
@@ -663,64 +657,58 @@ static void addToHeader(char **header_buffer, const char *specifier, const char
663657// Author/Date: jcar 20/9/96
664658// History:
665659//*********************************************************************
666- static int PostHeader (char * RPath , const char * Subject , const char * mailTo , char * xheaders )
660+ static bool PostHeader (char * RPath , const char * Subject , const char * mailTo , zend_string * xheaders )
667661{
668662 /* Print message header according to RFC 822 */
669663 /* Return-path, Received, Date, From, Subject, Sender, To, cc */
670664
671- int res ;
672- char * header_buffer ;
673- char * headers_lc = NULL ;
674- size_t i ;
665+ zend_string * headers_lc = NULL ;
666+ smart_str combined_headers = {0 };
675667
676668 if (xheaders ) {
677- size_t headers_lc_len ;
678-
679- headers_lc = estrdup (xheaders );
680- headers_lc_len = strlen (headers_lc );
681-
682- for (i = 0 ; i < headers_lc_len ; i ++ ) {
683- headers_lc [i ] = tolower (headers_lc [i ]);
684- }
669+ headers_lc = zend_string_tolower (xheaders );
685670 }
686671
687- header_buffer = ecalloc (1 , MAIL_BUFFER_SIZE );
688-
689- if (!xheaders || !strstr (headers_lc , "date:" )) {
672+ if (!xheaders || !strstr (ZSTR_VAL (headers_lc ), "date:" )) {
690673 time_t tNow = time (NULL );
691674 zend_string * dt = php_format_date ("r" , 1 , tNow , 1 );
692675
693- snprintf (header_buffer , MAIL_BUFFER_SIZE , "Date: %s\r\n" , ZSTR_VAL (dt ));
676+ smart_str_appends (& combined_headers , "Date: " );
677+ smart_str_append (& combined_headers , dt );
678+ smart_str_appends (& combined_headers , "\r\n" );
694679 zend_string_free (dt );
695680 }
696681
697- if (!headers_lc || !strstr (headers_lc , "from:" )) {
698- addToHeader (& header_buffer , "From: %s\r\n" , RPath );
682+ if (!headers_lc || !strstr (ZSTR_VAL (headers_lc ), "from:" )) {
683+ smart_str_appends (& combined_headers , "From: " );
684+ smart_str_appends (& combined_headers , RPath );
685+ smart_str_appends (& combined_headers , "\r\n" );
699686 }
700- addToHeader (& header_buffer , "Subject: %s\r\n" , Subject );
687+ smart_str_appends (& combined_headers , "Subject: " );
688+ smart_str_appends (& combined_headers , Subject );
689+ smart_str_appends (& combined_headers , "\r\n" );
701690
702691 /* Only add the To: field from the $to parameter if isn't in the custom headers */
703- if ((headers_lc && (!strstr (headers_lc , "\r\nto:" ) && (strncmp (headers_lc , "to:" , 3 ) != 0 ))) || !headers_lc ) {
704- addToHeader (& header_buffer , "To: %s\r\n" , mailTo );
692+ if (!headers_lc || (!strstr (ZSTR_VAL (headers_lc ), "\r\nto:" ) && (strncmp (ZSTR_VAL (headers_lc ), "to:" , 3 ) != 0 ))) {
693+ smart_str_appends (& combined_headers , "To: " );
694+ smart_str_appends (& combined_headers , mailTo );
695+ smart_str_appends (& combined_headers , "\r\n" );
705696 }
706697 if (xheaders ) {
707- addToHeader (& header_buffer , "%s\r\n" , xheaders );
698+ smart_str_append (& combined_headers , xheaders );
699+ smart_str_appends (& combined_headers , "\r\n" );
708700 }
701+ /* End of headers */
702+ smart_str_appends (& combined_headers , "\r\n" );
703+ zend_string * combined_headers_str = smart_str_extract (& combined_headers );
709704
710705 if (headers_lc ) {
711- efree (headers_lc );
712- }
713- if (!Post (header_buffer )) {
714- efree (header_buffer );
715- return (FAILED_TO_SEND );
706+ zend_string_release_ex (headers_lc , false);
716707 }
717- efree (header_buffer );
718708
719- if (!Post ("\r\n" )) {
720- return (FAILED_TO_SEND );
721- }
722-
723- return (SUCCESS );
709+ bool header_post_status = Post (ZSTR_VAL (combined_headers_str ));
710+ zend_string_release_ex (combined_headers_str , false);
711+ return header_post_status ;
724712}
725713
726714
0 commit comments