mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-31 22:10:45 +02:00
[ticket/17135] Refactor messenger code to services
PHPBB3-17135
This commit is contained in:
@@ -14,12 +14,16 @@
|
||||
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\template;
|
||||
use phpbb\template\twig\lexer;
|
||||
use phpbb\user;
|
||||
|
||||
/**
|
||||
@@ -36,6 +40,9 @@ abstract class base
|
||||
/** @var dispatcher */
|
||||
protected $dispatcher;
|
||||
|
||||
/** @var manager */
|
||||
protected $ext_manager;
|
||||
|
||||
/** @var language */
|
||||
protected $language;
|
||||
|
||||
@@ -48,6 +55,9 @@ abstract class base
|
||||
/** @var queue */
|
||||
protected $queue;
|
||||
|
||||
/** @var path_helper */
|
||||
protected $path_helper;
|
||||
|
||||
/** @var request */
|
||||
protected $request;
|
||||
|
||||
@@ -57,6 +67,15 @@ abstract class base
|
||||
/** @var 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;
|
||||
|
||||
@@ -73,8 +92,26 @@ abstract class base
|
||||
* @param request $request
|
||||
* @param user $user
|
||||
* @param queue $queue
|
||||
* @param path_helper $path_helper
|
||||
* @param manager $ext_manager
|
||||
* @param service_collection $twig_extensions_collection
|
||||
* @param lexer $twig_lexer
|
||||
* @param string $template_cache_path
|
||||
*/
|
||||
function __construct(config $config, dispatcher $dispatcher, language $language, log_interface $log, request $request, user $user, queue $queue)
|
||||
function __construct(
|
||||
config $config,
|
||||
dispatcher $dispatcher,
|
||||
language $language,
|
||||
log_interface $log,
|
||||
request $request,
|
||||
user $user,
|
||||
queue $queue,
|
||||
path_helper $path_helper,
|
||||
manager $ext_manager,
|
||||
service_collection $twig_extensions_collection,
|
||||
lexer $twig_lexer,
|
||||
$template_cache_path
|
||||
)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->dispatcher = $dispatcher;
|
||||
@@ -83,6 +120,11 @@ abstract class base
|
||||
$this->request = $request;
|
||||
$this->user = $user;
|
||||
$this->queue = $queue;
|
||||
$this->path_helper = $path_helper;
|
||||
$this->ext_manager = $ext_manager;
|
||||
$this->twig_extensions_collection = $twig_extensions_collection;
|
||||
$this->twig_lexer = $twig_lexer;
|
||||
$this->template_cache_path = $template_cache_path;
|
||||
|
||||
$this->set_use_queue();
|
||||
}
|
||||
@@ -91,19 +133,7 @@ abstract class base
|
||||
* Get messenger method id
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get_id()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* get messenger method fie queue object name
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_queue_object_name($user)
|
||||
{
|
||||
return '';
|
||||
}
|
||||
abstract public function get_id();
|
||||
|
||||
/**
|
||||
* Sets the use of messenger queue flag
|
||||
@@ -120,13 +150,7 @@ abstract class base
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function reset()
|
||||
{
|
||||
$this->subject = $this->additional_headers = [];
|
||||
$this->msg = '';
|
||||
$this->use_queue = true;
|
||||
unset($this->template);
|
||||
}
|
||||
abstract public function reset();
|
||||
|
||||
/**
|
||||
* Set addresses for to/im as available
|
||||
@@ -134,9 +158,13 @@ abstract class base
|
||||
* @param array $user User row
|
||||
* @return void
|
||||
*/
|
||||
abstract public function set_addresses($user)
|
||||
{
|
||||
}
|
||||
abstract public function set_addresses($user);
|
||||
|
||||
/**
|
||||
* Get messenger method fie queue object name
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_queue_object_name();
|
||||
|
||||
/**
|
||||
* Set up subject for mail
|
||||
@@ -152,8 +180,8 @@ abstract class base
|
||||
/**
|
||||
* Adds antiabuse headers
|
||||
*
|
||||
* @param \phpbb\config\config $config Config object
|
||||
* @param \phpbb\user $user User object
|
||||
* @param config $config Config object
|
||||
* @param user $user User object
|
||||
* @return void
|
||||
*/
|
||||
public function anti_abuse_headers($config, $user)
|
||||
@@ -185,9 +213,7 @@ abstract class base
|
||||
* Send out messages
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function send()
|
||||
{
|
||||
}
|
||||
abstract protected function send();
|
||||
|
||||
/**
|
||||
* Send messages from the queue
|
||||
@@ -195,9 +221,7 @@ abstract class base
|
||||
* @param array $queue_data Queue data array
|
||||
* @return void
|
||||
*/
|
||||
abstract public function process_queue(&$queue_data)
|
||||
{
|
||||
}
|
||||
abstract public function process_queue(&$queue_data);
|
||||
|
||||
/**
|
||||
* Set email template to use
|
||||
@@ -336,8 +360,8 @@ abstract class base
|
||||
* Event to modify the template before parsing
|
||||
*
|
||||
* @event core.modify_notification_template
|
||||
* @var string subject The message subject
|
||||
* @var \phpbb\template\template template The (readonly) template object
|
||||
* @var string subject The message subject
|
||||
* @var template template The (readonly) template object
|
||||
* @since 3.2.4-RC1
|
||||
* @changed 4.0.0-a1 Removed vars: method, break.
|
||||
*/
|
||||
@@ -395,11 +419,10 @@ abstract class base
|
||||
/**
|
||||
* Add error message to log
|
||||
*
|
||||
* @param string $type Error type: EMAIL / etc
|
||||
* @param string $msg Error message text
|
||||
* @return void
|
||||
*/
|
||||
public function error($type, $msg)
|
||||
public function error($msg)
|
||||
{
|
||||
// Session doesn't exist, create it
|
||||
if (!isset($this->user->session_id) || $this->user->session_id === '')
|
||||
@@ -407,6 +430,7 @@ abstract class base
|
||||
$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>';
|
||||
$this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_ERROR_' . $type, false, [$message]);
|
||||
@@ -431,32 +455,32 @@ abstract class base
|
||||
*/
|
||||
protected function setup_template()
|
||||
{
|
||||
if ($this->template instanceof \phpbb\template\template)
|
||||
if (isset($this->template) && $this->template instanceof template)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$template_environment = new \phpbb\template\twig\environment(
|
||||
$this->config,
|
||||
$this->phpbb_container->get('filesystem'),
|
||||
$this->phpbb_container->get('path_helper'),
|
||||
$this->phpbb_container->getParameter('core.template.cache_path'),
|
||||
$this->phpbb_container->get('ext.manager'),
|
||||
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->phpbb_container->get('template.twig.lexer'));
|
||||
$template_environment->setLexer($this->twig_lexer);
|
||||
|
||||
$this->template = new \phpbb\template\twig\twig(
|
||||
$this->phpbb_container->get('path_helper'),
|
||||
$this->path_helper,
|
||||
$this->config,
|
||||
new \phpbb\template\context(),
|
||||
$template_environment,
|
||||
$this->phpbb_container->getParameter('core.template.cache_path'),
|
||||
$this->template_cache_path,
|
||||
$this->user,
|
||||
$this->phpbb_container->get('template.twig.extensions.collection'),
|
||||
$this->phpbb_container->get('ext.manager')
|
||||
$this->twig_extensions_collection,
|
||||
$this->ext_manager
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -22,7 +22,7 @@ use Symfony\Component\Mime\Header\Headers;
|
||||
/**
|
||||
* Messenger class
|
||||
*/
|
||||
class email extends base
|
||||
class phpbb_email extends base
|
||||
{
|
||||
/** @var array */
|
||||
private const PRIORITY_MAP = [
|
||||
@@ -58,7 +58,7 @@ class email extends base
|
||||
/** @var string */
|
||||
protected $from;
|
||||
|
||||
/** @var Symfony\Component\Mime\Header\Headers */
|
||||
/** @var Headers */
|
||||
protected $headers;
|
||||
|
||||
/**
|
||||
@@ -79,13 +79,11 @@ class email extends base
|
||||
/** @var string */
|
||||
protected $replyto = '';
|
||||
|
||||
/** @var Symfony\Component\Mailer\Transport */
|
||||
/** @var Transport */
|
||||
protected $transport;
|
||||
|
||||
/**
|
||||
* Get messenger method id
|
||||
*
|
||||
* @return int
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get_id()
|
||||
{
|
||||
@@ -93,10 +91,9 @@ class email extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* get messenger method fie queue object name
|
||||
* @return string
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
abstract public function get_queue_object_name($user)
|
||||
public function get_queue_object_name()
|
||||
{
|
||||
return 'email';
|
||||
}
|
||||
@@ -111,9 +108,7 @@ class email extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits/resets the data to default
|
||||
*
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
@@ -122,7 +117,10 @@ class email extends base
|
||||
$this->msg = $this->replyto = $this->from = '';
|
||||
$this->mail_priority = Email::PRIORITY_NORMAL;
|
||||
|
||||
parent::reset();
|
||||
$this->subject = $this->additional_headers = [];
|
||||
$this->msg = '';
|
||||
$this->use_queue = true;
|
||||
unset($this->template);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,10 +134,7 @@ class email extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Set address as available
|
||||
*
|
||||
* @param array $user User row
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set_addresses($user)
|
||||
{
|
||||
@@ -251,8 +246,8 @@ class email extends base
|
||||
*/
|
||||
public function header($header_name, $header_value)
|
||||
{
|
||||
$header_name = trim($header_name);
|
||||
$header_value = trim($header_value);
|
||||
$header_name = $header_name;
|
||||
$header_value = $header_value;
|
||||
|
||||
// addMailboxListHeader() requires value to be array
|
||||
if ($this->get_header_method($header_name) == 'addMailboxListHeader')
|
||||
@@ -295,18 +290,6 @@ class email extends base
|
||||
$this->email->priority($priority);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error message to log
|
||||
*
|
||||
* @param string $msg Error message text
|
||||
* @return void
|
||||
*/
|
||||
public function error($msg)
|
||||
{
|
||||
$type = 'EMAIL/' . ($this->config['smtp_delivery']) ? 'SMTP' : 'PHP/mail()';
|
||||
parent::error($type, $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect proper Header class method to add header
|
||||
*
|
||||
@@ -448,15 +431,12 @@ class email extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Send messages from the queue
|
||||
*
|
||||
* @param array $queue_data Queue data array
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function process_queue(&$queue_data)
|
||||
{
|
||||
$queue_object_name = $this->get_queue_object_name();
|
||||
$messages_count = count($queue_data[$queue_object_name]['data'];
|
||||
$messages_count = count($queue_data[$queue_object_name]['data']);
|
||||
|
||||
if (!$this->is_enabled() || !$messages_count)
|
||||
{
|
||||
@@ -480,8 +460,8 @@ class email extends base
|
||||
* Event to send message via external transport
|
||||
*
|
||||
* @event core.notification_message_process
|
||||
* @var bool break Flag indicating if the function return after hook
|
||||
* @var Symfony\Component\Mime\Email email The Symfony Email object
|
||||
* @var bool break Flag indicating if the function return after hook
|
||||
* @var Email email The Symfony Email object
|
||||
* @since 3.2.4-RC1
|
||||
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, subject, msg.
|
||||
*/
|
||||
@@ -515,7 +495,7 @@ class email extends base
|
||||
/**
|
||||
* Get mailer transport object
|
||||
*
|
||||
* @return Symfony\Component\Mailer\Transport Symfony Mailer transport object
|
||||
* @return Transport Symfony Mailer transport object
|
||||
*/
|
||||
public function get_transport()
|
||||
{
|
||||
@@ -527,7 +507,7 @@ class email extends base
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function send()
|
||||
public function send()
|
||||
{
|
||||
$this->prepare_message();
|
||||
|
||||
@@ -544,9 +524,9 @@ class email extends base
|
||||
* Event to send message via external transport
|
||||
*
|
||||
* @event core.notification_message_email
|
||||
* @var string subject The message subject
|
||||
* @var string msg The message text
|
||||
* @var Symfony\Component\Mime\Email email The Symfony Email object
|
||||
* @var string subject The message subject
|
||||
* @var string msg The message text
|
||||
* @var Email email The Symfony Email object
|
||||
* @since 3.2.4-RC1
|
||||
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, break
|
||||
*/
|
||||
@@ -587,10 +567,10 @@ class email extends base
|
||||
* Modify data before sending out emails with PHP's mail function
|
||||
*
|
||||
* @event core.phpbb_mail_before
|
||||
* @var Symfony\Component\Mime\Email email The Symfony Email object
|
||||
* @var string subject The message subject
|
||||
* @var string msg The message text
|
||||
* @var string headers The email headers
|
||||
* @var Email 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.
|
||||
*/
|
||||
@@ -621,10 +601,10 @@ class email extends base
|
||||
* Execute code after sending out emails with PHP's mail function
|
||||
*
|
||||
* @event core.phpbb_mail_after
|
||||
* @var Symfony\Component\Mime\Email email The Symfony Email object
|
||||
* @var string subject The message subject
|
||||
* @var string msg The message text
|
||||
* @var string headers The email headers
|
||||
* @var Email 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.
|
||||
*/
|
@@ -24,7 +24,7 @@ namespace phpbb\messenger\method;
|
||||
* Slightly modified by Acyd Burn (2006)
|
||||
* Refactored to a service (2023)
|
||||
*/
|
||||
class jabber extends base
|
||||
class phpbb_jabber extends base
|
||||
{
|
||||
/** @var string */
|
||||
protected $connect_server;
|
||||
@@ -87,8 +87,8 @@ class jabber extends base
|
||||
* ->password($password)
|
||||
* ->ssl($use_ssl)
|
||||
* ->server($server)
|
||||
* ->port($port)
|
||||
* ->stream_options(
|
||||
* ->port($port)
|
||||
* ->stream_options(
|
||||
* 'verify_peer' => true,
|
||||
* 'verify_peer_name' => true,
|
||||
* 'allow_self_signed' => false,
|
||||
@@ -99,10 +99,10 @@ class jabber extends base
|
||||
public function init()
|
||||
{
|
||||
$this->username($this->config['jab_username'])
|
||||
->password($this->config['jab_password'])
|
||||
->ssl((bool) $this->config['jab_use_ssl'])
|
||||
->server($this->config['jab_host'])
|
||||
->port($this->config['jab_port'])
|
||||
->password($this->config['jab_password'])
|
||||
->ssl((bool) $this->config['jab_use_ssl'])
|
||||
->server($this->config['jab_host'])
|
||||
->port($this->config['jab_port'])
|
||||
->stream_options['ssl'] = [
|
||||
'verify_peer' => $this->config['jab_verify_peer'],
|
||||
'verify_peer_name' => $this->config['jab_verify_peer_name'],
|
||||
@@ -111,9 +111,7 @@ class jabber extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messenger method id
|
||||
*
|
||||
* @return int
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get_id()
|
||||
{
|
||||
@@ -121,10 +119,9 @@ class jabber extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* get messenger method fie queue object name
|
||||
* @return string
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
abstract public function get_queue_object_name($user)
|
||||
public function get_queue_object_name()
|
||||
{
|
||||
return 'jabber';
|
||||
}
|
||||
@@ -369,10 +366,7 @@ class jabber extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Set address as available
|
||||
*
|
||||
* @param array $user User row
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set_addresses($user)
|
||||
{
|
||||
@@ -403,16 +397,17 @@ class jabber extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Inits/resets the data to default
|
||||
*
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->msg = '';
|
||||
$this->to = [];
|
||||
$this->to = [];
|
||||
|
||||
parent::reset();
|
||||
$this->subject = $this->additional_headers = [];
|
||||
$this->msg = '';
|
||||
$this->use_queue = true;
|
||||
unset($this->template);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -426,15 +421,12 @@ class jabber extends base
|
||||
}
|
||||
|
||||
/**
|
||||
* Send messages from the queue
|
||||
*
|
||||
* @param array $queue_data Queue data array
|
||||
* @return void
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function process_queue(&$queue_data)
|
||||
{
|
||||
$queue_object_name = $this->get_queue_object_name();
|
||||
$messages_count = count($queue_data[$queue_object_name]['data'];
|
||||
$messages_count = count($queue_data[$queue_object_name]['data']);
|
||||
|
||||
if (!$this->is_enabled() || !$messages_count)
|
||||
{
|
@@ -121,7 +121,7 @@ class queue
|
||||
while ($messenger_collection_iterator->valid())
|
||||
{
|
||||
$messenger_method = $messenger_collection_iterator->current();
|
||||
if (isset($this->queue_data[$messenger_method->get_queue_object_name()])
|
||||
if (isset($this->queue_data[$messenger_method->get_queue_object_name()]))
|
||||
{
|
||||
$messenger_method->process_queue($this->queue_data);
|
||||
}
|
||||
|
Reference in New Issue
Block a user