MsgReportReply_Notify_Background   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 193
rs 10
c 0
b 0
f 0
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
F execute() 0 187 17
1
<?php
2
3
/**
4
 * This task handles notifying users when they've commented to a moderation report and
5
 * someone else replies to them.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2019 Simple Machines and individual contributors
12
 * @license http://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC2
15
 */
16
17
/**
18
 * Class MsgReportReply_Notify_Background
19
 */
20
class MsgReportReply_Notify_Background extends SMF_BackgroundTask
21
{
22
	/**
23
     * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
24
	 * @return bool Always returns true.
25
	 */
26
	public function execute()
27
	{
28
		global $smcFunc, $sourcedir, $modSettings, $language, $scripturl;
29
30
		// Let's see. Let us, first of all, establish the list of possible people.
31
		$possible_members = array();
32
		$request = $smcFunc['db_query']('', '
33
			SELECT id_member
34
			FROM {db_prefix}log_comments
35
			WHERE id_notice = {int:report}
36
				AND comment_type = {literal:reportc}
37
				AND id_comment < {int:last_comment}',
38
			array(
39
				'report' => $this->_details['report_id'],
40
				'last_comment' => $this->_details['comment_id'],
41
			)
42
		);
43
		while ($row = $smcFunc['db_fetch_row']($request))
44
			$possible_members[] = $row[0];
45
		$smcFunc['db_free_result']($request);
46
47
		// Presumably, there are some people?
48
		if (!empty($possible_members))
49
		{
50
			$possible_members = array_flip(array_flip($possible_members));
51
			$possible_members = array_diff($possible_members, array($this->_details['sender_id']));
52
		}
53
		if (empty($possible_members))
54
			return true;
55
56
		// We need to know who can moderate this board - and therefore who can see this report.
57
		// First up, people who have moderate_board in the board this topic was in.
58
		require_once($sourcedir . '/Subs-Members.php');
59
		$members = membersAllowedTo('moderate_board', $this->_details['board_id']);
60
61
		// Second, anyone assigned to be a moderator of this board directly.
62
		$request = $smcFunc['db_query']('', '
63
			SELECT id_member
64
			FROM {db_prefix}moderators
65
			WHERE id_board = {int:current_board}',
66
			array(
67
				'current_board' => $this->_details['board_id'],
68
			)
69
		);
70
		while ($row = $smcFunc['db_fetch_assoc']($request))
71
			$members[] = $row['id_member'];
72
		$smcFunc['db_free_result']($request);
73
74
		// Thirdly, anyone assigned to be a moderator of this group as a group->board moderator.
75
		$request = $smcFunc['db_query']('', '
76
			SELECT mem.id_member
77
			FROM {db_prefix}members AS mem, {db_prefix}moderator_groups AS bm
78
			WHERE bm.id_board = {int:current_board}
79
				AND(
80
					mem.id_group = bm.id_group
81
					OR FIND_IN_SET(bm.id_group, mem.additional_groups) != 0
82
				)',
83
			array(
84
				'current_board' => $this->_details['board_id'],
85
			)
86
		);
87
88
		while ($row = $smcFunc['db_fetch_assoc']($request))
89
			$members[] = $row['id_member'];
90
		$smcFunc['db_free_result']($request);
91
92
		// So now we have two lists: the people who replied to a report in the past,
93
		// and all the possible people who could see said report.
94
		$members = array_intersect($possible_members, $members);
95
96
		// Having successfully figured this out, now let's get the preferences of everyone.
97
		require_once($sourcedir . '/Subs-Notify.php');
98
		$prefs = getNotifyPrefs($members, 'msg_report_reply', true);
99
100
		// So now we find out who wants what.
101
		$alert_bits = array(
102
			'alert' => self::RECEIVE_NOTIFY_ALERT,
103
			'email' => self::RECEIVE_NOTIFY_EMAIL,
104
		);
105
		$notifies = array();
106
107
		foreach ($prefs as $member => $pref_option)
108
		{
109
			foreach ($alert_bits as $type => $bitvalue)
110
			{
111
				if ($pref_option['msg_report_reply'] & $bitvalue)
112
					$notifies[$type][] = $member;
113
			}
114
		}
115
116
		// Firstly, anyone who wants alerts.
117
		if (!empty($notifies['alert']))
118
		{
119
			// Alerts are relatively easy.
120
			$insert_rows = array();
121
			foreach ($notifies['alert'] as $member)
122
			{
123
				$insert_rows[] = array(
124
					'alert_time' => $this->_details['time'],
125
					'id_member' => $member,
126
					'id_member_started' => $this->_details['sender_id'],
127
					'member_name' => $this->_details['sender_name'],
128
					'content_type' => 'msg',
129
					'content_id' => $this->_details['msg_id'],
130
					'content_action' => 'report_reply',
131
					'is_read' => 0,
132
					'extra' => $smcFunc['json_encode'](
133
						array(
134
							'report_link' => '?action=moderate;area=reportedposts;sa=details;rid=' . $this->_details['report_id'], // We don't put $scripturl in these!
135
						)
136
					),
137
				);
138
			}
139
140
			$smcFunc['db_insert']('insert',
141
				'{db_prefix}user_alerts',
142
				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
143
					'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
144
					'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
145
				$insert_rows,
146
				array('id_alert')
147
			);
148
149
			// And update the count of alerts for those people.
150
			updateMemberData($notifies['alert'], array('alerts' => '+'));
151
		}
152
153
		// Secondly, anyone who wants emails.
154
		if (!empty($notifies['email']))
155
		{
156
			// Emails are a bit complicated. We have to do language stuff.
157
			require_once($sourcedir . '/Subs-Post.php');
158
			require_once($sourcedir . '/ScheduledTasks.php');
159
			loadEssentialThemeData();
160
161
			// First, get everyone's language and details.
162
			$emails = array();
163
			$request = $smcFunc['db_query']('', '
164
				SELECT id_member, lngfile, email_address
165
				FROM {db_prefix}members
166
				WHERE id_member IN ({array_int:members})',
167
				array(
168
					'members' => $notifies['email'],
169
				)
170
			);
171
			while ($row = $smcFunc['db_fetch_assoc']($request))
172
			{
173
				if (empty($row['lngfile']))
174
					$row['lngfile'] = $language;
175
				$emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
176
			}
177
			$smcFunc['db_free_result']($request);
178
179
			// Second, get some details that might be nice for the report email.
180
			// We don't bother cluttering up the tasks data for this, when it's really no bother to fetch it.
181
			$request = $smcFunc['db_query']('', '
182
				SELECT lr.subject, lr.membername, lr.body
183
				FROM {db_prefix}log_reported AS lr
184
				WHERE id_report = {int:report}',
185
				array(
186
					'report' => $this->_details['report_id'],
187
				)
188
			);
189
			list ($subject, $poster_name, $comment) = $smcFunc['db_fetch_row']($request);
190
			$smcFunc['db_free_result']($request);
191
192
			// Third, iterate through each language, load the relevant templates and set up sending.
193
			foreach ($emails as $this_lang => $recipients)
194
			{
195
				$replacements = array(
196
					'TOPICSUBJECT' => $subject,
197
					'POSTERNAME' => $poster_name,
198
					'COMMENTERNAME' => $this->_details['sender_name'],
199
					'TOPICLINK' => $scripturl . '?topic=' . $this->_details['topic_id'] . '.msg' . $this->_details['msg_id'] . '#msg' . $this->_details['msg_id'],
200
					'REPORTLINK' => $scripturl . '?action=moderate;area=reportedposts;sa=details;rid=' . $this->_details['report_id'],
201
				);
202
203
				$emaildata = loadEmailTemplate('reply_to_moderator', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
204
205
				// And do the actual sending...
206
				foreach ($recipients as $id_member => $email_address)
207
					sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, 'rptrpy' . $this->_details['comment_id'], $emaildata['is_html'], 3);
208
			}
209
		}
210
211
		// And now we're all done.
212
		return true;
213
	}
214
}
215
216
?>