PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.36
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.36
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / Form_Builder / helpers / Send_Email.php

Send_Email.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.36, at includes/widgets/Form_Builder/helpers/Send_Email.php

347 lines 12.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons;
4
5 use King_Addons\Core;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 class Send_Email
12 {
13
14 public function __construct()
15 {
16 add_action('wp_ajax_king_addons_form_builder_email', [$this, 'send_email']);
17 add_action('wp_ajax_nopriv_king_addons_form_builder_email', [$this, 'send_email']);
18 }
19
20 public function send_email()
21 {
22
23 $nonce = $_POST['nonce'];
24
25 if (!wp_verify_nonce($nonce, 'king-addons-js')) {
26 return;
27 }
28
29 $message_body = [];
30
31 // Security fix: Validate and sanitize form_content array
32 $form_content = isset($_POST['form_content']) && is_array($_POST['form_content']) ? $_POST['form_content'] : [];
33
34 foreach ($form_content as $field) {
35 if (!is_array($field) || count($field) < 2) {
36 continue; // Skip malformed fields
37 }
38
39 if ($field[0] === 'email') {
40 if (!is_email(sanitize_email($field[1]))) {
41 wp_send_json_error(array(
42 'action' => 'king_addons_form_builder_email',
43 'message' => esc_html__('Email provided is invalid', 'king-addons'),
44 'status' => 'error'
45 ));
46 }
47 }
48 }
49
50
51 $content_type = get_option('king_addons_email_content_type_' . $_POST['king_addons_form_id']);
52
53 $line_break = 'html' === $content_type ? '<br>' : "\n";
54
55 $email_fields = trim(get_option('king_addons_email_fields_' . $_POST['king_addons_form_id']));
56
57 if ($email_fields === '[all-fields]' || str_contains($email_fields, '[all-fields]')) {
58
59
60 $replace_shortcode_with_value = function ($matches) use ($form_content) {
61 $field_id = sanitize_text_field($matches[1]);
62 foreach ($form_content as $key => $value) {
63 $key_parts = explode('-', $key);
64 $last_part = end($key_parts);
65 if ($last_part === $field_id) {
66 // Security fix: Sanitize form field values before using in email
67 return is_array($value[1]) ? implode("\n", array_map('sanitize_text_field', $value[1])) : sanitize_text_field($value[1]);
68 }
69 }
70 return '';
71 };
72
73
74 $all_fields_content = [];
75
76 foreach ($form_content as $key => $value) {
77 if (!is_array($value) || count($value) < 3) {
78 continue; // Skip malformed fields
79 }
80 // Security fix: Sanitize all field data before using in email
81 $field_label = sanitize_text_field($value[2]);
82 $field_value = is_array($value[1]) ? implode("\n", array_map('sanitize_text_field', $value[1])) : sanitize_text_field($value[1]);
83 $all_fields_content[] = $field_label . ': ' . $field_value;
84 }
85 $all_fields_content = implode("\n", $all_fields_content);
86
87
88 $processed_message = str_replace('[all-fields]', $all_fields_content, $email_fields);
89
90 $processed_message = preg_replace_callback(
91 '/\[id="([^"]+)"\]/',
92 $replace_shortcode_with_value,
93 $processed_message
94 );
95 } else {
96
97
98 $replace_shortcode_with_value = function ($matches) use ($form_content) {
99 $field_id = sanitize_text_field($matches[1]);
100 foreach ($form_content as $key => $value) {
101 if (!is_array($value) || count($value) < 3) {
102 continue; // Skip malformed fields
103 }
104 $key_parts = explode('-', $key);
105 $last_part = end($key_parts);
106 if ($last_part === $field_id) {
107 // Security fix: Sanitize form field data
108 $field_label = sanitize_text_field($value[2]);
109 $field_value = is_array($value[1]) ? implode("\n", array_map('sanitize_text_field', $value[1])) : sanitize_text_field($value[1]);
110 return $field_label . ': ' . $field_value;
111 }
112 }
113 return '';
114 };
115
116
117 $processed_message = preg_replace_callback(
118 '/\[id="([^"]+)"\]/',
119 $replace_shortcode_with_value,
120 $email_fields
121 );
122 }
123
124 $meta_keys = get_option('king_addons_meta_keys_' . $_POST['king_addons_form_id']);
125 $meta_fields = [];
126
127 foreach ($meta_keys as $metadata_type) {
128 switch ($metadata_type) {
129 case 'date':
130 $meta_fields['date'] = [
131 'title' => esc_html__('Date', 'king-addons'),
132 'value' => date_i18n(get_option('date_format')),
133 ];
134 break;
135
136 case 'time':
137 $meta_fields['time'] = [
138 'title' => esc_html__('Time', 'king-addons'),
139 'value' => date_i18n(get_option('time_format')),
140 ];
141 break;
142
143 case 'page_url':
144 $meta_fields['page_url'] = [
145 'title' => esc_html__('Page URL', 'king-addons'),
146
147 'value' => get_option('king_addons_referrer_' . $_POST['king_addons_form_id']) ? get_option('king_addons_referrer_' . $_POST['king_addons_form_id']) : '',
148 ];
149 break;
150
151 case 'page_title':
152 $meta_fields['page_title'] = [
153 'title' => esc_html__('Page Title', 'king-addons'),
154
155 'value' => get_option('king_addons_referrer_title_' . $_POST['king_addons_form_id']) ? get_option('king_addons_referrer_title_' . $_POST['king_addons_form_id']) : '',
156 ];
157 break;
158
159 case 'user_agent':
160 $meta_fields['user_agent'] = [
161 'title' => esc_html__('User Agent', 'king-addons'),
162 'value' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_textarea_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : '',
163 ];
164 break;
165
166 case 'remote_ip':
167 $meta_fields['remote_ip'] = [
168 'title' => esc_html__('Remote IP', 'king-addons'),
169 'value' => Core::getClientIP(),
170 ];
171 break;
172
173 case 'credit':
174 $meta_fields['credit'] = [
175 'title' => esc_html__('Powered by', 'king-addons'),
176 'value' => esc_html__('King Addons', 'king-addons'),
177 ];
178 break;
179 }
180 }
181
182 $email_meta = [];
183
184 foreach ($meta_fields as $key => $value) {
185 $email_meta[] = $value['title'] . ': ' . $value['value'];
186 }
187
188 $to = get_option('king_addons_email_to_' . $_POST['king_addons_form_id']);
189
190 $to = preg_replace_callback(
191 '/\[id="(\w+)"\]/',
192 function ($matches) {
193 return $this->get_field_value($matches[1]);
194 },
195 $to
196 );
197
198 $subject = get_option('king_addons_email_subject_' . $_POST['king_addons_form_id']);
199
200 $subject = preg_replace_callback(
201 '/\[id="(\w+)"\]/',
202 function ($matches) {
203 return $this->get_field_value($matches[1]);
204 },
205 $subject
206 );
207
208 if ($processed_message) {
209 $message_body[] = $processed_message;
210 }
211
212
213 if ($content_type === 'html') {
214
215 foreach ($message_body as &$item) {
216 $item = nl2br($item);
217 }
218 unset($item);
219 }
220
221 $body = implode($line_break, $message_body) . $line_break . '-----' . $line_break . implode($line_break, $email_meta);
222
223 $cc_header = '';
224 if (!empty(get_option('king_addons_cc_header_' . $_POST['king_addons_form_id']))) {
225 $cc_header = 'Cc: ' . get_option('king_addons_cc_header_' . $_POST['king_addons_form_id']);
226
227 $cc_header = preg_replace_callback(
228 '/\[id="(\w+)"\]/',
229 function ($matches) {
230 return $this->get_field_value($matches[1]);
231 },
232 $cc_header
233 );
234 }
235
236 $bcc_header = '';
237 if (!empty(get_option('king_addons_bcc_header_' . $_POST['king_addons_form_id']))) {
238 $bcc_header = 'Bcc: ' . get_option('king_addons_bcc_header_' . $_POST['king_addons_form_id']);
239
240 $bcc_header = preg_replace_callback(
241 '/\[id="([^\"]+)"\]/',
242 function ($matches) {
243 return $this->get_field_value($matches[1]);
244 },
245 $bcc_header
246 );
247 }
248
249 // Initialize reply-to and email-from variables to avoid undefined variable warnings
250 $reply_to_address = '';
251 $email_from_name = '';
252 $email_from_mail = '';
253 $reply_to = '';
254
255 if (!empty(get_option('king_addons_reply_to_' . $_POST['king_addons_form_id'])) && !empty(get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id'])) && !empty(get_option('king_addons_email_from_' . $_POST['king_addons_form_id']))) {
256
257 preg_match_all('/id="([^"]+)"/', get_option('king_addons_reply_to_' . $_POST['king_addons_form_id']), $matche);
258 $reply_to_field_id = $matche[1];
259
260 preg_match_all('/id="([^"]+)"/', get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id']), $matche);
261 $email_from_name_field_id = $matche[1];
262
263 preg_match_all('/id="([^"]+)"/', get_option('king_addons_email_from_' . $_POST['king_addons_form_id']), $matche);
264 $email_from_field_id = $matche[1];
265
266 foreach ($form_content as $key => $value) {
267 if (!is_array($value) || count($value) < 2) {
268 continue; // Skip malformed fields
269 }
270
271 $key_parts = explode('-', $key);
272 $last_part = end($key_parts);
273
274 if (in_array($last_part, $reply_to_field_id)) {
275 $reply_to_address = sanitize_email($value[1]);
276 }
277
278 if (in_array($last_part, $email_from_name_field_id)) {
279 $email_from_name = sanitize_text_field($value[1]);
280 }
281
282 if (in_array($last_part, $email_from_field_id)) {
283 $email_from_mail = sanitize_email($value[1]);
284 }
285 }
286
287 if (!$reply_to_address) {
288 $reply_to_address = get_option('king_addons_reply_to_' . $_POST['king_addons_form_id']);
289 }
290
291 if (!$email_from_name) {
292 $email_from_name = get_option('king_addons_email_from_name_' . $_POST['king_addons_form_id']);
293 }
294
295 if (!$email_from_mail) {
296 $email_from_mail = get_option('king_addons_email_from_' . $_POST['king_addons_form_id']);
297 }
298
299 $reply_to = 'Reply-To: ' . $reply_to_address;
300 }
301
302 $email_from = sprintf('From: %s <%s>' . "\r\n", $email_from_name, $email_from_mail);
303
304 $headers = array('Content-Type: text/' . $content_type . '; charset=UTF-8', $email_from, $cc_header, $bcc_header, $reply_to);
305
306
307 $sent = wp_mail($to, $subject, $body, $headers);
308
309 if ($sent) {
310 wp_send_json_success(array(
311 'action' => 'king_addons_form_builder_email',
312 'message' => esc_html__('Message sent successfully', 'king-addons'),
313 'status' => 'success'
314 // Security fix: Removed potentially unsafe details from response
315 ));
316 } else {
317 wp_send_json_error(array(
318 'action' => 'king_addons_form_builder_email',
319 'message' => esc_html__('Message could not be sent', 'king-addons'),
320 'status' => 'error'
321 // Security fix: Removed potentially unsafe details from response
322 ));
323 }
324 }
325
326 public function get_field_value($field_id)
327 {
328 // Security fix: Use sanitized form_content instead of $_POST directly
329 $form_content = isset($_POST['form_content']) && is_array($_POST['form_content']) ? $_POST['form_content'] : [];
330
331 foreach ($form_content as $key => $field) {
332 if (!is_array($field) || count($field) < 2) {
333 continue; // Skip malformed fields
334 }
335
336 $key_parts = explode('-', $key);
337 $last_part = end($key_parts);
338
339 if ($last_part === $field_id) {
340 return sanitize_text_field($field[1]);
341 }
342 }
343 return '';
344 }
345 }
346
347 new Send_Email();