forked from phpbb/phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.php
More file actions
164 lines (142 loc) · 4.9 KB
/
email.php
File metadata and controls
164 lines (142 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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\notification\method;
use phpbb\notification\type\type_interface;
/**
* Email notification method class
* This class handles sending emails for notifications
*/
class email extends \phpbb\notification\method\messenger_base
{
/** @var \phpbb\user */
protected $user;
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var string Notification emails table */
protected $notification_emails_table;
/**
* 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 string $phpbb_root_path
* @param string $php_ext
* @param string $notification_emails_table
*/
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)
{
parent::__construct($user_loader, $phpbb_root_path, $php_ext);
$this->user = $user;
$this->config = $config;
$this->db = $db;
$this->notification_emails_table = $notification_emails_table;
}
/**
* Get notification method name
*
* @return string
*/
public function get_type()
{
return 'notification.method.email';
}
/**
* Is this method available for the user?
* This is checked on the notifications options
*
* @param type_interface|null $notification_type An optional instance of a notification type. If provided, this
* method additionally checks if the type provides an email template.
* @return bool
*/
public function is_available(type_interface $notification_type = null)
{
return parent::is_available($notification_type) && $this->config['email_enable'] && !empty($this->user->data['user_email']);
}
/**
* {@inheritdoc}
*/
public function get_notified_users($notification_type_id, array $options)
{
$notified_users = [];
$sql = 'SELECT user_id
FROM ' . $this->notification_emails_table . '
WHERE notification_type_id = ' . (int) $notification_type_id .
(isset($options['item_id']) ? ' AND item_id = ' . (int) $options['item_id'] : '') .
(isset($options['item_parent_id']) ? ' AND item_parent_id = ' . (int) $options['item_parent_id'] : '') .
(isset($options['user_id']) ? ' AND user_id = ' . (int) $options['user_id'] : '');
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$notified_users[$row['user_id']] = $row;
}
$this->db->sql_freeresult($result);
return $notified_users;
}
/**
* Parse the queue and notify the users
*/
public function notify()
{
$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->notification_emails_table);
/** @var type_interface $notification */
foreach ($this->queue as $notification)
{
$data = self::clean_data($notification->get_insert_array());
$insert_buffer->insert($data);
}
$insert_buffer->flush();
$this->notify_using_messenger(NOTIFY_EMAIL);
}
/**
* {@inheritdoc}
*/
public function mark_notifications($notification_type_id, $item_id, $user_id, $time = false, $mark_read = true)
{
$sql = 'DELETE FROM ' . $this->notification_emails_table . '
WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', is_array($notification_type_id) ? $notification_type_id : [$notification_type_id]) : '1=1') .
($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') .
($item_id !== false ? ' AND ' . $this->db->sql_in_set('item_id', $item_id) : '');
$this->db->sql_query($sql);
}
/**
* {@inheritdoc}
*/
public function mark_notifications_by_parent($notification_type_id, $item_parent_id, $user_id, $time = false, $mark_read = true)
{
$sql = 'DELETE FROM ' . $this->notification_emails_table . '
WHERE ' . ($notification_type_id !== false ? $this->db->sql_in_set('notification_type_id', is_array($notification_type_id) ? $notification_type_id : [$notification_type_id]) : '1=1') .
($user_id !== false ? ' AND ' . $this->db->sql_in_set('user_id', $user_id) : '') .
($item_parent_id !== false ? ' AND ' . $this->db->sql_in_set('item_parent_id', $item_parent_id, false, true) : '');
$this->db->sql_query($sql);
}
/**
* Clean data to contain only what we need for email notifications table
*
* @param array $data Notification data
* @return array Cleaned notification data
*/
public static function clean_data(array $data)
{
$row = [
'notification_type_id' => null,
'item_id' => null,
'item_parent_id' => null,
'user_id' => null,
];
return array_intersect_key($data, $row);
}
}