w/s diff. mostly eol w/s and using only tabs of width 4 to indent.
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
index 83df8b2..098d936 100644 (file)
@@ -1,25 +1,47 @@
 <?php
 /**
+ * Implements Special:Emailuser
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup SpecialPage
  */
 
+/**
+ * A special page that allows users to send e-mails to other users
+ *
+ * @ingroup SpecialPage
+ */
 class SpecialEmailUser extends UnlistedSpecialPage {
        protected $mTarget;
-       
+
        public function __construct(){
                parent::__construct( 'Emailuser' );
        }
-       
+
        protected function getFormFields(){
                global $wgUser;
                return array(
                        'From' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $wgUser->getUserPage(), 
-                                       htmlspecialchars( $wgUser->getName() ) 
+                               'default' => $wgUser->getSkin()->link(
+                                       $wgUser->getUserPage(),
+                                       htmlspecialchars( $wgUser->getName() )
                                ),
                                'label-message' => 'emailfrom',
                                'id' => 'mw-emailuser-sender',
@@ -27,8 +49,8 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        'To' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $this->mTargetObj->getUserPage(), 
+                               'default' => $wgUser->getSkin()->link(
+                                       $this->mTargetObj->getUserPage(),
                                        htmlspecialchars( $this->mTargetObj->getName() )
                                ),
                                'label-message' => 'emailto',
@@ -60,13 +82,18 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        ),
                );
        }
-       
+
        public function execute( $par ) {
                global $wgRequest, $wgOut, $wgUser;
+
+               $this->setHeaders();
+               $this->outputHeader();
+               $wgOut->addModuleStyles( 'mediawiki.special' );
+
                $this->mTarget = is_null( $par )
-                       ? $wgRequest->getVal( 'wpTarget', '' )
+                       ? $wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
                        : $par;
-                       
+
                $ret = self::getTarget( $this->mTarget );
                if( $ret instanceof User ){
                        $this->mTargetObj = $ret;
@@ -74,7 +101,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        $wgOut->showErrorPage( "{$ret}title", "{$ret}text" );
                        return false;
                }
-       
+
                $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
                switch ( $error ) {
                        case null:
@@ -99,7 +126,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                                $wgOut->showErrorPage( $title, $msg, $params );
                                return;
                }
-               
+
                $form = new HTMLForm( $this->getFormFields() );
                $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
                $form->setSubmitText( wfMsg( 'emailsend' ) );
@@ -107,15 +134,15 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
                $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
                $form->loadData();
-               
+
                if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ){
                        return false;
                }
-               
+
                $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
                $result = $form->show();
-               
-               if( $result === true ){
+
+               if( $result === true || ( $result instanceof Status && $result->isGood() ) ){
                        $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
                        $wgOut->addWikiMsg( 'emailsenttext' );
                        $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
@@ -134,13 +161,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        return 'notarget';
                }
 
-               $nt = Title::newFromURL( $target );
-               if ( !$nt instanceof Title ) {
-                       wfDebug( "Target is invalid title.\n" );
-                       return 'notarget';
-               }
-
-               $nu = User::newFromName( $nt->getText() );
+               $nu = User::newFromName( $target );
                if( !$nu instanceof User || !$nu->getId() ) {
                        wfDebug( "Target is invalid user.\n" );
                        return 'notarget';
@@ -167,11 +188,11 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                if( !$wgEnableEmail || !$wgEnableUserEmail ){
                        return 'usermaildisabled';
                }
-               
+
                if( !$user->isAllowed( 'sendemail' ) ) {
                        return 'badaccess';
                }
-               
+
                if( !$user->isEmailConfirmed() ){
                        return 'mailnologin';
                }
@@ -198,13 +219,14 @@ class SpecialEmailUser extends UnlistedSpecialPage {
 
        /**
         * Really send a mail. Permissions should have been checked using
-        * getPermissionsError(). It is probably also a good 
+        * getPermissionsError(). It is probably also a good
         * idea to check the edit token and ping limiter in advance.
         *
-        * @return Mixed: True on success, String on error
+        * @return Mixed: Status object, or potentially a String on error
+        * or maybe even true on success if anything uses the EmailUser hook.
         */
        public static function submit( $data ) {
-               global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
+               global $wgUser, $wgUserEmailUseReplyTo;
 
                $target = self::getTarget( $data['Target'] );
                if( !$target instanceof User ){
@@ -217,17 +239,17 @@ class SpecialEmailUser extends UnlistedSpecialPage {
 
                // Add a standard footer and trim up trailing newlines
                $text = rtrim( $text ) . "\n\n-- \n";
-               $text .= wfMsgExt( 
+               $text .= wfMsgExt(
                        'emailuserfooter',
-                       array( 'content', 'parsemag' ), 
-                       array( $from->name, $to->name ) 
+                       array( 'content', 'parsemag' ),
+                       array( $from->name, $to->name )
                );
 
                $error = '';
                if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
                        return $error;
                }
-               
+
                if( $wgUserEmailUseReplyTo ) {
                        // Put the generic wiki autogenerated address in the From:
                        // header and reserve the user for Reply-To.
@@ -235,8 +257,8 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        // This is a bit ugly, but will serve to differentiate
                        // wiki-borne mails from direct mails and protects against
                        // SPF and bounce problems with some mailers (see below).
-                       global $wgPasswordSender;
-                       $mailFrom = new MailAddress( $wgPasswordSender );
+                       global $wgPasswordSender, $wgPasswordSenderName;
+                       $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
                        $replyTo = $from;
                } else {
                        // Put the sending user's e-mail address in the From: header.
@@ -256,35 +278,27 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        $replyTo = null;
                }
 
-               $mailResult = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
+               $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
 
-               if( WikiError::isError( $mailResult ) && false ) {
-                       return $mailResult->getMessage();
+               if( !$status->isGood() ) {
+                       return $status;
                } else {
                        // if the user requested a copy of this mail, do this now,
-                       // unless they are emailing themselves, in which case one 
+                       // unless they are emailing themselves, in which case one
                        // copy of the message is sufficient.
                        if ( $data['CCMe'] && $to != $from ) {
                                $cc_subject = wfMsg(
-                                       'emailccsubject', 
-                                       $target->getName(), 
+                                       'emailccsubject',
+                                       $target->getName(),
                                        $subject
                                );
                                wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
-                               $ccResult = UserMailer::send( $from, $from, $cc_subject, $text );
-                               if( WikiError::isError( $ccResult ) ) {
-                                       // At this stage, the user's CC mail has failed, but their
-                                       // original mail has succeeded. It's unlikely, but still, 
-                                       // what to do? We can either show them an error, or we can 
-                                       // say everything was fine, or we can say we sort of failed 
-                                       // AND sort of succeeded. Of these options, simply saying 
-                                       // there was an error is probably best.
-                                       return $ccResult->getMessage();
-                               }
+                               $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
+                               $status->merge( $ccStatus );
                        }
 
                        wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
-                       return true;
+                       return $status;
                }
        }
 }