MediaWiki r47529 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r47528‎ | r47529 (on ViewVC)‎ | r47530 >
Date:03:47, 20 February 2009
Author:tstarling
Status:deferred (Comments)
Tags:
Comment:
* Removed MW 1.9 compatibility
* Use early returns to avoid excessive indenting
* Fixed a bug causing the "email me a copy" feature to not work if $wgUserEmailUseReplyTo=false
* Make the "email me a copy" emails come from the contact sender instead of the person who submitted the form. Makes more sense to me.
* Rename $from and $to so we don't have to send mails to from, or from to
Modified paths:

Diff [purge]

Index: trunk/extensions/ContactPage/SpecialContact.php
===================================================================
--- trunk/extensions/ContactPage/SpecialContact.php	(revision 47528)
+++ trunk/extensions/ContactPage/SpecialContact.php	(revision 47529)
@@ -15,9 +15,6 @@
 	die( 1 );
 }
 
-global $IP; #needed when called from the autoloader
-require_once("$IP/includes/UserMailer.php");
-
 /**
  * Provides the contact form
  * @ingroup SpecialPage
@@ -271,16 +268,17 @@
 
 		wfDebug( __METHOD__ . ": start\n" );
 
-		$to = new MailAddress( $this->target );
-		$replyto = NULL;
+		$targetAddress = new MailAddress( $this->target );
+		$replyto = null;
+		$contactSender = new MailAddress( $csender, $cname );
 
 		if ( !$this->fromaddress ) {
-			$from = new MailAddress( $csender, $cname );
-		} else if ( $wgUserEmailUseReplyTo ) {
-			$from = new MailAddress( $csender, $cname );
-			$replyto = new MailAddress( $this->fromaddress, $this->fromname );
+			$submitterAddress = $contactSender;
 		} else {
-			$from = new MailAddress( $this->fromaddress, $this->fromname );
+			$submitterAddress = new MailAddress( $this->fromaddress, $this->fromname );
+			if ( $wgUserEmailUseReplyTo ) {
+				$replyto = $submitterAddress;
+			}
 		}
 
 		$subject = trim( $this->subject );
@@ -295,48 +293,49 @@
 			$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $this->fromaddress );
 		}
 
-		if( wfRunHooks( 'ContactForm', array( &$to, &$replyto, &$subject, &$this->text ) ) ) {
+		if( !wfRunHooks( 'ContactForm', array( &$targetAddress, &$replyto, &$subject, &$this->text ) ) ) {
+			wfDebug( __METHOD__ . ": aborted by hook\n" );
+			return;
+		}
 
-			wfDebug( __METHOD__ . ": sending mail from ".$from->toString()." to ".$to->toString()." replyto ".( $replyto == null ? '-/-' : $replyto->toString() )."\n" );
+		wfDebug( __METHOD__ . ": sending mail from ".$submitterAddress->toString().
+			" to ".$targetAddress->toString().
+			" replyto ".( $replyto == null ? '-/-' : $replyto->toString() )."\n" );
 
-			#HACK: in MW 1.9, replyto must be a string, in MW 1.10 it must be an object!
-			$ver = preg_replace( '![^\d._+]!', '', $GLOBALS['wgVersion'] );
-			$replyaddr = $replyto == null
-					? NULL : version_compare( $ver, '1.10', '<' )
-						? $replyto->toString() : $replyto;
+		$mailResult = UserMailer::send( $targetAddress, $submitterAddress, $subject, $this->text, $replyto );
 
-			$mailResult = userMailer( $to, $from, $subject, $this->text, $replyaddr );
+		if( WikiError::isError( $mailResult ) ) {
+			$wgOut->addWikiMsg( 'usermailererror' ) . $mailResult->getMessage();
+			wfDebug( __METHOD__ . ": got error from UserMailer: " . $mailResult->getMessage() . "\n" );
+			return;
+		}
 
-			if( WikiError::isError( $mailResult ) ) {
-				$wgOut->addWikiMsg( 'usermailererror' ) . $mailResult->getMessage();
-			} 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 && $replyto ) {
-					$cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
-					if( wfRunHooks( 'ContactForm', array( &$from, &$replyto, &$cc_subject, &$this->text ) ) ) {
-						wfDebug( __METHOD__ . ": sending cc mail from ".$from->toString()." to ".$replyto->toString()."\n" );
-						$ccResult = userMailer( $replyto, $from, $cc_subject, $this->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.
-							$wgOut->addWikiText( wfMsg( 'usermailererror' ) . $ccResult );
-							return;
-						}
-					}
+		// 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 && $this->fromaddress ) {
+			$cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
+			if( wfRunHooks( 'ContactForm', array( &$submitterAddress, &$contactSender, &$cc_subject, &$this->text ) ) ) {
+				wfDebug( __METHOD__ . ": sending cc mail from ".$contactSender->toString().
+					" to ".$submitterAddress->toString()."\n" );
+				$ccResult = UserMailer::send( $submitterAddress, $contactSender, $cc_subject, $this->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.
+					$wgOut->addWikiText( wfMsg( 'usermailererror' ) . $ccResult );
+					return;
 				}
-
-				wfDebug( __METHOD__ . ": success\n" );
-
-				$titleObj = SpecialPage::getTitleFor( 'Contact' );
-				$wgOut->redirect( $titleObj->getFullURL( "action=success" ) );
-				wfRunHooks( 'ContactFromComplete', array( $to, $replyto, $subject, $this->text ) );
 			}
 		}
 
+		wfDebug( __METHOD__ . ": success\n" );
+
+		$titleObj = SpecialPage::getTitleFor( 'Contact' );
+		$wgOut->redirect( $titleObj->getFullURL( "action=success" ) );
+		wfRunHooks( 'ContactFromComplete', array( $targetAddress, $replyto, $subject, $this->text ) );
+
 		wfDebug( __METHOD__ . ": end\n" );
 	}
 
@@ -348,4 +347,4 @@
 
 		$wgOut->returnToMain( false );
 	}
-}
\ No newline at end of file
+}

Comments

#Comment by GreenReaper (Talk | contribs)   21:13, 31 July 2009

The section in doSubmit where it works out the correct From and ReplyTo addresses is incorrect and breaks the intended functionality of $wgUserEmailUseReplyTo (sending from the server address, so it gets past spam filters that check that the origin has the authority to send email as that person).

I suggest the following code instead:

               if ( !$this->fromaddress ) {
                       $submitterAddress = $contactSender;
               } else {
                       if ( $wgUserEmailUseReplyTo ) {
                               $submitterAddress = $contactSender;
                               $replyto = $submitterAddress;
                       } else {
                               $submitterAddress = new MailAddress( $this->fromaddress, $this->fromname );
                       }
               }
#Comment by GreenReaper (Talk | contribs)   21:30, 31 July 2009

Or shorter and more correctly . . .

              if ( !$this->fromaddress ) {
                      $submitterAddress = $contactSender;
              } else {
                      $submitterAddress = new MailAddress( $this->fromaddress, $this->fromname );
                      if ( $wgUserEmailUseReplyTo ) {
                              # Use the submitter as Reply-To: and the standard contact address as From:
                              # Avoids spam filters that notice we're sending from a domain we don't run
                              $replyto = $submitterAddress;
                              $submitterAddress = $contactSender;
                      }
              }

Status & tagging log

Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox