1<?php
2/***************************************************************************
3                                emailer.php
4                             -------------------
5    begin                : Sunday Aug. 12, 2001
6    copyright            : (C) 2001 The phpBB Group
7    email                : support@phpbb.com
8
9    $Id: emailer.php 5261 2005-10-05 17:42:04Z grahamje $
10
11***************************************************************************/
12
13/***************************************************************************
14 *
15 *   This program is free software; you can redistribute it and/or modify
16 *   it under the terms of the GNU General Public License as published by
17 *   the Free Software Foundation; either version 2 of the License, or
18 *   (at your option) any later version.
19 *
20 ***************************************************************************/
21
22//
23// The emailer class has support for attaching files, that isn't implemented
24// in the 2.0 release but we can probable find some way of using it in a future
25// release
26//
27class emailer
28{
29	var $msg, $subject, $extra_headers;
30	var $addresses, $reply_to, $from;
31	var $use_smtp;
32
33	var $tpl_msg = array();
34
35	function emailer($use_smtp)
36	{
37		$this->reset();
38		$this->use_smtp = $use_smtp;
39		$this->reply_to = $this->from = '';
40	}
41
42	// Resets all the data (address, template file, etc etc to default
43	function reset()
44	{
45		$this->addresses = array();
46		$this->vars = $this->msg = $this->extra_headers = '';
47	}
48
49	// Sets an email address to send to
50	function email_address($address)
51	{
52		$this->addresses['to'] = trim($address);
53	}
54
55	function cc($address)
56	{
57		$this->addresses['cc'][] = trim($address);
58	}
59
60	function bcc($address)
61	{
62		$this->addresses['bcc'][] = trim($address);
63	}
64
65	function replyto($address)
66	{
67		$this->reply_to = trim($address);
68	}
69
70	function from($address)
71	{
72		$this->from = trim($address);
73	}
74
75	// set up subject for mail
76	function set_subject($subject = '')
77	{
78		$this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
79	}
80
81	// set up extra mail headers
82	function extra_headers($headers)
83	{
84		$this->extra_headers .= trim($headers) . "\n";
85	}
86
87	function use_template($template_file, $template_lang = '')
88	{
89		global $board_config, $phpbb_root_path;
90
91		if (trim($template_file) == '')
92		{
93			message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
94		}
95
96		if (trim($template_lang) == '')
97		{
98			$template_lang = $board_config['default_lang'];
99		}
100
101		if (empty($this->tpl_msg[$template_lang . $template_file]))
102		{
103			$tpl_file = $phpbb_root_path . 'language/lang_' . $template_lang . '/email/' . $template_file . '.tpl';
104
105			if (!@file_exists(@phpbb_realpath($tpl_file)))
106			{
107				$tpl_file = $phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/email/' . $template_file . '.tpl';
108
109				if (!@file_exists(@phpbb_realpath($tpl_file)))
110				{
111					message_die(GENERAL_ERROR, 'Could not find email template file :: ' . $template_file, '', __LINE__, __FILE__);
112				}
113			}
114
115			if (!($fd = @fopen($tpl_file, 'r')))
116			{
117				message_die(GENERAL_ERROR, 'Failed opening template file :: ' . $tpl_file, '', __LINE__, __FILE__);
118			}
119
120			$this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
121			fclose($fd);
122		}
123
124		$this->msg = $this->tpl_msg[$template_lang . $template_file];
125
126		return true;
127	}
128
129	// assign variables
130	function assign_vars($vars)
131	{
132		$this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
133	}
134
135	// Send the mail out to the recipients set previously in var $this->address
136	function send()
137	{
138		global $board_config, $lang, $phpEx, $phpbb_root_path, $db;
139
140    	// Escape all quotes, else the eval will fail.
141		$this->msg = str_replace ("'", "\'", $this->msg);
142		$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
143
144		// Set vars
145		reset ($this->vars);
146		while (list($key, $val) = each($this->vars))
147		{
148			$$key = $val;
149		}
150
151		eval("\$this->msg = '$this->msg';");
152
153		// Clear vars
154		reset ($this->vars);
155		while (list($key, $val) = each($this->vars))
156		{
157			unset($$key);
158		}
159
160		// We now try and pull a subject from the email body ... if it exists,
161		// do this here because the subject may contain a variable
162		$drop_header = '';
163		$match = array();
164		if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
165		{
166			$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
167			$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
168		}
169		else
170		{
171			$this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
172		}
173
174		if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
175		{
176			$this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
177			$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
178		}
179		else
180		{
181			$this->encoding = trim($lang['ENCODING']);
182		}
183
184		if ($drop_header != '')
185		{
186			$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
187		}
188
189		$to = $this->addresses['to'];
190
191		$cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
192		$bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
193
194		// Build header
195		$this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '')  . (($bcc != '') ? "Bcc: $bcc\n" : '');
196
197		// Send message ... removed $this->encode() from subject for time being
198		if ( $this->use_smtp )
199		{
200			if ( !defined('SMTP_INCLUDED') )
201			{
202				include($phpbb_root_path . 'includes/smtp.' . $phpEx);
203			}
204
205			$result = smtpmail($to, $this->subject, $this->msg, $this->extra_headers);
206		}
207		else
208		{
209			$empty_to_header = ($to == '') ? TRUE : FALSE;
210			$to = ($to == '') ? (($board_config['sendmail_fix']) ? ' ' : 'Undisclosed-recipients:;') : $to;
211
212			$result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
213
214			if (!$result && !$board_config['sendmail_fix'] && $empty_to_header)
215			{
216				$to = ' ';
217
218				$sql = "UPDATE " . CONFIG_TABLE . "
219					SET config_value = '1'
220					WHERE config_name = 'sendmail_fix'";
221				if (!$db->sql_query($sql))
222				{
223					message_die(GENERAL_ERROR, 'Unable to update config table', '', __LINE__, __FILE__, $sql);
224				}
225
226				$board_config['sendmail_fix'] = 1;
227				$result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
228			}
229		}
230
231		// Did it work?
232		if (!$result)
233		{
234			message_die(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result, '', __LINE__, __FILE__);
235		}
236
237		return true;
238	}
239
240	// Encodes the given string for proper display for this encoding ... nabbed
241	// from php.net and modified. There is an alternative encoding method which
242	// may produce lesd output but it's questionable as to its worth in this
243	// scenario IMO
244	function encode($str)
245	{
246		if ($this->encoding == '')
247		{
248			return $str;
249		}
250
251		// define start delimimter, end delimiter and spacer
252		$end = "?=";
253		$start = "=?$this->encoding?B?";
254		$spacer = "$end\r\n $start";
255
256		// determine length of encoded text within chunks and ensure length is even
257		$length = 75 - strlen($start) - strlen($end);
258		$length = floor($length / 2) * 2;
259
260		// encode the string and split it into chunks with spacers after each chunk
261		$str = chunk_split(base64_encode($str), $length, $spacer);
262
263		// remove trailing spacer and add start and end delimiters
264		$str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str);
265
266		return $start . $str . $end;
267	}
268
269	//
270	// Attach files via MIME.
271	//
272	function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
273	{
274		global $lang;
275		$mime_boundary = "--==================_846811060==_";
276
277		$this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->msg;
278
279		if ($mime_filename)
280		{
281			$filename = $mime_filename;
282			$encoded = $this->encode_file($filename);
283		}
284
285		$fd = fopen($filename, "r");
286		$contents = fread($fd, filesize($filename));
287
288		$this->mimeOut = "--" . $mime_boundary . "\n";
289		$this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
290		$this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
291		$this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
292
293		if ( $mimetype == "message/rfc822" )
294		{
295			$this->mimeOut .= "From: ".$szFromAddress."\n";
296			$this->mimeOut .= "To: ".$this->emailAddress."\n";
297			$this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
298			$this->mimeOut .= "Reply-To:".$szFromAddress."\n";
299			$this->mimeOut .= "Subject: ".$this->mailSubject."\n";
300			$this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
301			$this->mimeOut .= "MIME-Version: 1.0\n";
302		}
303
304		$this->mimeOut .= $contents."\n";
305		$this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
306
307		return $out;
308		// added -- to notify email client attachment is done
309	}
310
311	function getMimeHeaders($filename, $mime_filename="")
312	{
313		$mime_boundary = "--==================_846811060==_";
314
315		if ($mime_filename)
316		{
317			$filename = $mime_filename;
318		}
319
320		$out = "MIME-Version: 1.0\n";
321		$out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
322		$out .= "This message is in MIME format. Since your mail reader does not understand\n";
323		$out .= "this format, some or all of this message may not be legible.";
324
325		return $out;
326	}
327
328	//
329   // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
330	//
331	function myChunkSplit($str)
332	{
333		$stmp = $str;
334		$len = strlen($stmp);
335		$out = "";
336
337		while ($len > 0)
338		{
339			if ($len >= 76)
340			{
341				$out .= substr($stmp, 0, 76) . "\r\n";
342				$stmp = substr($stmp, 76);
343				$len = $len - 76;
344			}
345			else
346			{
347				$out .= $stmp . "\r\n";
348				$stmp = "";
349				$len = 0;
350			}
351		}
352		return $out;
353	}
354
355	//
356   // Split the specified file up into a string and return it
357	//
358	function encode_file($sourcefile)
359	{
360		if (is_readable(phpbb_realpath($sourcefile)))
361		{
362			$fd = fopen($sourcefile, "r");
363			$contents = fread($fd, filesize($sourcefile));
364	      $encoded = $this->myChunkSplit(base64_encode($contents));
365	      fclose($fd);
366		}
367
368		return $encoded;
369	}
370
371} // class emailer
372
373?>