[ticket/17135] Refactor messenger code to services [ci skip]

PHPBB3-17135
This commit is contained in:
rxu 2023-06-06 12:54:15 +07:00
parent df5b7fd66e
commit 5be1f5d5c9
No known key found for this signature in database
GPG key ID: 955F0567380E586A
20 changed files with 296 additions and 236 deletions

View file

@ -331,6 +331,7 @@ services:
- '@user_loader'
- '%core.root_path%'
- '%core.php_ext%'
- '@messenger.method_collection'
tags:
- { name: console.command }
@ -344,6 +345,7 @@ services:
- '@passwords.manager'
- '%core.root_path%'
- '%core.php_ext%'
- '@messenger.method_collection'
tags:
- { name: console.command }

View file

@ -86,10 +86,9 @@ services:
cron.task.core.queue:
class: phpbb\cron\task\core\queue
arguments:
- '%core.root_path%'
- '%core.php_ext%'
- '@config'
- '%core.cache_dir%'
- '@messenger.queue'
- '%core.messenger_queue_file%'
calls:
- [set_name, [cron.task.core.queue]]
tags:

View file

@ -9,7 +9,7 @@ services:
tags:
- { name: service_collection, tag: messenger.method, class_name_aware: true }
messenger.method_base:
messenger.method.base:
class: phpbb\messenger\method\base
shared: false
arguments:
@ -21,20 +21,24 @@ services:
- '@user'
- '@messenger.queue'
messenger.method_email:
messenger.method.email:
class: phpbb\messenger\method\email
shared: false
parent: messenger.method_base
parent: messenger.method.base
calls:
- [init, []]
- [set_transport, []]
tags:
- { name: messenger.method }
messenger.method_jabber:
messenger.method.jabber:
class: phpbb\messenger\method\jabber
shared: false
parent: messenger.method_base
parent: messenger.method.base
calls:
- [init, []]
tags:
- { name: messenger.method }
messenger.queue:
class: phpbb\messenger\queue

View file

@ -229,6 +229,7 @@ services:
- '%core.root_path%'
- '%core.php_ext%'
- '%tables.notification_emails%'
- '@messenger.method_collection'
tags:
- { name: notification.method }
@ -241,6 +242,7 @@ services:
- '@config'
- '%core.root_path%'
- '%core.php_ext%'
- '@messenger.method_collection'
tags:
- { name: notification.method }

View file

@ -26,6 +26,7 @@ services:
- '%tables.users%'
- '%core.root_path%'
- '%core.php_ext%'
- '@messenger.method_collection'
phpbb.ucp.controller.webpush:
class: phpbb\ucp\controller\webpush

View file

@ -23,8 +23,6 @@ services:
- '@installer.helper.container_factory'
- '@installer.helper.config'
- '@installer.helper.iohandler'
- '%core.root_path%'
- '%core.php_ext%'
tags:
- { name: install_finish, order: 3 }

View file

@ -428,8 +428,6 @@ switch ($mode)
if (check_form_key('memberlist_messaging'))
{
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$subject = sprintf($user->lang['IM_JABBER_SUBJECT'], $user->data['username'], $config['server_name']);
$message = $request->variable('message', '', true);
@ -438,22 +436,22 @@ switch ($mode)
trigger_error('EMPTY_MESSAGE_IM');
}
$messenger = new messenger(false);
$messenger = $phpbb_container->get('messenger.method_collection');
$jabber = $messenger->offsetGet('messenger.method.jabber');
$jabber->set_use_queue(false);
$messenger->template('profile_send_im', $row['user_lang']);
$messenger->subject(html_entity_decode($subject, ENT_COMPAT));
$jabber->template('profile_send_im', $row['user_lang']);
$jabber->subject(html_entity_decode($subject, ENT_COMPAT));
$jabber->set_addresses($row);
$messenger->replyto($user->data['user_email']);
$messenger->set_addresses($row);
$messenger->assign_vars(array(
$jabber->assign_vars([
'BOARD_CONTACT' => phpbb_get_board_contact($config, $phpEx),
'FROM_USERNAME' => html_entity_decode($user->data['username'], ENT_COMPAT),
'TO_USERNAME' => html_entity_decode($row['username'], ENT_COMPAT),
'MESSAGE' => html_entity_decode($message, ENT_COMPAT))
);
'MESSAGE' => html_entity_decode($message, ENT_COMPAT),
]);
$messenger->send(NOTIFY_IM);
$jabber->send();
$s_select = 'S_SENT_JABBER';
}
@ -903,10 +901,7 @@ switch ($mode)
case 'contactadmin':
case 'email':
if (!class_exists('messenger'))
{
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
}
$messenger = $phpbb_container->get('messenger.method_collection');
$user_id = $request->variable('u', 0);
$topic_id = $request->variable('t', 0);
@ -940,7 +935,6 @@ switch ($mode)
if ($request->is_set_post('submit'))
{
$messenger = new messenger(false);
$form->submit($messenger);
}

