1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-16 21:58:17 +01:00

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

PHPBB3-17135
This commit is contained in:
rxu 2023-06-08 15:23:09 +07:00
parent 5873c72ca5
commit f95816cbe3
No known key found for this signature in database
GPG Key ID: 955F0567380E586A
12 changed files with 137 additions and 167 deletions

View File

@ -33,7 +33,6 @@ require_once $phpbb_root_path . 'includes/functions_compress.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_content.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_content.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_display.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_display.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_mcp.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_mcp.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_messenger.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_module.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_module.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_posting.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_posting.' . $phpEx;
require_once $phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx; require_once $phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx;

View File

@ -720,17 +720,17 @@ class acp_board
{ {
if ($config['email_enable']) if ($config['email_enable'])
{ {
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $messenger = $phpbb_container->get('messenger.method_collection');
$email = $messenger->offsetGet('messenger.method.email');
$messenger = new messenger(false); $email->set_use_queue(false);
$messenger->template('test'); $email->template('test');
$messenger->set_addresses($user->data); $email->set_addresses($user->data);
$messenger->anti_abuse_headers($config, $user); $email->anti_abuse_headers($config, $user);
$messenger->assign_vars(array( $email->assign_vars([
'USERNAME' => html_entity_decode($user->data['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($user->data['username'], ENT_COMPAT),
'MESSAGE' => html_entity_decode($request->variable('send_test_email_text', '', true), ENT_COMPAT), 'MESSAGE' => html_entity_decode($request->variable('send_test_email_text', '', true), ENT_COMPAT),
)); ]);
$messenger->send(NOTIFY_EMAIL); $email->send();
trigger_error($user->lang('TEST_EMAIL_SENT') . adm_back_link($this->u_action)); trigger_error($user->lang('TEST_EMAIL_SENT') . adm_back_link($this->u_action));
} }

View File

@ -182,25 +182,13 @@ class acp_email
} }
} }
// Send the messages
if (!class_exists('messenger'))
{
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
}
if (!function_exists('get_group_name'))
{
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
}
$messenger = new messenger($use_queue);
$errored = false; $errored = false;
$email_template = 'admin_send_email'; $email_template = 'admin_send_email';
$template_data = array( $template_data = [
'CONTACT_EMAIL' => phpbb_get_board_contact($config, $phpEx), 'CONTACT_EMAIL' => phpbb_get_board_contact($config, $phpEx),
'MESSAGE' => html_entity_decode($message, ENT_COMPAT), 'MESSAGE' => html_entity_decode($message, ENT_COMPAT),
); ];
$generate_log_entry = true; $generate_log_entry = true;
/** /**
@ -229,31 +217,50 @@ class acp_email
); );
extract($phpbb_dispatcher->trigger_event('core.acp_email_send_before', compact($vars))); extract($phpbb_dispatcher->trigger_event('core.acp_email_send_before', compact($vars)));
$messenger = $phpbb_container->get('messenger.method_collection');
$messenger_collection_iterator = $messenger->getIterator();
for ($i = 0, $size = count($email_list); $i < $size; $i++) for ($i = 0, $size = count($email_list); $i < $size; $i++)
{ {
$used_lang = $email_list[$i][0]['lang']; $used_lang = $email_list[$i][0]['lang'];
$used_method = $email_list[$i][0]['method']; $used_method = $email_list[$i][0]['method'];
while ($messenger_collection_iterator->valid())
{
$messenger_method = $messenger_collection_iterator->current();
if ($messenger_method->get_id() == $used_method || $used_method == NOTIFY_BOTH)
{
$messenger_method->set_use_queue($use_queue);
$messenger_method->template($email_template, $used_lang);
$messenger_method->subject(html_entity_decode($subject, ENT_COMPAT));
$messenger_method->assign_vars($template_data);
if ($messenger_method->get_id() == NOTIFY_EMAIL)
{
for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++) for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++)
{ {
$email_row = $email_list[$i][$j]; $email_row = $email_list[$i][$j];
$messenger_method->{((count($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
$messenger->{((count($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
$messenger->im($email_row['jabber'], $email_row['name']);
} }
$messenger->template($email_template, $used_lang); $messenger_method->anti_abuse_headers($config, $user);
$messenger_method->set_mail_priority($priority);
$messenger->anti_abuse_headers($config, $user); }
else if ($messenger_method->get_id() == NOTIFY_JABBER)
$messenger->subject(html_entity_decode($subject, ENT_COMPAT));
$messenger->set_mail_priority($priority);
$messenger->assign_vars($template_data);
if (!($messenger->send($used_method)))
{ {
$errored = true; for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++)
{
$email_row = $email_list[$i][$j];
$messenger_method->to($email_row['jabber'], $email_row['name']);
}
}
$errored = !$messenger_method->send();
if ($use_queue)
{
$messenger_method->save_queue();
}
}
$messenger_collection_iterator->next();
} }
} }
unset($email_list); unset($email_list);

View File

@ -114,29 +114,20 @@ class acp_inactive
if ($config['require_activation'] == USER_ACTIVATION_ADMIN && !empty($inactive_users)) if ($config['require_activation'] == USER_ACTIVATION_ADMIN && !empty($inactive_users))
{ {
if (!class_exists('messenger')) $messenger = $phpbb_container->get('messenger.method_collection');
{ $email = $messenger->offsetGet('messenger.method.email');
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $email->set_use_queue(false);
}
$messenger = new messenger(false);
foreach ($inactive_users as $row) foreach ($inactive_users as $row)
{ {
$messenger->template('admin_welcome_activated', $row['user_lang']); $email->template('admin_welcome_activated', $row['user_lang']);
$email->set_addresses($row);
$messenger->set_addresses($row); $email->anti_abuse_headers($config, $user);
$email->assign_vars([
$messenger->anti_abuse_headers($config, $user); 'USERNAME' => html_entity_decode($row['username'], ENT_COMPAT),
]);
$messenger->assign_vars(array( $email->send();
'USERNAME' => html_entity_decode($row['username'], ENT_COMPAT))
);
$messenger->send(NOTIFY_EMAIL);
} }
$messenger->save_queue();
} }
if (!empty($inactive_users)) if (!empty($inactive_users))
@ -207,37 +198,37 @@ class acp_inactive
if ($row = $db->sql_fetchrow($result)) if ($row = $db->sql_fetchrow($result))
{ {
// Send the messages // Send the messages
if (!class_exists('messenger'))
{
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
}
$messenger = new messenger();
$usernames = $user_ids = array(); $usernames = $user_ids = array();
$messenger = $phpbb_container->get('messenger.method_collection');
$messenger_collection_iterator = $messenger->getIterator();
do do
{ {
$messenger->template('user_remind_inactive', $row['user_lang']); while ($messenger_collection_iterator->valid())
{
$messenger->set_addresses($row); $messenger_method = $messenger_collection_iterator->current();
if ($messenger_method->get_id() == $user_row['user_notify_type'] || $user_row['user_notify_type'] == NOTIFY_BOTH)
$messenger->anti_abuse_headers($config, $user); {
$messenger_method->template('user_remind_inactive', $row['user_lang']);
$messenger->assign_vars(array( $messenger_method->set_addresses($row);
$messenger_method->anti_abuse_headers($config, $user);
$messenger_method->assign_vars([
'USERNAME' => html_entity_decode($row['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($row['username'], ENT_COMPAT),
'REGISTER_DATE' => $user->format_date($row['user_regdate'], false, true), 'REGISTER_DATE' => $user->format_date($row['user_regdate'], false, true),
'U_ACTIVATE' => generate_board_url() . "/ucp.$phpEx?mode=activate&u=" . $row['user_id'] . '&k=' . $row['user_actkey']) 'U_ACTIVATE' => generate_board_url() . "/ucp.$phpEx?mode=activate&u=" . $row['user_id'] . '&k=' . $row['user_actkey'],
); ]);
$messenger->send($row['user_notify_type']); $messenger_method->send();
$messenger_method->save_queue();
}
$messenger_collection_iterator->next();
}
$usernames[] = $row['username']; $usernames[] = $row['username'];
$user_ids[] = (int) $row['user_id']; $user_ids[] = (int) $row['user_id'];
} }
while ($row = $db->sql_fetchrow($result)); while ($row = $db->sql_fetchrow($result));
$messenger->save_queue();
// Add the remind state to the database and increase activation expiration by one day // Add the remind state to the database and increase activation expiration by one day
$sql = 'UPDATE ' . USERS_TABLE . ' $sql = 'UPDATE ' . USERS_TABLE . '
SET user_reminded = user_reminded + 1, SET user_reminded = user_reminded + 1,

View File

@ -364,11 +364,6 @@ class acp_users
if ($config['email_enable']) if ($config['email_enable'])
{ {
if (!class_exists('messenger'))
{
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
}
$server_url = generate_board_url(); $server_url = generate_board_url();
$user_actkey = gen_rand_string(mt_rand(6, 10)); $user_actkey = gen_rand_string(mt_rand(6, 10));
@ -403,21 +398,18 @@ class acp_users
$db->sql_query($sql); $db->sql_query($sql);
// Start sending email // Start sending email
$messenger = new messenger(false); $messenger = $phpbb_container->get('messenger.method_collection');
$email = $messenger->offsetGet('messenger.method.email');
$messenger->template($email_template, $user_row['user_lang']); $email->set_use_queue(false);
$email->template($email_template, $user_row['user_lang']);
$messenger->set_addresses($user_row); $email->set_addresses($user_row);
$email->anti_abuse_headers($config, $user);
$messenger->anti_abuse_headers($config, $user); $email->assign_vars([
$messenger->assign_vars(array(
'WELCOME_MSG' => html_entity_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename']), ENT_COMPAT), 'WELCOME_MSG' => html_entity_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename']), ENT_COMPAT),
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u={$user_row['user_id']}&k=$user_actkey") 'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u={$user_row['user_id']}&k=$user_actkey",
); ]);
$email->send();
$messenger->send(NOTIFY_EMAIL);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE', false, array($user_row['username'])); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE', false, array($user_row['username']));
$phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE_USER', false, array( $phpbb_log->add('user', $user->data['user_id'], $user->ip, 'LOG_USER_REACTIVATE_USER', false, array(
@ -462,24 +454,16 @@ class acp_users
$phpbb_notifications = $phpbb_container->get('notification_manager'); $phpbb_notifications = $phpbb_container->get('notification_manager');
$phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']); $phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']);
if (!class_exists('messenger')) $messenger = $phpbb_container->get('messenger.method_collection');
{ $email = $messenger->offsetGet('messenger.method.email');
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $email->set_use_queue(false);
} $email->template('admin_welcome_activated', $user_row['user_lang']);
$email->set_addresses($user_row);
$messenger = new messenger(false); $email->anti_abuse_headers($config, $user);
$email->assign_vars([
$messenger->template('admin_welcome_activated', $user_row['user_lang']); 'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
]);
$messenger->set_addresses($user_row); $email->send();
$messenger->anti_abuse_headers($config, $user);
$messenger->assign_vars(array(
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT))
);
$messenger->send(NOTIFY_EMAIL);
} }
} }

View File

@ -51,8 +51,6 @@ class mcp_pm_reports
{ {
case 'close': case 'close':
case 'delete': case 'delete':
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$report_id_list = $request->variable('report_id_list', array(0)); $report_id_list = $request->variable('report_id_list', array(0));
if (!count($report_id_list)) if (!count($report_id_list))

View File

@ -50,8 +50,6 @@ class mcp_queue
{ {
case 'approve': case 'approve':
case 'restore': case 'restore':
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$post_id_list = $request->variable('post_id_list', array(0)); $post_id_list = $request->variable('post_id_list', array(0));
$topic_id_list = $request->variable('topic_id_list', array(0)); $topic_id_list = $request->variable('topic_id_list', array(0));
@ -113,11 +111,6 @@ class mcp_queue
return; return;
} }
if (!class_exists('messenger'))
{
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
}
if (!empty($topic_id_list)) if (!empty($topic_id_list))
{ {
$post_visibility = ($mode == 'deleted_topics') ? ITEM_DELETED : array(ITEM_UNAPPROVED, ITEM_REAPPROVE); $post_visibility = ($mode == 'deleted_topics') ? ITEM_DELETED : array(ITEM_UNAPPROVED, ITEM_REAPPROVE);

View File

@ -49,8 +49,6 @@ class mcp_reports
{ {
case 'close': case 'close':
case 'delete': case 'delete':
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$report_id_list = $request->variable('report_id_list', array(0)); $report_id_list = $request->variable('report_id_list', array(0));
if (!count($report_id_list)) if (!count($report_id_list))

View File

@ -107,7 +107,6 @@ function mcp_topic_view($id, $mode, $action)
} }
include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx); include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
if (!count($post_id_list)) if (!count($post_id_list))
{ {

View File

@ -131,21 +131,25 @@ class ucp_activate
$phpbb_notifications = $phpbb_container->get('notification_manager'); $phpbb_notifications = $phpbb_container->get('notification_manager');
$phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']); $phpbb_notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']);
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $messenger = $phpbb_container->get('messenger.method_collection');
$messenger_collection_iterator = $messenger->getIterator();
while ($messenger_collection_iterator->valid())
{
$messenger_method = $messenger_collection_iterator->current();
if ($messenger_method->get_id() == $user_row['user_notify_type'] || $user_row['user_notify_type'] == NOTIFY_BOTH)
{
$messenger_method->set_use_queue(false);
$messenger_method->template('admin_welcome_activated', $user_row['user_lang']);
$messenger_method->set_addresses($user_row);
$messenger_method->anti_abuse_headers($config, $user);
$messenger_method->assign_vars([
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT),
]);
$messenger = new messenger(false); $messenger_method->send();
}
$messenger->template('admin_welcome_activated', $user_row['user_lang']); $messenger_collection_iterator->next();
}
$messenger->set_addresses($user_row);
$messenger->anti_abuse_headers($config, $user);
$messenger->assign_vars(array(
'USERNAME' => html_entity_decode($user_row['username'], ENT_COMPAT))
);
$messenger->send($user_row['user_notify_type']);
$message = 'ACCOUNT_ACTIVE_ADMIN'; $message = 'ACCOUNT_ACTIVE_ADMIN';
} }

View File

@ -170,27 +170,21 @@ class ucp_profile
{ {
$message = ($config['require_activation'] == USER_ACTIVATION_SELF) ? 'ACCOUNT_EMAIL_CHANGED' : 'ACCOUNT_EMAIL_CHANGED_ADMIN'; $message = ($config['require_activation'] == USER_ACTIVATION_SELF) ? 'ACCOUNT_EMAIL_CHANGED' : 'ACCOUNT_EMAIL_CHANGED_ADMIN';
include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$server_url = generate_board_url(); $server_url = generate_board_url();
$user_actkey = gen_rand_string(mt_rand(6, 10)); $user_actkey = gen_rand_string(mt_rand(6, 10));
$messenger = new messenger(false); $messenger = $phpbb_container->get('messenger.method_collection');
$email = $messenger->offsetGet('messenger.method.email');
$template_file = ($config['require_activation'] == USER_ACTIVATION_ADMIN) ? 'user_activate_inactive' : 'user_activate'; $template_file = ($config['require_activation'] == USER_ACTIVATION_ADMIN) ? 'user_activate_inactive' : 'user_activate';
$messenger->template($template_file, $user->data['user_lang']); $email->template($template_file, $user->data['user_lang']);
$email->to($data['email'], $data['username']);
$messenger->to($data['email'], $data['username']); $email->anti_abuse_headers($config, $user);
$email->assign_vars([
$messenger->anti_abuse_headers($config, $user);
$messenger->assign_vars(array(
'USERNAME' => html_entity_decode($data['username'], ENT_COMPAT), 'USERNAME' => html_entity_decode($data['username'], ENT_COMPAT),
'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u={$user->data['user_id']}&k=$user_actkey") 'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u={$user->data['user_id']}&k=$user_actkey",
); ]);
$email->send();
$messenger->send(NOTIFY_EMAIL);
if ($config['require_activation'] == USER_ACTIVATION_ADMIN) if ($config['require_activation'] == USER_ACTIVATION_ADMIN)
{ {

View File

@ -109,14 +109,17 @@ class phpbb_email_parsing_test extends phpbb_test_case
$phpbb_container->setParameter('core.root_path', $phpbb_root_path); $phpbb_container->setParameter('core.root_path', $phpbb_root_path);
$phpbb_container->setParameter('core.php_ext', $phpEx); $phpbb_container->setParameter('core.php_ext', $phpEx);
if (!class_exists('messenger')) $core_cache_dir = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/';
{ $phpbb_container->setParameter('core.cache_dir', $core_cache_dir);
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); $core_messenger_queue_file = $core_cache_dir . 'queue.' . $phpEx;
} $phpbb_container->setParameter('core.messenger_queue_file', $core_messenger_queue_file);
$messenger_method_collection = new \phpbb\di\service_collection($phpbb_container);
$phpbb_container->set('messenger.method_collection', $messenger_method_collection);
$messenger_queue = new \phpbb\messenger\queue($config, $dispatcher, $messenger_method_collection, $core_messenger_queue_file);
$phpbb_container->set('messenger.queue', $messenger_queue);
$this->email = new \phpbb\messenger\email($config, $dispatcher, $lang, $log, $request, $user, $messenger_queue);
$this->messenger = new \messenger(); $reflection = new ReflectionObject($this->email);
$reflection = new ReflectionObject($this->messenger);
$this->reflection_template_property = $reflection->getProperty('template'); $this->reflection_template_property = $reflection->getProperty('template');
$this->reflection_template_property->setAccessible(true); $this->reflection_template_property->setAccessible(true);
} }
@ -136,9 +139,9 @@ class phpbb_email_parsing_test extends phpbb_test_case
{ {
global $config, $phpEx, $user; global $config, $phpEx, $user;
$this->messenger->set_addresses($user->data); $this->email->set_addresses($user->data);
$this->messenger->assign_vars(array( $this->email->assign_vars(array(
'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . html_entity_decode($config['board_email_sig'], ENT_COMPAT)), 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . html_entity_decode($config['board_email_sig'], ENT_COMPAT)),
'SITENAME' => html_entity_decode($config['sitename'], ENT_COMPAT), 'SITENAME' => html_entity_decode($config['sitename'], ENT_COMPAT),
@ -150,9 +153,9 @@ class phpbb_email_parsing_test extends phpbb_test_case
'U_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?f=1", 'U_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?f=1",
'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?uid=2&f=1&unwatch=forum", 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?uid=2&f=1&unwatch=forum",
)); ));
$this->messenger->template('newtopic_notify', $user->data['user_lang'], '', ''); $this->email->template('newtopic_notify', $user->data['user_lang'], '', '');
$reflection_template = $this->reflection_template_property->getValue($this->messenger); $reflection_template = $this->reflection_template_property->getValue($this->email);
$msg = trim($reflection_template->assign_display('body')); $msg = trim($reflection_template->assign_display('body'));
$this->assertStringContainsString($author_name, $msg); $this->assertStringContainsString($author_name, $msg);