indvdum@0: use_queue = (!$config['email_package_size']) ? false : $use_queue;
indvdum@0: $this->subject = '';
indvdum@0:
indvdum@0: // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
indvdum@0: $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
indvdum@0: $this->eol = (!$this->eol) ? "\n" : $this->eol;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Resets all the data (address, template file, etc etc) to default
indvdum@0: */
indvdum@0: function reset()
indvdum@0: {
indvdum@0: $this->addresses = $this->extra_headers = array();
indvdum@0: $this->vars = $this->msg = $this->replyto = $this->from = '';
indvdum@0: $this->mail_priority = MAIL_NORMAL_PRIORITY;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Sets an email address to send to
indvdum@0: */
indvdum@0: function to($address, $realname = '')
indvdum@0: {
indvdum@0: global $config;
indvdum@0:
indvdum@0: if (!trim($address))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
indvdum@0:
indvdum@0: $this->addresses['to'][$pos]['email'] = trim($address);
indvdum@0:
indvdum@0: // If empty sendmail_path on windows, PHP changes the to line
indvdum@0: if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
indvdum@0: {
indvdum@0: $this->addresses['to'][$pos]['name'] = '';
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $this->addresses['to'][$pos]['name'] = trim($realname);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Sets an cc address to send to
indvdum@0: */
indvdum@0: function cc($address, $realname = '')
indvdum@0: {
indvdum@0: if (!trim($address))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
indvdum@0: $this->addresses['cc'][$pos]['email'] = trim($address);
indvdum@0: $this->addresses['cc'][$pos]['name'] = trim($realname);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Sets an bcc address to send to
indvdum@0: */
indvdum@0: function bcc($address, $realname = '')
indvdum@0: {
indvdum@0: if (!trim($address))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
indvdum@0: $this->addresses['bcc'][$pos]['email'] = trim($address);
indvdum@0: $this->addresses['bcc'][$pos]['name'] = trim($realname);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Sets a im contact to send to
indvdum@0: */
indvdum@0: function im($address, $realname = '')
indvdum@0: {
indvdum@0: // IM-Addresses could be empty
indvdum@0: if (!trim($address))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
indvdum@0: $this->addresses['im'][$pos]['uid'] = trim($address);
indvdum@0: $this->addresses['im'][$pos]['name'] = trim($realname);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Set the reply to address
indvdum@0: */
indvdum@0: function replyto($address)
indvdum@0: {
indvdum@0: $this->replyto = trim($address);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Set the from address
indvdum@0: */
indvdum@0: function from($address)
indvdum@0: {
indvdum@0: $this->from = trim($address);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * set up subject for mail
indvdum@0: */
indvdum@0: function subject($subject = '')
indvdum@0: {
indvdum@0: $this->subject = trim($subject);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * set up extra mail headers
indvdum@0: */
indvdum@0: function headers($headers)
indvdum@0: {
indvdum@0: $this->extra_headers[] = trim($headers);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Adds X-AntiAbuse headers
indvdum@0: *
indvdum@0: * @param array $config Configuration array
indvdum@0: * @param user $user A user object
indvdum@0: *
indvdum@0: * @return null
indvdum@0: */
indvdum@0: function anti_abuse_headers($config, $user)
indvdum@0: {
indvdum@0: $this->headers('X-AntiAbuse: Board servername - ' . mail_encode($config['server_name']));
indvdum@0: $this->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
indvdum@0: $this->headers('X-AntiAbuse: Username - ' . mail_encode($user->data['username']));
indvdum@0: $this->headers('X-AntiAbuse: User IP - ' . $user->ip);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Set the email priority
indvdum@0: */
indvdum@0: function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
indvdum@0: {
indvdum@0: $this->mail_priority = $priority;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Set email template to use
indvdum@0: */
indvdum@0: function template($template_file, $template_lang = '', $template_path = '')
indvdum@0: {
indvdum@0: global $config, $phpbb_root_path, $user;
indvdum@0:
indvdum@0: if (!trim($template_file))
indvdum@0: {
indvdum@0: trigger_error('No template file for emailing set.', E_USER_ERROR);
indvdum@0: }
indvdum@0:
indvdum@0: if (!trim($template_lang))
indvdum@0: {
indvdum@0: // fall back to board default language if the user's language is
indvdum@0: // missing $template_file. If this does not exist either,
indvdum@0: // $tpl->set_custom_template will do a trigger_error
indvdum@0: $template_lang = basename($config['default_lang']);
indvdum@0: }
indvdum@0:
indvdum@0: // tpl_msg now holds a template object we can use to parse the template file
indvdum@0: if (!isset($this->tpl_msg[$template_lang . $template_file]))
indvdum@0: {
indvdum@0: $this->tpl_msg[$template_lang . $template_file] = new template();
indvdum@0: $tpl = &$this->tpl_msg[$template_lang . $template_file];
indvdum@0:
indvdum@0: $fallback_template_path = false;
indvdum@0:
indvdum@0: if (!$template_path)
indvdum@0: {
indvdum@0: $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
indvdum@0: $template_path .= $template_lang . '/email';
indvdum@0:
indvdum@0: // we can only specify default language fallback when the path is not a custom one for which we
indvdum@0: // do not know the default language alternative
indvdum@0: if ($template_lang !== basename($config['default_lang']))
indvdum@0: {
indvdum@0: $fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
indvdum@0: $fallback_template_path .= basename($config['default_lang']) . '/email';
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: $tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path);
indvdum@0:
indvdum@0: $tpl->set_filenames(array(
indvdum@0: 'body' => $template_file . '.txt',
indvdum@0: ));
indvdum@0: }
indvdum@0:
indvdum@0: $this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file];
indvdum@0: $this->vars = &$this->tpl_obj->_rootref;
indvdum@0: $this->tpl_msg = '';
indvdum@0:
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * assign variables to email template
indvdum@0: */
indvdum@0: function assign_vars($vars)
indvdum@0: {
indvdum@0: if (!is_object($this->tpl_obj))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $this->tpl_obj->assign_vars($vars);
indvdum@0: }
indvdum@0:
indvdum@0: function assign_block_vars($blockname, $vars)
indvdum@0: {
indvdum@0: if (!is_object($this->tpl_obj))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $this->tpl_obj->assign_block_vars($blockname, $vars);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Send the mail out to the recipients set previously in var $this->addresses
indvdum@0: */
indvdum@0: function send($method = NOTIFY_EMAIL, $break = false)
indvdum@0: {
indvdum@0: global $config, $user;
indvdum@0:
indvdum@0: // We add some standard variables we always use, no need to specify them always
indvdum@0: if (!isset($this->vars['U_BOARD']))
indvdum@0: {
indvdum@0: $this->assign_vars(array(
indvdum@0: 'U_BOARD' => generate_board_url(),
indvdum@0: ));
indvdum@0: }
indvdum@0:
indvdum@0: if (!isset($this->vars['EMAIL_SIG']))
indvdum@0: {
indvdum@0: $this->assign_vars(array(
indvdum@0: 'EMAIL_SIG' => str_replace('
', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])),
indvdum@0: ));
indvdum@0: }
indvdum@0:
indvdum@0: if (!isset($this->vars['SITENAME']))
indvdum@0: {
indvdum@0: $this->assign_vars(array(
indvdum@0: 'SITENAME' => htmlspecialchars_decode($config['sitename']),
indvdum@0: ));
indvdum@0: }
indvdum@0:
indvdum@0: // Parse message through template
indvdum@0: $this->msg = trim($this->tpl_obj->assign_display('body'));
indvdum@0:
indvdum@0: // 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: $this->msg = str_replace("\r\n", "\n", $this->msg);
indvdum@0:
indvdum@0: // We now try and pull a subject from the email body ... if it exists,
indvdum@0: // do this here because the subject may contain a variable
indvdum@0: $drop_header = '';
indvdum@0: $match = array();
indvdum@0: if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
indvdum@0: {
indvdum@0: $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
indvdum@0: $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
indvdum@0: }
indvdum@0:
indvdum@0: if ($drop_header)
indvdum@0: {
indvdum@0: $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
indvdum@0: }
indvdum@0:
indvdum@0: if ($break)
indvdum@0: {
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: switch ($method)
indvdum@0: {
indvdum@0: case NOTIFY_EMAIL:
indvdum@0: $result = $this->msg_email();
indvdum@0: break;
indvdum@0:
indvdum@0: case NOTIFY_IM:
indvdum@0: $result = $this->msg_jabber();
indvdum@0: break;
indvdum@0:
indvdum@0: case NOTIFY_BOTH:
indvdum@0: $result = $this->msg_email();
indvdum@0: $this->msg_jabber();
indvdum@0: break;
indvdum@0: }
indvdum@0:
indvdum@0: $this->reset();
indvdum@0: return $result;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Add error message to log
indvdum@0: */
indvdum@0: function error($type, $msg)
indvdum@0: {
indvdum@0: global $user, $phpEx, $phpbb_root_path, $config;
indvdum@0:
indvdum@0: // Session doesn't exist, create it
indvdum@0: if (!isset($user->session_id) || $user->session_id === '')
indvdum@0: {
indvdum@0: $user->session_begin();
indvdum@0: }
indvdum@0:
indvdum@0: $calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
indvdum@0:
indvdum@0: $message = '';
indvdum@0: switch ($type)
indvdum@0: {
indvdum@0: case 'EMAIL':
indvdum@0: $message = 'EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '';
indvdum@0: break;
indvdum@0:
indvdum@0: default:
indvdum@0: $message = "$type";
indvdum@0: break;
indvdum@0: }
indvdum@0:
indvdum@0: $message .= '
' . htmlspecialchars($calling_page) . '
' . $msg . '
';
indvdum@0: add_log('critical', 'LOG_ERROR_' . $type, $message);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Save to queue
indvdum@0: */
indvdum@0: function save_queue()
indvdum@0: {
indvdum@0: global $config;
indvdum@0:
indvdum@0: if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
indvdum@0: {
indvdum@0: $this->queue->save();
indvdum@0: return;
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Return email header
indvdum@0: */
indvdum@0: function build_header($to, $cc, $bcc)
indvdum@0: {
indvdum@0: global $config;
indvdum@0:
indvdum@0: // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
indvdum@0: $headers = array();
indvdum@0:
indvdum@0: $headers[] = 'From: ' . $this->from;
indvdum@0:
indvdum@0: if ($cc)
indvdum@0: {
indvdum@0: $headers[] = 'Cc: ' . $cc;
indvdum@0: }
indvdum@0:
indvdum@0: if ($bcc)
indvdum@0: {
indvdum@0: $headers[] = 'Bcc: ' . $bcc;
indvdum@0: }
indvdum@0:
indvdum@0: $headers[] = 'Reply-To: ' . $this->replyto;
indvdum@0: $headers[] = 'Return-Path: <' . $config['board_email'] . '>';
indvdum@0: $headers[] = 'Sender: <' . $config['board_email'] . '>';
indvdum@0: $headers[] = 'MIME-Version: 1.0';
indvdum@0: $headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
indvdum@0: $headers[] = 'Date: ' . date('r', time());
indvdum@0: $headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
indvdum@0: $headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
indvdum@0:
indvdum@0: $headers[] = 'X-Priority: ' . $this->mail_priority;
indvdum@0: $headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
indvdum@0: $headers[] = 'X-Mailer: phpBB3';
indvdum@0: $headers[] = 'X-MimeOLE: phpBB3';
indvdum@0: $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
indvdum@0:
indvdum@0: if (sizeof($this->extra_headers))
indvdum@0: {
indvdum@0: $headers = array_merge($headers, $this->extra_headers);
indvdum@0: }
indvdum@0:
indvdum@0: return $headers;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Send out emails
indvdum@0: */
indvdum@0: function msg_email()
indvdum@0: {
indvdum@0: global $config, $user;
indvdum@0:
indvdum@0: if (empty($config['email_enable']))
indvdum@0: {
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Addresses to send to?
indvdum@0: if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc'])))
indvdum@0: {
indvdum@0: // Send was successful. ;)
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: $use_queue = false;
indvdum@0: if ($config['email_package_size'] && $this->use_queue)
indvdum@0: {
indvdum@0: if (empty($this->queue))
indvdum@0: {
indvdum@0: $this->queue = new queue();
indvdum@0: $this->queue->init('email', $config['email_package_size']);
indvdum@0: }
indvdum@0: $use_queue = true;
indvdum@0: }
indvdum@0:
indvdum@0: if (empty($this->replyto))
indvdum@0: {
indvdum@0: $this->replyto = '<' . $config['board_contact'] . '>';
indvdum@0: }
indvdum@0:
indvdum@0: if (empty($this->from))
indvdum@0: {
indvdum@0: $this->from = '<' . $config['board_contact'] . '>';
indvdum@0: }
indvdum@0:
indvdum@0: $encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol;
indvdum@0:
indvdum@0: // Build to, cc and bcc strings
indvdum@0: $to = $cc = $bcc = '';
indvdum@0: foreach ($this->addresses as $type => $address_ary)
indvdum@0: {
indvdum@0: if ($type == 'im')
indvdum@0: {
indvdum@0: continue;
indvdum@0: }
indvdum@0:
indvdum@0: foreach ($address_ary as $which_ary)
indvdum@0: {
indvdum@0: $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: // Build header
indvdum@0: $headers = $this->build_header($to, $cc, $bcc);
indvdum@0:
indvdum@0: // Send message ...
indvdum@0: if (!$use_queue)
indvdum@0: {
indvdum@0: $mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
indvdum@0: $err_msg = '';
indvdum@0:
indvdum@0: if ($config['smtp_delivery'])
indvdum@0: {
indvdum@0: $result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg);
indvdum@0: }
indvdum@0:
indvdum@0: if (!$result)
indvdum@0: {
indvdum@0: $this->error('EMAIL', $err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $this->queue->put('email', array(
indvdum@0: 'to' => $to,
indvdum@0: 'addresses' => $this->addresses,
indvdum@0: 'subject' => $this->subject,
indvdum@0: 'msg' => $this->msg,
indvdum@0: 'headers' => $headers)
indvdum@0: );
indvdum@0: }
indvdum@0:
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Send jabber message out
indvdum@0: */
indvdum@0: function msg_jabber()
indvdum@0: {
indvdum@0: global $config, $db, $user, $phpbb_root_path, $phpEx;
indvdum@0:
indvdum@0: if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
indvdum@0: {
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: if (empty($this->addresses['im']))
indvdum@0: {
indvdum@0: // Send was successful. ;)
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: $use_queue = false;
indvdum@0: if ($config['jab_package_size'] && $this->use_queue)
indvdum@0: {
indvdum@0: if (empty($this->queue))
indvdum@0: {
indvdum@0: $this->queue = new queue();
indvdum@0: $this->queue->init('jabber', $config['jab_package_size']);
indvdum@0: }
indvdum@0: $use_queue = true;
indvdum@0: }
indvdum@0:
indvdum@0: $addresses = array();
indvdum@0: foreach ($this->addresses['im'] as $type => $uid_ary)
indvdum@0: {
indvdum@0: $addresses[] = $uid_ary['uid'];
indvdum@0: }
indvdum@0: $addresses = array_unique($addresses);
indvdum@0:
indvdum@0: if (!$use_queue)
indvdum@0: {
indvdum@0: include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
indvdum@0: $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
indvdum@0:
indvdum@0: if (!$this->jabber->connect())
indvdum@0: {
indvdum@0: $this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '
' . $this->jabber->get_log());
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: if (!$this->jabber->login())
indvdum@0: {
indvdum@0: $this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '
' . $this->jabber->get_log());
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: foreach ($addresses as $address)
indvdum@0: {
indvdum@0: $this->jabber->send_message($address, $this->msg, $this->subject);
indvdum@0: }
indvdum@0:
indvdum@0: $this->jabber->disconnect();
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $this->queue->put('jabber', array(
indvdum@0: 'addresses' => $addresses,
indvdum@0: 'subject' => $this->subject,
indvdum@0: 'msg' => $this->msg)
indvdum@0: );
indvdum@0: }
indvdum@0: unset($addresses);
indvdum@0: return true;
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * handling email and jabber queue
indvdum@0: * @package phpBB3
indvdum@0: */
indvdum@0: class queue
indvdum@0: {
indvdum@0: var $data = array();
indvdum@0: var $queue_data = array();
indvdum@0: var $package_size = 0;
indvdum@0: var $cache_file = '';
indvdum@0: var $eol = "\n";
indvdum@0:
indvdum@0: /**
indvdum@0: * constructor
indvdum@0: */
indvdum@0: function queue()
indvdum@0: {
indvdum@0: global $phpEx, $phpbb_root_path;
indvdum@0:
indvdum@0: $this->data = array();
indvdum@0: $this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
indvdum@0:
indvdum@0: // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
indvdum@0: $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
indvdum@0: $this->eol = (!$this->eol) ? "\n" : $this->eol;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Init a queue object
indvdum@0: */
indvdum@0: function init($object, $package_size)
indvdum@0: {
indvdum@0: $this->data[$object] = array();
indvdum@0: $this->data[$object]['package_size'] = $package_size;
indvdum@0: $this->data[$object]['data'] = array();
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Put object in queue
indvdum@0: */
indvdum@0: function put($object, $scope)
indvdum@0: {
indvdum@0: $this->data[$object]['data'][] = $scope;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Obtains exclusive lock on queue cache file.
indvdum@0: * Returns resource representing the lock
indvdum@0: */
indvdum@0: function lock()
indvdum@0: {
indvdum@0: // For systems that can't have two processes opening
indvdum@0: // one file for writing simultaneously
indvdum@0: if (file_exists($this->cache_file . '.lock'))
indvdum@0: {
indvdum@0: $mode = 'rb';
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $mode = 'wb';
indvdum@0: }
indvdum@0:
indvdum@0: $lock_fp = @fopen($this->cache_file . '.lock', $mode);
indvdum@0:
indvdum@0: if ($mode == 'wb')
indvdum@0: {
indvdum@0: if (!$lock_fp)
indvdum@0: {
indvdum@0: // Two processes may attempt to create lock file at the same time.
indvdum@0: // Have the losing process try opening the lock file again for reading
indvdum@0: // on the assumption that the winning process created it
indvdum@0: $mode = 'rb';
indvdum@0: $lock_fp = @fopen($this->cache_file . '.lock', $mode);
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: // Only need to set mode when the lock file is written
indvdum@0: @chmod($this->cache_file . '.lock', 0666);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: if ($lock_fp)
indvdum@0: {
indvdum@0: @flock($lock_fp, LOCK_EX);
indvdum@0: }
indvdum@0:
indvdum@0: return $lock_fp;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Releases lock on queue cache file, using resource obtained from lock()
indvdum@0: */
indvdum@0: function unlock($lock_fp)
indvdum@0: {
indvdum@0: // lock() will return null if opening lock file, and thus locking, failed.
indvdum@0: // Accept null values here so that client code does not need to check them
indvdum@0: if ($lock_fp)
indvdum@0: {
indvdum@0: @flock($lock_fp, LOCK_UN);
indvdum@0: fclose($lock_fp);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Process queue
indvdum@0: * Using lock file
indvdum@0: */
indvdum@0: function process()
indvdum@0: {
indvdum@0: global $db, $config, $phpEx, $phpbb_root_path, $user;
indvdum@0:
indvdum@0: $lock_fp = $this->lock();
indvdum@0:
indvdum@0: set_config('last_queue_run', time(), true);
indvdum@0:
indvdum@0: if (!file_exists($this->cache_file) || filemtime($this->cache_file) > time() - $config['queue_interval'])
indvdum@0: {
indvdum@0: $this->unlock($lock_fp);
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: include($this->cache_file);
indvdum@0:
indvdum@0: foreach ($this->queue_data as $object => $data_ary)
indvdum@0: {
indvdum@0: @set_time_limit(0);
indvdum@0:
indvdum@0: if (!isset($data_ary['package_size']))
indvdum@0: {
indvdum@0: $data_ary['package_size'] = 0;
indvdum@0: }
indvdum@0:
indvdum@0: $package_size = $data_ary['package_size'];
indvdum@0: $num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
indvdum@0:
indvdum@0: /*
indvdum@0: * This code is commented out because it causes problems on some web hosts.
indvdum@0: * The core problem is rather restrictive email sending limits.
indvdum@0: * This code is nly useful if you have no such restrictions from the
indvdum@0: * web host and the package size setting is wrong.
indvdum@0:
indvdum@0: // 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: if (sizeof($data_ary['data']) > $package_size * 2.5)
indvdum@0: {
indvdum@0: $num_items = sizeof($data_ary['data']);
indvdum@0: }
indvdum@0: */
indvdum@0:
indvdum@0: switch ($object)
indvdum@0: {
indvdum@0: case 'email':
indvdum@0: // Delete the email queued objects if mailing is disabled
indvdum@0: if (!$config['email_enable'])
indvdum@0: {
indvdum@0: unset($this->queue_data['email']);
indvdum@0: continue 2;
indvdum@0: }
indvdum@0: break;
indvdum@0:
indvdum@0: case 'jabber':
indvdum@0: if (!$config['jab_enable'])
indvdum@0: {
indvdum@0: unset($this->queue_data['jabber']);
indvdum@0: continue 2;
indvdum@0: }
indvdum@0:
indvdum@0: include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
indvdum@0: $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
indvdum@0:
indvdum@0: if (!$this->jabber->connect())
indvdum@0: {
indvdum@0: messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
indvdum@0: continue 2;
indvdum@0: }
indvdum@0:
indvdum@0: if (!$this->jabber->login())
indvdum@0: {
indvdum@0: messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
indvdum@0: continue 2;
indvdum@0: }
indvdum@0:
indvdum@0: break;
indvdum@0:
indvdum@0: default:
indvdum@0: $this->unlock($lock_fp);
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: for ($i = 0; $i < $num_items; $i++)
indvdum@0: {
indvdum@0: // Make variables available...
indvdum@0: extract(array_shift($this->queue_data[$object]['data']));
indvdum@0:
indvdum@0: switch ($object)
indvdum@0: {
indvdum@0: case 'email':
indvdum@0: $err_msg = '';
indvdum@0: $to = (!$to) ? 'undisclosed-recipients:;' : $to;
indvdum@0:
indvdum@0: if ($config['smtp_delivery'])
indvdum@0: {
indvdum@0: $result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg);
indvdum@0: }
indvdum@0:
indvdum@0: if (!$result)
indvdum@0: {
indvdum@0: messenger::error('EMAIL', $err_msg);
indvdum@0: continue 2;
indvdum@0: }
indvdum@0: break;
indvdum@0:
indvdum@0: case 'jabber':
indvdum@0: foreach ($addresses as $address)
indvdum@0: {
indvdum@0: if ($this->jabber->send_message($address, $msg, $subject) === false)
indvdum@0: {
indvdum@0: messenger::error('JABBER', $this->jabber->get_log());
indvdum@0: continue 3;
indvdum@0: }
indvdum@0: }
indvdum@0: break;
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: // No more data for this object? Unset it
indvdum@0: if (!sizeof($this->queue_data[$object]['data']))
indvdum@0: {
indvdum@0: unset($this->queue_data[$object]);
indvdum@0: }
indvdum@0:
indvdum@0: // Post-object processing
indvdum@0: switch ($object)
indvdum@0: {
indvdum@0: case 'jabber':
indvdum@0: // Hang about a couple of secs to ensure the messages are
indvdum@0: // handled, then disconnect
indvdum@0: $this->jabber->disconnect();
indvdum@0: break;
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: if (!sizeof($this->queue_data))
indvdum@0: {
indvdum@0: @unlink($this->cache_file);
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: if ($fp = @fopen($this->cache_file, 'wb'))
indvdum@0: {
indvdum@0: fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
indvdum@0: fclose($fp);
indvdum@0:
indvdum@0: phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: $this->unlock($lock_fp);
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Save queue
indvdum@0: */
indvdum@0: function save()
indvdum@0: {
indvdum@0: if (!sizeof($this->data))
indvdum@0: {
indvdum@0: return;
indvdum@0: }
indvdum@0:
indvdum@0: $lock_fp = $this->lock();
indvdum@0:
indvdum@0: if (file_exists($this->cache_file))
indvdum@0: {
indvdum@0: include($this->cache_file);
indvdum@0:
indvdum@0: foreach ($this->queue_data as $object => $data_ary)
indvdum@0: {
indvdum@0: if (isset($this->data[$object]) && sizeof($this->data[$object]))
indvdum@0: {
indvdum@0: $this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $this->data[$object]['data'] = $data_ary['data'];
indvdum@0: }
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: if ($fp = @fopen($this->cache_file, 'w'))
indvdum@0: {
indvdum@0: fwrite($fp, "queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
indvdum@0: fclose($fp);
indvdum@0:
indvdum@0: phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
indvdum@0: }
indvdum@0:
indvdum@0: $this->unlock($lock_fp);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Replacement or substitute for PHP's mail command
indvdum@0: */
indvdum@0: function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)
indvdum@0: {
indvdum@0: global $config, $user;
indvdum@0:
indvdum@0: // Fix any bare linefeeds in the message to make it RFC821 Compliant.
indvdum@0: $message = preg_replace("#(?lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: if (trim($message) == '')
indvdum@0: {
indvdum@0: $err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: $mail_rcpt = $mail_to = $mail_cc = array();
indvdum@0:
indvdum@0: // Build correct addresses for RCPT TO command and the client side display (TO, CC)
indvdum@0: if (isset($addresses['to']) && sizeof($addresses['to']))
indvdum@0: {
indvdum@0: foreach ($addresses['to'] as $which_ary)
indvdum@0: {
indvdum@0: $mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
indvdum@0: $mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
indvdum@0: {
indvdum@0: foreach ($addresses['bcc'] as $which_ary)
indvdum@0: {
indvdum@0: $mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: if (isset($addresses['cc']) && sizeof($addresses['cc']))
indvdum@0: {
indvdum@0: foreach ($addresses['cc'] as $which_ary)
indvdum@0: {
indvdum@0: $mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
indvdum@0: $mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: $smtp = new smtp_class();
indvdum@0:
indvdum@0: $errno = 0;
indvdum@0: $errstr = '';
indvdum@0:
indvdum@0: $smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
indvdum@0:
indvdum@0: // Ok we have error checked as much as we can to this point let's get on it already.
indvdum@0: if (!class_exists('phpbb_error_collector'))
indvdum@0: {
indvdum@0: global $phpbb_root_path, $phpEx;
indvdum@0: include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
indvdum@0: }
indvdum@0: $collector = new phpbb_error_collector;
indvdum@0: $collector->install();
indvdum@0: $smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
indvdum@0: $collector->uninstall();
indvdum@0: $error_contents = $collector->format_errors();
indvdum@0:
indvdum@0: if (!$smtp->socket)
indvdum@0: {
indvdum@0: if ($errstr)
indvdum@0: {
indvdum@0: $errstr = utf8_convert_message($errstr);
indvdum@0: }
indvdum@0:
indvdum@0: $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: $err_msg .= ($error_contents) ? '
' . htmlspecialchars($error_contents) : '';
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Wait for reply
indvdum@0: if ($err_msg = $smtp->server_parse('220', __LINE__))
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Let me in. This function handles the complete authentication process
indvdum@0: if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // From this point onward most server response codes should be 250
indvdum@0: // Specify who the mail is from....
indvdum@0: $smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
indvdum@0: if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Specify each user to send to and build to header.
indvdum@0: $to_header = implode(', ', $mail_to);
indvdum@0: $cc_header = implode(', ', $mail_cc);
indvdum@0:
indvdum@0: // Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
indvdum@0: $rcpt = false;
indvdum@0: foreach ($mail_rcpt as $type => $mail_to_addresses)
indvdum@0: {
indvdum@0: foreach ($mail_to_addresses as $mail_to_address)
indvdum@0: {
indvdum@0: // Add an additional bit of error checking to the To field.
indvdum@0: if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
indvdum@0: {
indvdum@0: $smtp->server_send("RCPT TO:$mail_to_address");
indvdum@0: if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0: {
indvdum@0: // We continue... if users are not resolved we do not care
indvdum@0: if ($smtp->numeric_response_code != 550)
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0: }
indvdum@0: else
indvdum@0: {
indvdum@0: $rcpt = true;
indvdum@0: }
indvdum@0: }
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: // 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: if (!$rcpt)
indvdum@0: {
indvdum@0: $user->session_begin();
indvdum@0: $err_msg .= '
';
indvdum@0: $err_msg .= (isset($user->lang['INVALID_EMAIL_LOG'])) ? sprintf($user->lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address)) : '' . htmlspecialchars($mail_to_address) . ' possibly an invalid email address?';
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Ok now we tell the server we are ready to start sending data
indvdum@0: $smtp->server_send('DATA');
indvdum@0:
indvdum@0: // This is the last response code we look for until the end of the message.
indvdum@0: if ($err_msg = $smtp->server_parse('354', __LINE__))
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Send the Subject Line...
indvdum@0: $smtp->server_send("Subject: $subject");
indvdum@0:
indvdum@0: // Now the To Header.
indvdum@0: $to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
indvdum@0: $smtp->server_send("To: $to_header");
indvdum@0:
indvdum@0: // Now the CC Header.
indvdum@0: if ($cc_header != '')
indvdum@0: {
indvdum@0: $smtp->server_send("CC: $cc_header");
indvdum@0: }
indvdum@0:
indvdum@0: // Now any custom headers....
indvdum@0: if ($headers !== false)
indvdum@0: {
indvdum@0: $smtp->server_send("$headers\r\n");
indvdum@0: }
indvdum@0:
indvdum@0: // Ok now we are ready for the message...
indvdum@0: $smtp->server_send($message);
indvdum@0:
indvdum@0: // Ok the all the ingredients are mixed in let's cook this puppy...
indvdum@0: $smtp->server_send('.');
indvdum@0: if ($err_msg = $smtp->server_parse('250', __LINE__))
indvdum@0: {
indvdum@0: $smtp->close_session($err_msg);
indvdum@0: return false;
indvdum@0: }
indvdum@0:
indvdum@0: // Now tell the server we are done and close the socket...
indvdum@0: $smtp->server_send('QUIT');
indvdum@0: $smtp->close_session($err_msg);
indvdum@0:
indvdum@0: return true;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * SMTP Class
indvdum@0: * Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
indvdum@0: * See docs/AUTHORS for more details
indvdum@0: * @package phpBB3
indvdum@0: */
indvdum@0: class smtp_class
indvdum@0: {
indvdum@0: var $server_response = '';
indvdum@0: var $socket = 0;
indvdum@0: var $responses = array();
indvdum@0: var $commands = array();
indvdum@0: var $numeric_response_code = 0;
indvdum@0:
indvdum@0: var $backtrace = false;
indvdum@0: var $backtrace_log = array();
indvdum@0:
indvdum@0: function smtp_class()
indvdum@0: {
indvdum@0: // Always create a backtrace for admins to identify SMTP problems
indvdum@0: $this->backtrace = true;
indvdum@0: $this->backtrace_log = array();
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Add backtrace message for debugging
indvdum@0: */
indvdum@0: function add_backtrace($message)
indvdum@0: {
indvdum@0: if ($this->backtrace)
indvdum@0: {
indvdum@0: $this->backtrace_log[] = utf8_htmlspecialchars($message);
indvdum@0: }
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Send command to smtp server
indvdum@0: */
indvdum@0: function server_send($command, $private_info = false)
indvdum@0: {
indvdum@0: fputs($this->socket, $command . "\r\n");
indvdum@0:
indvdum@0: (!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
indvdum@0:
indvdum@0: // We could put additional code here
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * We use the line to give the support people an indication at which command the error occurred
indvdum@0: */
indvdum@0: function server_parse($response, $line)
indvdum@0: {
indvdum@0: global $user;
indvdum@0:
indvdum@0: $this->server_response = '';
indvdum@0: $this->responses = array();
indvdum@0: $this->numeric_response_code = 0;
indvdum@0:
indvdum@0: while (substr($this->server_response, 3, 1) != ' ')
indvdum@0: {
indvdum@0: if (!($this->server_response = fgets($this->socket, 256)))
indvdum@0: {
indvdum@0: return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
indvdum@0: }
indvdum@0: $this->responses[] = substr(rtrim($this->server_response), 4);
indvdum@0: $this->numeric_response_code = (int) substr($this->server_response, 0, 3);
indvdum@0:
indvdum@0: $this->add_backtrace("LINE: $line <- {$this->server_response}");
indvdum@0: }
indvdum@0:
indvdum@0: if (!(substr($this->server_response, 0, 3) == $response))
indvdum@0: {
indvdum@0: $this->numeric_response_code = (int) substr($this->server_response, 0, 3);
indvdum@0: return (isset($user->lang['EMAIL_SMTP_ERROR_RESPONSE'])) ? sprintf($user->lang['EMAIL_SMTP_ERROR_RESPONSE'], $line, $this->server_response) : "Ran into problems sending Mail at Line $line. Response: $this->server_response";
indvdum@0: }
indvdum@0:
indvdum@0: return 0;
indvdum@0: }
indvdum@0:
indvdum@0: /**
indvdum@0: * Close session
indvdum@0: */
indvdum@0: function close_session(&$err_msg)
indvdum@0: {
indvdum@0: fclose($this->socket);
indvdum@0:
indvdum@0: if ($this->backtrace)
indvdum@0: {
indvdum@0: $message = '
' . implode('
', $this->backtrace_log) . '