View file

@ -20,6 +20,7 @@ use phpbb\log\log_interface;
use phpbb\notification\manager;
use phpbb\user;
use phpbb\user_loader;
use phpbb\di\service_collection;
use Symfony\Component\Console\Command\Command as symfony_command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@ -58,6 +59,9 @@ class activate extends command
*/
protected $php_ext;
/** @var service_collection */
protected $messenger;
/**
* Construct method
*
@ -69,8 +73,9 @@ class activate extends command
* @param user_loader $user_loader
* @param string $phpbb_root_path
* @param string $php_ext
* @param service_collection $messenger
*/
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, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext, service_collection $messenger)
{
$this->config = $config;
$this->language = $language;
@ -79,6 +84,7 @@ class activate extends command
$this->user_loader = $user_loader;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->messenger = $messenger;
$this->language->add_lang('acp/users');
parent::__construct($user);
@ -194,20 +200,15 @@ 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);
$email = $this->messenger->offsetGet('messenger.method.email');
$email->set_use_queue(false);
$email->template('admin_welcome_activated', $user_row['user_lang']);
$email->set_addresses($user_row);
$email->anti_abuse_headers($this->config, $this->user);
$email->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
]);
$email->send();
}
}
}

View file

@ -20,6 +20,7 @@ use phpbb\exception\runtime_exception;
use phpbb\language\language;
use phpbb\passwords\manager;
use phpbb\user;
use phpbb\di\service_collection;
use Symfony\Component\Console\Command\Command as symfony_command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
@ -59,6 +60,9 @@ class add extends command
*/
protected $php_ext;
/** @var service_collection */
protected $messenger;
/**
* Construct method
*
@ -69,8 +73,9 @@ class add extends command
* @param manager $password_manager
* @param string $phpbb_root_path
* @param string $php_ext
* @param service_collection $messenger
*/
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, manager $password_manager, $phpbb_root_path, $php_ext, service_collection $messenger)
{
$this->db = $db;
$this->config = $config;
@ -78,6 +83,7 @@ class add extends command
$this->password_manager = $password_manager;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->messenger = $messenger;
$this->language->add_lang('ucp');
parent::__construct($user);
@ -307,24 +313,18 @@ 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(
$email = $this->messenger->offsetGet('messenger.method.email');
$email->set_use_queue(false);
$email->template($email_template, $this->user->lang_name);
$email->to($this->data['email'], $this->data['username']);
$email->anti_abuse_headers($this->config, $this->user);
$email->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",
]);
$email->send();
/**
* Get user activation key

View file

@ -13,30 +13,35 @@
namespace phpbb\cron\task\core;
use \phpbb\config\config;
use \phpbb\messenger\queue;
/**
* 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 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 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, 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 +51,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 +63,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

@ -14,62 +14,48 @@
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\di\service_collection;
/**
* 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 log_interface */
protected $log;
/**
* @var \phpbb\user
*/
/** @var user */
protected $user;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/** @var service_collection */
protected $messenger;
/**
* 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
*/
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)
{
$this->install_config = $install_config;
$this->iohandler = $iohandler;
@ -77,8 +63,7 @@ class notify_user extends \phpbb\install\task_base
$this->auth = $container->get('auth');
$this->log = $container->get('log');
$this->user = $container->get('user');
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->messenger = $container->get('messenger.method_collection');
// We need to reload config for cases when it doesn't have all values
/** @var \phpbb\cache\driver\driver_interface $cache */
@ -107,17 +92,16 @@ 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);
$email = $this->messenger->offsetGet('messenger.method.email');
$email->set_use_queue(false);
$email->template('installed', $this->install_config->get('user_language', 'en'));
$email->to($this->config['board_email'], $this->install_config->get('admin_name'));
$email->anti_abuse_headers($this->config, $this->user);
$email->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),
]);
$email->send();
}
// Login admin
@ -141,7 +125,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

@ -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

