Issues (1065)

Sources/tasks/CreatePost-Notify.php (3 issues)

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