1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains background notification code |
||
5 | * |
||
6 | * Simple Machines Forum (SMF) |
||
7 | * |
||
8 | * @package SMF |
||
9 | * @author Simple Machines https://www.simplemachines.org |
||
10 | * @copyright 2020 Simple Machines and individual contributors |
||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1 RC2 |
||
14 | */ |
||
15 | |||
16 | /** |
||
17 | * Class Birthday_Notify_Background |
||
18 | */ |
||
19 | class Birthday_Notify_Background extends SMF_BackgroundTask |
||
20 | { |
||
21 | /** |
||
22 | * This executes the task. It loads up the birthdays, figures out the greeting, etc. |
||
23 | * @return bool Always returns true |
||
24 | */ |
||
25 | public function execute() |
||
26 | { |
||
27 | global $txt, $smcFunc, $txtBirthdayEmails, $modSettings, $sourcedir; |
||
28 | |||
29 | $greeting = isset($modSettings['birthday_email']) ? $modSettings['birthday_email'] : 'happy_birthday'; |
||
30 | |||
31 | // Get the month and day of today. |
||
32 | $month = date('n'); // Month without leading zeros. |
||
33 | $day = date('j'); // Day without leading zeros. |
||
34 | |||
35 | // So who are the lucky ones? Don't include those who are banned and those who don't want them. |
||
36 | $result = $smcFunc['db_query']('', ' |
||
37 | SELECT id_member, real_name, lngfile, email_address |
||
38 | FROM {db_prefix}members |
||
39 | WHERE is_activated < 10 |
||
40 | AND MONTH(birthdate) = {int:month} |
||
41 | AND DAYOFMONTH(birthdate) = {int:day} |
||
42 | AND YEAR(birthdate) > {int:year} |
||
43 | ' . ($smcFunc['db_title'] === POSTGRE_TITLE ? 'AND indexable_month_day(birthdate) = indexable_month_day({date:bdate})' : ''), |
||
44 | array( |
||
45 | 'year' => 1004, |
||
46 | 'month' => $month, |
||
47 | 'day' => $day, |
||
48 | 'bdate' => '1004-' . $month . '-' . $day, // a random leap year is here needed |
||
49 | ) |
||
50 | ); |
||
51 | |||
52 | // Group them by languages. |
||
53 | $birthdays = array(); |
||
54 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
55 | { |
||
56 | if (!isset($birthdays[$row['lngfile']])) |
||
57 | $birthdays[$row['lngfile']] = array(); |
||
58 | $birthdays[$row['lngfile']][$row['id_member']] = array( |
||
59 | 'name' => $row['real_name'], |
||
60 | 'email' => $row['email_address'] |
||
61 | ); |
||
62 | } |
||
63 | $smcFunc['db_free_result']($result); |
||
64 | |||
65 | if (!empty($birthdays)) |
||
66 | { |
||
67 | require_once($sourcedir . '/ScheduledTasks.php'); |
||
68 | loadEssentialThemeData(); |
||
69 | // We need this for sendmail and AddMailQueue |
||
70 | require_once($sourcedir . '/Subs-Post.php'); |
||
71 | |||
72 | // Send out the greetings! |
||
73 | foreach ($birthdays as $lang => $members) |
||
74 | { |
||
75 | // We need to do some shuffling to make this work properly. |
||
76 | loadLanguage('EmailTemplates', $lang); |
||
77 | $txt['happy_birthday_subject'] = $txtBirthdayEmails[$greeting . '_subject']; |
||
78 | $txt['happy_birthday_body'] = $txtBirthdayEmails[$greeting . '_body']; |
||
79 | require_once($sourcedir . '/Subs-Notify.php'); |
||
80 | |||
81 | $prefs = getNotifyPrefs(array_keys($members), array('birthday'), true); |
||
82 | |||
83 | foreach ($members as $member_id => $member) |
||
84 | { |
||
85 | $pref = !empty($prefs[$member_id]['birthday']) ? $prefs[$member_id]['birthday'] : 0; |
||
86 | |||
87 | // Let's load replacements ahead |
||
88 | $replacements = array( |
||
89 | 'REALNAME' => $member['name'], |
||
90 | ); |
||
91 | |||
92 | if ($pref & self::RECEIVE_NOTIFY_ALERT) |
||
93 | { |
||
94 | $alertdata = loadEmailTemplate('happy_birthday', $replacements, $lang, false); |
||
95 | // For the alerts, we need to replace \n line breaks with <br> line breaks. |
||
96 | // For space saving sake, we'll be removing extra line breaks |
||
97 | $alertdata['body'] = preg_replace("~\s*[\r\n]+\s*~", '<br>', $alertdata['body']); |
||
98 | $alert_rows[] = array( |
||
99 | 'alert_time' => time(), |
||
100 | 'id_member' => $member_id, |
||
101 | 'content_type' => 'birthday', |
||
102 | 'content_id' => 0, |
||
103 | 'content_action' => 'msg', |
||
104 | 'is_read' => 0, |
||
105 | 'extra' => $smcFunc['json_encode'](array('happy_birthday' => $alertdata['body'])), |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | if ($pref & self::RECEIVE_NOTIFY_EMAIL) |
||
110 | { |
||
111 | $emaildata = loadEmailTemplate('happy_birthday', $replacements, $lang, false); |
||
112 | sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'birthday', $emaildata['is_html'], 4); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // Flush the mail queue, just in case. |
||
118 | AddMailQueue(true); |
||
119 | |||
120 | // Insert the alerts if any |
||
121 | if (!empty($alert_rows)) |
||
122 | { |
||
123 | $smcFunc['db_insert']('', |
||
124 | '{db_prefix}user_alerts', |
||
125 | array( |
||
126 | 'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string', |
||
127 | 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string', |
||
128 | ), |
||
129 | $alert_rows, |
||
130 | array() |
||
131 | ); |
||
132 | |||
133 | updateMemberData(array_keys($members), array('alerts' => '+')); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
134 | } |
||
135 | } |
||
136 | |||
137 | return true; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | ?> |