punbb/include/email.php
changeset 0 f9ffdbd96607
child 2 a8a21e1c7afa
equal deleted inserted replaced
-1:000000000000 0:f9ffdbd96607
       
     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: 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 (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
       
   136 		error('Could not connect to smtp host "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.')', __FILE__, __LINE__);
       
   137 
       
   138 	server_parse($socket, '220');
       
   139 
       
   140 	if ($pun_config['o_smtp_user'] != '' && $pun_config['o_smtp_pass'] != '')
       
   141 	{
       
   142 		fwrite($socket, 'EHLO '.$smtp_host."\r\n");
       
   143 		server_parse($socket, '250');
       
   144 
       
   145 		fwrite($socket, 'AUTH LOGIN'."\r\n");
       
   146 		server_parse($socket, '334');
       
   147 
       
   148 		fwrite($socket, base64_encode($pun_config['o_smtp_user'])."\r\n");
       
   149 		server_parse($socket, '334');
       
   150 
       
   151 		fwrite($socket, base64_encode($pun_config['o_smtp_pass'])."\r\n");
       
   152 		server_parse($socket, '235');
       
   153 	}
       
   154 	else
       
   155 	{
       
   156 		fwrite($socket, 'HELO '.$smtp_host."\r\n");
       
   157 		server_parse($socket, '250');
       
   158 	}
       
   159 
       
   160 	fwrite($socket, 'MAIL FROM: <'.$pun_config['o_webmaster_email'].'>'."\r\n");
       
   161 	server_parse($socket, '250');
       
   162 
       
   163 	$to_header = 'To: ';
       
   164 
       
   165 	@reset($recipients);
       
   166 	while (list(, $email) = @each($recipients))
       
   167 	{
       
   168 		fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
       
   169 		server_parse($socket, '250');
       
   170 
       
   171 		$to_header .= '<'.$email.'>, ';
       
   172 	}
       
   173 
       
   174 	fwrite($socket, 'DATA'."\r\n");
       
   175 	server_parse($socket, '354');
       
   176 
       
   177 	fwrite($socket, 'Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n");
       
   178 
       
   179 	fwrite($socket, '.'."\r\n");
       
   180 	server_parse($socket, '250');
       
   181 
       
   182 	fwrite($socket, 'QUIT'."\r\n");
       
   183 	fclose($socket);
       
   184 
       
   185 	return true;
       
   186 }