includes/functions_messenger.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Thu, 08 Mar 2012 01:33:19 +0400
changeset 11 0a7892c58331
parent 0 ceef6d64c21d
permissions -rw-r--r--
Style fix
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package phpBB3
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Messenger
indvdum@0
    21
* @package phpBB3
indvdum@0
    22
*/
indvdum@0
    23
class messenger
indvdum@0
    24
{
indvdum@0
    25
	var $vars, $msg, $extra_headers, $replyto, $from, $subject;
indvdum@0
    26
	var $addresses = array();
indvdum@0
    27
indvdum@0
    28
	var $mail_priority = MAIL_NORMAL_PRIORITY;
indvdum@0
    29
	var $use_queue = true;
indvdum@0
    30
indvdum@0
    31
	var $tpl_obj = NULL;
indvdum@0
    32
	var $tpl_msg = array();
indvdum@0
    33
	var $eol = "\n";
indvdum@0
    34
indvdum@0
    35
	/**
indvdum@0
    36
	* Constructor
indvdum@0
    37
	*/
indvdum@0
    38
	function messenger($use_queue = true)
indvdum@0
    39
	{
indvdum@0
    40
		global $config;
indvdum@0
    41
indvdum@0
    42
		$this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
indvdum@0
    43
		$this->subject = '';
indvdum@0
    44
indvdum@0
    45
		// Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
indvdum@0
    46
		$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
indvdum@0
    47
		$this->eol = (!$this->eol) ? "\n" : $this->eol;
indvdum@0
    48
	}
indvdum@0
    49
indvdum@0
    50
	/**
indvdum@0
    51
	* Resets all the data (address, template file, etc etc) to default
indvdum@0
    52
	*/
indvdum@0
    53
	function reset()
indvdum@0
    54
	{
indvdum@0
    55
		$this->addresses = $this->extra_headers = array();
indvdum@0
    56
		$this->vars = $this->msg = $this->replyto = $this->from = '';
indvdum@0
    57
		$this->mail_priority = MAIL_NORMAL_PRIORITY;
indvdum@0
    58
	}
indvdum@0
    59
indvdum@0
    60
	/**
indvdum@0
    61
	* Sets an email address to send to
indvdum@0
    62
	*/
indvdum@0
    63
	function to($address, $realname = '')
indvdum@0
    64
	{
indvdum@0
    65
		global $config;
indvdum@0
    66
indvdum@0
    67
		if (!trim($address))
indvdum@0
    68
		{
indvdum@0
    69
			return;
indvdum@0
    70
		}
indvdum@0
    71
indvdum@0
    72
		$pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
indvdum@0
    73
indvdum@0
    74
		$this->addresses['to'][$pos]['email'] = trim($address);
indvdum@0
    75
indvdum@0
    76
		// If empty sendmail_path on windows, PHP changes the to line
indvdum@0
    77
		if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
indvdum@0
    78
		{
indvdum@0
    79
			$this->addresses['to'][$pos]['name'] = '';
indvdum@0
    80
		}
indvdum@0
    81
		else
indvdum@0
    82
		{
indvdum@0
    83
			$this->addresses['to'][$pos]['name'] = trim($realname);
indvdum@0
    84
		}
indvdum@0
    85
	}
indvdum@0
    86
indvdum@0
    87
	/**
indvdum@0
    88
	* Sets an cc address to send to
indvdum@0
    89
	*/
indvdum@0
    90
	function cc($address, $realname = '')
indvdum@0
    91
	{
indvdum@0
    92
		if (!trim($address))
indvdum@0
    93
		{
indvdum@0
    94
			return;
indvdum@0
    95
		}
indvdum@0
    96
indvdum@0
    97
		$pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
indvdum@0
    98
		$this->addresses['cc'][$pos]['email'] = trim($address);
indvdum@0
    99
		$this->addresses['cc'][$pos]['name'] = trim($realname);
indvdum@0
   100
	}
indvdum@0
   101
indvdum@0
   102
	/**
indvdum@0
   103
	* Sets an bcc address to send to
indvdum@0
   104
	*/
indvdum@0
   105
	function bcc($address, $realname = '')
indvdum@0
   106
	{
indvdum@0
   107
		if (!trim($address))
indvdum@0
   108
		{
indvdum@0
   109
			return;
indvdum@0
   110
		}
indvdum@0
   111
indvdum@0
   112
		$pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
indvdum@0
   113
		$this->addresses['bcc'][$pos]['email'] = trim($address);
indvdum@0
   114
		$this->addresses['bcc'][$pos]['name'] = trim($realname);
indvdum@0
   115
	}
indvdum@0
   116
indvdum@0
   117
	/**
indvdum@0
   118
	* Sets a im contact to send to
indvdum@0
   119
	*/
indvdum@0
   120
	function im($address, $realname = '')
indvdum@0
   121
	{
indvdum@0
   122
		// IM-Addresses could be empty
indvdum@0
   123
		if (!trim($address))
indvdum@0
   124
		{
indvdum@0
   125
			return;
indvdum@0
   126
		}
indvdum@0
   127
indvdum@0
   128
		$pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
indvdum@0
   129
		$this->addresses['im'][$pos]['uid'] = trim($address);
indvdum@0
   130
		$this->addresses['im'][$pos]['name'] = trim($realname);
indvdum@0
   131
	}
indvdum@0
   132
indvdum@0
   133
	/**
indvdum@0
   134
	* Set the reply to address
indvdum@0
   135
	*/
indvdum@0
   136
	function replyto($address)
indvdum@0
   137
	{
indvdum@0
   138
		$this->replyto = trim($address);
indvdum@0
   139
	}
indvdum@0
   140
indvdum@0
   141
	/**
indvdum@0
   142
	* Set the from address
indvdum@0
   143
	*/
indvdum@0
   144
	function from($address)
indvdum@0
   145
	{
indvdum@0
   146
		$this->from = trim($address);
indvdum@0
   147
	}
indvdum@0
   148
indvdum@0
   149
	/**
indvdum@0
   150
	* set up subject for mail
indvdum@0
   151
	*/
indvdum@0
   152
	function subject($subject = '')
indvdum@0
   153
	{
indvdum@0
   154
		$this->subject = trim($subject);
indvdum@0
   155
	}
indvdum@0
   156
indvdum@0
   157
	/**
indvdum@0
   158
	* set up extra mail headers
indvdum@0
   159
	*/
indvdum@0
   160
	function headers($headers)
indvdum@0
   161
	{
indvdum@0
   162
		$this->extra_headers[] = trim($headers);
indvdum@0
   163
	}
indvdum@0
   164
indvdum@0
   165
	/**
indvdum@0
   166
	* Adds X-AntiAbuse headers
indvdum@0
   167
	*
indvdum@0
   168
	* @param array $config		Configuration array
indvdum@0
   169
	* @param user $user			A user object
indvdum@0
   170
	*
indvdum@0
   171
	* @return null
indvdum@0
   172
	*/
indvdum@0
   173
	function anti_abuse_headers($config, $user)
indvdum@0
   174
	{
indvdum@0
   175
		$this->headers('X-AntiAbuse: Board servername - ' . mail_encode($config['server_name']));
indvdum@0
   176
		$this->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
indvdum@0
   177
		$this->headers('X-AntiAbuse: Username - ' . mail_encode($user->data['username']));
indvdum@0
   178
		$this->headers('X-AntiAbuse: User IP - ' . $user->ip);
indvdum@0
   179
	}
indvdum@0
   180
indvdum@0
   181
	/**
indvdum@0
   182
	* Set the email priority
indvdum@0
   183
	*/
indvdum@0
   184
	function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
indvdum@0
   185
	{
indvdum@0
   186
		$this->mail_priority = $priority;
indvdum@0
   187
	}
indvdum@0
   188
indvdum@0
   189
	/**
indvdum@0
   190
	* Set email template to use
indvdum@0
   191
	*/
indvdum@0
   192
	function template($template_file, $template_lang = '', $template_path = '')
indvdum@0
   193
	{
indvdum@0
   194
		global $config, $phpbb_root_path, $user;
indvdum@0
   195
indvdum@0
   196
		if (!trim($template_file))
indvdum@0
   197
		{
indvdum@0
   198
			trigger_error('No template file for emailing set.', E_USER_ERROR);
indvdum@0
   199
		}
indvdum@0
   200
indvdum@0
   201
		if (!trim($template_lang))
indvdum@0
   202
		{
indvdum@0
   203
			// fall back to board default language if the user's language is
indvdum@0
   204
			// missing $template_file.  If this does not exist either,
indvdum@0
   205
			// $tpl->set_custom_template will do a trigger_error
indvdum@0
   206
			$template_lang = basename($config['default_lang']);
indvdum@0
   207
		}
indvdum@0
   208
indvdum@0
   209
		// tpl_msg now holds a template object we can use to parse the template file
indvdum@0
   210
		if (!isset($this->tpl_msg[$template_lang . $template_file]))
indvdum@0
   211
		{
indvdum@0
   212
			$this->tpl_msg[$template_lang . $template_file] = new template();
indvdum@0
   213
			$tpl = &$this->tpl_msg[$template_lang . $template_file];
indvdum@0
   214
indvdum@0
   215
			$fallback_template_path = false;
indvdum@0
   216
indvdum@0
   217
			if (!$template_path)
indvdum@0
   218
			{
indvdum@0
   219
				$template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
indvdum@0
   220
				$template_path .= $template_lang . '/email';
indvdum@0
   221
indvdum@0
   222
				// we can only specify default language fallback when the path is not a custom one for which we
indvdum@0
   223
				// do not know the default language alternative
indvdum@0
   224
				if ($template_lang !== basename($config['default_lang']))
indvdum@0
   225
				{
indvdum@0
   226
					$fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
indvdum@0
   227
					$fallback_template_path .= basename($config['default_lang']) . '/email';
indvdum@0
   228
				}
indvdum@0
   229
			}
indvdum@0
   230
indvdum@0
   231
			$tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path);
indvdum@0
   232
indvdum@0
   233
			$tpl->set_filenames(array(
indvdum@0
   234
				'body'		=> $template_file . '.txt',
indvdum@0
   235
			));
indvdum@0
   236
		}
indvdum@0
   237
indvdum@0
   238
		$this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file];
