CreatePost_Notify_Background::updateAlerts()   C
last analyzed

Complexity

Conditions 17
Paths 96

Size

Total Lines 105
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 55
c 0
b 0
f 0
nc 96
nop 1
dl 0
loc 105
rs 5.2166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file contains code used to notify people when a new post is created that
5
 * is relevant to them in some way: new topics in boards they watch, replies to
6
 * topics they watch, posts that mention them, and/or posts that quote them.
7
 *
8
 * Simple Machines Forum (SMF)
9
 *
10
 * @package SMF
11
 * @author Simple Machines https://www.simplemachines.org
12
 * @copyright 2022 Simple Machines and individual contributors
13
 * @license https://www.simplemachines.org/about/smf/license.php BSD
14
 *
15
 * @version 2.1.2
16
 */
17
18
/**
19
 * Class CreatePost_Notify_Background
20
 */
21
class CreatePost_Notify_Background extends SMF_BackgroundTask
22
{
23
	/**
24
	 * Constants for reply types.
25
	 */
26
	const NOTIFY_TYPE_REPLIES_AND_MODERATION = 1;
27
	const NOTIFY_TYPE_REPLIES_AND_OWN_TOPIC_MODERATION = 2;
28
	const NOTIFY_TYPE_ONLY_REPLIES = 3;
29
	const NOTIFY_TYPE_NOTHING = 4;
30
31
	/**
32
	 * Constants for frequencies.
33
	 */
34
	const FREQUENCY_NOTHING = 0;
35
	const FREQUENCY_EVERYTHING = 1;
36
	const FREQUENCY_FIRST_UNREAD_MSG = 2;
37
	const FREQUENCY_DAILY_DIGEST = 3;
38
	const FREQUENCY_WEEKLY_DIGEST = 4;
39
40
	/**
41
	 * Minutes to wait before sending notifications about about mentions
42
	 * and quotes in unwatched and/or edited posts.
43
	 */
44
	const MENTION_DELAY = 5;
45
46
	/**
47
	 * @var array Info about members to be notified.
48
	 */
49
	private $members = array(
50
		// These three contain nested arrays of member info.
51
		'mentioned' => array(),
52
		'quoted' => array(),
53
		'watching' => array(),
54
55
		// These ones just contain member IDs.
56
		'all' => array(),
57
		'emailed' => array(),
58
		'alerted' => array(),
59
		'done' => array(),
60
	);
61
62
	/**
63
	 * @var array Alerts to be inserted into the alerts table.
64
	 */
65
	private $alert_rows = array();
66
67
	/**
68
	 * @var array Members' notification and alert preferences.
69
	 */
70
	private $prefs = array();
71
72
	/**
73
	 * @var int Timestamp after which email notifications should be sent about
74
	 *			mentions and quotes in unwatched and/or edited posts.
75
	 */
76
	private $mention_mail_time = 0;
77
78
	/**
79
	 * This executes the task: loads up the info, puts the email in the queue
80
	 * and inserts any alerts as needed.
81
	 *
82
	 * @return bool Always returns true
83
	 * @throws Exception
84
	 */
85
	public function execute()
86
	{
87
		global $smcFunc, $sourcedir, $scripturl, $language, $modSettings, $user_info, $txt;
88
89
		require_once($sourcedir . '/Subs-Post.php');
90
		require_once($sourcedir . '/Mentions.php');
91
		require_once($sourcedir . '/Subs-Notify.php');
92
		require_once($sourcedir . '/Subs.php');
93
		require_once($sourcedir . '/ScheduledTasks.php');
94
		loadEssentialThemeData();
95
96
		$msgOptions = &$this->_details['msgOptions'];
97
		$topicOptions = &$this->_details['topicOptions'];
98
		$posterOptions = &$this->_details['posterOptions'];
99
		$type = &$this->_details['type'];
100
101
		// Board id is required; if missing, log an error and return
102
		if (!isset($topicOptions['board']))
103
		{
104
			require_once($sourcedir . '/Errors.php');
105
			loadLanguage('Errors');
106
			log_error($txt['missing_board_id'], 'general', __FILE__, __LINE__);
107
			return true;
108
		}
109
110
		// poster_time not always supplied, but used throughout
111
		if (empty($msgOptions['poster_time']))
112
			$msgOptions['poster_time'] = 0;
113
114
		$this->mention_mail_time = $msgOptions['poster_time'] + self::MENTION_DELAY * 60;
115
116
		// We need some more info about the quoted and mentioned members.
117
		if (!empty($msgOptions['quoted_members']))
118
			$this->members['quoted'] = Mentions::getMentionsByContent('quote', $msgOptions['id'], array_keys($msgOptions['quoted_members']));
119
		if (!empty($msgOptions['mentioned_members']))
120
			$this->members['mentioned'] = Mentions::getMentionsByContent('msg', $msgOptions['id'], array_keys($msgOptions['mentioned_members']));
121
122
		// Find the people interested in receiving notifications for this topic
123
		$request = $smcFunc['db_query']('', '
124
			SELECT
125
				ln.id_member, ln.id_board, ln.id_topic, ln.sent,
126
				mem.email_address, mem.lngfile, mem.pm_ignore_list,
127
				mem.id_group, mem.id_post_group, mem.additional_groups,
128
				mem.time_format, mem.time_offset, mem.timezone,
129
				b.member_groups, t.id_member_started, t.id_member_updated
130
			FROM {db_prefix}log_notify AS ln
131
				INNER JOIN {db_prefix}members AS mem ON (ln.id_member = mem.id_member)
132
				LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)
133
				LEFT JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board OR b.id_board = t.id_board)
134
			WHERE ln.id_member != {int:member}
135
				AND (ln.id_topic = {int:topic} OR ln.id_board = {int:board})',
136
			array(
137
				'member' => $posterOptions['id'],
138
				'topic' => $topicOptions['id'],
139
				'board' => $topicOptions['board'],
140
			)
141
		);
