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