indvdum@0
   239
		$this->vars = &$this->tpl_obj->_rootref;
indvdum@0
   240
		$this->tpl_msg = '';
indvdum@0
   241
indvdum@0
   242
		return true;
indvdum@0
   243
	}
indvdum@0
   244
indvdum@0
   245
	/**
indvdum@0
   246
	* assign variables to email template
indvdum@0
   247
	*/
indvdum@0
   248
	function assign_vars($vars)
indvdum@0
   249
	{
indvdum@0
   250
		if (!is_object($this->tpl_obj))
indvdum@0
   251
		{
indvdum@0
   252
			return;
indvdum@0
   253
		}
indvdum@0
   254
indvdum@0
   255
		$this->tpl_obj->assign_vars($vars);
indvdum@0
   256
	}
indvdum@0
   257
indvdum@0
   258
	function assign_block_vars($blockname, $vars)
indvdum@0
   259
	{
indvdum@0
   260
		if (!is_object($this->tpl_obj))
indvdum@0
   261
		{
indvdum@0
   262
			return;
indvdum@0
   263
		}
indvdum@0
   264
indvdum@0
   265
		$this->tpl_obj->assign_block_vars($blockname, $vars);
indvdum@0
   266
	}
indvdum@0
   267
indvdum@0
   268
	/**
indvdum@0
   269
	* Send the mail out to the recipients set previously in var $this->addresses
indvdum@0
   270
	*/
indvdum@0
   271
	function send($method = NOTIFY_EMAIL, $break = false)
indvdum@0
   272
	{
indvdum@0
   273
		global $config, $user;
indvdum@0
   274
indvdum@0
   275
		// We add some standard variables we always use, no need to specify them always
indvdum@0
   276
		if (!isset($this->vars['U_BOARD']))
indvdum@0
   277
		{
indvdum@0
   278
			$this->assign_vars(array(
indvdum@0
   279
				'U_BOARD'	=> generate_board_url(),
indvdum@0
   280
			));
indvdum@0
   281
		}
indvdum@0
   282
indvdum@0
   283
		if (!isset($this->vars['EMAIL_SIG']))
indvdum@0
   284
		{
indvdum@0
   285
			$this->assign_vars(array(
indvdum@0
   286
				'EMAIL_SIG'	=> str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])),
indvdum@0
   287
			));
indvdum@0
   288
		}
indvdum@0
   289
indvdum@0
   290
		if (!isset($this->vars['SITENAME']))
indvdum@0
   291
		{
indvdum@0
   292
			$this->assign_vars(array(
indvdum@0
   293
				'SITENAME'	=> htmlspecialchars_decode($config['sitename']),
indvdum@0
   294
			));
indvdum@0
   295
		}
indvdum@0
   296
indvdum@0
   297
		// Parse message through template
indvdum@0
   298
		$this->msg = trim($this->tpl_obj->assign_display('body'));
indvdum@0
   299
indvdum@0
   300
		// 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
indvdum@0
   301
		$this->msg = str_replace("\r\n", "\n", $this->msg);
indvdum@0
   302
indvdum@0
   303
		// We now try and pull a subject from the email body ... if it exists,
indvdum@0
   304
		// do this here because the subject may contain a variable
indvdum@0
   305
		$drop_header = '';
indvdum@0
   306
		$match = array();
indvdum@0
   307
		if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
indvdum@0
   308
		{
indvdum@0
   309
			$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
indvdum@0
   310
			$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
indvdum@0
   311
		}
indvdum@0
   312
		else
indvdum@0
   313
		{
indvdum@0
   314
			$this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
indvdum@0
   315
		}
indvdum@0
   316
indvdum@0
   317
		if ($drop_header)
indvdum@0
   318
		{
indvdum@0
   319
			$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
indvdum@0
   320
		}
indvdum@0
   321
indvdum@0
   322
		if ($break)
indvdum@0
   323
		{
indvdum@0
   324
			return true;
indvdum@0
   325
		}
indvdum@0
   326
indvdum@0
   327
		switch ($method)
indvdum@0
   328
		{
indvdum@0
   329
			case NOTIFY_EMAIL:
indvdum@0
   330
				$result = $this->msg_email();
indvdum@0
   331
			break;
indvdum@0
   332
indvdum@0
   333
			case NOTIFY_IM:
indvdum@0
   334
				$result = $this->msg_jabber();
indvdum@0
   335
			break;
indvdum@0
   336
indvdum@0
   337
			case NOTIFY_BOTH:
indvdum@0
   338
				$result = $this->msg_email();
indvdum@0
   339
				$this->msg_jabber();
indvdum@0
   340
			break;
indvdum@0
   341
		}
indvdum@0
   342
indvdum@0
   343
		$this->reset();
indvdum@0
   344
		return $result;
indvdum@0
   345
	}
indvdum@0
   346
indvdum@0
   347
	/**
indvdum@0
   348
	* Add error message to log
indvdum@0
   349
	*/
indvdum@0
   350
	function error($type, $msg)
indvdum@0
   351
	{
indvdum@0
   352
		global $user, $phpEx, $phpbb_root_path, $config;
indvdum@0
   353
indvdum@0
   354
		// Session doesn't exist, create it
indvdum@0
   355
		if (!isset($user->session_id) || $user->session_id === '')
indvdum@0
   356
		{
indvdum@0
   357
			$user->session_begin();
indvdum@0
   358
		}
indvdum@0
   359
indvdum@0
   360
		$calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
indvdum@0
   361
indvdum@0
   362
		$message = '';
indvdum@0
   363
		switch ($type)
indvdum@0
   364
		{
indvdum@0
   365
			case 'EMAIL':
indvdum@0
   366
				$message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '</strong>';
indvdum@0
   367
			break;
indvdum@0
   368
indvdum@0
   369
			default:
indvdum@0
   370
				$message = "<strong>$type</strong>";
indvdum@0
   371
			break;
indvdum@0
   372
		}
indvdum@0
   373
indvdum@0
   374
		$message .= '<br /><em>' . htmlspecialchars($calling_page) . '</em><br /><br />' . $msg . '<br />';
indvdum@0
   375
		add_log('critical', 'LOG_ERROR_' . $type, $message);
indvdum@0
   376
	}
indvdum@0
   377
indvdum@0
   378
	/**
indvdum@0
   379
	* Save to queue
indvdum@0
   380
	*/
indvdum@0
   381
	function save_queue()
indvdum@0
   382
	{
indvdum@0
   383
		global $config;
indvdum@0
   384
indvdum@0
   385
		if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
indvdum@0
   386
		{
indvdum@0
   387
			$this->queue->save();
indvdum@0
   388
			return;
indvdum@0
   389
		}
indvdum@0
   390
	}
indvdum@0
   391
indvdum@0
   392
	/**
indvdum@0
   393
	* Return email header
indvdum@0
   394
	*/
indvdum@0
   395
	function build_header($to, $cc, $bcc)
indvdum@0
   396
	{
indvdum@0
   397
		global $config;
indvdum@0
   398
indvdum@0
   399
		// We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
indvdum@0
   400
		$headers = array();
indvdum@0
   401
indvdum@0
   402
		$headers[] = 'From: ' . $this->from;
indvdum@0
   403
indvdum@0
   404
		if ($cc)
indvdum@0
   405
		{
indvdum@0
   406
			$headers[] = 'Cc: ' . $cc;
indvdum@0
   407
		}
indvdum@0
   408
indvdum@0
   409
		if ($bcc)
indvdum@0
   410
		{
indvdum@0
   411
			$headers[] = 'Bcc: ' . $bcc;
indvdum@0
   412
		}
indvdum@0
   413
indvdum@0
   414
		$headers[] = 'Reply-To: ' . $this->replyto;
indvdum@0
   415
		$headers[] = 'Return-Path: <' . $config['board_email'] . '>';
indvdum@0
   416
		$headers[] = 'Sender: <' . $config['board_email'] . '>';
indvdum@0
   417
		$headers[] = 'MIME-Version: 1.0';
indvdum@0
   418
		$headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
indvdum@0
   419
		$headers[] = 'Date: ' . date('r', time());
indvdum@0
   420
		$headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
indvdum@0
   421
		$headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
indvdum@0
   422
indvdum@0
   423
		$headers[] = 'X-Priority: ' . $this->mail_priority;
indvdum@0
   424
		$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
indvdum@0
   425
		$headers[] = 'X-Mailer: phpBB3';
indvdum@0
   426
		$headers[] = 'X-MimeOLE: phpBB3';
indvdum@0
   427
		$headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
indvdum@0
   428
indvdum@0
   429
		if (sizeof($this->extra_headers))
indvdum@0
   430
		{
indvdum@0
   431
			$headers = array_merge($headers, $this->extra_headers);
indvdum@0
   432
		}
indvdum@0
   433
indvdum@0
   434
		return $headers;
indvdum@0
   435
	}