142
		while ($row = $smcFunc['db_fetch_assoc']($request))
143
		{
144
			// Skip members who aren't allowed to see this board
145
			$groups = array_merge(array($row['id_group'], $row['id_post_group']), (empty($row['additional_groups']) ? array() : explode(',', $row['additional_groups'])));
146
147
			$allowed_groups = explode(',', $row['member_groups']);
148
149
			if (!in_array(1, $groups) && count(array_intersect($groups, $allowed_groups)) == 0)
150
				continue;
151
			else
152
			{
153
				$row['groups'] = $groups;
154
				unset($row['id_group'], $row['id_post_group'], $row['additional_groups']);
155
			}
156
157
			$this->members['watching'][$row['id_member']] = $row;
158
		}
159
		$smcFunc['db_free_result']($request);
160
161
		// Filter out mentioned and quoted members who can't see this board.
162
		if (!empty($this->members['mentioned']) || !empty($this->members['quoted']))
163
		{
164
			// This won't be set yet if no one is watching this board or topic.
165
			if (!isset($allowed_groups))
166
			{
167
				$request = $smcFunc['db_query']('', '
168
					SELECT member_groups
169
					FROM {db_prefix}boards
170
					WHERE id_board = {int:board}',
171
					array(
172
						'board' => $topicOptions['board'],
173
					)
174
				);
175
				list($allowed_groups) = $smcFunc['db_fetch_row']($request);
176
				$smcFunc['db_free_result']($request);
177
				$allowed_groups = explode(',', $allowed_groups);
178
			}
179
180
			foreach (array('mentioned', 'quoted') as $member_type)
181
			{
182
				foreach ($this->members[$member_type] as $member_id => $member_data)
183
				{
184
					if (!in_array(1, $member_data['groups']) && count(array_intersect($member_data['groups'], $allowed_groups)) == 0)
185
						unset($this->members[$member_type][$member_id], $msgOptions[$member_type . '_members'][$member_id]);
186
				}
187
			}
188
		}
189
190
		$unnotified = array_filter($this->members['watching'], function ($member) {
191
			return empty($member['sent']);
192
		});
193
194
195
		// Modified post, or dealing with delayed mention and quote notifications.
196
		if ($type == 'edit' || !empty($this->_details['respawns']))
