From 456c3ee794adb21c16c18c920ccd89fbe22bc05f Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Thu, 25 Feb 2016 16:29:01 +0100 Subject: [PATCH 1/4] Extract sendmail mail transport to its own function --- ext/standard/basic_functions.c | 1 + ext/standard/mail.c | 67 ++++++++++++++++++++++++++++------ ext/standard/php_mail.h | 9 +++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index f8121c1ebfdba..fdb9053c5e97d 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -3628,6 +3628,7 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */ BASIC_ADD_SUBMODULE(dl) BASIC_ADD_SUBMODULE(mail) BASIC_ADD_SUBMODULE(streams) + BASIC_MINIT_SUBMODULE(mail) BASIC_MINIT_SUBMODULE(file) BASIC_MINIT_SUBMODULE(pack) BASIC_MINIT_SUBMODULE(browscap) diff --git a/ext/standard/mail.c b/ext/standard/mail.c index b27e12adc114e..43b42c8d10872 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -269,10 +269,6 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char int tsm_err; char *tsm_errmsg = NULL; #endif - FILE *sendmail; - int ret; - char *sendmail_path = INI_STR("sendmail_path"); - char *sendmail_cmd = NULL; char *mail_log = INI_STR("mail.log"); char *hdr = headers; #if PHP_SIGCHILD @@ -335,6 +331,19 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char MAIL_RET(0); } + if (php_mail_sendmail(to, subject, message, hdr, extra_cmd)) { + MAIL_RET(1); + } + MAIL_RET(0); +} +/* }}} */ + +int php_mail_sendmail(char *to, char *subject, char *message, char *hdr, char *extra_cmd) { + FILE *sendmail; + int ret; + char *sendmail_path = INI_STR("sendmail_path"); + char *sendmail_cmd = NULL; + if (!sendmail_path) { #if (defined PHP_WIN32 || defined NETWARE) /* handle old style win smtp sending */ @@ -345,11 +354,11 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char } else { php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err)); } - MAIL_RET(0); + return 0; } - MAIL_RET(1); + return 1; #else - MAIL_RET(0); + return 0; #endif } if (extra_cmd != NULL) { @@ -393,7 +402,7 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char signal(SIGCHLD, sig_handler); } #endif - MAIL_RET(0); + return 0; } #endif fprintf(sendmail, "To: %s\n", to); @@ -422,9 +431,9 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char #endif #endif { - MAIL_RET(0); + return 0; } else { - MAIL_RET(1); + return 1; } } else { php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path); @@ -433,10 +442,44 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char signal(SIGCHLD, sig_handler); } #endif - MAIL_RET(0); + return 0; } - MAIL_RET(1); /* never reached */ + return 1; /* never reached */ +} + + +/* ********************* + * Transport Modules * + ********************* */ + +#define MAX_MODULES 32 + +static mail_module *mail_modules[MAX_MODULES + 1] = { +}; + +PHPAPI int php_mail_register_module(mail_module *ptr) /* {{{ */ +{ + int ret = FAILURE; + int i; + + for (i = 0; i < MAX_MODULES; i++) { + if (!mail_modules[i]) { + mail_modules[i] = ptr; + ret = SUCCESS; + break; + } + } + return ret; +} +/* }}} */ + +/* {{{ PHP_MINIT_FUNCTION +*/ +PHP_MINIT_FUNCTION(mail) +{ + fprintf( stdout, ">>>> hello world\n" ); + return SUCCESS; } /* }}} */ diff --git a/ext/standard/php_mail.h b/ext/standard/php_mail.h index 514b189681b61..33451e915dcab 100644 --- a/ext/standard/php_mail.h +++ b/ext/standard/php_mail.h @@ -21,10 +21,19 @@ #ifndef PHP_MAIL_H #define PHP_MAIL_H +typedef struct mail_module_struct { + const char *s_name; + int (*s_send)(char *to, char *subject, char *message, char *hdr, char *extra_cmd); + +} mail_module; + PHP_FUNCTION(mail); PHP_MINFO_FUNCTION(mail); +PHP_MINIT_FUNCTION(mail); PHP_FUNCTION(ezmlm_hash); PHPAPI extern int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd); +PHPAPI int php_mail_register_module(mail_module *); + #endif /* PHP_MAIL_H */ From e7b978591b4ddb4565acdd8b6cb4ad0b012a49c1 Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Thu, 25 Feb 2016 19:29:41 +0100 Subject: [PATCH 2/4] Allow to use custom mail transport --- ext/standard/mail.c | 37 +++++++++++++++++++++++++++++++++---- ext/standard/php_mail.h | 6 +++--- main/main.c | 1 + 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ext/standard/mail.c b/ext/standard/mail.c index 43b42c8d10872..0f1f5bed5fae7 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -331,9 +331,15 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char MAIL_RET(0); } - if (php_mail_sendmail(to, subject, message, hdr, extra_cmd)) { - MAIL_RET(1); - } + mail_module *transport = _php_find_mail_module(INI_STR("mail.transport")); + if (!transport) { + php_error_docref(NULL, E_WARNING, "Cannot find mail transport handler '%s'", INI_STR("mail.transport")); + MAIL_RET(0); + } + + if (transport->send(to, subject, message, hdr, extra_cmd)) { + MAIL_RET(1); + } MAIL_RET(0); } /* }}} */ @@ -455,7 +461,13 @@ int php_mail_sendmail(char *to, char *subject, char *message, char *hdr, char *e #define MAX_MODULES 32 +mail_module mail_module_sendmail = { + "sendmail", + php_mail_sendmail +}; + static mail_module *mail_modules[MAX_MODULES + 1] = { + &mail_module_sendmail }; PHPAPI int php_mail_register_module(mail_module *ptr) /* {{{ */ @@ -478,7 +490,6 @@ PHPAPI int php_mail_register_module(mail_module *ptr) /* {{{ */ */ PHP_MINIT_FUNCTION(mail) { - fprintf( stdout, ">>>> hello world\n" ); return SUCCESS; } /* }}} */ @@ -488,6 +499,7 @@ PHP_MINIT_FUNCTION(mail) PHP_MINFO_FUNCTION(mail) { char *sendmail_path = INI_STR("sendmail_path"); + char *transport_name = INI_STR("mail.transport"); #ifdef PHP_WIN32 if (!sendmail_path) { @@ -498,6 +510,23 @@ PHP_MINFO_FUNCTION(mail) #else php_info_print_table_row(2, "Path to sendmail", sendmail_path); #endif + php_info_print_table_row(2, "Mail Transport", transport_name); +} +/* }}} */ + +PHPAPI mail_module *_php_find_mail_module(char *name) /* {{{ */ +{ + mail_module *ret = NULL; + mail_module **mod; + int i; + + for (i = 0, mod = mail_modules; i < MAX_MODULES; i++, mod++) { + if (*mod && !strcasecmp(name, (*mod)->name)) { + ret = *mod; + break; + } + } + return ret; } /* }}} */ diff --git a/ext/standard/php_mail.h b/ext/standard/php_mail.h index 33451e915dcab..4bf7fcaacc784 100644 --- a/ext/standard/php_mail.h +++ b/ext/standard/php_mail.h @@ -22,9 +22,8 @@ #define PHP_MAIL_H typedef struct mail_module_struct { - const char *s_name; - int (*s_send)(char *to, char *subject, char *message, char *hdr, char *extra_cmd); - + const char *name; + int (*send)(char *to, char *subject, char *message, char *hdr, char *extra_cmd); } mail_module; PHP_FUNCTION(mail); @@ -35,5 +34,6 @@ PHP_FUNCTION(ezmlm_hash); PHPAPI extern int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd); PHPAPI int php_mail_register_module(mail_module *); +PHPAPI mail_module *_php_find_mail_module(char *name); #endif /* PHP_MAIL_H */ diff --git a/main/main.c b/main/main.c index ab879369bc71f..734ae941e5af3 100644 --- a/main/main.c +++ b/main/main.c @@ -559,6 +559,7 @@ PHP_INI_BEGIN() PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL) PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("mail.force_extra_parameters",NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnChangeMailForceExtra) + PHP_INI_ENTRY("mail.transport", "sendmail", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("max_file_uploads", "20", PHP_INI_SYSTEM|PHP_INI_PERDIR, NULL) From eba15e0e527eaafcdb1364aa515c5925948aa00a Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Thu, 25 Feb 2016 19:58:50 +0100 Subject: [PATCH 3/4] Fix white space in mail.c --- ext/standard/mail.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ext/standard/mail.c b/ext/standard/mail.c index 0f1f5bed5fae7..3eab0ba8ee0fa 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -52,8 +52,8 @@ #endif #ifdef NETWARE -#define EX_OK 0 /* successful termination */ -#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ +#define EX_OK 0 /* successful termination */ +#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ #endif #define SKIP_LONG_HEADER_SEP(str, pos) \ @@ -331,16 +331,16 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char MAIL_RET(0); } - mail_module *transport = _php_find_mail_module(INI_STR("mail.transport")); - if (!transport) { - php_error_docref(NULL, E_WARNING, "Cannot find mail transport handler '%s'", INI_STR("mail.transport")); - MAIL_RET(0); - } + mail_module *transport = _php_find_mail_module(INI_STR("mail.transport")); + if (!transport) { + php_error_docref(NULL, E_WARNING, "Cannot find mail transport handler '%s'", INI_STR("mail.transport")); + MAIL_RET(0); + } - if (transport->send(to, subject, message, hdr, extra_cmd)) { - MAIL_RET(1); - } - MAIL_RET(0); + if (transport->send(to, subject, message, hdr, extra_cmd)) { + MAIL_RET(1); + } + MAIL_RET(0); } /* }}} */ @@ -462,8 +462,8 @@ int php_mail_sendmail(char *to, char *subject, char *message, char *hdr, char *e #define MAX_MODULES 32 mail_module mail_module_sendmail = { - "sendmail", - php_mail_sendmail + "sendmail", + php_mail_sendmail }; static mail_module *mail_modules[MAX_MODULES + 1] = { @@ -490,7 +490,7 @@ PHPAPI int php_mail_register_module(mail_module *ptr) /* {{{ */ */ PHP_MINIT_FUNCTION(mail) { - return SUCCESS; + return SUCCESS; } /* }}} */ From 8e4e3978bcfabdb8352aa9fed425645796558bf3 Mon Sep 17 00:00:00 2001 From: Dolf Schimmel Date: Thu, 25 Feb 2016 20:04:10 +0100 Subject: [PATCH 4/4] Clean up some mail stuff --- ext/standard/basic_functions.c | 1 - ext/standard/mail.c | 47 ++++++++++++++-------------------- ext/standard/php_mail.h | 1 - 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index fdb9053c5e97d..f8121c1ebfdba 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -3628,7 +3628,6 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */ BASIC_ADD_SUBMODULE(dl) BASIC_ADD_SUBMODULE(mail) BASIC_ADD_SUBMODULE(streams) - BASIC_MINIT_SUBMODULE(mail) BASIC_MINIT_SUBMODULE(file) BASIC_MINIT_SUBMODULE(pack) BASIC_MINIT_SUBMODULE(browscap) diff --git a/ext/standard/mail.c b/ext/standard/mail.c index 3eab0ba8ee0fa..9ad1338b2ac0f 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -454,6 +454,25 @@ int php_mail_sendmail(char *to, char *subject, char *message, char *hdr, char *e return 1; /* never reached */ } +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(mail) +{ + char *sendmail_path = INI_STR("sendmail_path"); + char *transport_name = INI_STR("mail.transport"); + +#ifdef PHP_WIN32 + if (!sendmail_path) { + php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled"); + } else { + php_info_print_table_row(2, "Path to sendmail", sendmail_path); + } +#else + php_info_print_table_row(2, "Path to sendmail", sendmail_path); +#endif + php_info_print_table_row(2, "Mail Transport", transport_name); +} +/* }}} */ /* ********************* * Transport Modules * @@ -486,34 +505,6 @@ PHPAPI int php_mail_register_module(mail_module *ptr) /* {{{ */ } /* }}} */ -/* {{{ PHP_MINIT_FUNCTION -*/ -PHP_MINIT_FUNCTION(mail) -{ - return SUCCESS; -} -/* }}} */ - -/* {{{ PHP_MINFO_FUNCTION - */ -PHP_MINFO_FUNCTION(mail) -{ - char *sendmail_path = INI_STR("sendmail_path"); - char *transport_name = INI_STR("mail.transport"); - -#ifdef PHP_WIN32 - if (!sendmail_path) { - php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled"); - } else { - php_info_print_table_row(2, "Path to sendmail", sendmail_path); - } -#else - php_info_print_table_row(2, "Path to sendmail", sendmail_path); -#endif - php_info_print_table_row(2, "Mail Transport", transport_name); -} -/* }}} */ - PHPAPI mail_module *_php_find_mail_module(char *name) /* {{{ */ { mail_module *ret = NULL; diff --git a/ext/standard/php_mail.h b/ext/standard/php_mail.h index 4bf7fcaacc784..d67b2b37a2c5d 100644 --- a/ext/standard/php_mail.h +++ b/ext/standard/php_mail.h @@ -28,7 +28,6 @@ typedef struct mail_module_struct { PHP_FUNCTION(mail); PHP_MINFO_FUNCTION(mail); -PHP_MINIT_FUNCTION(mail); PHP_FUNCTION(ezmlm_hash); PHPAPI extern int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd);