Blame Extras/phpBB/3.0.4/includes/functions_messenger.php

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

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

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

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

Backtrace

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

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