indvdum@0
   436
indvdum@0
   437
	/**
indvdum@0
   438
	* Send out emails
indvdum@0
   439
	*/
indvdum@0
   440
	function msg_email()
indvdum@0
   441
	{
indvdum@0
   442
		global $config, $user;
indvdum@0
   443
indvdum@0
   444
		if (empty($config['email_enable']))
indvdum@0
   445
		{
indvdum@0
   446
			return false;
indvdum@0
   447
		}
indvdum@0
   448
indvdum@0
   449
		// Addresses to send to?
indvdum@0
   450
		if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc'])))
indvdum@0
   451
		{
indvdum@0
   452
			// Send was successful. ;)
indvdum@0
   453
			return true;
indvdum@0
   454
		}
indvdum@0
   455
indvdum@0
   456
		$use_queue = false;
indvdum@0
   457
		if ($config['email_package_size'] && $this->use_queue)
indvdum@0
   458
		{
indvdum@0
   459
			if (empty($this->queue))
indvdum@0
   460
			{
indvdum@0
   461
				$this->queue = new queue();
indvdum@0
   462
				$this->queue->init('email', $config['email_package_size']);
indvdum@0
   463
			}
indvdum@0
   464
			$use_queue = true;
indvdum@0
   465
		}
indvdum@0
   466
indvdum@0
   467
		if (empty($this->replyto))
indvdum@0
   468
		{
indvdum@0
   469
			$this->replyto = '<' . $config['board_contact'] . '>';
indvdum@0
   470
		}
indvdum@0
   471
indvdum@0
   472
		if (empty($this->from))
indvdum@0
   473
		{
indvdum@0
   474
			$this->from = '<' . $config['board_contact'] . '>';
indvdum@0
   475
		}
indvdum@0
   476
indvdum@0
   477
		$encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol;
indvdum@0
   478
indvdum@0
   479
		// Build to, cc and bcc strings
indvdum@0
   480
		$to = $cc = $bcc = '';
indvdum@0
   481
		foreach ($this->addresses as $type => $address_ary)
indvdum@0
   482
		{
indvdum@0
   483
			if ($type == 'im')
indvdum@0
   484
			{
indvdum@0
   485
				continue;
indvdum@0
   486
			}
indvdum@0
   487
indvdum@0
   488
			foreach ($address_ary as $which_ary)
indvdum@0
   489
			{
indvdum@0
   490
				$$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']);
indvdum@0
   491
			}
indvdum@0
   492
		}
indvdum@0
   493
indvdum@0
   494
		// Build header
indvdum@0
   495
		$headers = $this->build_header($to, $cc, $bcc);
indvdum@0
   496
indvdum@0
   497
		// Send message ...
indvdum@0
   498
		if (!$use_queue)
indvdum@0
   499
		{
indvdum@0
   500
			$mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
indvdum@0
   501
			$err_msg = '';
indvdum@0
   502
indvdum@0
   503
			if ($config['smtp_delivery'])
indvdum@0
   504
			{
indvdum@0
   505
				$result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
indvdum@0
   506
			}
indvdum@0
   507
			else
indvdum@0
   508
			{
indvdum@0
   509
				$result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg);
indvdum@0
   510
			}
indvdum@0
   511
indvdum@0
   512
			if (!$result)
indvdum@0
   513
			{
indvdum@0
   514
				$this->error('EMAIL', $err_msg);
indvdum@0
   515
				return false;
indvdum@0
   516
			}
indvdum@0
   517
		}
indvdum@0
   518
		else
indvdum@0
   519
		{
indvdum@0
   520
			$this->queue->put('email', array(
indvdum@0
   521
				'to'			=> $to,
indvdum@0
   522
				'addresses'		=> $this->addresses,
indvdum@0
   523
				'subject'		=> $this->subject,
indvdum@0
   524
				'msg'			=> $this->msg,
indvdum@0
   525
				'headers'		=> $headers)
indvdum@0
   526
			);
indvdum@0
   527
		}
indvdum@0
   528
indvdum@0
   529
		return true;
indvdum@0
   530
	}
indvdum@0
   531
indvdum@0
   532
	/**
indvdum@0
   533
	* Send jabber message out
indvdum@0
   534
	*/
indvdum@0
   535
	function msg_jabber()
indvdum@0
   536
	{
indvdum@0
   537
		global $config, $db, $user, $phpbb_root_path, $phpEx;
indvdum@0
   538
indvdum@0
   539
		if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
indvdum@0
   540
		{
indvdum@0
   541
			return false;
indvdum@0
   542
		}
indvdum@0
   543
indvdum@0
   544
		if (empty($this->addresses['im']))
indvdum@0
   545
		{
indvdum@0
   546
			// Send was successful. ;)
indvdum@0
   547
			return true;
indvdum@0
   548
		}
indvdum@0
   549
indvdum@0
   550
		$use_queue = false;
indvdum@0
   551
		if ($config['jab_package_size'] && $this->use_queue)
indvdum@0
   552
		{
indvdum@0
   553
			if (empty($this->queue))
indvdum@0
   554
			{
indvdum@0
   555
				$this->queue = new queue();
indvdum@0
   556
				$this->queue->init('jabber', $config['jab_package_size']);
indvdum@0
   557
			}
indvdum@0
   558
			$use_queue = true;
indvdum@0
   559
		}
indvdum@0
   560
indvdum@0
   561
		$addresses = array();
indvdum@0
   562
		foreach ($this->addresses['im'] as $type => $uid_ary)
indvdum@0
   563
		{
indvdum@0
   564
			$addresses[] = $uid_ary['uid'];
indvdum@0
   565
		}
indvdum@0
   566
		$addresses = array_unique($addresses);
indvdum@0
   567
indvdum@0
   568
		if (!$use_queue)
indvdum@0
   569
		{
indvdum@0
   570
			include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
indvdum@0
   571
			$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
indvdum@0
   572
indvdum@0
   573
			if (!$this->jabber->connect())
indvdum@0
   574
			{
indvdum@0
   575
				$this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log());
indvdum@0
   576
				return false;
indvdum@0
   577
			}
indvdum@0
   578
indvdum@0
   579
			if (!$this->jabber->login())
indvdum@0
   580
			{
indvdum@0
   581
				$this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log());
indvdum@0
   582
				return false;
indvdum@0
   583
			}
indvdum@0
   584
indvdum@0
   585
			foreach ($addresses as $address)
indvdum@0
   586
			{
indvdum@0
   587
				$this->jabber->send_message($address, $this->msg, $this->subject);
indvdum@0
   588
			}
indvdum@0
   589
indvdum@0
   590
			$this->jabber->disconnect();
indvdum@0
   591
		}
indvdum@0
   592
		else
indvdum@0
   593
		{
indvdum@0
   594
			$this->queue->put('jabber', array(
indvdum@0
   595
				'addresses'		=> $addresses,
indvdum@0
   596
				'subject'		=> $this->subject,
indvdum@0
   597
				'msg'			=> $this->msg)
indvdum@0
   598
			);
indvdum@0
   599
		}
indvdum@0
   600
		unset($addresses);
indvdum@0
   601
		return true;
indvdum@0
   602
	}
indvdum@0
   603
}
indvdum@0
   604
indvdum@0
   605
/**
indvdum@0
   606
* handling email and jabber queue
indvdum@0
   607
* @package phpBB3
indvdum@0
   608
*/
indvdum@0
   609
