share
8 years ago
.htaccess
11 years ago
anti_malware.php
4 years ago
class-coupon.php
6 months ago
class-email-sodium.php
3 weeks ago
class-firewall-log.php
1 week ago
class-helpers.php
7 months ago
class-import-export.php
3 months ago
class-ip.php
4 months ago
class-nfw-database.php
6 months ago
class-nfw-session.php
1 week ago
class-php-session.php
1 year ago
class-plugin-upgrade.php
1 week ago
class_mail.php
3 weeks ago
event_updates.php
9 months ago
firewall.php
4 months ago
fw_centlog.php
1 week ago
fw_fileguard.php
4 months ago
fw_livelog.php
1 year ago
help.php
3 weeks ago
helpers.php
1 week ago
i18n-extra.php
3 weeks ago
i18n.php
1 year ago
index.html
13 years ago
init_update.php
1 year ago
install.php
1 year ago
install_default.php
6 months ago
loader.php
6 months ago
mail_template_firewall.php
1 year ago
mail_template_plugin.php
1 month ago
scheduled_tasks.php
3 years ago
settings_dashboard.php
1 month ago
settings_dashboard_about.php
3 weeks ago
settings_dashboard_statistics.php
1 month ago
settings_event_notifications.php
1 month ago
settings_events.php
1 month ago
settings_firewall_options.php
1 month ago
settings_firewall_policies.php
1 week ago
settings_login_protection.php
1 month ago
settings_logs.php
1 month ago
settings_logs_firewall_log.php
3 weeks ago
settings_logs_live_log.php
1 month ago
settings_monitoring.php
3 weeks ago
settings_monitoring_file_check.php
1 month ago
settings_monitoring_file_guard.php
1 month ago
settings_network.php
1 month ago
settings_security_rules.php
1 month ago
settings_security_rules_editor.php
1 month ago
settings_security_rules_update.php
1 week ago
sign.pub
7 years ago
thickbox.php
4 years ago
widget.php
3 years ago
wpplus.php
3 months ago
class_mail.php
205 lines
| 1 | <?php |
| 2 | /* |
| 3 | +=====================================================================+ |
| 4 | | _ _ _ _ _____ _ _ _ | |
| 5 | | | \ | (_)_ __ (_) __ _| ___(_)_ __ _____ ____ _| | | | |
| 6 | | | \| | | '_ \ | |/ _` | |_ | | '__/ _ \ \ /\ / / _` | | | | |
| 7 | | | |\ | | | | || | (_| | _| | | | | __/\ V V / (_| | | | | |
| 8 | | |_| \_|_|_| |_|/ |\__,_|_| |_|_| \___| \_/\_/ \__,_|_|_| | |
| 9 | | |__/ | |
| 10 | | (c) NinTechNet Limited ~ https://nintechnet.com/ | |
| 11 | +=====================================================================+ |
| 12 | */ |
| 13 | |
| 14 | if ( class_exists('NinjaFirewall_mail') ) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | class NinjaFirewall_mail { |
| 19 | |
| 20 | private static $template = []; |
| 21 | |
| 22 | /** |
| 23 | * Load the template. |
| 24 | */ |
| 25 | private static function initialize( $what, $dir = '') { |
| 26 | /** |
| 27 | * Merge the default and custom (if any) templates. |
| 28 | */ |
| 29 | if ( $what == 'firewall') { |
| 30 | $file = '/mail_template_firewall.php'; |
| 31 | $logdir = $dir; |
| 32 | } else { |
| 33 | $file = '/mail_template_plugin.php'; |
| 34 | $logdir = NFW_LOG_DIR .'/nfwlog'; |
| 35 | } |
| 36 | |
| 37 | $custom = []; |
| 38 | if ( is_file( __DIR__ . $file ) ) { |
| 39 | $default = require __DIR__ . $file; |
| 40 | } |
| 41 | if ( is_file( "$logdir/$file" ) ) { |
| 42 | $custom = require "$logdir/$file"; |
| 43 | } |
| 44 | self::$template = array_merge( $default, $custom ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Send an email using WordPress wp_mail(). |
| 50 | */ |
| 51 | public static function send( $tpl, $s_values = [], $c_values = [], $headers = '', |
| 52 | $attachment = [], $unsubscribe = 0 ) { |
| 53 | /** |
| 54 | * Initialize the template. |
| 55 | */ |
| 56 | self::initialize('plugin'); |
| 57 | |
| 58 | $nfw_options = nfw_get_option('nfw_options'); |
| 59 | /** |
| 60 | * Retrieve recipient. |
| 61 | */ |
| 62 | if ( is_multisite() && ( $nfw_options['alert_sa_only'] == 2 ) ) { |
| 63 | $recipient = get_option('admin_email'); |
| 64 | $unsubscribe = 0; |
| 65 | } else { |
| 66 | $recipient = $nfw_options['alert_email']; |
| 67 | } |
| 68 | if ( empty( $recipient ) ) { |
| 69 | nfw_log_error( sprintf( |
| 70 | __('Cannot send notification, no valid email found (%s)', 'nfwplus'), |
| 71 | 'alert_email') |
| 72 | ); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $subject = self::$template['subject_line_tag'] .' '. |
| 77 | vsprintf( self::$template[$tpl]['subject'], $s_values ); |
| 78 | $message = vsprintf( self::$template[$tpl]['content'], $c_values ) . |
| 79 | "\n\n". self::$template['signature']; |
| 80 | |
| 81 | /** |
| 82 | * In order to use Sodium, we must have WordPress >=5.2 or PHP >= 7.2.0. |
| 83 | */ |
| 84 | if (! empty( $unsubscribe ) ) { |
| 85 | require_once __DIR__ .'/class-email-sodium.php'; |
| 86 | $unsubscribe = NinjaFirewall_emailsodium::check_sodium(); |
| 87 | |
| 88 | /** |
| 89 | * Link will be valid for 12 hours. |
| 90 | */ |
| 91 | $expire = time() + 60 * 60 * 12; |
| 92 | } |
| 93 | |
| 94 | $admin_email = get_option('admin_email'); |
| 95 | |
| 96 | /** |
| 97 | * Look for all recipients. |
| 98 | */ |
| 99 | $recipients = explode(',', $recipient ); |
| 100 | |
| 101 | foreach( $recipients as $to ) { |
| 102 | $to = trim( $to ); |
| 103 | /** |
| 104 | * Add an unsubscribe link if required. |
| 105 | */ |
| 106 | $click = ''; |
| 107 | if (! empty( $unsubscribe ) ) { |
| 108 | /** |
| 109 | * Must no be the admin email, because we can't remove it. |
| 110 | */ |
| 111 | if ( $to != $admin_email ) { |
| 112 | $link = NinjaFirewall_emailsodium::sodium_encrypt( $to, $expire, $unsubscribe ); |
| 113 | $uri = home_url('/') ."?nfw_stop_notification=$link"; |
| 114 | $click = "\n\n". sprintf( |
| 115 | /* Translators: unsubscribe link */ |
| 116 | __("If you don't have access to that site any longer, you can remove your email by clicking the following link (valid for 12 hours): %s", 'ninjafirewall'), |
| 117 | $uri |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | $res = wp_mail( $to, $subject, $message . $click, $headers, $attachment ); |
| 123 | if ( $res === false ) { |
| 124 | nfw_log_error( sprintf( |
| 125 | /* Translators: 1=subject, 2=recipient */ |
| 126 | __('Cannot send email "%1$s" to recipient "%2$s"', 'ninjafirewall'), |
| 127 | $subject, $to |
| 128 | ) ); |
| 129 | } |
| 130 | /** |
| 131 | * Delete attachment. |
| 132 | */ |
| 133 | if ( $attachment && is_file( $attachment ) ) { |
| 134 | unlink( $attachment ); |
| 135 | } |
| 136 | } |
| 137 | return $res; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Send an email using PHP mail(). |
| 143 | * Used by the firewall part that loads before WordPress. |
| 144 | * Note: multiple comma-separated email addresses can be present in the "To:" field. |
| 145 | */ |
| 146 | public static function PHPsend( $to, $tpl, $s_values = [], $c_values = [], |
| 147 | $logdir = '', $headers = '', $attachment = '', $attachment_name = '') { |
| 148 | |
| 149 | if (! function_exists('mail') ) { |
| 150 | /** |
| 151 | * There's nothing we can do here. |
| 152 | */ |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Initialize the template. |
| 158 | */ |
| 159 | self::initialize('firewall', $logdir); |
| 160 | |
| 161 | $subject = self::$template['subject_line_tag'] .' '. |
| 162 | vsprintf( self::$template[$tpl]['subject'], $s_values ); |
| 163 | $message = vsprintf( self::$template[$tpl]['content'], $c_values ) . |
| 164 | "\n\n". self::$template['signature']; |
| 165 | |
| 166 | /** |
| 167 | * Multipart mime for attachments. |
| 168 | */ |
| 169 | if (! empty( $attachment ) ) { |
| 170 | $random_hash = md5( date('r', time() ) ); |
| 171 | $headers .= "MIME-Version: 1.0\r\n"; |
| 172 | $headers .= "Content-Type: multipart/mixed; boundary=\"NFWP-mixed-{$random_hash}\"\r\n"; |
| 173 | |
| 174 | $body = '--NFWP-mixed-' . $random_hash . "\n" . |
| 175 | 'Content-Type: multipart/alternative; boundary="NFWP-alt-' . $random_hash . '"' . "\n\n" . |
| 176 | '--NFWP-alt-' . $random_hash . "\n" . |
| 177 | 'Content-Type: text/plain; charset="UTF-8"'. "\n" . |
| 178 | 'Content-Transfer-Encoding: 7bit'. "\n\n" . |
| 179 | $message ."\n". |
| 180 | '--NFWP-alt-' . $random_hash . '--'. "\n\n\n" . |
| 181 | '--NFWP-mixed-' . $random_hash . "\n" . |
| 182 | 'Content-Type: text/plain; name="'. $attachment_name .'"'. "\n" . |
| 183 | 'Content-Transfer-Encoding: base64' . "\n" . |
| 184 | 'Content-Disposition: attachment' . "\n\n" . |
| 185 | chunk_split( base64_encode( $attachment ) ) . "\n" . |
| 186 | '--NFWP-mixed-' . $random_hash . '--' . "\n\n"; |
| 187 | |
| 188 | return mail( $to, $subject, $body, $headers ); |
| 189 | /** |
| 190 | * No attachment. |
| 191 | */ |
| 192 | } else { |
| 193 | $headers .= "Content-Transfer-Encoding: 7bit\r\n"; |
| 194 | $headers .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n"; |
| 195 | $headers .= "MIME-Version: 1.0\r\n"; |
| 196 | |
| 197 | return mail( $to, $subject, $message, $headers ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | // ===================================================================== |
| 204 | // EOF |
| 205 |