1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 
<?php

/**
 * This file contains background notification code
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC1
 */

/**
 * Class Birthday_Notify_Background
 */
class Birthday_Notify_Background extends SMF_BackgroundTask
{
    /**
     * This executes the task. It loads up the birthdays, figures out the greeting, etc.
     * @return bool Always returns true
     */
    public function execute()
    {
        global $txt, $smcFunc, $txtBirthdayEmails, $modSettings, $sourcedir;

        $greeting = isset($modSettings['birthday_email']) ? $modSettings['birthday_email'] : 'happy_birthday';

        // Get the month and day of today.
        $month = date('n'); // Month without leading zeros.
        $day = date('j'); // Day without leading zeros.

        // So who are the lucky ones?  Don't include those who are banned and those who don't want them.
        $result = $smcFunc['db_query']('', '
            SELECT id_member, real_name, lngfile, email_address
            FROM {db_prefix}members
            WHERE is_activated < 10
                AND MONTH(birthdate) = {int:month}
                AND DAYOFMONTH(birthdate) = {int:day}
                AND YEAR(birthdate) > {int:year}',
            array(
                'year' => 1,
                'month' => $month,
                'day' => $day,
            )
        );

        // Group them by languages.
        $birthdays = array();
        while ($row = $smcFunc['db_fetch_assoc']($result))
        {
            if (!isset($birthdays[$row['lngfile']]))
                $birthdays[$row['lngfile']] = array();
            $birthdays[$row['lngfile']][$row['id_member']] = array(
                'name' => $row['real_name'],
                'email' => $row['email_address']
            );
        }
        $smcFunc['db_free_result']($result);

        if (!empty($birthdays))
        {
            require_once($sourcedir . '/ScheduledTasks.php');
            loadEssentialThemeData();
            // We need this for sendmail and AddMailQueue
            require_once($sourcedir . '/Subs-Post.php');

            // Send out the greetings!
            foreach ($birthdays as $lang => $members)
            {
                // We need to do some shuffling to make this work properly.
                loadLanguage('EmailTemplates', $lang);
                $txt['happy_birthday_subject'] = $txtBirthdayEmails[$greeting . '_subject'];
                $txt['happy_birthday_body'] = $txtBirthdayEmails[$greeting . '_body'];
                require_once($sourcedir . '/Subs-Notify.php');

                $prefs = getNotifyPrefs(array_keys($members), array('birthday'), true);

                foreach ($members as $member_id => $member)
                {
                    $pref = !empty($prefs[$member_id]['birthday']) ? $prefs[$member_id]['birthday'] : 0;

                    // Let's load replacements ahead
                    $replacements = array(
                        'REALNAME' => $member['name'],
                    );

                    if ($pref & self::RECEIVE_NOTIFY_ALERT)
                    {
                        $alertdata = loadEmailTemplate('happy_birthday', $replacements, $lang, false);
                        // For the alerts, we need to replace \n line breaks with <br> line breaks.
                        // For space saving sake, we'll be removing extra line breaks
                        $alertdata['body'] = preg_replace("~\s*[\r\n]+\s*~", '<br>', $alertdata['body']);
                        $alert_rows[] = array(
                            'alert_time' => time(),
                            'id_member' => $member_id,
                            'content_type' => 'birthday',
                            'content_id' => 0,
                            'content_action' => 'msg',
                            'is_read' => 0,
                            'extra' => $smcFunc['json_encode'](array('happy_birthday' => $alertdata['body'])),
                        );
                        updateMemberData($member_id, array('alerts' => '+'));
                    }

                    if ($pref & self::RECEIVE_NOTIFY_EMAIL)
                    {
                        $emaildata = loadEmailTemplate('happy_birthday', $replacements, $lang, false);
                        sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, 'birthday', $emaildata['is_html'], 4);
                    }
                }
            }

            // Flush the mail queue, just in case.
            AddMailQueue(true);

            // Insert the alerts if any
            if (!empty($alert_rows))
                $smcFunc['db_insert']('',
                    '{db_prefix}user_alerts',
                    array(
                        'alert_time' => 'int', 'id_member' => 'int', 'content_type' => 'string',
                        'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string',
                    ),
                    $alert_rows,
                    array()
                );
        }

        return true;
    }
}

?>