1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-07 17:27:16 +02:00

Merge pull request #6483 from rxu/ticket/17135

[ticket/17135] Introduce Symfony Mailer for emails backend
This commit is contained in:
Marc Alexander
2025-01-08 19:35:19 +01:00
committed by GitHub
72 changed files with 3080 additions and 2823 deletions

View File

@@ -17,6 +17,7 @@ use phpbb\config\config;
use phpbb\console\command\command;
use phpbb\language\language;
use phpbb\log\log_interface;
use phpbb\messenger\method\email;
use phpbb\notification\manager;
use phpbb\user;
use phpbb\user_loader;
@@ -32,6 +33,9 @@ class activate extends command
/** @var config */
protected $config;
/** @var email */
protected $email_method;
/** @var language */
protected $language;
@@ -65,14 +69,16 @@ class activate extends command
* @param config $config
* @param language $language
* @param log_interface $log
* @param email $email_method
* @param manager $notifications
* @param user_loader $user_loader
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __construct(user $user, config $config, language $language, log_interface $log, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext)
public function __construct(user $user, config $config, language $language, log_interface $log, email $email_method, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext)
{
$this->config = $config;
$this->email_method = $email_method;
$this->language = $language;
$this->log = $log;
$this->notifications = $notifications;
@@ -194,20 +200,14 @@ class activate extends command
if ($input->getOption('send-email'))
{
if (!class_exists('messenger'))
{
require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
}
$messenger = new \messenger(false);
$messenger->template('admin_welcome_activated', $user_row['user_lang']);
$messenger->set_addresses($user_row);
$messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars(array(
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT))
);
$messenger->send(NOTIFY_EMAIL);
$this->email_method->set_use_queue(false);
$this->email_method->template('admin_welcome_activated', $user_row['user_lang']);
$this->email_method->set_addresses($user_row);
$this->email_method->anti_abuse_headers($this->config, $this->user);
$this->email_method->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
]);
$this->email_method->send();
}
}
}

View File

