Register-Notify.php 4.0 KB

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