bug3
[aidenligne_francais_universite.git] / fluxbb / include / email.php
1 <?php
2 /***********************************************************************
3
4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org)
5
6 This file is part of PunBB.
7
8 PunBB is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PunBB is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 MA 02111-1307 USA
22
23 ************************************************************************/
24
25
26 // Make sure no one attempts to run this script "directly"
27 if (!defined('PUN'))
28 exit;
29
30
31 //
32 // Validate an e-mail address
33 //
34 function is_valid_email($email)
35 {
36 if (strlen($email) > 50)
37 return false;
38
39 return preg_match('/^(([^<>()[\]\\.,;:\s@"\']+(\.[^<>()[\]\\.,;:\s@"\']+)*)|("[^"\']+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-zA-Z\d\-]+\.)+[a-zA-Z]{2,}))$/', $email);
40 }
41
42
43 //
44 // Check if $email is banned
45 //
46 function is_banned_email($email)
47 {
48 global $db, $pun_bans;
49
50 foreach ($pun_bans as $cur_ban)
51 {
52 if ($cur_ban['email'] != '' &&
53 ($email == $cur_ban['email'] ||
54 (strpos($cur_ban['email'], '@') === false && stristr($email, '@'.$cur_ban['email']))))
55 return true;
56 }
57
58 return false;
59 }
60
61
62 //
63 // Wrapper for PHP's mail()
64 //
65 function pun_mail($to, $subject, $message, $from = '')
66 {
67 global $pun_config, $lang_common;
68
69 // Default sender/return address
70 if (!$from)
71 $from = '"'.str_replace('"', '', $pun_config['o_board_title'].' '.$lang_common['Mailer']).'" <'.$pun_config['o_webmaster_email'].'>';
72
73 // Do a little spring cleaning
74 $to = trim(preg_replace('#[\n\r]+#s', '', $to));
75 $subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
76 $from = trim(preg_replace('#[\n\r:]+#s', '', $from));
77
78 $headers = 'From: '.$from."\r\n".'Date: '.date('r')."\r\n".'MIME-Version: 1.0'."\r\n".'Content-transfer-encoding: 8bit'."\r\n".'Content-type: text/plain; charset='.$lang_common['lang_encoding']."\r\n".'X-Mailer: FluxBB Mailer';
79
80 // Make sure all linebreaks are CRLF in message (and strip out any NULL bytes)
81 $message = str_replace(array("\n", "\0"), array("\r\n", ''), pun_linebreaks($message));
82
83 if ($pun_config['o_smtp_host'] != '')
84 smtp_mail($to, $subject, $message, $headers);
85 else
86 {
87 // Change the linebreaks used in the headers according to OS
88 if (strtoupper(substr(PHP_OS, 0, 3)) == 'MAC')
89 $headers = str_replace("\r\n", "\r", $headers);
90 else if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN')
91 $headers = str_replace("\r\n", "\n", $headers);
92
93 mail($to, $subject, $message, $headers);
94 }
95 }
96
97
98 //
99 // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
100 // They deserve all the credit for writing it. I made small modifications for it to suit FluxBB and it's coding standards.
101 //
102 function server_parse($socket, $expected_response)
103 {
104 $server_response = '';
105 while (substr($server_response, 3, 1) != ' ')
106 {
107 if (!($server_response = fgets($socket, 256)))
108 error('Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__);
109 }
110
111 if (!(substr($server_response, 0, 3) == $expected_response))
112 error('Unable to send e-mail. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
113 }
114
115
116 //
117 // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
118 // They deserve all the credit for writing it. I made small modifications for it to suit FluxBB and it's coding standards.
119 //
120 function smtp_mail($to, $subject, $message, $headers = '')
121 {
122 global $pun_config;
123
124 $recipients = explode(',', $to);
125
126 // Sanitize the message
127 $message = str_replace("\r\n.", "\r\n..", $message);
128 $message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
129
130 // Are we using port 25 or a custom port?
131 if (strpos($pun_config['o_smtp_host'], ':') !== false)
132 list($smtp_host, $smtp_port) = explode(':', $pun_config['o_smtp_host']);
133 else
134 {
135 $smtp_host = $pun_config['o_smtp_host'];
136 $smtp_port = 25;
137 }
138
139 if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
140 error('Could not connect to smtp host "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.')', __FILE__, __LINE__);
141
142 server_parse($socket, '220');
143
144 if ($pun_config['o_smtp_user'] != '' && $pun_config['o_smtp_pass'] != '')
145 {
146 fwrite($socket, 'EHLO '.$smtp_host."\r\n");
147 server_parse($socket, '250');
148
149 fwrite($socket, 'AUTH LOGIN'."\r\n");
150 server_parse($socket, '334');
151
152 fwrite($socket, base64_encode($pun_config['o_smtp_user'])."\r\n");
153 server_parse($socket, '334');
154
155 fwrite($socket, base64_encode($pun_config['o_smtp_pass'])."\r\n");
156 server_parse($socket, '235');
157 }
158 else
159 {
160 fwrite($socket, 'HELO '.$smtp_host."\r\n");
161 server_parse($socket, '250');
162 }
163
164 fwrite($socket, 'MAIL FROM: <'.$pun_config['o_webmaster_email'].'>'."\r\n");
165 server_parse($socket, '250');
166
167 $to_header = 'To: ';
168
169 @reset($recipients);
170 while (list(, $email) = @each($recipients))
171 {
172 fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
173 server_parse($socket, '250');
174
175 $to_header .= '<'.$email.'>, ';
176 }
177
178 fwrite($socket, 'DATA'."\r\n");
179 server_parse($socket, '354');
180
181 fwrite($socket, 'Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n");
182
183 fwrite($socket, '.'."\r\n");
184 server_parse($socket, '250');
185
186 fwrite($socket, 'QUIT'."\r\n");
187 fclose($socket);
188
189 return true;
190 }