PluginProbe ʕ •ᴥ•ʔ
NinjaFirewall (WP Edition) – Advanced Security Plugin and Firewall / 4.8.8
NinjaFirewall (WP Edition) – Advanced Security Plugin and Firewall v4.8.8
4.9 4.8.8 4.8.7 4.8.6 trunk 4.5 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6 4.6.1 4.7 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.8 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5
ninjafirewall / lib / class_mail.php
ninjafirewall / lib Last commit date
share 9 years ago .htaccess 11 years ago anti_malware.php 5 years ago class-coupon.php 7 months ago class-email-sodium.php 1 month ago class-firewall-log.php 1 month ago class-helpers.php 9 months ago class-import-export.php 5 months ago class-ip.php 5 months ago class-nfw-database.php 7 months ago class-nfw-session.php 1 month ago class-php-session.php 1 year ago class-plugin-upgrade.php 1 month ago class_mail.php 1 month ago event_updates.php 11 months ago firewall.php 5 months ago fw_centlog.php 1 month ago fw_fileguard.php 5 months ago fw_livelog.php 1 year ago help.php 1 month ago helpers.php 1 month ago i18n-extra.php 1 month ago i18n.php 1 year ago index.html 13 years ago init_update.php 2 years ago install.php 1 year ago install_default.php 7 months ago loader.php 7 months ago mail_template_firewall.php 1 year ago mail_template_plugin.php 2 months ago scheduled_tasks.php 3 years ago settings_dashboard.php 2 months ago settings_dashboard_about.php 1 month ago settings_dashboard_statistics.php 2 months ago settings_event_notifications.php 2 months ago settings_events.php 2 months ago settings_firewall_options.php 2 months ago settings_firewall_policies.php 1 month ago settings_login_protection.php 2 months ago settings_logs.php 2 months ago settings_logs_firewall_log.php 1 month ago settings_logs_live_log.php 2 months ago settings_monitoring.php 1 month ago settings_monitoring_file_check.php 2 months ago settings_monitoring_file_guard.php 2 months ago settings_network.php 2 months ago settings_security_rules.php 2 months ago settings_security_rules_editor.php 2 months ago settings_security_rules_update.php 1 month ago sign.pub 7 years ago thickbox.php 4 years ago widget.php 3 years ago wpplus.php 5 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