com php-src: add sendmail_log: ext/standard/basic_funct ions.c ext/standard/mail.c main/main.c

From: Date: Thu, 12 Dec 2013 08:47:41 +0000
Subject: com php-src: add sendmail_log: ext/standard/basic_funct ions.c ext/standard/mail.c main/main.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-76645@lists.php.net to get a copy of this message
Commit: 65e20623b27e2b995188fa49bdf83cbc5eb44428 Author: Antony Dovgal <tony@daylessday.org> Sat, 31 Dec 2011 00:58:55 +0400 Committer: Antony Dovgal <tony2001@php.net> Thu, 12 Dec 2013 12:47:41 +0400 Parents: e379a4fb319d1a4c281bb8cb8fa54f88f22ddafa Branches: badoo-5..5_mysqlnd Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=65e20623b27e2b995188fa49bdf83cbc5eb44428 Log: add sendmail_log Changed paths: M ext/standard/basic_functions.c M ext/standard/mail.c M main/main.c

diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 96b3c30..3dac962 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1624,6 +1624,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_mail, 0, 0, 3) ZEND_ARG_INFO(0, message) ZEND_ARG_INFO(0, additional_headers) ZEND_ARG_INFO(0, additional_parameters) + ZEND_ARG_INFO(1, ssmtp_stdout) + ZEND_ARG_INFO(1, ssmtp_stderr) ZEND_END_ARG_INFO() /* }}} */ /* {{{ math.c */ diff --git a/ext/standard/mail.c b/ext/standard/mail.c index 9499981..b73f08f 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -18,6 +18,7 @@ /* $Id$ */ +#define _GNU_SOURCE /* for dprintf() */ #include <stdlib.h> #include <ctype.h> #include <stdio.h> @@ -51,6 +52,8 @@ #include "win32/sendmail.h" #endif +#include <sys/wait.h> + #ifdef NETWARE #define EX_OK 0 /* successful termination */ #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ @@ -103,12 +106,13 @@ PHP_FUNCTION(mail) char *to=NULL, *message=NULL, *headers=NULL, *headers_trimmed=NULL; char *subject=NULL, *extra_cmd=NULL; int to_len, message_len, headers_len = 0; - int subject_len, extra_cmd_len = 0, i; + int subject_len, extra_cmd_len = 0, i, res; char *force_extra_parameters = INI_STR("mail.force_extra_parameters"); char *to_r, *subject_r; - char *p, *e; + char *p, *e, *ssmtp_stdout, *ssmtp_stderr; + zval *s_stdout = NULL, *s_stderr = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss", &to, &to_len, &subject, &subject_len, &message, &message_len, &headers, &headers_len, &extra_cmd, &extra_cmd_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|sszz", &to, &to_len, &subject, &subject_len, &message, &message_len, &headers, &headers_len, &extra_cmd, &extra_cmd_len, &s_stdout, &s_stderr) == FAILURE) { return; } @@ -170,7 +174,32 @@ PHP_FUNCTION(mail) extra_cmd = php_escape_shell_cmd(extra_cmd); } - if (php_mail(to_r, subject_r, message, headers_trimmed, extra_cmd TSRMLS_CC)) { + ssmtp_stdout = NULL; + ssmtp_stderr = NULL; + if (s_stdout) { + zval_dtor(s_stdout); + ZVAL_STRING(s_stdout, "", 1); + } + if (s_stderr) { + zval_dtor(s_stderr); + ZVAL_STRING(s_stderr, "", 1); + } + + res = php_mail_ex(to_r, subject_r, message, headers_trimmed, extra_cmd, &ssmtp_stdout, &ssmtp_stderr TSRMLS_CC); + if (s_stdout && ssmtp_stdout) { + zval_dtor(s_stdout); + ZVAL_STRING(s_stdout, ssmtp_stdout, 0); + } else if (ssmtp_stdout) { + efree(ssmtp_stdout); + } + + if (s_stderr && ssmtp_stderr) { + zval_dtor(s_stderr); + ZVAL_STRING(s_stderr, ssmtp_stderr, 0); + } else if (ssmtp_stderr) { + efree(ssmtp_stderr); + } + if (res) { RETVAL_TRUE; } else { RETVAL_FALSE; @@ -192,7 +221,6 @@ PHP_FUNCTION(mail) } /* }}} */ - void php_mail_log_crlf_to_spaces(char *message) { /* Find all instances of carriage returns or line feeds and * replace them with spaces. Thus, a log line is always one line @@ -222,6 +250,238 @@ void php_mail_log_to_file(char *filename, char *message, size_t message_size TSR } } +#define FREE_PPTR(pptr) {int i = 0; if (pptr) while(pptr[i]) free(pptr[i++]); free(pptr); pptr = NULL;} + +/* {{{ php_mail_hack + */ +int mail_hack( char *sendmail_path, char *sendmail_log, char *to, char *subject, char *message, char *headers, char **ssmtp_stdout, char **ssmtp_stderr ) +{ + int ret, sock0[2], sock1[2], sock2[2], i = 0, w; + pid_t pid; + char buff[MAXPATHLEN], str[64], *tmp, *start, *end; + char **larg = NULL; + int status = 0; + + strcpy(buff, sendmail_path); + start = buff; end = start + strlen(start); + + /* skip leading spaces */ + w = strspn(start, " "); + start += w; + tmp = start; + + while( start < end ) { + w = strcspn(start, " "); /* skip all non-spaces */ + if (w > 0) { + tmp += w; /* put 0 in place of the first space found */ + *tmp = 0; + tmp++; + } else { + /* no spaces found */ + tmp = end; + } + + larg = (char **)realloc(larg, (i+1)*sizeof(char *)); + larg[i] = strdup(start); i++; + + if (tmp < end) { + w = strspn(tmp, " "); /* skip all additional spaces (if any) */ + } + start = tmp + w; + } + + larg = (char **)realloc(larg, (i+1)*sizeof(char *)); + larg[i] = NULL; + + if ( !larg || !larg[0] ) { + FREE_PPTR(larg); + return 0; + } + + if ( socketpair(AF_LOCAL, SOCK_STREAM, 0, sock0) ) { + FREE_PPTR(larg); + return 0; + } + + if ( socketpair(AF_LOCAL, SOCK_STREAM, 0, sock1) ) { + close(sock0[0]); close(sock0[1]); + FREE_PPTR(larg); + return 0; + } + + if ( socketpair(AF_LOCAL, SOCK_STREAM, 0, sock2) ) { + close(sock0[0]); close(sock0[1]); + close(sock1[0]); close(sock1[1]); + FREE_PPTR(larg); + return 0; + } + + switch( (pid = fork()) ) { + case -1: + close(sock0[0]); close(sock0[1]); + close(sock1[0]); close(sock1[1]); + close(sock2[0]); close(sock2[1]); + FREE_PPTR(larg); + return 0; + case 0: + close(0); dup2( sock0[0], 0 ); close( sock0[0] ); close( sock0[1] ); + close(1); dup2( sock2[1], 1 ); close( sock2[0] ); close( sock2[1] ); + close(2); dup2( sock1[1], 2 ); close( sock1[0] ); close( sock1[1] ); + + execve( larg[0], larg, NULL ); + FREE_PPTR(larg); + exit(-1); + default: + close( sock0[0] ); close( sock1[1] ); close( sock2[1] ); + + FREE_PPTR(larg); + + dprintf( sock0[1], "To: %s\n", to); + if ( headers ) + dprintf( sock0[1], "%s\n", headers); + dprintf( sock0[1], "Subject: %s\n", subject); + dprintf( sock0[1], "\n%s\n", message); + + shutdown(sock0[1], SHUT_WR); + close( sock0[1] ); + + if ( (ret = waitpid(pid, &status, 0)) != (int)pid) { + close(sock1[0]); + close(sock2[0]); + return 0; + } + + if (WIFEXITED(status)) { + status = WEXITSTATUS(status); + } + + if (ssmtp_stdout) { + ret = read( sock2[0], buff, sizeof(buff) - 1 ); + if (ret > 0) { + buff[ret] = '\0'; + *ssmtp_stdout = estrdup(buff); + } + } + + ret = 0; + if (ssmtp_stderr || (sendmail_log && sendmail_log[0])) { + ret = read( sock1[0], buff, sizeof(buff) - 1 ); + if (ret > 0) { + buff[ret] = '\0'; + } + } + + if (ssmtp_stderr && ret > 0) { + *ssmtp_stderr = estrdup(buff); + } + + if ( sendmail_log && sendmail_log[0] && ret > 0) { + FILE *log = fopen( sendmail_log, "a+" ); + + if ( !log ) { + close(sock1[0]); + close(sock2[0]); + return 0; + } + + buff[ret] = 0; + { + char *tmp; + struct tm tmbuf; + char error_time_str[128]; + time_t error_time; + static const char *months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + int rr; + + time(&error_time); + php_localtime_r(&error_time, &tmbuf); + strftime(error_time_str, sizeof(error_time_str), "%Y %H:%M:%S", &tmbuf); + + rr = strcspn(buff, "\n"); + buff[rr] = 0; + + spprintf(&tmp, 0, "[%02d-%s-%s] To:%s %s%s", tmbuf.tm_mday, months[tmbuf.tm_mon], error_time_str, to, buff, PHP_EOL); + fprintf(log, "%s", tmp); + efree(tmp); + } + + fclose( log ); + } + + close(sock1[0]); + close(sock2[0]); + + if (status != 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to run sendmail program, check its path and arguments (status: %d)", status); + return 0; + } + } + + return 1; +} +/* }}} */ + +#if 1 +/* {{{ php_mail + */ + +PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC) +{ + return php_mail_ex(to, subject, message, headers, extra_cmd, NULL, NULL TSRMLS_CC); +} + + +PHPAPI int php_mail_ex(char *to, char *subject, char *message, char *headers, char *extra_cmd, char **ssmtp_stdout, char **ssmtp_stderr TSRMLS_DC) +{ +#if (defined PHP_WIN32 || defined NETWARE) + int tsm_err; + char *tsm_errmsg = NULL; + FILE *sendmail; +#endif + int ret; + char *sendmail_path = INI_STR("sendmail_path"); + char *sendmail_log = INI_STR("sendmail_log"); + char *sendmail_cmd = NULL; + + if (!sendmail_path) { +#if (defined PHP_WIN32 || defined NETWARE) + /* handle old style win smtp sending */ + if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, headers, subject, to, message, NULL, NULL, NULL) == FAILURE) { + if (tsm_errmsg) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg); + efree(tsm_errmsg); + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err)); + } + return 0; + } + return 1; +#else + return 0; +#endif + } + if (extra_cmd != NULL) { + sendmail_cmd = emalloc (strlen (sendmail_path) + strlen (extra_cmd) + 2); + strcpy (sendmail_cmd, sendmail_path); + strcat (sendmail_cmd, " "); + strcat (sendmail_cmd, extra_cmd); + } else { + sendmail_cmd = sendmail_path; + } + +#ifdef PHP_WIN32 + sendmail = popen(sendmail_cmd, "wb"); +#else + ret = mail_hack( sendmail_cmd, sendmail_log, to, subject, message, headers, ssmtp_stdout, ssmtp_stderr ); + if (extra_cmd != NULL) { + efree (sendmail_cmd); + } +#endif + return ret; +} +/* }}} */ + +#else /* {{{ php_mail */ @@ -395,6 +655,7 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char MAIL_RET(1); /* never reached */ } /* }}} */ +#endif /* {{{ PHP_MINFO_FUNCTION */ diff --git a/main/main.c b/main/main.c index adb44de..4d78ac4 100644 --- a/main/main.c +++ b/main/main.c @@ -555,6 +555,7 @@ PHP_INI_BEGIN() PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) 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("sendmail_log", NULL, PHP_INI_ALL, NULL) PHP_INI_ENTRY("mail.force_extra_parameters",NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnChangeMailForceExtra) PHP_INI_ENTRY("disable_functions", "", PHP_INI_SYSTEM, NULL) PHP_INI_ENTRY("disable_classes", "", PHP_INI_SYSTEM, NULL)
« previous php.cvs (#76645) next »