Issues (1014)

Sources/tasks/Birthday-Notify.php (1 issue)

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