Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.90% covered (danger)
12.90%
4 / 31
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
messenger_base
12.90% covered (danger)
12.90%
4 / 31
66.67% covered (warning)
66.67%
2 / 3
107.14
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 notify_using_messenger
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\notification\method;
15
16use phpbb\notification\type\type_interface;
17
18/**
19* Abstract notification method handling email and jabber notifications
20* using the phpBB messenger.
21*/
22abstract class messenger_base extends \phpbb\notification\method\base
23{
24    /** @var \phpbb\user_loader */
25    protected $user_loader;
26
27    /** @var string */
28    protected $phpbb_root_path;
29
30    /** @var string */
31    protected $php_ext;
32
33    /**
34     * Notification Method Board Constructor
35     *
36     * @param \phpbb\user_loader $user_loader
37     * @param string $phpbb_root_path
38     * @param string $php_ext
39     */
40    public function __construct(\phpbb\user_loader $user_loader, $phpbb_root_path, $php_ext)
41    {
42        $this->user_loader = $user_loader;
43        $this->phpbb_root_path = $phpbb_root_path;
44        $this->php_ext = $php_ext;
45    }
46
47    /**
48    * Is this method available for the user?
49    * This is checked on the notifications options
50    *
51    * @param type_interface|null $notification_type    An optional instance of a notification type. This method returns false
52    *                                            only if the type is provided and if it doesn't provide an email template.
53    * @return bool
54    */
55    public function is_available(type_interface $notification_type = null)
56    {
57        return $notification_type === null || $notification_type->get_email_template() !== false;
58    }
59
60    /**
61    * Notify using phpBB messenger
62    *
63    * @param int $notify_method                Notify method for messenger (e.g. NOTIFY_IM)
64    * @param string $template_dir_prefix    Base directory to prepend to the email template name
65    *
66    * @return void
67    */
68    protected function notify_using_messenger($notify_method, $template_dir_prefix = '')
69    {
70        if (empty($this->queue))
71        {
72            return;
73        }
74
75        // Load all users we want to notify (we need their email address)
76        $user_ids = array();
77        foreach ($this->queue as $notification)
78        {
79            $user_ids[] = $notification->user_id;
80        }
81
82        // We do not send emails to banned users
83        if (!function_exists('phpbb_get_banned_user_ids'))
84        {
85            include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
86        }
87        $banned_users = phpbb_get_banned_user_ids($user_ids);
88
89        // Load all the users we need
90        $this->user_loader->load_users(array_diff($user_ids, $banned_users), array(USER_IGNORE));
91
92        // Load the messenger
93        if (!class_exists('messenger'))
94        {
95            include($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
96        }
97        $messenger = new \messenger();
98
99        // Time to go through the queue and send emails
100        /** @var type_interface $notification */
101        foreach ($this->queue as $notification)
102        {
103            if ($notification->get_email_template() === false)
104            {
105                continue;
106            }
107
108            $user = $this->user_loader->get_user($notification->user_id);
109
110            if ($user['user_type'] == USER_INACTIVE && $user['user_inactive_reason'] == INACTIVE_MANUAL)
111            {
112                continue;
113            }
114
115            $messenger->template($notification->get_email_template(), $user['user_lang'], '', $template_dir_prefix);
116
117            $messenger->set_addresses($user);
118
119            $messenger->assign_vars(array_merge(array(
120                'USERNAME'                        => $user['username'],
121
122                'U_NOTIFICATION_SETTINGS'        => generate_board_url() . '/ucp.' . $this->php_ext . '?i=ucp_notifications&mode=notification_options',
123            ), $notification->get_email_template_variables()));
124
125            $messenger->send($notify_method);
126        }
127
128        // Save the queue in the messenger class (has to be called or these emails could be lost?)
129        $messenger->save_queue();
130
131        // We're done, empty the queue
132        $this->empty_queue();
133    }
134}