Core
1 year ago
Util
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
508 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( 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 | |
| 145 | if ( ! wp_verify_nonce( $_POST['ck_mail_security_nonce'], 'ck_mail_ajax_check_nonce' ) ) { |
| 146 | echo esc_html__('security_nonce_not_verified', 'check-email'); |
| 147 | die(); |
| 148 | } |
| 149 | if ( !current_user_can( 'manage_options' ) ) { |
| 150 | die(); |
| 151 | } |
| 152 | $api_url = 'http://magazine3.company/wp-json/api/central/email/subscribe'; |
| 153 | |
| 154 | $api_params = array( |
| 155 | 'name' => sanitize_text_field(wp_unslash($_POST['name'])), |
| 156 | 'email'=> sanitize_email(wp_unslash($_POST['email'])), |
| 157 | 'website'=> sanitize_text_field(wp_unslash($_POST['website'])), |
| 158 | 'type'=> 'checkmail' |
| 159 | ); |
| 160 | wp_remote_post( $api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) ); |
| 161 | wp_die(); |
| 162 | } |
| 163 | |
| 164 | function ck_mail_forward_mail($atts) { |
| 165 | if ( isset( $atts['to'] ) ) { |
| 166 | $to = $atts['to']; |
| 167 | if ( ! is_array( $to ) ) { |
| 168 | $to = explode( ',', $to ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | if ( isset( $atts['subject'] ) ) { |
| 174 | $subject = $atts['subject']; |
| 175 | } |
| 176 | |
| 177 | if ( isset( $atts['message'] ) ) { |
| 178 | $message = $atts['message']; |
| 179 | } |
| 180 | |
| 181 | if ( isset( $atts['headers'] ) ) { |
| 182 | $headers = $atts['headers']; |
| 183 | } |
| 184 | |
| 185 | if ( isset( $atts['attachments'] ) ) { |
| 186 | $attachments = $atts['attachments']; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | $subject = esc_html('Forward Email Check & Log ', 'check-email').$subject; |
| 191 | |
| 192 | if ( ! is_array( $attachments ) ) { |
| 193 | $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); |
| 194 | } |
| 195 | global $phpmailer; |
| 196 | if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) { |
| 197 | require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php'; |
| 198 | require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php'; |
| 199 | require_once ABSPATH . WPINC . '/PHPMailer/Exception.php'; |
| 200 | $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true ); |
| 201 | |
| 202 | $phpmailer::$validator = static function ( $email ) { |
| 203 | return (bool) is_email( $email ); |
| 204 | }; |
| 205 | } |
| 206 | |
| 207 | // Headers. |
| 208 | $cc = array(); |
| 209 | $bcc = array(); |
| 210 | $reply_to = array(); |
| 211 | |
| 212 | if ( empty( $headers ) ) { |
| 213 | $headers = array(); |
| 214 | } else { |
| 215 | if ( ! is_array( $headers ) ) { |
| 216 | $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); |
| 217 | } else { |
| 218 | $tempheaders = $headers; |
| 219 | } |
| 220 | $headers = array(); |
| 221 | |
| 222 | // If it's actually got contents. |
| 223 | if ( ! empty( $tempheaders ) ) { |
| 224 | // Iterate through the raw headers. |
| 225 | foreach ( (array) $tempheaders as $header ) { |
| 226 | if ( ! str_contains( $header, ':' ) ) { |
| 227 | if ( false !== stripos( $header, 'boundary=' ) ) { |
| 228 | $parts = preg_split( '/boundary=/i', trim( $header ) ); |
| 229 | $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) ); |
| 230 | } |
| 231 | continue; |
| 232 | } |
| 233 | // Explode them out. |
| 234 | list( $name, $content ) = explode( ':', trim( $header ), 2 ); |
| 235 | |
| 236 | // Cleanup crew. |
| 237 | $name = trim( $name ); |
| 238 | $content = trim( $content ); |
| 239 | |
| 240 | switch ( strtolower( $name ) ) { |
| 241 | // Mainly for legacy -- process a "From:" header if it's there. |
| 242 | case 'from': |
| 243 | $bracket_pos = strpos( $content, '<' ); |
| 244 | if ( false !== $bracket_pos ) { |
| 245 | // Text before the bracketed email is the "From" name. |
| 246 | if ( $bracket_pos > 0 ) { |
| 247 | $from_name = substr( $content, 0, $bracket_pos ); |
| 248 | $from_name = str_replace( '"', '', $from_name ); |
| 249 | $from_name = trim( $from_name ); |
| 250 | } |
| 251 | |
| 252 | $from_email = substr( $content, $bracket_pos + 1 ); |
| 253 | $from_email = str_replace( '>', '', $from_email ); |
| 254 | $from_email = trim( $from_email ); |
| 255 | |
| 256 | // Avoid setting an empty $from_email. |
| 257 | } elseif ( '' !== trim( $content ) ) { |
| 258 | $from_email = trim( $content ); |
| 259 | } |
| 260 | break; |
| 261 | case 'content-type': |
| 262 | if ( str_contains( $content, ';' ) ) { |
| 263 | list( $type, $charset_content ) = explode( ';', $content ); |
| 264 | $content_type = trim( $type ); |
| 265 | if ( false !== stripos( $charset_content, 'charset=' ) ) { |
| 266 | $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) ); |
| 267 | } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) { |
| 268 | $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) ); |
| 269 | $charset = ''; |
| 270 | } |
| 271 | |
| 272 | // Avoid setting an empty $content_type. |
| 273 | } elseif ( '' !== trim( $content ) ) { |
| 274 | $content_type = trim( $content ); |
| 275 | } |
| 276 | break; |
| 277 | case 'cc': |
| 278 | $cc = array_merge( (array) $cc, explode( ',', $content ) ); |
| 279 | break; |
| 280 | case 'bcc': |
| 281 | $bcc = array_merge( (array) $bcc, explode( ',', $content ) ); |
| 282 | break; |
| 283 | case 'reply-to': |
| 284 | $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) ); |
| 285 | break; |
| 286 | default: |
| 287 | // Add it to our grand headers array. |
| 288 | $headers[ trim( $name ) ] = trim( $content ); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Empty out the values that may be set. |
| 296 | $phpmailer->clearAllRecipients(); |
| 297 | $phpmailer->clearAttachments(); |
| 298 | $phpmailer->clearCustomHeaders(); |
| 299 | $phpmailer->clearReplyTos(); |
| 300 | $phpmailer->Body = ''; |
| 301 | $phpmailer->AltBody = ''; |
| 302 | |
| 303 | // Set "From" name and email. |
| 304 | |
| 305 | // If we don't have a name from the input headers. |
| 306 | if ( ! isset( $from_name ) ) { |
| 307 | $from_name = 'WordPress'; |
| 308 | } |
| 309 | if ( ! isset( $from_email ) ) { |
| 310 | // Get the site domain and get rid of www. |
| 311 | $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); |
| 312 | $from_email = 'wordpress@'; |
| 313 | |
| 314 | if ( null !== $sitename ) { |
| 315 | if ( str_starts_with( $sitename, 'www.' ) ) { |
| 316 | $sitename = substr( $sitename, 4 ); |
| 317 | } |
| 318 | |
| 319 | $from_email .= $sitename; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | try { |
| 324 | $phpmailer->setFrom( $from_email, $from_name, false ); |
| 325 | } catch ( PHPMailer\PHPMailer\Exception $e ) { |
| 326 | error_log(esc_html__('Error in forwar email check & log : ', 'check-email').$e->getMessage()); |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Set mail's subject and body. |
| 331 | $phpmailer->Subject = $subject; |
| 332 | $phpmailer->Body = $message; |
| 333 | |
| 334 | // Set destination addresses, using appropriate methods for handling addresses. |
| 335 | $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' ); |
| 336 | |
| 337 | foreach ( $address_headers as $address_header => $addresses ) { |
| 338 | if ( empty( $addresses ) ) { |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | foreach ( (array) $addresses as $address ) { |
| 343 | try { |
| 344 | // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>". |
| 345 | $recipient_name = ''; |
| 346 | |
| 347 | if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) { |
| 348 | if ( count( $matches ) === 3 ) { |
| 349 | $recipient_name = $matches[1]; |
| 350 | $address = $matches[2]; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | switch ( $address_header ) { |
| 355 | case 'to': |
| 356 | $phpmailer->addAddress( $address, $recipient_name ); |
| 357 | break; |
| 358 | case 'cc': |
| 359 | $phpmailer->addCc( $address, $recipient_name ); |
| 360 | break; |
| 361 | case 'bcc': |
| 362 | $phpmailer->addBcc( $address, $recipient_name ); |
| 363 | break; |
| 364 | case 'reply_to': |
| 365 | $phpmailer->addReplyTo( $address, $recipient_name ); |
| 366 | break; |
| 367 | } |
| 368 | } catch ( PHPMailer\PHPMailer\Exception $e ) { |
| 369 | continue; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Set to use PHP's mail(). |
| 375 | $phpmailer->isMail(); |
| 376 | |
| 377 | // Set Content-Type and charset. |
| 378 | |
| 379 | // If we don't have a Content-Type from the input headers. |
| 380 | if ( ! isset( $content_type ) ) { |
| 381 | $content_type = 'text/plain'; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | $content_type = apply_filters( 'wp_mail_content_type', $content_type ); |
| 386 | |
| 387 | $phpmailer->ContentType = $content_type; |
| 388 | |
| 389 | // Set whether it's plaintext, depending on $content_type. |
| 390 | if ( 'text/html' === $content_type ) { |
| 391 | $phpmailer->isHTML( true ); |
| 392 | } |
| 393 | |
| 394 | // If we don't have a charset from the input headers. |
| 395 | if ( ! isset( $charset ) ) { |
| 396 | $charset = get_bloginfo( 'charset' ); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset ); |
| 401 | |
| 402 | // Set custom headers. |
| 403 | if ( ! empty( $headers ) ) { |
| 404 | foreach ( (array) $headers as $name => $content ) { |
| 405 | // Only add custom headers not added automatically by PHPMailer. |
| 406 | if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) { |
| 407 | try { |
| 408 | $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) ); |
| 409 | } catch ( PHPMailer\PHPMailer\Exception $e ) { |
| 410 | continue; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) { |
| 416 | $phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) ); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if ( ! empty( $attachments ) ) { |
| 421 | foreach ( $attachments as $filename => $attachment ) { |
| 422 | $filename = is_string( $filename ) ? $filename : ''; |
| 423 | |
| 424 | try { |
| 425 | $phpmailer->addAttachment( $attachment, $filename ); |
| 426 | } catch ( PHPMailer\PHPMailer\Exception $e ) { |
| 427 | continue; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Fires after PHPMailer is initialized. |
| 434 | * |
| 435 | * @since 2.2.0 |
| 436 | * |
| 437 | * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference). |
| 438 | */ |
| 439 | do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); |
| 440 | |
| 441 | $mail_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' ); |
| 442 | |
| 443 | // Send! |
| 444 | try { |
| 445 | $send = $phpmailer->send(); |
| 446 | return $send; |
| 447 | } catch ( PHPMailer\PHPMailer\Exception $e ) { |
| 448 | error_log(esc_html__('Error in forwar email send check & log : ', 'check-email').$e->getMessage()); |
| 449 | return false; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | function ck_mail_create_error_logs() { |
| 454 | |
| 455 | global $wpdb; |
| 456 | |
| 457 | $table_name = $wpdb->prefix . 'check_email_error_logs'; |
| 458 | $charset_collate = $wpdb->get_charset_collate(); |
| 459 | // phpcs:disable. |
| 460 | if ( $wpdb->get_var( $wpdb->prepare( "show tables like %s",$wpdb->esc_like( $table_name )) ) != $table_name ) { |
| 461 | |
| 462 | $sql = "CREATE TABLE IF NOT EXISTS `$table_name` ( |
| 463 | `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 464 | `check_email_log_id` INT DEFAULT NULL, |
| 465 | `content` TEXT DEFAULT NULL, |
| 466 | `initiator` TEXT DEFAULT NULL, |
| 467 | `event_type` TINYINT UNSIGNED NOT NULL DEFAULT '0', |
| 468 | `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 469 | PRIMARY KEY (id) |
| 470 | ) |
| 471 | ENGINE='InnoDB' |
| 472 | {$charset_collate};"; |
| 473 | |
| 474 | $wpdb->query($sql); |
| 475 | } |
| 476 | // phpcs:enable. |
| 477 | } |
| 478 | |
| 479 | function ck_mail_insert_error_logs($data_to_insert) { |
| 480 | |
| 481 | global $wpdb; |
| 482 | |
| 483 | $table_name = $wpdb->prefix . 'check_email_error_logs'; |
| 484 | $wpdb->insert( $table_name, $data_to_insert ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 485 | } |
| 486 | |
| 487 | function ck_mail_local_file_get_contents($file_path){ |
| 488 | |
| 489 | // Include WordPress Filesystem API |
| 490 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 491 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 492 | } |
| 493 | |
| 494 | // Initialize the API |
| 495 | global $wp_filesystem; |
| 496 | if ( ! WP_Filesystem() ) { |
| 497 | return false; |
| 498 | } |
| 499 | // Check if the file exists |
| 500 | if ( $wp_filesystem->exists( $file_path ) ) { |
| 501 | // Read the file content |
| 502 | $file_content = $wp_filesystem->get_contents( $file_path ); |
| 503 | return $file_content; |
| 504 | } else { |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | } |