MediaWiki  REL1_24
SpecialChangeEmail.php
Go to the documentation of this file.
00001 <?php
00029 class SpecialChangeEmail extends FormSpecialPage {
00033     private $status;
00034 
00035     public function __construct() {
00036         parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
00037     }
00038 
00042     function isListed() {
00043         global $wgAuth;
00044 
00045         return $wgAuth->allowPropChange( 'emailaddress' );
00046     }
00047 
00052     function execute( $par ) {
00053         $out = $this->getOutput();
00054         $out->disallowUserJs();
00055         $out->addModules( 'mediawiki.special.changeemail' );
00056 
00057         return parent::execute( $par );
00058     }
00059 
00060     protected function checkExecutePermissions( User $user ) {
00061         global $wgAuth;
00062 
00063         if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
00064             throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
00065         }
00066 
00067         $this->requireLogin( 'changeemail-no-info' );
00068 
00069         // This could also let someone check the current email address, so
00070         // require both permissions.
00071         if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
00072             throw new PermissionsError( 'viewmyprivateinfo' );
00073         }
00074 
00075         parent::checkExecutePermissions( $user );
00076     }
00077 
00078     protected function getFormFields() {
00079         $user = $this->getUser();
00080 
00081         $fields = array(
00082             'Name' => array(
00083                 'type' => 'info',
00084                 'label-message' => 'username',
00085                 'default' => $user->getName(),
00086             ),
00087             'OldEmail' => array(
00088                 'type' => 'info',
00089                 'label-message' => 'changeemail-oldemail',
00090                 'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
00091             ),
00092             'NewEmail' => array(
00093                 'type' => 'email',
00094                 'label-message' => 'changeemail-newemail',
00095             ),
00096         );
00097 
00098         if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
00099             $fields['Password'] = array(
00100                 'type' => 'password',
00101                 'label-message' => 'changeemail-password',
00102                 'autofocus' => true,
00103             );
00104         }
00105 
00106         return $fields;
00107     }
00108 
00109     protected function alterForm( HTMLForm $form ) {
00110         $form->setDisplayFormat( 'vform' );
00111         $form->setId( 'mw-changeemail-form' );
00112         $form->setTableId( 'mw-changeemail-table' );
00113         $form->setWrapperLegend( false );
00114         $form->setSubmitTextMsg( 'changeemail-submit' );
00115         $form->addHiddenField( 'returnto', $this->getRequest()->getVal( 'returnto' ) );
00116     }
00117 
00118     public function onSubmit( array $data ) {
00119         if ( $this->getRequest()->getBool( 'wpCancel' ) ) {
00120             $status = Status::newGood( true );
00121         } else {
00122             $password = isset( $data['Password'] ) ? $data['Password'] : null;
00123             $status = $this->attemptChange( $this->getUser(), $password, $data['NewEmail'] );
00124         }
00125 
00126         $this->status = $status;
00127 
00128         return $status;
00129     }
00130 
00131     public function onSuccess() {
00132         $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
00133         if ( !$titleObj instanceof Title ) {
00134             $titleObj = Title::newMainPage();
00135         }
00136 
00137         if ( $this->status->value === true ) {
00138             $this->getOutput()->redirect( $titleObj->getFullURL() );
00139         } elseif ( $this->status->value === 'eauth' ) {
00140             # Notify user that a confirmation email has been sent...
00141             $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
00142                 'eauthentsent', $this->getUser()->getName() );
00143             $this->getOutput()->addReturnTo( $titleObj ); // just show the link to go back
00144         }
00145     }
00146 
00153     protected function attemptChange( User $user, $pass, $newaddr ) {
00154         global $wgAuth;
00155 
00156         if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
00157             return Status::newFatal( 'invalidemailaddress' );
00158         }
00159 
00160         $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
00161         if ( $throttleCount === true ) {
00162             $lang = $this->getLanguage();
00163             $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
00164             return Status::newFatal(
00165                 'changeemail-throttled',
00166                 $lang->formatDuration( $throttleInfo['seconds'] )
00167             );
00168         }
00169 
00170         if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' )
00171             && !$user->checkTemporaryPassword( $pass )
00172             && !$user->checkPassword( $pass )
00173         ) {
00174             return Status::newFatal( 'wrongpassword' );
00175         }
00176 
00177         if ( $throttleCount ) {
00178             LoginForm::clearLoginThrottle( $user->getName() );
00179         }
00180 
00181         $oldaddr = $user->getEmail();
00182         $status = $user->setEmailWithConfirmation( $newaddr );
00183         if ( !$status->isGood() ) {
00184             return $status;
00185         }
00186 
00187         wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
00188 
00189         $user->saveSettings();
00190 
00191         $wgAuth->updateExternalDB( $user );
00192 
00193         return $status;
00194     }
00195 
00196     public function requiresUnblock() {
00197         return false;
00198     }
00199 
00200     protected function getGroupName() {
00201         return 'users';
00202     }
00203 }