1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2007 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: mail.c,v 1.87.2.1.2.5 2007/03/30 00:28:58 iliaa Exp $ */
20 :
21 : #include <stdlib.h>
22 : #include <ctype.h>
23 : #include <stdio.h>
24 : #include "php.h"
25 : #include "ext/standard/info.h"
26 :
27 : #if HAVE_SYSEXITS_H
28 : #include <sysexits.h>
29 : #endif
30 : #if HAVE_SYS_SYSEXITS_H
31 : #include <sys/sysexits.h>
32 : #endif
33 :
34 : #include "php_mail.h"
35 : #include "php_ini.h"
36 : #include "safe_mode.h"
37 : #include "exec.h"
38 :
39 : #if HAVE_SENDMAIL
40 : #ifdef PHP_WIN32
41 : #include "win32/sendmail.h"
42 : #endif
43 :
44 : #ifdef NETWARE
45 : #define EX_OK 0 /* successful termination */
46 : #define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
47 : #endif
48 :
49 : #define SKIP_LONG_HEADER_SEP(str, pos) \
50 : if (str[pos] == '\r' && str[pos + 1] == '\n' && (str[pos + 2] == ' ' || str[pos + 2] == '\t')) { \
51 : pos += 2; \
52 : while (str[pos + 1] == ' ' || str[pos + 1] == '\t') { \
53 : pos++; \
54 : } \
55 : continue; \
56 : } \
57 :
58 : #define MAIL_ASCIIZ_CHECK(str, len) \
59 : p = str; \
60 : e = p + len; \
61 : while ((p = memchr(p, '\0', (e - p)))) { \
62 : *p = ' '; \
63 : } \
64 :
65 :
66 : /* {{{ proto int ezmlm_hash(string addr)
67 : Calculate EZMLM list hash value. */
68 : PHP_FUNCTION(ezmlm_hash)
69 0 : {
70 0 : char *str = NULL;
71 0 : unsigned long h = 5381L;
72 : int j, str_len;
73 :
74 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
75 : &str, &str_len) == FAILURE) {
76 0 : return;
77 : }
78 :
79 0 : for (j = 0; j < str_len; j++) {
80 0 : h = (h + (h << 5)) ^ (unsigned long) (unsigned char) tolower(str[j]);
81 : }
82 :
83 0 : h = (h % 53);
84 :
85 0 : RETURN_LONG((int) h);
86 : }
87 : /* }}} */
88 :
89 : /* {{{ proto int mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]])
90 : Send an email message */
91 : PHP_FUNCTION(mail)
92 0 : {
93 0 : char *to=NULL, *message=NULL, *headers=NULL;
94 0 : char *subject=NULL, *extra_cmd=NULL;
95 : int to_len, message_len, headers_len;
96 : int subject_len, extra_cmd_len, i;
97 0 : char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
98 : char *to_r, *subject_r;
99 : char *p, *e;
100 :
101 0 : if (PG(safe_mode) && (ZEND_NUM_ARGS() == 5)) {
102 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "SAFE MODE Restriction in effect. The fifth parameter is disabled in SAFE MODE.");
103 0 : RETURN_FALSE;
104 : }
105 :
106 0 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ss",
107 : &to, &to_len,
108 : &subject, &subject_len,
109 : &message, &message_len,
110 : &headers, &headers_len,
111 : &extra_cmd, &extra_cmd_len
112 : ) == FAILURE) {
113 0 : return;
114 : }
115 :
116 : /* ASCIIZ check */
117 0 : MAIL_ASCIIZ_CHECK(to, to_len);
118 0 : MAIL_ASCIIZ_CHECK(subject, subject_len);
119 0 : MAIL_ASCIIZ_CHECK(message, message_len);
120 0 : if (headers) {
121 0 : MAIL_ASCIIZ_CHECK(headers, headers_len);
122 : }
123 0 : if (extra_cmd) {
124 0 : MAIL_ASCIIZ_CHECK(extra_cmd, extra_cmd_len);
125 : }
126 :
127 0 : if (to_len > 0) {
128 0 : to_r = estrndup(to, to_len);
129 0 : for (; to_len; to_len--) {
130 0 : if (!isspace((unsigned char) to_r[to_len - 1])) {
131 0 : break;
132 : }
133 0 : to_r[to_len - 1] = '\0';
134 : }
135 0 : for (i = 0; to_r[i]; i++) {
136 0 : if (iscntrl((unsigned char) to_r[i])) {
137 : /* According to RFC 822, section 3.1.1 long headers may be separated into
138 : * parts using CRLF followed at least one linear-white-space character ('\t' or ' ').
139 : * To prevent these separators from being replaced with a space, we use the
140 : * SKIP_LONG_HEADER_SEP to skip over them.
141 : */
142 0 : SKIP_LONG_HEADER_SEP(to_r, i);
143 0 : to_r[i] = ' ';
144 : }
145 : }
146 : } else {
147 0 : to_r = to;
148 : }
149 :
150 0 : if (subject_len > 0) {
151 0 : subject_r = estrndup(subject, subject_len);
152 0 : for (; subject_len; subject_len--) {
153 0 : if (!isspace((unsigned char) subject_r[subject_len - 1])) {
154 0 : break;
155 : }
156 0 : subject_r[subject_len - 1] = '\0';
157 : }
158 0 : for(i = 0; subject_r[i]; i++) {
159 0 : if (iscntrl((unsigned char) subject_r[i])) {
160 0 : SKIP_LONG_HEADER_SEP(subject_r, i);
161 0 : subject_r[i] = ' ';
162 : }
163 : }
164 : } else {
165 0 : subject_r = subject;
166 : }
167 :
168 0 : if (force_extra_parameters) {
169 0 : extra_cmd = estrdup(force_extra_parameters);
170 0 : } else if (extra_cmd) {
171 0 : extra_cmd = php_escape_shell_cmd(extra_cmd);
172 : }
173 :
174 0 : if (php_mail(to_r, subject_r, message, headers, extra_cmd TSRMLS_CC)) {
175 0 : RETVAL_TRUE;
176 : } else {
177 0 : RETVAL_FALSE;
178 : }
179 :
180 0 : if (extra_cmd) {
181 0 : efree (extra_cmd);
182 : }
183 0 : if (to_r != to) {
184 0 : efree(to_r);
185 : }
186 0 : if (subject_r != subject) {
187 0 : efree(subject_r);
188 : }
189 : }
190 : /* }}} */
191 :
192 : /* {{{ php_mail
193 : */
194 : PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC)
195 0 : {
196 : #if (defined PHP_WIN32 || defined NETWARE)
197 : int tsm_err;
198 : char *tsm_errmsg = NULL;
199 : #endif
200 : FILE *sendmail;
201 : int ret;
202 0 : char *sendmail_path = INI_STR("sendmail_path");
203 0 : char *sendmail_cmd = NULL;
204 :
205 0 : if (!sendmail_path) {
206 : #if (defined PHP_WIN32 || defined NETWARE)
207 : /* handle old style win smtp sending */
208 : if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, headers, subject, to, message, NULL, NULL, NULL TSRMLS_CC) == FAILURE) {
209 : if (tsm_errmsg) {
210 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg);
211 : efree(tsm_errmsg);
212 : } else {
213 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err));
214 : }
215 : return 0;
216 : }
217 : return 1;
218 : #else
219 0 : return 0;
220 : #endif
221 : }
222 0 : if (extra_cmd != NULL) {
223 0 : spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
224 : } else {
225 0 : sendmail_cmd = sendmail_path;
226 : }
227 :
228 : #ifdef PHP_WIN32
229 : sendmail = popen(sendmail_cmd, "wb");
230 : #else
231 : /* Since popen() doesn't indicate if the internal fork() doesn't work
232 : * (e.g. the shell can't be executed) we explicitely set it to 0 to be
233 : * sure we don't catch any older errno value. */
234 0 : errno = 0;
235 0 : sendmail = popen(sendmail_cmd, "w");
236 : #endif
237 0 : if (extra_cmd != NULL)
238 0 : efree (sendmail_cmd);
239 :
240 0 : if (sendmail) {
241 : #ifndef PHP_WIN32
242 0 : if (EACCES == errno) {
243 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
244 0 : pclose(sendmail);
245 0 : return 0;
246 : }
247 : #endif
248 0 : fprintf(sendmail, "To: %s\n", to);
249 0 : fprintf(sendmail, "Subject: %s\n", subject);
250 0 : if (headers != NULL) {
251 0 : fprintf(sendmail, "%s\n", headers);
252 : }
253 0 : fprintf(sendmail, "\n%s\n", message);
254 0 : ret = pclose(sendmail);
255 : #ifdef PHP_WIN32
256 : if (ret == -1)
257 : #else
258 : #if defined(EX_TEMPFAIL)
259 0 : if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
260 : #elif defined(EX_OK)
261 : if (ret != EX_OK)
262 : #else
263 : if (ret != 0)
264 : #endif
265 : #endif
266 : {
267 0 : return 0;
268 : } else {
269 0 : return 1;
270 : }
271 : } else {
272 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
273 0 : return 0;
274 : }
275 :
276 : return 1; /* never reached */
277 : }
278 : /* }}} */
279 :
280 : /* {{{ PHP_MINFO_FUNCTION
281 : */
282 : PHP_MINFO_FUNCTION(mail)
283 0 : {
284 0 : char *sendmail_path = INI_STR("sendmail_path");
285 :
286 : #ifdef PHP_WIN32
287 : if (!sendmail_path) {
288 : php_info_print_table_row(2, "Internal Sendmail Support for Windows", "enabled");
289 : } else {
290 : php_info_print_table_row(2, "Path to sendmail", sendmail_path);
291 : }
292 : #else
293 0 : php_info_print_table_row(2, "Path to sendmail", sendmail_path);
294 : #endif
295 0 : }
296 : /* }}} */
297 :
298 : #else
299 :
300 : PHP_FUNCTION(mail) {}
301 : PHP_MINFO_FUNCTION(mail) {}
302 :
303 : #endif
304 :
305 : /*
306 : * Local variables:
307 : * tab-width: 4
308 : * c-basic-offset: 4
309 : * End:
310 : * vim600: sw=4 ts=4 fdm=marker
311 : * vim<600: sw=4 ts=4
312 : */
|