MsgReportReply-Notify.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. print_r($prefs);
  89. foreach ($prefs as $member => $pref_option)
  90. {
  91. foreach ($alert_bits as $type => $bitvalue)
  92. {
  93. if ($pref_option['msg_report_reply'] & $bitvalue)
  94. $notifies[$type][] = $member;
  95. }
  96. }
  97. print_r($notifies);
  98. // Firstly, anyone who wants alerts.
  99. if (!empty($notifies['alert']))
  100. {
  101. // Alerts are relatively easy.
  102. $insert_rows = array();
  103. foreach ($notifies['alert'] as $member)
  104. {
  105. $insert_rows[] = array(
  106. 'alert_time' => $this->_details['time'],
  107. 'id_member' => $member,
  108. 'id_member_started' => $this->_details['sender_id'],
  109. 'member_name' => $this->_details['sender_name'],
  110. 'content_type' => 'msg',
  111. 'content_id' => $this->_details['msg_id'],
  112. 'content_action' => 'report_reply',
  113. 'is_read' => 0,
  114. 'extra' => serialize(
  115. array(
  116. 'report_link' => $scripturl . '?action=moderate;area=reports;report=' . $this->_details['report_id'],
  117. )
  118. ),
  119. );
  120. }
  121. $smcFunc['db_insert']('insert',
  122. '{db_prefix}user_alerts',
  123. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  124. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  125. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  126. $insert_rows,
  127. array('id_alert')
  128. );
  129. // And update the count of alerts for those people.
  130. updateMemberData($notifies['alert'], array('alerts' => '+'));
  131. }
  132. // Secondly, anyone who wants emails.
  133. if (!empty($notifies['email']))
  134. {
  135. // Emails are a bit complicated. We have to do language stuff.
  136. require_once($sourcedir . '/Subs-Post.php');
  137. require_once($sourcedir . '/ScheduledTasks.php');
  138. loadEssentialThemeData();
  139. // First, get everyone's language and details.
  140. $emails = array();
  141. $request = $smcFunc['db_query']('', '
  142. SELECT id_member, lngfile, email_address
  143. FROM {db_prefix}members
  144. WHERE id_member IN ({array_int:members})',
  145. array(
  146. 'members' => $notifies['email'],
  147. )
  148. );
  149. while ($row = $smcFunc['db_fetch_assoc']($request))
  150. {
  151. if (empty($row['lngfile']))
  152. $row['lngfile'] = $language;
  153. $emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
  154. }
  155. $smcFunc['db_free_result']($request);
  156. // Second, get some details that might be nice for the report email.
  157. // We don't bother cluttering up the tasks data for this, when it's really no bother to fetch it.
  158. $request = $smcFunc['db_query']('', '
  159. SELECT lr.subject, lr.membername, lr.body
  160. FROM {db_prefix}log_reported AS lr
  161. WHERE id_report = {int:report}',
  162. array(
  163. 'report' => $this->_details['report_id'],
  164. )
  165. );
  166. list ($subject, $poster_name, $comment) = $smcFunc['db_fetch_row']($request);
  167. $smcFunc['db_free_result']($request);
  168. // Third, iterate through each language, load the relevant templates and set up sending.
  169. foreach ($emails as $this_lang => $recipients)
  170. {
  171. echo 'Emailing (' . $this_lang . ') to ' . print_r($recipients, true);
  172. $replacements = array(
  173. 'TOPICSUBJECT' => $subject,
  174. 'POSTERNAME' => $poster_name,
  175. 'COMMENTERNAME' => $this->_details['sender_name'],
  176. 'TOPICLINK' => $scripturl . '?topic=' . $this->_details['topic_id'] . '.msg' . $this->_details['msg_id'] . '#msg' . $this->_details['msg_id'],
  177. 'REPORTLINK' => $scripturl . '?action=moderate;area=reports;report=' . $this->_details['report_id'],
  178. );
  179. $emaildata = loadEmailTemplate('reply_to_moderator', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
  180. // And do the actual sending...
  181. foreach ($recipients as $id_member => $email_address)
  182. sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, null, false, 3);
  183. }
  184. }
  185. // And now we're all done.
  186. return true;
  187. }
  188. }
  189. ?>