197
		{
198
			// Notifications about modified posts only go to members who were mentioned or quoted
199
			$this->members['watching'] = $type == 'edit' ? array(): $unnotified;
200
201
			// If this post has no quotes or mentions, just delete any obsolete alerts and bail out.
202
			if (empty($this->members['quoted']) && empty($this->members['mentioned']))
203
			{
204
				$this->updateAlerts($msgOptions['id']);
205
				return true;
206
			}
207
208
			// Never notify about edits to ancient posts.
209
			if (!empty($modSettings['oldTopicDays']) && time() > $msgOptions['poster_time'] + $modSettings['oldTopicDays'] * 86400)
210
				return true;
211
212
			// If editing is only allowed for a brief time, send after editing becomes disabled.
213
			if (!empty($modSettings['edit_disable_time']) && $modSettings['edit_disable_time'] <= self::MENTION_DELAY)
214
			{
215
				$this->mention_mail_time = $msgOptions['poster_time'] + $modSettings['edit_disable_time'] * 60;
216
			}
217
			// Otherwise, impose a delay before sending notifications about edited posts.
218
			else
219
			{
220
				if (!empty($this->_details['respawns']))
221
				{
222
					$request = $smcFunc['db_query']('', '
223
						SELECT modified_time
224
						FROM {db_prefix}messages
225
						WHERE id_msg = {int:msg}
226
						LIMIT 1',
227
						array(
228
							'msg' => $msgOptions['id'],
229
						)
230
					);
231
					list($real_modified_time) = $smcFunc['db_fetch_row']($request);
232
					$smcFunc['db_free_result']($request);
233
234
					// If it was modified again while we weren't looking, bail out.
235
					// A future instance of this task will take care of it instead.
236
					if ((!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) < $real_modified_time)
237
						return true;
238
				}
239
240
				$this->mention_mail_time = (!empty($msgOptions['modify_time']) ? $msgOptions['modify_time'] : $msgOptions['poster_time']) + self::MENTION_DELAY * 60;
241
			}
242
		}
243
244
		$this->members['all'] = array_unique(array_merge(array_keys($this->members['watching']), array_keys($this->members['quoted']), array_keys($this->members['mentioned'])));
245
246
		if (empty($this->members['all']))
247
			return true;
248
249
		$this->prefs = getNotifyPrefs($this->members['all'], '', true);
250
251
		// May as well disable these, since they'll be stripped out anyway.
252
		$disable = array('attach', 'img', 'iurl', 'url', 'youtube');
253
		if (!empty($modSettings['disabledBBC']))
254
		{
255
			$disabledBBC = $modSettings['disabledBBC'];
256
			$disable = array_unique(array_merge($disable, explode(',', $modSettings['disabledBBC'])));
257
		}
258
		$modSettings['disabledBBC'] = implode(',', $disable);
259
260
		// Notify any members who were mentioned.
261
		if (!empty($this->members['mentioned']))
262
			$this->handleMentionedNotifications();
263
264
		// Notify any members who were quoted.
265
		if (!empty($this->members['quoted']))
266
			$this->handleQuoteNotifications();
267
268
		// Handle rest of the notifications for watched topics and boards
269
		if (!empty($this->members['watching']))
270
			$this->handleWatchedNotifications();
271
272
		// Put this back the way we found it.
273
		if (!empty($disabledBBC))
274
			$modSettings['disabledBBC'] = $disabledBBC;
275
276
		// Track what we sent.
277
		$members_to_log = array_intersect($this->members['emailed'], array_keys($this->members['watching']));
278
		if (!empty($members_to_log))
279
		{
280
			$smcFunc['db_query']('', '
281
				UPDATE {db_prefix}log_notify
282
				SET sent = {int:is_sent}
283
				WHERE (id_topic = {int:topic} OR id_board = {int:board})
284
					AND id_member IN ({array_int:members})',
285
				array(
286
					'topic' => $topicOptions['id'],
287
					'board' => $topicOptions['board'],
288
					// 'members' => $this->members['emailed'],
289
					'members' => $members_to_log,
290
					'is_sent' => 1,
291
				)
292
			);
293
		}
294
295
		// Insert it into the digest for daily/weekly notifications
