MsgReportReply-Notify.php 6.9 KB

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