Blame Identity/Models/Html/phpBB/3.0.4/includes/functions_messenger.php

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

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

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

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

Backtrace

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

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