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: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 
<?php

/**
 * This taks handles notifying someone that a user has
 * requested to join a group they moderate.
 *
 * 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 GroupAct_Notify_Background
 */
class GroupAct_Notify_Background extends SMF_BackgroundTask
{
    /**
     * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
     * @return bool Always returns true
     */
    public function execute()
    {
        global $sourcedir, $smcFunc, $language, $modSettings;

        // Get the details of all the members concerned...
        $request = $smcFunc['db_query']('', '
            SELECT lgr.id_request, lgr.id_member, lgr.id_group, mem.email_address,
                mem.lngfile, mem.member_name,  mg.group_name
            FROM {db_prefix}log_group_requests AS lgr
                INNER JOIN {db_prefix}members AS mem ON (mem.id_member = lgr.id_member)
                INNER JOIN {db_prefix}membergroups AS mg ON (mg.id_group = lgr.id_group)
            WHERE lgr.id_request IN ({array_int:request_list})
            ORDER BY mem.lngfile',
            array(
                'request_list' => $this->_details['request_list'],
            )
        );
        $affected_users = array();
        $members = array();
        $alert_rows = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $members[] = $row['id_member'];
            $row['lngfile'] = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];

            // If we are approving, add them!
            if ($this->_details['status'] == 'approve')
            {
                // Hack in blank permissions so that allowedTo() will fail.
                require_once($sourcedir . '/Security.php');
                $user_info['permissions'] = array();

                // For the moddlog
                $user_info['id'] = $this->_details['member_id'];
                $user_info['ip'] = $this->_details['member_ip'];

                require_once($sourcedir . '/Subs-Membergroups.php');
                addMembersToGroup($row['id_member'], $row['id_group'], 'auto', true);
            }

            // Build the required information array
            $affected_users[] = array(
                'rid' => $row['id_request'],
                'member_id' => $row['id_member'],
                'member_name' => $row['member_name'],
                'group_id' => $row['id_group'],
                'group_name' => $row['group_name'],
                'email' => $row['email_address'],
                'language' => $row['lngfile'],
            );
        }
        $smcFunc['db_free_result']($request);

        // Ensure everyone who is online gets their changes right away.
        updateSettings(array('settings_updated' => time()));

        if (!empty($affected_users))
        {
            require_once($sourcedir . '/Subs-Notify.php');
            $prefs = getNotifyPrefs($members, array('groupr_approved', 'groupr_rejected'), true);

            // They are being approved?
            if ($this->_details['status'] == 'approve')
            {
                $pref_name = 'approved';
                $email_template_name = 'mc_group_approve';
                $email_message_id_prefix = 'grpapp';
            }
            // Otherwise, they are getting rejected (With or without a reason).
            else
            {
                $pref_name = 'rejected';
                $email_template_name = empty($custom_reason) ? 'mc_group_reject' : 'mc_group_reject_reason';
                $email_message_id_prefix = 'grprej';
            }

            // Same as for approving, kind of.
            foreach ($affected_users as $user)
            {
                $pref = !empty($prefs[$user['member_id']]['groupr_' . $pref_name]) ? $prefs[$user['member_id']]['groupr_' . $pref_name] : 0;
                $custom_reason = isset($this->_details['reason']) && isset($this->_details['reason'][$user['rid']]) ? $this->_details['reason'][$user['rid']] : '';

                if ($pref & self::RECEIVE_NOTIFY_ALERT)
                {
                    $alert_rows[] = array(
                        'alert_time' => time(),
                        'id_member' => $user['member_id'],
                        'content_type' => 'groupr',
                        'content_id' => 0,
                        'content_action' => $pref_name,
                        'is_read' => 0,
                        'extra' => $smcFunc['json_encode'](array('group_name' => $user['group_name'], 'reason' => !empty($custom_reason) ? '<br><br>' . $custom_reason : '')),
                    );
                    updateMemberData($user['member_id'], array('alerts' => '+'));
                }

                if ($pref & self::RECEIVE_NOTIFY_EMAIL)
                {
                    // Emails are a bit complicated. We have to do language stuff.
                    require_once($sourcedir . '/Subs-Post.php');
                    require_once($sourcedir . '/ScheduledTasks.php');
                    loadEssentialThemeData();

                    $replacements = array(
                        'USERNAME' => $user['member_name'],
                        'GROUPNAME' => $user['group_name'],
                    );

                    if (!empty($custom_reason))
                        $replacements['REASON'] = $custom_reason;

                    $emaildata = loadEmailTemplate($email_template_name, $replacements, $user['language']);

                    sendmail($user['email'], $emaildata['subject'], $emaildata['body'], null, $email_message_id_prefix . $user['rid'], $emaildata['is_html'], 2);
                }
            }

            // 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;
    }
}

?>