1<?php
2/**
3 * Loads functions used in dealing with email addresses and email sending.
4 *
5 * @copyright (C) 2008-2012 PunBB, partially based on code (C) 2008-2009 FluxBB.org
6 * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
7 * @package PunBB
8 */
9
10
11// Make sure no one attempts to run this script "directly"
12if (!defined('FORUM'))
13	exit;
14
15
16//
17// Validate an e-mail address
18//
19function is_valid_email($email)
20{
21	$return = ($hook = get_hook('em_fn_is_valid_email_start')) ? eval($hook) : null;
22	if ($return !== null)
23		return $return;
24
25	if (strlen($email) > 80)
26		return false;
27
28	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);
29}
30
31
32//
33// Check if $email is banned
34//
35function is_banned_email($email)
36{
37	global $forum_db, $forum_bans;
38
39	$return = ($hook = get_hook('em_fn_is_banned_email_start')) ? eval($hook) : null;
40	if ($return !== null)
41		return $return;
42
43	foreach ($forum_bans as $cur_ban)
44	{
45		if ($cur_ban['email'] != '' &&
46			($email == $cur_ban['email'] ||
47			(strpos($cur_ban['email'], '@') === false && stristr($email, '@'.$cur_ban['email']))))
48			return true;
49	}
50
51	return false;
52}
53
54
55//
56// Wrapper for PHP's mail()
57//
58function forum_mail($to, $subject, $message, $reply_to_email = '', $reply_to_name = '')
59{
60	global $forum_config, $lang_common;
61
62	// Default sender address
63	$from_name = sprintf($lang_common['Forum mailer'], $forum_config['o_board_title']);
64	$from_email = $forum_config['o_webmaster_email'];
65
66	($hook = get_hook('em_fn_forum_mail_start')) ? eval($hook) : null;
67
68	// Do a little spring cleaning
69	$to = forum_trim(preg_replace('#[\n\r]+#s', '', $to));
70	$subject = forum_trim(preg_replace('#[\n\r]+#s', '', $subject));
71	$from_email = forum_trim(preg_replace('#[\n\r:]+#s', '', $from_email));
72	$from_name = forum_trim(preg_replace('#[\n\r:]+#s', '', str_replace('"', '', $from_name)));
73	$reply_to_email = forum_trim(preg_replace('#[\n\r:]+#s', '', $reply_to_email));
74	$reply_to_name = forum_trim(preg_replace('#[\n\r:]+#s', '', str_replace('"', '', $reply_to_name)));
75
76	// Set up some headers to take advantage of UTF-8
77	$from = "=?UTF-8?B?".base64_encode($from_name)."?=".' <'.$from_email.'>';
78	$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
79
80	$headers = 'From: '.$from."\r\n".'Date: '.gmdate('r')."\r\n".'MIME-Version: 1.0'."\r\n".'Content-transfer-encoding: 8bit'."\r\n".'Content-type: text/plain; charset=utf-8'."\r\n".'X-Mailer: PunBB Mailer';
81
82	// If we specified a reply-to email, we deal with it here
83	if (!empty($reply_to_email))
84	{
85		$reply_to = "=?UTF-8?B?".base64_encode($reply_to_name)."?=".' <'.$reply_to_email.'>';
86
87		$headers .= "\r\n".'Reply-To: '.$reply_to;
88	}
89
90	// Make sure all linebreaks are CRLF in message (and strip out any NULL bytes)
91	$message = str_replace(array("\n", "\0"), array("\r\n", ''), forum_linebreaks($message));
92
93	($hook = get_hook('em_fn_forum_mail_pre_send')) ? eval($hook) : null;
94
95	if ($forum_config['o_smtp_host'] != '')
96		smtp_mail($to, $subject, $message, $headers);
97	else
98	{
99		// Change the linebreaks used in the headers according to OS
100		if (strtoupper(substr(PHP_OS, 0, 3)) == 'MAC')
101			$headers = str_replace("\r\n", "\r", $headers);
102		else if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN')
103			$headers = str_replace("\r\n", "\n", $headers);
104
105		mail($to, $subject, $message, $headers);
106	}
107}
108
109
110//
111// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
112// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
113//
114function server_parse($socket, $expected_response)
115{
116	$server_response = '';
117	while (substr($server_response, 3, 1) != ' ')
118	{
119		if (!($server_response = fgets($socket, 256)))
120			error($expected_response.' Couldn\'t get mail server response codes.<br />Please contact the forum administrator.', __FILE__, __LINE__);
121	}
122
123	if (!(substr($server_response, 0, 3) == $expected_response)) {
124        error($expected_response.' Unable to send e-mail.<br />Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
125        }
126}
127
128
129//
130// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
131// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
132//
133function smtp_mail($to, $subject, $message, $headers = '')
134{
135	global $forum_config;
136	$recipients = explode(',', $to);
137
138	// Sanitize the message
139	$message = str_replace("\r\n.", "\r\n..", $message);
140	$message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
141
142	// Are we using port 25 or a custom port?
143	if (strpos($forum_config['o_smtp_host'], ':') !== false)
144		list($smtp_host, $smtp_port) = explode(':', $forum_config['o_smtp_host']);
145	else
146	{
147		$smtp_host = $forum_config['o_smtp_host'];
148		$smtp_port = 25;
149	}
150
151	if ($forum_config['o_smtp_ssl'] == '1')
152		$smtp_host = 'ssl://'.$smtp_host;
153
154	if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
155		error('Could not connect to smtp host "'.$forum_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.').', __FILE__, __LINE__);
156
157	server_parse($socket, '220');
158
159	if ($forum_config['o_smtp_user'] != '' && $forum_config['o_smtp_pass'] != '')
160	{
161		fwrite($socket, 'EHLO '.$smtp_host."\r\n");
162		server_parse($socket, '250');
163
164		fwrite($socket, 'AUTH LOGIN'."\r\n");
165		server_parse($socket, '334');
166
167		fwrite($socket, base64_encode($forum_config['o_smtp_user'])."\r\n");
168		server_parse($socket, '334');
169
170		fwrite($socket, base64_encode($forum_config['o_smtp_pass'])."\r\n");
171		server_parse($socket, '235');
172	}
173	else
174	{
175		fwrite($socket, 'HELO '.$smtp_host."\r\n");
176		server_parse($socket, '250');
177	}
178
179	fwrite($socket, 'MAIL FROM: <'.$forum_config['o_webmaster_email'].'>'."\r\n");
180	server_parse($socket, '250');
181
182	foreach ($recipients as $email)
183	{
184		fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
185		server_parse($socket, '250');
186	}
187
188	fwrite($socket, 'DATA'."\r\n");
189	server_parse($socket, '354');
190
191	fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
192
193	fwrite($socket, '.'."\r\n");
194	server_parse($socket, '250');
195
196	fwrite($socket, 'QUIT'."\r\n");
197	fclose($socket);
198
199	return true;
200}
201
202define('FORUM_EMAIL_FUNCTIONS_LOADED', 1);
203