Register-Notify.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * This task handles notifying users when someone new signs up.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2013 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 Register_Notify_Background extends SMF_BackgroundTask
  15. {
  16. public function execute()
  17. {
  18. global $smcFunc, $sourcedir, $language, $scripturl;
  19. // Get everyone who could be notified.
  20. require_once($sourcedir . '/Subs-Members.php');
  21. $members = membersAllowedTo('moderate_forum');
  22. // Having successfully figured this out, now let's get the preferences of everyone.
  23. require_once($sourcedir . '/Subs-Notify.php');
  24. $prefs = getNotifyPrefs($members, 'member_register');
  25. // Apply defaults if we got some.
  26. if (isset($prefs[0]))
  27. {
  28. foreach ($members as $member)
  29. if (!isset($prefs[$member]))
  30. $prefs[$member] = $prefs[0];
  31. unset ($prefs[0]); // Don't need this any more.
  32. }
  33. // So now we find out who wants what.
  34. $alert_bits = array(
  35. 'alert' => 0x01,
  36. 'email' => 0x02,
  37. );
  38. $notifies = array();
  39. foreach ($prefs as $member => $pref_option)
  40. {
  41. foreach ($alert_bits as $type => $bitvalue)
  42. if ($pref_option & $bitvalue == $bitvalue)
  43. $notifies[$type][] = $member;
  44. }
  45. // Firstly, anyone who wants alerts.
  46. if (!empty($notifies['alert']))
  47. {
  48. // Alerts are relatively easy.
  49. $insert_rows = array();
  50. foreach ($notifies['alert'] as $member)
  51. {
  52. $insert_rows[] = array(
  53. 'alert_time' => $this->_details['time'],
  54. 'id_member' => $member,
  55. 'id_member_started' => $this->_details['new_member_id'],
  56. 'member_name' => $this->_details['new_member_name'],
  57. 'content_type' => 'member',
  58. 'content_id' => 0,
  59. 'content_action' => 'register_' . $this->_details['notify_type'],
  60. 'is_read' => 0,
  61. 'extra' => '',
  62. );
  63. }
  64. $smcFunc['db_insert']('insert',
  65. '{db_prefix}user_alerts',
  66. array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
  67. 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
  68. 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
  69. $insert_rows,
  70. array('id_alert')
  71. );
  72. // And update the count of alerts for those people.
  73. updateMemberData($notifies['alert'], array('alerts' => '+'));
  74. }
  75. // Secondly, anyone who wants emails.
  76. if (!empty($notifies['email']))
  77. {
  78. // Emails are a bit complicated. We have to do language stuff.
  79. require_once($sourcedir . '/Subs-Post.php');
  80. require_once($sourcedir . '/ScheduledTasks.php');
  81. loadEssentialThemeData();
  82. // First, get everyone's language and details.
  83. $emails = array();
  84. $request = $smcFunc['db_query']('', '
  85. SELECT id_member, lngfile, email_address
  86. FROM {db_prefix}members
  87. WHERE id_member IN ({array_int:members})',
  88. array(
  89. 'members' => $notifies['email'],
  90. )
  91. );
  92. while ($row = $smcFunc['db_fetch_assoc']($request))
  93. {
  94. if (empty($row['lngfile']))
  95. $row['lngfile'] = $language;
  96. $emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
  97. }
  98. $smcFunc['db_free_result']($request);
  99. // Second, iterate through each language, load the relevant templates and set up sending.
  100. foreach ($emails as $this_lang => $recipients)
  101. {
  102. $replacements = array(
  103. 'USERNAME' => $this->_details['new_member_name'],
  104. 'PROFILELINK' => $scripturl . '?action=profile;u=' . $this->_details['new_member_id']
  105. );
  106. $emailtype = 'admin_notify';
  107. // If they need to be approved add more info...
  108. if ($this->_details['notify_type'] == 'approval')
  109. {
  110. $replacements['APPROVALLINK'] = $scripturl . '?action=admin;area=viewmembers;sa=browse;type=approve';
  111. $emailtype .= '_approval';
  112. }
  113. $emaildata = loadEmailTemplate($emailtype, $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
  114. // And do the actual sending...
  115. foreach ($recipients as $id_member => $email_address)
  116. sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  117. }
  118. }
  119. // And now we're all done.
  120. return true;
  121. }
  122. }
  123. ?>