@ -138,10 +138,10 @@ class message
{
$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 +220,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 +232,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 +245,46 @@ 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)
$messenger_collection_iterator = $this->messenger->getIterator();
while ($messenger_collection_iterator->valid())
{
$messenger->headers('X-AntiAbuse', 'User_id - ' . $this->sender_id);
$messenger_method = $messenger_collection_iterator->current();
$messenger_method->set_use_queue(false);
if ($messenger_method->get_id() == $recipient['notify_type'] || $recipient['notify_type'] == NOTIFY_BOTH)
{
$messenger_method->template($this->template, $recipient['lang']);
$messenger_method->set_addresses($recipient);
$messenger_method->replyto($this->sender_address);
$messenger_method->headers('X-AntiAbuse', 'Board servername - ' . $this->server_name);
$messenger_method->headers('X-AntiAbuse', 'User IP - ' . $this->sender_ip);
if ($this->sender_id)
{
$messenger_method->headers('X-AntiAbuse', 'User_id - ' . $this->sender_id);
}
if ($this->sender_username)
{
$messenger_method->headers('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();
}
$messenger_collection_iterator->next();
}
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

@ -132,6 +132,38 @@ class base
$this->subject = $subject;
}
/**
* Adds antiabuse headers
*
* @param \phpbb\config\config $config Config object
* @param \phpbb\user $user User object
* @return void
*/
public function anti_abuse_headers($config, $user)
{
}
/**
* Set up extra headers
*
* @param string $header_name Email header name
* @param string $header_value Email header body
* @return void
*/
public function header($header_name, $header_value)
{
}
/**
* Set the reply to address
*
* @param string $address Email "Reply to" address
* @return void
*/
public function replyto($address)
{
}
/**
* Set email template to use
*
@ -232,7 +264,6 @@ class base
public function assign_vars($vars)
{
$this->setup_template();
$this->template->assign_vars($vars);
}
@ -352,13 +383,15 @@ class base
*/
public function save_queue()
{
$this->queue->save();
return;
if ($this->use_queue && !empty($this->queue))
{
$this->queue->save();
return;
}
}
/**
* Setup template engine
*
* @return void
*/
protected function setup_template()
@ -402,7 +435,6 @@ class base
protected function set_template_paths($path_name, $paths)
{
$this->setup_template();
$this->template->set_custom_style($path_name, $paths);
}
}

View file

@ -82,6 +82,16 @@ class email extends base
/** @var Symfony\Component\Mailer\Transport */
protected $transport;
/**
* Get messenger method id
*
* @return int
*/
public function get_id()
{
return NOTIFY_EMAIL;
}
/**
* Check if the messenger method is enabled
* @return void
@ -286,19 +296,6 @@ class email extends base
parent::error($type, $msg);
}
/**
* Save message data to the messenger file queue
* @return void
*/
public function save_queue()
{
if ($this->config['email_package_size'] && $this->use_queue && !empty($this->queue))
{
$this->queue->save();
return;
}
}
/**
* Detect proper Header class method to add header
*

View file

@ -110,6 +110,16 @@ class jabber extends base
];
}
/**
* Get messenger method id
*
* @return int
*/
public function get_id()
{
return NOTIFY_IM;
}
/**
* Check if the messenger method is enabled
* @return void

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\db\driver\driver_interface;
use phpbb\di\service_collection;
/**
* Email notification method class
@ -34,20 +39,33 @@ class email extends \phpbb\notification\method\messenger_base
/** @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;

View file

@ -14,6 +14,10 @@
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;
/**
* Jabber notification method class
@ -28,18 +32,22 @@ class jabber extends \phpbb\notification\method\messenger_base
/** @var \phpbb\config\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;

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;
@ -73,7 +80,7 @@ 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;
@ -89,13 +96,6 @@ 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 emails
/** @var type_interface $notification */
foreach ($this->queue as $notification)
@ -112,22 +112,28 @@ abstract class messenger_base extends \phpbb\notification\method\base
continue;
}
$messenger->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix);
$messenger_collection_iterator = $this->messenger->getIterator();
while ($messenger_collection_iterator->valid())
{
$messenger_method = $messenger_collection_iterator->current();
if ($messenger_method->get_id() == $notify_method || $notify_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_method->send();
$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);
// Save the queue in the messenger method class (has to be called or these messages could be lost)
$messenger_method->save_queue();
}
$messenger_collection_iterator->next();
}
}
// Save the queue in the messenger class (has to be called or these emails could be lost?)
$messenger->save_queue();
// We're done, empty the queue
$this->empty_queue();
}

View file

@ -25,6 +25,7 @@ use phpbb\passwords\manager;
use phpbb\request\request;
use phpbb\template\template;
use phpbb\user;
use phpbb\di\service_collection;
use Symfony\Component\HttpFoundation\Response;
/**
@ -71,6 +72,9 @@ class reset_password
/** @var string PHP extension */
protected $php_ext;
/** @var service_collection */
protected $messenger;
/**
* Reset password controller constructor.
*
@ -87,11 +91,12 @@ class reset_password
* @param string $users_table
* @param string $root_path
* @param string $php_ext
* @param service_collection $messenger
*/
public function __construct(config $config, driver_interface $db, dispatcher $dispatcher, helper $helper,
language $language, log_interface $log, manager $passwords_manager,
request $request, template $template, user $user, string $users_table,
string $root_path, string $php_ext)
string $root_path, string $php_ext, service_collection $messenger)
{
$this->config = $config;
$this->db = $db;
@ -106,6 +111,7 @@ class reset_password
$this->users_table = $users_table;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->messenger = $messenger;
}
/**
@ -250,29 +256,19 @@ 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)
$email = $this->messenger->offsetGet('messenger.method.email');
$email->set_use_queue(false);
$email->template('user_forgot_password', $user_row['user_lang']);
$email->set_addresses($user_row);
$email->anti_abuse_headers($this->config, $this->user);
$email->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']);
$email->send();
return $this->helper->message($message);
}