5
* @version $Id: functions_messenger.php,v 1.102 2007/11/17 20:03:32 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
25
var $vars, $msg, $extra_headers, $replyto, $from, $subject;
26
var $addresses = array();
28
var $mail_priority = MAIL_NORMAL_PRIORITY;
29
var $use_queue = true;
30
var $tpl_msg = array();
35
function messenger($use_queue = true)
39
$this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
44
* Resets all the data (address, template file, etc etc) to default
48
$this->addresses = $this->extra_headers = array();
49
$this->vars = $this->msg = $this->replyto = $this->from = '';
50
$this->mail_priority = MAIL_NORMAL_PRIORITY;
54
* Sets an email address to send to
56
function to($address, $realname = '')
60
$pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
62
$this->addresses['to'][$pos]['email'] = trim($address);
64
// If empty sendmail_path on windows, PHP changes the to line
65
if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
67
$this->addresses['to'][$pos]['name'] = '';
71
$this->addresses['to'][$pos]['name'] = trim($realname);
76
* Sets an cc address to send to
78
function cc($address, $realname = '')
80
$pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
81
$this->addresses['cc'][$pos]['email'] = trim($address);
82
$this->addresses['cc'][$pos]['name'] = trim($realname);
86
* Sets an bcc address to send to
88
function bcc($address, $realname = '')
90
$pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
91
$this->addresses['bcc'][$pos]['email'] = trim($address);
92
$this->addresses['bcc'][$pos]['name'] = trim($realname);
96
* Sets a im contact to send to
98
function im($address, $realname = '')
100
$pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
101
$this->addresses['im'][$pos]['uid'] = trim($address);
102
$this->addresses['im'][$pos]['name'] = trim($realname);
106
* Set the reply to address
108
function replyto($address)
110
$this->replyto = trim($address);
114
* Set the from address
116
function from($address)
118
$this->from = trim($address);
122
* set up subject for mail
124
function subject($subject = '')
126
$this->subject = trim($subject);
130
* set up extra mail headers
132
function headers($headers)
134
$this->extra_headers[] = trim($headers);
138
* Set the email priority
140
function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
142
$this->mail_priority = $priority;
146
* Set email template to use
148
function template($template_file, $template_lang = '')
150
global $config, $phpbb_root_path;
152
if (!trim($template_file))
154
trigger_error('No template file set', E_USER_ERROR);
157
if (!trim($template_lang))
159
$template_lang = basename($config['default_lang']);
162
if (empty($this->tpl_msg[$template_lang . $template_file]))
164
$tpl_file = "{$phpbb_root_path}language/$template_lang/email/$template_file.txt";
166
if (!file_exists($tpl_file))
168
trigger_error("Could not find email template file [ $tpl_file ]", E_USER_ERROR);
171
if (($data = @file_get_contents($tpl_file)) === false)
173
trigger_error("Failed opening template file [ $tpl_file ]", E_USER_ERROR);
176
$this->tpl_msg[$template_lang . $template_file] = $data;
179
$this->msg = $this->tpl_msg[$template_lang . $template_file];
185
* assign variables to email template
187
function assign_vars($vars)
189
$this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars;
193
* Send the mail out to the recipients set previously in var $this->addresses
195
function send($method = NOTIFY_EMAIL, $break = false)
197
global $config, $user;
199
// We add some standard variables we always use, no need to specify them always
200
$this->vars['U_BOARD'] = (!isset($this->vars['U_BOARD'])) ? generate_board_url() : $this->vars['U_BOARD'];
201
$this->vars['EMAIL_SIG'] = (!isset($this->vars['EMAIL_SIG'])) ? str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])) : $this->vars['EMAIL_SIG'];
202
$this->vars['SITENAME'] = (!isset($this->vars['SITENAME'])) ? htmlspecialchars_decode($config['sitename']) : $this->vars['SITENAME'];
204
// Escape all quotes, else the eval will fail.
205
$this->msg = str_replace ("'", "\'", $this->msg);
206
$this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->msg);
208
eval("\$this->msg = '$this->msg';");
210
// We now try and pull a subject from the email body ... if it exists,
211
// do this here because the subject may contain a variable
214
if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
216
$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
217
$drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
221
$this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
226
$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
237
$result = $this->msg_email();
241
$result = $this->msg_jabber();
245
$result = $this->msg_email();
255
* Add error message to log
257
function error($type, $msg)
259
global $user, $phpEx, $phpbb_root_path, $config;
261
// Session doesn't exist, create it
262
if (!isset($user->session_id) || $user->session_id === '')
264
$user->session_begin();
267
$calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
273
$message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '</strong>';
277
$message = "<strong>$type</strong>";
281
$message .= '<br /><em>' . htmlspecialchars($calling_page) . '</em><br /><br />' . $msg . '<br />';
282
add_log('critical', 'LOG_ERROR_' . $type, $message);
288
function save_queue()
292
if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
294
$this->queue->save();
300
* Return email header
302
function build_header($to, $cc, $bcc)
308
$headers[] = 'From: ' . $this->from;
312
$headers[] = 'Cc: ' . $cc;
317
$headers[] = 'Bcc: ' . $bcc;
320
$headers[] = 'Reply-To: ' . $this->replyto;
321
$headers[] = 'Return-Path: <' . $config['board_email'] . '>';
322
$headers[] = 'Sender: <' . $config['board_email'] . '>';
323
$headers[] = 'MIME-Version: 1.0';
324
$headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
325
$headers[] = 'Date: ' . date('r', time());
326
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
327
$headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
329
$headers[] = 'X-Priority: ' . $this->mail_priority;
330
$headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
331
$headers[] = 'X-Mailer: PhpBB3';
332
$headers[] = 'X-MimeOLE: phpBB3';
333
$headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
335
// We use \n here instead of \r\n because our smtp mailer is adjusting it to \r\n automatically, whereby the php mail function only works
338
if (sizeof($this->extra_headers))
340
$headers[] = implode("\n", $this->extra_headers);
343
return implode("\n", $headers);
351
global $config, $user;
353
if (empty($config['email_enable']))
359
if ($config['email_package_size'] && $this->use_queue)
361
if (empty($this->queue))
363
$this->queue = new queue();
364
$this->queue->init('email', $config['email_package_size']);
369
if (empty($this->replyto))
371
$this->replyto = '<' . $config['board_contact'] . '>';
374
if (empty($this->from))
376
$this->from = '<' . $config['board_contact'] . '>';
379
// Build to, cc and bcc strings
380
$to = $cc = $bcc = '';
381
foreach ($this->addresses as $type => $address_ary)
388
foreach ($address_ary as $which_ary)
390
$$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name']) . '" <' . $which_ary['email'] . '>' : $which_ary['email']);
395
$headers = $this->build_header($to, $cc, $bcc);
400
$mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
403
if ($config['smtp_delivery'])
405
$result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
410
$result = $config['email_function_name']($mail_to, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers);
411
$err_msg = ob_get_clean();
416
$this->error('EMAIL', $err_msg);
422
$this->queue->put('email', array(
424
'addresses' => $this->addresses,
425
'subject' => $this->subject,
427
'headers' => $headers)
435
* Send jabber message out
437
function msg_jabber()
439
global $config, $db, $user, $phpbb_root_path, $phpEx;
441
if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
447
if ($config['jab_package_size'] && $this->use_queue)
449
if (empty($this->queue))
451
$this->queue = new queue();
452
$this->queue->init('jabber', $config['jab_package_size']);
457
$addresses = array();
458
foreach ($this->addresses['im'] as $type => $uid_ary)
460
$addresses[] = $uid_ary['uid'];
462
$addresses = array_unique($addresses);
466
include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
467
$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
469
if (!$this->jabber->connect())
471
$this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log());
475
if (!$this->jabber->login())
477
$this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log());
481
foreach ($addresses as $address)
483
$this->jabber->send_message($address, $this->msg, $this->subject);
486
$this->jabber->disconnect();
490
$this->queue->put('jabber', array(
491
'addresses' => $addresses,
492
'subject' => $this->subject,
502
* handling email and jabber queue
508
var $queue_data = array();
509
var $package_size = 0;
510
var $cache_file = '';
517
global $phpEx, $phpbb_root_path;
519
$this->data = array();
520
$this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
524
* Init a queue object
526
function init($object, $package_size)
528
$this->data[$object] = array();
529
$this->data[$object]['package_size'] = $package_size;
530
$this->data[$object]['data'] = array();
534
* Put object in queue
536
function put($object, $scope)
538
$this->data[$object]['data'][] = $scope;
547
global $db, $config, $phpEx, $phpbb_root_path, $user;
549
set_config('last_queue_run', time(), true);
551
// Delete stale lock file
552
if (file_exists($this->cache_file . '.lock') && !file_exists($this->cache_file))
554
@unlink($this->cache_file . '.lock');
558
if (!file_exists($this->cache_file) || (file_exists($this->cache_file . '.lock') && filemtime($this->cache_file) > time() - $config['queue_interval']))
563
$fp = @fopen($this->cache_file . '.lock', 'wb');
565
@chmod($this->cache_file . '.lock', 0666);
567
include($this->cache_file);
569
foreach ($this->queue_data as $object => $data_ary)
573
if (!isset($data_ary['package_size']))
575
$data_ary['package_size'] = 0;
578
$package_size = $data_ary['package_size'];
579
$num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
581
// If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs...
582
if (sizeof($data_ary['data']) > $package_size * 2.5)
584
$num_items = sizeof($data_ary['data']);
590
// Delete the email queued objects if mailing is disabled
591
if (!$config['email_enable'])
593
unset($this->queue_data['email']);
599
if (!$config['jab_enable'])
601
unset($this->queue_data['jabber']);
605
include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
606
$this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
608
if (!$this->jabber->connect())
610
messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
614
if (!$this->jabber->login())
616
messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
626
for ($i = 0; $i < $num_items; $i++)
628
// Make variables available...
629
extract(array_shift($this->queue_data[$object]['data']));
635
$to = (!$to) ? 'undisclosed-recipients:;' : $to;
637
if ($config['smtp_delivery'])
639
$result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
644
$result = $config['email_function_name']($to, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers);
645
$err_msg = ob_get_clean();
650
@unlink($this->cache_file . '.lock');
652
messenger::error('EMAIL', $err_msg);
658
foreach ($addresses as $address)
660
if ($this->jabber->send_message($address, $msg, $subject) === false)
662
messenger::error('JABBER', $this->jabber->get_log());
670
// No more data for this object? Unset it
671
if (!sizeof($this->queue_data[$object]['data']))
673
unset($this->queue_data[$object]);
676
// Post-object processing
680
// Hang about a couple of secs to ensure the messages are
681
// handled, then disconnect
682
$this->jabber->disconnect();
687
if (!sizeof($this->queue_data))
689
@unlink($this->cache_file);
693
if ($fp = @fopen($this->cache_file, 'w'))
695
@flock($fp, LOCK_EX);
696
fwrite($fp, "<?php\n\$this->queue_data = " . var_export($this->queue_data, true) . ";\n?>");
697
@flock($fp, LOCK_UN);
700
@chmod($this->cache_file, 0666);
704
@unlink($this->cache_file . '.lock');
712
if (!sizeof($this->data))
717
if (file_exists($this->cache_file))
719
include($this->cache_file);
721
foreach ($this->queue_data as $object => $data_ary)
723
if (isset($this->data[$object]) && sizeof($this->data[$object]))
725
$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
729
$this->data[$object]['data'] = $data_ary['data'];
734
if ($fp = @fopen($this->cache_file, 'w'))
736
@flock($fp, LOCK_EX);
737
fwrite($fp, "<?php\n\$this->queue_data = " . var_export($this->data, true) . ";\n?>");
738
@flock($fp, LOCK_UN);
741
@chmod($this->cache_file, 0666);
747
* Replacement or substitute for PHP's mail command
749
function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '')
751
global $config, $user;
753
// Fix any bare linefeeds in the message to make it RFC821 Compliant.
754
$message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
758
if (is_array($headers))
760
$headers = (sizeof($headers) > 1) ? join("\n", $headers) : $headers[0];
762
$headers = chop($headers);
764
// Make sure there are no bare linefeeds in the headers
765
$headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
767
// Ok this is rather confusing all things considered,
768
// but we have to grab bcc and cc headers and treat them differently
769
// Something we really didn't take into consideration originally
770
$header_array = explode("\r\n", $headers);
773
foreach ($header_array as $header)
775
if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
779
$headers .= ($header != '') ? $header . "\r\n" : '';
782
$headers = chop($headers);
785
if (trim($subject) == '')
787
$err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
791
if (trim($message) == '')
793
$err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
797
$mail_rcpt = $mail_to = $mail_cc = array();
799
// Build correct addresses for RCPT TO command and the client side display (TO, CC)
800
if (isset($addresses['to']) && sizeof($addresses['to']))
802
foreach ($addresses['to'] as $which_ary)
804
$mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
805
$mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
809
if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
811
foreach ($addresses['bcc'] as $which_ary)
813
$mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
817
if (isset($addresses['cc']) && sizeof($addresses['cc']))
819
foreach ($addresses['cc'] as $which_ary)
821
$mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
822
$mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
826
$smtp = new smtp_class();
831
$smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
833
// Ok we have error checked as much as we can to this point let's get on it already.
835
$smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
836
$error_contents = ob_get_clean();
842
$errstr = utf8_convert_message($errstr);
845
$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";
846
$err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents) : '';
851
if ($err_msg = $smtp->server_parse('220', __LINE__))
853
$smtp->close_session($err_msg);
857
// Let me in. This function handles the complete authentication process
858
if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
860
$smtp->close_session($err_msg);
864
// From this point onward most server response codes should be 250
865
// Specify who the mail is from....
866
$smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
867
if ($err_msg = $smtp->server_parse('250', __LINE__))
869
$smtp->close_session($err_msg);
873
// Specify each user to send to and build to header.
874
$to_header = implode(', ', $mail_to);
875
$cc_header = implode(', ', $mail_cc);
877
// Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
879
foreach ($mail_rcpt as $type => $mail_to_addresses)
881
foreach ($mail_to_addresses as $mail_to_address)
883
// Add an additional bit of error checking to the To field.
884
if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
886
$smtp->server_send("RCPT TO:$mail_to_address");
887
if ($err_msg = $smtp->server_parse('250', __LINE__))
889
// We continue... if users are not resolved we do not care
890
if ($smtp->numeric_response_code != 550)
892
$smtp->close_session($err_msg);
904
// 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.
907
$user->session_begin();
908
$err_msg .= '<br /><br />';
909
$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?';
910
$smtp->close_session($err_msg);
914
// Ok now we tell the server we are ready to start sending data
915
$smtp->server_send('DATA');
917
// This is the last response code we look for until the end of the message.
918
if ($err_msg = $smtp->server_parse('354', __LINE__))
920
$smtp->close_session($err_msg);
924
// Send the Subject Line...
925
$smtp->server_send("Subject: $subject");
927
// Now the To Header.
928
$to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
929
$smtp->server_send("To: $to_header");
931
// Now the CC Header.
932
if ($cc_header != '')
934
$smtp->server_send("CC: $cc_header");
937
// Now any custom headers....
938
$smtp->server_send("$headers\r\n");
940
// Ok now we are ready for the message...
941
$smtp->server_send($message);
943
// Ok the all the ingredients are mixed in let's cook this puppy...
944
$smtp->server_send('.');
945
if ($err_msg = $smtp->server_parse('250', __LINE__))
947
$smtp->close_session($err_msg);
951
// Now tell the server we are done and close the socket...
952
$smtp->server_send('QUIT');
953
$smtp->close_session($err_msg);
960
* Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
961
* See docs/AUTHORS for more details
966
var $server_response = '';
968
var $responses = array();
969
var $commands = array();
970
var $numeric_response_code = 0;
972
var $backtrace = false;
973
var $backtrace_log = array();
975
function smtp_class()
977
// Always create a backtrace for admins to identify SMTP problems
978
$this->backtrace = true;
979
$this->backtrace_log = array();
983
* Add backtrace message for debugging
985
function add_backtrace($message)
987
if ($this->backtrace)
989
$this->backtrace_log[] = utf8_htmlspecialchars($message);
994
* Send command to smtp server
996
function server_send($command, $private_info = false)
998
fputs($this->socket, $command . "\r\n");
1000
(!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
1002
// We could put additional code here
1006
* We use the line to give the support people an indication at which command the error occurred
1008
function server_parse($response, $line)
1012
$this->server_response = '';
1013
$this->responses = array();
1014
$this->numeric_response_code = 0;
1016
while (substr($this->server_response, 3, 1) != ' ')
1018
if (!($this->server_response = fgets($this->socket, 256)))
1020
return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
1022
$this->responses[] = substr(rtrim($this->server_response), 4);
1023
$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
1025
$this->add_backtrace("LINE: $line <- {$this->server_response}");
1028
if (!(substr($this->server_response, 0, 3) == $response))
1030
$this->numeric_response_code = (int) substr($this->server_response, 0, 3);
1031
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";
1040
function close_session(&$err_msg)
1042
fclose($this->socket);
1044
if ($this->backtrace)
1046
$message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>';
1047
$err_msg .= $message;
1052
* Log into server and get possible auth codes if neccessary
1054
function log_into_server($hostname, $username, $password, $default_auth_method)
1059
$local_host = php_uname('n');
1060
$local_host = (empty($local_host)) ? 'localhost' : $local_host;
1062
// If we are authenticating through pop-before-smtp, we
1063
// have to login ones before we get authenticated
1064
// NOTE: on some configurations the time between an update of the auth database takes so
1065
// long that the first email send does not work. This is not a biggie on a live board (only
1066
// the install mail will most likely fail) - but on a dynamic ip connection this might produce
1067
// severe problems and is not fixable!
1068
if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password)
1075
$this->server_send("QUIT");
1076
fclose($this->socket);
1078
$result = $this->pop_before_smtp($hostname, $username, $password);
1079
$username = $password = $default_auth_method = '';
1081
// We need to close the previous session, else the server is not
1082
// able to get our ip for matching...
1083
if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10))
1087
$errstr = utf8_convert_message($errstr);
1090
$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";
1095
if ($err_msg = $this->server_parse('220', __LINE__))
1097
$this->close_session($err_msg);
1103
$this->server_send("EHLO {$local_host}");
1104
if ($err_msg = $this->server_parse('250', __LINE__))
1106
// a 503 response code means that we're already authenticated
1107
if ($this->numeric_response_code == 503)
1112
// If EHLO fails, we try HELO
1113
$this->server_send("HELO {$local_host}");
1114
if ($err_msg = $this->server_parse('250', __LINE__))
1116
return ($this->numeric_response_code == 503) ? false : $err_msg;
1120
foreach ($this->responses as $response)
1122
$response = explode(' ', $response);
1123
$response_code = $response[0];
1124
unset($response[0]);
1125
$this->commands[$response_code] = implode(' ', $response);
1128
// If we are not authenticated yet, something might be wrong if no username and passwd passed
1129
if (!$username || !$password)
1134
if (!isset($this->commands['AUTH']))
1136
return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication';
1139
// Get best authentication method
1140
$available_methods = explode(' ', $this->commands['AUTH']);
1142
// Define the auth ordering if the default auth method was not found
1143
$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5');
1146
if (in_array($default_auth_method, $available_methods))
1148
$method = $default_auth_method;
1152
foreach ($auth_methods as $_method)
1154
if (in_array($_method, $available_methods))
1164
return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods';
1167
$method = strtolower(str_replace('-', '_', $method));
1168
return $this->$method($username, $password);
1172
* Pop before smtp authentication
1174
function pop_before_smtp($hostname, $username, $password)
1178
if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10))
1182
$errstr = utf8_convert_message($errstr);
1185
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";
1188
$this->server_send("USER $username", true);
1189
if ($err_msg = $this->server_parse('+OK', __LINE__))
1194
$this->server_send("PASS $password", true);
1195
if ($err_msg = $this->server_parse('+OK', __LINE__))
1200
$this->server_send('QUIT');
1201
fclose($this->socket);
1207
* Plain authentication method
1209
function plain($username, $password)
1211
$this->server_send('AUTH PLAIN');
1212
if ($err_msg = $this->server_parse('334', __LINE__))
1214
return ($this->numeric_response_code == 503) ? false : $err_msg;
1217
$base64_method_plain = base64_encode("\0" . $username . "\0" . $password);
1218
$this->server_send($base64_method_plain, true);
1219
if ($err_msg = $this->server_parse('235', __LINE__))
1228
* Login authentication method
1230
function login($username, $password)
1232
$this->server_send('AUTH LOGIN');
1233
if ($err_msg = $this->server_parse('334', __LINE__))
1235
return ($this->numeric_response_code == 503) ? false : $err_msg;
1238
$this->server_send(base64_encode($username), true);
1239
if ($err_msg = $this->server_parse('334', __LINE__))
1244
$this->server_send(base64_encode($password), true);
1245
if ($err_msg = $this->server_parse('235', __LINE__))
1254
* cram_md5 authentication method
1256
function cram_md5($username, $password)
1258
$this->server_send('AUTH CRAM-MD5');
1259
if ($err_msg = $this->server_parse('334', __LINE__))
1261
return ($this->numeric_response_code == 503) ? false : $err_msg;
1264
$md5_challenge = base64_decode($this->responses[0]);
1265
$password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password);
1266
$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))));
1268
$base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest);
1270
$this->server_send($base64_method_cram_md5, true);
1271
if ($err_msg = $this->server_parse('235', __LINE__))
1280
* digest_md5 authentication method
1281
* A real pain in the ***
1283
function digest_md5($username, $password)
1285
global $config, $user;
1287
$this->server_send('AUTH DIGEST-MD5');
1288
if ($err_msg = $this->server_parse('334', __LINE__))
1290
return ($this->numeric_response_code == 503) ? false : $err_msg;
1293
$md5_challenge = base64_decode($this->responses[0]);
1295
// Parse the md5 challenge - from AUTH_SASL (PEAR)
1297
while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches))
1299
// Ignore these as per rfc2831
1300
if ($matches[1] == 'opaque' || $matches[1] == 'domain')
1302
$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
1306
// Allowed multiple "realm" and "auth-param"
1307
if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param'))
1309
if (is_array($tokens[$matches[1]]))
1311
$tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
1315
$tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
1318
else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure
1325
$tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
1328
// Remove the just parsed directive from the challenge
1329
$md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
1333
if (empty($tokens['realm']))
1335
$tokens['realm'] = php_uname('n');
1339
if (empty($tokens['maxbuf']))
1341
$tokens['maxbuf'] = 65536;
1344
// Required: nonce, algorithm
1345
if (empty($tokens['nonce']) || empty($tokens['algorithm']))
1349
$md5_challenge = $tokens;
1351
if (!empty($md5_challenge))
1354
for ($i = 0; $i < 32; $i++)
1356
$str .= chr(mt_rand(0, 255));
1358
$cnonce = base64_encode($str);
1360
$digest_uri = 'smtp/' . $config['smtp_host'];
1362
$auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce);
1363
$auth_2 = 'AUTHENTICATE:' . $digest_uri;
1364
$response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2)));
1366
$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']);
1370
return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge';
1373
$base64_method_digest_md5 = base64_encode($input_string);
1374
$this->server_send($base64_method_digest_md5, true);
1375
if ($err_msg = $this->server_parse('334', __LINE__))
1380
$this->server_send(' ');
1381
if ($err_msg = $this->server_parse('235', __LINE__))
1391
* Encodes the given string for proper display in UTF-8.
1393
* This version is using base64 encoded data. The downside of this
1394
* is if the mail client does not understand this encoding the user
1395
* is basically doomed with an unreadable subject.
1397
* Please note that this version fully supports RFC 2045 section 6.8.
1399
function mail_encode($str)
1401
// define start delimimter, end delimiter and spacer
1402
$start = "=?UTF-8?B?";
1404
$spacer = $end . ' ' . $start;
1407
$encoded_str = base64_encode($str);
1409
// If encoded string meets the limits, we just return with the correct data.
1410
if (strlen($encoded_str) <= $split_length)
1412
return $start . $encoded_str . $end;
1415
// If there is only ASCII data, we just return what we want, correctly splitting the lines.
1416
if (strlen($str) === utf8_strlen($str))
1418
return $start . implode($spacer, str_split($encoded_str, $split_length)) . $end;
1421
// UTF-8 data, compose encoded lines
1422
$array = utf8_str_split($str);
1425
while (sizeof($array))
1429
while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length)
1431
$text .= array_shift($array);
1434
$str .= $start . base64_encode($text) . $end . ' ';
1437
return substr($str, 0, -1);
b'\\ No newline at end of file'