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