]> granicus.if.org Git - php/commitdiff
MFB: Added mail logging functionality that allows logging of mail sent via
authorIlia Alshanetsky <iliaa@php.net>
Fri, 9 Jan 2009 15:00:36 +0000 (15:00 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Fri, 9 Jan 2009 15:00:36 +0000 (15:00 +0000)
mail() function

ext/standard/mail.c
main/main.c
main/php_globals.h
php.ini-dist
php.ini-recommended

index 9793fe9476aea7269b59a82cf8e9454896c24fd9..472f1f0cb680ebd41b0115eada487789afc9fc7d 100644 (file)
@@ -23,6 +23,8 @@
 #include <stdio.h>
 #include "php.h"
 #include "ext/standard/info.h"
+#include "ext/standard/php_string.h"
+#include "ext/standard/basic_functions.h"
 
 #if HAVE_SYSEXITS_H
 #include <sysexits.h>
@@ -66,6 +68,8 @@
                *p = ' ';                                                               \
        }                                                                                       \
 
+extern long php_getuid(void);
+
 /* {{{ proto int ezmlm_hash(string addr) U
    Calculate EZMLM list hash value. */
 PHP_FUNCTION(ezmlm_hash)
@@ -193,25 +197,61 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
        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
        void (*sig_handler)() = NULL;
 #endif
 
+#define MAIL_RET(val) \
+       if (hdr != headers) {   \
+               efree(hdr);     \
+       }       \
+       return val;     \
+
+       if (mail_log) {
+               char *tmp;
+               int l = spprintf(&tmp, 0, "mail() on [%s:%d]: To: %s -- Headers: %s\n", zend_get_executed_filename(TSRMLS_C), zend_get_executed_lineno(TSRMLS_C), to, hdr ? hdr : "");
+               if (hdr) { /* find all \r\n instances and replace them with spaces, so a log line is always one line long */ 
+                       char *p = tmp;
+                       while ((p = strpbrk(p, "\r\n"))) {
+                               *p = ' ';
+                       }
+                       tmp[l - 1] = '\n';
+               }
+               _php_error_log(3, tmp, mail_log, NULL TSRMLS_CC);
+               efree(tmp);
+       }
+       if (PG(mail_x_header)) {
+               char *tmp = zend_get_executed_filename(TSRMLS_C);
+               char *f;
+               size_t f_len;
+
+               php_basename(tmp, strlen(tmp), NULL, 0,&f, &f_len);
+
+               if (headers != NULL) {
+                       spprintf(&hdr, 0, "%s\r\nX-PHP-Originating-Script: %ld:%s\n", headers, php_getuid(), f);
+               } else {
+                       spprintf(&hdr, 0, "X-PHP-Originating-Script: %ld:%s\n", php_getuid(), f);
+               }
+               efree(f);
+       }
+
        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 TSRMLS_CC) == FAILURE) {
+               if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == 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;
+                       MAIL_RET(0);
                }
-               return 1;
+               MAIL_RET(1);
 #else
-               return 0;
+               MAIL_RET(0);
 #endif
        }
        if (extra_cmd != NULL) {
@@ -255,13 +295,13 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
                                signal(SIGCHLD, sig_handler);
                        }
 #endif
-                       return 0;
+                       MAIL_RET(0);
                }
 #endif
                fprintf(sendmail, "To: %s\n", to);
                fprintf(sendmail, "Subject: %s\n", subject);
-               if (headers != NULL) {
-                       fprintf(sendmail, "%s\n", headers);
+               if (hdr != NULL) {
+                       fprintf(sendmail, "%s\n", hdr);
                }
                fprintf(sendmail, "\n%s\n", message);
                ret = pclose(sendmail);
@@ -284,9 +324,9 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
 #endif
 #endif
                {
-                       return 0;
+                       MAIL_RET(0);
                } else {
-                       return 1;
+                       MAIL_RET(1);
                }
        } else {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
@@ -295,10 +335,10 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
                        signal(SIGCHLD, sig_handler);                                           
                }
 #endif
-               return 0;
+               MAIL_RET(0);
        }
 
-       return 1; /* never reached */
+       MAIL_RET(1); /* never reached */
 }
 /* }}} */
 
index e78d483873a7cb408390e44a403dd28d2175cf61..97750a29ad048e069981856ca1040709ede5c75a 100644 (file)
@@ -539,6 +539,8 @@ PHP_INI_BEGIN()
 
        PHP_INI_ENTRY("SMTP",                                           "localhost",PHP_INI_ALL,                NULL)
        PHP_INI_ENTRY("smtp_port",                                      "25",           PHP_INI_ALL,            NULL)
+       STD_PHP_INI_BOOLEAN("mail.add_x_header",                        "0",            PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateBool,                   mail_x_header,                  php_core_globals,       core_globals)
+       STD_PHP_INI_ENTRY("mail.log",                                   NULL,           PHP_INI_SYSTEM|PHP_INI_PERDIR,          OnUpdateString,                 mail_log,                       php_core_globals,       core_globals)
        PHP_INI_ENTRY("browscap",                                       NULL,           PHP_INI_SYSTEM,         NULL)
        PHP_INI_ENTRY("memory_limit",                           "128M",         PHP_INI_ALL,            OnChangeMemoryLimit)
        PHP_INI_ENTRY("precision",                                      "14",           PHP_INI_ALL,            OnSetPrecision)
index 3fbecbb3b07f25695f9046b0e056dd0182768b80..4d918bc0a256696659a0680a13eddec88112f002 100644 (file)
@@ -151,6 +151,9 @@ struct _php_core_globals {
        long user_ini_cache_ttl;
 
        char *request_order;
+
+       zend_bool mail_x_header;
+       char *mail_log;
 };
 
 
index f44014dc96aff9cd20353f383f798d865a70d01c..dcc89147a079a378a4151bd8e8962d82ec2103c4 100644 (file)
@@ -669,6 +669,12 @@ smtp_port = 25
 ; the 5th parameter to mail(), even in safe mode.
 ;mail.force_extra_parameters =
 
+; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename
+mail.add_x_header = Off
+
+; Log all mail() calls including the full path of the script, line #, to address and headers
+mail.log =
+
 [SQL]
 sql.safe_mode = Off
 
index 313273af0efdd0b4179cb6be9607d13e0cd59dcd..646b0779b9dbe9ab49894d70282a25090b28e1fc 100644 (file)
@@ -706,6 +706,12 @@ smtp_port = 25
 ; the 5th parameter to mail(), even in safe mode.
 ;mail.force_extra_parameters =
 
+; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename
+mail.add_x_header = On
+
+; Log all mail() calls including the full path of the script, line #, to address and headers
+mail.log =
+
 [SQL]
 sql.safe_mode = Off