class queue
indvdum@0
   610
{
indvdum@0
   611
	var $data = array();
indvdum@0
   612
	var $queue_data = array();
indvdum@0
   613
	var $package_size = 0;
indvdum@0
   614
	var $cache_file = '';
indvdum@0
   615
	var $eol = "\n";
indvdum@0
   616
indvdum@0
   617
	/**
indvdum@0
   618
	* constructor
indvdum@0
   619
	*/
indvdum@0
   620
	function queue()
indvdum@0
   621
	{
indvdum@0
   622
		global $phpEx, $phpbb_root_path;
indvdum@0
   623
indvdum@0
   624
		$this->data = array();
indvdum@0
   625
		$this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
indvdum@0
   626
indvdum@0
   627
		// Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
indvdum@0
   628
		$this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
indvdum@0
   629
		$this->eol = (!$this->eol) ? "\n" : $this->eol;
indvdum@0
   630
	}
indvdum@0
   631
indvdum@0
   632
	/**
indvdum@0
   633
	* Init a queue object
indvdum@0
   634
	*/
indvdum@0
   635
	function init($object, $package_size)
indvdum@0
   636
	{
indvdum@0
   637
		$this->data[$object] = array();
indvdum@0
   638
		$this->data[$object]['package_size'] = $package_size;
indvdum@0
   639
		$this->data[$object]['data'] = array();
indvdum@0
   640
	}
indvdum@0
   641
indvdum@0
   642
	/**
indvdum@0
   643
	* Put object in queue
indvdum@0
   644
	*/
indvdum@0
   645
	function put($object, $scope)
indvdum@0
   646
	{
indvdum@0
   647
		$this->data[$object]['data'][] = $scope;
indvdum@0
   648
	}
indvdum@0
   649
indvdum@0
   650
	/**
indvdum@0
   651
	* Obtains exclusive lock on queue cache file.
indvdum@0
   652
	* Returns resource representing the lock
indvdum@0
   653
	*/
indvdum@0
   654
	function lock()
indvdum@0
   655
	{
indvdum@0
   656
		// For systems that can't have two processes opening
indvdum@0
   657
		// one file for writing simultaneously
indvdum@0
   658
		if (file_exists($this->cache_file . '.lock'))
indvdum@0
   659
		{
indvdum@0
   660
			$mode = 'rb';
indvdum@0
   661
		}
indvdum@0
   662
		else
indvdum@0
   663
		{
indvdum@0
   664
			$mode = 'wb';
indvdum@0
   665
		}
indvdum@0
   666
indvdum@0
   667
		$lock_fp = @fopen($this->cache_file . '.lock', $mode);
indvdum@0
   668
indvdum@0
   669
		if ($mode == 'wb')
indvdum@0
   670
		{
indvdum@0
   671
			if (!$lock_fp)
indvdum@0
   672
			{
indvdum@0
   673
				// Two processes may attempt to create lock file at the same time.
indvdum@0
   674
				// Have the losing process try opening the lock file again for reading
indvdum@0
   675
				// on the assumption that the winning process created it
indvdum@0
   676
				$mode = 'rb';
indvdum@0
   677
				$lock_fp = @fopen($this->cache_file . '.lock', $mode);
indvdum@0
   678
			}
indvdum@0
   679
			else
indvdum@0
   680
			{
indvdum@0
   681
				// Only need to set mode when the lock file is written
indvdum@0
   682
				@chmod($this->cache_file . '.lock', 0666);
indvdum@0
   683
			}
indvdum@0
   684
		}
indvdum@0
   685
indvdum@0
   686
		if ($lock_fp)
indvdum@0
   687
		{
indvdum@0
   688
			@flock($lock_fp, LOCK_EX);
indvdum@0
   689
		}
indvdum@0
   690
indvdum@0
   691
		return $lock_fp;
indvdum@0
   692
	}
indvdum@0
   693
indvdum@0
   694
	/**
indvdum@0
   695
	* Releases lock on queue cache file, using resource obtained from lock()
indvdum@0
   696
	*/
indvdum@0
   697
	function unlock($lock_fp)
indvdum@0
   698
	{
indvdum@0
   699
		// lock() will return null if opening lock file, and thus locking, failed.
indvdum@0
   700
		// Accept null values here so that client code does not need to check them
indvdum@0
   701
		if ($lock_fp)
indvdum@0
   702
		{
indvdum@0
   703
			@flock($lock_fp, LOCK_UN);
indvdum@0
   704
			fclose($lock_fp);
indvdum@0
   705
		}
indvdum@0
   706
	}
indvdum@0
   707
indvdum@0
   708
	/**
indvdum@0
   709
	* Process queue
indvdum@0
   710
	* Using lock file
indvdum@0
   711
	*/
indvdum@0
   712
	function process()
indvdum@0
   713
	{
indvdum@0
   714
		global $db, $config, $phpEx, $phpbb_root_path, $user;
indvdum@0
   715
indvdum@0
   716
		$lock_fp = $this->lock();
indvdum@0
   717
indvdum@0
   718
		set_config('last_queue_run', time(), true);
indvdum@0
   719
indvdum@0
   720
		if (!file_exists($this->cache_file) || filemtime($this->cache_file) > time() - $config['queue_interval'])
indvdum@0
   721
		{
indvdum@0
   722
			$this->unlock($lock_fp);
indvdum@0
   723
			return;
indvdum@0
   724
		}
indvdum@0
   725
indvdum@0
   726
		include($this->cache_file);
indvdum@0
   727
indvdum@0
   728
		foreach ($this->queue_data as $object => $data_ary)
indvdum@0
   729
		{
indvdum@0
   730
			@set_time_limit(0);
indvdum@0
   731
indvdum@0
   732
			if (!isset($data_ary['package_size']))
indvdum@0
   733
			{
indvdum@0
   734
				$data_ary['package_size'] = 0;
indvdum@0
   735
			}
indvdum@0
   736
indvdum@0
   737
			$package_size = $data_ary['package_size'];
indvdum@0
   738
			$num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
indvdum@0
   739
indvdum@0
   740
			/*
indvdum@0
   741
			* This code is commented out because it causes problems on some web hosts.
indvdum@0
   742
			* The core problem is rather restrictive email sending limits.
indvdum@0
   743
			* This code is nly useful if you have no such restrictions from the
indvdum@0
   744
			* web host and the package size setting is wrong.
indvdum@0
   745
indvdum@0
   746
			// If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs...
indvdum@0
   747
			if (sizeof($data_ary['data']) > $package_size * 2.5)
indvdum@0
   748
			{
indvdum@0
   749
				$num_items = sizeof($data_ary['data']);
indvdum@0
   750
			}
indvdum@0
   751
			*/
indvdum@0
   752
indvdum@0
   753
			switch ($object)
indvdum@0
   754
			{
indvdum@0
   755
				case 'email':
indvdum@0
   756
					// Delete the email queued objects if mailing is disabled
indvdum@0
   757
					if (!$config['email_enable'])
indvdum@0
   758
					{
indvdum@0
   759
						unset($this->queue_data['email']);
indvdum@0
   760
						continue 2;
indvdum@0
   761
					}
indvdum@0
   762
				break;
indvdum@0
   763
indvdum@0
   764
				case 'jabber':
indvdum@0
   765
					if (!$config['jab_enable'])
indvdum@0
   766
					{
indvdum@0
   767
						unset($this->queue_data['jabber']);
indvdum@0
   768
						continue 2;
indvdum@0
   769
					}
indvdum@0
   770
indvdum@0
   771
					include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
indvdum@0
   772
					$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
indvdum@0
   773
indvdum@0
   774
					if (!$this->jabber->connect())
indvdum@0
   775
					{
indvdum@0
   776
						messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
indvdum@0
   777
						continue 2;
indvdum@0
   778
					}
indvdum@0
   779
indvdum@0
   780
					if (!$this->jabber->login())
indvdum@0
   781
					{
indvdum@0
   782
						messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
indvdum@0
   783
						continue 2;
indvdum@0
   784
					}
indvdum@0
   785
indvdum@0
   786
				break;
indvdum@0
   787
indvdum@0
   788
				default:
indvdum@0
   789
					$this->unlock($lock_fp);
indvdum@0
   790
					return;
indvdum@0
   791
			}
indvdum@0
   792
indvdum@0
   793
			for ($i = 0; $i < $num_items; $i++)
indvdum@0
   794
			{
indvdum@0
   795
				// Make variables available...
indvdum@0
   796
				extract(array_shift($this->queue_data[$object]['data']));
indvdum@0
   797
indvdum@0
   798
				switch ($object)
indvdum@0
   799
				{
indvdum@0
   800
					case 'email':
indvdum@0
   801
						$err_msg = '';
indvdum@0
   802
						$to = (!$to) ? 'undisclosed-recipients:;' : $to;
indvdum@0
   803
indvdum@0
   804
						if ($config['smtp_delivery'])
indvdum@0
   805
						{
indvdum@0
   806
							$result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
indvdum@0
   807
						}
indvdum@0
   808
						else
indvdum@0
   809
						{
indvdum@0
   810
							$result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg);
indvdum@0
   811
						}
indvdum@0
   812
indvdum@0
   813
						if (!$result)
indvdum@0
   814
						{
indvdum@0
   815
							messenger::error('EMAIL', $err_msg);
indvdum@0
   816
							continue 2;
indvdum@0
   817
						}
indvdum@0
   818
					break;
indvdum@0
   819
indvdum@0
   820
					case 'jabber':
indvdum@0
   821
						foreach ($addresses as $address)
indvdum@0
   822
						{
indvdum@0
   823
							if ($this->jabber->send_message($address, $msg, $subject) === false)
indvdum@0
   824
							{
indvdum@0
   825
								messenger::error('JABBER', $this->jabber->get_log());
indvdum@0
   826
								continue 3;
indvdum@0
   827
							}
indvdum@0
   828
						}
indvdum@0
   829
					break;
indvdum@0
   830
				}
indvdum@0
   831
			}
indvdum@0
   832
indvdum@0
   833
			// No more data for this object? Unset it
indvdum@0
   834
			if (!sizeof($this->queue_data[$object]['data']))
indvdum@0
   835
			{
indvdum@0
   836
				unset($this->queue_data[$object]);
indvdum@0
   837
			}
indvdum@0
   838
indvdum@0
   839
			// Post-object processing
indvdum@0
   840
			switch ($object)
indvdum@0
   841
			{
indvdum@0
   842
				case 'jabber':
indvdum@0
   843
					// Hang about a couple of secs to ensure the messages are
indvdum@0
   844
					// handled, then disconnect
indvdum@0
   845
					$this->jabber->disconnect();
indvdum@0
   846
				break;
indvdum@0
   847
			}
indvdum@0
   848
		}
indvdum@0
   849
indvdum@0
   850
		if (!sizeof($this->queue_data))
indvdum@0
   851
		{
indvdum@0
   852
			@unlink($this->cache_file);
indvdum@0
   853
		}
indvdum@0
   854
		else
indvdum@0
   855
		{
indvdum@0
   856
			if ($fp = @fopen($this->cache_file, 'wb'))
indvdum@0
   857
			{
indvdum@0
   858
				fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
indvdum@0
   859
				fclose($fp);
indvdum@0
   860
indvdum@0
   861
				phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   862
			}
indvdum@0
   863
		}
indvdum@0
   864
indvdum@0
   865
		$this->unlock($lock_fp);
indvdum@0
   866
	}
