|
39 | 39 | #endif
|
40 | 40 | #endif
|
41 | 41 |
|
| 42 | +#include "php_syslog.h" |
42 | 43 | #include "php_mail.h"
|
43 | 44 | #include "php_ini.h"
|
44 | 45 | #include "php_string.h"
|
@@ -189,6 +190,37 @@ PHP_FUNCTION(mail)
|
189 | 190 | }
|
190 | 191 | /* }}} */
|
191 | 192 |
|
| 193 | + |
| 194 | +void php_mail_log_crlf_to_spaces(char *message) { |
| 195 | + /* Find all instances of carriage returns or line feeds and |
| 196 | + * replace them with spaces. Thus, a log line is always one line |
| 197 | + * long |
| 198 | + */ |
| 199 | + char *p = message; |
| 200 | + while ((p = strpbrk(p, "\r\n"))) { |
| 201 | + *p = ' '; |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +void php_mail_log_to_syslog(char *message) { |
| 206 | + /* Write 'message' to syslog. */ |
| 207 | +#ifdef HAVE_SYSLOG_H |
| 208 | + php_syslog(LOG_NOTICE, "%s", message); |
| 209 | +#endif |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +void php_mail_log_to_file(char *filename, char *message, size_t message_size) { |
| 214 | + /* Write 'message' to the given file. */ |
| 215 | + uint flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR; |
| 216 | + php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL); |
| 217 | + if (stream) { |
| 218 | + php_stream_write(stream, message, message_size); |
| 219 | + php_stream_close(stream); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | + |
192 | 224 | /* {{{ php_mail
|
193 | 225 | */
|
194 | 226 | PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC)
|
@@ -216,19 +248,22 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char
|
216 | 248 | if (mail_log && *mail_log) {
|
217 | 249 | char *tmp;
|
218 | 250 | 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 : "");
|
219 |
| - php_stream *stream = php_stream_open_wrapper(mail_log, "a", IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR, NULL); |
220 | 251 |
|
221 |
| - if (hdr) { /* find all \r\n instances and replace them with spaces, so a log line is always one line long */ |
222 |
| - char *p = tmp; |
223 |
| - while ((p = strpbrk(p, "\r\n"))) { |
224 |
| - *p = ' '; |
225 |
| - } |
226 |
| - tmp[l - 1] = '\n'; |
| 252 | + if (hdr) { |
| 253 | + php_mail_log_crlf_to_spaces(tmp); |
227 | 254 | }
|
228 |
| - if (stream) { |
229 |
| - php_stream_write(stream, tmp, l); |
230 |
| - php_stream_close(stream); |
| 255 | + |
| 256 | + if (!strcmp(mail_log, "syslog")) { |
| 257 | + /* Drop the final space when logging to syslog. */ |
| 258 | + tmp[l - 1] = 0; |
| 259 | + php_mail_log_to_syslog(tmp); |
| 260 | + } |
| 261 | + else { |
| 262 | + /* Convert the final space to a newline when logging to file. */ |
| 263 | + tmp[l - 1] = '\n'; |
| 264 | + php_mail_log_to_file(mail_log, tmp, l); |
231 | 265 | }
|
| 266 | + |
232 | 267 | efree(tmp);
|
233 | 268 | }
|
234 | 269 | if (PG(mail_x_header)) {
|
|
0 commit comments