296
		if ($type != 'edit' && empty($this->_details['respawns']))
297
		{
298
			$smcFunc['db_insert']('',
299
				'{db_prefix}log_digest',
300
				array(
301
					'id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int',
302
				),
303
				array($topicOptions['id'], $msgOptions['id'], $type, $posterOptions['id']),
304
				array()
305
			);
306
		}
307
308
		// Insert the alerts if any
309
		$this->updateAlerts($msgOptions['id']);
310
311
		// If there is anyone still to notify via email, create a new task later.
312
		$unnotified = array_diff_key($unnotified, array_flip($this->members['emailed']));
313
		if (!empty($unnotified) || !empty($msgOptions['mentioned_members']) || !empty($msgOptions['quoted_members']))
314
		{
315
			$new_details = $this->_details;
316
317
			if (empty($new_details['respawns']))
318
				$new_details['respawns'] = 0;
319
320
			if ($new_details['respawns']++ < 10)
321
			{
322
				$smcFunc['db_insert']('',
323
					'{db_prefix}background_tasks',
324
					array(
325
						'task_file' => 'string',
326
						'task_class' => 'string',
327
						'task_data' => 'string',
328
						'claimed_time' => 'int',
329
					),
330
					array(
331
						'$sourcedir/tasks/CreatePost-Notify.php',
332
						'CreatePost_Notify_Background',
333
						$smcFunc['json_encode']($new_details),
334
						max(0, $this->mention_mail_time - MAX_CLAIM_THRESHOLD),
335
					),
336
					array('id_task')
337
				);
338
			}
339
		}
340
341
		return true;
342
	}
343
344
	private function updateAlerts($msg_id)
345
	{
346
		global $smcFunc;
347
348
		// We send alerts only on the first iteration of this task.
349
		if (!empty($this->_details['respawns']))
350
			return;
351
352
		// Delete alerts about any mentions and quotes that no longer exist.
353
		if ($this->_details['type'] == 'edit')
354
		{
355
			$old_alerts = array();
356
357
			$request = $smcFunc['db_query']('', '
358
				SELECT content_action, id_member
359
				FROM {db_prefix}user_alerts
360
				WHERE content_id = {int:msg_id}
361
					AND content_type = {literal:msg}
362
					AND (content_action = {literal:quote} OR content_action = {literal:mention})',
363
				array(
364
					'msg_id' => $msg_id,
365
				)
366
			);
367
			if ($smcFunc['db_num_rows']($request) != 0)
368
			{
369
				while ($row = $smcFunc['db_fetch_assoc']($request))
370
					$old_alerts[$row['content_action']][$row['id_member']] = $row['id_member'];
371
			}
372
			$smcFunc['db_free_result']($request);
373
374
			if (!empty($old_alerts))
375
			{
376
				$request = $smcFunc['db_query']('', '
377
					SELECT content_type, id_mentioned
378
					FROM {db_prefix}mentions
379
					WHERE content_id = {int:msg_id}
380
						AND (content_type = {literal:quote} OR content_type = {literal:msg})',
381
					array(
382
						'msg_id' => $msg_id,
383
					)
384
				);
385
				while ($row = $smcFunc['db_fetch_assoc']($request))
386
				{
387
					$content_action = $row['content_type'] == 'quote' ? 'quote' : 'mention';
388
					unset($old_alerts[$content_action][$row['id_mentioned']]);
389
				}
390
				$smcFunc['db_free_result']($request);
391
392
				$conditions = array();
393
394
				if (!empty($old_alerts['quote']))
395
					$conditions[] = '(content_action = {literal:quote} AND id_member IN ({array_int:members_not_quoted}))';
396
397
				if (!empty($old_alerts['mention']))
398
					$conditions[] = '(content_action = {literal:mention} AND id_member IN ({array_int:members_not_mentioned}))';
399
400
				if (!empty($conditions))
401
				{
402
					$smcFunc['db_query']('', '
403
						DELETE FROM {db_prefix}user_alerts
404
						WHERE content_id = {int:msg_id}
405
							AND content_type = {literal:msg}
406
							AND (' . implode(' OR ', $conditions) . ')',
407
						array(
408
							'msg_id' => $msg_id,
409
							'members_not_quoted' => empty($old_alerts['quote']) ? array(0) : $old_alerts['quote'],
410
							'members_not_mentioned' => empty($old_alerts['mention']) ? array(0) : $old_alerts['mention'],
411
						)
412
					);
413
414
					foreach ($old_alerts as $member_ids)
415
						updateMemberData($member_ids, array('alerts' => '-'));
416
				}
417
			}
418
		}
419
420
		// Insert the new alerts.
421
		if (!empty($this->alert_rows))
422
		{
423
			$smcFunc['db_insert']('',
424
				'{db_prefix}user_alerts',
425
				array(
426
					'alert_time' => 'int',
427
					'id_member' => 'int',
428
					'id_member_started' => 'int',
429
					'member_name' => 'string',
430
					'content_type' => 'string',
431
					'content_id' => 'int',
432
					'content_action' => 'string',
433
					'is_read' => 'int',
434
					'extra' => 'string',
435
				),
436
				$this->alert_rows,
437
				array()
438
			);
439
440
			$new_alerts = array();
441
			foreach ($this->alert_rows as $row)
442
			{
443
				$group = implode(' ', array($row['content_type'], $row['content_id'], $row['content_action']));
444
				$new_alerts[$group] = $row['id_member'];
445
			}
446
447
			foreach ($new_alerts as $member_ids)
448
				updateMemberData($member_ids, array('alerts' => '+'));
449
		}
450
	}
