2
0

MsgReport-Notify.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * This task handles notifying users when a message gets reported.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2014 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. class MsgReport_Notify_Background extends SMF_BackgroundTask
  15. {
  16. public function execute()
  17. {
  18. global $smcFunc, $sourcedir, $modSettings, $language, $scripturl;
  19. // We need to know who can moderate this board - and therefore who can see this report.
  20. // First up, people who have moderate_board in the board this topic was in.
  21. require_once($sourcedir . '/Subs-Members.php');
  22. $members = membersAllowedTo('moderate_board', $this->_details['board_id']);
  23. // Second, anyone assigned to be a moderator of this board directly.
  24. $request = $smcFunc['db_query']('', '
  25. SELECT id_member
  26. FROM {db_prefix}moderators
  27. WHERE id_board = {int:current_board}',
  28. array(
  29. 'current_board' => $this->_details['board_id'],
  30. )
  31. );
  32. while ($row = $smcFunc['db_fetch_assoc']($request))
  33. $members[] = $row['id_member'];
  34. $smcFunc['db_free_result']($request);
  35. // Thirdly, anyone assigned to be a moderator of this group as a group->board moderator.
  36. $request = $smcFunc['db_query']('', '
  37. SELECT mem.id_member
  38. FROM {db_prefix}members AS mem, {db_prefix}moderator_groups AS bm
  39. WHERE bm.id_board = {int:current_board}
  40. AND(
  41. mem.id_group = bm.id_group
  42. OR FIND_IN_SET(bm.id_group, mem.additional_groups) != 0
  43. )',
  44. array(
  45. 'current_board' => $this->_details['board_id'],
  46. )
  47. );
  48. while ($row = $smcFunc['db_fetch_assoc']($request))
  49. $members[] = $row['id_member'];
  50. $smcFunc['db_free_result']($request);
  51. // And now weed out the duplicates.
  52. $members = array_flip(array_flip($members));
  53. // And don't send it to them if they're the one who reported it.
  54. $members = array_diff($members, array($this->_details['sender_id']));
  55. // Having successfully figured this out, now let's get the preferences of everyone.
  56. require_once($sourcedir . '/Subs-Notify.php');
  57. $prefs = getNotifyPrefs($members, 'msg_report', true);
  58. // So now we find out who wants what.
  59. $alert_bits = array(
  60. 'alert' => 0x01,
  61. 'email' => 0x02,
  62. );
  63. $notifies = array();
  64. foreach ($prefs as $member => $pref_option)
  65. {
  66. foreach ($alert_bits as $type => $bitvalue)
  67. if ($pref_option['msg_report'] & $bitvalue)
  68. $notifies[$type][] = $member;
  69. }
  70. // Firstly, anyone who wants alerts.
  71. if (!empty($notifies['alert']))
  72. {
  73. // Alerts are relatively easy.
  74. $insert_rows = array();
  75. foreach ($notifies['alert'] as $member)
  76. {
  77. $insert_rows[] = array(
  78. 'alert_time' => $this->_details['time'],
  79. 'id_member' => $member,
  80. 'id_member_started' => $this->_details['sender_id'],
  81. 'member_name' => $this->_details['sender_name'],
  82. 'content_type' => 'msg',
  83. 'content_id' => $this->_details['msg_id'],
  84. 'content_action' => 'report',
  85. 'is_read' => 0,
  86. 'extra' => serialize(
  87. array(
  88. 'report_link' => '?action=moderate;area=reports;report=' . $this->_details['report_id'], // We don't put $scripturl in these!
  89. )
  90. ),
  91. );
  92. }
  93. $smcFunc['db_insert']('insert',
  94. '{db_prefix}user_alerts',
  95. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  96. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  97. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  98. $insert_rows,
  99. array('id_alert')
  100. );
  101. // And update the count of alerts for those people.
  102. updateMemberData($notifies['alert'], array('alerts' => '+'));
  103. }
  104. // Secondly, anyone who wants emails.
  105. if (!empty($notifies['email']))
  106. {
  107. // Emails are a bit complicated. We have to do language stuff.
  108. require_once($sourcedir . '/Subs-Post.php');
  109. require_once($sourcedir . '/ScheduledTasks.php');
  110. loadEssentialThemeData();
  111. // First, get everyone's language and details.
  112. $emails = array();
  113. $request = $smcFunc['db_query']('', '
  114. SELECT id_member, lngfile, email_address
  115. FROM {db_prefix}members
  116. WHERE id_member IN ({array_int:members})',
  117. array(
  118. 'members' => $notifies['email'],
  119. )
  120. );
  121. while ($row = $smcFunc['db_fetch_assoc']($request))
  122. {
  123. if (empty($row['lngfile']))
  124. $row['lngfile'] = $language;
  125. $emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
  126. }
  127. $smcFunc['db_free_result']($request);
  128. // Second, get some details that might be nice for the report email.
  129. // We don't bother cluttering up the tasks data for this, when it's really no bother to fetch it.
  130. $request = $smcFunc['db_query']('', '
  131. SELECT lr.subject, lr.membername, lr.body
  132. FROM {db_prefix}log_reported AS lr
  133. WHERE id_report = {int:report}',
  134. array(
  135. 'report' => $this->_details['report_id'],
  136. )
  137. );
  138. list ($subject, $poster_name, $comment) = $smcFunc['db_fetch_row']($request);
  139. $smcFunc['db_free_result']($request);
  140. // Third, iterate through each language, load the relevant templates and set up sending.
  141. foreach ($emails as $this_lang => $recipients)
  142. {
  143. $replacements = array(
  144. 'TOPICSUBJECT' => $subject,
  145. 'POSTERNAME' => $poster_name,
  146. 'REPORTERNAME' => $this->_details['sender_name'],
  147. 'TOPICLINK' => $scripturl . '?topic=' . $this->_details['topic_id'] . '.msg' . $this->_details['msg_id'] . '#msg' . $this->_details['msg_id'],
  148. 'REPORTLINK' => $scripturl . '?action=moderate;area=reports;report=' . $this->_details['report_id'],
  149. 'COMMENT' => $comment,
  150. );
  151. $emaildata = loadEmailTemplate('report_to_moderator', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
  152. // And do the actual sending...
  153. foreach ($recipients as $id_member => $email_address)
  154. sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, 'report' . $this->_details['report_id'], false, 2);
  155. }
  156. }
  157. // And now we're all done.
  158. return true;
  159. }
  160. }
  161. ?>