includes/functions_messenger.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/functions_messenger.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1659 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package phpBB3
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* Messenger
    1.24 +* @package phpBB3
    1.25 +*/
    1.26 +class messenger
    1.27 +{
    1.28 +	var $vars, $msg, $extra_headers, $replyto, $from, $subject;
    1.29 +	var $addresses = array();
    1.30 +
    1.31 +	var $mail_priority = MAIL_NORMAL_PRIORITY;
    1.32 +	var $use_queue = true;
    1.33 +
    1.34 +	var $tpl_obj = NULL;
    1.35 +	var $tpl_msg = array();
    1.36 +	var $eol = "\n";
    1.37 +
    1.38 +	/**
    1.39 +	* Constructor
    1.40 +	*/
    1.41 +	function messenger($use_queue = true)
    1.42 +	{
    1.43 +		global $config;
    1.44 +
    1.45 +		$this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
    1.46 +		$this->subject = '';
    1.47 +
    1.48 +		// Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
    1.49 +		$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
    1.50 +		$this->eol = (!$this->eol) ? "\n" : $this->eol;
    1.51 +	}
    1.52 +
    1.53 +	/**
    1.54 +	* Resets all the data (address, template file, etc etc) to default
    1.55 +	*/
    1.56 +	function reset()
    1.57 +	{
    1.58 +		$this->addresses = $this->extra_headers = array();
    1.59 +		$this->vars = $this->msg = $this->replyto = $this->from = '';
    1.60 +		$this->mail_priority = MAIL_NORMAL_PRIORITY;
    1.61 +	}
    1.62 +
    1.63 +	/**
    1.64 +	* Sets an email address to send to
    1.65 +	*/
    1.66 +	function to($address, $realname = '')
    1.67 +	{
    1.68 +		global $config;
    1.69 +
    1.70 +		if (!trim($address))
    1.71 +		{
    1.72 +			return;
    1.73 +		}
    1.74 +
    1.75 +		$pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
    1.76 +
    1.77 +		$this->addresses['to'][$pos]['email'] = trim($address);
    1.78 +
    1.79 +		// If empty sendmail_path on windows, PHP changes the to line
    1.80 +		if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
    1.81 +		{
    1.82 +			$this->addresses['to'][$pos]['name'] = '';
    1.83 +		}
    1.84 +		else
    1.85 +		{
    1.86 +			$this->addresses['to'][$pos]['name'] = trim($realname);
    1.87 +		}
    1.88 +	}
    1.89 +
    1.90 +	/**
    1.91 +	* Sets an cc address to send to
    1.92 +	*/
    1.93 +	function cc($address, $realname = '')
    1.94 +	{
    1.95 +		if (!trim($address))
    1.96 +		{
    1.97 +			return;
    1.98 +		}
    1.99 +
   1.100 +		$pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
   1.101 +		$this->addresses['cc'][$pos]['email'] = trim($address);
   1.102 +		$this->addresses['cc'][$pos]['name'] = trim($realname);
   1.103 +	}
   1.104 +
   1.105 +	/**
   1.106 +	* Sets an bcc address to send to
   1.107 +	*/
   1.108 +	function bcc($address, $realname = '')
   1.109 +	{
   1.110 +		if (!trim($address))
   1.111 +		{
   1.112 +			return;
   1.113 +		}
   1.114 +
   1.115 +		$pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
   1.116 +		$this->addresses['bcc'][$pos]['email'] = trim($address);
   1.117 +		$this->addresses['bcc'][$pos]['name'] = trim($realname);
   1.118 +	}
   1.119 +
   1.120 +	/**
   1.121 +	* Sets a im contact to send to
   1.122 +	*/
   1.123 +	function im($address, $realname = '')
   1.124 +	{
   1.125 +		// IM-Addresses could be empty
   1.126 +		if (!trim($address))
   1.127 +		{
   1.128 +			return;
   1.129 +		}
   1.130 +
   1.131 +		$pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
   1.132 +		$this->addresses['im'][$pos]['uid'] = trim($address);
   1.133 +		$this->addresses['im'][$pos]['name'] = trim($realname);
   1.134 +	}
   1.135 +
   1.136 +	/**
   1.137 +	* Set the reply to address
   1.138 +	*/
   1.139 +	function replyto($address)
   1.140 +	{
   1.141 +		$this->replyto = trim($address);
   1.142 +	}
   1.143 +
   1.144 +	/**
   1.145 +	* Set the from address
   1.146 +	*/
   1.147 +	function from($address)
   1.148 +	{
   1.149 +		$this->from = trim($address);
   1.150 +	}
   1.151 +
   1.152 +	/**
   1.153 +	* set up subject for mail
   1.154 +	*/
   1.155 +	function subject($subject = '')
   1.156 +	{
   1.157 +		$this->subject = trim($subject);
   1.158 +	}
   1.159 +
   1.160 +	/**
   1.161 +	* set up extra mail headers
   1.162 +	*/
   1.163 +	function headers($headers)
   1.164 +	{
   1.165 +		$this->extra_headers[] = trim($headers);
   1.166 +	}
   1.167 +
   1.168 +	/**
   1.169 +	* Adds X-AntiAbuse headers
   1.170 +	*
   1.171 +	* @param array $config		Configuration array
   1.172 +	* @param user $user			A user object
   1.173 +	*
   1.174 +	* @return null
   1.175 +	*/
   1.176 +	function anti_abuse_headers($config, $user)
   1.177 +	{
   1.178 +		$this->headers('X-AntiAbuse: Board servername - ' . mail_encode($config['server_name']));
   1.179 +		$this->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
   1.180 +		$this->headers('X-AntiAbuse: Username - ' . mail_encode($user->data['username']));
   1.181 +		$this->headers('X-AntiAbuse: User IP - ' . $user->ip);
   1.182 +	}
   1.183 +
   1.184 +	/**
   1.185 +	* Set the email priority
   1.186 +	*/
   1.187 +	function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
   1.188 +	{
   1.189 +		$this->mail_priority = $priority;
   1.190 +	}
   1.191 +
   1.192 +	/**
   1.193 +	* Set email template to use
   1.194 +	*/
   1.195 +	function template($template_file, $template_lang = '', $template_path = '')
   1.196 +	{
   1.197 +		global $config, $phpbb_root_path, $user;
   1.198 +
   1.199 +		if (!trim($template_file))
   1.200 +		{
   1.201 +			trigger_error('No template file for emailing set.', E_USER_ERROR);
   1.202 +		}
   1.203 +
   1.204 +		if (!trim($template_lang))
   1.205 +		{
   1.206 +			// fall back to board default language if the user's language is
   1.207 +			// missing $template_file.  If this does not exist either,
   1.208 +			// $tpl->set_custom_template will do a trigger_error
   1.209 +			$template_lang = basename($config['default_lang']);
   1.210 +		}
   1.211 +
   1.212 +		// tpl_msg now holds a template object we can use to parse the template file
   1.213 +		if (!isset($this->tpl_msg[$template_lang . $template_file]))
   1.214 +		{
   1.215 +			$this->tpl_msg[$template_lang . $template_file] = new template();
   1.216 +			$tpl = &$this->tpl_msg[$template_lang . $template_file];
   1.217 +
   1.218 +			$fallback_template_path = false;
   1.219 +
   1.220 +			if (!$template_path)
   1.221 +			{
   1.222 +				$template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
   1.223 +				$template_path .= $template_lang . '/email';
   1.224 +
   1.225 +				// we can only specify default language fallback when the path is not a custom one for which we
   1.226 +				// do not know the default language alternative
   1.227 +				if ($template_lang !== basename($config['default_lang']))
   1.228 +				{
   1.229 +					$fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
   1.230 +					$fallback_template_path .= basename($config['default_lang']) . '/email';
   1.231 +				}
   1.232 +			}
   1.233 +
   1.234 +			$tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path);
   1.235 +
   1.236 +			$tpl->set_filenames(array(
   1.237 +				'body'		=> $template_file . '.txt',
   1.238 +			));
   1.239 +		}
   1.240 +
   1.241 +		$this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file];
   1.242 +		$this->vars = &$this->tpl_obj->_rootref;
   1.243 +		$this->tpl_msg = '';
   1.244 +
   1.245 +		return true;
   1.246 +	}
   1.247 +
   1.248 +	/**
   1.249 +	* assign variables to email template
   1.250 +	*/
   1.251 +	function assign_vars($vars)
   1.252 +	{
   1.253 +		if (!is_object($this->tpl_obj))
   1.254 +		{
   1.255 +			return;
   1.256 +		}
   1.257 +
   1.258 +		$this->tpl_obj->assign_vars($vars);
   1.259 +	}
   1.260 +
   1.261 +	function assign_block_vars($blockname, $vars)
   1.262 +	{
   1.263 +		if (!is_object($this->tpl_obj))
   1.264 +		{
   1.265 +			return;
   1.266 +		}
   1.267 +
   1.268 +		$this->tpl_obj->assign_block_vars($blockname, $vars);
   1.269 +	}
   1.270 +
   1.271 +	/**
   1.272 +	* Send the mail out to the recipients set previously in var $this->addresses
   1.273 +	*/
   1.274 +	function send($method = NOTIFY_EMAIL, $break = false)
   1.275 +	{
   1.276 +		global $config, $user;
   1.277 +
   1.278 +		// We add some standard variables we always use, no need to specify them always
   1.279 +		if (!isset($this->vars['U_BOARD']))
   1.280 +		{
   1.281 +			$this->assign_vars(array(
   1.282 +				'U_BOARD'	=> generate_board_url(),
   1.283 +			));
   1.284 +		}
   1.285 +
   1.286 +		if (!isset($this->vars['EMAIL_SIG']))
   1.287 +		{
   1.288 +			$this->assign_vars(array(
   1.289 +				'EMAIL_SIG'	=> str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])),
   1.290 +			));
   1.291 +		}
   1.292 +
   1.293 +		if (!isset($this->vars['SITENAME']))
   1.294 +		{
   1.295 +			$this->assign_vars(array(
   1.296 +				'SITENAME'	=> htmlspecialchars_decode($config['sitename']),
   1.297 +			));
   1.298 +		}
   1.299 +
   1.300 +		// Parse message through template
   1.301 +		$this->msg = trim($this->tpl_obj->assign_display('body'));
   1.302 +
   1.303 +		// Because we use \n for newlines in the body message we need to fix line encoding errors for those admins who uploaded email template files in the wrong encoding
   1.304 +		$this->msg = str_replace("\r\n", "\n", $this->msg);
   1.305 +
   1.306 +		// We now try and pull a subject from the email body ... if it exists,
   1.307 +		// do this here because the subject may contain a variable
   1.308 +		$drop_header = '';
   1.309 +		$match = array();
   1.310 +		if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
   1.311 +		{
   1.312 +			$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
   1.313 +			$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
   1.314 +		}
   1.315 +		else
   1.316 +		{
   1.317 +			$this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
   1.318 +		}
   1.319 +
   1.320 +		if ($drop_header)
   1.321 +		{
   1.322 +			$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
   1.323 +		}
   1.324 +
   1.325 +		if ($break)
   1.326 +		{
   1.327 +			return true;
   1.328 +		}
   1.329 +
   1.330 +		switch ($method)
   1.331 +		{
   1.332 +			case NOTIFY_EMAIL:
   1.333 +				$result = $this->msg_email();
   1.334 +			break;
   1.335 +
   1.336 +			case NOTIFY_IM:
   1.337 +				$result = $this->msg_jabber();
   1.338 +			break;
   1.339 +
   1.340 +			case NOTIFY_BOTH:
   1.341 +				$result = $this->msg_email();
   1.342 +				$this->msg_jabber();
   1.343 +			break;
   1.344 +		}
   1.345 +
   1.346 +		$this->reset();
   1.347 +		return $result;
   1.348 +	}
   1.349 +
   1.350 +	/**
   1.351 +	* Add error message to log
   1.352 +	*/
   1.353 +	function error($type, $msg)
   1.354 +	{
   1.355 +		global $user, $phpEx, $phpbb_root_path, $config;
   1.356 +
   1.357 +		// Session doesn't exist, create it
   1.358 +		if (!isset($user->session_id) || $user->session_id === '')
   1.359 +		{
   1.360 +			$user->session_begin();
   1.361 +		}
   1.362 +
   1.363 +		$calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
   1.364 +
   1.365 +		$message = '';
   1.366 +		switch ($type)
   1.367 +		{
   1.368 +			case 'EMAIL':
   1.369 +				$message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '</strong>';
   1.370 +			break;
   1.371 +
   1.372 +			default:
   1.373 +				$message = "<strong>$type</strong>";
   1.374 +			break;
   1.375 +		}
   1.376 +
   1.377 +		$message .= '<br /><em>' . htmlspecialchars($calling_page) . '</em><br /><br />' . $msg . '<br />';
   1.378 +		add_log('critical', 'LOG_ERROR_' . $type, $message);
   1.379 +	}
   1.380 +
   1.381 +	/**
   1.382 +	* Save to queue
   1.383 +	*/
   1.384 +	function save_queue()
   1.385 +	{
   1.386 +		global $config;
   1.387 +
   1.388 +		if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
   1.389 +		{
   1.390 +			$this->queue->save();
   1.391 +			return;
   1.392 +		}
   1.393 +	}
   1.394 +
   1.395 +	/**
   1.396 +	* Return email header
   1.397 +	*/
   1.398 +	function build_header($to, $cc, $bcc)
   1.399 +	{
   1.400 +		global $config;
   1.401 +
   1.402 +		// We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
   1.403 +		$headers = array();
   1.404 +
   1.405 +		$headers[] = 'From: ' . $this->from;
   1.406 +
   1.407 +		if ($cc)
   1.408 +		{
   1.409 +			$headers[] = 'Cc: ' . $cc;
   1.410 +		}
   1.411 +
   1.412 +		if ($bcc)
   1.413 +		{
   1.414 +			$headers[] = 'Bcc: ' . $bcc;
   1.415 +		}
   1.416 +
   1.417 +		$headers[] = 'Reply-To: ' . $this->replyto;
   1.418 +		$headers[] = 'Return-Path: <' . $config['board_email'] . '>';
   1.419 +		$headers[] = 'Sender: <' . $config['board_email'] . '>';
   1.420 +		$headers[] = 'MIME-Version: 1.0';
   1.421 +		$headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
   1.422 +		$headers[] = 'Date: ' . date('r', time());
   1.423 +		$headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
   1.424 +		$headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
   1.425 +
   1.426 +		$headers[] = 'X-Priority: ' . $this->mail_priority;
   1.427 +		$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
   1.428 +		$headers[] = 'X-Mailer: phpBB3';
   1.429 +		$headers[] = 'X-MimeOLE: phpBB3';
   1.430 +		$headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
   1.431 +
   1.432 +		if (sizeof($this->extra_headers))
   1.433 +		{
   1.434 +			$headers = array_merge($headers, $this->extra_headers);
   1.435 +		}
   1.436 +
   1.437 +		return $headers;
   1.438 +	}
   1.439 +
   1.440 +	/**
   1.441 +	* Send out emails
   1.442 +	*/
   1.443 +	function msg_email()
   1.444 +	{
   1.445 +		global $config, $user;
   1.446 +
   1.447 +		if (empty($config['email_enable']))
   1.448 +		{
   1.449 +			return false;
   1.450 +		}
   1.451 +
   1.452 +		// Addresses to send to?
   1.453 +		if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc'])))
   1.454 +		{
   1.455 +			// Send was successful. ;)
   1.456 +			return true;
   1.457 +		}
   1.458 +
   1.459 +		$use_queue = false;
   1.460 +		if ($config['email_package_size'] && $this->use_queue)
   1.461 +		{
   1.462 +			if (empty($this->queue))
   1.463 +			{
   1.464 +				$this->queue = new queue();
   1.465 +				$this->queue->init('email', $config['email_package_size']);
   1.466 +			}
   1.467 +			$use_queue = true;
   1.468 +		}
   1.469 +
   1.470 +		if (empty($this->replyto))
   1.471 +		{
   1.472 +			$this->replyto = '<' . $config['board_contact'] . '>';
   1.473 +		}
   1.474 +
   1.475 +		if (empty($this->from))
   1.476 +		{
   1.477 +			$this->from = '<' . $config['board_contact'] . '>';
   1.478 +		}
   1.479 +
   1.480 +		$encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol;
   1.481 +
   1.482 +		// Build to, cc and bcc strings
   1.483 +		$to = $cc = $bcc = '';
   1.484 +		foreach ($this->addresses as $type => $address_ary)
   1.485 +		{
   1.486 +			if ($type == 'im')
   1.487 +			{
   1.488 +				continue;
   1.489 +			}
   1.490 +
   1.491 +			foreach ($address_ary as $which_ary)
   1.492 +			{
   1.493 +				$$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']);
   1.494 +			}
   1.495 +		}
   1.496 +
   1.497 +		// Build header
   1.498 +		$headers = $this->build_header($to, $cc, $bcc);
   1.499 +
   1.500 +		// Send message ...
   1.501 +		if (!$use_queue)
   1.502 +		{
   1.503 +			$mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
   1.504 +			$err_msg = '';
   1.505 +
   1.506 +			if ($config['smtp_delivery'])
   1.507 +			{
   1.508 +				$result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
   1.509 +			}
   1.510 +			else
   1.511 +			{
   1.512 +				$result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg);
   1.513 +			}
   1.514 +
   1.515 +			if (!$result)
   1.516 +			{
   1.517 +				$this->error('EMAIL', $err_msg);
   1.518 +				return false;
   1.519 +			}
   1.520 +		}
   1.521 +		else
   1.522 +		{
   1.523 +			$this->queue->put('email', array(
   1.524 +				'to'			=> $to,
   1.525 +				'addresses'		=> $this->addresses,
   1.526 +				'subject'		=> $this->subject,
   1.527 +				'msg'			=> $this->msg,
   1.528 +				'headers'		=> $headers)
   1.529 +			);
   1.530 +		}
   1.531 +
   1.532 +		return true;
   1.533 +	}
   1.534 +
   1.535 +	/**
   1.536 +	* Send jabber message out
   1.537 +	*/
   1.538 +	function msg_jabber()
   1.539 +	{
   1.540 +		global $config, $db, $user, $phpbb_root_path, $phpEx;
   1.541 +
   1.542 +		if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
   1.543 +		{
   1.544 +			return false;
   1.545 +		}
   1.546 +
   1.547 +		if (empty($this->addresses['im']))
   1.548 +		{
   1.549 +			// Send was successful. ;)
   1.550 +			return true;
   1.551 +		}
   1.552 +
   1.553 +		$use_queue = false;
   1.554 +		if ($config['jab_package_size'] && $this->use_queue)
   1.555 +		{
   1.556 +			if (empty($this->queue))
   1.557 +			{
   1.558 +				$this->queue = new queue();
   1.559 +				$this->queue->init('jabber', $config['jab_package_size']);
   1.560 +			}
   1.561 +			$use_queue = true;
   1.562 +		}
   1.563 +
   1.564 +		$addresses = array();
   1.565 +		foreach ($this->addresses['im'] as $type => $uid_ary)
   1.566 +		{
   1.567 +			$addresses[] = $uid_ary['uid'];
   1.568 +		}
   1.569 +		$addresses = array_unique($addresses);
   1.570 +
   1.571 +		if (!$use_queue)
   1.572 +		{
   1.573 +			include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
   1.574 +			$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
   1.575 +
   1.576 +			if (!$this->jabber->connect())
   1.577 +			{
   1.578 +				$this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log());
   1.579 +				return false;
   1.580 +			}
   1.581 +
   1.582 +			if (!$this->jabber->login())
   1.583 +			{
   1.584 +				$this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log());
   1.585 +				return false;
   1.586 +			}
   1.587 +
   1.588 +			foreach ($addresses as $address)
   1.589 +			{
   1.590 +				$this->jabber->send_message($address, $this->msg, $this->subject);
   1.591 +			}
   1.592 +
   1.593 +			$this->jabber->disconnect();
   1.594 +		}
   1.595 +		else
   1.596 +		{
   1.597 +			$this->queue->put('jabber', array(
   1.598 +				'addresses'		=> $addresses,
   1.599 +				'subject'		=> $this->subject,
   1.600 +				'msg'			=> $this->msg)
   1.601 +			);
   1.602 +		}
   1.603 +		unset($addresses);
   1.604 +		return true;
   1.605 +	}
   1.606 +}
   1.607 +
   1.608 +/**
   1.609 +* handling email and jabber queue
   1.610 +* @package phpBB3
   1.611 +*/
   1.612 +class queue
   1.613 +{
   1.614 +	var $data = array();
   1.615 +	var $queue_data = array();
   1.616 +	var $package_size = 0;
   1.617 +	var $cache_file = '';
   1.618 +	var $eol = "\n";
   1.619 +
   1.620 +	/**
   1.621 +	* constructor
   1.622 +	*/
   1.623 +	function queue()
   1.624 +	{
   1.625 +		global $phpEx, $phpbb_root_path;
   1.626 +
   1.627 +		$this->data = array();
   1.628 +		$this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
   1.629 +
   1.630 +		// Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
   1.631 +		$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
   1.632 +		$this->eol = (!$this->eol) ? "\n" : $this->eol;
   1.633 +	}
   1.634 +
   1.635 +	/**
   1.636 +	* Init a queue object
   1.637 +	*/
   1.638 +	function init($object, $package_size)
   1.639 +	{
   1.640 +		$this->data[$object] = array();
   1.641 +		$this->data[$object]['package_size'] = $package_size;
   1.642 +		$this->data[$object]['data'] = array();
   1.643 +	}
   1.644 +
   1.645 +	/**
   1.646 +	* Put object in queue
   1.647 +	*/
   1.648 +	function put($object, $scope)
   1.649 +	{
   1.650 +		$this->data[$object]['data'][] = $scope;
   1.651 +	}
   1.652 +
   1.653 +	/**
   1.654 +	* Obtains exclusive lock on queue cache file.
   1.655 +	* Returns resource representing the lock
   1.656 +	*/
   1.657 +	function lock()
   1.658 +	{
   1.659 +		// For systems that can't have two processes opening
   1.660 +		// one file for writing simultaneously
   1.661 +		if (file_exists($this->cache_file . '.lock'))
   1.662 +		{
   1.663 +			$mode = 'rb';
   1.664 +		}
   1.665 +		else
   1.666 +		{
   1.667 +			$mode = 'wb';
   1.668 +		}
   1.669 +
   1.670 +		$lock_fp = @fopen($this->cache_file . '.lock', $mode);
   1.671 +
   1.672 +		if ($mode == 'wb')
   1.673 +		{
   1.674 +			if (!$lock_fp)
   1.675 +			{
   1.676 +				// Two processes may attempt to create lock file at the same time.
   1.677 +				// Have the losing process try opening the lock file again for reading
   1.678 +				// on the assumption that the winning process created it
   1.679 +				$mode = 'rb';
   1.680 +				$lock_fp = @fopen($this->cache_file . '.lock', $mode);
   1.681 +			}
   1.682 +			else
   1.683 +			{
   1.684 +				// Only need to set mode when the lock file is written
   1.685 +				@chmod($this->cache_file . '.lock', 0666);
   1.686 +			}
   1.687 +		}
   1.688 +
   1.689 +		if ($lock_fp)
   1.690 +		{
   1.691 +			@flock($lock_fp, LOCK_EX);
   1.692 +		}
   1.693 +
   1.694 +		return $lock_fp;
   1.695 +	}
   1.696 +
   1.697 +	/**
   1.698 +	* Releases lock on queue cache file, using resource obtained from lock()
   1.699 +	*/
   1.700 +	function unlock($lock_fp)
   1.701 +	{
   1.702 +		// lock() will return null if opening lock file, and thus locking, failed.
   1.703 +		// Accept null values here so that client code does not need to check them
   1.704 +		if ($lock_fp)
   1.705 +		{
   1.706 +			@flock($lock_fp, LOCK_UN);
   1.707 +			fclose($lock_fp);
   1.708 +		}
   1.709 +	}
   1.710 +
   1.711 +	/**
   1.712 +	* Process queue
   1.713 +	* Using lock file
   1.714 +	*/
   1.715 +	function process()
   1.716 +	{
   1.717 +		global $db, $config, $phpEx, $phpbb_root_path, $user;
   1.718 +
   1.719 +		$lock_fp = $this->lock();
   1.720 +
   1.721 +		set_config('last_queue_run', time(), true);
   1.722 +
   1.723 +		if (!file_exists($this->cache_file) || filemtime($this->cache_file) > time() - $config['queue_interval'])
   1.724 +		{
   1.725 +			$this->unlock($lock_fp);
   1.726 +			return;
   1.727 +		}
   1.728 +
   1.729 +		include($this->cache_file);
   1.730 +
   1.731 +		foreach ($this->queue_data as $object => $data_ary)
   1.732 +		{
   1.733 +			@set_time_limit(0);
   1.734 +
   1.735 +			if (!isset($data_ary['package_size']))
   1.736 +			{
   1.737 +				$data_ary['package_size'] = 0;
   1.738 +			}
   1.739 +
   1.740 +			$package_size = $data_ary['package_size'];
   1.741 +			$num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
   1.742 +
   1.743 +			/*
   1.744 +			* This code is commented out because it causes problems on some web hosts.
   1.745 +			* The core problem is rather restrictive email sending limits.
   1.746 +			* This code is nly useful if you have no such restrictions from the
   1.747 +			* web host and the package size setting is wrong.
   1.748 +
   1.749 +			// If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs...
   1.750 +			if (sizeof($data_ary['data']) > $package_size * 2.5)
   1.751 +			{
   1.752 +				$num_items = sizeof($data_ary['data']);
   1.753 +			}
   1.754 +			*/
   1.755 +
   1.756 +			switch ($object)
   1.757 +			{
   1.758 +				case 'email':
   1.759 +					// Delete the email queued objects if mailing is disabled
   1.760 +					if (!$config['email_enable'])
   1.761 +					{
   1.762 +						unset($this->queue_data['email']);
   1.763 +						continue 2;
   1.764 +					}
   1.765 +				break;
   1.766 +
   1.767 +				case 'jabber':
   1.768 +					if (!$config['jab_enable'])
   1.769 +					{
   1.770 +						unset($this->queue_data['jabber']);
   1.771 +						continue 2;
   1.772 +					}
   1.773 +
   1.774 +					include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
   1.775 +					$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
   1.776 +
   1.777 +					if (!$this->jabber->connect())
   1.778 +					{
   1.779 +						messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
   1.780 +						continue 2;
   1.781 +					}
   1.782 +
   1.783 +					if (!$this->jabber->login())
   1.784 +					{
   1.785 +						messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
   1.786 +						continue 2;
   1.787 +					}
   1.788 +
   1.789 +				break;
   1.790 +
   1.791 +				default:
   1.792 +					$this->unlock($lock_fp);
   1.793 +					return;
   1.794 +			}
   1.795 +
   1.796 +			for ($i = 0; $i < $num_items; $i++)
   1.797 +			{
   1.798 +				// Make variables available...
   1.799 +				extract(array_shift($this->queue_data[$object]['data']));
   1.800 +
   1.801 +				switch ($object)
   1.802 +				{
   1.803 +					case 'email':
   1.804 +						$err_msg = '';
   1.805 +						$to = (!$to) ? 'undisclosed-recipients:;' : $to;
   1.806 +
   1.807 +						if ($config['smtp_delivery'])
   1.808 +						{
   1.809 +							$result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
   1.810 +						}
   1.811 +						else
   1.812 +						{
   1.813 +							$result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg);
   1.814 +						}
   1.815 +
   1.816 +						if (!$result)
   1.817 +						{
   1.818 +							messenger::error('EMAIL', $err_msg);
   1.819 +							continue 2;
   1.820 +						}
   1.821 +					break;
   1.822 +
   1.823 +					case 'jabber':
   1.824 +						foreach ($addresses as $address)
   1.825 +						{
   1.826 +							if ($this->jabber->send_message($address, $msg, $subject) === false)
   1.827 +							{
   1.828 +								messenger::error('JABBER', $this->jabber->get_log());
   1.829 +								continue 3;
   1.830 +							}
   1.831 +						}
   1.832 +					break;
   1.833 +				}
   1.834 +			}
   1.835 +
   1.836 +			// No more data for this object? Unset it
   1.837 +			if (!sizeof($this->queue_data[$object]['data']))
   1.838 +			{
   1.839 +				unset($this->queue_data[$object]);
   1.840 +			}
   1.841 +
   1.842 +			// Post-object processing
   1.843 +			switch ($object)
   1.844 +			{
   1.845 +				case 'jabber':
   1.846 +					// Hang about a couple of secs to ensure the messages are
   1.847 +					// handled, then disconnect
   1.848 +					$this->jabber->disconnect();
   1.849 +				break;
   1.850 +			}
   1.851 +		}
   1.852 +
   1.853 +		if (!sizeof($this->queue_data))
   1.854 +		{
   1.855 +			@unlink($this->cache_file);
   1.856 +		}
   1.857 +		else
   1.858 +		{
   1.859 +			if ($fp = @fopen($this->cache_file, 'wb'))
   1.860 +			{
   1.861 +				fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
   1.862 +				fclose($fp);
   1.863 +
   1.864 +				phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
   1.865 +			}
   1.866 +		}
   1.867 +
   1.868 +		$this->unlock($lock_fp);
   1.869 +	}
   1.870 +
   1.871 +	/**
   1.872 +	* Save queue
   1.873 +	*/
   1.874 +	function save()
   1.875 +	{
   1.876 +		if (!sizeof($this->data))
   1.877 +		{
   1.878 +			return;
   1.879 +		}
   1.880 +
   1.881 +		$lock_fp = $this->lock();
   1.882 +
   1.883 +		if (file_exists($this->cache_file))
   1.884 +		{
   1.885 +			include($this->cache_file);
   1.886 +
   1.887 +			foreach ($this->queue_data as $object => $data_ary)
   1.888 +			{
   1.889 +				if (isset($this->data[$object]) && sizeof($this->data[$object]))
   1.890 +				{
   1.891 +					$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
   1.892 +				}
   1.893 +				else
   1.894 +				{
   1.895 +					$this->data[$object]['data'] = $data_ary['data'];
   1.896 +				}
   1.897 +			}
   1.898 +		}
   1.899 +
   1.900 +		if ($fp = @fopen($this->cache_file, 'w'))
   1.901 +		{
   1.902 +			fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
   1.903 +			fclose($fp);
   1.904 +
   1.905 +			phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
   1.906 +		}
   1.907 +
   1.908 +		$this->unlock($lock_fp);
   1.909 +	}
   1.910 +}
   1.911 +
   1.912 +/**
   1.913 +* Replacement or substitute for PHP's mail command
   1.914 +*/
   1.915 +function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)
   1.916 +{
   1.917 +	global $config, $user;
   1.918 +
   1.919 +	// Fix any bare linefeeds in the message to make it RFC821 Compliant.
   1.920 +	$message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
   1.921 +
   1.922 +	if ($headers !== false)
   1.923 +	{
   1.924 +		if (!is_array($headers))
   1.925 +		{
   1.926 +			// Make sure there are no bare linefeeds in the headers
   1.927 +			$headers = preg_replace('#(?<!\r)\n#si', "\n", $headers);
   1.928 +			$headers = explode("\n", $headers);
   1.929 +		}
   1.930 +
   1.931 +		// Ok this is rather confusing all things considered,
   1.932 +		// but we have to grab bcc and cc headers and treat them differently
   1.933 +		// Something we really didn't take into consideration originally
   1.934 +		$headers_used = array();
   1.935 +
   1.936 +		foreach ($headers as $header)
   1.937 +		{
   1.938 +			if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
   1.939 +			{
   1.940 +				continue;
   1.941 +			}
   1.942 +			$headers_used[] = trim($header);
   1.943 +		}
   1.944 +
   1.945 +		$headers = chop(implode("\r\n", $headers_used));
   1.946 +	}
   1.947 +
   1.948 +	if (trim($subject) == '')
   1.949 +	{
   1.950 +		$err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
   1.951 +		return false;
   1.952 +	}
   1.953 +
   1.954 +	if (trim($message) == '')
   1.955 +	{
   1.956 +		$err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
   1.957 +		return false;
   1.958 +	}
   1.959 +
   1.960 +	$mail_rcpt = $mail_to = $mail_cc = array();
   1.961 +
   1.962 +	// Build correct addresses for RCPT TO command and the client side display (TO, CC)
   1.963 +	if (isset($addresses['to']) && sizeof($addresses['to']))
   1.964 +	{
   1.965 +		foreach ($addresses['to'] as $which_ary)
   1.966 +		{
   1.967 +			$mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
   1.968 +			$mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
   1.969 +		}
   1.970 +	}
   1.971 +
   1.972 +	if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
   1.973 +	{
   1.974 +		foreach ($addresses['bcc'] as $which_ary)
   1.975 +		{
   1.976 +			$mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
   1.977 +		}
   1.978 +	}
   1.979 +
   1.980 +	if (isset($addresses['cc']) && sizeof($addresses['cc']))
   1.981 +	{
   1.982 +		foreach ($addresses['cc'] as $which_ary)
   1.983 +		{
   1.984 +			$mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
   1.985 +			$mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
   1.986 +		}
   1.987 +	}
   1.988 +
   1.989 +	$smtp = new smtp_class();
   1.990 +
   1.991 +	$errno = 0;
   1.992 +	$errstr = '';
   1.993 +
   1.994 +	$smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
   1.995 +
   1.996 +	// Ok we have error checked as much as we can to this point let's get on it already.
   1.997 +	if (!class_exists('phpbb_error_collector'))
   1.998 +	{
   1.999 +		global $phpbb_root_path, $phpEx;
  1.1000 +		include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
  1.1001 +	}
  1.1002 +	$collector = new phpbb_error_collector;
  1.1003 +	$collector->install();
  1.1004 +	$smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
  1.1005 +	$collector->uninstall();
  1.1006 +	$error_contents = $collector->format_errors();
  1.1007 +
  1.1008 +	if (!$smtp->socket)
  1.1009 +	{
  1.1010 +		if ($errstr)
  1.1011 +		{
  1.1012 +			$errstr = utf8_convert_message($errstr);
  1.1013 +		}
  1.1014 +
  1.1015 +		$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";
  1.1016 +		$err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents) : '';
  1.1017 +		return false;
  1.1018 +	}
  1.1019 +
  1.1020 +	// Wait for reply
  1.1021 +	if ($err_msg = $smtp->server_parse('220', __LINE__))
  1.1022 +	{
  1.1023 +		$smtp->close_session($err_msg);
  1.1024 +		return false;
  1.1025 +	}
  1.1026 +
  1.1027 +	// Let me in. This function handles the complete authentication process
  1.1028 +	if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
  1.1029 +	{
  1.1030 +		$smtp->close_session($err_msg);
  1.1031 +		return false;
  1.1032 +	}
  1.1033 +
  1.1034 +	// From this point onward most server response codes should be 250
  1.1035 +	// Specify who the mail is from....
  1.1036 +	$smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
  1.1037 +	if ($err_msg = $smtp->server_parse('250', __LINE__))
  1.1038 +	{
  1.1039 +		$smtp->close_session($err_msg);
  1.1040 +		return false;
  1.1041 +	}
  1.1042 +
  1.1043 +	// Specify each user to send to and build to header.
  1.1044 +	$to_header = implode(', ', $mail_to);
  1.1045 +	$cc_header = implode(', ', $mail_cc);
  1.1046 +
  1.1047 +	// Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
  1.1048 +	$rcpt = false;
  1.1049 +	foreach ($mail_rcpt as $type => $mail_to_addresses)
  1.1050 +	{
  1.1051 +		foreach ($mail_to_addresses as $mail_to_address)
  1.1052 +		{
  1.1053 +			// Add an additional bit of error checking to the To field.
  1.1054 +			if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
  1.1055 +			{
  1.1056 +				$smtp->server_send("RCPT TO:$mail_to_address");
  1.1057 +				if ($err_msg = $smtp->server_parse('250', __LINE__))
  1.1058 +				{
  1.1059 +					// We continue... if users are not resolved we do not care
  1.1060 +					if ($smtp->numeric_response_code != 550)
  1.1061 +					{
  1.1062 +						$smtp->close_session($err_msg);
  1.1063 +						return false;
  1.1064 +					}
  1.1065 +				}
  1.1066 +				else
  1.1067 +				{
  1.1068 +					$rcpt = true;
  1.1069 +				}
  1.1070 +			}
  1.1071 +		}
  1.1072 +	}
  1.1073 +
  1.1074 +	// 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.
  1.1075 +	if (!$rcpt)
  1.1076 +	{
  1.1077 +		$user->session_begin();
  1.1078 +		$err_msg .= '<br /><br />';
  1.1079 +		$err_msg .= (isset($user->lang['INVALID_EMAIL_LOG'])) ? sprintf($user->lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address)) : '<strong>' . htmlspecialchars($mail_to_address) . '</strong> possibly an invalid email address?';
  1.1080 +		$smtp->close_session($err_msg);
  1.1081 +		return false;
  1.1082 +	}
  1.1083 +
  1.1084 +	// Ok now we tell the server we are ready to start sending data
  1.1085 +	$smtp->server_send('DATA');
  1.1086 +
  1.1087 +	// This is the last response code we look for until the end of the message.
  1.1088 +	if ($err_msg = $smtp->server_parse('354', __LINE__))
  1.1089 +	{
  1.1090 +		$smtp->close_session($err_msg);
  1.1091 +		return false;
  1.1092 +	}
  1.1093 +
  1.1094 +	// Send the Subject Line...
  1.1095 +	$smtp->server_send("Subject: $subject");
  1.1096 +
  1.1097 +	// Now the To Header.
  1.1098 +	$to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
  1.1099 +	$smtp->server_send("To: $to_header");
  1.1100 +
  1.1101 +	// Now the CC Header.
  1.1102 +	if ($cc_header != '')
  1.1103 +	{
  1.1104 +		$smtp->server_send("CC: $cc_header");
  1.1105 +	}
  1.1106 +
  1.1107 +	// Now any custom headers....
  1.1108 +	if ($headers !== false)
  1.1109 +	{
  1.1110 +		$smtp->server_send("$headers\r\n");
  1.1111 +	}
  1.1112 +
  1.1113 +	// Ok now we are ready for the message...
  1.1114 +	$smtp->server_send($message);
  1.1115 +
  1.1116 +	// Ok the all the ingredients are mixed in let's cook this puppy...
  1.1117 +	$smtp->server_send('.');
  1.1118 +	if ($err_msg = $smtp->server_parse('250', __LINE__))
  1.1119 +	{
  1.1120 +		$smtp->close_session($err_msg);
  1.1121 +		return false;
  1.1122 +	}
  1.1123 +
  1.1124 +	// Now tell the server we are done and close the socket...
  1.1125 +	$smtp->server_send('QUIT');
  1.1126 +	$smtp->close_session($err_msg);
  1.1127 +
  1.1128 +	return true;
  1.1129 +}
  1.1130 +
  1.1131 +/**
  1.1132 +* SMTP Class
  1.1133 +* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
  1.1134 +* See docs/AUTHORS for more details
  1.1135 +* @package phpBB3
  1.1136 +*/
  1.1137 +class smtp_class
  1.1138 +{
  1.1139 +	var $server_response = '';
  1.1140 +	var $socket = 0;
  1.1141 +	var $responses = array();
  1.1142 +	var $commands = array();
  1.1143 +	var $numeric_response_code = 0;
  1.1144 +
  1.1145 +	var $backtrace = false;
  1.1146 +	var $backtrace_log = array();
  1.1147 +
  1.1148 +	function smtp_class()
  1.1149 +	{
  1.1150 +		// Always create a backtrace for admins to identify SMTP problems
  1.1151 +		$this->backtrace = true;
  1.1152 +		$this->backtrace_log = array();
  1.1153 +	}
  1.1154 +
  1.1155 +	/**
  1.1156 +	* Add backtrace message for debugging
  1.1157 +	*/
  1.1158 +	function add_backtrace($message)
  1.1159 +	{
  1.1160 +		if ($this->backtrace)
  1.1161 +		{
  1.1162 +			$this->backtrace_log[] = utf8_htmlspecialchars($message);
  1.1163 +		}
  1.1164 +	}
  1.1165 +
  1.1166 +	/**
  1.1167 +	* Send command to smtp server
  1.1168 +	*/
  1.1169 +	function server_send($command, $private_info = false)
  1.1170 +	{
  1.1171 +		fputs($this->socket, $command . "\r\n");
  1.1172 +
  1.1173 +		(!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
  1.1174 +
  1.1175 +		// We could put additional code here
  1.1176 +	}
  1.1177 +
  1.1178 +	/**
  1.1179 +	* We use the line to give the support people an indication at which command the error occurred
  1.1180 +	*/
  1.1181 +	function server_parse($response, $line)
  1.1182 +	{
  1.1183 +		global $user;
  1.1184 +
  1.1185 +		$this->server_response = '';
  1.1186 +		$this->responses = array();
  1.1187 +		$this->numeric_response_code = 0;
  1.1188 +
  1.1189 +		while (substr($this->server_response, 3, 1) != ' ')
  1.1190 +		{
  1.1191 +			if (!($this->server_response = fgets($this->socket, 256)))
  1.1192 +			{
  1.1193 +				return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
  1.1194 +			}
  1.1195 +			$this->responses[] = substr(rtrim($this->server_response), 4);
  1.1196 +			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
  1.1197 +
  1.1198 +			$this->add_backtrace("LINE: $line <- {$this->server_response}");
  1.1199 +		}
  1.1200 +
  1.1201 +		if (!(substr($this->server_response, 0, 3) == $response))
  1.1202 +		{
  1.1203 +			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
  1.1204 +			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 <strong>Line $line</strong>. Response: $this->server_response";
  1.1205 +		}
  1.1206 +
  1.1207 +		return 0;
  1.1208 +	}
  1.1209 +
  1.1210 +	/**
  1.1211 +	* Close session
  1.1212 +	*/
  1.1213 +	function close_session(&$err_msg)
  1.1214 +	{
  1.1215 +		fclose($this->socket);
  1.1216 +
  1.1217 +		if ($this->backtrace)
  1.1218 +		{
  1.1219 +			$message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>';
  1.1220 +			$err_msg .= $message;
  1.1221 +		}
  1.1222 +	}
  1.1223 +
  1.1224 +	/**
  1.1225 +	* Log into server and get possible auth codes if neccessary
  1.1226 +	*/
  1.1227 +	function log_into_server($hostname, $username, $password, $default_auth_method)
  1.1228 +	{
  1.1229 +		global $user;
  1.1230 +
  1.1231 +		$err_msg = '';
  1.1232 +
  1.1233 +		// Here we try to determine the *real* hostname (reverse DNS entry preferrably)
  1.1234 +		$local_host = $user->host;
  1.1235 +
  1.1236 +		if (function_exists('php_uname'))
  1.1237 +		{
  1.1238 +			$local_host = php_uname('n');
  1.1239 +
  1.1240 +			// Able to resolve name to IP
  1.1241 +			if (($addr = @gethostbyname($local_host)) !== $local_host)
  1.1242 +			{
  1.1243 +				// Able to resolve IP back to name
  1.1244 +				if (($name = @gethostbyaddr($addr)) !== $addr)
  1.1245 +				{
  1.1246 +					$local_host = $name;
  1.1247 +				}
  1.1248 +			}
  1.1249 +		}
  1.1250 +
  1.1251 +		// If we are authenticating through pop-before-smtp, we
  1.1252 +		// have to login ones before we get authenticated
  1.1253 +		// NOTE: on some configurations the time between an update of the auth database takes so
  1.1254 +		// long that the first email send does not work. This is not a biggie on a live board (only
  1.1255 +		// the install mail will most likely fail) - but on a dynamic ip connection this might produce
  1.1256 +		// severe problems and is not fixable!
  1.1257 +		if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password)
  1.1258 +		{
  1.1259 +			global $config;
  1.1260 +
  1.1261 +			$errno = 0;
  1.1262 +			$errstr = '';
  1.1263 +
  1.1264 +			$this->server_send("QUIT");
  1.1265 +			fclose($this->socket);
  1.1266 +
  1.1267 +			$result = $this->pop_before_smtp($hostname, $username, $password);
  1.1268 +			$username = $password = $default_auth_method = '';
  1.1269 +
  1.1270 +			// We need to close the previous session, else the server is not
  1.1271 +			// able to get our ip for matching...
  1.1272 +			if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10))
  1.1273 +			{
  1.1274 +				if ($errstr)
  1.1275 +				{
  1.1276 +					$errstr = utf8_convert_message($errstr);
  1.1277 +				}
  1.1278 +
  1.1279 +				$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";
  1.1280 +				return $err_msg;
  1.1281 +			}
  1.1282 +
  1.1283 +			// Wait for reply
  1.1284 +			if ($err_msg = $this->server_parse('220', __LINE__))
  1.1285 +			{
  1.1286 +				$this->close_session($err_msg);
  1.1287 +				return $err_msg;
  1.1288 +			}
  1.1289 +		}
  1.1290 +
  1.1291 +		// Try EHLO first
  1.1292 +		$this->server_send("EHLO {$local_host}");
  1.1293 +		if ($err_msg = $this->server_parse('250', __LINE__))
  1.1294 +		{
  1.1295 +			// a 503 response code means that we're already authenticated
  1.1296 +			if ($this->numeric_response_code == 503)
  1.1297 +			{
  1.1298 +				return false;
  1.1299 +			}
  1.1300 +
  1.1301 +			// If EHLO fails, we try HELO
  1.1302 +			$this->server_send("HELO {$local_host}");
  1.1303 +			if ($err_msg = $this->server_parse('250', __LINE__))
  1.1304 +			{
  1.1305 +				return ($this->numeric_response_code == 503) ? false : $err_msg;
  1.1306 +			}
  1.1307 +		}
  1.1308 +
  1.1309 +		foreach ($this->responses as $response)
  1.1310 +		{
  1.1311 +			$response = explode(' ', $response);
  1.1312 +			$response_code = $response[0];
  1.1313 +			unset($response[0]);
  1.1314 +			$this->commands[$response_code] = implode(' ', $response);
  1.1315 +		}
  1.1316 +
  1.1317 +		// If we are not authenticated yet, something might be wrong if no username and passwd passed
  1.1318 +		if (!$username || !$password)
  1.1319 +		{
  1.1320 +			return false;
  1.1321 +		}
  1.1322 +
  1.1323 +		if (!isset($this->commands['AUTH']))
  1.1324 +		{
  1.1325 +			return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication';
  1.1326 +		}
  1.1327 +
  1.1328 +		// Get best authentication method
  1.1329 +		$available_methods = explode(' ', $this->commands['AUTH']);
  1.1330 +
  1.1331 +		// Define the auth ordering if the default auth method was not found
  1.1332 +		$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5');
  1.1333 +		$method = '';
  1.1334 +
  1.1335 +		if (in_array($default_auth_method, $available_methods))
  1.1336 +		{
  1.1337 +			$method = $default_auth_method;
  1.1338 +		}
  1.1339 +		else
  1.1340 +		{
  1.1341 +			foreach ($auth_methods as $_method)
  1.1342 +			{
  1.1343 +				if (in_array($_method, $available_methods))
  1.1344 +				{
  1.1345 +					$method = $_method;
  1.1346 +					break;
  1.1347 +				}
  1.1348 +			}
  1.1349 +		}
  1.1350 +
  1.1351 +		if (!$method)
  1.1352 +		{
  1.1353 +			return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods';
  1.1354 +		}
  1.1355 +
  1.1356 +		$method = strtolower(str_replace('-', '_', $method));
  1.1357 +		return $this->$method($username, $password);
  1.1358 +	}
  1.1359 +
  1.1360 +	/**
  1.1361 +	* Pop before smtp authentication
  1.1362 +	*/
  1.1363 +	function pop_before_smtp($hostname, $username, $password)
  1.1364 +	{
  1.1365 +		global $user;
  1.1366 +
  1.1367 +		if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10))
  1.1368 +		{
  1.1369 +			if ($errstr)
  1.1370 +			{
  1.1371 +				$errstr = utf8_convert_message($errstr);
  1.1372 +			}
  1.1373 +
  1.1374 +			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";
  1.1375 +		}
  1.1376 +
  1.1377 +		$this->server_send("USER $username", true);
  1.1378 +		if ($err_msg = $this->server_parse('+OK', __LINE__))
  1.1379 +		{
  1.1380 +			return $err_msg;
  1.1381 +		}
  1.1382 +
  1.1383 +		$this->server_send("PASS $password", true);
  1.1384 +		if ($err_msg = $this->server_parse('+OK', __LINE__))
  1.1385 +		{
  1.1386 +			return $err_msg;
  1.1387 +		}
  1.1388 +
  1.1389 +		$this->server_send('QUIT');
  1.1390 +		fclose($this->socket);
  1.1391 +
  1.1392 +		return false;
  1.1393 +	}
  1.1394 +
  1.1395 +	/**
  1.1396 +	* Plain authentication method
  1.1397 +	*/
  1.1398 +	function plain($username, $password)
  1.1399 +	{
  1.1400 +		$this->server_send('AUTH PLAIN');
  1.1401 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1402 +		{
  1.1403 +			return ($this->numeric_response_code == 503) ? false : $err_msg;
  1.1404 +		}
  1.1405 +
  1.1406 +		$base64_method_plain = base64_encode("\0" . $username . "\0" . $password);
  1.1407 +		$this->server_send($base64_method_plain, true);
  1.1408 +		if ($err_msg = $this->server_parse('235', __LINE__))
  1.1409 +		{
  1.1410 +			return $err_msg;
  1.1411 +		}
  1.1412 +
  1.1413 +		return false;
  1.1414 +	}
  1.1415 +
  1.1416 +	/**
  1.1417 +	* Login authentication method
  1.1418 +	*/
  1.1419 +	function login($username, $password)
  1.1420 +	{
  1.1421 +		$this->server_send('AUTH LOGIN');
  1.1422 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1423 +		{
  1.1424 +			return ($this->numeric_response_code == 503) ? false : $err_msg;
  1.1425 +		}
  1.1426 +
  1.1427 +		$this->server_send(base64_encode($username), true);
  1.1428 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1429 +		{
  1.1430 +			return $err_msg;
  1.1431 +		}
  1.1432 +
  1.1433 +		$this->server_send(base64_encode($password), true);
  1.1434 +		if ($err_msg = $this->server_parse('235', __LINE__))
  1.1435 +		{
  1.1436 +			return $err_msg;
  1.1437 +		}
  1.1438 +
  1.1439 +		return false;
  1.1440 +	}
  1.1441 +
  1.1442 +	/**
  1.1443 +	* cram_md5 authentication method
  1.1444 +	*/
  1.1445 +	function cram_md5($username, $password)
  1.1446 +	{
  1.1447 +		$this->server_send('AUTH CRAM-MD5');
  1.1448 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1449 +		{
  1.1450 +			return ($this->numeric_response_code == 503) ? false : $err_msg;
  1.1451 +		}
  1.1452 +
  1.1453 +		$md5_challenge = base64_decode($this->responses[0]);
  1.1454 +		$password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password);
  1.1455 +		$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))));
  1.1456 +
  1.1457 +		$base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest);
  1.1458 +
  1.1459 +		$this->server_send($base64_method_cram_md5, true);
  1.1460 +		if ($err_msg = $this->server_parse('235', __LINE__))
  1.1461 +		{
  1.1462 +			return $err_msg;
  1.1463 +		}
  1.1464 +
  1.1465 +		return false;
  1.1466 +	}
  1.1467 +
  1.1468 +	/**
  1.1469 +	* digest_md5 authentication method
  1.1470 +	* A real pain in the ***
  1.1471 +	*/
  1.1472 +	function digest_md5($username, $password)
  1.1473 +	{
  1.1474 +		global $config, $user;
  1.1475 +
  1.1476 +		$this->server_send('AUTH DIGEST-MD5');
  1.1477 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1478 +		{
  1.1479 +			return ($this->numeric_response_code == 503) ? false : $err_msg;
  1.1480 +		}
  1.1481 +
  1.1482 +		$md5_challenge = base64_decode($this->responses[0]);
  1.1483 +
  1.1484 +		// Parse the md5 challenge - from AUTH_SASL (PEAR)
  1.1485 +		$tokens = array();
  1.1486 +		while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches))
  1.1487 +		{
  1.1488 +			// Ignore these as per rfc2831
  1.1489 +			if ($matches[1] == 'opaque' || $matches[1] == 'domain')
  1.1490 +			{
  1.1491 +				$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
  1.1492 +				continue;
  1.1493 +			}
  1.1494 +
  1.1495 +			// Allowed multiple "realm" and "auth-param"
  1.1496 +			if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param'))
  1.1497 +			{
  1.1498 +				if (is_array($tokens[$matches[1]]))
  1.1499 +				{
  1.1500 +					$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
  1.1501 +				}
  1.1502 +				else
  1.1503 +				{
  1.1504 +					$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
  1.1505 +				}
  1.1506 +			}
  1.1507 +			else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure
  1.1508 +			{
  1.1509 +				$tokens = array();
  1.1510 +				break;
  1.1511 +			}
  1.1512 +			else
  1.1513 +			{
  1.1514 +				$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
  1.1515 +			}
  1.1516 +
  1.1517 +			// Remove the just parsed directive from the challenge
  1.1518 +			$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
  1.1519 +		}
  1.1520 +
  1.1521 +		// Realm
  1.1522 +		if (empty($tokens['realm']))
  1.1523 +		{
  1.1524 +			$tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host;
  1.1525 +		}
  1.1526 +
  1.1527 +		// Maxbuf
  1.1528 +		if (empty($tokens['maxbuf']))
  1.1529 +		{
  1.1530 +			$tokens['maxbuf'] = 65536;
  1.1531 +		}
  1.1532 +
  1.1533 +		// Required: nonce, algorithm
  1.1534 +		if (empty($tokens['nonce']) || empty($tokens['algorithm']))
  1.1535 +		{
  1.1536 +			$tokens = array();
  1.1537 +		}
  1.1538 +		$md5_challenge = $tokens;
  1.1539 +
  1.1540 +		if (!empty($md5_challenge))
  1.1541 +		{
  1.1542 +			$str = '';
  1.1543 +			for ($i = 0; $i < 32; $i++)
  1.1544 +			{
  1.1545 +				$str .= chr(mt_rand(0, 255));
  1.1546 +			}
  1.1547 +			$cnonce = base64_encode($str);
  1.1548 +
  1.1549 +			$digest_uri = 'smtp/' . $config['smtp_host'];
  1.1550 +
  1.1551 +			$auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce);
  1.1552 +			$auth_2 = 'AUTHENTICATE:' . $digest_uri;
  1.1553 +			$response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2)));
  1.1554 +
  1.1555 +			$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']);
  1.1556 +		}
  1.1557 +		else
  1.1558 +		{
  1.1559 +			return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge';
  1.1560 +		}
  1.1561 +
  1.1562 +		$base64_method_digest_md5 = base64_encode($input_string);
  1.1563 +		$this->server_send($base64_method_digest_md5, true);
  1.1564 +		if ($err_msg = $this->server_parse('334', __LINE__))
  1.1565 +		{
  1.1566 +			return $err_msg;
  1.1567 +		}
  1.1568 +
  1.1569 +		$this->server_send(' ');
  1.1570 +		if ($err_msg = $this->server_parse('235', __LINE__))
  1.1571 +		{
  1.1572 +			return $err_msg;
  1.1573 +		}
  1.1574 +
  1.1575 +		return false;
  1.1576 +	}
  1.1577 +}
  1.1578 +
  1.1579 +/**
  1.1580 +* Encodes the given string for proper display in UTF-8.
  1.1581 +*
  1.1582 +* This version is using base64 encoded data. The downside of this
  1.1583 +* is if the mail client does not understand this encoding the user
  1.1584 +* is basically doomed with an unreadable subject.
  1.1585 +*
  1.1586 +* Please note that this version fully supports RFC 2045 section 6.8.
  1.1587 +*
  1.1588 +* @param string $eol End of line we are using (optional to be backwards compatible)
  1.1589 +*/
  1.1590 +function mail_encode($str, $eol = "\r\n")
  1.1591 +{
  1.1592 +	// define start delimimter, end delimiter and spacer
  1.1593 +	$start = "=?UTF-8?B?";
  1.1594 +	$end = "?=";
  1.1595 +	$delimiter = "$eol ";
  1.1596 +
  1.1597 +	// Maximum length is 75. $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $delimiter . $end)!!!
  1.1598 +	$split_length = 60;
  1.1599 +	$encoded_str = base64_encode($str);
  1.1600 +
  1.1601 +	// If encoded string meets the limits, we just return with the correct data.
  1.1602 +	if (strlen($encoded_str) <= $split_length)
  1.1603 +	{
  1.1604 +		return $start . $encoded_str . $end;
  1.1605 +	}
  1.1606 +
  1.1607 +	// If there is only ASCII data, we just return what we want, correctly splitting the lines.
  1.1608 +	if (strlen($str) === utf8_strlen($str))
  1.1609 +	{
  1.1610 +		return $start . implode($end . $delimiter . $start, str_split($encoded_str, $split_length)) . $end;
  1.1611 +	}
  1.1612 +
  1.1613 +	// UTF-8 data, compose encoded lines
  1.1614 +	$array = utf8_str_split($str);
  1.1615 +	$str = '';
  1.1616 +
  1.1617 +	while (sizeof($array))
  1.1618 +	{
  1.1619 +		$text = '';
  1.1620 +
  1.1621 +		while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length)
  1.1622 +		{
  1.1623 +			$text .= array_shift($array);
  1.1624 +		}
  1.1625 +
  1.1626 +		$str .= $start . base64_encode($text) . $end . $delimiter;
  1.1627 +	}
  1.1628 +
  1.1629 +	return substr($str, 0, -strlen($delimiter));
  1.1630 +}
  1.1631 +
  1.1632 +/**
  1.1633 +* Wrapper for sending out emails with the PHP's mail function
  1.1634 +*/
  1.1635 +function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg)
  1.1636 +{
  1.1637 +	global $config, $phpbb_root_path, $phpEx;
  1.1638 +
  1.1639 +	// We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used...
  1.1640 +	// Reference: http://bugs.php.net/bug.php?id=15841
  1.1641 +	$headers = implode($eol, $headers);
  1.1642 +
  1.1643 +	if (!class_exists('phpbb_error_collector'))
  1.1644 +	{
  1.1645 +		include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
  1.1646 +	}
  1.1647 +
  1.1648 +	$collector = new phpbb_error_collector;
  1.1649 +	$collector->install();
  1.1650 +
  1.1651 +	// On some PHP Versions mail() *may* fail if there are newlines within the subject.
  1.1652 +	// Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8.
  1.1653 +	// Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space (Use '' as parameter to mail_encode() results in SPACE used)
  1.1654 +	$result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers);
  1.1655 +
  1.1656 +	$collector->uninstall();
  1.1657 +	$err_msg = $collector->format_errors();
  1.1658 +
  1.1659 +	return $result;
  1.1660 +}
  1.1661 +
  1.1662 +?>
  1.1663 \ No newline at end of file