Handle multiple warnings correctly in ApiBase::setWarning(). Calling this function...
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
index 76add30..2395822 100644 (file)
@@ -1,61 +1,37 @@
 <?php
 /**
- *
- * @addtogroup SpecialPage
+ * @file
+ * @ingroup SpecialPage
  */
 
 /**
  * @todo document
  */
 function wfSpecialEmailuser( $par ) {
-       global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
+       global $wgRequest, $wgUser, $wgOut;
 
-       if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
-               $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
-               return;
-       }
-
-       if( !$wgUser->canSendEmail() ) {
-               wfDebug( "User can't send.\n" );
-               $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
-               return;
+       $target = isset($par) ? $par : $wgRequest->getVal( 'target' );  
+       $error = EmailUserForm::getPermissionsError( $target );
+       if ( $error ) {
+               if ( $error[0] === "blockedemailuser" ) {
+                       $wgOut->blockedPage();
+                       return;
+               } else {
+                       $wgOut->showErrorPage( $error[0], $error[1] );
+                       return;
+               }
        }
 
+       $form = EmailUserForm::newFromURL( $target,
+                       $wgRequest->getText( 'wpText' ),
+                       $wgRequest->getText( 'wpSubject' ),
+                       $wgRequest->getBool( 'wpCCMe' ) );
+       
        $action = $wgRequest->getVal( 'action' );
-       $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
-       if ( "" == $target ) {
-               wfDebug( "Target is empty.\n" );
-               $wgOut->showErrorPage( "notargettitle", "notargettext" );
-               return;
-       }
-
-       $nt = Title::newFromURL( $target );
-       if ( is_null( $nt ) ) {
-               wfDebug( "Target is invalid title.\n" );
-               $wgOut->showErrorPage( "notargettitle", "notargettext" );
-               return;
-       }
-
-       $nu = User::newFromName( $nt->getText() );
-       if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
-               wfDebug( "Target is invalid user or can't receive.\n" );
-               $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
-               return;
-       }
-
-       if ( $wgUser->isBlockedFromEmailUser() ) {
-               // User has been blocked from sending e-mail. Show the std blocked form.
-               wfDebug( "User is blocked from sending e-mail.\n" );
-               $wgOut->blockedPage();
-               return;
-       }
-
-       $f = new EmailUserForm( $nu );
-
        if ( "success" == $action ) {
-               $f->showSuccess( $nu );
+               $form->showSuccess();
        } else if ( "submit" == $action && $wgRequest->wasPosted() &&
-                               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) 
+                               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
        {
                # Check against the rate limiter
                if( $wgUser->pingLimiter( 'emailuser' ) ) {
@@ -63,15 +39,24 @@ function wfSpecialEmailuser( $par ) {
                        return;
                }
 
-               $f->doSubmit();
+               $result = $form->doSubmit();
+               
+               if ( !is_null( $result ) ) {
+                       $wgOut->addHTML( wfMsg( "usermailererror" ) .
+                                       ' ' . htmlspecialchars( $result->getMessage() ) );
+               } else {
+                       $titleObj = SpecialPage::getTitleFor( "Emailuser" );
+                       $encTarget = wfUrlencode( $form->getTarget()->getName() );
+                       $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
+               }
        } else {
-               $f->showForm();
+               $form->showForm();
        }
 }
 
 /**
  * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
- * @addtogroup SpecialPage
+ * @ingroup SpecialPage
  */
 class EmailUserForm {
 
@@ -82,28 +67,30 @@ class EmailUserForm {
        /**
         * @param User $target
         */
-       function EmailUserForm( $target ) {
-               global $wgRequest;
+       function EmailUserForm( $target, $text, $subject, $cc_me ) {
                $this->target = $target;
-               $this->text = $wgRequest->getText( 'wpText' );
-               $this->subject = $wgRequest->getText( 'wpSubject' );
-               $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
+               $this->text = $text;
+               $this->subject = $subject;
+               $this->cc_me = $cc_me;
        }
 
        function showForm() {
                global $wgOut, $wgUser;
+               $skin = $wgUser->getSkin();
 
                $wgOut->setPagetitle( wfMsg( "emailpage" ) );
-               $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
+               $wgOut->addWikiMsg( "emailpagetext" );
 
                if ( $this->subject === "" ) {
-                       $this->subject = wfMsg( "defemailsubject" );
+                       $this->subject = wfMsgForContent( "defemailsubject" );
                }
 
                $emf = wfMsg( "emailfrom" );
-               $sender = $wgUser->getName();
+               $senderLink = $skin->makeLinkObj(
+                       $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
                $emt = wfMsg( "emailto" );
-               $rcpt = $this->target->getName();
+               $recipientLink = $skin->makeLinkObj(
+                       $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) );
                $emr = wfMsg( "emailsubject" );
                $emm = wfMsg( "emailmessage" );
                $ems = wfMsg( "emailsend" );
@@ -119,10 +106,10 @@ class EmailUserForm {
 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
 <table border='0' id='mailheader'><tr>
 <td align='right'>{$emf}:</td>
-<td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
+<td align='left'><strong>{$senderLink}</strong></td>
 </tr><tr>
 <td align='right'>{$emt}:</td>
-<td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
+<td align='left'><strong>{$recipientLink}</strong></td>
 </tr><tr>
 <td align='right'>{$emr}:</td>
 <td align='left'>
@@ -140,15 +127,20 @@ class EmailUserForm {
 
        }
 
+       /*
+        * Really send a mail. Permissions should have been checked using 
+        * EmailUserForm::getPermissionsError. It is probably also a good idea to
+        * check the edit token and ping limiter in advance.
+        */
        function doSubmit() {
-               global $wgOut, $wgUser, $wgUserEmailUseReplyTo;
+               global $wgUser, $wgUserEmailUseReplyTo;
 
                $to = new MailAddress( $this->target );
                $from = new MailAddress( $wgUser );
                $subject = $this->subject;
 
                if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
-                       
+
                        if( $wgUserEmailUseReplyTo ) {
                                // Put the generic wiki autogenerated address in the From:
                                // header and reserve the user for Reply-To.
@@ -176,14 +168,14 @@ class EmailUserForm {
                                $mailFrom = $from;
                                $replyTo = null;
                        }
-
+                       
                        $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
 
                        if( WikiError::isError( $mailResult ) ) {
-                               $wgOut->addHTML( wfMsg( "usermailererror" ) .
-                                       ' ' . htmlspecialchars( $mailResult->getMessage() ) );
-                       } else {
+                               return $mailResult;                     
                                
+                       } else {
+
                                // if the user requested a copy of this mail, do this now,
                                // unless they are emailing themselves, in which case one copy of the message is sufficient.
                                if ($this->cc_me && $to != $from) {
@@ -191,32 +183,78 @@ class EmailUserForm {
                                        if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
                                                $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
                                                if( WikiError::isError( $ccResult ) ) {
-                                                       // At this stage, the user's CC mail has failed, but their 
+                                                       // 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, 
+                                                       // or we can say we sort of failed AND sort of succeeded. Of these options,
                                                        // simply saying there was an error is probably best.
-                                                       $wgOut->addHTML( wfMsg( "usermailererror" ) .
-                                                               ' ' . htmlspecialchars( $ccResult->getMessage() ) );
-                                                       return;
+                                                       return $ccResult->getMessage();
                                                }
                                        }
                                }
-                               
-                               $titleObj = SpecialPage::getTitleFor( "Emailuser" );
-                               $encTarget = wfUrlencode( $this->target->getName() );
-                               $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
+
                                wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
+                               return;
                        }
                }
        }
 
-       function showSuccess( &$user ) {
+       function showSuccess( &$user = null ) {
                global $wgOut;
+               
+               if ( is_null($user) )
+                       $user = $this->target;
 
                $wgOut->setPagetitle( wfMsg( "emailsent" ) );
                $wgOut->addHTML( wfMsg( "emailsenttext" ) );
 
                $wgOut->returnToMain( false, $user->getUserPage() );
        }
+       
+       function getTarget() {
+               return $this->target;
+       }
+       
+       static function getPermissionsError ( $target ) {
+               global $wgUser, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
+
+               if( !( $wgEnableEmail && $wgEnableUserEmail ) ) 
+                       return array( "nosuchspecialpage", "nospecialpagetext" );
+       
+               if( !$wgUser->canSendEmail() ) {
+                       wfDebug( "User can't send.\n" );
+                       return array( "mailnologin", "mailnologintext" );
+               }
+               
+               if( $wgUser->isBlockedFromEmailuser() ) {
+                       wfDebug( "User is blocked from sending e-mail.\n" );
+                       return array( "blockedemailuser", "" );
+               }
+       
+               if ( "" == $target ) {
+                       wfDebug( "Target is empty.\n" );
+                       return array( "notargettitle", "notargettext" );
+               }
+       
+               $nt = Title::newFromURL( $target );
+               if ( is_null( $nt ) ) {
+                       wfDebug( "Target is invalid title.\n" );
+                       return array( "notargettitle", "notargettext" );
+               }
+       
+               $nu = User::newFromName( $nt->getText() );
+               if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
+                       wfDebug( "Target is invalid user or can't receive.\n" );
+                       return array( "noemailtitle", "noemailtext" );
+               }
+               
+               return;
+       }
+       
+       static function newFromURL( $target, $text, $subject, $cc_me )
+       {
+               $nt = Title::newFromURL( $target );
+               $nu = User::newFromName( $nt->getText() );
+               return new EmailUserForm( $nu, $text, $subject, $cc_me );
+       }
 }