Example #1
0
function sucuriscan_settings_alert_recipients($nonce)
{
    $params = array();
    $params['AlertSettings.Recipients'] = '';
    $notify_to = SucuriScanOption::get_option(':notify_to');
    $emails = array();
    // If the recipient list is not empty, explode.
    if (is_string($notify_to)) {
        $emails = explode(',', $notify_to);
    }
    // Process form submission.
    if ($nonce) {
        // Add new email address to the alert recipient list.
        if (SucuriScanRequest::post(':save_recipient') !== false) {
            $new_email = SucuriScanRequest::post(':recipient');
            if (SucuriScan::is_valid_email($new_email)) {
                $emails[] = $new_email;
                $message = 'Sucuri will send email alerts to: <code>' . $new_email . '</code>';
                SucuriScanOption::update_option(':notify_to', implode(',', $emails));
                SucuriScanEvent::report_info_event($message);
                SucuriScanEvent::notify_event('plugin_change', $message);
                SucuriScanInterface::info($message);
            } else {
                SucuriScanInterface::error('Email format not supported.');
            }
        }
        // Delete one or more recipients from the list.
        if (SucuriScanRequest::post(':delete_recipients') !== false) {
            $deleted_emails = array();
            $recipients = SucuriScanRequest::post(':recipients', '_array');
            foreach ($recipients as $address) {
                if (in_array($address, $emails)) {
                    $deleted_emails[] = $address;
                    $index = array_search($address, $emails);
                    unset($emails[$index]);
                }
            }
            if (!empty($deleted_emails)) {
                $deleted_emails_str = implode(", ", $deleted_emails);
                $message = 'Sucuri will not send email alerts to: <code>' . $deleted_emails_str . '</code>';
                SucuriScanOption::update_option(':notify_to', implode(',', $emails));
                SucuriScanEvent::report_info_event($message);
                SucuriScanEvent::notify_event('plugin_change', $message);
                SucuriScanInterface::info($message);
            }
        }
        // Debug ability of the plugin to send email alerts correctly.
        if (SucuriScanRequest::post(':debug_email')) {
            $recipients = SucuriScanOption::get_option(':notify_to');
            SucuriScanMail::send_mail($recipients, 'Test Email Alert', sprintf('Test email alert sent at %s', date('r')), array('Force' => true));
            SucuriScanInterface::info('Test email alert sent, check your inbox.');
        }
    }
    $counter = 0;
    foreach ($emails as $email) {
        if (!empty($email)) {
            $css_class = $counter % 2 === 0 ? '' : 'alternate';
            $params['AlertSettings.Recipients'] .= SucuriScanTemplate::getSnippet('settings-alert-recipients', array('Recipient.CssClass' => $css_class, 'Recipient.Email' => $email));
            $counter++;
        }
    }
    return SucuriScanTemplate::getSection('settings-alert-recipients', $params);
}