Blame Identity/Webenv/App/phpBB/3.0.4/includes/functions_messenger.php

f2e824
f2e824
/**
f2e824
*
f2e824
* @package phpBB3
f2e824
* @version $Id: functions_messenger.php 9078 2008-11-22 19:55:00Z acydburn $
f2e824
* @copyright (c) 2005 phpBB Group
f2e824
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
f2e824
*
f2e824
*/
f2e824
f2e824
/**
f2e824
* @ignore
f2e824
*/
f2e824
if (!defined('IN_PHPBB'))
f2e824
{
f2e824
	exit;
f2e824
}
f2e824
f2e824
/**
f2e824
* Messenger
f2e824
* @package phpBB3
f2e824
*/
f2e824
class messenger
f2e824
{
f2e824
	var $vars, $msg, $extra_headers, $replyto, $from, $subject;
f2e824
	var $addresses = array();
f2e824
f2e824
	var $mail_priority = MAIL_NORMAL_PRIORITY;
f2e824
	var $use_queue = true;
f2e824
	var $tpl_msg = array();
f2e824
f2e824
	/**
f2e824
	* Constructor
f2e824
	*/
f2e824
	function messenger($use_queue = true)
f2e824
	{
f2e824
		global $config;
f2e824
f2e824
		$this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
f2e824
		$this->subject = '';
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Resets all the data (address, template file, etc etc) to default
f2e824
	*/
f2e824
	function reset()
f2e824
	{
f2e824
		$this->addresses = $this->extra_headers = array();
f2e824
		$this->vars = $this->msg = $this->replyto = $this->from = '';
f2e824
		$this->mail_priority = MAIL_NORMAL_PRIORITY;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Sets an email address to send to
f2e824
	*/
f2e824
	function to($address, $realname = '')
f2e824
	{
f2e824
		global $config;
f2e824
f2e824
		$pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
f2e824
f2e824
		$this->addresses['to'][$pos]['email'] = trim($address);
f2e824
f2e824
		// If empty sendmail_path on windows, PHP changes the to line
f2e824
		if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
f2e824
		{
f2e824
			$this->addresses['to'][$pos]['name'] = '';
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$this->addresses['to'][$pos]['name'] = trim($realname);
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Sets an cc address to send to
f2e824
	*/
f2e824
	function cc($address, $realname = '')
f2e824
	{
f2e824
		$pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
f2e824
		$this->addresses['cc'][$pos]['email'] = trim($address);
f2e824
		$this->addresses['cc'][$pos]['name'] = trim($realname);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Sets an bcc address to send to
f2e824
	*/
f2e824
	function bcc($address, $realname = '')
f2e824
	{
f2e824
		$pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
f2e824
		$this->addresses['bcc'][$pos]['email'] = trim($address);
f2e824
		$this->addresses['bcc'][$pos]['name'] = trim($realname);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Sets a im contact to send to
f2e824
	*/
f2e824
	function im($address, $realname = '')
f2e824
	{
f2e824
		// IM-Addresses could be empty
f2e824
		if (!$address)
f2e824
		{
f2e824
			return;
f2e824
		}
f2e824
f2e824
		$pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
f2e824
		$this->addresses['im'][$pos]['uid'] = trim($address);
f2e824
		$this->addresses['im'][$pos]['name'] = trim($realname);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Set the reply to address
f2e824
	*/
f2e824
	function replyto($address)
f2e824
	{
f2e824
		$this->replyto = trim($address);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Set the from address
f2e824
	*/
f2e824
	function from($address)
f2e824
	{
f2e824
		$this->from = trim($address);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* set up subject for mail
f2e824
	*/
f2e824
	function subject($subject = '')
f2e824
	{
f2e824
		$this->subject = trim($subject);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* set up extra mail headers
f2e824
	*/
f2e824
	function headers($headers)
f2e824
	{
f2e824
		$this->extra_headers[] = trim($headers);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Set the email priority
f2e824
	*/
f2e824
	function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
f2e824
	{
f2e824
		$this->mail_priority = $priority;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Set email template to use
f2e824
	*/
f2e824
	function template($template_file, $template_lang = '')
f2e824
	{
f2e824
		global $config, $phpbb_root_path;
f2e824
f2e824
		if (!trim($template_file))
f2e824
		{
f2e824
			trigger_error('No template file set', E_USER_ERROR);
f2e824
		}
f2e824
f2e824
		if (!trim($template_lang))
f2e824
		{
f2e824
			$template_lang = basename($config['default_lang']);
f2e824
		}
f2e824
f2e824
		if (empty($this->tpl_msg[$template_lang . $template_file]))
f2e824
		{
f2e824
			$tpl_file = "{$phpbb_root_path}language/$template_lang/email/$template_file.txt";
f2e824
f2e824
			if (!file_exists($tpl_file))
f2e824
			{
f2e824
				trigger_error("Could not find email template file [ $tpl_file ]", E_USER_ERROR);
f2e824
			}
f2e824
f2e824
			if (($data = @file_get_contents($tpl_file)) === false)
f2e824
			{
f2e824
				trigger_error("Failed opening template file [ $tpl_file ]", E_USER_ERROR);
f2e824
			}
f2e824
f2e824
			$this->tpl_msg[$template_lang . $template_file] = $data;
f2e824
		}
f2e824
f2e824
		$this->msg = $this->tpl_msg[$template_lang . $template_file];
f2e824
f2e824
		return true;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* assign variables to email template
f2e824
	*/
f2e824
	function assign_vars($vars)
f2e824
	{
f2e824
		$this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Send the mail out to the recipients set previously in var $this->addresses
f2e824
	*/
f2e824
	function send($method = NOTIFY_EMAIL, $break = false)
f2e824
	{
f2e824
		global $config, $user;
f2e824
f2e824
		// We add some standard variables we always use, no need to specify them always
f2e824
		$this->vars['U_BOARD'] = (!isset($this->vars['U_BOARD'])) ? generate_board_url() : $this->vars['U_BOARD'];
f2e824
		$this->vars['EMAIL_SIG'] = (!isset($this->vars['EMAIL_SIG'])) ? str_replace('
', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])) : $this->vars['EMAIL_SIG'];
f2e824
		$this->vars['SITENAME'] = (!isset($this->vars['SITENAME'])) ? htmlspecialchars_decode($config['sitename']) : $this->vars['SITENAME'];
f2e824
f2e824
		// Escape all quotes, else the eval will fail.
f2e824
		$this->msg = str_replace ("'", "\'", $this->msg);
f2e824
		$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->msg);
f2e824
f2e824
		eval("\$this->msg = '$this->msg';");
f2e824
f2e824
		// We now try and pull a subject from the email body ... if it exists,
f2e824
		// do this here because the subject may contain a variable
f2e824
		$drop_header = '';
f2e824
		$match = array();
f2e824
		if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
f2e824
		{
f2e824
			$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
f2e824
			$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
f2e824
		}
f2e824
f2e824
		if ($drop_header)
f2e824
		{
f2e824
			$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
f2e824
		}
f2e824
f2e824
		if ($break)
f2e824
		{
f2e824
			return true;
f2e824
		}
f2e824
f2e824
		switch ($method)
f2e824
		{
f2e824
			case NOTIFY_EMAIL:
f2e824
				$result = $this->msg_email();
f2e824
			break;
f2e824
f2e824
			case NOTIFY_IM:
f2e824
				$result = $this->msg_jabber();
f2e824
			break;
f2e824
f2e824
			case NOTIFY_BOTH:
f2e824
				$result = $this->msg_email();
f2e824
				$this->msg_jabber();
f2e824
			break;
f2e824
		}
f2e824
f2e824
		$this->reset();
f2e824
		return $result;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Add error message to log
f2e824
	*/
f2e824
	function error($type, $msg)
f2e824
	{
f2e824
		global $user, $phpEx, $phpbb_root_path, $config;
f2e824
f2e824
		// Session doesn't exist, create it
f2e824
		if (!isset($user->session_id) || $user->session_id === '')
f2e824
		{
f2e824
			$user->session_begin();
f2e824
		}
f2e824
f2e824
		$calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
f2e824
f2e824
		$message = '';
f2e824
		switch ($type)
f2e824
		{
f2e824
			case 'EMAIL':
f2e824
				$message = 'EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '';
f2e824
			break;
f2e824
f2e824
			default:
f2e824
				$message = "$type";
f2e824
			break;
f2e824
		}
f2e824
f2e824
		$message .= '
' . htmlspecialchars($calling_page) . '

' . $msg . '
';
f2e824
		add_log('critical', 'LOG_ERROR_' . $type, $message);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Save to queue
f2e824
	*/
f2e824
	function save_queue()
f2e824
	{
f2e824
		global $config;
f2e824
f2e824
		if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
f2e824
		{
f2e824
			$this->queue->save();
f2e824
			return;
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Return email header
f2e824
	*/
f2e824
	function build_header($to, $cc, $bcc)
f2e824
	{
f2e824
		global $config;
f2e824
f2e824
		$headers = array();
f2e824
f2e824
		$headers[] = 'From: ' . $this->from;
f2e824
f2e824
		if ($cc)
f2e824
		{
f2e824
			$headers[] = 'Cc: ' . $cc;
f2e824
		}
f2e824
f2e824
		if ($bcc)
f2e824
		{
f2e824
			$headers[] = 'Bcc: ' . $bcc;
f2e824
		}
f2e824
f2e824
		$headers[] = 'Reply-To: ' . $this->replyto;
f2e824
		$headers[] = 'Return-Path: <' . $config['board_email'] . '>';
f2e824
		$headers[] = 'Sender: <' . $config['board_email'] . '>';
f2e824
		$headers[] = 'MIME-Version: 1.0';
f2e824
		$headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
f2e824
		$headers[] = 'Date: ' . date('r', time());
f2e824
		$headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
f2e824
		$headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
f2e824
f2e824
		$headers[] = 'X-Priority: ' . $this->mail_priority;
f2e824
		$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
f2e824
		$headers[] = 'X-Mailer: PhpBB3';
f2e824
		$headers[] = 'X-MimeOLE: phpBB3';
f2e824
		$headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
f2e824
f2e824
		// We use \n here instead of \r\n because our smtp mailer is adjusting it to \r\n automatically, whereby the php mail function only works
f2e824
		// if using \n.
f2e824
f2e824
		if (sizeof($this->extra_headers))
f2e824
		{
f2e824
			$headers[] = implode("\n", $this->extra_headers);
f2e824
		}
f2e824
f2e824
		return implode("\n", $headers);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Send out emails
f2e824
	*/
f2e824
	function msg_email()
f2e824
	{
f2e824
		global $config, $user;
f2e824
f2e824
		if (empty($config['email_enable']))
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
f2e824
		$use_queue = false;
f2e824
		if ($config['email_package_size'] && $this->use_queue)
f2e824
		{
f2e824
			if (empty($this->queue))
f2e824
			{
f2e824
				$this->queue = new queue();
f2e824
				$this->queue->init('email', $config['email_package_size']);
f2e824
			}
f2e824
			$use_queue = true;
f2e824
		}
f2e824
f2e824
		if (empty($this->replyto))
f2e824
		{
f2e824
			$this->replyto = '<' . $config['board_contact'] . '>';
f2e824
		}
f2e824
f2e824
		if (empty($this->from))
f2e824
		{
f2e824
			$this->from = '<' . $config['board_contact'] . '>';
f2e824
		}
f2e824
f2e824
		// Build to, cc and bcc strings
f2e824
		$to = $cc = $bcc = '';
f2e824
		foreach ($this->addresses as $type => $address_ary)
f2e824
		{
f2e824
			if ($type == 'im')
f2e824
			{
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			foreach ($address_ary as $which_ary)
f2e824
			{
f2e824
				$$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name']) . '" <' . $which_ary['email'] . '>' : $which_ary['email']);
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Build header
f2e824
		$headers = $this->build_header($to, $cc, $bcc);
f2e824
f2e824
		// Send message ...
f2e824
		if (!$use_queue)
f2e824
		{
f2e824
			$mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
f2e824
			$err_msg = '';
f2e824
f2e824
			if ($config['smtp_delivery'])
f2e824
			{
f2e824
				$result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				ob_start();
f2e824
				$result = $config['email_function_name']($mail_to, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers);
f2e824
				$err_msg = ob_get_clean();
f2e824
			}
f2e824
f2e824
			if (!$result)
f2e824
			{
f2e824
				$this->error('EMAIL', $err_msg);
f2e824
				return false;
f2e824
			}
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$this->queue->put('email', array(
f2e824
				'to'			=> $to,
f2e824
				'addresses'		=> $this->addresses,
f2e824
				'subject'		=> $this->subject,
f2e824
				'msg'			=> $this->msg,
f2e824
				'headers'		=> $headers)
f2e824
			);
f2e824
		}
f2e824
f2e824
		return true;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Send jabber message out
f2e824
	*/
f2e824
	function msg_jabber()
f2e824
	{
f2e824
		global $config, $db, $user, $phpbb_root_path, $phpEx;
f2e824
f2e824
		if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
f2e824
		if (empty($this->addresses['im']))
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
f2e824
		$use_queue = false;
f2e824
		if ($config['jab_package_size'] && $this->use_queue)
f2e824
		{
f2e824
			if (empty($this->queue))
f2e824
			{
f2e824
				$this->queue = new queue();
f2e824
				$this->queue->init('jabber', $config['jab_package_size']);
f2e824
			}
f2e824
			$use_queue = true;
f2e824
		}
f2e824
f2e824
		$addresses = array();
f2e824
		foreach ($this->addresses['im'] as $type => $uid_ary)
f2e824
		{
f2e824
			$addresses[] = $uid_ary['uid'];
f2e824
		}
f2e824
		$addresses = array_unique($addresses);
f2e824
f2e824
		if (!$use_queue)
f2e824
		{
f2e824
			include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
f2e824
			$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
f2e824
f2e824
			if (!$this->jabber->connect())
f2e824
			{
f2e824
				$this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '
' . $this->jabber->get_log());
f2e824
				return false;
f2e824
			}
f2e824
f2e824
			if (!$this->jabber->login())
f2e824
			{
f2e824
				$this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '
' . $this->jabber->get_log());
f2e824
				return false;
f2e824
			}
f2e824
f2e824
			foreach ($addresses as $address)
f2e824
			{
f2e824
				$this->jabber->send_message($address, $this->msg, $this->subject);
f2e824
			}
f2e824
f2e824
			$this->jabber->disconnect();
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$this->queue->put('jabber', array(
f2e824
				'addresses'		=> $addresses,
f2e824
				'subject'		=> $this->subject,
f2e824
				'msg'			=> $this->msg)
f2e824
			);
f2e824
		}
f2e824
		unset($addresses);
f2e824
		return true;
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* handling email and jabber queue
f2e824
* @package phpBB3
f2e824
*/
f2e824
class queue
f2e824
{
f2e824
	var $data = array();
f2e824
	var $queue_data = array();
f2e824
	var $package_size = 0;
f2e824
	var $cache_file = '';
f2e824
f2e824
	/**
f2e824
	* constructor
f2e824
	*/
f2e824
	function queue()
f2e824
	{
f2e824
		global $phpEx, $phpbb_root_path;
f2e824
f2e824
		$this->data = array();
f2e824
		$this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Init a queue object
f2e824
	*/
f2e824
	function init($object, $package_size)
f2e824
	{
f2e824
		$this->data[$object] = array();
f2e824
		$this->data[$object]['package_size'] = $package_size;
f2e824
		$this->data[$object]['data'] = array();
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Put object in queue
f2e824
	*/
f2e824
	function put($object, $scope)
f2e824
	{
f2e824
		$this->data[$object]['data'][] = $scope;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Process queue
f2e824
	* Using lock file
f2e824
	*/
f2e824
	function process()
f2e824
	{
f2e824
		global $db, $config, $phpEx, $phpbb_root_path, $user;
f2e824
f2e824
		set_config('last_queue_run', time(), true);
f2e824
f2e824
		// Delete stale lock file
f2e824
		if (file_exists($this->cache_file . '.lock') && !file_exists($this->cache_file))
f2e824
		{
f2e824
			@unlink($this->cache_file . '.lock');
f2e824
			return;
f2e824
		}
f2e824
f2e824
		if (!file_exists($this->cache_file) || (file_exists($this->cache_file . '.lock') && filemtime($this->cache_file) > time() - $config['queue_interval']))
f2e824
		{
f2e824
			return;
f2e824
		}
f2e824
f2e824
		$fp = @fopen($this->cache_file . '.lock', 'wb');
f2e824
		fclose($fp);
f2e824
		@chmod($this->cache_file . '.lock', 0777);
f2e824
f2e824
		include($this->cache_file);
f2e824
f2e824
		foreach ($this->queue_data as $object => $data_ary)
f2e824
		{
f2e824
			@set_time_limit(0);
f2e824
f2e824
			if (!isset($data_ary['package_size']))
f2e824
			{
f2e824
				$data_ary['package_size'] = 0;
f2e824
			}
f2e824
f2e824
			$package_size = $data_ary['package_size'];
f2e824
			$num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
f2e824
f2e824
			// If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs...
f2e824
			if (sizeof($data_ary['data']) > $package_size * 2.5)
f2e824
			{
f2e824
				$num_items = sizeof($data_ary['data']);
f2e824
			}
f2e824
f2e824
			switch ($object)
f2e824
			{
f2e824
				case 'email':
f2e824
					// Delete the email queued objects if mailing is disabled
f2e824
					if (!$config['email_enable'])
f2e824
					{
f2e824
						unset($this->queue_data['email']);
f2e824
						continue 2;
f2e824
					}
f2e824
				break;
f2e824
f2e824
				case 'jabber':
f2e824
					if (!$config['jab_enable'])
f2e824
					{
f2e824
						unset($this->queue_data['jabber']);
f2e824
						continue 2;
f2e824
					}
f2e824
f2e824
					include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
f2e824
					$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
f2e824
f2e824
					if (!$this->jabber->connect())
f2e824
					{
f2e824
						messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
f2e824
						continue 2;
f2e824
					}
f2e824
f2e824
					if (!$this->jabber->login())
f2e824
					{
f2e824
						messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
f2e824
						continue 2;
f2e824
					}
f2e824
f2e824
				break;
f2e824
f2e824
				default:
f2e824
					return;
f2e824
			}
f2e824
f2e824
			for ($i = 0; $i < $num_items; $i++)
f2e824
			{
f2e824
				// Make variables available...
f2e824
				extract(array_shift($this->queue_data[$object]['data']));
f2e824
f2e824
				switch ($object)
f2e824
				{
f2e824
					case 'email':
f2e824
						$err_msg = '';
f2e824
						$to = (!$to) ? 'undisclosed-recipients:;' : $to;
f2e824
f2e824
						if ($config['smtp_delivery'])
f2e824
						{
f2e824
							$result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
f2e824
						}
f2e824
						else
f2e824
						{
f2e824
							ob_start();
f2e824
							$result = $config['email_function_name']($to, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers);
f2e824
							$err_msg = ob_get_clean();
f2e824
						}
f2e824
f2e824
						if (!$result)
f2e824
						{
f2e824
							@unlink($this->cache_file . '.lock');
f2e824
f2e824
							messenger::error('EMAIL', $err_msg);
f2e824
							continue 2;
f2e824
						}
f2e824
					break;
f2e824
f2e824
					case 'jabber':
f2e824
						foreach ($addresses as $address)
f2e824
						{
f2e824
							if ($this->jabber->send_message($address, $msg, $subject) === false)
f2e824
							{
f2e824
								messenger::error('JABBER', $this->jabber->get_log());
f2e824
								continue 3;
f2e824
							}
f2e824
						}
f2e824
					break;
f2e824
				}
f2e824
			}
f2e824
f2e824
			// No more data for this object? Unset it
f2e824
			if (!sizeof($this->queue_data[$object]['data']))
f2e824
			{
f2e824
				unset($this->queue_data[$object]);
f2e824
			}
f2e824
f2e824
			// Post-object processing
f2e824
			switch ($object)
f2e824
			{
f2e824
				case 'jabber':
f2e824
					// Hang about a couple of secs to ensure the messages are
f2e824
					// handled, then disconnect
f2e824
					$this->jabber->disconnect();
f2e824
				break;
f2e824
			}
f2e824
		}
f2e824
f2e824
		if (!sizeof($this->queue_data))
f2e824
		{
f2e824
			@unlink($this->cache_file);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			if ($fp = @fopen($this->cache_file, 'wb'))
f2e824
			{
f2e824
				@flock($fp, LOCK_EX);
f2e824
				fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
f2e824
				@flock($fp, LOCK_UN);
f2e824
				fclose($fp);
f2e824
f2e824
				phpbb_chmod($this->cache_file, CHMOD_WRITE);
f2e824
			}
f2e824
		}
f2e824
f2e824
		@unlink($this->cache_file . '.lock');
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Save queue
f2e824
	*/
f2e824
	function save()
f2e824
	{
f2e824
		if (!sizeof($this->data))
f2e824
		{
f2e824
			return;
f2e824
		}
f2e824
f2e824
		if (file_exists($this->cache_file))
f2e824
		{
f2e824
			include($this->cache_file);
f2e824
f2e824
			foreach ($this->queue_data as $object => $data_ary)
f2e824
			{
f2e824
				if (isset($this->data[$object]) && sizeof($this->data[$object]))
f2e824
				{
f2e824
					$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$this->data[$object]['data'] = $data_ary['data'];
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		if ($fp = @fopen($this->cache_file, 'w'))
f2e824
		{
f2e824
			@flock($fp, LOCK_EX);
f2e824
			fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
f2e824
			@flock($fp, LOCK_UN);
f2e824
			fclose($fp);
f2e824
f2e824
			phpbb_chmod($this->cache_file, CHMOD_WRITE);
f2e824
		}
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* Replacement or substitute for PHP's mail command
f2e824
*/
f2e824
function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '')
f2e824
{
f2e824
	global $config, $user;
f2e824
f2e824
	// Fix any bare linefeeds in the message to make it RFC821 Compliant.
f2e824
	$message = preg_replace("#(?
f2e824
f2e824
	if ($headers != '')
f2e824
	{
f2e824
		if (is_array($headers))
f2e824
		{
f2e824
			$headers = (sizeof($headers) > 1) ? join("\n", $headers) : $headers[0];
f2e824
		}
f2e824
		$headers = chop($headers);
f2e824
f2e824
		// Make sure there are no bare linefeeds in the headers
f2e824
		$headers = preg_replace('#(?
f2e824
f2e824
		// Ok this is rather confusing all things considered,
f2e824
		// but we have to grab bcc and cc headers and treat them differently
f2e824
		// Something we really didn't take into consideration originally
f2e824
		$header_array = explode("\r\n", $headers);
f2e824
		$headers = '';
f2e824
f2e824
		foreach ($header_array as $header)
f2e824
		{
f2e824
			if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
f2e824
			{
f2e824
				$header = '';
f2e824
			}
f2e824
			$headers .= ($header != '') ? $header . "\r\n" : '';
f2e824
		}
f2e824
f2e824
		$headers = chop($headers);
f2e824
	}
f2e824
f2e824
	if (trim($subject) == '')
f2e824
	{
f2e824
		$err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	if (trim($message) == '')
f2e824
	{
f2e824
		$err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	$mail_rcpt = $mail_to = $mail_cc = array();
f2e824
f2e824
	// Build correct addresses for RCPT TO command and the client side display (TO, CC)
f2e824
	if (isset($addresses['to']) && sizeof($addresses['to']))
f2e824
	{
f2e824
		foreach ($addresses['to'] as $which_ary)
f2e824
		{
f2e824
			$mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
f2e824
			$mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
f2e824
		}
f2e824
	}
f2e824
f2e824
	if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
f2e824
	{
f2e824
		foreach ($addresses['bcc'] as $which_ary)
f2e824
		{
f2e824
			$mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
f2e824
		}
f2e824
	}
f2e824
f2e824
	if (isset($addresses['cc']) && sizeof($addresses['cc']))
f2e824
	{
f2e824
		foreach ($addresses['cc'] as $which_ary)
f2e824
		{
f2e824
			$mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
f2e824
			$mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
f2e824
		}
f2e824
	}
f2e824
f2e824
	$smtp = new smtp_class();
f2e824
f2e824
	$errno = 0;
f2e824
	$errstr = '';
f2e824
f2e824
	$smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
f2e824
f2e824
	// Ok we have error checked as much as we can to this point let's get on it already.
f2e824
	ob_start();
f2e824
	$smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
f2e824
	$error_contents = ob_get_clean();
f2e824
f2e824
	if (!$smtp->socket)
f2e824
	{
f2e824
		if ($errstr)
f2e824
		{
f2e824
			$errstr = utf8_convert_message($errstr);
f2e824
		}
f2e824
f2e824
		$err_msg = (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr";
f2e824
		$err_msg .= ($error_contents) ? '

' . htmlspecialchars($error_contents) : '';
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Wait for reply
f2e824
	if ($err_msg = $smtp->server_parse('220', __LINE__))
f2e824
	{
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Let me in. This function handles the complete authentication process
f2e824
	if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
f2e824
	{
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// From this point onward most server response codes should be 250
f2e824
	// Specify who the mail is from....
f2e824
	$smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
f2e824
	if ($err_msg = $smtp->server_parse('250', __LINE__))
f2e824
	{
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Specify each user to send to and build to header.
f2e824
	$to_header = implode(', ', $mail_to);
f2e824
	$cc_header = implode(', ', $mail_cc);
f2e824
f2e824
	// Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
f2e824
	$rcpt = false;
f2e824
	foreach ($mail_rcpt as $type => $mail_to_addresses)
f2e824
	{
f2e824
		foreach ($mail_to_addresses as $mail_to_address)
f2e824
		{
f2e824
			// Add an additional bit of error checking to the To field.
f2e824
			if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
f2e824
			{
f2e824
				$smtp->server_send("RCPT TO:$mail_to_address");
f2e824
				if ($err_msg = $smtp->server_parse('250', __LINE__))
f2e824
				{
f2e824
					// We continue... if users are not resolved we do not care
f2e824
					if ($smtp->numeric_response_code != 550)
f2e824
					{
f2e824
						$smtp->close_session($err_msg);
f2e824
						return false;
f2e824
					}
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$rcpt = true;
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
	}
f2e824
f2e824
	// We try to send messages even if a few people do not seem to have valid email addresses, but if no one has, we have to exit here.
f2e824
	if (!$rcpt)
f2e824
	{
f2e824
		$user->session_begin();
f2e824
		$err_msg .= '

';
f2e824
		$err_msg .= (isset($user->lang['INVALID_EMAIL_LOG'])) ? sprintf($user->lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address)) : '' . htmlspecialchars($mail_to_address) . ' possibly an invalid email address?';
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Ok now we tell the server we are ready to start sending data
f2e824
	$smtp->server_send('DATA');
f2e824
f2e824
	// This is the last response code we look for until the end of the message.
f2e824
	if ($err_msg = $smtp->server_parse('354', __LINE__))
f2e824
	{
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Send the Subject Line...
f2e824
	$smtp->server_send("Subject: $subject");
f2e824
f2e824
	// Now the To Header.
f2e824
	$to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
f2e824
	$smtp->server_send("To: $to_header");
f2e824
f2e824
	// Now the CC Header.
f2e824
	if ($cc_header != '')
f2e824
	{
f2e824
		$smtp->server_send("CC: $cc_header");
f2e824
	}
f2e824
f2e824
	// Now any custom headers....
f2e824
	$smtp->server_send("$headers\r\n");
f2e824
f2e824
	// Ok now we are ready for the message...
f2e824
	$smtp->server_send($message);
f2e824
f2e824
	// Ok the all the ingredients are mixed in let's cook this puppy...
f2e824
	$smtp->server_send('.');
f2e824
	if ($err_msg = $smtp->server_parse('250', __LINE__))
f2e824
	{
f2e824
		$smtp->close_session($err_msg);
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	// Now tell the server we are done and close the socket...
f2e824
	$smtp->server_send('QUIT');
f2e824
	$smtp->close_session($err_msg);
f2e824
f2e824
	return true;
f2e824
}
f2e824
f2e824
/**
f2e824
* SMTP Class
f2e824
* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
f2e824
* See docs/AUTHORS for more details
f2e824
* @package phpBB3
f2e824
*/
f2e824
class smtp_class
f2e824
{
f2e824
	var $server_response = '';
f2e824
	var $socket = 0;
f2e824
	var $responses = array();
f2e824
	var $commands = array();
f2e824
	var $numeric_response_code = 0;
f2e824
f2e824
	var $backtrace = false;
f2e824
	var $backtrace_log = array();
f2e824
f2e824
	function smtp_class()
f2e824
	{
f2e824
		// Always create a backtrace for admins to identify SMTP problems
f2e824
		$this->backtrace = true;
f2e824
		$this->backtrace_log = array();
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Add backtrace message for debugging
f2e824
	*/
f2e824
	function add_backtrace($message)
f2e824
	{
f2e824
		if ($this->backtrace)
f2e824
		{
f2e824
			$this->backtrace_log[] = utf8_htmlspecialchars($message);
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Send command to smtp server
f2e824
	*/
f2e824
	function server_send($command, $private_info = false)
f2e824
	{
f2e824
		fputs($this->socket, $command . "\r\n");
f2e824
f2e824
		(!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
f2e824
f2e824
		// We could put additional code here
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* We use the line to give the support people an indication at which command the error occurred
f2e824
	*/
f2e824
	function server_parse($response, $line)
f2e824
	{
f2e824
		global $user;
f2e824
f2e824
		$this->server_response = '';
f2e824
		$this->responses = array();
f2e824
		$this->numeric_response_code = 0;
f2e824
f2e824
		while (substr($this->server_response, 3, 1) != ' ')
f2e824
		{
f2e824
			if (!($this->server_response = fgets($this->socket, 256)))
f2e824
			{
f2e824
				return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
f2e824
			}
f2e824
			$this->responses[] = substr(rtrim($this->server_response), 4);
f2e824
			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
f2e824
f2e824
			$this->add_backtrace("LINE: $line <- {$this->server_response}");
f2e824
		}
f2e824
f2e824
		if (!(substr($this->server_response, 0, 3) == $response))
f2e824
		{
f2e824
			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
f2e824
			return (isset($user->lang['EMAIL_SMTP_ERROR_RESPONSE'])) ? sprintf($user->lang['EMAIL_SMTP_ERROR_RESPONSE'], $line, $this->server_response) : "Ran into problems sending Mail at Line $line. Response: $this->server_response";
f2e824
		}
f2e824
f2e824
		return 0;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Close session
f2e824
	*/
f2e824
	function close_session(&$err_msg)
f2e824
	{
f2e824
		fclose($this->socket);
f2e824
f2e824
		if ($this->backtrace)
f2e824
		{
f2e824
			$message = '

Backtrace

' . implode('
', $this->backtrace_log) . '

';
f2e824
			$err_msg .= $message;
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Log into server and get possible auth codes if neccessary
f2e824
	*/
f2e824
	function log_into_server($hostname, $username, $password, $default_auth_method)
f2e824
	{
f2e824
		global $user;
f2e824
f2e824
		$err_msg = '';
f2e824
		$local_host = (function_exists('php_uname')) ? php_uname('n') : $user->host;
f2e824
f2e824
		// If we are authenticating through pop-before-smtp, we
f2e824
		// have to login ones before we get authenticated
f2e824
		// NOTE: on some configurations the time between an update of the auth database takes so
f2e824
		// long that the first email send does not work. This is not a biggie on a live board (only
f2e824
		// the install mail will most likely fail) - but on a dynamic ip connection this might produce
f2e824
		// severe problems and is not fixable!
f2e824
		if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password)
f2e824
		{
f2e824
			global $config;
f2e824
f2e824
			$errno = 0;
f2e824
			$errstr = '';
f2e824
f2e824
			$this->server_send("QUIT");
f2e824
			fclose($this->socket);
f2e824
f2e824
			$result = $this->pop_before_smtp($hostname, $username, $password);
f2e824
			$username = $password = $default_auth_method = '';
f2e824
f2e824
			// We need to close the previous session, else the server is not
f2e824
			// able to get our ip for matching...
f2e824
			if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10))
f2e824
			{
f2e824
				if ($errstr)
f2e824
				{
f2e824
					$errstr = utf8_convert_message($errstr);
f2e824
				}
f2e824
f2e824
				$err_msg = (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr";
f2e824
				return $err_msg;
f2e824
			}
f2e824
f2e824
			// Wait for reply
f2e824
			if ($err_msg = $this->server_parse('220', __LINE__))
f2e824
			{
f2e824
				$this->close_session($err_msg);
f2e824
				return $err_msg;
f2e824
			}
f2e824
		}
f2e824
f2e824
		// Try EHLO first
f2e824
		$this->server_send("EHLO {$local_host}");
f2e824
		if ($err_msg = $this->server_parse('250', __LINE__))
f2e824
		{
f2e824
			// a 503 response code means that we're already authenticated
f2e824
			if ($this->numeric_response_code == 503)
f2e824
			{
f2e824
				return false;
f2e824
			}
f2e824
f2e824
			// If EHLO fails, we try HELO
f2e824
			$this->server_send("HELO {$local_host}");
f2e824
			if ($err_msg = $this->server_parse('250', __LINE__))
f2e824
			{
f2e824
				return ($this->numeric_response_code == 503) ? false : $err_msg;
f2e824
			}
f2e824
		}
f2e824
f2e824
		foreach ($this->responses as $response)
f2e824
		{
f2e824
			$response = explode(' ', $response);
f2e824
			$response_code = $response[0];
f2e824
			unset($response[0]);
f2e824
			$this->commands[$response_code] = implode(' ', $response);
f2e824
		}
f2e824
f2e824
		// If we are not authenticated yet, something might be wrong if no username and passwd passed
f2e824
		if (!$username || !$password)
f2e824
		{
f2e824
			return false;
f2e824
		}
f2e824
f2e824
		if (!isset($this->commands['AUTH']))
f2e824
		{
f2e824
			return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication';
f2e824
		}
f2e824
f2e824
		// Get best authentication method
f2e824
		$available_methods = explode(' ', $this->commands['AUTH']);
f2e824
f2e824
		// Define the auth ordering if the default auth method was not found
f2e824
		$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5');
f2e824
		$method = '';
f2e824
f2e824
		if (in_array($default_auth_method, $available_methods))
f2e824
		{
f2e824
			$method = $default_auth_method;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			foreach ($auth_methods as $_method)
f2e824
			{
f2e824
				if (in_array($_method, $available_methods))
f2e824
				{
f2e824
					$method = $_method;
f2e824
					break;
f2e824
				}
f2e824
			}
f2e824
		}
f2e824
f2e824
		if (!$method)
f2e824
		{
f2e824
			return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods';
f2e824
		}
f2e824
f2e824
		$method = strtolower(str_replace('-', '_', $method));
f2e824
		return $this->$method($username, $password);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Pop before smtp authentication
f2e824
	*/
f2e824
	function pop_before_smtp($hostname, $username, $password)
f2e824
	{
f2e824
		global $user;
f2e824
f2e824
		if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10))
f2e824
		{
f2e824
			if ($errstr)
f2e824
			{
f2e824
				$errstr = utf8_convert_message($errstr);
f2e824
			}
f2e824
f2e824
			return (isset($user->lang['NO_CONNECT_TO_SMTP_HOST'])) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : $errno : $errstr";
f2e824
		}
f2e824
f2e824
		$this->server_send("USER $username", true);
f2e824
		if ($err_msg = $this->server_parse('+OK', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		$this->server_send("PASS $password", true);
f2e824
		if ($err_msg = $this->server_parse('+OK', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		$this->server_send('QUIT');
f2e824
		fclose($this->socket);
f2e824
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Plain authentication method
f2e824
	*/
f2e824
	function plain($username, $password)
f2e824
	{
f2e824
		$this->server_send('AUTH PLAIN');
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return ($this->numeric_response_code == 503) ? false : $err_msg;
f2e824
		}
f2e824
f2e824
		$base64_method_plain = base64_encode("\0" . $username . "\0" . $password);
f2e824
		$this->server_send($base64_method_plain, true);
f2e824
		if ($err_msg = $this->server_parse('235', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Login authentication method
f2e824
	*/
f2e824
	function login($username, $password)
f2e824
	{
f2e824
		$this->server_send('AUTH LOGIN');
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return ($this->numeric_response_code == 503) ? false : $err_msg;
f2e824
		}
f2e824
f2e824
		$this->server_send(base64_encode($username), true);
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		$this->server_send(base64_encode($password), true);
f2e824
		if ($err_msg = $this->server_parse('235', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* cram_md5 authentication method
f2e824
	*/
f2e824
	function cram_md5($username, $password)
f2e824
	{
f2e824
		$this->server_send('AUTH CRAM-MD5');
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return ($this->numeric_response_code == 503) ? false : $err_msg;
f2e824
		}
f2e824
f2e824
		$md5_challenge = base64_decode($this->responses[0]);
f2e824
		$password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password);
f2e824
		$md5_digest = md5((substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64)) . (pack('H32', md5((substr($password, 0, 64) ^ str_repeat(chr(0x36), 64)) . $md5_challenge))));
f2e824
f2e824
		$base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest);
f2e824
f2e824
		$this->server_send($base64_method_cram_md5, true);
f2e824
		if ($err_msg = $this->server_parse('235', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		return false;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* digest_md5 authentication method
f2e824
	* A real pain in the ***
f2e824
	*/
f2e824
	function digest_md5($username, $password)
f2e824
	{
f2e824
		global $config, $user;
f2e824
f2e824
		$this->server_send('AUTH DIGEST-MD5');
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return ($this->numeric_response_code == 503) ? false : $err_msg;
f2e824
		}
f2e824
f2e824
		$md5_challenge = base64_decode($this->responses[0]);
f2e824
f2e824
		// Parse the md5 challenge - from AUTH_SASL (PEAR)
f2e824
		$tokens = array();
f2e824
		while (preg_match('/^([a-z-]+)=("[^"]+(?
f2e824
		{
f2e824
			// Ignore these as per rfc2831
f2e824
			if ($matches[1] == 'opaque' || $matches[1] == 'domain')
f2e824
			{
f2e824
				$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
f2e824
				continue;
f2e824
			}
f2e824
f2e824
			// Allowed multiple "realm" and "auth-param"
f2e824
			if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param'))
f2e824
			{
f2e824
				if (is_array($tokens[$matches[1]]))
f2e824
				{
f2e824
					$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
f2e824
				}
f2e824
			}
f2e824
			else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure
f2e824
			{
f2e824
				$tokens = array();
f2e824
				break;
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
f2e824
			}
f2e824
f2e824
			// Remove the just parsed directive from the challenge
f2e824
			$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
f2e824
		}
f2e824
f2e824
		// Realm
f2e824
		if (empty($tokens['realm']))
f2e824
		{
f2e824
			$tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host;
f2e824
		}
f2e824
f2e824
		// Maxbuf
f2e824
		if (empty($tokens['maxbuf']))
f2e824
		{
f2e824
			$tokens['maxbuf'] = 65536;
f2e824
		}
f2e824
f2e824
		// Required: nonce, algorithm
f2e824
		if (empty($tokens['nonce']) || empty($tokens['algorithm']))
f2e824
		{
f2e824
			$tokens = array();
f2e824
		}
f2e824
		$md5_challenge = $tokens;
f2e824
f2e824
		if (!empty($md5_challenge))
f2e824
		{
f2e824
			$str = '';
f2e824
			for ($i = 0; $i < 32; $i++)
f2e824
			{
f2e824
				$str .= chr(mt_rand(0, 255));
f2e824
			}
f2e824
			$cnonce = base64_encode($str);
f2e824
f2e824
			$digest_uri = 'smtp/' . $config['smtp_host'];
f2e824
f2e824
			$auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce);
f2e824
			$auth_2 = 'AUTHENTICATE:' . $digest_uri;
f2e824
			$response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2)));
f2e824
f2e824
			$input_string = sprintf('username="%s",realm="%s",nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d', $username, $md5_challenge['realm'], $md5_challenge['nonce'], $cnonce, $digest_uri, $response_value, $md5_challenge['maxbuf']);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge';
f2e824
		}
f2e824
f2e824
		$base64_method_digest_md5 = base64_encode($input_string);
f2e824
		$this->server_send($base64_method_digest_md5, true);
f2e824
		if ($err_msg = $this->server_parse('334', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		$this->server_send(' ');
f2e824
		if ($err_msg = $this->server_parse('235', __LINE__))
f2e824
		{
f2e824
			return $err_msg;
f2e824
		}
f2e824
f2e824
		return false;
f2e824
	}
f2e824
}
f2e824
f2e824
/**
f2e824
* Encodes the given string for proper display in UTF-8.
f2e824
*
f2e824
* This version is using base64 encoded data. The downside of this
f2e824
* is if the mail client does not understand this encoding the user
f2e824
* is basically doomed with an unreadable subject.
f2e824
*
f2e824
* Please note that this version fully supports RFC 2045 section 6.8.
f2e824
*/
f2e824
function mail_encode($str)
f2e824
{
f2e824
	// define start delimimter, end delimiter and spacer
f2e824
	$start = "=?UTF-8?B?";
f2e824
	$end = "?=";
f2e824
	$spacer = $end . ' ' . $start;
f2e824
	$split_length = 64;
f2e824
f2e824
	$encoded_str = base64_encode($str);
f2e824
f2e824
	// If encoded string meets the limits, we just return with the correct data.
f2e824
	if (strlen($encoded_str) <= $split_length)
f2e824
	{
f2e824
		return $start . $encoded_str . $end;
f2e824
	}
f2e824
f2e824
	// If there is only ASCII data, we just return what we want, correctly splitting the lines.
f2e824
	if (strlen($str) === utf8_strlen($str))
f2e824
	{
f2e824
		return $start . implode($spacer, str_split($encoded_str, $split_length)) . $end;
f2e824
	}
f2e824
f2e824
	// UTF-8 data, compose encoded lines
f2e824
	$array = utf8_str_split($str);
f2e824
	$str = '';
f2e824
f2e824
	while (sizeof($array))
f2e824
	{
f2e824
		$text = '';
f2e824
f2e824
		while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length)
f2e824
		{
f2e824
			$text .= array_shift($array);
f2e824
		}
f2e824
f2e824
		$str .= $start . base64_encode($text) . $end . ' ';
f2e824
	}
f2e824
f2e824
	return substr($str, 0, -1);
f2e824
}
f2e824
f2e824
?>