indvdum@0
   867
indvdum@0
   868
	/**
indvdum@0
   869
	* Save queue
indvdum@0
   870
	*/
indvdum@0
   871
	function save()
indvdum@0
   872
	{
indvdum@0
   873
		if (!sizeof($this->data))
indvdum@0
   874
		{
indvdum@0
   875
			return;
indvdum@0
   876
		}
indvdum@0
   877
indvdum@0
   878
		$lock_fp = $this->lock();
indvdum@0
   879
indvdum@0
   880
		if (file_exists($this->cache_file))
indvdum@0
   881
		{
indvdum@0
   882
			include($this->cache_file);
indvdum@0
   883
indvdum@0
   884
			foreach ($this->queue_data as $object => $data_ary)
indvdum@0
   885
			{
indvdum@0
   886
				if (isset($this->data[$object]) && sizeof($this->data[$object]))
indvdum@0
   887
				{
indvdum@0
   888
					$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
indvdum@0
   889
				}
indvdum@0
   890
				else
indvdum@0
   891
				{
indvdum@0
   892
					$this->data[$object]['data'] = $data_ary['data'];
indvdum@0
   893
				}
indvdum@0
   894
			}
indvdum@0
   895
		}
indvdum@0
   896
indvdum@0
   897
		if ($fp = @fopen($this->cache_file, 'w'))
indvdum@0
   898
		{
indvdum@0
   899
			fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
indvdum@0
   900
			fclose($fp);
indvdum@0
   901
indvdum@0
   902
			phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
indvdum@0
   903
		}
indvdum@0
   904
indvdum@0
   905
		$this->unlock($lock_fp);
indvdum@0
   906
	}
indvdum@0
   907
}
indvdum@0
   908
indvdum@0
   909
/**
indvdum@0
   910
* Replacement or substitute for PHP's mail command
indvdum@0
   911
*/
indvdum@0
   912
function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)
indvdum@0
   913
{
indvdum@0
   914
	global $config, $user;
indvdum@0
   915
indvdum@0
   916
	// Fix any bare linefeeds in the message to make it RFC821 Compliant.
indvdum@0
   917
	$message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
indvdum@0
   918
indvdum@0
   919
	if ($headers !== false)
indvdum@0
   920
	{
indvdum@0
   921
		if (!is_array($headers))
indvdum@0
   922
		{
indvdum@0
   923
			// Make sure there are no bare linefeeds in the headers
indvdum@0
   924
			$headers = preg_replace('#(?<!\r)\n#si', "\n", $headers);
indvdum@0
   925
			$headers = explode("\n", $headers);
indvdum@0
   926
		}
indvdum@0
   927
indvdum@0
   928
		// Ok this is rather confusing all things considered,
indvdum@0
   929
		// but we have to grab bcc and cc headers and treat them differently
indvdum@0
   930
		// Something we really didn't take into consideration originally
indvdum@0
   931
		$headers_used = array();
indvdum@0
   932
indvdum@0
   933
		foreach ($headers as $header)
indvdum@0
   934
		{
indvdum@0
   935
			if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
indvdum@0
   936
			{
indvdum@0
   937
				continue;
indvdum@0
   938
			}
indvdum@0
   939
			$headers_used[] = trim($header);
indvdum@0
   940
		}
indvdum@0
   941
indvdum@0
   942
		$headers = chop(implode("\r\n", $headers_used));
indvdum@0
   943
	}
indvdum@0
   944
indvdum@0
   945
	if (trim($subject) == '')
indvdum@0
   946
	{
indvdum@0
   947
		$err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
indvdum@0
   948
		return false;
indvdum@0
   949
	}
indvdum@0
   950
indvdum@0
   951
	if (trim($message) == '')
indvdum@0
   952
	{
indvdum@0
   953
		$err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
indvdum@0
   954
		return false;
indvdum@0
   955
	}
indvdum@0
   956
indvdum@0
   957
	$mail_rcpt = $mail_to = $mail_cc = array();
indvdum@0
   958
indvdum@0
   959
	// Build correct addresses for RCPT TO command and the client side display (TO, CC)
indvdum@0
   960
	if (isset($addresses['to']) && sizeof($addresses['to']))
indvdum@0
   961
	{
indvdum@0
   962
		foreach ($addresses['to'] as $which_ary)
indvdum@0
   963
		{
indvdum@0
   964
			$mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
indvdum@0
   965
			$mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0
   966
		}
indvdum@0
   967
	}
indvdum@0
   968
indvdum@0
   969
	if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
indvdum@0
   970
	{
indvdum@0
   971
		foreach ($addresses['bcc'] as $which_ary)
indvdum@0
   972
		{
indvdum@0
   973
			$mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0
   974
		}
indvdum@0
   975
	}
indvdum@0
   976
indvdum@0
   977
	if (isset($addresses['cc']) && sizeof($addresses['cc']))
indvdum@0
   978
	{
indvdum@0
   979
		foreach ($addresses['cc'] as $which_ary)
indvdum@0
   980
		{
indvdum@0
   981
			$mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
indvdum@0
   982
			$mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0
   983
		}
indvdum@0
   984
	}
indvdum@0
   985
indvdum@0
   986
	$smtp = new smtp_class();
indvdum@0
   987
indvdum@0
   988
	$errno = 0;
indvdum@0
   989
	$errstr = '';
indvdum@0
   990
indvdum@0
   991
	$smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
indvdum@0
   992
indvdum@0
   993
	// Ok we have error checked as much as we can to this point let's get on it already.
indvdum@0
   994
	if (!class_exists('phpbb_error_collector'))
indvdum@0
   995
	{
indvdum@0
   996
		global $phpbb_root_path, $phpEx;
indvdum@0
   997
		include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
indvdum@0
   998
	}
indvdum@0
   999
	$collector = new phpbb_error_collector;
indvdum@0
  1000
	$collector->install();
indvdum@0
  1001
	$smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
indvdum@0
  1002
	$collector->uninstall();
indvdum@0
  1003
	$error_contents = $collector->format_errors();
indvdum@0
  1004
indvdum@0
  1005
	if (!$smtp->socket)
indvdum@0
  1006
	{
indvdum@0
  1007
		if ($errstr)
indvdum@0
  1008
		{
indvdum@0
  1009
			$errstr = utf8_convert_message($errstr);
indvdum@0
  1010
		}
indvdum@0
  1011
indvdum@0
  1012
		$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";
indvdum@0
  1013
		$err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents) : '';
indvdum@0
  1014
		return false;
indvdum@0
  1015
	}
indvdum@0
  1016
indvdum@0
  1017
	// Wait for reply
indvdum@0
  1018
	if ($err_msg = $smtp->server_parse('220', __LINE__))
indvdum@0
  1019
	{
indvdum@0
  1020
		$smtp->close_session($err_msg);
indvdum@0
  1021
		return false;
indvdum@0
  1022
	}
indvdum@0
  1023
indvdum@0
  1024
	// Let me in. This function handles the complete authentication process
indvdum@0
  1025
	if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
indvdum@0
  1026
	{
indvdum@0
  1027
		$smtp->close_session($err_msg);
indvdum@0
  1028
		return false;
indvdum@0
  1029
	}
indvdum@0
  1030
indvdum@0
  1031
	// From this point onward most server response codes should be 250
indvdum@0
  1032
	// Specify who the mail is from....
indvdum@0
  1033
	$smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
indvdum@0
  1034
	if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0
  1035
	{
indvdum@0
  1036
		$smtp->close_session($err_msg);
indvdum@0
  1037
		return false;
indvdum@0
  1038
	}
indvdum@0
  1039
indvdum@0
  1040
	// Specify each user to send to and build to header.
indvdum@0
  1041
	$to_header = implode(', ', $mail_to);
indvdum@0
  1042
	$cc_header = implode(', ', $mail_cc);
indvdum@0
  1043
indvdum@0
  1044
	// Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
indvdum@0
  1045
	$rcpt = false;
indvdum@0
  1046
	foreach ($mail_rcpt as $type => $mail_to_addresses)
indvdum@0
  1047
	{
indvdum@0
  1048
		foreach ($mail_to_addresses as $mail_to_address)
indvdum@0
  1049
		{
indvdum@0
  1050
			// Add an additional bit of error checking to the To field.
indvdum@0
  1051
			if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
indvdum@0
  1052
			{
indvdum@0
  1053
				$smtp->server_send("RCPT TO:$mail_to_address");
indvdum@0
  1054
				if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0
  1055
				{
indvdum@0
  1056
					// We continue... if users are not resolved we do not care
indvdum@0
  1057
					if ($smtp->numeric_response_code != 550)
indvdum@0
  1058
					{
indvdum@0
  1059
						$smtp->close_session($err_msg);
indvdum@0
  1060
						return false;
indvdum@0
  1061
					}
indvdum@0
  1062
				}
indvdum@0
  1063
				else
indvdum@0
  1064
				{
indvdum@0
  1065
					$rcpt = true;
indvdum@0
  1066
				}
indvdum@0
  1067
			}
indvdum@0
  1068
		}
indvdum@0
  1069
	}
indvdum@0
  1070
indvdum@0
  1071
	// 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.
indvdum@0
  1072
	if (!$rcpt)
indvdum@0
  1073
	{
indvdum@0
  1074
		$user->session_begin();
indvdum@0
  1075
		$err_msg .= '<br /><br />';
indvdum@0
  1076
		$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?';
indvdum@0
  1077
		$smtp->close_session($err_msg);
indvdum@0
  1078
		return false;
indvdum@0
  1079
	}
indvdum@0
  1080
indvdum@0
  1081
	// Ok now we tell the server we are ready to start sending data
indvdum@0
  1082
	$smtp->server_send('DATA');
indvdum@0
  1083
indvdum@0
  1084
	// This is the last response code we look for until the end of the message.
indvdum@0
  1085
	if ($err_msg = $smtp->server_parse('354', __LINE__))
indvdum@0
  1086
	{
indvdum@0
  1087
		$smtp->close_session($err_msg);
indvdum@0
  1088
		return false;
indvdum@0
  1089
	}
indvdum@0
  1090
indvdum@0
  1091
	// Send the Subject Line...
indvdum@0
  1092
	$smtp->server_send("Subject: $subject");
indvdum@0
  1093
indvdum@0
  1094
	// Now the To Header.
indvdum@0
  1095
	$to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
indvdum@0
  1096
	$smtp->server_send("To: $to_header");
indvdum@0
  1097
indvdum@0
  1098
	// Now the CC Header.
indvdum@0
  1099
	if ($cc_header != '')
indvdum@0
  1100
	{
indvdum@0
  1101
		$smtp->server_send("CC: $cc_header");
indvdum@0
  1102
	}
indvdum@0
  1103
indvdum@0
  1104
	// Now any custom headers....
indvdum@0
  1105
	if ($headers !== false)
indvdum@0
  1106
	{
indvdum@0
  1107
		$smtp->server_send("$headers\r\n");
indvdum@0
  1108
	}
indvdum@0
  1109
indvdum@0
  1110
	// Ok now we are ready for the message...
indvdum@0
  1111
	$smtp->server_send($message);
indvdum@0
  1112
indvdum@0
  1113
	// Ok the all the ingredients are mixed in let's cook this puppy...
indvdum@0
  1114
	$smtp->server_send('.');
indvdum@0
  1115
	if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0
  1116
	{
indvdum@0
  1117
		$smtp->close_session($err_msg);
indvdum@0
  1118
		return false;
indvdum@0
  1119
	}
indvdum@0
  1120
indvdum@0
  1121
	// Now tell the server we are done and close the socket...
indvdum@0
  1122
	$smtp->server_send('QUIT');
indvdum@0
  1123
	$smtp->close_session($err_msg);
indvdum@0
  1124
indvdum@0
  1125
	return true;
indvdum@0
  1126
}
indvdum@0
  1127
indvdum@0
  1128
/**
indvdum@0
  1129
* SMTP Class
indvdum@0
  1130
* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
indvdum@0
  1131
* See docs/AUTHORS for more details
indvdum@0
  1132
* @package phpBB3
indvdum@0
  1133
*/
indvdum@0
  1134
class smtp_class
indvdum@0
  1135
{
indvdum@0
  1136
	var $server_response = '';
indvdum@0
  1137
	var $socket = 0;
indvdum@0
  1138
	var $responses = array();
indvdum@0
  1139
	var $commands = array();
indvdum@0
  1140
	var $numeric_response_code = 0;
indvdum@0
  1141
indvdum@0
  1142
	var $backtrace = false;
indvdum@0
  1143
	var $backtrace_log = array();
indvdum@0
  1144
indvdum@0
  1145
	function smtp_class()
indvdum@0
  1146
	{
indvdum@0
  1147
		// Always create a backtrace for admins to identify SMTP problems
indvdum@0
  1148
		$this->backtrace = true;
indvdum@0
  1149
		$this->backtrace_log = array();
indvdum@0
  1150
	}
indvdum@0
  1151
indvdum@0
  1152
	/**
indvdum@0
  1153
	* Add backtrace message for debugging
indvdum@0
  1154
	*/
indvdum@0
  1155
	function add_backtrace($message)
indvdum@0
  1156
	{
indvdum@0
  1157
		if ($this->backtrace)
indvdum@0
  1158
		{
indvdum@0
  1159
			$this->backtrace_log[] = utf8_htmlspecialchars($message);
indvdum@0
  1160
		}
indvdum@0
  1161
	}
indvdum@0
  1162
indvdum@0
  1163
	/**
indvdum@0
  1164
	* Send command to smtp server
indvdum@0
  1165
	*/
indvdum@0
  1166
	function server_send($command, $private_info = false)
indvdum@0
  1167
	{
indvdum@0
  1168
		fputs($this->socket, $command . "\r\n");
indvdum@0
  1169
indvdum@0
  1170
		(!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
indvdum@0
  1171
indvdum@0
  1172
		// We could put additional code here
indvdum@0
  1173
	}
indvdum@0
  1174
indvdum@0
  1175
	/**
indvdum@0
  1176
	* We use the line to give the support people an indication at which command the error occurred
indvdum@0
  1177
	*/
indvdum@0
  1178
	function server_parse($response, $line)
indvdum@0
  1179
	{
indvdum@0
  1180
		global $user;
indvdum@0
  1181
indvdum@0
  1182
		$this->server_response = '';
indvdum@0
  1183
		$this->responses = array();
indvdum@0
  1184
		$this->numeric_response_code = 0;
indvdum@0
  1185
indvdum@0
  1186
		while (substr($this->server_response, 3, 1) != ' ')
indvdum@0
  1187
		{
indvdum@0
  1188
			if (!($this->server_response = fgets($this->socket, 256)))
indvdum@0
  1189
			{
indvdum@0
  1190
				return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
indvdum@0
  1191
			}
indvdum@0
  1192
			$this->responses[] = substr(rtrim($this->server_response), 4);
indvdum@0
  1193
			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
indvdum@0
  1194
indvdum@0
  1195
			$this->add_backtrace("LINE: $line <- {$this->server_response}");
indvdum@0
  1196
		}
indvdum@0
  1197
indvdum@0
  1198
		if (!(substr($this->server_response, 0, 3) == $response))
indvdum@0
  1199
		{
indvdum@0
  1200
			$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
indvdum@0
  1201
			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";
indvdum@0
  1202
		}
indvdum@0
  1203
indvdum@0
  1204
		return 0;
indvdum@0
  1205
	}
indvdum@0
  1206
indvdum@0
  1207
	/**
indvdum@0
  1208
	* Close session
indvdum@0
  1209
	*/
indvdum@0
  1210
	function close_session(&$err_msg)
indvdum@0
  1211
	{
indvdum@0
  1212
		fclose($this->socket);
indvdum@0
  1213
indvdum@0
  1214
		if ($this->backtrace)
indvdum@0
  1215
		{
indvdum@0
  1216
			$message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>';
indvdum@0
  1217
			$err_msg .= $message;
indvdum@0
  1218
		}
indvdum@0
  1219
	}
indvdum@0
  1220
indvdum@0
  1221
	/**
indvdum@0
  1222
	* Log into server and get possible auth codes if neccessary
indvdum@0
  1223
	*/
indvdum@0
  1224
	function log_into_server($hostname, $username, $password, $default_auth_method)
indvdum@0
  1225
	{
indvdum@0
  1226
		global $user;
indvdum@0
  1227
indvdum@0
  1228
		$err_msg = '';
indvdum@0
  1229
indvdum@0
  1230
		// Here we try to determine the *real* hostname (reverse DNS entry preferrably)
indvdum@0
  1231
		$local_host = $user->host;
indvdum@0
  1232
indvdum@0
  1233
		if (function_exists('php_uname'))
indvdum@0
  1234
		{
indvdum@0
  1235
			$local_host = php_uname('n');
indvdum@0
  1236
indvdum@0
  1237
			// Able to resolve name to IP
indvdum@0
  1238
			if (($addr = @gethostbyname($local_host)) !== $local_host)
indvdum@0
  1239
			{
indvdum@0
  1240
				// Able to resolve IP back to name
indvdum@0
  1241
				if (($name = @gethostbyaddr($addr)) !== $addr)
indvdum@0
  1242
				{
indvdum@0
  1243
					$local_host = $name;
indvdum@0
  1244
				}
indvdum@0
  1245
			}
indvdum@0
  1246
		}
indvdum@0
  1247
indvdum@0
  1248
		// If we are authenticating through pop-before-smtp, we
indvdum@0
  1249
		// have to login ones before we get authenticated
indvdum@0
  1250
		// NOTE: on some configurations the time between an update of the auth database takes so
indvdum@0
  1251
		// long that the first email send does not work. This is not a biggie on a live board (only
indvdum@0
  1252
		// the install mail will most likely fail) - but on a dynamic ip connection this might produce
indvdum@0
  1253
		// severe problems and is not fixable!
indvdum@0
  1254
		if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password)
indvdum@0
  1255
		{
indvdum@0
  1256
			global $config;
indvdum@0
  1257
indvdum@0
  1258
			$errno = 0;
indvdum@0
  1259
			$errstr = '';
indvdum@0
  1260
indvdum@0
  1261
			$this->server_send("QUIT");
indvdum@0
  1262
			fclose($this->socket);
indvdum@0
  1263
indvdum@0
  1264
			$result = $this->pop_before_smtp($hostname, $username, $password);
indvdum@0
  1265
			$username = $password = $default_auth_method = '';
indvdum@0
  1266
indvdum@0
  1267
			// We need to close the previous session, else the server is not
indvdum@0
  1268
			// able to get our ip for matching...
indvdum@0
  1269
			if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10))
indvdum@0
  1270
			{
indvdum@0
  1271
				if ($errstr)
indvdum@0
  1272
				{
indvdum@0
  1273
					$errstr = utf8_convert_message($errstr);
indvdum@0
  1274
				}
indvdum@0
  1275
indvdum@0
  1276
				$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";
indvdum@0
  1277
				return $err_msg;
indvdum@0
  1278
			}
indvdum@0
  1279
indvdum@0
  1280
			// Wait for reply
indvdum@0
  1281
			if ($err_msg = $this->server_parse('220', __LINE__))
indvdum@0
  1282
			{
indvdum@0
  1283
				$this->close_session($err_msg);
indvdum@0
  1284
				return $err_msg;
indvdum@0
  1285
			}
indvdum@0
  1286
		}
