PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - mail.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 214 251 85.3 %
Date: 2016-09-27 Functions: 12 13 92.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include <stdlib.h>
      22             : #include <ctype.h>
      23             : #include <stdio.h>
      24             : #include <time.h>
      25             : #include "php.h"
      26             : #include "ext/standard/info.h"
      27             : #include "ext/standard/php_string.h"
      28             : #include "ext/standard/basic_functions.h"
      29             : #include "ext/date/php_date.h"
      30             : #include "zend_smart_str.h"
      31             : 
      32             : #if HAVE_SYSEXITS_H
      33             : #include <sysexits.h>
      34             : #endif
      35             : #if HAVE_SYS_SYSEXITS_H
      36             : #include <sys/sysexits.h>
      37             : #endif
      38             : 
      39             : #if PHP_SIGCHILD
      40             : #if HAVE_SIGNAL_H
      41             : #include <signal.h>
      42             : #endif
      43             : #endif
      44             : 
      45             : #include "php_syslog.h"
      46             : #include "php_mail.h"
      47             : #include "php_ini.h"
      48             : #include "php_string.h"
      49             : #include "exec.h"
      50             : 
      51             : #ifdef PHP_WIN32
      52             : #include "win32/sendmail.h"
      53             : #endif
      54             : 
      55             : #ifdef NETWARE
      56             : #define EX_OK           0       /* successful termination */
      57             : #define EX_TEMPFAIL     75      /* temp failure; user is invited to retry */
      58             : #endif
      59             : 
      60             : #define SKIP_LONG_HEADER_SEP(str, pos)                                                                                                                                  \
      61             :         if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) {        \
      62             :                 pos += 2;                                                                                                                                                                               \
      63             :                 while (str[pos + 1] == ' ' || str[pos + 1] == '\t') {                                                                                   \
      64             :                         pos++;                                                                                                                                                                          \
      65             :                 }                                                                                                                                                                                               \
      66             :                 continue;                                                                                                                                                                               \
      67             :         }                                                                                                                                                                                                       \
      68             : 
      69             : #define MAIL_ASCIIZ_CHECK(str, len)                             \
      70             :         p = str;                                                                        \
      71             :         e = p + len;                                                            \
      72             :         while ((p = memchr(p, '\0', (e - p)))) {        \
      73             :                 *p = ' ';                                                               \
      74             :         }                                                                                       \
      75             : 
      76             : extern zend_long php_getuid(void);
      77             : 
      78             : /* {{{ proto int ezmlm_hash(string addr)
      79             :    Calculate EZMLM list hash value. */
      80           7 : PHP_FUNCTION(ezmlm_hash)
      81             : {
      82           7 :         char *str = NULL;
      83           7 :         unsigned int h = 5381;
      84             :         size_t j, str_len;
      85             : 
      86           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &str_len) == FAILURE) {
      87           5 :                 return;
      88             :         }
      89             : 
      90          42 :         for (j = 0; j < str_len; j++) {
      91          40 :                 h = (h + (h << 5)) ^ (zend_ulong) (unsigned char) tolower(str[j]);
      92             :         }
      93             : 
      94           2 :         h = (h % 53);
      95             : 
      96           2 :         RETURN_LONG((zend_long) h);
      97             : }
      98             : /* }}} */
      99             : 
     100             : 
     101          10 : static zend_bool php_mail_build_headers_check_field_value(zval *val)
     102             : {
     103          10 :         size_t len = 0;
     104          10 :         zend_string *value = Z_STR_P(val);
     105             : 
     106             :         /* https://tools.ietf.org/html/rfc2822#section-2.2.1 */
     107             :         /* https://tools.ietf.org/html/rfc2822#section-2.2.3 */
     108          72 :         while (len < value->len) {
     109          53 :                 if (*(value->val+len) == '\r') {
     110           7 :                         if (value->len - len >= 3
     111           2 :                                 &&  *(value->val+len+1) == '\n'
     112           3 :                                 && (*(value->val+len+2) == ' '  || *(value->val+len+2) == '\t')) {
     113           2 :                                 len += 3;
     114           2 :                                 continue;
     115             :                         }
     116           0 :                         return FAILURE;
     117             :                 }
     118          51 :                 if (*(value->val+len) == '\0') {
     119           1 :                         return FAILURE;
     120             :                 }
     121          50 :                 len++;
     122             :         }
     123           9 :         return SUCCESS;
     124             : }
     125             : 
     126             : 
     127          14 : static zend_bool php_mail_build_headers_check_field_name(zend_string *key)
     128             : {
     129          14 :         size_t len = 0;
     130             : 
     131             :         /* https://tools.ietf.org/html/rfc2822#section-2.2 */
     132          83 :         while (len < key->len) {
     133          59 :                 if (*(key->val+len) < 33 || *(key->val+len) > 126 || *(key->val+len) == ':') {
     134           4 :                         return FAILURE;
     135             :                 }
     136          55 :                 len++;
     137             :         }
     138          10 :         return SUCCESS;
     139             : }
     140             : 
     141             : 
     142             : static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val);
     143             : 
     144          14 : static void php_mail_build_headers_elem(smart_str *s, zend_string *key, zval *val)
     145             : {
     146          14 :         switch(Z_TYPE_P(val)) {
     147             :                 case IS_STRING:
     148          14 :                         if (php_mail_build_headers_check_field_name(key) != SUCCESS) {
     149           4 :                                 php_error_docref(NULL, E_WARNING, "Header field name (%s) contains invalid chars", ZSTR_VAL(key));
     150           4 :                                 return;
     151             :                         }
     152          10 :                         if (php_mail_build_headers_check_field_value(val) != SUCCESS) {
     153           1 :                                 php_error_docref(NULL, E_WARNING, "Header field value (%s => %s) contains invalid chars or format", ZSTR_VAL(key), Z_STRVAL_P(val));
     154           1 :                                 return;
     155             :                         }
     156             :                         smart_str_append(s, key);
     157             :                         smart_str_appendl(s, ": ", 2);
     158           9 :                         smart_str_appends(s, Z_STRVAL_P(val));
     159             :                         smart_str_appendl(s, "\r\n", 2);
     160           9 :                         break;
     161             :                 case IS_ARRAY:
     162           0 :                         php_mail_build_headers_elems(s, key, val);
     163           0 :                         break;
     164             :                 default:
     165           0 :                         php_error_docref(NULL, E_WARNING, "headers array elements must be string or array (%s)", ZSTR_VAL(key));
     166             :         }
     167             : }
     168             : 
     169             : 
     170          16 : static void php_mail_build_headers_elems(smart_str *s, zend_string *key, zval *val)
     171             : {
     172             :         zend_ulong idx;
     173             :         zend_string *tmp_key;
     174             :         zval *tmp_val;
     175             : 
     176             :         (void)(idx);
     177          66 :         ZEND_HASH_FOREACH_KEY_VAL(HASH_OF(val), idx, tmp_key, tmp_val) {
     178          17 :                 if (tmp_key) {
     179           1 :                         php_error_docref(NULL, E_WARNING, "Multiple header key must be numeric index (%s)", ZSTR_VAL(tmp_key));
     180           1 :                         continue;
     181             :                 }
     182          16 :                 if (Z_TYPE_P(tmp_val) != IS_STRING) {
     183           6 :                         php_error_docref(NULL, E_WARNING, "Multiple header values must be string (%s)", ZSTR_VAL(key));
     184           6 :                         continue;
     185             :                 }
     186          10 :                 php_mail_build_headers_elem(s, key, tmp_val);
     187             :         } ZEND_HASH_FOREACH_END();
     188          16 : }
     189             : 
     190             : 
     191           4 : PHPAPI zend_string *php_mail_build_headers(zval *headers)
     192             : {
     193             :         zend_ulong idx;
     194             :         zend_string *key;
     195             :         zval *val;
     196           4 :         smart_str s = {0};
     197             : 
     198             :         ZEND_ASSERT(Z_TYPE_P(headers) == IS_ARRAY);
     199             : 
     200          64 :         ZEND_HASH_FOREACH_KEY_VAL(HASH_OF(headers), idx, key, val) {
     201          28 :                 if (!key) {
     202           0 :                         php_error_docref(NULL, E_WARNING, "Found numeric header (" ZEND_LONG_FMT ")", idx);
     203           0 :                         continue;
     204             :                 }
     205             :                 /* https://tools.ietf.org/html/rfc2822#section-3.6 */
     206          28 :                 switch(ZSTR_LEN(key)) {
     207             :                         case sizeof("orig-date")-1:
     208           1 :                                 if (!strncasecmp("orig-date", ZSTR_VAL(key), ZSTR_LEN(key))) {
     209           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("orig-date", s, key, val);
     210             :                                 } else {
     211           0 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     212             :                                 }
     213           1 :                                 break;
     214             :                         case sizeof("from")-1:
     215          10 :                                 if (!strncasecmp("from", ZSTR_VAL(key), ZSTR_LEN(key))) {
     216           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("from", s, key, val);
     217             :                                 } else {
     218          17 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     219             :                                 }
     220          10 :                                 break;
     221             :                         case sizeof("sender")-1:
     222           3 :                                 if (!strncasecmp("sender", ZSTR_VAL(key), ZSTR_LEN(key))) {
     223           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("sender", s, key, val);
     224             :                                 } else {
     225           4 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     226             :                                 }
     227           3 :                                 break;
     228             :                         case sizeof("reply-to")-1:
     229           2 :                                 if (!strncasecmp("reply-to", ZSTR_VAL(key), ZSTR_LEN(key))) {
     230           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("reply-to", s, key, val);
     231             :                                 } else {
     232           1 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     233             :                                 }
     234           2 :                                 break;
     235             :                         case sizeof("to")-1: /* "to", "cc" */
     236           1 :                                 if (!strncasecmp("to", ZSTR_VAL(key), ZSTR_LEN(key))) {
     237           1 :                                         php_error_docref(NULL, E_WARNING, "Extra header cannot contain 'To' header");
     238           1 :                                         continue;
     239             :                                 }
     240           0 :                                 if (!strncasecmp("cc", ZSTR_VAL(key), ZSTR_LEN(key))) {
     241           0 :                                         PHP_MAIL_BUILD_HEADER_CHECK("cc", s, key, val);
     242             :                                 } else {
     243           0 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     244             :                                 }
     245           0 :                                 break;
     246             :                         case sizeof("bcc")-1:
     247           3 :                                 if (!strncasecmp("bcc", ZSTR_VAL(key), ZSTR_LEN(key))) {
     248           3 :                                         PHP_MAIL_BUILD_HEADER_CHECK("bcc", s, key, val);
     249             :                                 } else {
     250           2 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     251             :                                 }
     252           3 :                                 break;
     253             :                         case sizeof("message-id")-1: /* "references" */
     254           1 :                                 if (!strncasecmp("message-id", ZSTR_VAL(key), ZSTR_LEN(key))) {
     255           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("message-id", s, key, val);
     256           0 :                                 } else if (!strncasecmp("references", ZSTR_VAL(key), ZSTR_LEN(key))) {
     257           0 :                                         PHP_MAIL_BUILD_HEADER_CHECK("references", s, key, val);
     258             :                                 } else {
     259           0 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     260             :                                 }
     261           1 :                                 break;
     262             :                         case sizeof("in-reply-to")-1:
     263           1 :                                 if (!strncasecmp("in-reply-to", ZSTR_VAL(key), ZSTR_LEN(key))) {
     264           2 :                                         PHP_MAIL_BUILD_HEADER_CHECK("in-reply-to", s, key, val);
     265             :                                 } else {
     266           0 :                                         PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     267             :                                 }
     268           1 :                                 break;
     269             :                         case sizeof("subject")-1:
     270           1 :                                 if (!strncasecmp("subject", ZSTR_VAL(key), ZSTR_LEN(key))) {
     271           0 :                                         php_error_docref(NULL, E_WARNING, "Extra header cannot contain 'Subject' header");
     272           0 :                                         continue;
     273             :                                 }
     274           2 :                                 PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     275           1 :                                 break;
     276             :                         default:
     277           9 :                                 PHP_MAIL_BUILD_HEADER_DEFAULT(s, key, val);
     278             :                 }
     279             :         } ZEND_HASH_FOREACH_END();
     280             : 
     281             :         /* Remove the last \r\n */
     282           4 :         if (s.s) s.s->len -= 2;
     283             :         smart_str_0(&s);
     284             : 
     285           4 :         return s.s;
     286             : }
     287             : 
     288             : 
     289             : /* {{{ proto int mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
     290             :    Send an email message */
     291          40 : PHP_FUNCTION(mail)
     292             : {
     293          40 :         char *to=NULL, *message=NULL;
     294          40 :         char *subject=NULL;
     295          40 :         zend_string *extra_cmd=NULL, *str_headers=NULL, *tmp_headers;
     296          40 :         zval *headers = NULL;
     297             :         size_t to_len, message_len;
     298             :         size_t subject_len, i;
     299          40 :         char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
     300             :         char *to_r, *subject_r;
     301             :         char *p, *e;
     302             : 
     303          40 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|zS", &to, &to_len, &subject, &subject_len, &message, &message_len, &headers, &extra_cmd) == FAILURE) {
     304           2 :                 return;
     305             :         }
     306             : 
     307             :         /* ASCIIZ check */
     308          38 :         MAIL_ASCIIZ_CHECK(to, to_len);
     309          38 :         MAIL_ASCIIZ_CHECK(subject, subject_len);
     310          38 :         MAIL_ASCIIZ_CHECK(message, message_len);
     311          38 :         if (headers) {
     312          60 :                 switch(Z_TYPE_P(headers)) {
     313             :                         case IS_STRING:
     314          50 :                                 tmp_headers = zend_string_init(Z_STRVAL_P(headers), Z_STRLEN_P(headers), 0);
     315          25 :                                 MAIL_ASCIIZ_CHECK(ZSTR_VAL(tmp_headers), ZSTR_LEN(tmp_headers));
     316          25 :                                 str_headers = php_trim(tmp_headers, NULL, 0, 2);
     317             :                                 zend_string_release(tmp_headers);
     318          25 :                                 break;
     319             :                         case IS_ARRAY:
     320           4 :                                 str_headers = php_mail_build_headers(headers);
     321           4 :                                 break;
     322             :                         default:
     323           1 :                                 php_error_docref(NULL, E_WARNING, "headers parameter must be string or array");
     324           1 :                                 RETURN_FALSE;
     325             :                 }
     326             :         }
     327          37 :         if (extra_cmd) {
     328           1 :                 MAIL_ASCIIZ_CHECK(ZSTR_VAL(extra_cmd), ZSTR_LEN(extra_cmd));
     329             :         }
     330             : 
     331          37 :         if (to_len > 0) {
     332          37 :                 to_r = estrndup(to, to_len);
     333          37 :                 for (; to_len; to_len--) {
     334          37 :                         if (!isspace((unsigned char) to_r[to_len - 1])) {
     335          37 :                                 break;
     336             :                         }
     337           0 :                         to_r[to_len - 1] = '\0';
     338             :                 }
     339         682 :                 for (i = 0; to_r[i]; i++) {
     340         645 :                         if (iscntrl((unsigned char) to_r[i])) {
     341             :                                 /* According to RFC 822, section 3.1.1 long headers may be separated into
     342             :                                  * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
     343             :                                  * To prevent these separators from being replaced with a space, we use the
     344             :                                  * SKIP_LONG_HEADER_SEP to skip over them. */
     345           0 :                                 SKIP_LONG_HEADER_SEP(to_r, i);
     346           0 :                                 to_r[i] = ' ';
     347             :                         }
     348             :                 }
     349             :         } else {
     350           0 :                 to_r = to;
     351             :         }
     352             : 
     353          37 :         if (subject_len > 0) {
     354          37 :                 subject_r = estrndup(subject, subject_len);
     355          37 :                 for (; subject_len; subject_len--) {
     356          37 :                         if (!isspace((unsigned char) subject_r[subject_len - 1])) {
     357          37 :                                 break;
     358             :                         }
     359           0 :                         subject_r[subject_len - 1] = '\0';
     360             :                 }
     361         477 :                 for (i = 0; subject_r[i]; i++) {
     362         440 :                         if (iscntrl((unsigned char) subject_r[i])) {
     363           0 :                                 SKIP_LONG_HEADER_SEP(subject_r, i);
     364           0 :                                 subject_r[i] = ' ';
     365             :                         }
     366             :                 }
     367             :         } else {
     368           0 :                 subject_r = subject;
     369             :         }
     370             : 
     371          37 :         if (force_extra_parameters) {
     372           1 :                 extra_cmd = php_escape_shell_cmd(force_extra_parameters);
     373          36 :         } else if (extra_cmd) {
     374           1 :                 extra_cmd = php_escape_shell_cmd(ZSTR_VAL(extra_cmd));
     375             :         }
     376             : 
     377          37 :         if (php_mail(to_r, subject_r, message, str_headers ? ZSTR_VAL(str_headers) : NULL, extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
     378          21 :                 RETVAL_TRUE;
     379             :         } else {
     380          16 :                 RETVAL_FALSE;
     381             :         }
     382             : 
     383          37 :         if (str_headers) {
     384             :                 zend_string_release(str_headers);
     385             :         }
     386             : 
     387          37 :         if (extra_cmd) {
     388           2 :                 zend_string_release(extra_cmd);
     389             :         }
     390          37 :         if (to_r != to) {
     391          37 :                 efree(to_r);
     392             :         }
     393          37 :         if (subject_r != subject) {
     394          37 :                 efree(subject_r);
     395             :         }
     396             : }
     397             : /* }}} */
     398             : 
     399             : 
     400           1 : void php_mail_log_crlf_to_spaces(char *message) {
     401             :         /* Find all instances of carriage returns or line feeds and
     402             :          * replace them with spaces. Thus, a log line is always one line
     403             :          * long
     404             :          */
     405           1 :         char *p = message;
     406           3 :         while ((p = strpbrk(p, "\r\n"))) {
     407           1 :                 *p = ' ';
     408             :         }
     409           1 : }
     410             : 
     411           0 : void php_mail_log_to_syslog(char *message) {
     412             :         /* Write 'message' to syslog. */
     413             : #ifdef HAVE_SYSLOG_H
     414           0 :         php_syslog(LOG_NOTICE, "%s", message);
     415             : #endif
     416           0 : }
     417             : 
     418             : 
     419           1 : void php_mail_log_to_file(char *filename, char *message, size_t message_size) {
     420             :         /* Write 'message' to the given file. */
     421           1 :         uint flags = IGNORE_URL_WIN | REPORT_ERRORS | STREAM_DISABLE_OPEN_BASEDIR;
     422           1 :         php_stream *stream = php_stream_open_wrapper(filename, "a", flags, NULL);
     423           1 :         if (stream) {
     424           1 :                 php_stream_write(stream, message, message_size);
     425           1 :                 php_stream_close(stream);
     426             :         }
     427           1 : }
     428             : 
     429             : 
     430          44 : static int php_mail_detect_multiple_crlf(char *hdr) {
     431             :         /* This function detects multiple/malformed multiple newlines. */
     432             : 
     433          44 :         if (!hdr || !strlen(hdr)) {
     434           1 :                 return 0;
     435             :         }
     436             : 
     437             :         /* Should not have any newlines at the beginning. */
     438             :         /* RFC 2822 2.2. Header Fields */
     439          43 :         if (*hdr < 33 || *hdr > 126 || *hdr == ':') {
     440           6 :                 return 1;
     441             :         }
     442             : 
     443        1824 :         while(*hdr) {
     444        1756 :                 if (*hdr == '\r') {
     445          17 :                         if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) {
     446             :                                 /* Malformed or multiple newlines. */
     447           3 :                                 return 1;
     448             :                         } else {
     449          14 :                                 hdr += 2;
     450             :                         }
     451        1739 :                 } else if (*hdr == '\n') {
     452          34 :                         if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') {
     453             :                                 /* Malformed or multiple newlines. */
     454           3 :                                 return 1;
     455             :                         } else {
     456          31 :                                 hdr += 2;
     457             :                         }
     458             :                 } else {
     459        1705 :                         hdr++;
     460             :                 }
     461             :         }
     462             : 
     463          31 :         return 0;
     464             : }
     465             : 
     466             : 
     467             : /* {{{ php_mail
     468             :  */
     469          52 : PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd)
     470             : {
     471             : #if (defined PHP_WIN32 || defined NETWARE)
     472             :         int tsm_err;
     473             :         char *tsm_errmsg = NULL;
     474             : #endif
     475             :         FILE *sendmail;
     476             :         int ret;
     477          52 :         char *sendmail_path = INI_STR("sendmail_path");
     478          52 :         char *sendmail_cmd = NULL;
     479          52 :         char *mail_log = INI_STR("mail.log");
     480          52 :         char *hdr = headers;
     481             : #if PHP_SIGCHILD
     482             :         void (*sig_handler)() = NULL;
     483             : #endif
     484             : 
     485             : #define MAIL_RET(val) \
     486             :         if (hdr != headers) {   \
     487             :                 efree(hdr);     \
     488             :         }       \
     489             :         return val;     \
     490             : 
     491          52 :         if (mail_log && *mail_log) {
     492             :                 char *tmp;
     493             :                 time_t curtime;
     494             :                 size_t l;
     495             :                 zend_string *date_str;
     496             : 
     497           1 :                 time(&curtime);
     498           1 :                 date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1);
     499             : 
     500           1 :                 l = spprintf(&tmp, 0, "[%s] mail() on [%s:%d]: To: %s -- Headers: %s\n", ZSTR_VAL(date_str), zend_get_executed_filename(), zend_get_executed_lineno(), to, hdr ? hdr : "");
     501             : 
     502             :                 zend_string_free(date_str);
     503             : 
     504           1 :                 if (hdr) {
     505           1 :                         php_mail_log_crlf_to_spaces(tmp);
     506             :                 }
     507             : 
     508           1 :                 if (!strcmp(mail_log, "syslog")) {
     509             :                         /* Drop the final space when logging to syslog. */
     510           0 :                         tmp[l - 1] = 0;
     511           0 :                         php_mail_log_to_syslog(tmp);
     512             :                 }
     513             :                 else {
     514             :                         /* Convert the final space to a newline when logging to file. */
     515           1 :                         tmp[l - 1] = '\n';
     516           1 :                         php_mail_log_to_file(mail_log, tmp, l);
     517             :                 }
     518             : 
     519           1 :                 efree(tmp);
     520             :         }
     521             : 
     522          52 :         if (PG(mail_x_header)) {
     523           2 :                 const char *tmp = zend_get_executed_filename();
     524             :                 zend_string *f;
     525             : 
     526           2 :                 f = php_basename(tmp, strlen(tmp), NULL, 0);
     527             : 
     528           2 :                 if (headers != NULL && *headers) {
     529           0 :                         spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\n%s", php_getuid(), ZSTR_VAL(f), headers);
     530             :                 } else {
     531           2 :                         spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), ZSTR_VAL(f));
     532             :                 }
     533             :                 zend_string_release(f);
     534             :         }
     535             : 
     536          52 :         if (hdr && php_mail_detect_multiple_crlf(hdr)) {
     537          12 :                 php_error_docref(NULL, E_WARNING, "Multiple or malformed newlines found in additional_header");
     538          12 :                 MAIL_RET(0);
     539             :         }
     540             : 
     541          40 :         if (!sendmail_path) {
     542             : #if (defined PHP_WIN32 || defined NETWARE)
     543             :                 /* handle old style win smtp sending */
     544             :                 if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL) == FAILURE) {
     545             :                         if (tsm_errmsg) {
     546             :                                 php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg);
     547             :                                 efree(tsm_errmsg);
     548             :                         } else {
     549             :                                 php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err));
     550             :                         }
     551             :                         MAIL_RET(0);
     552             :                 }
     553             :                 MAIL_RET(1);
     554             : #else
     555           0 :                 MAIL_RET(0);
     556             : #endif
     557             :         }
     558          40 :         if (extra_cmd != NULL) {
     559           2 :                 spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
     560             :         } else {
     561          38 :                 sendmail_cmd = sendmail_path;
     562             :         }
     563             : 
     564             : #if PHP_SIGCHILD
     565             :         /* Set signal handler of SIGCHLD to default to prevent other signal handlers
     566             :          * from being called and reaping the return code when our child exits.
     567             :          * The original handler needs to be restored after pclose() */
     568             :         sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
     569             :         if (sig_handler == SIG_ERR) {
     570             :                 sig_handler = NULL;
     571             :         }
     572             : #endif
     573             : 
     574             : #ifdef PHP_WIN32
     575             :         sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL);
     576             : #else
     577             :         /* Since popen() doesn't indicate if the internal fork() doesn't work
     578             :          * (e.g. the shell can't be executed) we explicitly set it to 0 to be
     579             :          * sure we don't catch any older errno value. */
     580          40 :         errno = 0;
     581          40 :         sendmail = popen(sendmail_cmd, "w");
     582             : #endif
     583          40 :         if (extra_cmd != NULL) {
     584           2 :                 efree (sendmail_cmd);
     585             :         }
     586             : 
     587          40 :         if (sendmail) {
     588             : #ifndef PHP_WIN32
     589          40 :                 if (EACCES == errno) {
     590           0 :                         php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
     591           0 :                         pclose(sendmail);
     592             : #if PHP_SIGCHILD
     593             :                         /* Restore handler in case of error on Windows
     594             :                            Not sure if this applicable on Win but just in case. */
     595             :                         if (sig_handler) {
     596             :                                 signal(SIGCHLD, sig_handler);
     597             :                         }
     598             : #endif
     599           0 :                         MAIL_RET(0);
     600             :                 }
     601             : #endif
     602          40 :                 fprintf(sendmail, "To: %s\n", to);
     603          40 :                 fprintf(sendmail, "Subject: %s\n", subject);
     604          40 :                 if (hdr != NULL) {
     605          32 :                         fprintf(sendmail, "%s\n", hdr);
     606             :                 }
     607          40 :                 fprintf(sendmail, "\n%s\n", message);
     608          40 :                 ret = pclose(sendmail);
     609             : 
     610             : #if PHP_SIGCHILD
     611             :                 if (sig_handler) {
     612             :                         signal(SIGCHLD, sig_handler);
     613             :                 }
     614             : #endif
     615             : 
     616             : #ifdef PHP_WIN32
     617             :                 if (ret == -1)
     618             : #else
     619             : #if defined(EX_TEMPFAIL)
     620          40 :                 if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
     621             : #elif defined(EX_OK)
     622             :                 if (ret != EX_OK)
     623             : #else
     624             :                 if (ret != 0)
     625             : #endif
     626             : #endif
     627             :                 {
     628           4 :                         MAIL_RET(0);
     629             :                 } else {
     630          36 :                         MAIL_RET(1);
     631             :                 }
     632             :         } else {
     633           0 :                 php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
     634             : #if PHP_SIGCHILD
     635             :                 if (sig_handler) {
     636             :                         signal(SIGCHLD, sig_handler);
     637             :                 }
     638             : #endif
     639           0 :                 MAIL_RET(0);
     640             :         }
     641             : 
     642             :         MAIL_RET(1); /* never reached */
     643             : }
     644             : /* }}} */
     645             : 
     646             : /* {{{ PHP_MINFO_FUNCTION
     647             :  */
     648         144 : PHP_MINFO_FUNCTION(mail)
     649             : {
     650         144 :         char *sendmail_path = INI_STR("sendmail_path");
     651             : 
     652             : #ifdef PHP_WIN32
     653             :         if (!sendmail_path) {
     654             :                 php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
     655             :         } else {
     656             :                 php_info_print_table_row(2, "Path to sendmail", sendmail_path);
     657             :         }
     658             : #else
     659         144 :         php_info_print_table_row(2, "Path to sendmail", sendmail_path);
     660             : #endif
     661         144 : }
     662             : /* }}} */
     663             : 
     664             : /*
     665             :  * Local variables:
     666             :  * tab-width: 4
     667             :  * c-basic-offset: 4
     668             :  * End:
     669             :  * vim600: sw=4 ts=4 fdm=marker
     670             :  * vim<600: sw=4 ts=4
     671             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:09 +0000 (3 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.