Issues (1027)

Sources/tasks/GroupAct-Notify.php (1 issue)

1
<?php
2
3
/**
4
 * This taks handles notifying someone that a user has
5
 * requested to join a group they moderate.
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 GroupAct_Notify_Background
19
 */
20
class GroupAct_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 $sourcedir, $smcFunc, $language, $modSettings;
29
30
		// Get the details of all the members concerned...
31
		$request = $smcFunc['db_query']('', '
32
			SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address,
33
				mem.lngfile, mem.member_name,  mg.group_name
34
			FROM {db_prefix}log_group_requests AS lgr
35
				INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
36
				INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
37
			WHERE lgr.id_request IN ({array_int:request_list})
38
			ORDER BY mem.lngfile',
39
			array(
40
				'request_list' => $this->_details['request_list'],
41
			)
42
		);
43
		$affected_users = array();
44
		$members = array();
45
		$alert_rows = array();
46
		while ($row = $smcFunc['db_fetch_assoc']($request))
47
		{
48
			$members[] = $row['id_member'];
49
			$row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
50
51
			// If we are approving, add them!
52
			if ($this->_details['status'] == 'approve')
53
			{
54
				// Hack in blank permissions so that allowedTo() will fail.
55
				require_once($sourcedir . '/Security.php');
56
				$user_info['permissions'] = array();
57
58
				// For the moddlog
59
				$user_info['id'] = $this->_details['member_id'];
60
				$user_info['ip'] = $this->_details['member_ip'];
61
62
				require_once($sourcedir . '/Subs-Membergroups.php');
63
				addMembersToGroup($row['id_member'], $row['id_group'], 'auto', true);
64
			}
65
66
			// Build the required information array
67
			$affected_users[$row['id_member']] = array(
68
				'rid' => $row['id_request'],
69
				'member_id' => $row['id_member'],
70
				'member_name' => $row['member_name'],
71
				'group_id' => $row['id_group'],
72
				'group_name' => $row['group_name'],
73
				'email' => $row['email_address'],
74
				'language' => $row['lngfile'],
75
			);
76
		}
77
		$smcFunc['db_free_result']($request);
78
79
		// Ensure everyone who is online gets their changes right away.
80
		updateSettings(array('settings_updated' => time()));
81
82
		if (!empty($affected_users))
83
		{
84
			require_once($sourcedir . '/Subs-Notify.php');
85
			$prefs = getNotifyPrefs($members, array('groupr_approved', 'groupr_rejected'), true);
86
87
			// They are being approved?
88
			if ($this->_details['status'] == 'approve')
89
			{
90
				$pref_name = 'approved';
91
				$email_template_name = 'mc_group_approve';
92
				$email_message_id_prefix = 'grpapp';
93
			}
94
			// Otherwise, they are getting rejected (With or without a reason).
95
			else
96
			{
97
				$pref_name = 'rejected';
98
				$email_template_name = empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $custom_reason seems to never exist and therefore empty should always be true.
Loading history...
99
				$email_message_id_prefix = 'grprej';
100
			}
101
102
			// Same as for approving, kind of.
103
			foreach ($affected_users as $user)
104
			{
105
				$pref = !empty($prefs[$user['member_id']]['groupr_' . $pref_name]) ? $prefs[$user['member_id']]['groupr_' . $pref_name] : 0;
106
				$custom_reason = isset($this->_details['reason']) && isset($this->_details['reason'][$user['rid']]) ? $this->_details['reason'][$user['rid']] : '';
107
108
				if ($pref & self::RECEIVE_NOTIFY_ALERT)
109
				{
110
					$alert_rows[] = array(
111
						'alert_time' => time(),
112
						'id_member' => $user['member_id'],
113
						'content_type' => 'groupr',
114
						'content_id' => 0,
115
						'content_action' => $pref_name,
116
						'is_read' => 0,
117
						'extra' => $smcFunc['json_encode'](array('group_name' => $user['group_name'], 'reason' => !empty($custom_reason) ? '<br><br>' . $custom_reason : '')),
118
					);
119
				}
120
121
				if ($pref & self::RECEIVE_NOTIFY_EMAIL)
122
				{
123
					// Emails are a bit complicated. We have to do language stuff.
124
					require_once($sourcedir . '/Subs-Post.php');
125
					require_once($sourcedir . '/ScheduledTasks.php');
126
					loadEssentialThemeData();
127
128
					$replacements = array(
129
						'USERNAME' => $user['member_name'],
130
						'GROUPNAME' => $user['group_name'],
131
					);
132
133
					if (!empty($custom_reason))
134
						$replacements['REASON'] = $custom_reason;
135
136
					$emaildata = loadEmailTemplate($email_template_name, $replacements, $user['language']);
137
138
					sendmail($user['email'], $emaildata['subject'], $emaildata['body'], null, $email_message_id_prefix . $user['rid'], $emaildata['is_html'], 2);
139
				}
140
			}
141
142
			// Insert the alerts if any
143
			if (!empty($alert_rows))
144
			{
145
				$smcFunc['db_insert']('',
146
					'{db_prefix}user_alerts',
147
					array(
148
						'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string',
149
						'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string',
150
					),
151
					$alert_rows,
152
					array()
153
				);
154
155
				updateMemberData(array_keys($affected_users), array('alerts' => '+'));
156
			}
157
		}
158
159
		return true;
160
	}
161
}
162
163
?>