indvdum@0
  1287
indvdum@0
  1288
		// Try EHLO first
indvdum@0
  1289
		$this->server_send("EHLO {$local_host}");
indvdum@0
  1290
		if ($err_msg = $this->server_parse('250', __LINE__))
indvdum@0
  1291
		{
indvdum@0
  1292
			// a 503 response code means that we're already authenticated
indvdum@0
  1293
			if ($this->numeric_response_code == 503)
indvdum@0
  1294
			{
indvdum@0
  1295
				return false;
indvdum@0
  1296
			}
indvdum@0
  1297
indvdum@0
  1298
			// If EHLO fails, we try HELO
indvdum@0
  1299
			$this->server_send("HELO {$local_host}");
indvdum@0
  1300
			if ($err_msg = $this->server_parse('250', __LINE__))
indvdum@0
  1301
			{
indvdum@0
  1302
				return ($this->numeric_response_code == 503) ? false : $err_msg;
indvdum@0
  1303
			}
indvdum@0
  1304
		}
indvdum@0
  1305
indvdum@0
  1306
		foreach ($this->responses as $response)
indvdum@0
  1307
		{
indvdum@0
  1308
			$response = explode(' ', $response);
indvdum@0
  1309
			$response_code = $response[0];
indvdum@0
  1310
			unset($response[0]);
indvdum@0
  1311
			$this->commands[$response_code] = implode(' ', $response);
indvdum@0
  1312
		}
indvdum@0
  1313
indvdum@0
  1314
		// If we are not authenticated yet, something might be wrong if no username and passwd passed
indvdum@0
  1315
		if (!$username || !$password)
indvdum@0
  1316
		{
indvdum@0
  1317
			return false;
indvdum@0
  1318
		}
indvdum@0
  1319
indvdum@0
  1320
		if (!isset($this->commands['AUTH']))
indvdum@0
  1321
		{
indvdum@0
  1322
			return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication';
indvdum@0
  1323
		}
indvdum@0
  1324
indvdum@0
  1325
		// Get best authentication method
indvdum@0
  1326
		$available_methods = explode(' ', $this->commands['AUTH']);
indvdum@0
  1327
indvdum@0
  1328
		// Define the auth ordering if the default auth method was not found
indvdum@0
  1329
		$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5');
indvdum@0
  1330
		$method = '';
indvdum@0
  1331
indvdum@0
  1332
		if (in_array($default_auth_method, $available_methods))
indvdum@0
  1333
		{
indvdum@0
  1334
			$method = $default_auth_method;
indvdum@0
  1335
		}
indvdum@0
  1336
		else
indvdum@0
  1337
		{
indvdum@0
  1338
			foreach ($auth_methods as $_method)
indvdum@0
  1339
			{
indvdum@0
  1340
				if (in_array($_method, $available_methods))
indvdum@0
  1341
				{
indvdum@0
  1342
					$method = $_method;
indvdum@0
  1343
					break;
indvdum@0
  1344
				}
indvdum@0
  1345
			}
indvdum@0
  1346
		}
indvdum@0
  1347
indvdum@0
  1348
		if (!$method)
indvdum@0
  1349
		{
indvdum@0
  1350
			return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods';
indvdum@0
  1351
		}
indvdum@0
  1352
indvdum@0
  1353
		$method = strtolower(str_replace('-', '_', $method));
indvdum@0
  1354
		return $this->$method($username, $password);
indvdum@0
  1355
	}
indvdum@0
  1356
indvdum@0
  1357
	/**
indvdum@0
  1358
	* Pop before smtp authentication
indvdum@0
  1359
	*/
indvdum@0
  1360
	function pop_before_smtp($hostname, $username, $password)
indvdum@0
  1361
	{
indvdum@0
  1362
		global $user;
indvdum@0
  1363
indvdum@0
  1364
		if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10))
indvdum@0
  1365
		{
indvdum@0
  1366
			if ($errstr)
indvdum@0
  1367
			{
indvdum@0
  1368
				$errstr = utf8_convert_message($errstr);
indvdum@0
  1369
			}
indvdum@0
  1370
indvdum@0
  1371
			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";
indvdum@0
  1372
		}
indvdum@0
  1373
indvdum@0
  1374
		$this->server_send("USER $username", true);
indvdum@0
  1375
		if ($err_msg = $this->server_parse('+OK', __LINE__))
indvdum@0
  1376
		{
indvdum@0
  1377
			return $err_msg;
indvdum@0
  1378
		}
indvdum@0
  1379
indvdum@0
  1380
		$this->server_send("PASS $password", true);
indvdum@0
  1381
		if ($err_msg = $this->server_parse('+OK', __LINE__))
indvdum@0
  1382
		{
indvdum@0
  1383
			return $err_msg;
indvdum@0
  1384
		}
indvdum@0
  1385
indvdum@0
  1386
		$this->server_send('QUIT');
indvdum@0
  1387
		fclose($this->socket);
indvdum@0
  1388
indvdum@0
  1389
		return false;
indvdum@0
  1390
	}
indvdum@0
  1391
indvdum@0
  1392
	/**
indvdum@0
  1393
	* Plain authentication method
indvdum@0
  1394
	*/
indvdum@0
  1395
	function plain($username, $password)
indvdum@0
  1396
	{
indvdum@0
  1397
		$this->server_send('AUTH PLAIN');
indvdum@0
  1398
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1399
		{
indvdum@0
  1400
			return ($this->numeric_response_code == 503) ? false : $err_msg;
indvdum@0
  1401
		}
indvdum@0
  1402
indvdum@0
  1403
		$base64_method_plain = base64_encode("\0" . $username . "\0" . $password);
indvdum@0
  1404
		$this->server_send($base64_method_plain, true);
indvdum@0
  1405
		if ($err_msg = $this->server_parse('235', __LINE__))
indvdum@0
  1406
		{
indvdum@0
  1407
			return $err_msg;
indvdum@0
  1408
		}
indvdum@0
  1409
indvdum@0
  1410
		return false;
indvdum@0
  1411
	}
indvdum@0
  1412
indvdum@0
  1413
	/**
indvdum@0
  1414
	* Login authentication method
indvdum@0
  1415
	*/
indvdum@0
  1416
	function login($username, $password)
indvdum@0
  1417
	{
indvdum@0
  1418
		$this->server_send('AUTH LOGIN');
indvdum@0
  1419
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1420
		{
indvdum@0
  1421
			return ($this->numeric_response_code == 503) ? false : $err_msg;
indvdum@0
  1422
		}
indvdum@0
  1423
indvdum@0
  1424
		$this->server_send(base64_encode($username), true);
indvdum@0
  1425
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1426
		{
indvdum@0
  1427
			return $err_msg;
indvdum@0
  1428
		}
indvdum@0
  1429
indvdum@0
  1430
		$this->server_send(base64_encode($password), true);
indvdum@0
  1431
		if ($err_msg = $this->server_parse('235', __LINE__))
indvdum@0
  1432
		{
indvdum@0
  1433
			return $err_msg;
indvdum@0
  1434
		}
indvdum@0
  1435
indvdum@0
  1436
		return false;
indvdum@0
  1437
	}
indvdum@0
  1438
indvdum@0
  1439
	/**
indvdum@0
  1440
	* cram_md5 authentication method
indvdum@0
  1441
	*/
indvdum@0
  1442
	function cram_md5($username, $password)
indvdum@0
  1443
	{
indvdum@0
  1444
		$this->server_send('AUTH CRAM-MD5');
indvdum@0
  1445
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1446
		{
indvdum@0
  1447
			return ($this->numeric_response_code == 503) ? false : $err_msg;
indvdum@0
  1448
		}
indvdum@0
  1449
indvdum@0
  1450
		$md5_challenge = base64_decode($this->responses[0]);
indvdum@0
  1451
		$password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password);
indvdum@0
  1452
		$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))));
indvdum@0
  1453
indvdum@0
  1454
		$base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest);
indvdum@0
  1455
indvdum@0
  1456
		$this->server_send($base64_method_cram_md5, true);
indvdum@0
  1457
		if ($err_msg = $this->server_parse('235', __LINE__))
indvdum@0
  1458
		{
indvdum@0
  1459
			return $err_msg;
indvdum@0
  1460
		}
indvdum@0
  1461
indvdum@0
  1462
		return false;
indvdum@0
  1463
	}
indvdum@0
  1464
indvdum@0
  1465
	/**
indvdum@0
  1466
	* digest_md5 authentication method
indvdum@0
  1467
	* A real pain in the ***
indvdum@0
  1468
	*/
