Merge "Add file extension mapping for opus"
[lhc/web/wiklou.git] / includes / mail / UserMailer.php
index 7c5f18d..85595f1 100644 (file)
@@ -64,7 +64,7 @@ class UserMailer {
         *
         * @return string
         */
-       static function arrayToHeaderString( $headers, $endl = "\n" ) {
+       static function arrayToHeaderString( $headers, $endl = PHP_EOL ) {
                $strings = array();
                foreach ( $headers as $name => $value ) {
                        // Prevent header injection by stripping newlines from value
@@ -115,23 +115,17 @@ class UserMailer {
         * @return Status
         */
        public static function send( $to, $from, $subject, $body, $options = array() ) {
-               global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams, $wgAllowHTMLEmail;
+               global $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 {
+               if ( !is_array( $options ) ) {
                        // Old calling style
                        wfDeprecated( __METHOD__ . ' with $replyto as 5th parameter', '1.26' );
-                       $replyto = $options;
+                       $options = array( 'replyTo' => $options );
                        if ( func_num_args() === 6 ) {
-                               $contentType = func_get_arg( 5 );
+                               $options['contentType'] = func_get_arg( 5 );
                        }
                }
 
-               $mime = null;
                if ( !is_array( $to ) ) {
                        $to = array( $to );
                }
@@ -178,32 +172,100 @@ class UserMailer {
                        return Status::newFatal( 'user-mail-no-addy' );
                }
 
-               // Forge email headers
-               // -------------------
-               //
-               // WARNING
-               //
-               // DO NOT add To: or Subject: headers at this step. They need to be
-               // handled differently depending upon the mailer we are going to use.
-               //
-               // To:
-               //  PHP mail() first argument is the mail receiver. The argument is
-               //  used as a recipient destination and as a To header.
-               //
-               //  PEAR mailer has a recipient argument which is only used to
-               //  send the mail. If no To header is given, PEAR will set it to
-               //  to 'undisclosed-recipients:'.
-               //
-               //  NOTE: To: is for presentation, the actual recipient is specified
-               //  by the mailer using the Rcpt-To: header.
-               //
-               // Subject:
-               //  PHP mail() second argument to pass the subject, passing a Subject
-               //  as an additional header will result in a duplicate header.
-               //
-               //  PEAR mailer should be passed a Subject header.
-               //
-               // -- hashar 20120218
+               // 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', array( &$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(
+                                               array( $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 = array()
+       ) {
+               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'] : array();
+
+               // Allow transformation of content, such as encrypting/signing
+               $error = false;
+               if ( !Hooks::run( 'UserMailerTransformContent', array( $to, $from, &$body, &$error ) ) ) {
+                       if ( $error ) {
+                               return Status::newFatal( 'php-mail-error', $error );
+                       } else {
+                               return Status::newFatal( 'php-mail-error-unknown' );
+                       }
+               }
+
+               /**
+                * Forge email headers
+                * -------------------
+                *
+                * WARNING
+                *
+                * DO NOT add To: or Subject: headers at this step. They need to be
+                * handled differently depending upon the mailer we are going to use.
+                *
+                * To:
+                *  PHP mail() first argument is the mail receiver. The argument is
+                *  used as a recipient destination and as a To header.
+                *
+                *  PEAR mailer has a recipient argument which is only used to
+                *  send the mail. If no To header is given, PEAR will set it to
+                *  to 'undisclosed-recipients:'.
+                *
+                *  NOTE: To: is for presentation, the actual recipient is specified
+                *  by the mailer using the Rcpt-To: header.
+                *
+                * Subject:
+                *  PHP mail() second argument to pass the subject, passing a Subject
+                *  as an additional header will result in a duplicate header.
+                *
+                *  PEAR mailer should be passed a Subject header.
+                *
+                * -- hashar 20120218
+                */
 
                $headers['From'] = $from->toString();
                $returnPath = $from->address;
@@ -230,11 +292,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
@@ -274,6 +332,17 @@ class UserMailer {
                        $headers['Content-transfer-encoding'] = '8bit';
                }
 
+               // allow transformation of MIME-encoded message
+               if ( !Hooks::run( 'UserMailerTransformMessage',
+                       array( $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', array( $headers, $to, $from, $subject, $body ) );
                if ( $ret === false ) {
                        // the hook implementation will return false to skip regular mail sending
@@ -326,9 +395,7 @@ class UserMailer {
                        MediaWiki\restoreWarnings();
                        return Status::newGood();
                } else {
-                       //
                        // PHP mail()
-                       //
                        if ( count( $to ) > 1 ) {
                                $headers['To'] = 'undisclosed-recipients:;';
                        }