451
452
	/**
453
	 * Notifies members about new posts in topics they are watching
454
	 * and new topics in boards they are watching.
455
	 */
456
	protected function handleWatchedNotifications()
457
	{
458
		global $smcFunc, $scripturl, $modSettings, $user_info;
459
460
		$msgOptions = &$this->_details['msgOptions'];
461
		$topicOptions = &$this->_details['topicOptions'];
462
		$posterOptions = &$this->_details['posterOptions'];
463
		$type = &$this->_details['type'];
464
465
		$user_ids = array_keys($this->members['watching']);
466
		if (!in_array($posterOptions['id'], $user_ids))
467
			$user_ids[] = $posterOptions['id'];
468
		$members_info = $this->getMinUserInfo($user_ids);
469
470
		$parsed_message = array();
471
		foreach ($this->members['watching'] as $member_id => $member_data)
472
		{
473
			if (in_array($member_id, $this->members['done']))
474
				continue;
475
476
			$frequency = isset($this->prefs[$member_id]['msg_notify_pref']) ? $this->prefs[$member_id]['msg_notify_pref'] : self::FREQUENCY_NOTHING;
477
			$notify_types = !empty($this->prefs[$member_id]['msg_notify_type']) ? $this->prefs[$member_id]['msg_notify_type'] : self::NOTIFY_TYPE_REPLIES_AND_MODERATION;
478
479
			// Don't send a notification if:
480
			// 1. The watching member ignored the member who did the action.
481
			if (!empty($member_data['pm_ignore_list']) && in_array($member_data['id_member_updated'], explode(',', $member_data['pm_ignore_list'])))
482
				continue;
483
484
			// 2. The watching member is not interested in moderation on this topic.
485
			if (!in_array($type, array('reply', 'topic')) && ($notify_types == self::NOTIFY_TYPE_ONLY_REPLIES || ($notify_types == self::NOTIFY_TYPE_REPLIES_AND_OWN_TOPIC_MODERATION && $member_id != $member_data['id_member_started'])))
486
				continue;
487
488
			// 3. This is the watching member's own post.
489
			if (in_array($type, array('reply', 'topic')) && $member_id == $posterOptions['id'])
490
				continue;
491
492
			// 4. The watching member doesn't want any notifications at all.
493
			if ($notify_types == self::NOTIFY_TYPE_NOTHING)
494
				continue;
495
496
			// 5. The watching member doesn't want notifications until later.
497
			if (in_array($frequency, array(
498
				self::FREQUENCY_NOTHING,
499
				self::FREQUENCY_DAILY_DIGEST,
500
				self::FREQUENCY_WEEKLY_DIGEST)))
501
				continue;
502
503
			// 6. We already sent one and the watching member doesn't want more.
504
			if ($frequency == self::FREQUENCY_FIRST_UNREAD_MSG && $member_data['sent'])
505
				continue;
506
507
			// 7. The watching member isn't on club security's VIP list.
508
			if (!empty($this->_details['members_only']) && !in_array($member_id, $this->_details['members_only']))
509
				continue;
510
511
			// Watched topic?
512
			if (!empty($member_data['id_topic']) && $type != 'topic' && !empty($this->prefs[$member_id]))
513
			{
514
				$pref = !empty($this->prefs[$member_id]['topic_notify_' . $topicOptions['id']]) ?
515
					$this->prefs[$member_id]['topic_notify_' . $topicOptions['id']] :
516
					(!empty($this->prefs[$member_id]['topic_notify']) ? $this->prefs[$member_id]['topic_notify'] : 0);
517
518
				$message_type = 'notification_' . $type;
519
520
				if ($type == 'reply')
521
				{
522
					if (empty($modSettings['disallow_sendBody']) && !empty($this->prefs[$member_id]['msg_receive_body']))
523
						$message_type .= '_body';
524
525
					if (!empty($frequency))
526
						$message_type .= '_once';
527
				}
528
529
				$content_type = 'topic';
530
			}
531
			// A new topic in a watched board then?
532
			elseif ($type == 'topic')
533
			{
534
				$pref = !empty($this->prefs[$member_id]['board_notify_' . $topicOptions['board']]) ?
535
					$this->prefs[$member_id]['board_notify_' . $topicOptions['board']] :
536
					(!empty($this->prefs[$member_id]['board_notify']) ? $this->prefs[$member_id]['board_notify'] : 0);
537
538
				$content_type = 'board';
539
540
				$message_type = !empty($frequency) ? 'notify_boards_once' : 'notify_boards';
541
542
				if (empty($modSettings['disallow_sendBody']) && !empty($this->prefs[$member_id]['msg_receive_body']))
543
					$message_type .= '_body';
544
			}
545
546
			// If neither of the above, this might be a redundant row due to the OR clause in our SQL query, skip
547
			else
548
				continue;
549
550
			// We need to fake some of $user_info to make BBC parsing work correctly.
551
			if (isset($user_info))
552
				$real_user_info = $user_info;
553
554
			$user_info = $members_info[$member_id];
555
556
			loadLanguage('index+Modifications', $member_data['lngfile'], false);
557
558
			// Censor and parse BBC in the receiver's localization. Don't repeat unnecessarily.
559
			$localization = implode('|', array($member_data['lngfile'], $user_info['time_offset'], $user_info['time_format']));
560
			if (empty($parsed_message[$localization]))
561
			{
562
				$parsed_message[$localization]['subject'] = $msgOptions['subject'];
563
				$parsed_message[$localization]['body'] = $msgOptions['body'];
564
565
				censorText($parsed_message[$localization]['subject']);
566
				censorText($parsed_message[$localization]['body']);
567
568
				$parsed_message[$localization]['subject'] = un_htmlspecialchars($parsed_message[$localization]['subject']);
569
				$parsed_message[$localization]['body'] = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($parsed_message[$localization]['body'], false), array('<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '&#91;' => '[', '&#93;' => ']', '&#39;' => '\'', '</tr>' => "\n", '</td>' => "\t", '<hr>' => "\n---------------------------------------------------------------\n")))));
570
			}
571
572
			// Put $user_info back the way we found it.
573
			if (isset($real_user_info))
574
			{
575
				$user_info = $real_user_info;
576
				unset($real_user_info);
577
			}
578
			else
579
				$user_info = null;
580
581
			// Bitwise check: Receiving a alert?
582
			if ($pref & self::RECEIVE_NOTIFY_ALERT)
583
			{
584
				$this->alert_rows[] = array(
585
					'alert_time' => time(),
586
					'id_member' => $member_id,
587
					// Only tell sender's information for new topics and replies
588
					'id_member_started' => in_array($type, array('topic', 'reply')) ? $posterOptions['id'] : 0,
589
					'member_name' => in_array($type, array('topic', 'reply')) ? $posterOptions['name'] : '',
590
					'content_type' => $content_type,
591
					'content_id' => $topicOptions['id'],
592
					'content_action' => $type,
593
					'is_read' => 0,
594
					'extra' => $smcFunc['json_encode'](array(
595
						'topic' => $topicOptions['id'],
596
						'board' => $topicOptions['board'],
597
						'content_subject' => $parsed_message[$localization]['subject'],
598
						'content_link' => $scripturl . '?topic=' . $topicOptions['id'] . (in_array($type, array('reply', 'topic')) ? '.new;topicseen#new' : '.0'),
599
					)),
600
				);
601
			}
602
603
			// Bitwise check: Receiving a email notification?
604
			if ($pref & self::RECEIVE_NOTIFY_EMAIL)
605
			{
606
				$itemID = $content_type == 'board' ? $topicOptions['board'] : $topicOptions['id'];
607
608
				$token = createUnsubscribeToken($member_data['id_member'], $member_data['email_address'], $content_type, $itemID);
609
610
				$replacements = array(
611
					'TOPICSUBJECT' => $parsed_message[$localization]['subject'],
612
					'POSTERNAME' => un_htmlspecialchars(isset($members_info[$posterOptions['id']]['name']) ? $members_info[$posterOptions['id']]['name'] : $posterOptions['name']),
613
					'TOPICLINK' => $scripturl . '?topic=' . $topicOptions['id'] . '.new#new',
614
					'MESSAGE' => $parsed_message[$localization]['body'],
615
					'UNSUBSCRIBELINK' => $scripturl . '?action=notify' . $content_type . ';' . $content_type . '=' . $itemID . ';sa=off;u=' . $member_data['id_member'] . ';token=' . $token,
616
				);
617
618
				$emaildata = loadEmailTemplate($message_type, $replacements, $member_data['lngfile']);
619
				$mail_result = sendmail($member_data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'm' . $topicOptions['id'], $emaildata['is_html']);
620
621
				if ($mail_result !== false)
622
					$this->members['emailed'][] = $member_id;
623
			}
624
625
			$this->members['done'][] = $member_id;
626
		}
627
	}
628
629
	/**
630
	 * Notifies members when their posts are quoted in other posts.
631
	 */
632
	protected function handleQuoteNotifications()
633
	{
634
		global $smcFunc, $modSettings, $language, $scripturl;
635
636
		$msgOptions = &$this->_details['msgOptions'];
637
		$posterOptions = &$this->_details['posterOptions'];
638
639
		foreach ($this->members['quoted'] as $member_id => $member_data)
640
		{
641
			if (in_array($member_id, $this->members['done']))
642
				continue;
643
644
			if (!isset($this->prefs[$member_id]) || empty($this->prefs[$member_id]['msg_quote']))
645
				continue;
646
			else
647
				$pref = $this->prefs[$member_id]['msg_quote'];
648
649
			// You don't need to be notified about quoting yourself.
650
			if ($member_id == $posterOptions['id'])
651
				continue;
652
653
			// Bitwise check: Receiving an alert?
654
			if ($pref & self::RECEIVE_NOTIFY_ALERT)
655
			{
656
				$this->alert_rows[] = array(
657
					'alert_time' => time(),
658
					'id_member' => $member_data['id'],
659
					'id_member_started' => $posterOptions['id'],
660
					'member_name' => $posterOptions['name'],
661
					'content_type' => 'msg',
662
					'content_id' => $msgOptions['id'],
663
					'content_action' => 'quote',
664
					'is_read' => 0,
665
					'extra' => $smcFunc['json_encode'](array(
666
						'content_subject' => $msgOptions['subject'],
667
						'content_link' => $scripturl . '?msg=' . $msgOptions['id'],
668
					)),
669
				);
670
			}
671
672
			// Bitwise check: Receiving a email notification?
673
			if (!($pref & self::RECEIVE_NOTIFY_EMAIL))
674
			{
675
				// Don't want an email, so forget this member in any respawned tasks.
676
				unset($msgOptions['quoted_members'][$member_id]);
677
			}
678
			elseif (TIME_START >= $this->mention_mail_time || in_array($member_id, $this->members['watching']))
679
			{
680
				$replacements = array(
681
					'CONTENTSUBJECT' => $msgOptions['subject'],
682
					'QUOTENAME' => $posterOptions['name'],
683
					'MEMBERNAME' => $member_data['real_name'],
684
					'CONTENTLINK' => $scripturl . '?msg=' . $msgOptions['id'],
685
				);
686
687
				$emaildata = loadEmailTemplate('msg_quote', $replacements, empty($member_data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $member_data['lngfile']);
688
				$mail_result = sendmail($member_data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'msg_quote_' . $msgOptions['id'], $emaildata['is_html'], 2);
689
690
				if ($mail_result !== false)
691
				{
692
					// Don't send multiple notifications about the same post.
693
					$this->members['emailed'][] = $member_id;
694
695
					// Ensure respawned tasks don't send this again.
696
					unset($msgOptions['quoted_members'][$member_id]);
697
				}
698
			}
699
700
			$this->members['done'][] = $member_id;
701
		}
702
	}
703
704
	/**
705
	 * Notifies members when they are mentioned in other members' posts.
706
	 */
707
	protected function handleMentionedNotifications()
708
	{
709
		global $smcFunc, $scripturl, $language, $modSettings;
710
711
		$msgOptions = &$this->_details['msgOptions'];
712
713
		foreach ($this->members['mentioned'] as $member_id => $member_data)
714
		{
715
			if (in_array($member_id, $this->members['done']))
716
				continue;
717
718
			if (empty($this->prefs[$member_id]) || empty($this->prefs[$member_id]['msg_mention']))
719
				continue;
720
			else
721
				$pref = $this->prefs[$member_id]['msg_mention'];
722
723
			// Mentioning yourself is silly, and we aren't going to notify you about it.
724
			if ($member_id == $member_data['mentioned_by']['id'])
725
				continue;
726
727
			// Bitwise check: Receiving an alert?
728
			if ($pref & self::RECEIVE_NOTIFY_ALERT)
729
			{
730
				$this->alert_rows[] = array(
731
					'alert_time' => time(),
732
					'id_member' => $member_data['id'],
733
					'id_member_started' => $member_data['mentioned_by']['id'],
734
					'member_name' => $member_data['mentioned_by']['name'],
735
					'content_type' => 'msg',
736
					'content_id' => $msgOptions['id'],
737
					'content_action' => 'mention',
738
					'is_read' => 0,
739
					'extra' => $smcFunc['json_encode'](array(
740
						'content_subject' => $msgOptions['subject'],
741
						'content_link' => $scripturl . '?msg=' . $msgOptions['id'],
742
					)),
743
				);
744
			}
745
746
747
			// Bitwise check: Receiving a email notification?
748
			if (!($pref & self::RECEIVE_NOTIFY_EMAIL))
749
			{
750
				// Don't want an email, so forget this member in any respawned tasks.
751
				unset($msgOptions['mentioned_members'][$member_id]);
752
			}
753
			elseif (TIME_START >= $this->mention_mail_time || in_array($member_id, $this->members['watching']))
754
			{
755
				$replacements = array(
756
					'CONTENTSUBJECT' => $msgOptions['subject'],
757
					'MENTIONNAME' => $member_data['mentioned_by']['name'],
758
					'MEMBERNAME' => $member_data['real_name'],
759
					'CONTENTLINK' => $scripturl . '?msg=' . $msgOptions['id'],
760
				);
761
762
				$emaildata = loadEmailTemplate('msg_mention', $replacements, empty($member_data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $member_data['lngfile']);
763
				$mail_result = sendmail($member_data['email_address'], $emaildata['subject'], $emaildata['body'], null, 'msg_mention_' . $msgOptions['id'], $emaildata['is_html'], 2);
764
765
				if ($mail_result !== false)
766
				{
767
					// Don't send multiple notifications about the same post.
768
					$this->members['emailed'][] = $member_id;
769
770
					// Ensure respawned tasks don't send this again.
771
					unset($msgOptions['mentioned_members'][$member_id]);
772
				}
773
			}
774
775
			$this->members['done'][] = $member_id;
776
		}
777
	}
778
}
779
780
?>