indvdum@0
  1469
	function digest_md5($username, $password)
indvdum@0
  1470
	{
indvdum@0
  1471
		global $config, $user;
indvdum@0
  1472
indvdum@0
  1473
		$this->server_send('AUTH DIGEST-MD5');
indvdum@0
  1474
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1475
		{
indvdum@0
  1476
			return ($this->numeric_response_code == 503) ? false : $err_msg;
indvdum@0
  1477
		}
indvdum@0
  1478
indvdum@0
  1479
		$md5_challenge = base64_decode($this->responses[0]);
indvdum@0
  1480
indvdum@0
  1481
		// Parse the md5 challenge - from AUTH_SASL (PEAR)
indvdum@0
  1482
		$tokens = array();
indvdum@0
  1483
		while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches))
indvdum@0
  1484
		{
indvdum@0
  1485
			// Ignore these as per rfc2831
indvdum@0
  1486
			if ($matches[1] == 'opaque' || $matches[1] == 'domain')
indvdum@0
  1487
			{
indvdum@0
  1488
				$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
indvdum@0
  1489
				continue;
indvdum@0
  1490
			}
indvdum@0
  1491
indvdum@0
  1492
			// Allowed multiple "realm" and "auth-param"
indvdum@0
  1493
			if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param'))
indvdum@0
  1494
			{
indvdum@0
  1495
				if (is_array($tokens[$matches[1]]))
indvdum@0
  1496
				{
indvdum@0
  1497
					$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
indvdum@0
  1498
				}
indvdum@0
  1499
				else
indvdum@0
  1500
				{
indvdum@0
  1501
					$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
indvdum@0
  1502
				}
indvdum@0
  1503
			}
indvdum@0
  1504
			else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure
indvdum@0
  1505
			{
indvdum@0
  1506
				$tokens = array();
indvdum@0
  1507
				break;
indvdum@0
  1508
			}
indvdum@0
  1509
			else
indvdum@0
  1510
			{
indvdum@0
  1511
				$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
indvdum@0
  1512
			}
indvdum@0
  1513
indvdum@0
  1514
			// Remove the just parsed directive from the challenge
indvdum@0
  1515
			$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
indvdum@0
  1516
		}
indvdum@0
  1517
indvdum@0
  1518
		// Realm
indvdum@0
  1519
		if (empty($tokens['realm']))
indvdum@0
  1520
		{
indvdum@0
  1521
			$tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host;
indvdum@0
  1522
		}
indvdum@0
  1523
indvdum@0
  1524
		// Maxbuf
indvdum@0
  1525
		if (empty($tokens['maxbuf']))
indvdum@0
  1526
		{
indvdum@0
  1527
			$tokens['maxbuf'] = 65536;
indvdum@0
  1528
		}
indvdum@0
  1529
indvdum@0
  1530
		// Required: nonce, algorithm
indvdum@0
  1531
		if (empty($tokens['nonce']) || empty($tokens['algorithm']))
indvdum@0
  1532
		{
indvdum@0
  1533
			$tokens = array();
indvdum@0
  1534
		}
indvdum@0
  1535
		$md5_challenge = $tokens;
indvdum@0
  1536
indvdum@0
  1537
		if (!empty($md5_challenge))
indvdum@0
  1538
		{
indvdum@0
  1539
			$str = '';
indvdum@0
  1540
			for ($i = 0; $i < 32; $i++)
indvdum@0
  1541
			{
indvdum@0
  1542
				$str .= chr(mt_rand(0, 255));
indvdum@0
  1543
			}
indvdum@0
  1544
			$cnonce = base64_encode($str);
indvdum@0
  1545
indvdum@0
  1546
			$digest_uri = 'smtp/' . $config['smtp_host'];
indvdum@0
  1547
indvdum@0
  1548
			$auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce);
indvdum@0
  1549
			$auth_2 = 'AUTHENTICATE:' . $digest_uri;
indvdum@0
  1550
			$response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2)));
indvdum@0
  1551
indvdum@0
  1552
			$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']);
indvdum@0
  1553
		}
indvdum@0
  1554
		else
indvdum@0
  1555
		{
indvdum@0
  1556
			return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge';
indvdum@0
  1557
		}
indvdum@0
  1558
indvdum@0
  1559
		$base64_method_digest_md5 = base64_encode($input_string);
indvdum@0
  1560
		$this->server_send($base64_method_digest_md5, true);
indvdum@0
  1561
		if ($err_msg = $this->server_parse('334', __LINE__))
indvdum@0
  1562
		{
indvdum@0
  1563
			return $err_msg;
indvdum@0
  1564
		}
indvdum@0
  1565
indvdum@0
  1566
		$this->server_send(' ');
indvdum@0
  1567
		if ($err_msg = $this->server_parse('235', __LINE__))
indvdum@0
  1568
		{
indvdum@0
  1569
			return $err_msg;
indvdum@0
  1570
		}
indvdum@0
  1571
indvdum@0
  1572
		return false;
indvdum@0
  1573
	}
indvdum@0
  1574
}
indvdum@0
  1575
indvdum@0
  1576
/**
indvdum@0
  1577
* Encodes the given string for proper display in UTF-8.
indvdum@0
  1578
*
indvdum@0
  1579
* This version is using base64 encoded data. The downside of this
indvdum@0
  1580
* is if the mail client does not understand this encoding the user
indvdum@0
  1581
* is basically doomed with an unreadable subject.
indvdum@0
  1582
*
indvdum@0
  1583
* Please note that this version fully supports RFC 2045 section 6.8.
indvdum@0
  1584
*
indvdum@0
  1585
* @param string $eol End of line we are using (optional to be backwards compatible)
indvdum@0
  1586
*/
indvdum@0
  1587
function mail_encode($str, $eol = "\r\n")
indvdum@0
  1588
{
indvdum@0
  1589
	// define start delimimter, end delimiter and spacer
indvdum@0
  1590
	$start = "=?UTF-8?B?";
indvdum@0
  1591
	$end = "?=";
indvdum@0
  1592
	$delimiter = "$eol ";
indvdum@0
  1593
indvdum@0
  1594
	// Maximum length is 75. $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $delimiter . $end)!!!
indvdum@0
  1595
	$split_length = 60;
indvdum@0
  1596
	$encoded_str = base64_encode($str);
indvdum@0
  1597
indvdum@0
  1598
	// If encoded string meets the limits, we just return with the correct data.
indvdum@0
  1599
	if (strlen($encoded_str) <= $split_length)
indvdum@0
  1600
	{
indvdum@0
  1601
		return $start . $encoded_str . $end;
indvdum@0
  1602
	}
indvdum@0
  1603
indvdum@0
  1604
	// If there is only ASCII data, we just return what we want, correctly splitting the lines.
indvdum@0
  1605
	if (strlen($str) === utf8_strlen($str))
indvdum@0
  1606
	{
indvdum@0
  1607
		return $start . implode($end . $delimiter . $start, str_split($encoded_str, $split_length)) . $end;
indvdum@0
  1608
	}
indvdum@0
  1609
indvdum@0
  1610
	// UTF-8 data, compose encoded lines
indvdum@0
  1611
	$array = utf8_str_split($str);
indvdum@0
  1612
	$str = '';
indvdum@0
  1613
indvdum@0
  1614
	while (sizeof($array))
indvdum@0
  1615
	{
indvdum@0
  1616
		$text = '';
indvdum@0
  1617
indvdum@0
  1618
		while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length)
indvdum@0
  1619
		{
indvdum@0
  1620
			$text .= array_shift($array);
indvdum@0
  1621
		}
indvdum@0
  1622
indvdum@0
  1623
		$str .= $start . base64_encode($text) . $end . $delimiter;
indvdum@0
  1624
	}
indvdum@0
  1625
indvdum@0
  1626
	return substr($str, 0, -strlen($delimiter));
indvdum@0
  1627
}
indvdum@0
  1628
indvdum@0
  1629
/**
indvdum@0
  1630
* Wrapper for sending out emails with the PHP's mail function
indvdum@0
  1631
*/
indvdum@0
  1632
function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg)
indvdum@0
  1633
{
indvdum@0
  1634
	global $config, $phpbb_root_path, $phpEx;
indvdum@0
  1635
indvdum@0
  1636
	// 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...
indvdum@0
  1637
	// Reference: http://bugs.php.net/bug.php?id=15841
indvdum@0
  1638
	$headers = implode($eol, $headers);
indvdum@0
  1639
indvdum@0
  1640
	if (!class_exists('phpbb_error_collector'))
indvdum@0
  1641
	{
indvdum@0
  1642
		include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
indvdum@0
  1643
	}
indvdum@0
  1644
indvdum@0
  1645
	$collector = new phpbb_error_collector;
indvdum@0
  1646
	$collector->install();
indvdum@0
  1647
indvdum@0
  1648
	// On some PHP Versions mail() *may* fail if there are newlines within the subject.
indvdum@0
  1649
	// Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8.
indvdum@0
  1650
	// 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)
indvdum@0
  1651
	$result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers);
indvdum@0
  1652
indvdum@0
  1653
	$collector->uninstall();
indvdum@0
  1654
	$err_msg = $collector->format_errors();
indvdum@0
  1655
indvdum@0
  1656
	return $result;
indvdum@0
  1657
}
indvdum@0
  1658
indvdum@0
  1659
?>