punbb/include/email.php
changeset 7 98bbc533541c
equal deleted inserted replaced
6:5e1f1e916419 7:98bbc533541c
       
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2008  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) > 80)
       
    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 $pun_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 = '"'.sprintf($lang_common['Forum mailer'], str_replace('"', '', $pun_config['o_board_title'])).'" <'.$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: '.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';
       
    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 PunBB 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 PunBB 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 	// Are we using port 25 or a custom port?
       
   127 	if (strpos($pun_config['o_smtp_host'], ':') !== false)
       
   128 		list($smtp_host, $smtp_port) = explode(':', $pun_config['o_smtp_host']);
       
   129 	else
       
   130 	{
       
   131 		$smtp_host = $pun_config['o_smtp_host'];
       
   132 		$smtp_port = 25;
       
   133 	}
       
   134 
       
   135 	if ($pun_config['o_smtp_ssl'] == '1')
       
   136 		$smtp_host = 'ssl://'.$smtp_host;
       
   137 
       
   138 	if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
       
   139 		error('Could not connect to smtp host "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.').', __FILE__, __LINE__);
       
   140 
       
   141 	server_parse($socket, '220');
       
   142 
       
   143 	if ($pun_config['o_smtp_user'] != '' && $pun_config['o_smtp_pass'] != '')
       
   144 	{
       
   145 		fwrite($socket, 'EHLO '.$smtp_host."\r\n");
       
   146 		server_parse($socket, '250');
       
   147 
       
   148 		fwrite($socket, 'AUTH LOGIN'."\r\n");
       
   149 		server_parse($socket, '334');
       
   150 
       
   151 		fwrite($socket, base64_encode($pun_config['o_smtp_user'])."\r\n");
       
   152 		server_parse($socket, '334');
       
   153 
       
   154 		fwrite($socket, base64_encode($pun_config['o_smtp_pass'])."\r\n");
       
   155 		server_parse($socket, '235');
       
   156 	}
       
   157 	else
       
   158 	{
       
   159 		fwrite($socket, 'HELO '.$smtp_host."\r\n");
       
   160 		server_parse($socket, '250');
       
   161 	}
       
   162 
       
   163 	fwrite($socket, 'MAIL FROM: <'.$pun_config['o_webmaster_email'].'>'."\r\n");
       
   164 	server_parse($socket, '250');
       
   165 
       
   166 	$to_header = 'To: ';
       
   167 
       
   168 	@reset($recipients);
       
   169 	while (list(, $email) = @each($recipients))
       
   170 	{
       
   171 		fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
       
   172 		server_parse($socket, '250');
       
   173 
       
   174 		$to_header .= '<'.$email.'>, ';
       
   175 	}
       
   176 
       
   177 	fwrite($socket, 'DATA'."\r\n");
       
   178 	server_parse($socket, '354');
       
   179 
       
   180 	fwrite($socket, 'Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n");
       
   181 
       
   182 	fwrite($socket, '.'."\r\n");
       
   183 	server_parse($socket, '250');
       
   184 
       
   185 	fwrite($socket, 'QUIT'."\r\n");
       
   186 	fclose($socket);
       
   187 
       
   188 	return true;
       
   189 }