CreateAttachment_Notify_Background   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 84
c 0
b 0
f 0
dl 0
loc 141
rs 10
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C execute() 0 135 12
1
<?php
2
/**
3
 * This file contains background notification code for moderators
4
 * to approve attachments.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
/**
17
 * Class CreateAttachment_Notify_Background
18
 */
19
class CreateAttachment_Notify_Background extends SMF_BackgroundTask
20
{
21
	/**
22
	 * This executes the task - loads up the info, puts the email in the queue and inserts any alerts as needed.
23
	 * @return bool Always returns true
24
	 */
25
	public function execute()
26
	{
27
		global $smcFunc, $sourcedir, $scripturl, $modSettings, $language;
28
29
		// Validate the attachment does exist and is the right approval state.
30
		$request = $smcFunc['db_query']('', '
31
			SELECT a.id_attach, m.id_board, m.id_msg, m.id_topic, m.id_member, m.subject
32
			FROM {db_prefix}attachments AS a
33
				INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
34
			WHERE a.id_attach = {int:attachment}
35
				AND a.attachment_type = {int:attachment_type}
36
				AND a.approved = {int:is_approved}',
37
			array(
38
				'attachment' => $this->_details['id'],
39
				'attachment_type' => 0,
40
				'is_approved' => 0,
41
			)
42
		);
43
		// Return true if either not found or invalid so that the cron runner deletes this task.
44
		if ($smcFunc['db_num_rows']($request) == 0)
45
			return true;
46
		list ($id_attach, $id_board, $id_msg, $id_topic, $id_member, $subject) = $smcFunc['db_fetch_row']($request);
47
		$smcFunc['db_free_result']($request);
48
49
		// We need to know who can approve this attachment.
50
		require_once($sourcedir . '/Subs-Members.php');
51
		$modMembers = membersAllowedTo('approve_posts', $id_board);
52
53
		$request = $smcFunc['db_query']('', '
54
			SELECT id_member, email_address, lngfile, real_name
55
			FROM {db_prefix}members
56
			WHERE id_member IN ({array_int:members})',
57
			array(
58
				'members' => array_merge($modMembers, array($id_member)),
59
			)
60
		);
61
62
		$members = array();
63
		$watched = array();
64
		$real_name = '';
65
		while ($row = $smcFunc['db_fetch_assoc']($request))
66
		{
67
			if ($row['id_member'] == $id_member)
68
				$real_name = $row['real_name'];
69
			else
70
			{
71
				$members[] = $row['id_member'];
72
				$watched[$row['id_member']] = $row;
73
			}
74
		}
75
		$smcFunc['db_free_result']($request);
76
77
		if (empty($members))
78
			return true;
79
80
		require_once($sourcedir . '/Subs-Notify.php');
81
		$members = array_unique($members);
82
		$prefs = getNotifyPrefs($members, 'unapproved_attachment', true);
83
		foreach ($watched as $member => $data)
84
		{
85
			$pref = !empty($prefs[$member]['unapproved_attachment']) ? $prefs[$member]['unapproved_attachment'] : 0;
86
87
			if ($pref & self::RECEIVE_NOTIFY_EMAIL)
88
			{
89
				// Emails are a bit complicated. (That's what she said)
90
				require_once($sourcedir . '/Subs-Post.php');
91
				require_once($sourcedir . '/ScheduledTasks.php');
92
				loadEssentialThemeData();
93
94
				$emaildata = loadEmailTemplate(
95
					'unapproved_attachment',
96
					array(
97
						'SUBJECT' => $subject,
98
						'LINK' => $scripturl . '?topic=' . $id_topic . '.msg' . $id_msg . '#msg' . $id_msg,
99
					),
100
					empty($data['lngfile']) || empty($modSettings['userLanguage']) ? $language : $data['lngfile']
101
				);
102
				sendmail(
103
					$data['email_address'],
104
					$emaildata['subject'],
105
					$emaildata['body'],
106
					null,
107
					'ma' . $id_attach,
108
					$emaildata['is_html']
109
				);
110
			}
111
112
			if ($pref & self::RECEIVE_NOTIFY_ALERT)
113
			{
114
				$alert_rows[] = array(
115
					'alert_time' => time(),
116
					'id_member' => $member,
117
					'id_member_started' => $id_member,
118
					'member_name' => $real_name,
119
					'content_type' => 'msg',
120
					'content_id' => $id_msg,
121
					'content_action' => 'unapproved_attachment',
122
					'is_read' => 0,
123
					'extra' => $smcFunc['json_encode'](
124
						array(
125
							'topic' => $id_topic,
126
							'board' => $id_board,
127
							'content_subject' => $subject,
128
							'content_link' => $scripturl . '?msg=' . $id_msg,
129
						)
130
					),
131
				);
132
			}
133
		}
134
135
		// Insert the alerts if any
136
		if (!empty($alert_rows))
137
		{
138
			$smcFunc['db_insert'](
139
				'insert',
140
				'{db_prefix}user_alerts',
141
				array(
142
					'alert_time' => 'int',
143
					'id_member' => 'int',
144
					'id_member_started' => 'int',
145
					'member_name' => 'string',
146
					'content_type' => 'string',
147
					'content_id' => 'int',
148
					'content_action' => 'string',
149
					'is_read' => 'int',
150
					'extra' => 'string',
151
				),
152
				$alert_rows,
153
				array()
154
			);
155
156
			updateMemberData(array_keys($watched), array('alerts' => '+'));
157
		}
158
159
		return true;
160
	}
161
}
162
163
?>