0, 'host' => '', 'port' => 587, 'encryption' => 'tls', 'auth' => 1, 'user' => '', 'from_email' => '', 'from_name' => '', 'force_from' => 0, 'log' => 0, ); } public static function get() { $saved = get_option( self::OPTION, array() ); return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ); } /** * Well-known mail services: id => label, host, port, encryption, user hint. * * @return array */ public static function presets() { return array( 'gmail' => array( 'Gmail / Google Workspace', 'smtp.gmail.com', 587, 'tls', __( 'your Gmail address; the password is an app password', 'ai-guardwall' ) ), 'outlook' => array( 'Outlook / Microsoft 365', 'smtp.office365.com', 587, 'tls', __( 'your Microsoft address', 'ai-guardwall' ) ), 'zoho' => array( 'Zoho Mail', 'smtp.zoho.com', 465, 'ssl', __( 'your Zoho address', 'ai-guardwall' ) ), 'brevo' => array( 'Brevo (Sendinblue)', 'smtp-relay.brevo.com', 587, 'tls', __( 'your Brevo login; the password is the SMTP key', 'ai-guardwall' ) ), 'sendgrid' => array( 'SendGrid', 'smtp.sendgrid.net', 587, 'tls', 'apikey' ), 'mailgun' => array( 'Mailgun', 'smtp.mailgun.org', 587, 'tls', __( 'the SMTP login from Mailgun', 'ai-guardwall' ) ), 'postmark' => array( 'Postmark', 'smtp.postmarkapp.com', 587, 'tls', __( 'the server API token', 'ai-guardwall' ) ), ); } /** * Save the settings (and the password, when a new one is typed). * * @param array $in Values. * @return array The settings. */ public static function save( array $in ) { $s = self::get(); foreach ( $in as $key => $value ) { switch ( $key ) { case 'enabled': case 'auth': case 'force_from': case 'log': $s[ $key ] = empty( $value ) || 'false' === $value ? 0 : 1; break; case 'host': $s[ $key ] = strtolower( preg_replace( '/[^A-Za-z0-9.\-:\[\]]/', '', (string) $value ) ); break; case 'port': $s[ $key ] = max( 1, min( 65535, (int) $value ) ); break; case 'encryption': $s[ $key ] = in_array( $value, array( 'none', 'ssl', 'tls' ), true ) ? $value : 'tls'; break; case 'user': $s[ $key ] = substr( trim( (string) $value ), 0, 200 ); break; case 'from_email': $s[ $key ] = sanitize_email( (string) $value ); break; case 'from_name': $s[ $key ] = sanitize_text_field( (string) $value ); break; case 'password_new': if ( '' !== (string) $value ) { Softvolt_Ai_Bodyguard_Settings::set_secret( self::SECRET, (string) $value ); } break; case 'password_clear': if ( ! empty( $value ) && 'false' !== $value ) { Softvolt_Ai_Bodyguard_Settings::set_secret( self::SECRET, '' ); } break; } } update_option( self::OPTION, $s, true ); if ( ! $s['log'] ) { delete_option( self::LOG ); } return $s; } public static function overview() { $s = self::get(); return array( 'settings' => $s, 'password_hint' => Softvolt_Ai_Bodyguard_Settings::secret_hint( self::SECRET ), 'presets' => self::presets(), 'default_from' => 'wordpress@' . preg_replace( '/^www\./', '', (string) wp_parse_url( network_home_url(), PHP_URL_HOST ) ), 'admin_email' => get_option( 'admin_email' ), 'log_count' => count( self::log() ), ); } /* ------------------------------ sending ------------------------------ */ /** * Point PHPMailer at the SMTP server. * * @param PHPMailer\PHPMailer\PHPMailer $phpmailer Mailer. */ public static function configure( $phpmailer ) { $s = self::get(); $phpmailer->isSMTP(); $phpmailer->Host = $s['host']; $phpmailer->Port = (int) $s['port']; $phpmailer->SMTPSecure = 'none' === $s['encryption'] ? '' : $s['encryption']; $phpmailer->SMTPAutoTLS = 'none' !== $s['encryption']; $phpmailer->SMTPAuth = (bool) $s['auth']; $phpmailer->Timeout = 20; if ( $s['auth'] ) { $phpmailer->Username = $s['user']; $phpmailer->Password = Softvolt_Ai_Bodyguard_Settings::secret( self::SECRET ); } if ( null !== self::$debug ) { $phpmailer->SMTPDebug = 3; $phpmailer->Debugoutput = function ( $line ) { self::$debug .= rtrim( (string) $line ) . "\n"; }; } } public static function from_email( $email ) { return self::get()['from_email']; } public static function from_name( $name ) { return self::get()['from_name']; } public static function remember_error( $error ) { self::$error = is_wp_error( $error ) ? $error->get_error_message() : ''; } /** * Send a test email and return the conversation with the server. * * @param string $to Address. * @return array|WP_Error message, transcript, ok. */ public static function test( $to ) { $to = sanitize_email( (string) $to ); if ( ! is_email( $to ) ) { return new WP_Error( 'svabg_mail', __( 'Type a valid email address.', 'ai-guardwall' ) ); } $s = self::get(); self::$debug = ''; self::$error = ''; $subject = sprintf( /* translators: %s: site name. */ __( 'Test email from %s', 'ai-guardwall' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); $body = sprintf( /* translators: 1: site address, 2: how it was sent. */ __( "This is a test email from AI GuardWall – AI-Powered Digital Security Guardian on %1\$s.\n\nSent through: %2\$s.\n\nIf you can read this, WordPress can send email.", 'ai-guardwall' ), home_url( '/' ), $s['enabled'] && '' !== $s['host'] ? $s['host'] . ':' . $s['port'] : __( 'the server\'s own mail() function', 'ai-guardwall' ) ); $ok = wp_mail( $to, $subject, $body ); $transcript = self::mask( (string) self::$debug ); self::$debug = null; Softvolt_Ai_Bodyguard_Logger::log( $ok ? 'info' : 'warning', 'mail_test', $ok ? sprintf( /* translators: %s: email address. */ __( 'Test email sent to %s.', 'ai-guardwall' ), $to ) : sprintf( /* translators: 1: email address, 2: error. */ __( 'Test email to %1$s failed: %2$s', 'ai-guardwall' ), $to, self::$error ) ); return array( 'ok' => (bool) $ok, 'message' => $ok ? sprintf( /* translators: %s: email address. */ __( 'Sent to %s. Check the inbox (and the spam folder).', 'ai-guardwall' ), $to ) : sprintf( /* translators: %s: error. */ __( 'Not sent: %s', 'ai-guardwall' ), '' !== self::$error ? self::$error : __( 'WordPress gave no reason.', 'ai-guardwall' ) ), 'transcript' => $transcript, ); } /** * Hide the login in an SMTP conversation. * * @param string $text Conversation. * @return string */ public static function mask( $text ) { $out = array(); $auth = 0; foreach ( explode( "\n", $text ) as $line ) { if ( preg_match( '/CLIENT -> SERVER:\s*AUTH\s+(PLAIN|LOGIN|CRAM-MD5|XOAUTH2)\b(.*)$/i', $line, $m ) ) { $line = preg_replace( '/(AUTH\s+\S+)\s+\S+/i', '$1 [hidden]', $line ); $auth = 2; } elseif ( $auth > 0 && false !== strpos( $line, 'CLIENT -> SERVER:' ) ) { $line = preg_replace( '/(CLIENT -> SERVER:\s*).*/', '$1[hidden]', $line ); --$auth; } $out[] = $line; } $password = Softvolt_Ai_Bodyguard_Settings::secret( self::SECRET ); $text = implode( "\n", $out ); if ( '' !== $password ) { $text = str_replace( array( $password, base64_encode( $password ) ), '[hidden]', $text ); } return trim( $text ); } /* ------------------------------ the log ------------------------------ */ public static function log() { $log = get_option( self::LOG, array() ); return is_array( $log ) ? $log : array(); } private static function add_entry( array $entry ) { $log = self::log(); array_unshift( $log, $entry ); update_option( self::LOG, array_slice( $log, 0, self::LOG_MAX ), false ); } private static function list_to( $to ) { $to = is_array( $to ) ? $to : explode( ',', (string) $to ); return implode( ', ', array_map( 'sanitize_text_field', array_map( 'trim', $to ) ) ); } public static function log_sent( $mail ) { self::add_entry( array( 'time' => time(), 'to' => self::list_to( isset( $mail['to'] ) ? $mail['to'] : '' ), 'subject' => sanitize_text_field( isset( $mail['subject'] ) ? (string) $mail['subject'] : '' ), 'ok' => 1, 'error' => '', ) ); } public static function log_failed( $error ) { $data = is_wp_error( $error ) ? (array) $error->get_error_data() : array(); self::add_entry( array( 'time' => time(), 'to' => self::list_to( isset( $data['to'] ) ? $data['to'] : '' ), 'subject' => sanitize_text_field( isset( $data['subject'] ) ? (string) $data['subject'] : '' ), 'ok' => 0, 'error' => is_wp_error( $error ) ? sanitize_text_field( $error->get_error_message() ) : '', ) ); } public static function log_rows() { $rows = array(); foreach ( self::log() as $entry ) { $entry['when'] = sprintf( /* translators: %s: time span. */ __( '%s ago', 'ai-guardwall' ), human_time_diff( (int) $entry['time'] ) ); $rows[] = $entry; } return array( 'rows' => $rows ); } public static function clear_log() { delete_option( self::LOG ); return array( 'message' => __( 'The email log is empty.', 'ai-guardwall' ) ); } }