From 5d30435e81427fcce3353d900faaf8d9fa9a8709 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 25 Apr 2018 22:24:24 -0700 Subject: [PATCH] mail: Refactor checks looking for PEAR libraries to be clearer I don't think this makes any difference towards the linked bug, but it makes it more obvious that MediaWiki doesn't try to use any PEAR classes before attempting to require them. Bug: T186456 Change-Id: I342f41903a15f13e72e76464499351412a9afa34 --- includes/mail/UserMailer.php | 55 ++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/includes/mail/UserMailer.php b/includes/mail/UserMailer.php index fb0f2f99f0..7b48ad095a 100644 --- a/includes/mail/UserMailer.php +++ b/includes/mail/UserMailer.php @@ -189,6 +189,46 @@ class UserMailer { return self::sendInternal( $to, $from, $subject, $body, $options ); } + /** + * Whether the PEAR Mail_mime library is usable. This will + * try and load it if it is not already. + * + * @return bool + */ + private static function isMailMimeUsable() { + static $usable = null; + if ( $usable === null ) { + // If the class is not already loaded, and it's in the include path, + // try requiring it. + if ( !class_exists( 'Mail_mime' ) && stream_resolve_include_path( 'Mail/mime.php' ) ) { + require_once 'Mail/mime.php'; + } + $usable = class_exists( 'Mail_mime' ); + } + + return $usable; + } + + /** + * Whether the PEAR Mail library is usable. This will + * try and load it if it is not already. + * + * @return bool + */ + private static function isMailUsable() { + static $usable = null; + if ( $usable === null ) { + // If the class is not already loaded, and it's in the include path, + // try requiring it. + if ( !class_exists( 'Mail' ) && stream_resolve_include_path( 'Mail.php' ) ) { + require_once 'Mail.php'; + } + $usable = class_exists( 'Mail' ); + } + + return $usable; + } + /** * Helper function fo UserMailer::send() which does the actual sending. It expects a $to * list which the UserMailerSplitTo hook would not split further. @@ -296,15 +336,12 @@ class UserMailer { if ( is_array( $body ) ) { // we are sending a multipart message wfDebug( "Assembling multipart mime email\n" ); - if ( !stream_resolve_include_path( 'Mail/mime.php' ) ) { + if ( !self::isMailMimeUsable() ) { wfDebug( "PEAR Mail_Mime package is not installed. Falling back to text email.\n" ); // remove the html body for text email fall back $body = $body['text']; } else { - // Check if pear/mail_mime is already loaded (via composer) - if ( !class_exists( 'Mail_mime' ) ) { - require_once 'Mail/mime.php'; - } + // pear/mail_mime is already loaded by this point if ( wfIsWindows() ) { $body['text'] = str_replace( "\n", "\r\n", $body['text'] ); $body['html'] = str_replace( "\n", "\r\n", $body['html'] ); @@ -352,12 +389,8 @@ class UserMailer { if ( is_array( $wgSMTP ) ) { // Check if pear/mail is already loaded (via composer) - if ( !class_exists( 'Mail' ) ) { - // PEAR MAILER - if ( !stream_resolve_include_path( 'Mail.php' ) ) { - throw new MWException( 'PEAR mail package is not installed' ); - } - require_once 'Mail.php'; + if ( !self::isMailUsable() ) { + throw new MWException( 'PEAR mail package is not installed' ); } Wikimedia\suppressWarnings(); -- 2.20.1