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