MemberReportReply-Notify.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 MemberReportReply_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_forum');
  48. // Having successfully figured this out, now let's get the preferences of everyone.
  49. require_once($sourcedir . '/Subs-Notify.php');
  50. $prefs = getNotifyPrefs($members, 'member_report_reply', true);
  51. // So now we find out who wants what.
  52. $alert_bits = array(
  53. 'alert' => 0x01,
  54. 'email' => 0x02,
  55. );
  56. $notifies = array();
  57. foreach ($prefs as $member => $pref_option)
  58. {
  59. foreach ($alert_bits as $type => $bitvalue)
  60. {
  61. if ($pref_option['member_report_reply'] & $bitvalue)
  62. $notifies[$type][] = $member;
  63. }
  64. }
  65. // Firstly, anyone who wants alerts.
  66. if (!empty($notifies['alert']))
  67. {
  68. // Alerts are relatively easy.
  69. $insert_rows = array();
  70. foreach ($notifies['alert'] as $member)
  71. {
  72. $insert_rows[] = array(
  73. 'alert_time' => $this->_details['time'],
  74. 'id_member' => $member,
  75. 'id_member_started' => $this->_details['sender_id'],
  76. 'member_name' => $this->_details['sender_name'],
  77. 'content_type' => 'profile',
  78. 'content_id' => $this->_details['user_id'],
  79. 'content_action' => 'report_reply',
  80. 'is_read' => 0,
  81. 'extra' => serialize(
  82. array(
  83. 'report_link' => '?action=moderate;area=memberreports;report=' . $this->_details['report_id'], // We don't put $scripturl in these!
  84. 'user_name' => $this->_details['user_name'],
  85. )
  86. ),
  87. );
  88. }
  89. $smcFunc['db_insert']('insert',
  90. '{db_prefix}user_alerts',
  91. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  92. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  93. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  94. $insert_rows,
  95. array('id_alert')
  96. );
  97. // And update the count of alerts for those people.
  98. updateMemberData($notifies['alert'], array('alerts' => '+'));
  99. }
  100. // Secondly, anyone who wants emails.
  101. if (!empty($notifies['email']))
  102. {
  103. // Emails are a bit complicated. We have to do language stuff.
  104. require_once($sourcedir . '/Subs-Post.php');
  105. require_once($sourcedir . '/ScheduledTasks.php');
  106. loadEssentialThemeData();
  107. // First, get everyone's language and details.
  108. $emails = array();
  109. $request = $smcFunc['db_query']('', '
  110. SELECT id_member, lngfile, email_address
  111. FROM {db_prefix}members
  112. WHERE id_member IN ({array_int:members})',
  113. array(
  114. 'members' => $notifies['email'],
  115. )
  116. );
  117. while ($row = $smcFunc['db_fetch_assoc']($request))
  118. {
  119. if (empty($row['lngfile']))
  120. $row['lngfile'] = $language;
  121. $emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
  122. }
  123. $smcFunc['db_free_result']($request);
  124. // Iterate through each language, load the relevant templates and set up sending.
  125. foreach ($emails as $this_lang => $recipients)
  126. {
  127. $replacements = array(
  128. 'MEMBERNAME' => $this->_details['member_name'],
  129. 'COMMENTERNAME' => $this->_details['sender_name'],
  130. 'PROFILELINK' => $scripturl . 'action=profile;u=' . $this->_details['user_id'],
  131. 'REPORTLINK' => $scripturl . '?action=moderate;area=userreports;report=' . $this->_details['report_id'],
  132. );
  133. $emaildata = loadEmailTemplate('reply_to_user_reports', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
  134. // And do the actual sending...
  135. foreach ($recipients as $id_member => $email_address)
  136. sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, 'urptrpy' . $this->_details['comment_id'], false, 3);
  137. }
  138. }
  139. // And now we're all done.
  140. return true;
  141. }
  142. }
  143. ?>