PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 2.0.15
Check & Log Email – Easy Email Testing & Mail logging v2.0.15
2.0.15 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.13.1 2.0.13.2 2.0.14 2.0.2 2.0.3 2.0.4 2.0.5 2.0.5.1 2.0.6 2.0.7 2.0.8 2.0.9 trunk 0.5.7 0.6.0 0.6.1 0.6.2 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.12.1 1.0.13 1.0.13.1 1.0.2 1.0.3
check-email / include / helper-function.php
check-email / include Last commit date
Core 4 days ago Util 4 days ago Check_Email_Encode_Tab.php 4 days ago Check_Email_Notify_Tab.php 4 days ago Check_Email_SMTP_Tab.php 4 days ago class-check-email-header-parser.php 4 days ago class-check-email-log-autoloader.php 4 days ago class-check-email-newsletter.php 4 days ago deactivate-feedback.php 4 days ago helper-function.php 4 days ago install.php 4 days ago
helper-function.php
1328 lines
1 <?php
2
3 /**
4 * Helper Functions
5 *
6 * @package check-mail
7 * @subpackage Helper/Templates
8 * @copyright Copyright (c) 2016, René Hermenau
9 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10 * @since 1.4.0
11 */
12 // Exit if accessed directly
13 if( !defined( 'ABSPATH' ) )
14 exit;
15
16 /**
17 * Helper method to check if user is in the plugins page.
18 *
19 * @author René Hermenau
20 * @since 1.4.0
21 *
22 * @return bool
23 */
24
25 /**
26 * display deactivation logic on plugins page
27 *
28 * @since 1.4.0
29 */
30 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
31 function ck_mail_is_plugins_page() {
32
33 if(function_exists('get_current_screen')){
34 $screen = get_current_screen();
35 if(is_object($screen)){
36 if($screen->id == 'plugins' || $screen->id == 'plugins-network'){
37 return true;
38 }
39 }
40 }
41 return false;
42 }
43
44 add_filter('admin_footer', 'ck_mail_add_deactivation_feedback_modal');
45 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
46 function ck_mail_add_deactivation_feedback_modal() {
47
48 if( is_admin() && ck_mail_is_plugins_page() ) {
49
50 require_once CK_MAIL_PATH ."/include/deactivate-feedback.php";
51 }
52
53 }
54
55 /**
56 * send feedback via email
57 *
58 * @since 1.4.0
59 */
60 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
61 function ck_mail_send_feedback() {
62 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: in form variable.
63 if( isset( $_POST['data'] ) ) {
64 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: in form variable.
65 parse_str( sanitize_text_field( wp_unslash($_POST['data'])), $form );
66 }
67
68 if( !isset( $form['ck_mail_security_nonce'] ) || isset( $form['ck_mail_security_nonce'] ) && !wp_verify_nonce( sanitize_text_field( $form['ck_mail_security_nonce'] ), 'ck_mail_ajax_check_nonce' ) ) {
69 echo esc_html__('security_nonce_not_verified', 'check-email');
70 die();
71 }
72 if ( !current_user_can( 'manage_options' ) ) {
73 die();
74 }
75
76 $text = '';
77 if( isset( $form['ck_mail_disable_text'] ) ) {
78 if (is_array($form['ck_mail_disable_text'])) {
79 $text = implode( " ", $form['ck_mail_disable_text'] );
80 }
81 }
82
83 $headers = array();
84
85 $from = isset( $form['ck_mail_disable_from'] ) ? $form['ck_mail_disable_from'] : '';
86 if( $from ) {
87 $headers[] = "From: $from";
88 $headers[] = "Reply-To: $from";
89 }
90
91 $subject = isset( $form['ck_mail_disable_reason'] ) ? $form['ck_mail_disable_reason'] : '(no reason given)';
92
93 if($subject == 'technical issue'){
94
95 $subject = 'Check & Log Email '.$subject;
96 $text = trim($text);
97
98 if(!empty($text)){
99
100 $text = 'technical issue description: '.$text;
101
102 }else{
103
104 $text = 'no description: '.$text;
105 }
106
107 }else{
108 $subject = 'Check & Log Email';
109 }
110
111 $success = wp_mail( 'team@magazine3.in', $subject, $text, $headers );
112
113 echo 'sent';
114 die();
115 }
116 add_action( 'wp_ajax_ck_mail_send_feedback', 'ck_mail_send_feedback' );
117
118 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
119 function ck_mail_enqueue_makebetter_email_js() {
120
121 if ( is_admin() && ck_mail_is_plugins_page() ) {
122
123 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
124
125 wp_register_script( 'ck_mail_make_better_js', CK_MAIL_URL . 'assets/js/admin/feedback'. $suffix .'.js', array( 'jquery' ), CK_MAIL_VERSION, true);
126 $data = array(
127 'ajax_url' => admin_url( 'admin-ajax.php' ),
128 'ck_mail_security_nonce' => wp_create_nonce('ck_mail_ajax_check_nonce'),
129 );
130 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
131 $data = apply_filters( 'ck_mail_localize_filter', $data, 'eztoc_admin_data' );
132
133 wp_localize_script( 'ck_mail_make_better_js', 'cn_ck_mail_admin_data', $data );
134 wp_enqueue_script( 'ck_mail_make_better_js' );
135 wp_enqueue_style( 'ck_mail_make_better_css', CK_MAIL_URL . 'assets/css/admin/feedback'. $suffix .'.css', array(), CK_MAIL_VERSION );
136
137 }
138
139 }
140 add_action( 'admin_enqueue_scripts', 'ck_mail_enqueue_makebetter_email_js' );
141
142
143 add_action('wp_ajax_ck_mail_subscribe_newsletter','ck_mail_subscribe_for_newsletter');
144 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
145 function ck_mail_subscribe_for_newsletter() {
146 if ( ! isset( $_POST['ck_mail_security_nonce'] ) ){
147 echo esc_html__('security_nonce_not_verified', 'check-email');
148 die();
149 }
150 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ck_mail_security_nonce'] ) ), 'ck_mail_ajax_check_nonce' ) ) {
151 echo esc_html__('security_nonce_not_verified', 'check-email');
152 die();
153 }
154 if ( !current_user_can( 'manage_options' ) ) {
155 die();
156 }
157 if (isset( $_POST['name'] ) && isset( $_POST['email'] ) && isset( $_POST['website'] )) {
158 $api_url = 'http://magazine3.company/wp-json/api/central/email/subscribe';
159
160 $api_params = array(
161 'name' => sanitize_text_field(wp_unslash($_POST['name'])),
162 'email'=> sanitize_email(wp_unslash($_POST['email'])),
163 'website'=> sanitize_text_field(wp_unslash($_POST['website'])),
164 'type'=> 'checkmail'
165 );
166 wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
167 }
168 wp_die();
169 }
170 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
171 function ck_mail_forward_mail($atts) {
172 if ( isset( $atts['to'] ) ) {
173 $to = $atts['to'];
174 if ( ! is_array( $to ) ) {
175 $to = explode( ',', $to );
176 }
177 }
178
179
180 if ( isset( $atts['subject'] ) ) {
181 $subject = $atts['subject'];
182 }
183
184 if ( isset( $atts['message'] ) ) {
185 $message = $atts['message'];
186 }
187
188 if ( isset( $atts['headers'] ) ) {
189 $headers = $atts['headers'];
190 }
191
192 if ( isset( $atts['attachments'] ) ) {
193 $attachments = $atts['attachments'];
194 }
195
196
197 $subject = esc_html__('Forward Email Check & Log ', 'check-email').$subject;
198
199 if ( ! is_array( $attachments ) ) {
200 $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
201 }
202 global $phpmailer;
203 if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
204 require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
205 require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
206 require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
207 $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );
208
209 $phpmailer::$validator = static function ( $email ) {
210 return (bool) is_email( $email );
211 };
212 }
213
214 // Headers.
215 $cc = array();
216 $bcc = array();
217 $reply_to = array();
218
219 if ( empty( $headers ) ) {
220 $headers = array();
221 } else {
222 if ( ! is_array( $headers ) ) {
223 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
224 } else {
225 $tempheaders = $headers;
226 }
227 $headers = array();
228
229 // If it's actually got contents.
230 if ( ! empty( $tempheaders ) ) {
231 // Iterate through the raw headers.
232 foreach ( (array) $tempheaders as $header ) {
233 // phpcs:ignore wp_function_not_compatible_with_requires_wp
234 if ( ! str_contains( $header, ':' ) ) {
235 if ( false !== stripos( $header, 'boundary=' ) ) {
236 $parts = preg_split( '/boundary=/i', trim( $header ) );
237 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
238 }
239 continue;
240 }
241 // Explode them out.
242 list( $name, $content ) = explode( ':', trim( $header ), 2 );
243
244 // Cleanup crew.
245 $name = trim( $name );
246 $content = trim( $content );
247
248 switch ( strtolower( $name ) ) {
249 // Mainly for legacy -- process a "From:" header if it's there.
250 case 'from':
251 $bracket_pos = strpos( $content, '<' );
252 if ( false !== $bracket_pos ) {
253 // Text before the bracketed email is the "From" name.
254 if ( $bracket_pos > 0 ) {
255 $from_name = substr( $content, 0, $bracket_pos );
256 $from_name = str_replace( '"', '', $from_name );
257 $from_name = trim( $from_name );
258 }
259
260 $from_email = substr( $content, $bracket_pos + 1 );
261 $from_email = str_replace( '>', '', $from_email );
262 $from_email = trim( $from_email );
263
264 // Avoid setting an empty $from_email.
265 } elseif ( '' !== trim( $content ) ) {
266 $from_email = trim( $content );
267 }
268 break;
269 case 'content-type':
270 // phpcs:ignore wp_function_not_compatible_with_requires_wp
271 if ( str_contains( $content, ';' ) ) {
272 list( $type, $charset_content ) = explode( ';', $content );
273 $content_type = trim( $type );
274 if ( false !== stripos( $charset_content, 'charset=' ) ) {
275 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
276 } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
277 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
278 $charset = '';
279 }
280
281 // Avoid setting an empty $content_type.
282 } elseif ( '' !== trim( $content ) ) {
283 $content_type = trim( $content );
284 }
285 break;
286 case 'cc':
287 $cc = array_merge( (array) $cc, explode( ',', $content ) );
288 break;
289 case 'bcc':
290 $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
291 break;
292 case 'reply-to':
293 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
294 break;
295 default:
296 // Add it to our grand headers array.
297 $headers[ trim( $name ) ] = trim( $content );
298 break;
299 }
300 }
301 }
302 }
303
304 // Empty out the values that may be set.
305 $phpmailer->clearAllRecipients();
306 $phpmailer->clearAttachments();
307 $phpmailer->clearCustomHeaders();
308 $phpmailer->clearReplyTos();
309 $phpmailer->Body = '';
310 $phpmailer->AltBody = '';
311
312 // Set "From" name and email.
313
314 // If we don't have a name from the input headers.
315 if ( ! isset( $from_name ) ) {
316 $from_name = 'WordPress';
317 }
318 if ( ! isset( $from_email ) ) {
319 // Get the site domain and get rid of www.
320 $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
321 $from_email = 'wordpress@';
322
323 if ( null !== $sitename ) {
324 // phpcs:ignore wp_function_not_compatible_with_requires_wp
325 if ( str_starts_with( $sitename, 'www.' ) ) {
326 $sitename = substr( $sitename, 4 );
327 }
328
329 $from_email .= $sitename;
330 }
331 }
332
333 try {
334 $phpmailer->setFrom( $from_email, $from_name, false );
335 } catch ( PHPMailer\PHPMailer\Exception $e ) {
336 // error_log(esc_html__('Error in forwar email check & log : ', 'check-email').$e->getMessage());
337 return false;
338 }
339
340 // Set mail's subject and body.
341 $phpmailer->Subject = $subject;
342 $phpmailer->Body = $message;
343
344 // Set destination addresses, using appropriate methods for handling addresses.
345 $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
346
347 foreach ( $address_headers as $address_header => $addresses ) {
348 if ( empty( $addresses ) ) {
349 continue;
350 }
351
352 foreach ( (array) $addresses as $address ) {
353 try {
354 // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
355 $recipient_name = '';
356
357 if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
358 if ( count( $matches ) === 3 ) {
359 $recipient_name = $matches[1];
360 $address = $matches[2];
361 }
362 }
363
364 switch ( $address_header ) {
365 case 'to':
366 $phpmailer->addAddress( $address, $recipient_name );
367 break;
368 case 'cc':
369 $phpmailer->addCc( $address, $recipient_name );
370 break;
371 case 'bcc':
372 $phpmailer->addBcc( $address, $recipient_name );
373 break;
374 case 'reply_to':
375 $phpmailer->addReplyTo( $address, $recipient_name );
376 break;
377 }
378 } catch ( PHPMailer\PHPMailer\Exception $e ) {
379 continue;
380 }
381 }
382 }
383
384 // Set to use PHP's mail().
385 $phpmailer->isMail();
386
387 // Set Content-Type and charset.
388
389 // If we don't have a Content-Type from the input headers.
390 if ( ! isset( $content_type ) ) {
391 $content_type = 'text/html';
392 }
393
394 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
395 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
396
397 $phpmailer->ContentType = $content_type;
398
399 // Set whether it's plaintext, depending on $content_type.
400 if ( 'text/html' === $content_type ) {
401 $phpmailer->isHTML( true );
402 }
403
404 // If we don't have a charset from the input headers.
405 if ( ! isset( $charset ) ) {
406 $charset = get_bloginfo( 'charset' );
407 }
408
409 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
410 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
411
412 // Set custom headers.
413 if ( ! empty( $headers ) ) {
414 foreach ( (array) $headers as $name => $content ) {
415 // Only add custom headers not added automatically by PHPMailer.
416 if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) {
417 try {
418 $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
419 } catch ( PHPMailer\PHPMailer\Exception $e ) {
420 continue;
421 }
422 }
423 }
424
425 if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) {
426 $phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) );
427 }
428 }
429
430 if ( ! empty( $attachments ) ) {
431 foreach ( $attachments as $filename => $attachment ) {
432 $filename = is_string( $filename ) ? $filename : '';
433
434 try {
435 $phpmailer->addAttachment( $attachment, $filename );
436 } catch ( PHPMailer\PHPMailer\Exception $e ) {
437 continue;
438 }
439 }
440 }
441
442 /**
443 * Fires after PHPMailer is initialized.
444 *
445 * @since 2.2.0
446 *
447 * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
448 */
449 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
450 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
451
452 $mail_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
453
454 // Send!
455 try {
456 $send = $phpmailer->send();
457 return $send;
458 } catch ( PHPMailer\PHPMailer\Exception $e ) {
459 // error_log(esc_html__('Error in forwar email send check & log : ', 'check-email').$e->getMessage());
460 return false;
461 }
462 }
463 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
464 function ck_mail_create_error_logs() {
465
466 global $wpdb;
467
468 $table_name = $wpdb->prefix . 'check_email_error_logs';
469 $charset_collate = $wpdb->get_charset_collate();
470 // phpcs:disable.
471 if ( $wpdb->get_var( $wpdb->prepare( "show tables like %s",$wpdb->esc_like( $table_name )) ) != $table_name ) {
472
473 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
474 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
475 `check_email_log_id` INT DEFAULT NULL,
476 `content` TEXT DEFAULT NULL,
477 `initiator` TEXT DEFAULT NULL,
478 `event_type` TINYINT UNSIGNED NOT NULL DEFAULT '0',
479 `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
480 PRIMARY KEY (id)
481 )
482 ENGINE='InnoDB'
483 {$charset_collate};";
484
485 $wpdb->query($sql);
486 }
487 // phpcs:enable.
488 }
489 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
490 function ck_mail_create_spam_analyzer_table() {
491
492 global $wpdb;
493
494 $table_name = $wpdb->prefix . 'check_email_spam_analyzer';
495 $charset_collate = $wpdb->get_charset_collate();
496 // phpcs:disable.
497 if ( $wpdb->get_var( $wpdb->prepare( "show tables like %s",$wpdb->esc_like( $table_name )) ) != $table_name ) {
498
499 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
500 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
501 `html_content` LONGTEXT DEFAULT NULL,
502 `spam_assassin` LONGTEXT DEFAULT NULL,
503 `authenticated` LONGTEXT DEFAULT NULL,
504 `block_listed` TEXT DEFAULT NULL,
505 `broken_links` TEXT DEFAULT NULL,
506 `final_score` TEXT DEFAULT NULL,
507 `test_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
508 `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
509 PRIMARY KEY (`id`)
510 )
511 ENGINE='InnoDB'
512 {$charset_collate};";
513
514 $wpdb->query($sql);
515 }
516 // phpcs:enable.
517 }
518 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
519 function ck_mail_insert_spam_analyzer($data_to_insert) {
520
521 global $wpdb;
522
523 $table_name = $wpdb->prefix . 'check_email_spam_analyzer';
524 $wpdb->insert( $table_name, $data_to_insert ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
525 }
526 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
527 function ck_mail_insert_error_logs($data_to_insert) {
528
529 global $wpdb;
530
531 $table_name = $wpdb->prefix . 'check_email_error_logs';
532 $wpdb->insert( $table_name, $data_to_insert ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
533 }
534 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
535 function ck_mail_local_file_get_contents($file_path){
536
537 // Include WordPress Filesystem API
538 if ( ! function_exists( 'WP_Filesystem' ) ) {
539 require_once( ABSPATH . 'wp-admin/includes/file.php' );
540 }
541
542 // Initialize the API
543 global $wp_filesystem;
544 if ( ! WP_Filesystem() ) {
545 return false;
546 }
547 // Check if the file exists
548 if ( $wp_filesystem->exists( $file_path ) ) {
549 // Read the file content
550 $file_content = $wp_filesystem->get_contents( $file_path );
551 return $file_content;
552 } else {
553 return false;
554 }
555
556 }
557 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
558 function ck_mail_update_network_settings() {
559 // Check nonce
560 check_ajax_referer( 'ck_mail_ajax_check_nonce', 'nonce' );
561
562 // Check if user is allowed to manage network options
563 if ( ! current_user_can( 'manage_check_email' ) ) {
564 wp_send_json_error(esc_html__('Unauthorized user', 'check-email') );
565 return;
566 }
567 if ( isset( $_POST['check-email-log-global'] ) ) {
568 $all_fields = array_map('sanitize_text_field', wp_unslash($_POST['check-email-log-global']));
569
570 // Sanitize all the key
571 if ( ! empty( $all_fields ) ) {
572 foreach ($all_fields as $key => $value) {
573 $all_fields[sanitize_key( $key ) ] = sanitize_text_field( $value );
574 }
575 if ( isset( $all_fields['smtp_username'] ) ) {
576 $all_fields['smtp_username'] = base64_encode( $all_fields['smtp_username'] );
577 }
578 if ( isset( $all_fields['smtp_password'] ) ) {
579 $all_fields['smtp_password'] = base64_encode( $all_fields['smtp_password'] );
580 }
581 $all_fields['enable_smtp'] = 1;
582
583 if (!isset($all_fields['enable_global'])) {
584 $all_fields['enable_global'] = 0;
585 }
586 $old_settings = get_site_option('check-email-log-global-smtp');
587
588 if ( ! empty( $old_settings ) && is_array( $old_settings ) ) {
589 $updated_settings = array_merge( $old_settings, $all_fields );
590 } else {
591 $updated_settings = $all_fields;
592 }
593 update_site_option( 'check-email-log-global-smtp', $updated_settings );
594 if ( isset($all_fields['mailer'] ) == 'outlook' && isset( $_POST['check-email-outlook-options'] ) ) {
595 $outlook_fields = array_map('sanitize_text_field', wp_unslash($_POST['check-email-outlook-options']));
596 if(isset($outlook_fields['client_id']) && !empty($outlook_fields['client_id'])){
597 $outlook_option['client_id'] = base64_encode($outlook_fields['client_id']);
598 }
599 if(isset($outlook_fields['client_secret']) && !empty($outlook_fields['client_secret'])){
600 $outlook_option['client_secret'] = base64_encode($outlook_fields['client_secret']);
601 }
602 $auth = new CheckEmail\Core\Auth( 'outlook' );
603 $auth->update_mailer_option( $outlook_option );
604 }
605 wp_send_json_success();
606 }
607 } else {
608 wp_send_json_error(esc_html__('Invalid input', 'check-email') );
609 }
610 }
611
612 add_action( 'wp_ajax_update_network_settings', 'ck_mail_update_network_settings' );
613 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
614 function ck_mail_check_dns() {
615 // Check nonce
616 if ( isset( $_POST['ck_mail_security_nonce'] ) ) {
617 if ( !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ck_mail_security_nonce'] ) ), 'ck_mail_security_nonce' ) ){
618 die( '-1' );
619 }
620
621 // Check if user is allowed to manage network options
622 if ( ! current_user_can( 'manage_check_email' ) ) {
623 wp_send_json_error(esc_html__('Unauthorized user', 'check-email') );
624 return;
625 }
626 // $api_url = 'http://127.0.0.1:8000/custom-api/check-dns';
627 $api_url = 'https://enchain.tech/custom-api/check-dns';
628 $domain = null;
629 if ( isset( $_POST['domain'] ) ) {
630 $domain = sanitize_text_field( wp_unslash( $_POST['domain'] ) );
631 }
632 $api_params = array(
633 'domain' => $domain,
634 );
635
636 $response = wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
637
638 if ( ! is_wp_error( $response ) ) {
639 $response = wp_remote_retrieve_body( $response );
640 $response = json_decode( $response, true );
641 if (isset($response['is_error'])) {
642 $result = $response;
643 }else{
644 $result['is_error'] = 0;
645 $result['data'] = $response;
646 }
647 echo wp_json_encode( $result );
648 } else {
649 $error_message = $response->get_error_message();
650 echo wp_json_encode( array( 'response' => $error_message ) );
651 }
652 }
653 wp_die();
654 }
655 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
656 function ck_email_verify($email) {
657 $spoof_valid = 1;
658 $dns_valid = 1;
659 $email_valid = 1;
660 if (class_exists('\Egulias\EmailValidator\EmailValidator')) {
661 $validator = new \Egulias\EmailValidator\EmailValidator();
662 // ietf.org has MX records signaling a server with email capabilities
663 $email_valid = $validator->isValid($email, new \Egulias\EmailValidator\Validation\RFCValidation());
664 $dns_valid = $validator->isValid($email, new \Egulias\EmailValidator\Validation\DNSCheckValidation());
665 $spoof_valid = $validator->isValid($email, new \Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation());
666 }
667 $response['status'] = true;
668 $response['spoof_valid'] = ($spoof_valid) ? 1 : 0;
669 $response['dns_valid'] = ($dns_valid) ? 1 : 0;
670 $response['email_valid'] = ($email_valid) ? 1 : 0;
671 return $response;
672 }
673
674 add_action( 'wp_ajax_check_dns', 'ck_mail_check_dns' );
675 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
676 function ck_mail_check_email_analyze() {
677 // Check nonce
678 if (isset($_POST['ck_mail_security_nonce'])) {
679 if ( !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ck_mail_security_nonce'] ) ), 'ck_mail_security_nonce' ) ){
680 die( '-1' );
681 }
682 if ( ! current_user_can( 'manage_check_email' ) ) {
683 wp_send_json_error(esc_html__('Unauthorized user', 'check-email') );
684 return;
685 }
686 // $api_url = 'http://127.0.0.1:8000/custom-api/email-analyze';
687 $api_url = 'https://spamanalyser.check-email.tech/custom-api/email-analyze';
688 $current_user = wp_get_current_user();
689 $email = $current_user->user_email;
690 if ( !empty( $email ) ) {
691 $to = 'plugintest@check-email.tech';
692 $title = esc_html__("Test email to analyze check email", "check-email");
693 $body = esc_html__('This test email will analyze score', "check-email");
694 $site_name = get_bloginfo('name');
695 $headers = [
696 'Content-Type: text/html; charset=UTF-8',
697 'From: '.$site_name .'<'.$email.'>',
698 'Reply-To: '.$email
699 ];
700 wp_mail($to, $title, $body, $headers);
701 }
702 $api_params = array(
703 'email' => $email,
704 );
705
706 if (function_exists('ck_mail_create_spam_analyzer_table') ) {
707 ck_mail_create_spam_analyzer_table();
708 }
709
710 $response = wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
711
712 if ( ! is_wp_error( $response ) ) {
713 $response = wp_remote_retrieve_body( $response );
714 $response = json_decode( $response, true );
715 if (isset($response['is_error']) && $response['is_error'] == 1) {
716 $result = $response;
717 }else{
718 $result['is_error'] = 0;
719 $result['data'] = $response;
720 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated , WordPress.Security.ValidatedSanitizedInput.MissingUnslash , WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
721 $ip_address = $_SERVER['SERVER_ADDR']; // Replace with your target IP
722 $blocklist = check_email_is_ip_blocked($ip_address);
723 $result['blocklist'] = $blocklist;
724 $result['ip_address'] = $ip_address;
725 $spam_final_score = 0;
726 $block_final_score = 0;
727 $auth_final_score = 0;
728 $link_final_score = 0;
729 if ( isset( $response['spamcheck_result'] )) {
730 $spam_score = $response['spamcheck_result']['score'];
731 if ($spam_score > 0) {
732 $spam_final_score = 2.5;
733 } else if ($spam_score < 0 && $spam_score > -5) {
734 $spam_final_score = 1.5;
735 } else if ($spam_score < -5) {
736 $spam_final_score = 0;
737 }
738 }
739 $block_count = 0;
740 foreach ($blocklist as $key => $value) {
741 if($value['status']){
742 $block_count +=1;
743 }
744 }
745 if ($block_count == 0) {
746 $block_final_score = 2.5;
747 } else if ($block_count > 0 && $block_count <= 12) {
748 $block_final_score = 1.5;
749 } else if ($block_count > 12) {
750 $block_final_score = 0;
751 }
752 if ( isset( $response['authenticated'] )) {
753 $auth_count = 0;
754 foreach ($response['authenticated'] as $key => $value) {
755 if( ! $value['status'] ){
756 $auth_count +=1;
757 }
758 }
759 if ($auth_count == 0) {
760 $auth_final_score = 2.5;
761 } else if ($auth_count > 0 && $auth_count < 3) {
762 $auth_final_score = 1.5;
763 } else if ($auth_count >= 3) {
764 $auth_final_score = 0;
765 }
766 }
767 if ( isset( $response['links'] ) ) {
768 $link_count = 0;
769 foreach ($response['links'] as $key => $value) {
770 if( $value['status'] > 200 ){
771 $link_count +=1;
772 }
773 }
774 if ($link_count > 0) {
775 $link_final_score = 0;
776 } else {
777 $link_final_score = 2.5;
778 }
779 }
780 $final_score = ($link_final_score + $auth_final_score + $block_final_score + $spam_final_score);
781 $spam_score_get = get_option('check_email_spam_score_' . $current_user->user_email,[]);
782 $current_date_time = current_time('Y-m-d H:i:s');
783 $spam_score_get[$current_date_time] = array('score' => $final_score, 'datetime' => $current_date_time);
784 $spam_score = array_reverse($spam_score_get);
785 $n = 1;
786 foreach (array_reverse($spam_score_get) as $key => $value) {
787 if( $n > 15 ){
788 unset($spam_score[$key]);
789 }
790 $n++;
791 }
792 update_option('check_email_spam_score_' . $current_user->user_email, $spam_score);
793 $result['previous_spam_score'] = $spam_score;
794 $result['previous_email_result'] = ck_email_verify($email);
795 $data_to_insert = array(
796 'html_content' => wp_json_encode($response['html_tab']),
797 'spam_assassin' => wp_json_encode(array('data'=> $response['spamcheck_result'],'spam_final_score' => $spam_final_score)),
798 'authenticated' => wp_json_encode(array('data'=> $response['authenticated'],'auth_final_score' => $auth_final_score)),
799 'block_listed' => wp_json_encode(array('data'=> $blocklist,'block_final_score' => $block_final_score)),
800 'broken_links' => wp_json_encode(array('data'=> $response['links'],'link_final_score' => $link_final_score)),
801 'final_score' => $final_score,
802 'test_date' => $current_date_time,
803 );
804 if ( function_exists('ck_mail_insert_spam_analyzer') ) {
805 ck_mail_insert_spam_analyzer($data_to_insert);
806 }
807 }
808 echo wp_json_encode( $result );
809 } else {
810 $error_message = $response->get_error_message();
811 echo wp_json_encode( array( 'response' => $error_message ) );
812 }
813 }
814 wp_die();
815 }
816
817 add_action( 'wp_ajax_check_email_analyze', 'ck_mail_check_email_analyze' );
818
819 add_action('wp_ajax_checkmail_save_admin_fcm_token', 'checkmail_save_admin_fcm_token');
820 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
821 function checkmail_save_admin_fcm_token() {
822 $result['status'] = false;
823 if (!isset($_POST['ck_mail_security_nonce'])) {
824 return;
825 }
826 if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ck_mail_security_nonce'])), 'ck_mail_security_nonce')) {
827 return;
828 }
829 if (isset($_POST['token']) && !empty($_POST['token'])) {
830
831 $current_user = wp_get_current_user();
832
833 if (in_array('administrator', (array) $current_user->roles)) {
834
835 $device_tokens = get_option('checkmail_admin_fcm_token');
836 if (!is_array($device_tokens)) {
837 $device_tokens = [];
838 }
839 $new_token = sanitize_text_field(wp_unslash(($_POST['token'] )));
840
841 if (!in_array($new_token, $device_tokens)) {
842 $device_tokens[] = $new_token;
843 }
844 $device_tokens = array_slice(array_unique($device_tokens), -5);
845 update_option('checkmail_admin_fcm_token', $device_tokens);
846 $result['status'] = true;
847 }
848 }
849 echo wp_json_encode( $result );
850 wp_die();
851 }
852
853
854
855 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
856 function check_email_is_ip_blocked($ip) {
857 $dnsbl_list = [
858 "zen.spamhaus.org",
859 "bl.spamcop.net",
860 "dnsbl.sorbs.net",
861 "b.barracudacentral.org",
862 "spam.dnsbl.sorbs.net",
863 "pbl.spamhaus.org",
864 "xbl.spamhaus.org",
865 "dbl.spamhaus.org",
866 "cbl.abuseat.org",
867 "psbl.surriel.com",
868 "rbl.spamlab.com",
869 "rbl.dns-servicios.com",
870 "dnsbl.spfbl.net",
871 "ipbl.mailspike.net",
872 "aspews.ext.sorbs.net",
873 "ubl.unsubscore.com",
874 "dnsbl.kempt.net",
875 "truncate.gbudb.net",
876 "rbl.efnetrbl.org",
877 "dnsbl-1.uceprotect.net",
878 "all.s5h.net",
879 "dnsbl.inps.de",
880 "dnsbl.dronebl.org",
881 "hostkarma.junkemailfilter.com"
882 ];
883 $reversed_ip = implode(".", array_reverse(explode(".", $ip)));
884 $blocked_on = [];
885
886 foreach ($dnsbl_list as $blocklist) {
887 $query = $reversed_ip . "." . $blocklist;
888 // Perform DNS lookup
889 $outpt = checkdnsrr($query, "A");
890 if ($outpt) {
891 $blocked_on[] = array('status' => 1,'ip' => $blocklist);
892 }else{
893 $blocked_on[] = array('status' => 0,'ip' => $blocklist);
894 }
895 }
896 return $blocked_on;
897 }
898
899
900 // email and phone encoding start
901 /**
902 * Define filter-priority constant, unless it has already been defined.
903 */
904 if ( ! defined( 'CHECK_EMAIL_E_FILTER_PRIORITY' ) ) {
905 define(
906 'CHECK_EMAIL_E_FILTER_PRIORITY',
907 (integer) get_option( 'check_email_e_filter_priority', 2000 )
908 );
909 }
910
911 if ( ! defined( 'CHECK_EMAIL_E_REGEXP' ) ) {
912 define(
913 'CHECK_EMAIL_E_REGEXP',
914 '{
915 (?:mailto:)? # Optional mailto:
916 (?:
917 [-!#$%&*+/=?^_`.{|}~\w\x80-\xFF]+ # Local part before @
918 )
919 \@ # At sign (@)
920 (?:
921 [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+ # Domain name
922 |
923 \[[\d.a-fA-F:]+\] # IPv4/IPv6 address
924 )
925 }xi'
926 );
927 }
928
929 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
930 $encode_options = get_option('check-email-email-encode-options', true);
931 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
932 $is_enable = ( isset( $encode_options['is_enable'] ) ) ? $encode_options['is_enable'] : 0;
933 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
934 $email_using = ( isset( $encode_options['email_using'] ) ) ? $encode_options['email_using'] : "";
935 if ( $is_enable && $email_using == 'filters' ) {
936 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
937 foreach ( array( 'the_content', 'the_excerpt', 'widget_text', 'comment_text', 'comment_excerpt' ) as $filter ) {
938 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
939 add_filter( $filter, 'check_email_e_encode_emails', CHECK_EMAIL_E_FILTER_PRIORITY );
940 }
941 }
942 if ( $is_enable && $email_using == 'full_page' ) {
943 add_action( 'wp', 'check_email_full_page_scanner',999 );
944 }
945
946 add_action( 'init', 'check_email_e_register_shortcode', 2000 );
947 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
948 function check_email_e_register_shortcode() {
949 if ( ! shortcode_exists( 'checkmail-encode' ) ) {
950 add_shortcode( 'checkmail-encode', 'check_email_e_shortcode' );
951 }
952 }
953 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
954 function check_email_rot47($str) {
955 $rotated = '';
956 foreach (str_split($str) as $char) {
957 $ascii = ord($char);
958 if ($ascii >= 33 && $ascii <= 126) {
959 $rotated .= chr(33 + (($ascii + 14) % 94));
960 } else {
961 $rotated .= $char;
962 }
963 }
964 return $rotated;
965 }
966 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
967 function check_email_encode_str( $string, $hex = false ) {
968 $encode_options = get_option('check-email-email-encode-options', true);
969 $email_technique = ( isset( $encode_options['email_technique'] ) ) ? $encode_options['email_technique'] : "";
970 if (strpos($string, 'mailto:') !== false) {
971 $string = str_replace('mailto:', '', $string);
972 switch ($email_technique) {
973 case 'css_direction':
974 $reversed_email = strrev($string);
975 // Wrap it with the span and necessary CSS
976 return 'mailto:'.esc_html($reversed_email);
977 break;
978 case 'rot_13':
979 $encoded_email = check_email_rot13($string);
980 return 'mailto:'.esc_html($encoded_email);
981 break;
982 case 'rot_47':
983 $encoded_email = check_email_rot47($string);
984 return 'mailto:'.esc_html($encoded_email);
985 break;
986
987 default:
988 # code...
989 break;
990 }
991 }else{
992 switch ($email_technique) {
993 case 'css_direction':
994 $reversed_email = strrev($string);
995 // Wrap it with the span and necessary CSS
996 return ' <span style="direction: rtl; unicode-bidi: bidi-override;">' . esc_html($reversed_email) . '</span>';
997 break;
998 case 'rot_13':
999 $encoded_email = check_email_rot13($string);
1000 return ' <span class="check-email-encoded-email" >' . esc_html($encoded_email).' </span>';
1001 break;
1002 case 'rot_47':
1003 $encoded_email = check_email_rot47($string);
1004 return ' <span class="check-email-rot47-email" >' . esc_html($encoded_email).' </span>';
1005 break;
1006
1007 default:
1008 # code...
1009 break;
1010 }
1011 }
1012
1013
1014 $chars = str_split( $string );
1015 $string_length = (int) abs(crc32($string) / strlen($string));
1016 $length = max($string_length, 1);
1017 $seed = random_int($length, PHP_INT_MAX);
1018
1019 foreach ( $chars as $key => $char ) {
1020 $ord = ord( $char );
1021
1022 if ( $ord < 128 ) { // ignore non-ascii chars
1023 $r = ( $seed * ( 1 + $key ) ) % 100; // pseudo "random function"
1024
1025 if ( $r > 75 && $char !== '@' && $char !== '.' ); // plain character (not encoded), except @-signs and dots
1026 else if ( $hex && $r < 25 ) $chars[ $key ] = '%' . bin2hex( $char ); // hex
1027 else if ( $r < 45 ) $chars[ $key ] = '&#x' . dechex( $ord ) . ';'; // hexadecimal
1028 else $chars[ $key ] = "&#{$ord};"; // decimal (ascii)
1029 }
1030 }
1031
1032 return implode( '', $chars );
1033 }
1034 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1035 function check_email_e_shortcode( $attributes, $content = '' ) {
1036 $atts = shortcode_atts( array(
1037 'link' => null,
1038 'class' => null,
1039 ), $attributes, 'checkmail-encode' );
1040
1041 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1042 $method = apply_filters( 'check_email_e_method', 'check_email_encode_str' );
1043
1044 if ( ! empty( $atts[ 'link' ] ) ) {
1045 $link = esc_url( $atts[ 'link' ], null, 'shortcode' );
1046
1047 if ( $link === '' ) {
1048 return $method( $content );
1049 }
1050
1051 if ( empty( $atts[ 'class' ] ) ) {
1052 return sprintf(
1053 '<a href="%s">%s</a>',
1054 $method( $link ),
1055 $method( $content )
1056 );
1057 }
1058
1059 return sprintf(
1060 '<a href="%s" class="%s">%s</a>',
1061 $method( $link ),
1062 esc_attr( $atts[ 'class' ] ),
1063 $method( $content )
1064 );
1065 }
1066
1067 return $method( $content );
1068 }
1069 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1070 function check_email_e_encode_emails( $string ) {
1071 if ( ! is_string( $string ) ) {
1072 return $string;
1073 }
1074 // abort if `check_email_e_at_sign_check` is true and `$string` doesn't contain a @-sign
1075 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1076 if ( apply_filters( 'check_email_e_at_sign_check', true ) && strpos( $string, '@' ) === false ) {
1077 return $string;
1078 }
1079 // override encoding function with the 'check_email_e_method' filter
1080 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1081 $method = apply_filters( 'check_email_e_method', 'check_email_encode_str' );
1082 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1083 $regexp = apply_filters( 'check_email_e_regexp', CHECK_EMAIL_E_REGEXP );
1084
1085 $callback = function ( $matches ) use ( $method ) {
1086 return $method( $matches[ 0 ] );
1087 };
1088
1089 if ( has_filter( 'check_email_e_callback' ) ) {
1090 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
1091 $callback = apply_filters( 'check_email_e_callback', $callback, $method );
1092 return preg_replace_callback( $regexp, $callback, $string );
1093 }
1094
1095 return preg_replace_callback( $regexp, $callback, $string );
1096 }
1097 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1098 function check_email_full_page_scanner() {
1099 if(!is_admin() ) {
1100 ob_start('check_email_full_page_callback');
1101 }
1102 }
1103 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1104 function check_email_full_page_callback($string) {
1105 return check_email_e_encode_emails($string);
1106 }
1107
1108
1109 add_action( 'wp_enqueue_scripts', 'ck_mail_enqueue_encoder_js' );
1110 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1111 function ck_mail_enqueue_encoder_js() {
1112 $encode_options = get_option('check-email-email-encode-options', true);
1113 $is_enable = ( isset( $encode_options['is_enable'] ) ) ? $encode_options['is_enable'] : 0;
1114 if ( $is_enable ) {
1115 $email_using = ( isset( $encode_options['email_using'] ) ) ? $encode_options['email_using'] : "";
1116 $email_technique = ( isset( $encode_options['email_technique'] ) ) ? $encode_options['email_technique'] : "";
1117
1118 $check_email = wpchill_check_email();
1119 $plugin_dir_url = plugin_dir_url( $check_email->get_plugin_file() );
1120 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1121 wp_register_script( 'checkemail_encoder', $plugin_dir_url . 'assets/js/check-email-front'. $suffix .'.js', array(), $check_email->get_version(), true );
1122 $data = array();
1123 $data['email_using'] = $email_using;
1124 $data['is_enable'] = $is_enable;
1125 $data['email_technique'] = $email_technique;
1126
1127 wp_localize_script( 'checkemail_encoder', 'checkemail_encoder_data', $data );
1128 wp_enqueue_script( 'checkemail_encoder' );
1129 }
1130 }
1131 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1132 function check_email_rot13( $string ) {
1133
1134 $from = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
1135 $to = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
1136
1137 return strtr( $string, $from, $to );
1138 }
1139
1140 // email and phone encoding end
1141 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1142 function check_email_track_email_open() {
1143 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1144 if (isset($_GET['action']) && $_GET['action'] === 'check_email_track_email_open' && isset($_GET['open_tracking_id']) && isset($_GET['_wpnonce'])) {
1145 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1146 if (!check_email_verify_extended_nonce(sanitize_text_field( wp_unslash($_GET['_wpnonce'])))) {
1147 return false;
1148 }
1149 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1150 $open_tracking_id = absint($_GET['open_tracking_id']);
1151
1152 if ($open_tracking_id) {
1153 global $wpdb;
1154 $table_name = $wpdb->prefix . 'check_email_log';
1155 $query = $wpdb->prepare(
1156 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1157 "SELECT * FROM {$table_name} WHERE open_tracking_id = %s",
1158 $open_tracking_id
1159 );
1160 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter
1161 $record = $wpdb->get_row($query);
1162
1163 if ($record) {
1164 $data_to_update = [
1165 'open_count' => $record->open_count + 1
1166 ];
1167 $where = [
1168 'open_tracking_id' => $open_tracking_id,
1169 ];
1170 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
1171 $wpdb->update( $table_name, $data_to_update, $where );
1172 header("Content-Type: image/png");
1173 echo esc_html(base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgMBAptL0ygAAAAASUVORK5CYII='));
1174 exit;
1175 }
1176 }
1177 }
1178
1179 }
1180 add_action('init', 'check_email_track_email_open');
1181 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1182 function check_email_generate_extended_nonce($action = -1, $lifetime = WEEK_IN_SECONDS) {
1183 $i = wp_nonce_tick() - (floor(time() / $lifetime) - floor(time() / (DAY_IN_SECONDS * 2)));
1184 return wp_create_nonce($action . $i);
1185 }
1186 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1187 function check_email_verify_extended_nonce($nonce, $action = -1, $lifetime = WEEK_IN_SECONDS) {
1188 $i = wp_nonce_tick() - (floor(time() / $lifetime) - floor(time() / (DAY_IN_SECONDS * 2)));
1189
1190 if (wp_verify_nonce($nonce, $action . $i)) {
1191 return true;
1192 }
1193 if (wp_verify_nonce($nonce, $action . ($i - 1))) {
1194 return true;
1195 }
1196 return false;
1197 }
1198 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1199 function check_email_content_with_tracking($open_tracking_id) {
1200 $nonce = check_email_generate_extended_nonce();
1201 $tracking_url = add_query_arg(
1202 array(
1203 '_wpnonce'=>$nonce,
1204 'open_tracking_id' => $open_tracking_id,
1205 'action' => 'check_email_track_email_open',
1206 ),
1207 site_url('/check-email-tracking/')
1208 );
1209 $tracking_url = esc_url_raw($tracking_url);
1210 // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
1211 $email_content = "<img src='$tracking_url' class='check-email-tracking' alt='' width='1' height='1' style='display:none;' />";
1212 return $email_content;
1213 }
1214
1215 if ( is_admin() ) {
1216 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1217 function checmail_dashboard_widget() {
1218 echo '<canvas id="checkmail-dashboard-chart" style="width: 100%; height: 250px;"></canvas>';
1219 echo '
1220 <div style="margin-top: 10px; text-align: center; display: flex; justify-content: space-between; align-items: center;">
1221 <div>
1222 <select id="checkmail-dashboard-date-range">
1223 <option value="7">'.esc_html__('Last 7 Days', 'check-email').'</option>
1224 <option value="14">'.esc_html__('Last 14 Days', 'check-email').'</option>
1225 <option value="30">'.esc_html__('Last 30 Days', 'check-email').'</option>
1226 </select>
1227 </div>
1228 <div style="margin-top: 10px; text-align: center; font-size: 14px;">
1229 <p><span style="color: blue; font-weight: bold;" id="js_checkmail_total"></span> |
1230 <span style="color: green; font-weight: bold;" id="js_checkmail_sent"></span> |
1231 <span style="color: red; font-weight: bold;" id="js_checkmail_failed"></span></p>
1232 </div>
1233 </div>
1234 ';
1235 }
1236 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1237 function check_email_add_checmail_dashboard_widget() {
1238 $option = get_option( 'check-email-log-core' );
1239
1240 if(!isset( $option['enable_dashboard_widget']) || (isset( $option['enable_dashboard_widget']) && $option['enable_dashboard_widget'] ) ){
1241 wp_add_dashboard_widget(
1242 'checmail_dashboard_widget',
1243 esc_html__('Check & Log Email Activity', 'check-email'),
1244 'checmail_dashboard_widget'
1245 );
1246 }
1247 }
1248 add_action('wp_dashboard_setup', 'check_email_add_checmail_dashboard_widget');
1249 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1250 function check_email_custom_dashboard_scripts($hook) {
1251 if ($hook !== 'index.php') return;
1252 $option = get_option( 'check-email-log-core' );
1253 if(!isset( $option['enable_dashboard_widget']) || (isset( $option['enable_dashboard_widget']) && $option['enable_dashboard_widget'] ) ){
1254 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
1255 wp_enqueue_script('chartjs', CK_MAIL_URL . 'assets/js/admin/chart.js', [], CK_MAIL_VERSION, true);
1256 wp_register_script('checkmail-dashboard-chart', CK_MAIL_URL . 'assets/js/admin/checkmail-dashboard-chart'. $suffix .'.js', ['jquery','chartjs'], CK_MAIL_VERSION, true);
1257 $data = array(
1258 'ajax_url' => admin_url( 'admin-ajax.php' ),
1259 'ck_mail_security_nonce' => wp_create_nonce('ck_mail_ajax_check_nonce'),
1260 );
1261
1262 wp_localize_script( 'checkmail-dashboard-chart', 'checkmail_chart', $data );
1263 wp_enqueue_script( 'checkmail-dashboard-chart' );
1264 }
1265
1266
1267
1268 }
1269 add_action('admin_enqueue_scripts', 'check_email_custom_dashboard_scripts');
1270 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
1271 function check_email_get_email_analytics_data() {
1272 if( !isset( $_GET['ck_mail_security_nonce'] ) || isset( $_GET['ck_mail_security_nonce'] ) && !wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['ck_mail_security_nonce'] ) ), 'ck_mail_ajax_check_nonce' ) ) {
1273 echo esc_html__('security_nonce_not_verified', 'check-email');
1274 die();
1275 }
1276 if ( !current_user_can( 'manage_options' ) ) {
1277 die();
1278 }
1279 global $wpdb;
1280
1281 $table_name = $wpdb->prefix . 'check_email_log';
1282 $ck_days = isset($_GET['ck_days']) ? sanitize_text_field( wp_unslash( $_GET['ck_days'] ) ) : 7;
1283 $query = $wpdb->prepare(
1284 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
1285 "SELECT * FROM $table_name WHERE sent_date >= CURDATE() - INTERVAL %d DAY",
1286 $ck_days
1287 );
1288 // phpcs:ignore InterpolatedNotPrepared
1289 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter
1290 $results = $wpdb->get_results($query);
1291
1292 $data = [
1293 'labels' => [],
1294 'sent' => [],
1295 'failed' => [],
1296 ];
1297
1298
1299 $daily_counts = [];
1300 foreach ($results as $row) {
1301 $created_at = $row->sent_date;
1302 $status = $row->result;
1303 $date = gmdate('M j', strtotime($created_at));
1304 if (!isset($daily_counts[$date])) {
1305 $daily_counts[$date] = ['sent' => 0, 'failed' => 0];
1306 }
1307 if ($status == 1) {
1308 $daily_counts[$date]['sent']++;
1309 } else {
1310 $daily_counts[$date]['failed']++;
1311 }
1312 }
1313 ksort($daily_counts);
1314 foreach ($daily_counts as $date => $counts) {
1315 $data['labels'][] = $date;
1316 $data['sent'][] = $counts['sent'];
1317 $data['failed'][] = $counts['failed'];
1318 }
1319
1320 $data['total_mail'] = array_sum($data['sent']) + array_sum($data['failed']);
1321 $data['total_failed'] = array_sum($data['failed']);
1322 $data['total_sent'] = array_sum($data['sent']);
1323
1324 wp_send_json($data);
1325 }
1326 add_action('wp_ajax_get_email_analytics', 'check_email_get_email_analytics_data');
1327
1328 }