MediaWiki  REL1_20
SpecialChangeEmail.php
Go to the documentation of this file.
00001 <?php
00029 class SpecialChangeEmail extends UnlistedSpecialPage {
00030 
00035         protected $mPassword;
00036 
00041         protected $mNewEmail;
00042 
00043         public function __construct() {
00044                 parent::__construct( 'ChangeEmail' );
00045         }
00046 
00050         function isListed() {
00051                 global $wgAuth;
00052                 return $wgAuth->allowPropChange( 'emailaddress' );
00053         }
00054 
00058         function execute( $par ) {
00059                 global $wgAuth;
00060 
00061                 $this->setHeaders();
00062                 $this->outputHeader();
00063 
00064                 $out = $this->getOutput();
00065                 $out->disallowUserJs();
00066                 $out->addModules( 'mediawiki.special.changeemail' );
00067 
00068                 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
00069                         $this->error( 'cannotchangeemail' );
00070                         return;
00071                 }
00072 
00073                 $user = $this->getUser();
00074                 $request = $this->getRequest();
00075 
00076                 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
00077                         $this->error( 'changeemail-no-info' );
00078                         return;
00079                 }
00080 
00081                 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
00082                         $this->doReturnTo();
00083                         return;
00084                 }
00085 
00086                 $this->checkReadOnly();
00087 
00088                 $this->mPassword = $request->getVal( 'wpPassword' );
00089                 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
00090 
00091                 if ( $request->wasPosted()
00092                         && $user->matchEditToken( $request->getVal( 'token' ) ) )
00093                 {
00094                         $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
00095                         if ( $info === true ) {
00096                                 $this->doReturnTo();
00097                         } elseif ( $info === 'eauth' ) {
00098                                 # Notify user that a confirmation email has been sent...
00099                                 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
00100                                         'eauthentsent', $user->getName() );
00101                                 $this->doReturnTo( 'soft' ); // just show the link to go back
00102                                 return; // skip form
00103                         }
00104                 }
00105 
00106                 $this->showForm();
00107         }
00108 
00112         protected function doReturnTo( $type = 'hard' ) {
00113                 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
00114                 if ( !$titleObj instanceof Title ) {
00115                         $titleObj = Title::newMainPage();
00116                 }
00117                 if ( $type == 'hard' ) {
00118                         $this->getOutput()->redirect( $titleObj->getFullURL() );
00119                 } else {
00120                         $this->getOutput()->addReturnTo( $titleObj );
00121                 }
00122         }
00123 
00127         protected function error( $msg ) {
00128                 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
00129         }
00130 
00131         protected function showForm() {
00132                 global $wgRequirePasswordforEmailChange;
00133                 $user = $this->getUser();
00134 
00135                 $oldEmailText = $user->getEmail()
00136                         ? $user->getEmail()
00137                         : $this->msg( 'changeemail-none' )->text();
00138 
00139                 $this->getOutput()->addHTML(
00140                         Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
00141                         Xml::openElement( 'form',
00142                                 array(
00143                                         'method' => 'post',
00144                                         'action' => $this->getTitle()->getLocalUrl(),
00145                                         'id' => 'mw-changeemail-form' ) ) . "\n" .
00146                         Html::hidden( 'token', $user->getEditToken() ) . "\n" .
00147                         Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
00148                         $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
00149                         Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
00150                 );
00151                 $items = array(
00152                         array( 'wpName', 'username', 'text', $user->getName() ),
00153                         array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
00154                         array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail ),
00155                 );
00156                 if ( $wgRequirePasswordforEmailChange ) {
00157                         $items[] = array( 'wpPassword', 'yourpassword', 'password', $this->mPassword );
00158                 }
00159 
00160                 $this->getOutput()->addHTML(
00161                         $this->pretty( $items ) .
00162                         "\n" .
00163                         "<tr>\n" .
00164                                 "<td></td>\n" .
00165                                 '<td class="mw-input">' .
00166                                         Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
00167                                         Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
00168                                 "</td>\n" .
00169                         "</tr>\n" .
00170                         Xml::closeElement( 'table' ) .
00171                         Xml::closeElement( 'form' ) .
00172                         Xml::closeElement( 'fieldset' ) . "\n"
00173                 );
00174         }
00175 
00180         protected function pretty( $fields ) {
00181                 $out = '';
00182                 foreach ( $fields as $list ) {
00183                         list( $name, $label, $type, $value ) = $list;
00184                         if( $type == 'text' ) {
00185                                 $field = htmlspecialchars( $value );
00186                         } else {
00187                                 $attribs = array( 'id' => $name );
00188                                 if ( $name == 'wpPassword' ) {
00189                                         $attribs[] = 'autofocus';
00190                                 }
00191                                 $field = Html::input( $name, $value, $type, $attribs );
00192                         }
00193                         $out .= "<tr>\n";
00194                         $out .= "\t<td class='mw-label'>";
00195                         if ( $type != 'text' ) {
00196                                 $out .= Xml::label( $this->msg( $label )->text(), $name );
00197                         } else {
00198                                 $out .=  $this->msg( $label )->escaped();
00199                         }
00200                         $out .= "</td>\n";
00201                         $out .= "\t<td class='mw-input'>";
00202                         $out .= $field;
00203                         $out .= "</td>\n";
00204                         $out .= "</tr>";
00205                 }
00206                 return $out;
00207         }
00208 
00215         protected function attemptChange( User $user, $pass, $newaddr ) {
00216                 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
00217                         $this->error( 'invalidemailaddress' );
00218                         return false;
00219                 }
00220 
00221                 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
00222                 if ( $throttleCount === true ) {
00223                         $this->error( 'login-throttled' );
00224                         return false;
00225                 }
00226 
00227                 global $wgRequirePasswordforEmailChange;
00228                 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
00229                         $this->error( 'wrongpassword' );
00230                         return false;
00231                 }
00232 
00233                 if ( $throttleCount ) {
00234                         LoginForm::clearLoginThrottle( $user->getName() );
00235                 }
00236 
00237                 $oldaddr = $user->getEmail();
00238                 $status = $user->setEmailWithConfirmation( $newaddr );
00239                 if ( !$status->isGood() ) {
00240                         $this->getOutput()->addHTML(
00241                                 '<p class="error">' .
00242                                 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
00243                                 '</p>' );
00244                         return false;
00245                 }
00246 
00247                 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
00248 
00249                 $user->saveSettings();
00250 
00251                 return $status->value;
00252         }
00253 }