Merge "Fixup contenttype stuff in UserMailer"
[lhc/web/wiklou.git] / includes / mail / UserMailer.php
index 3c28c5f..464e7b8 100644 (file)
@@ -33,7 +33,7 @@ class UserMailer {
        /**
         * Send mail using a PEAR mailer
         *
-        * @param UserMailer $mailer
+        * @param Mail_smtp $mailer
         * @param string $dest
         * @param string $headers
         * @param string $body
@@ -64,8 +64,8 @@ class UserMailer {
         *
         * @return string
         */
-       static function arrayToHeaderString( $headers, $endl = "\n" ) {
-               $strings = array();
+       static function arrayToHeaderString( $headers, $endl = PHP_EOL ) {
+               $strings = [];
                foreach ( $headers as $name => $value ) {
                        // Prevent header injection by stripping newlines from value
                        $value = self::sanitizeHeaderValue( $value );
@@ -114,26 +114,23 @@ class UserMailer {
         * @throws Exception
         * @return Status
         */
-       public static function send( $to, $from, $subject, $body, $options = array() ) {
-               global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams, $wgAllowHTMLEmail;
-               $contentType = 'text/plain; charset=UTF-8';
-               $headers = array();
-               if ( is_array( $options ) ) {
-                       $replyto = isset( $options['replyTo'] ) ? $options['replyTo'] : null;
-                       $contentType = isset( $options['contentType'] ) ? $options['contentType'] : $contentType;
-                       $headers = isset( $options['headers'] ) ? $options['headers'] : $headers;
-               } else {
+       public static function send( $to, $from, $subject, $body, $options = [] ) {
+               global $wgAllowHTMLEmail;
+
+               if ( !is_array( $options ) ) {
                        // Old calling style
                        wfDeprecated( __METHOD__ . ' with $replyto as 5th parameter', '1.26' );
-                       $replyto = $options;
+                       $options = [ 'replyTo' => $options ];
                        if ( func_num_args() === 6 ) {
-                               $contentType = func_get_arg( 5 );
+                               $options['contentType'] = func_get_arg( 5 );
                        }
                }
+               if ( !isset( $options['contentType'] ) ) {
+                       $options['contentType'] = 'text/plain; charset=UTF-8';
+               }
 
-               $mime = null;
                if ( !is_array( $to ) ) {
-                       $to = array( $to );
+                       $to = [ $to ];
                }
 
                // mail body must have some content
@@ -178,6 +175,72 @@ class UserMailer {
                        return Status::newFatal( 'user-mail-no-addy' );
                }
 
+               // give a chance to UserMailerTransformContents subscribers who need to deal with each
+               // target differently to split up the address list
+               if ( count( $to ) > 1 ) {
+                       $oldTo = $to;
+                       Hooks::run( 'UserMailerSplitTo', [ &$to ] );
+                       if ( $oldTo != $to ) {
+                               $splitTo = array_diff( $oldTo, $to );
+                               $to = array_diff( $oldTo, $splitTo ); // ignore new addresses added in the hook
+                               // first send to non-split address list, then to split addresses one by one
+                               $status = Status::newGood();
+                               if ( $to ) {
+                                       $status->merge( UserMailer::sendInternal(
+                                               $to, $from, $subject, $body, $options ) );
+                               }
+                               foreach ( $splitTo as $newTo ) {
+                                       $status->merge( UserMailer::sendInternal(
+                                               [ $newTo ], $from, $subject, $body, $options ) );
+                               }
+                               return $status;
+                       }
+               }
+
+               return UserMailer::sendInternal( $to, $from, $subject, $body, $options );
+       }
+
+       /**
+        * Helper function fo UserMailer::send() which does the actual sending. It expects a $to
+        * list which the UserMailerSplitTo hook would not split further.
+        * @param MailAddress[] $to Array of recipients' email addresses
+        * @param MailAddress $from Sender's email
+        * @param string $subject Email's subject.
+        * @param string $body Email's text or Array of two strings to be the text and html bodies
+        * @param array $options:
+        *              'replyTo' MailAddress
+        *              'contentType' string default 'text/plain; charset=UTF-8'
+        *              'headers' array Extra headers to set
+        *
+        * @throws MWException
+        * @throws Exception
+        * @return Status
+        */
+       protected static function sendInternal(
+               array $to,
+               MailAddress $from,
+               $subject,
+               $body,
+               $options = []
+       ) {
+               global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams;
+               $mime = null;
+
+               $replyto = isset( $options['replyTo'] ) ? $options['replyTo'] : null;
+               $contentType = isset( $options['contentType'] ) ?
+                       $options['contentType'] : 'text/plain; charset=UTF-8';
+               $headers = isset( $options['headers'] ) ? $options['headers'] : [];
+
+               // Allow transformation of content, such as encrypting/signing
+               $error = false;
+               if ( !Hooks::run( 'UserMailerTransformContent', [ $to, $from, &$body, &$error ] ) ) {
+                       if ( $error ) {
+                               return Status::newFatal( 'php-mail-error', $error );
+                       } else {
+                               return Status::newFatal( 'php-mail-error-unknown' );
+                       }
+               }
+
                /**
                 * Forge email headers
                 * -------------------
@@ -212,7 +275,7 @@ class UserMailer {
                $extraParams = $wgAdditionalMailParams;
 
                // Hook to generate custom VERP address for 'Return-Path'
-               Hooks::run( 'UserMailerChangeReturnPath', array( $to, &$returnPath ) );
+               Hooks::run( 'UserMailerChangeReturnPath', [ $to, &$returnPath ] );
                // Add the envelope sender address using the -f command line option when PHP mail() is used.
                // Will default to the $from->address when the UserMailerChangeReturnPath hook fails and the
                // generated VERP address when the hook runs effectively.
@@ -232,11 +295,7 @@ class UserMailer {
 
                // Line endings need to be different on Unix and Windows due to
                // the bug described at http://trac.wordpress.org/ticket/2603
-               if ( wfIsWindows() ) {
-                       $endl = "\r\n";
-               } else {
-                       $endl = "\n";
-               }
+               $endl = PHP_EOL;
 
                if ( is_array( $body ) ) {
                        // we are sending a multipart message
@@ -254,11 +313,11 @@ class UserMailer {
                                        $body['text'] = str_replace( "\n", "\r\n", $body['text'] );
                                        $body['html'] = str_replace( "\n", "\r\n", $body['html'] );
                                }
-                               $mime = new Mail_mime( array(
+                               $mime = new Mail_mime( [
                                        'eol' => $endl,
                                        'text_charset' => 'UTF-8',
                                        'html_charset' => 'UTF-8'
-                               ) );
+                               ] );
                                $mime->setTXTBody( $body['text'] );
                                $mime->setHTMLBody( $body['html'] );
                                $body = $mime->get(); // must call get() before headers()
@@ -271,12 +330,22 @@ class UserMailer {
                                $body = str_replace( "\n", "\r\n", $body );
                        }
                        $headers['MIME-Version'] = '1.0';
-                       $headers['Content-type'] = ( is_null( $contentType ) ?
-                               'text/plain; charset=UTF-8' : $contentType );
+                       $headers['Content-type'] = $contentType;
                        $headers['Content-transfer-encoding'] = '8bit';
                }
 
-               $ret = Hooks::run( 'AlternateUserMailer', array( $headers, $to, $from, $subject, $body ) );
+               // allow transformation of MIME-encoded message
+               if ( !Hooks::run( 'UserMailerTransformMessage',
+                       [ $to, $from, &$subject, &$headers, &$body, &$error ] )
+               ) {
+                       if ( $error ) {
+                               return Status::newFatal( 'php-mail-error', $error );
+                       } else {
+                               return Status::newFatal( 'php-mail-error-unknown' );
+                       }
+               }
+
+               $ret = Hooks::run( 'AlternateUserMailer', [ $headers, $to, $from, $subject, $body ] );
                if ( $ret === false ) {
                        // the hook implementation will return false to skip regular mail sending
                        return Status::newGood();
@@ -342,20 +411,14 @@ class UserMailer {
                        set_error_handler( 'UserMailer::errorHandler' );
 
                        try {
-                               $safeMode = wfIniGetBool( 'safe_mode' );
-
                                foreach ( $to as $recip ) {
-                                       if ( $safeMode ) {
-                                               $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers );
-                                       } else {
-                                               $sent = mail(
-                                                       $recip,
-                                                       self::quotedPrintable( $subject ),
-                                                       $body,
-                                                       $headers,
-                                                       $extraParams
-                                               );
-                                       }
+                                       $sent = mail(
+                                               $recip,
+                                               self::quotedPrintable( $subject ),
+                                               $body,
+                                               $headers,
+                                               $extraParams
+                                       );
                                }
                        } catch ( Exception $e ) {
                                restore_error_handler();
@@ -394,7 +457,7 @@ class UserMailer {
         * @return string
         */
        public static function sanitizeHeaderValue( $val ) {
-               return strtr( $val, array( "\r" => '', "\n" => '' ) );
+               return strtr( $val, [ "\r" => '', "\n" => '' ] );
        }
 
        /**
@@ -438,7 +501,7 @@ class UserMailer {
                }
                $out = "=?$charset?Q?";
                $out .= preg_replace_callback( "/([$replace])/",
-                       array( __CLASS__, 'quotedPrintableCallback' ), $string );
+                       [ __CLASS__, 'quotedPrintableCallback' ], $string );
                $out .= '?=';
                return $out;
        }