1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains just the functions that turn on and off notifications |
||
5 | * to topics or boards. |
||
6 | * |
||
7 | * Simple Machines Forum (SMF) |
||
8 | * |
||
9 | * @package SMF |
||
10 | * @author Simple Machines https://www.simplemachines.org |
||
11 | * @copyright 2022 Simple Machines and individual contributors |
||
12 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
13 | * |
||
14 | * @version 2.1.0 |
||
15 | */ |
||
16 | |||
17 | if (!defined('SMF')) |
||
18 | die('No direct access...'); |
||
19 | |||
20 | /** |
||
21 | * Turn off/on notification for a particular board. |
||
22 | * Must be called with a board specified in the URL. |
||
23 | * Only uses the template if no mode (or subaction) was given. |
||
24 | * Redirects the user back to the board after it is done. |
||
25 | * Accessed via ?action=notifyboard. |
||
26 | * |
||
27 | * @uses template_notify_board() |
||
28 | */ |
||
29 | function BoardNotify() |
||
30 | { |
||
31 | global $board, $user_info, $context, $smcFunc, $sourcedir, $scripturl, $txt; |
||
32 | |||
33 | require_once($sourcedir . '/Subs-Notify.php'); |
||
34 | |||
35 | // Subscribing or unsubscribing with a token. |
||
36 | if (isset($_REQUEST['u']) && isset($_REQUEST['token'])) |
||
37 | { |
||
38 | $member_info = getMemberWithToken('board'); |
||
39 | $skipCheckSession = true; |
||
40 | } |
||
41 | // No token, so try with the current user. |
||
42 | else |
||
43 | { |
||
44 | // Permissions are an important part of anything ;). |
||
45 | is_not_guest(); |
||
46 | $member_info = $user_info; |
||
47 | } |
||
48 | |||
49 | // You have to specify a board to turn notifications on! |
||
50 | if (empty($board)) |
||
51 | fatal_lang_error('no_board', false); |
||
52 | |||
53 | // sa=on/off is used for email subscribe/unsubscribe links |
||
54 | if (!isset($_GET['mode']) && isset($_GET['sa'])) |
||
55 | { |
||
56 | $_GET['mode'] = $_GET['sa'] == 'on' ? 3 : -1; |
||
57 | unset($_GET['sa']); |
||
58 | } |
||
59 | |||
60 | // No mode: find out what to do. |
||
61 | if (!isset($_GET['mode']) && !isset($_GET['xml'])) |
||
62 | { |
||
63 | // We're gonna need the notify template... |
||
64 | loadTemplate('Notify'); |
||
65 | |||
66 | // Find out if they have notification set for this board already. |
||
67 | $request = $smcFunc['db_query']('', ' |
||
68 | SELECT id_member |
||
69 | FROM {db_prefix}log_notify |
||
70 | WHERE id_member = {int:current_member} |
||
71 | AND id_board = {int:current_board} |
||
72 | LIMIT 1', |
||
73 | array( |
||
74 | 'current_board' => $board, |
||
75 | 'current_member' => $member_info['id'], |
||
76 | ) |
||
77 | ); |
||
78 | $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0; |
||
79 | $smcFunc['db_free_result']($request); |
||
80 | |||
81 | if ($member_info['id'] !== $user_info['id']) |
||
82 | $context['notify_info'] = array( |
||
83 | 'u' => $member_info['id'], |
||
84 | 'token' => $_REQUEST['token'], |
||
85 | ); |
||
86 | |||
87 | // Set the template variables... |
||
88 | $context['board_href'] = $scripturl . '?board=' . $board . '.' . $_REQUEST['start']; |
||
89 | $context['start'] = $_REQUEST['start']; |
||
90 | $context['page_title'] = $txt['notification']; |
||
91 | $context['sub_template'] = 'notify_board'; |
||
92 | |||
93 | return; |
||
94 | } |
||
95 | elseif (isset($_GET['mode'])) |
||
96 | { |
||
97 | if (empty($skipCheckSession)) |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
98 | checkSession('get'); |
||
99 | |||
100 | $mode = (int) $_GET['mode']; |
||
101 | |||
102 | // -1 is used to turn off email notifications while leaving the alert pref unchanged. |
||
103 | if ($mode == -1) |
||
104 | $mode = min(2, getNotifyPrefs($member_info['id'], array('board_notify_' . $board), true)); |
||
105 | |||
106 | $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3); |
||
107 | |||
108 | setNotifyPrefs((int) $member_info['id'], array('board_notify_' . $board => $alertPref)); |
||
109 | |||
110 | if ($mode > 1) |
||
111 | // Turn notification on. (note this just blows smoke if it's already on.) |
||
112 | $smcFunc['db_insert']('ignore', |
||
113 | '{db_prefix}log_notify', |
||
114 | array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'), |
||
115 | array($user_info['id'], 0, $board), |
||
116 | array('id_member', 'id_topic', 'id_board') |
||
117 | ); |
||
118 | else |
||
119 | $smcFunc['db_query']('', ' |
||
120 | DELETE FROM {db_prefix}log_notify |
||
121 | WHERE id_member = {int:current_member} |
||
122 | AND id_board = {int:current_board}', |
||
123 | array( |
||
124 | 'current_board' => $board, |
||
125 | 'current_member' => $member_info['id'], |
||
126 | ) |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | if (isset($_GET['xml'])) |
||
131 | { |
||
132 | $context['xml_data']['errors'] = array( |
||
133 | 'identifier' => 'error', |
||
134 | 'children' => array( |
||
135 | array( |
||
136 | 'value' => 0, |
||
137 | ), |
||
138 | ), |
||
139 | ); |
||
140 | $context['sub_template'] = 'generic_xml'; |
||
141 | } |
||
142 | // Probably followed an unsubscribe link, so just show a confirmation message. |
||
143 | elseif (!empty($skipCheckSession) && isset($mode)) |
||
0 ignored issues
–
show
|
|||
144 | { |
||
145 | loadTemplate('Notify'); |
||
146 | $context['page_title'] = $txt['notification']; |
||
147 | $context['sub_template'] = 'notify_pref_changed'; |
||
148 | $context['notify_success_msg'] = sprintf($txt['notify_board' . ($mode == 3 ? '_subscribed' : '_unsubscribed')], $member_info['email']); |
||
149 | return; |
||
150 | } |
||
151 | // Back to the board! |
||
152 | else |
||
153 | redirectexit('board=' . $board . '.' . $_REQUEST['start']); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Turn off/on unread replies subscription for a topic as well as sets individual topic's alert preferences |
||
158 | * Must be called with a topic specified in the URL. |
||
159 | * The mode can be from 0 to 3 |
||
160 | * 0 => unwatched, 1 => no alerts/emails, 2 => alerts, 3 => emails/alerts |
||
161 | * Upon successful completion of action will direct user back to topic. |
||
162 | * Accessed via ?action=notifytopic. |
||
163 | */ |
||
164 | function TopicNotify() |
||
165 | { |
||
166 | global $smcFunc, $user_info, $topic, $sourcedir, $context, $scripturl, $txt; |
||
167 | |||
168 | require_once($sourcedir . '/Subs-Notify.php'); |
||
169 | |||
170 | if (isset($_REQUEST['u']) && isset($_REQUEST['token'])) |
||
171 | { |
||
172 | $member_info = getMemberWithToken('topic'); |
||
173 | $skipCheckSession = true; |
||
174 | } |
||
175 | else |
||
176 | { |
||
177 | is_not_guest(); |
||
178 | $member_info = $user_info; |
||
179 | } |
||
180 | |||
181 | // Make sure the topic has been specified. |
||
182 | if (empty($topic)) |
||
183 | fatal_lang_error('not_a_topic', false); |
||
184 | |||
185 | // sa=on/off is used to toggle email notifications |
||
186 | if (!isset($_GET['mode']) && isset($_GET['sa'])) |
||
187 | { |
||
188 | $_GET['mode'] = $_GET['sa'] == 'on' ? 3 : -1; |
||
189 | unset($_GET['sa']); |
||
190 | } |
||
191 | |||
192 | // What do we do? Better ask if they didn't say.. |
||
193 | if (!isset($_GET['mode']) && !isset($_GET['xml'])) |
||
194 | { |
||
195 | // Load the template, but only if it is needed. |
||
196 | loadTemplate('Notify'); |
||
197 | |||
198 | // Find out if they have notification set for this topic already. |
||
199 | $request = $smcFunc['db_query']('', ' |
||
200 | SELECT id_member |
||
201 | FROM {db_prefix}log_notify |
||
202 | WHERE id_member = {int:current_member} |
||
203 | AND id_topic = {int:current_topic} |
||
204 | LIMIT 1', |
||
205 | array( |
||
206 | 'current_member' => $member_info['id'], |
||
207 | 'current_topic' => $topic, |
||
208 | ) |
||
209 | ); |
||
210 | $context['notification_set'] = $smcFunc['db_num_rows']($request) != 0; |
||
211 | $smcFunc['db_free_result']($request); |
||
212 | |||
213 | if ($member_info['id'] !== $user_info['id']) |
||
214 | $context['notify_info'] = array( |
||
215 | 'u' => $member_info['id'], |
||
216 | 'token' => $_REQUEST['token'], |
||
217 | ); |
||
218 | |||
219 | // Set the template variables... |
||
220 | $context['topic_href'] = $scripturl . '?topic=' . $topic . '.' . $_REQUEST['start']; |
||
221 | $context['start'] = $_REQUEST['start']; |
||
222 | $context['page_title'] = $txt['notification']; |
||
223 | |||
224 | return; |
||
225 | } |
||
226 | elseif (isset($_GET['mode'])) |
||
227 | { |
||
228 | if (empty($skipCheckSession)) |
||
0 ignored issues
–
show
|
|||
229 | checkSession('get'); |
||
230 | |||
231 | $mode = (int) $_GET['mode']; |
||
232 | |||
233 | // Turn off email notifications while leaving the alert pref alone. |
||
234 | if ($mode == -1) |
||
235 | $mode = min(2, getNotifyPrefs($member_info['id'], array('topic_notify_' . $topic), true)); |
||
236 | |||
237 | $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3); |
||
238 | |||
239 | $request = $smcFunc['db_query']('', ' |
||
240 | SELECT id_member, id_topic, id_msg, unwatched |
||
241 | FROM {db_prefix}log_topics |
||
242 | WHERE id_member = {int:current_user} |
||
243 | AND id_topic = {int:current_topic}', |
||
244 | array( |
||
245 | 'current_user' => $member_info['id'], |
||
246 | 'current_topic' => $topic, |
||
247 | ) |
||
248 | ); |
||
249 | $log = $smcFunc['db_fetch_assoc']($request); |
||
250 | $smcFunc['db_free_result']($request); |
||
251 | if (empty($log)) |
||
252 | { |
||
253 | $insert = true; |
||
254 | $log = array( |
||
255 | 'id_member' => $member_info['id'], |
||
256 | 'id_topic' => $topic, |
||
257 | 'id_msg' => 0, |
||
258 | 'unwatched' => empty($mode) ? 1 : 0, |
||
259 | ); |
||
260 | } |
||
261 | else |
||
262 | { |
||
263 | $insert = false; |
||
264 | $log['unwatched'] = empty($mode) ? 1 : 0; |
||
265 | } |
||
266 | |||
267 | $smcFunc['db_insert']($insert ? 'insert' : 'replace', |
||
268 | '{db_prefix}log_topics', |
||
269 | array( |
||
270 | 'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int', |
||
271 | ), |
||
272 | $log, |
||
273 | array('id_member', 'id_topic') |
||
274 | ); |
||
275 | |||
276 | setNotifyPrefs((int) $member_info['id'], array('topic_notify_' . $log['id_topic'] => $alertPref)); |
||
277 | |||
278 | if ($mode > 1) |
||
279 | { |
||
280 | // Turn notification on. (note this just blows smoke if it's already on.) |
||
281 | $smcFunc['db_insert']('ignore', |
||
282 | '{db_prefix}log_notify', |
||
283 | array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int'), |
||
284 | array($user_info['id'], $log['id_topic'], 0), |
||
285 | array('id_member','id_topic', 'id_board') |
||
286 | ); |
||
287 | } |
||
288 | else |
||
289 | $smcFunc['db_query']('', ' |
||
290 | DELETE FROM {db_prefix}log_notify |
||
291 | WHERE id_topic = {int:topic} |
||
292 | AND id_member = {int:member}', |
||
293 | array( |
||
294 | 'topic' => $log['id_topic'], |
||
295 | 'member' => $member_info['id'], |
||
296 | ) |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | if (isset($_GET['xml'])) |
||
301 | { |
||
302 | $context['xml_data']['errors'] = array( |
||
303 | 'identifier' => 'error', |
||
304 | 'children' => array( |
||
305 | array( |
||
306 | 'value' => 0, |
||
307 | ), |
||
308 | ), |
||
309 | ); |
||
310 | $context['sub_template'] = 'generic_xml'; |
||
311 | } |
||
312 | // Probably followed an unsubscribe link, so just show a confirmation message. |
||
313 | elseif (!empty($skipCheckSession) && isset($mode)) |
||
0 ignored issues
–
show
|
|||
314 | { |
||
315 | loadTemplate('Notify'); |
||
316 | $context['page_title'] = $txt['notification']; |
||
317 | $context['sub_template'] = 'notify_pref_changed'; |
||
318 | $context['notify_success_msg'] = sprintf($txt['notify_topic' . ($mode == 3 ? '_subscribed' : '_unsubscribed')], $member_info['email']); |
||
319 | return; |
||
320 | } |
||
321 | // Back to the topic. |
||
322 | else |
||
323 | redirectexit('topic=' . $topic . '.' . $_REQUEST['start']); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Turn off/on notifications for announcements. |
||
328 | * Only uses the template if no mode was given. |
||
329 | * Accessed via ?action=notifyannouncements. |
||
330 | */ |
||
331 | function AnnouncementsNotify() |
||
332 | { |
||
333 | global $scripturl, $txt, $board, $user_info, $context, $smcFunc, $sourcedir; |
||
334 | |||
335 | require_once($sourcedir . '/Subs-Notify.php'); |
||
336 | |||
337 | if (isset($_REQUEST['u']) && isset($_REQUEST['token'])) |
||
338 | { |
||
339 | $member_info = getMemberWithToken('announcements'); |
||
340 | $skipCheckSession = true; |
||
341 | } |
||
342 | else |
||
343 | { |
||
344 | is_not_guest(); |
||
345 | $member_info = $user_info; |
||
346 | } |
||
347 | |||
348 | loadTemplate('Notify'); |
||
349 | $context['page_title'] = $txt['notification']; |
||
350 | |||
351 | // Backward compatibility. |
||
352 | if (!isset($_GET['mode']) && isset($_GET['sa'])) |
||
353 | { |
||
354 | $_GET['mode'] = $_GET['sa'] == 'on' ? 1 : 0; |
||
355 | unset($_GET['sa']); |
||
356 | } |
||
357 | |||
358 | // Ask what they want to do. |
||
359 | if (!isset($_GET['mode'])) |
||
360 | { |
||
361 | $context['sub_template'] = 'notify_announcements'; |
||
362 | |||
363 | if ($member_info['id'] !== $user_info['id']) |
||
364 | $context['notify_info'] = array( |
||
365 | 'u' => $member_info['id'], |
||
366 | 'token' => $_REQUEST['token'], |
||
367 | ); |
||
368 | |||
369 | return; |
||
370 | } |
||
371 | |||
372 | // We don't tolerate imposters around here. |
||
373 | if (empty($skipCheckSession)) |
||
374 | checkSession('get'); |
||
375 | |||
376 | $mode = (int) !empty($_GET['mode']); |
||
377 | |||
378 | // Update their announcement notification preference. |
||
379 | setNotifyPrefs((int) $member_info['id'], array('announcements' => $mode)); |
||
380 | |||
381 | // Show a confirmation message. |
||
382 | $context['sub_template'] = 'notify_pref_changed'; |
||
383 | $context['notify_success_msg'] = sprintf($txt['notify_announcements' . (!empty($mode) ? '_subscribed' : '_unsubscribed')], $member_info['email']); |
||
384 | } |
||
385 | |||
386 | ?> |