@@ -18,6 +18,7 @@ use phpbb\console\command\command;
use phpbb\db\driver\driver_interface;
use phpbb\exception\runtime_exception;
use phpbb\language\language;
use phpbb\messenger\method\email;
use phpbb\passwords\manager;
use phpbb\user;
use Symfony\Component\Console\Command\Command as symfony_command;
@@ -39,6 +40,9 @@ class add extends command
/** @var config */
protected $config;
/** @var email */
protected $email_method;
/** @var language */
protected $language;
@@ -66,14 +70,16 @@ class add extends command
* @param driver_interface $db
* @param config $config
* @param language $language
* @param service_collection $messenger
* @param manager $password_manager
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __construct(user $user, driver_interface $db, config $config, language $language, manager $password_manager, $phpbb_root_path, $php_ext)
public function __construct(user $user, driver_interface $db, config $config, language $language, email $email_method, manager $password_manager, $phpbb_root_path, $php_ext)
{
$this->db = $db;
$this->config = $config;
$this->db = $db;
$this->email_method = $email_method;
$this->language = $language;
$this->password_manager = $password_manager;
$this->phpbb_root_path = $phpbb_root_path;
@@ -307,23 +313,17 @@ class add extends command
$user_actkey = $this->get_activation_key($user_id);
if (!class_exists('messenger'))
{
require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
}
$messenger = new \messenger(false);
$messenger->template($email_template, $this->user->lang_name);
$messenger->to($this->data['email'], $this->data['username']);
$messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars(array(
$this->email_method->set_use_queue(false);
$this->email_method->template($email_template, $this->user->lang_name);
$this->email_method->to($this->data['email'], $this->data['username']);
$this->email_method->anti_abuse_headers($this->config, $this->user);
$this->email_method->assign_vars([
'WELCOME_MSG' => html_entity_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename']), ENT_COMPAT),
'USERNAME' => html_entity_decode($this->data['username'], ENT_COMPAT),
'PASSWORD' => html_entity_decode($this->data['new_password'], ENT_COMPAT),
'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey")
);
$messenger->send(NOTIFY_EMAIL);
'U_ACTIVATE' => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey",
]);
$this->email_method->send();
}
/**

View File

@@ -13,30 +13,34 @@
namespace phpbb\cron\task\core;
use phpbb\config\config;
/**
* Queue cron task. Sends email and jabber messages queued by other scripts.
*/
class queue extends \phpbb\cron\task\base
{
protected $phpbb_root_path;
protected $php_ext;
protected $cache_dir;
/** var config */
protected $config;
/** var \phpbb\messenger\queue */
protected $queue;
/** var string */
protected $queue_cache_file;
/**
* Constructor.
*
* @param string $phpbb_root_path The root path
* @param string $php_ext PHP file extension
* @param \phpbb\config\config $config The config
* @param string $cache_dir phpBB cache directory
* @param config $config The config
* @param string $queue_cache_file The messenger file queue cache filename
* @param \phpbb\messenger\queue $queue The messenger file queue object
*/
public function __construct($phpbb_root_path, $php_ext, \phpbb\config\config $config, $cache_dir)
public function __construct(config $config, \phpbb\messenger\queue $queue, $queue_cache_file)
{
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->config = $config;
$this->cache_dir = $cache_dir;
$this->queue = $queue;
$this->queue_cache_file = $queue_cache_file;
}
/**
@@ -46,12 +50,7 @@ class queue extends \phpbb\cron\task\base
*/
public function run()
{
if (!class_exists('queue'))
{
include($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
}
$queue = new \queue();
$queue->process();
$this->queue->process();
}
/**
@@ -63,7 +62,7 @@ class queue extends \phpbb\cron\task\base
*/
public function is_runnable()
{
return file_exists($this->cache_dir . 'queue.' . $this->php_ext);
return file_exists($this->queue_cache_file);
}
/**

View File

@@ -13,6 +13,8 @@
namespace phpbb\db\migration\data\v310;
use phpbb\messenger\method\messenger_interface;
class notification_options_reconvert extends \phpbb\db\migration\migration
{
public static function depends_on()
@@ -67,12 +69,12 @@ class notification_options_reconvert extends \phpbb\db\migration\migration
// In-board notification
$notification_methods[] = '';
if ($row['user_notify_type'] == NOTIFY_EMAIL || $row['user_notify_type'] == NOTIFY_BOTH)
if ($row['user_notify_type'] == messenger_interface::NOTIFY_EMAIL || $row['user_notify_type'] == messenger_interface::NOTIFY_BOTH)
{
$notification_methods[] = 'email';
}
if ($row['user_notify_type'] == NOTIFY_IM || $row['user_notify_type'] == NOTIFY_BOTH)
if ($row['user_notify_type'] == messenger_interface::NOTIFY_IM || $row['user_notify_type'] == messenger_interface::NOTIFY_BOTH)
{
$notification_methods[] = 'jabber';
}

View File

@@ -0,0 +1,45 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v400;
use phpbb\db\migration\migration;
class remove_smtp_auth_method extends migration
{
public function effectively_installed()
{
return !$this->config->offsetExists('smtp_auth_method');
}
public static function depends_on()
{
return [
'\phpbb\db\migration\data\v400\dev',
];
}
public function update_data()
{
return [
['config.remove', ['smtp_auth_method']],
];
}
public function revert_data()
{
return [
['config.add', ['smtp_auth_method', 'PLAIN']],
];
}
}

View File

@@ -195,7 +195,6 @@ class install extends \phpbb\console\command\command
$iohandler->set_input('smtp_delivery', $config['email']['smtp_delivery']);
$iohandler->set_input('smtp_host', $config['email']['smtp_host']);
$iohandler->set_input('smtp_port', $config['email']['smtp_port']);
$iohandler->set_input('smtp_auth', $config['email']['smtp_auth']);
$iohandler->set_input('smtp_user', $config['email']['smtp_user']);
$iohandler->set_input('smtp_pass', $config['email']['smtp_pass']);
$iohandler->set_input('submit_email', 'submit');

View File

@@ -97,9 +97,6 @@ class installer_configuration implements ConfigurationInterface
->scalarNode('smtp_port')
->defaultValue(null)
->end()
->scalarNode('smtp_auth')
->defaultValue(null)
->end()
->scalarNode('smtp_user')
->defaultValue(null)
->end()

View File

@@ -150,7 +150,6 @@ class add_config_settings extends database_task
'smtp_delivery' => $this->install_config->get('smtp_delivery'),
'smtp_host' => $this->install_config->get('smtp_host'),
'smtp_port' => $this->install_config->get('smtp_port'),
'smtp_auth_method' => $this->install_config->get('smtp_auth'),
'smtp_username' => $this->install_config->get('smtp_user'),
'smtp_password' => $this->install_config->get('smtp_pass'),

View File

@@ -14,62 +14,52 @@
namespace phpbb\install\module\install_finish\task;
use phpbb\config\db;
use phpbb\install\helper\config;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\auth\auth;
use phpbb\log\log_interface;
use phpbb\user;
use phpbb\install\helper\container_factory;
use phpbb\messenger\method\email;
/**
* Logs installation and sends an email to the admin
*/
class notify_user extends \phpbb\install\task_base
{
/**
* @var \phpbb\install\helper\config
*/
/** @var config */
protected $install_config;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
*/
/** @var iohandler_interface */
protected $iohandler;
/**
* @var \phpbb\auth\auth
*/
/** @var auth */
protected $auth;
/**
* @var db
*/
/** @var db */
protected $config;
/**
* @var \phpbb\log\log_interface
*/
/** @var email */
protected $email_method;
/** @var log_interface */
protected $log;
/**
* @var \phpbb\user
*/
protected $user;
/**
* @var string
*/
/** @var string */
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/** @var user */
protected $user;
/**
* Constructor
*
* @param \phpbb\install\helper\container_factory $container
* @param \phpbb\install\helper\config $install_config
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler
* @param string $phpbb_root_path
* @param string $php_ext
* @param container_factory $container
* @param config $install_config
* @param iohandler_interface $iohandler
* @param string $phpbb_root_path
*/
public function __construct(\phpbb\install\helper\container_factory $container, \phpbb\install\helper\config $install_config, \phpbb\install\helper\iohandler\iohandler_interface $iohandler, $phpbb_root_path, $php_ext)
public function __construct(container_factory $container, config $install_config, iohandler_interface $iohandler, $phpbb_root_path)
{
$this->install_config = $install_config;
$this->iohandler = $iohandler;
@@ -77,8 +67,8 @@ class notify_user extends \phpbb\install\task_base
$this->auth = $container->get('auth');
$this->log = $container->get('log');
$this->user = $container->get('user');
$this->email_method = $container->get('messenger.method.email');
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
// We need to reload config for cases when it doesn't have all values
/** @var \phpbb\cache\driver\driver_interface $cache */
@@ -107,17 +97,15 @@ class notify_user extends \phpbb\install\task_base
if ($this->config['email_enable'])
{
include ($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
$messenger = new \messenger(false);
$messenger->template('installed', $this->install_config->get('user_language', 'en'));
$messenger->to($this->config['board_email'], $this->install_config->get('admin_name'));
$messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars(array(
'USERNAME' => html_entity_decode($this->install_config->get('admin_name'), ENT_COMPAT),
'PASSWORD' => html_entity_decode($this->install_config->get('admin_passwd'), ENT_COMPAT))
);
$messenger->send(NOTIFY_EMAIL);
$this->email_method->set_use_queue(false);
$this->email_method->template('installed', $this->install_config->get('user_language', 'en'));
$this->email_method->to($this->config['board_email'], $this->install_config->get('admin_name'));
$this->email_method->anti_abuse_headers($this->config, $this->user);
$this->email_method->assign_vars([
'USERNAME' => html_entity_decode($this->install_config->get('admin_name'), ENT_COMPAT),
'PASSWORD' => html_entity_decode($this->install_config->get('admin_passwd'), ENT_COMPAT),
]);
$this->email_method->send();
}
// Login admin
@@ -141,7 +129,7 @@ class notify_user extends \phpbb\install\task_base
$this->user->ip,
'LOG_INSTALL_INSTALLED',
false,
array($this->config['version'])
[$this->config['version']]
);
// Remove install_lock

View File

@@ -52,12 +52,9 @@ class obtain_email_data extends \phpbb\install\task_base implements \phpbb\insta
$smtp_delivery = $this->io_handler->get_input('smtp_delivery', '');
$smtp_host = $this->io_handler->get_input('smtp_host', '', true);
$smtp_port = $this->io_handler->get_input('smtp_port', '');
$smtp_auth = $this->io_handler->get_input('smtp_auth', '');
$smtp_user = $this->io_handler->get_input('smtp_user', '', true);
$smtp_passwd = $this->io_handler->get_input('smtp_pass', '', true);
$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5', 'POP-BEFORE-SMTP');
// Check if data is sent
if ($this->io_handler->get_input('submit_email', false))
{
@@ -65,22 +62,11 @@ class obtain_email_data extends \phpbb\install\task_base implements \phpbb\insta
$this->install_config->set('smtp_delivery', $smtp_delivery);
$this->install_config->set('smtp_host', $smtp_host);
$this->install_config->set('smtp_port', $smtp_port);
$this->install_config->set('smtp_auth', $smtp_auth);
$this->install_config->set('smtp_user', $smtp_user);
$this->install_config->set('smtp_pass', $smtp_passwd);
}
else
{
$auth_options = array();
foreach ($auth_methods as $method)
{
$auth_options[] = array(
'value' => $method,
'label' => 'SMTP_' . str_replace('-', '_', $method),
'selected' => false,
);
}
$email_form = array(
'email_enable' => array(
'label' => 'ENABLE_EMAIL',
@@ -126,12 +112,6 @@ class obtain_email_data extends \phpbb\install\task_base implements \phpbb\insta
'type' => 'text',
'default' => $smtp_port,
),
'smtp_auth' => array(
'label' => 'SMTP_AUTH_METHOD',
'description' => 'SMTP_AUTH_METHOD_EXPLAIN',
'type' => 'select',
'options' => $auth_options,
),
'smtp_user' => array(
'label' => 'SMTP_USERNAME',
'description' => 'SMTP_USERNAME_EXPLAIN',

View File

@@ -13,6 +13,8 @@
namespace phpbb\message;
use phpbb\messenger\method\messenger_interface;
/**
* Class admin_form
* Displays a message to the user and allows him to send an email
@@ -85,7 +87,7 @@ class admin_form extends form
/**
* {inheritDoc}
*/
public function submit(\messenger $messenger)
public function submit(\phpbb\di\service_collection $messenger)
{
if (!$this->subject)
{
@@ -155,7 +157,7 @@ class admin_form extends form
}
$this->message->set_sender($this->user->ip, $this->sender_name, $this->sender_address, $this->user->lang_name);
$this->message->set_sender_notify_type(NOTIFY_EMAIL);
$this->message->set_sender_notify_type(messenger_interface::NOTIFY_EMAIL);
}
$this->message->set_template('contact_admin');
@@ -165,7 +167,7 @@ class admin_form extends form
$this->user->lang['ADMINISTRATOR'],
$this->config['board_contact'],
$this->config['default_lang'],
NOTIFY_EMAIL
messenger_interface::NOTIFY_EMAIL
);
$this->message->set_template_vars(array(

View File

@@ -129,10 +129,10 @@ abstract class form
/**
* Submit form, generate the email and send it
*
* @param \messenger $messenger
* @param \phpbb\di\service_collection $messenger
* @return void
*/
public function submit(\messenger $messenger)
public function submit(\phpbb\di\service_collection $messenger)
{
if (!check_form_key('memberlist_email'))
{

View File

@@ -13,6 +13,8 @@
namespace phpbb\message;
use phpbb\messenger\method\messenger_interface;
/**
* Class message
* Holds all information for an email and sends it in the end
@@ -46,7 +48,7 @@ class message
/** @var string */
protected $sender_jabber = '';
/** @var int */
protected $sender_notify_type = NOTIFY_EMAIL;
protected $sender_notify_type = messenger_interface::NOTIFY_EMAIL;
/** @var array */
protected $recipients;
@@ -134,14 +136,14 @@ class message
* @param string $recipient_jabber
* @return void
*/
public function add_recipient($recipient_name, $recipient_address, $recipient_lang, $recipient_notify_type = NOTIFY_EMAIL, $recipient_username = '', $recipient_jabber = '')
public function add_recipient($recipient_name, $recipient_address, $recipient_lang, $recipient_notify_type = messenger_interface::NOTIFY_EMAIL, $recipient_username = '', $recipient_jabber = '')
{
$this->recipients[] = array(
'name' => $recipient_name,
'address' => $recipient_address,
'user_email' => $recipient_address,
'lang' => $recipient_lang,
'username' => $recipient_username,
'jabber' => $recipient_jabber,
'user_jabber' => $recipient_jabber,
'notify_type' => $recipient_notify_type,
'to_name' => $recipient_name,
);
@@ -220,10 +222,10 @@ class message
$this->recipients[] = array(
'lang' => $this->sender_lang,
'address' => $this->sender_address,
'user_email' => $this->sender_address,
'name' => $this->sender_name,
'username' => $this->sender_username,
'jabber' => $this->sender_jabber,
'user_jabber' => $this->sender_jabber,
'notify_type' => $this->sender_notify_type,
'to_name' => $this->recipients[0]['to_name'],
);
@@ -232,11 +234,11 @@ class message
/**
* Send the email
*
* @param \messenger $messenger
* @param \phpbb\di\service_collection $messenger
* @param string $contact
* @return void
*/
public function send(\messenger $messenger, $contact)
public function send(\phpbb\di\service_collection $messenger, $contact)
{
if (!count($this->recipients))
{
@@ -245,38 +247,45 @@ class message
foreach ($this->recipients as $recipient)
{
$messenger->template($this->template, $recipient['lang']);
$messenger->replyto($this->sender_address);
$messenger->to($recipient['address'], $recipient['name']);
$messenger->im($recipient['jabber'], $recipient['username']);
$messenger->headers('X-AntiAbuse: Board servername - ' . $this->server_name);
$messenger->headers('X-AntiAbuse: User IP - ' . $this->sender_ip);
if ($this->sender_id)
/** @psalm-suppress InvalidTemplateParam */
$messenger_collection_iterator = $messenger->getIterator();
foreach ($messenger_collection_iterator as $messenger_method)
{
$messenger->headers('X-AntiAbuse: User_id - ' . $this->sender_id);
$messenger_method->set_use_queue(false);
if ($messenger_method->get_id() == $recipient['notify_type'] || $recipient['notify_type'] == $messenger_method::NOTIFY_BOTH)
{
$messenger_method->template($this->template, $recipient['lang']);
$messenger_method->set_addresses($recipient);
$messenger_method->reply_to($this->sender_address);
$messenger_method->header('X-AntiAbuse', 'Board servername - ' . $this->server_name);
$messenger_method->header('X-AntiAbuse', 'User IP - ' . $this->sender_ip);
if ($this->sender_id)
{
$messenger_method->header('X-AntiAbuse', 'User_id - ' . $this->sender_id);
}
if ($this->sender_username)
{
$messenger_method->header('X-AntiAbuse', 'Username - ' . $this->sender_username);
}
$messenger_method->subject(html_entity_decode($this->subject, ENT_COMPAT));
$messenger_method->assign_vars([
'BOARD_CONTACT' => $contact,
'TO_USERNAME' => html_entity_decode($recipient['to_name'], ENT_COMPAT),
'FROM_USERNAME' => html_entity_decode($this->sender_name, ENT_COMPAT),
'MESSAGE' => html_entity_decode($this->body, ENT_COMPAT),
]);
if (count($this->template_vars))
{
$messenger_method->assign_vars($this->template_vars);
}
$messenger_method->send();
}
}
if ($this->sender_username)
{
$messenger->headers('X-AntiAbuse: Username - ' . $this->sender_username);
}
$messenger->subject(html_entity_decode($this->subject, ENT_COMPAT));
$messenger->assign_vars(array(
'BOARD_CONTACT' => $contact,
'TO_USERNAME' => html_entity_decode($recipient['to_name'], ENT_COMPAT),
'FROM_USERNAME' => html_entity_decode($this->sender_name, ENT_COMPAT),
'MESSAGE' => html_entity_decode($this->body, ENT_COMPAT))
);
if (count($this->template_vars))
{
$messenger->assign_vars($this->template_vars);
}
$messenger->send($recipient['notify_type']);
}
}
}

View File

@@ -13,6 +13,8 @@
namespace phpbb\message;
use phpbb\messenger\method\messenger_interface;
/**
* Class topic_form
* Form used to send topics as notification emails
@@ -108,7 +110,7 @@ class topic_form extends form
/**
* {inheritDoc}
*/
public function submit(\messenger $messenger)
public function submit(\phpbb\di\service_collection $messenger)
{
if (!$this->recipient_address || !preg_match('/^' . get_preg_expression('email') . '$/i', $this->recipient_address))
{
@@ -130,9 +132,9 @@ class topic_form extends form
$this->recipient_name,
$this->recipient_address,
$this->recipient_lang,
NOTIFY_EMAIL
messenger_interface::NOTIFY_EMAIL
);
$this->message->set_sender_notify_type(NOTIFY_EMAIL);
$this->message->set_sender_notify_type(messenger_interface::NOTIFY_EMAIL);
parent::submit($messenger);
}

View File

@@ -96,7 +96,7 @@ class user_form extends form
/**
* {inheritDoc}
*/
public function submit(\messenger $messenger)
public function submit(\phpbb\di\service_collection $messenger)
{
if (!$this->subject)
{

View File

@@ -0,0 +1,484 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\messenger\method;
use phpbb\config\config;
use phpbb\di\service_collection;
use phpbb\event\dispatcher;
use phpbb\extension\manager;
use phpbb\language\language;
use phpbb\log\log_interface;
use phpbb\path_helper;
use phpbb\request\request;
use phpbb\messenger\queue;
use phpbb\template\assets_bag;
use phpbb\template\twig\lexer;
use phpbb\user;
/**
* Messenger base class
*/
abstract class base implements messenger_interface
{
/** @var array */
protected $additional_headers = [];
/** @var assets_bag */
protected $assets_bag;
/** @var config */
protected $config;
/** @var dispatcher */
protected $dispatcher;
/** @var manager */
protected $ext_manager;
/** @var language */
protected $language;
/** @var log_interface */
protected $log;
/** @var string */
protected $msg = '';
/** @var queue */
protected $queue;
/** @var path_helper */
protected $path_helper;
/** @var request */
protected $request;
/** @var string */
protected $root_path;
/** @var string */
protected $subject = '';
/** @var \phpbb\template\template */
protected $template;
/** @var string */
protected $template_cache_path;
/** @var service_collection */
protected $twig_extensions_collection;
/** @var lexer */
protected $twig_lexer;
/** @var bool */
protected $use_queue = true;
/** @var user */
protected $user;
/**
* Messenger base class constructor
*
* @param assets_bag $assets_bag
* @param config $config
* @param dispatcher $dispatcher
* @param language $language
* @param queue $queue
* @param path_helper $path_helper
* @param request $request
* @param service_collection $twig_extensions_collection
* @param lexer $twig_lexer
* @param user $user
* @param string $phpbb_root_path
* @param string $template_cache_path
* @param manager $ext_manager
* @param log_interface $log
*/
public function __construct(
assets_bag $assets_bag,
config $config,
dispatcher $dispatcher,
language $language,
queue $queue,
path_helper $path_helper,
request $request,
service_collection $twig_extensions_collection,
lexer $twig_lexer,
user $user,
string $phpbb_root_path,
string $template_cache_path,
?manager $ext_manager = null,
?log_interface $log = null
)
{
$this->assets_bag = $assets_bag;
$this->config = $config;
$this->dispatcher = $dispatcher;
$this->ext_manager = $ext_manager;
$this->language = $language;
$this->log = $log;
$this->queue = $queue;
$this->path_helper = $path_helper;
$this->request = $request;
$this->twig_extensions_collection = $twig_extensions_collection;
$this->twig_lexer = $twig_lexer;
$this->user = $user;
$this->root_path = $phpbb_root_path;
$this->template_cache_path = $template_cache_path;
$this->set_use_queue();
}
/**
* {@inheritdoc}
*/
abstract public function get_id(): int;
/**
* {@inheritdoc}
*/
abstract public function is_enabled(): bool;
/**
* Sets the use of messenger queue flag
*
* @param bool $use_queue Flag indicating if cached queue to be used
*
* @return void
*/
public function set_use_queue(bool $use_queue = true): void
{
$this->use_queue = $use_queue;
}
/**
* Initializes all the data (address, template file, etc) or resets to default
*
* @return void
*/
abstract public function init(): void;
/**
* Set addresses for to/im as available
*
* @param array $user_row User row
*
* @return void
*/
abstract public function set_addresses(array $user_row): void;
/**
* Get messenger method fie queue object name
*
* @return string
*/
abstract public function get_queue_object_name(): string;
/**
* {@inheritdoc}
*/
public function subject(string $subject = ''): void
{
$this->subject = $subject;
}
/**
* {@inheritdoc}
*/
abstract public function send(): bool;
/**
* Send messages from the queue
*
* @param array $queue_data Queue data array
*
* @return void
*/
abstract public function process_queue(array &$queue_data): void;
/**
* Set email template to use
*
* @param string $template_file Email template file name
* @param string $template_lang Email template language
* @param string $template_path Email template path
* @param string $template_dir_prefix Email template directory prefix
*
* @return bool
*/
public function template(string $template_file, string $template_lang = '', string $template_path = '', string $template_dir_prefix = ''): bool
{
$template_dir_prefix = (!$template_dir_prefix || $template_dir_prefix[0] === '/') ? $template_dir_prefix : '/' . $template_dir_prefix;
$this->setup_template();
if (!trim($template_lang))
{
// fall back to board default language if the user's language is
// missing $template_file. If this does not exist either,
// $this->template->set_filenames will do a trigger_error
$template_lang = basename($this->config['default_lang']);
}
$ext_template_paths = [
[
'name' => $template_lang . '_email',
'ext_path' => 'language/' . $template_lang . '/email' . $template_dir_prefix,
],
];
if ($template_path)
{
$template_paths = [
$template_path . $template_dir_prefix,
];
}
else
{
$template_path = (!empty($this->user->lang_path)) ? $this->user->lang_path : $this->root_path . 'language/';
$template_path .= $template_lang . '/email';
$template_paths = [
$template_path . $template_dir_prefix,
];
$board_language = basename($this->config['default_lang']);
// we can only specify default language fallback when the path is not a custom one for which we
// do not know the default language alternative
if ($template_lang !== $board_language)
{
$fallback_template_path = (!empty($this->user->lang_path)) ? $this->user->lang_path : $this->root_path . 'language/';
$fallback_template_path .= $board_language . '/email';
$template_paths[] = $fallback_template_path . $template_dir_prefix;
$ext_template_paths[] = [
'name' => $board_language . '_email',
'ext_path' => 'language/' . $board_language . '/email' . $template_dir_prefix,
];
}
// If everything fails just fall back to en template
if ($template_lang !== 'en' && $board_language !== 'en')
{
$fallback_template_path = (!empty($this->user->lang_path)) ? $this->user->lang_path : $this->root_path . 'language/';
$fallback_template_path .= 'en/email';
$template_paths[] = $fallback_template_path . $template_dir_prefix;
$ext_template_paths[] = [
'name' => 'en_email',
'ext_path' => 'language/en/email' . $template_dir_prefix,
];
}
}
$this->set_template_paths($ext_template_paths, $template_paths);
$this->template->set_filenames([
'body' => $template_file . '.txt',
]);
return true;
}
/**
* Assign variables to email template
*
* @param array $vars Array of VAR => VALUE to assign to email template
*
* @return void
*/
public function assign_vars(array $vars): void
{
$this->setup_template();
$this->template->assign_vars($vars);
}
/**
* Assign block of variables to email template
*
* @param string $blockname Template block name
* @param array $vars Array of VAR => VALUE to assign to email template block
*
* @return void
*/
public function assign_block_vars(string $blockname, array $vars): void
{
$this->setup_template();
$this->template->assign_block_vars($blockname, $vars);
}
/**
* Prepare message before sending out to the recipients
*
* @return void
*/
public function prepare_message(): void
{
// We add some standard variables we always use, no need to specify them always
$this->assign_vars([
'U_BOARD' => generate_board_url(),
'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . html_entity_decode($this->config['board_email_sig'], ENT_COMPAT)),
'SITENAME' => html_entity_decode($this->config['sitename'], ENT_COMPAT),
]);
$subject = $this->subject;
$template = $this->template;
/**
* Event to modify the template before parsing
*
* @event core.modify_notification_template
* @var string subject The message subject
* @var string template The (readonly) template object
* @since 3.2.4-RC1
* @changed 4.0.0-a1 Removed vars: method, break.
*/
$vars = ['subject', 'template'];
extract($this->dispatcher->trigger_event('core.modify_notification_template', compact($vars)));
// Parse message through template
$message = trim($this->template->assign_display('body'));
/**
* Event to modify notification message text after parsing
*
* @event core.modify_notification_message
* @var string message The message text
* @var string subject The message subject
* @since 3.1.11-RC1
* @changed 4.0.0-a1 Removed vars: method, break.
*/
$vars = ['message', 'subject'];
extract($this->dispatcher->trigger_event('core.modify_notification_message', compact($vars)));
$this->subject = $subject;
$this->msg = $message;
unset($subject, $message, $template);
// 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
$this->msg = str_replace("\r\n", "\n", $this->msg);
// We now try and pull a subject from the email body ... if it exists,
// do this here because the subject may contain a variable
$drop_header = '';
$match = [];
if (preg_match('#^(Subject):(.*?)$#m', $this->msg, $match))
{
$this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $this->language->lang('NO_EMAIL_SUBJECT'));
$drop_header .= '[\r\n]*?' . preg_quote($match[0], '#');
}
else
{
$this->subject = (($this->subject != '') ? $this->subject : $this->language->lang('NO_EMAIL_SUBJECT'));
}
if (preg_match('#^(List-Unsubscribe):(.*?)$#m', $this->msg, $match))
{
$drop_header .= '[\r\n]*?' . preg_quote($match[0], '#');
$this->additional_headers[$match[1]] = trim($match[2]);
}
if ($drop_header)
{
$this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
}
}
/**
* {@inheritdoc}
*/
public function error(string $msg): void
{
// Session doesn't exist, create it
if (!isset($this->user->session_id) || $this->user->session_id === '')
{
$this->user->session_begin();
}
$type = strtoupper($this->get_queue_object_name());
$calling_page = html_entity_decode($this->request->server('PHP_SELF'), ENT_COMPAT);
$message = '<strong>' . $type . '</strong><br><em>' . htmlspecialchars($calling_page, ENT_COMPAT) . '</em><br><br>' . $msg . '<br>';
if ($this->log)
{
$this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_ERROR_' . $type, false, [$message]);
}
}
/**
* Save message data to the messenger file queue
*
* @return void
*/
public function save_queue(): void
{
if ($this->use_queue && !empty($this->queue))
{
$this->queue->save();
}
}
/**
* Setup template engine
*
* @return void
*/
protected function setup_template(): void
{
if (isset($this->template) && $this->template instanceof \phpbb\template\template)
{
return;
}
$template_environment = new \phpbb\template\twig\environment(
$this->assets_bag,
$this->config,
new \phpbb\filesystem\filesystem(),
$this->path_helper,
$this->template_cache_path,
$this->ext_manager,
new \phpbb\template\twig\loader(),
$this->dispatcher,
[]
);
$template_environment->setLexer($this->twig_lexer);
$this->template = new \phpbb\template\twig\twig(
$this->path_helper,
$this->config,
new \phpbb\template\context(),
$template_environment,
$this->template_cache_path,
$this->user,
$this->twig_extensions_collection,
$this->ext_manager
);
}
/**
* Set template paths to load
*
* @param string|array $path_name Email template path name
* @param string|array $paths Email template paths
*
* @return void
*/
protected function set_template_paths(string|array $path_name, string|array $paths): void
{
$this->setup_template();
$this->template->set_custom_style($path_name, $paths);
}
}

View File

@@ -0,0 +1,579 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\messenger\method;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email as symfony_email;
use Symfony\Component\Mime\Header\Headers;
/**
* Messenger class
*/
class email extends base
{
/** @var array */
private const PRIORITY_MAP = [
symfony_email::PRIORITY_HIGHEST => 'Highest',
symfony_email::PRIORITY_HIGH => 'High',
symfony_email::PRIORITY_NORMAL => 'Normal',
symfony_email::PRIORITY_LOW => 'Low',
symfony_email::PRIORITY_LOWEST => 'Lowest',
];
/**
* @var string
*
* Symfony Mailer transport DSN
*/
protected $dsn = '';
/** @var symfony_email */
protected $email;
/** @var Address */
protected $from;
/** @var Headers */
protected $headers;
/**
* @var int
*
* Possible values are:
* symfony_email::PRIORITY_HIGHEST
* symfony_email::PRIORITY_HIGH
* symfony_email::PRIORITY_NORMAL
* symfony_email::PRIORITY_LOW
* symfony_email::PRIORITY_LOWEST
*/
protected $mail_priority = symfony_email::PRIORITY_NORMAL;
/** @var \phpbb\messenger\queue */
protected $queue;
/** @var Address */
protected $reply_to;
/** @var \Symfony\Component\Mailer\Transport\AbstractTransport */
protected $transport;
/**
* {@inheritDoc}
*/
public function get_id(): int
{
return self::NOTIFY_EMAIL;
}
/**
* {@inheritDoc}
*/
public function get_queue_object_name(): string
{
return 'email';
}
/**
* {@inheritDoc}
*/
public function is_enabled(): bool
{
return (bool) $this->config['email_enable'];
}
/**
* {@inheritDoc}
*/
public function init(): void
{
$this->email = new symfony_email();
$this->headers = $this->email->getHeaders();
$this->subject = $this->msg = '';
$this->mail_priority = symfony_email::PRIORITY_NORMAL;
$this->additional_headers = [];
$this->use_queue = true;
unset($this->template, $this->reply_to, $this->from);
}
/**
* {@inheritdoc}
*/
public function set_use_queue(bool $use_queue = true): void
{
$this->use_queue = !$this->config['email_package_size'] ? false : $use_queue;
}
/**
* {@inheritDoc}
*/
public function set_addresses(array $user_row): void
{
if (!empty($user_row['user_email']))
{
$this->to($user_row['user_email'], $user_row['username'] ?: '');
}
}
/**
* Sets email address to send to
*
* @param string $address Email "To" recipient address
* @param string $realname Email "To" recipient name
* @return void
*/
public function to(string $address, string $realname = ''): void
{
if (!$address = trim($address))
{
return;
}
// If empty sendmail_path on windows, PHP changes the to line
$windows_empty_sendmail_path = !$this->config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\';
$to = new Address($address, $windows_empty_sendmail_path ? '' : trim($realname));
$this->email->getTo() ? $this->email->addTo($to) : $this->email->to($to);
}
/**
* Sets cc address to send to
*
* @param string $address Email carbon copy recipient address
* @param string $realname Email carbon copy recipient name
* @return void
*/
public function cc(string $address, string $realname = ''): void
{
if (!$address = trim($address))
{
return;
}
$cc = new Address($address, trim($realname));
$this->email->getCc() ? $this->email->addCc($cc) : $this->email->cc($cc);
}
/**
* Sets bcc address to send to
*
* @param string $address Email black carbon copy recipient address
* @param string $realname Email black carbon copy recipient name
* @return void
*/
public function bcc(string $address, string $realname = ''): void
{
if (!$address = trim($address))
{
return;
}
$bcc = new Address($address, trim($realname));
$this->email->getBcc() ? $this->email->addBcc($bcc) : $this->email->bcc($bcc);
}
/**
* Set the reply to address
*
* @param string $address Email "Reply to" address
* @param string $realname Email "Reply to" recipient name
* @return void
*/
public function reply_to(string $address, string $realname = ''): void
{
if (!$address = trim($address))
{
return;
}
$this->reply_to = new Address($address, trim($realname));
$this->email->getReplyTo() ? $this->email->addReplyTo($this->reply_to) : $this->email->replyTo($this->reply_to);
}
/**
* Set the from address
*
* @param string $address Email "from" address
* @param string $realname Email "from" recipient name
* @return void
*/
public function from(string $address, string $realname = ''): void
{
if (!$address = trim($address))
{
return;
}
$this->from = new Address($address, trim($realname));
$this->email->getFrom() ? $this->email->addFrom($this->from) : $this->email->from($this->from);
}
/**
* Set up subject for mail
*
* @param string $subject Email subject
* @return void
*/
public function subject(string $subject = ''): void
{
parent::subject(trim($subject));
$this->email->subject($this->subject);
}
/**
* Adds X-AntiAbuse headers
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\user $user User object
* @return void
*/
public function anti_abuse_headers(\phpbb\config\config $config, \phpbb\user $user): void
{
$this->headers->addHeader('X-AntiAbuse', 'Board servername - ' . $config['server_name']);
$this->headers->addHeader('X-AntiAbuse', 'User_id - ' . $user->data['user_id']);
$this->headers->addHeader('X-AntiAbuse', 'Username - ' . $user->data['username']);
$this->headers->addHeader('X-AntiAbuse', 'User IP - ' . $user->ip);
}
/**
* Set the email priority
*
* Possible values are:
* symfony_email::PRIORITY_HIGHEST = 1
* symfony_email::PRIORITY_HIGH = 2
* symfony_email::PRIORITY_NORMAL = 3
* symfony_email::PRIORITY_LOW = 4
* symfony_email::PRIORITY_LOWEST = 5
*
* @param int $priority Email priority level
* @return void
*/
public function set_mail_priority(int $priority = symfony_email::PRIORITY_NORMAL): void
{
$this->email->priority($priority);
}
/**
* Set email headers
*
* @return void
*/
protected function build_headers(): void
{
$board_contact = trim($this->config['board_contact']);
$contact_name = html_entity_decode($this->config['board_contact_name'], ENT_COMPAT);
if (empty($this->email->getReplyTo()))
{
$this->reply_to($board_contact, $contact_name);
}
if (empty($this->email->getFrom()))
{
$this->from($board_contact, $contact_name);
}
$this->email->priority($this->mail_priority);
$headers = [
'Return-Path' => new Address($this->config['board_email']),
'Sender' => new Address($this->config['board_email']),
'X-MSMail-Priority' => self::PRIORITY_MAP[$this->mail_priority],
'X-Mailer' => 'phpBB',
'X-MimeOLE' => 'phpBB',
'X-phpBB-Origin' => 'phpbb://' . str_replace(['http://', 'https://'], ['', ''], generate_board_url()),
];
// Add additional headers
$headers = array_merge($headers, $this->additional_headers);
/**
* Event to modify email header entries
*
* @event core.modify_email_headers
* @var array headers Array containing email header entries
* @since 3.1.11-RC1
*/
$vars = ['headers'];
extract($this->dispatcher->trigger_event('core.modify_email_headers', compact($vars)));
foreach ($headers as $header => $value)
{
$this->headers->addHeader($header, $value);
}
}
/**
* Generates valid DSN for Symfony Mailer transport
*
* @param string $dsn Symfony Mailer transport DSN
* @return void
*/
public function set_dsn(string $dsn = ''): void
{
if (!empty($dsn))
{
$this->dsn = $dsn;
}
else if ($this->config['smtp_delivery'])
{
if (empty($this->config['smtp_host']))
{
$this->dsn = 'null://null';
}
else
{
$user = urlencode($this->config['smtp_username']);
$password = urlencode($this->config['smtp_password']);
$smtp_host = urlencode($this->config['smtp_host']);
$smtp_port = $this->config['smtp_port'];
$this->dsn = "smtp://$user:$password@$smtp_host:$smtp_port";
}
}
else
{
$this->dsn = 'sendmail://default';
}
}
/**
* Get Symfony Mailer transport DSN
*
* @return string
*/
public function get_dsn(): string
{
return $this->dsn;
}
/**
* Generates a valid transport to send email
*
* @return void
*/
public function set_transport(): void
{
if (empty($this->dsn))
{
$this->set_dsn();
}
$this->transport = Transport::fromDsn($this->dsn);
if ($this->config['smtp_delivery'] && method_exists($this->transport, 'getStream'))
{
// Set ssl context options, see http://php.net/manual/en/context.ssl.php
$options['ssl'] = [
'verify_peer' => (bool) $this->config['smtp_verify_peer'],
'verify_peer_name' => (bool) $this->config['smtp_verify_peer_name'],
'allow_self_signed' => (bool) $this->config['smtp_allow_self_signed'],
];
$this->transport->getStream()->setStreamOptions($options);
}
}
/**
* {@inheritDoc}
*/
public function process_queue(array &$queue_data): void
{
$queue_object_name = $this->get_queue_object_name();
$messages_count = count($queue_data[$queue_object_name]['data']);
if (!$this->is_enabled() || !$messages_count)
{
unset($queue_data[$queue_object_name]);
return;
}
@set_time_limit(0);
$package_size = $queue_data[$queue_object_name]['package_size'] ?? 0;
$num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size;
$mailer = new Mailer($this->transport);
for ($i = 0; $i < $num_items; $i++)
{
// Make variables available...
extract(array_shift($queue_data[$queue_object_name]['data']));
$break = false;
/**
* Event to send message via external transport
*
* @event core.notification_message_process
* @var string break Flag indicating if the function return after hook
* @var string email The Symfony Email object
* @since 3.2.4-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, subject, msg.
*/
$vars = [
'break',
'email',
];
extract($this->dispatcher->trigger_event('core.notification_message_process', compact($vars)));
if (!$break)
{
try
{
$mailer->send($email);
}
catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
{
$this->error($e->getDebug());
continue;
}
}
}
// No more data for this object? Unset it
if (!count($queue_data[$queue_object_name]['data']))
{
unset($queue_data[$queue_object_name]);
}
}
/**
* Get mailer transport object
*
* @return \Symfony\Component\Mailer\Transport\TransportInterface Symfony Mailer transport object
*/
public function get_transport(): \Symfony\Component\Mailer\Transport\TransportInterface
{
return $this->transport;
}
/**
* {@inheritDoc}
*/
public function send(): bool
{
$this->prepare_message();
$this->email->subject($this->subject);
$this->email->text($this->msg);
$break = false;
$subject = $this->subject;
$msg = $this->msg;
$email = $this->email;
/**
* Event to send message via external transport
*
* @event core.notification_message_email
* @var bool break Flag indicating if the function return after hook
* @var string subject The message subject
* @var string msg The message text
* @var string email The Symfony Email object
* @since 3.2.4-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses
*/
$vars = [
'break',
'subject',
'msg',
'email',
];
extract($this->dispatcher->trigger_event('core.notification_message_email', compact($vars)));
$this->email = $email;
$this->build_headers();
if ($break)
{
return true;
}
// Send message ...
if (!$this->use_queue)
{
$mailer = new Mailer($this->transport);
$subject = $this->subject;
$msg = $this->msg;
$headers = $this->headers;
$email = $this->email;
/**
* Modify data before sending out emails with PHP's mail function
*
* @event core.phpbb_mail_before
* @var string email The Symfony Email object
* @var string subject The message subject
* @var string msg The message text
* @var string headers The email headers
* @since 3.3.6-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: to, eol, additional_parameters.
*/
$vars = [
'email',
'subject',
'msg',
'headers',
];
extract($this->dispatcher->trigger_event('core.phpbb_mail_before', compact($vars)));
$this->subject = $subject;
$this->msg = $msg;
$this->headers = $headers;
$this->email = $email;
try
{
$mailer->send($this->email);
}
catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
{
$this->error($e->getDebug());
return false;
}
/**
* Execute code after sending out emails with PHP's mail function
*
* @event core.phpbb_mail_after
* @var string email The Symfony Email object
* @var string subject The message subject
* @var string msg The message text
* @var string headers The email headers
* @since 3.3.6-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: to, eol, additional_parameters, $result.
*/
$vars = [
'email',
'subject',
'msg',
'headers',
];
extract($this->dispatcher->trigger_event('core.phpbb_mail_after', compact($vars)));
}
else
{
$this->queue->init('email', $this->config['email_package_size']);
$this->queue->put('email', [
'email' => $this->email,
]);
}
// Reset the object
$this->init();
return true;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,68 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\messenger\method;
/**
* Messenger method interface class
*/
interface messenger_interface
{
/** @var int Email notify method used */
public const NOTIFY_EMAIL = 0;
/** @var int Instant messaging (Jabber) notify method used */
public const NOTIFY_IM = 1;
/** @var int Both notify methods used */
public const NOTIFY_BOTH = 2;
/**
* Get messenger method id
*
* @return int
*/
public function get_id(): int;
/**
* Check if the messenger method is enabled
*
* @return bool
*/
public function is_enabled(): bool;
/**
* Set up subject for the message
*
* @param string $subject Email subject
*
* @return void
*/
public function subject(string $subject = ''): void;
/**
* Send out messages
*
* @return bool
*/
public function send(): bool;
/**
* Add error message to log
*
* @param string $msg Error message text
*
* @return void
*/
public function error(string $msg): void;
}

View File

@@ -0,0 +1,213 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\messenger;
use phpbb\config\config;
use phpbb\event\dispatcher;
use phpbb\di\service_collection;
use phpbb\filesystem\filesystem;
/**
* Handling messenger file queue
*/
class queue
{
/** @var string */
protected $cache_file;
/** @var config */
protected $config;
/** @var array */
protected $data = [];
/** @var dispatcher */
protected $dispatcher;
/** @var \phpbb\filesystem\filesystem_interface */
protected $filesystem;
/** @var service_collection */
protected $messenger_method_collection;
/** @var array */
protected $queue_data = [];
/**
* Messenger queue constructor.
*
* @param config $config
* @param dispatcher $dispatcher
* @param service_collection $messenger_method_collection
* @param string $cache_file
*/
public function __construct(config $config, dispatcher $dispatcher, service_collection $messenger_method_collection, $cache_file)
{
$this->config = $config;
$this->dispatcher = $dispatcher;
$this->messenger_method_collection = $messenger_method_collection;
$this->filesystem = new filesystem();
$this->cache_file = $cache_file;
}
/**
* Init a queue object
*
* @param string $object Queue object type: email/jabber/etc
* @param int $package_size Size of the messenger package to send
* @return void
*/
public function init(string $object, int $package_size): void
{
$this->data[$object] = [];
$this->data[$object]['package_size'] = $package_size;
$this->data[$object]['data'] = [];
}
/**
* Put message into the messenger file queue
*
* @param string $object Queue object type: email/jabber/etc
* @param array $message_data Message data to send
* @return void
*/
public function put(string $object, array $message_data): void
{
$this->data[$object]['data'][] = $message_data;
}
/**
* Process the messenger file queue (using lock file)
*
* @return void
*/
public function process(): void
{
$lock = new \phpbb\lock\flock($this->cache_file);
$lock->acquire();
// avoid races, check file existence once
$have_cache_file = file_exists($this->cache_file);
if (!$have_cache_file || $this->config['last_queue_run'] > time() - $this->config['queue_interval'])
{
if (!$have_cache_file)
{
$this->config->set('last_queue_run', time(), false);
}
$lock->release();
return;
}
$this->config->set('last_queue_run', time(), false);
include($this->cache_file);
/** @psalm-suppress InvalidTemplateParam */
$messenger_collection_iterator = $this->messenger_method_collection->getIterator();
foreach ($messenger_collection_iterator as $messenger_method)
{
if (isset($this->queue_data[$messenger_method->get_queue_object_name()]))
{
$messenger_method->process_queue($this->queue_data);
}
}
if (!count($this->queue_data))
{
@unlink($this->cache_file);
}
else
{
if ($fp = @fopen($this->cache_file, 'wb'))
{
fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
fclose($fp);
if (function_exists('opcache_invalidate'))
{
@opcache_invalidate($this->cache_file);
}
try
{
$this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE);
}
catch (\phpbb\filesystem\exception\filesystem_exception $e)
{
// Do nothing
}
}
}
$lock->release();
}
/**
* Save message data to the messenger file queue
*
* @return void
*/
public function save(): void
{
if (!count($this->data))
{
return;
}
$lock = new \phpbb\lock\flock($this->cache_file);
$lock->acquire();
if (file_exists($this->cache_file))
{
include($this->cache_file);
foreach ($this->queue_data as $object => $data_ary)
{
if (isset($this->data[$object]) && count($this->data[$object]))
{
$this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
}
else
{
$this->data[$object]['data'] = $data_ary['data'];
}
}
}
if ($fp = @fopen($this->cache_file, 'w'))
{
fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
fclose($fp);
if (function_exists('opcache_invalidate'))
{
@opcache_invalidate($this->cache_file);
}
try
{
$this->filesystem->phpbb_chmod($this->cache_file, \phpbb\filesystem\filesystem_interface::CHMOD_READ | \phpbb\filesystem\filesystem_interface::CHMOD_WRITE);
}
catch (\phpbb\filesystem\exception\filesystem_exception $e)
{
// Do nothing
}
$this->data = [];
}
$lock->release();
}
}

View File

@@ -14,6 +14,12 @@
namespace phpbb\notification\method;
use phpbb\notification\type\type_interface;
use phpbb\user;
use phpbb\user_loader;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\di\service_collection;
use phpbb\messenger\method\messenger_interface;
/**
* Email notification method class
@@ -22,32 +28,45 @@ use phpbb\notification\type\type_interface;
class email extends \phpbb\notification\method\messenger_base
{
/** @var \phpbb\user */
/** @var user */
protected $user;
/** @var \phpbb\config\config */
/** @var config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
/** @var driver_interface */
protected $db;
/** @var string Notification emails table */
protected $notification_emails_table;
/** @var service_collection */
protected $messenger;
/**
* Notification Method email Constructor
*
* @param \phpbb\user_loader $user_loader
* @param \phpbb\user $user
* @param \phpbb\config\config $config
* @param \phpbb\db\driver\driver_interface $db
* @param user_loader $user_loader
* @param user $user
* @param config $config
* @param driver_interface $db
* @param string $phpbb_root_path
* @param string $php_ext
* @param string $notification_emails_table
* @param service_collection $messenger
*/
public function __construct(\phpbb\user_loader $user_loader, \phpbb\user $user, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext, $notification_emails_table)
public function __construct(
user_loader $user_loader,
user $user,
config $config,
driver_interface $db,
$phpbb_root_path,
$php_ext,
$notification_emails_table,
service_collection $messenger
)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);
parent::__construct($messenger, $user_loader, $phpbb_root_path, $php_ext);
$this->user = $user;
$this->config = $config;
@@ -117,7 +136,7 @@ class email extends \phpbb\notification\method\messenger_base
$insert_buffer->flush();
$this->notify_using_messenger(NOTIFY_EMAIL);
$this->notify_using_messenger(messenger_interface::NOTIFY_EMAIL);
}
/**

View File

@@ -14,6 +14,11 @@
namespace phpbb\notification\method;
use phpbb\notification\type\type_interface;
use phpbb\user;
use phpbb\user_loader;
use phpbb\config\config;
use phpbb\di\service_collection;
use phpbb\messenger\method\messenger_interface;
/**
* Jabber notification method class
@@ -22,24 +27,28 @@ use phpbb\notification\type\type_interface;
class jabber extends \phpbb\notification\method\messenger_base
{
/** @var \phpbb\user */
/** @var user */
protected $user;
/** @var \phpbb\config\config */
/** @var config */
protected $config;
/** @var service_collection */
protected $messenger;
/**
* Notification Method jabber Constructor
*
* @param \phpbb\user_loader $user_loader
* @param \phpbb\user $user
* @param \phpbb\config\config $config
* @param user_loader $user_loader
* @param user $user
* @param config $config
* @param string $phpbb_root_path
* @param string $php_ext
* @param service_collection $messenger
*/
public function __construct(\phpbb\user_loader $user_loader, \phpbb\user $user, \phpbb\config\config $config, $phpbb_root_path, $php_ext)
public function __construct(user_loader $user_loader, user $user, config $config, $phpbb_root_path, $php_ext, service_collection $messenger)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);
parent::__construct($messenger, $user_loader, $phpbb_root_path, $php_ext);
$this->user = $user;
$this->config = $config;
@@ -90,6 +99,6 @@ class jabber extends \phpbb\notification\method\messenger_base
return;
}
$this->notify_using_messenger(NOTIFY_IM, 'short/');
$this->notify_using_messenger(messenger_interface::NOTIFY_IM, 'short/');
}
}

View File

@@ -14,6 +14,8 @@
namespace phpbb\notification\method;
use phpbb\notification\type\type_interface;
use phpbb\di\service_collection;
use phpbb\user_loader;
/**
* Abstract notification method handling email and jabber notifications
@@ -21,7 +23,10 @@ use phpbb\notification\type\type_interface;
*/
abstract class messenger_base extends \phpbb\notification\method\base
{
/** @var \phpbb\user_loader */
/** @var service_collection */
protected $messenger;
/** @var user_loader */
protected $user_loader;
/** @var string */
@@ -33,12 +38,14 @@ abstract class messenger_base extends \phpbb\notification\method\base
/**
* Notification Method Board Constructor
*
* @param \phpbb\user_loader $user_loader
* @param service_collection $messenger
* @param user_loader $user_loader
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __construct(\phpbb\user_loader $user_loader, $phpbb_root_path, $php_ext)
public function __construct(service_collection $messenger, user_loader $user_loader, $phpbb_root_path, $php_ext)
{
$this->messenger = $messenger;
$this->user_loader = $user_loader;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
@@ -60,7 +67,7 @@ abstract class messenger_base extends \phpbb\notification\method\base
/**
* Notify using phpBB messenger
*
* @param int $notify_method Notify method for messenger (e.g. NOTIFY_IM)
* @param int $notify_method Notify method for messenger (e.g. \phpbb\messenger\method\messenger_interface::NOTIFY_IM)
* @param string $template_dir_prefix Base directory to prepend to the email template name
*
* @return void
@@ -73,13 +80,13 @@ abstract class messenger_base extends \phpbb\notification\method\base
}
// Load all users we want to notify (we need their email address)
$user_ids = array();
$user_ids = [];
foreach ($this->queue as $notification)
{
$user_ids[] = $notification->user_id;
}
// We do not send emails to banned users
// We do not notify banned users
if (!function_exists('phpbb_get_banned_user_ids'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
@@ -89,14 +96,9 @@ abstract class messenger_base extends \phpbb\notification\method\base
// Load all the users we need
$this->user_loader->load_users(array_diff($user_ids, $banned_users), array(USER_IGNORE));
// Load the messenger
if (!class_exists('messenger'))
{
include($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
}
$messenger = new \messenger();
// Time to go through the queue and send notifications
$messenger_collection_iterator = $this->messenger->getIterator();
// Time to go through the queue and send emails
/** @var type_interface $notification */
foreach ($this->queue as $notification)
{
@@ -112,21 +114,27 @@ abstract class messenger_base extends \phpbb\notification\method\base
continue;
}
$messenger->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix);
foreach ($messenger_collection_iterator as $messenger_method)
{
if ($messenger_method->get_id() == $notify_method || $notify_method == $messenger_method::NOTIFY_BOTH)
{
$messenger_method->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix);
$messenger_method->set_addresses($user);
$messenger_method->assign_vars(array_merge([
'USERNAME' => $user['username'],
'U_NOTIFICATION_SETTINGS' => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options',
], $notification->get_email_template_variables()));
$messenger->set_addresses($user);
$messenger->assign_vars(array_merge(array(
'USERNAME' => $user['username'],
'U_NOTIFICATION_SETTINGS' => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options',
), $notification->get_email_template_variables()));
$messenger->send($notify_method);
$messenger_method->send();
}
}
}
// Save the queue in the messenger class (has to be called or these emails could be lost?)
$messenger->save_queue();
// Save the queue in the messenger method class (has to be called or these messages could be lost)
foreach ($messenger_collection_iterator as $messenger_method)
{
$messenger_method->save_queue();
}
// We're done, empty the queue
$this->empty_queue();

View File

@@ -14,6 +14,8 @@
namespace phpbb\template\twig;
use Twig\Error\RuntimeError;
use Twig\Extension\CoreExtension;
use Twig\Runtime\EscaperRuntime;
class extension extends \Twig\Extension\AbstractExtension
{
@@ -164,7 +166,7 @@ class extension extends \Twig\Extension\AbstractExtension
// We always include the last element (this was the past design)
$end = ($end == -1 || $end === null) ? null : $end + 1;
return twig_slice($env, $item, $start, $end, $preserveKeys);
return CoreExtension::slice($env->getCharset(), $item, $start, $end, $preserveKeys);
}
/**
@@ -213,7 +215,7 @@ class extension extends \Twig\Extension\AbstractExtension
{
$args = func_get_args();
return twig_escape_filter($this->environment, call_user_func_array([$this, 'lang'], $args), 'js');
return $this->environment->getRuntime(EscaperRuntime::class)->escape(call_user_func_array([$this, 'lang'], $args), 'js');
}
/**

View File

@@ -25,6 +25,7 @@ use phpbb\passwords\manager;
use phpbb\request\request;
use phpbb\template\template;
use phpbb\user;
use phpbb\messenger\method\email;
use Symfony\Component\HttpFoundation\Response;
/**
@@ -50,6 +51,9 @@ class reset_password
/** @var log_interface */
protected $log;
/** @var email */
protected $email_method;
/** @var manager */
protected $passwords_manager;
@@ -80,6 +84,7 @@ class reset_password
* @param helper $helper
* @param language $language
* @param log_interface $log
* @param email $email_method
* @param manager $passwords_manager
* @param request $request
* @param template $template
@@ -89,7 +94,7 @@ class reset_password
* @param string $php_ext
*/
public function __construct(config $config, driver_interface $db, dispatcher $dispatcher, helper $helper,
language $language, log_interface $log, manager $passwords_manager,
language $language, log_interface $log, email $email_method, manager $passwords_manager,
request $request, template $template, user $user, string $users_table,
string $root_path, string $php_ext)
{
@@ -99,6 +104,7 @@ class reset_password
$this->helper = $helper;
$this->language = $language;
$this->log = $log;
$this->email_method = $email_method;
$this->passwords_manager = $passwords_manager;
$this->request = $request;
$this->template = $template;
@@ -250,29 +256,18 @@ class reset_password
WHERE user_id = ' . $user_row['user_id'];
$this->db->sql_query($sql);
if (!class_exists('messenger'))
{
include($this->root_path . 'includes/functions_messenger.' . $this->php_ext);
}
/** @var \messenger $messenger */
$messenger = new \messenger(false);
$messenger->template('user_forgot_password', $user_row['user_lang']);
$messenger->set_addresses($user_row);
$messenger->anti_abuse_headers($this->config, $this->user);
$messenger->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
'U_RESET_PASSWORD' => generate_board_url(true) . $this->helper->route('phpbb_ucp_reset_password_controller', [
'u' => $user_row['user_id'],
'token' => $reset_token,
], false)
$this->email_method->set_use_queue(false);
$this->email_method->template('user_forgot_password', $user_row['user_lang']);
$this->email_method->set_addresses($user_row);
$this->email_method->anti_abuse_headers($this->config, $this->user);
$this->email_method->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
'U_RESET_PASSWORD' => generate_board_url(true) . $this->helper->route('phpbb_ucp_reset_password_controller', [
'u' => $user_row['user_id'],
'token' => $reset_token,
], false)
]);
$messenger->send($user_row['user_notify_type']);
$this->email_method->send();
return $this->helper->message($message);
}