MediaWiki  REL1_22
SpecialConfirmemail.php
Go to the documentation of this file.
00001 <?php
00032 class EmailConfirmation extends UnlistedSpecialPage {
00033     public function __construct() {
00034         parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
00035     }
00036 
00042     function execute( $code ) {
00043         $this->setHeaders();
00044 
00045         $this->checkReadOnly();
00046         $this->checkPermissions();
00047 
00048         // This could also let someone check the current email address, so
00049         // require both permissions.
00050         if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
00051             throw new PermissionsError( 'viewmyprivateinfo' );
00052         }
00053 
00054         if ( $code === null || $code === '' ) {
00055             if ( $this->getUser()->isLoggedIn() ) {
00056                 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
00057                     $this->showRequestForm();
00058                 } else {
00059                     $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
00060                 }
00061             } else {
00062                 $llink = Linker::linkKnown(
00063                     SpecialPage::getTitleFor( 'Userlogin' ),
00064                     $this->msg( 'loginreqlink' )->escaped(),
00065                     array(),
00066                     array( 'returnto' => $this->getTitle()->getPrefixedText() )
00067                 );
00068                 $this->getOutput()->addHTML(
00069                     $this->msg( 'confirmemail_needlogin' )->rawParams( $llink )->parse()
00070                 );
00071             }
00072         } else {
00073             $this->attemptConfirm( $code );
00074         }
00075     }
00076 
00080     function showRequestForm() {
00081         $user = $this->getUser();
00082         $out = $this->getOutput();
00083 
00084         if ( $this->getRequest()->wasPosted() &&
00085             $user->matchEditToken( $this->getRequest()->getText( 'token' ) )
00086         ) {
00087             $status = $user->sendConfirmationMail();
00088             if ( $status->isGood() ) {
00089                 $out->addWikiMsg( 'confirmemail_sent' );
00090             } else {
00091                 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
00092             }
00093         } else {
00094             if ( $user->isEmailConfirmed() ) {
00095                 // date and time are separate parameters to facilitate localisation.
00096                 // $time is kept for backward compat reasons.
00097                 // 'emailauthenticated' is also used in SpecialPreferences.php
00098                 $lang = $this->getLanguage();
00099                 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
00100                 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
00101                 $d = $lang->userDate( $emailAuthenticated, $user );
00102                 $t = $lang->userTime( $emailAuthenticated, $user );
00103                 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
00104             }
00105 
00106             if ( $user->isEmailConfirmationPending() ) {
00107                 $out->wrapWikiMsg(
00108                     "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
00109                     'confirmemail_pending'
00110                 );
00111             }
00112 
00113             $out->addWikiMsg( 'confirmemail_text' );
00114             $form = Html::openElement(
00115                 'form',
00116                 array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL() )
00117             ) . "\n";
00118             $form .= Html::hidden( 'token', $user->getEditToken() ) . "\n";
00119             $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
00120             $form .= Html::closeElement( 'form' ) . "\n";
00121             $out->addHTML( $form );
00122         }
00123     }
00124 
00131     function attemptConfirm( $code ) {
00132         $user = User::newFromConfirmationCode( $code );
00133         if ( !is_object( $user ) ) {
00134             $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
00135 
00136             return;
00137         }
00138 
00139         $user->confirmEmail();
00140         $user->saveSettings();
00141         $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
00142         $this->getOutput()->addWikiMsg( $message );
00143 
00144         if ( !$this->getUser()->isLoggedIn() ) {
00145             $title = SpecialPage::getTitleFor( 'Userlogin' );
00146             $this->getOutput()->returnToMain( true, $title );
00147         }
00148     }
00149 }
00150 
00157 class EmailInvalidation extends UnlistedSpecialPage {
00158     public function __construct() {
00159         parent::__construct( 'Invalidateemail', 'editmyprivateinfo' );
00160     }
00161 
00162     function execute( $code ) {
00163         $this->setHeaders();
00164         $this->checkReadOnly();
00165         $this->checkPermissions();
00166         $this->attemptInvalidate( $code );
00167     }
00168 
00175     function attemptInvalidate( $code ) {
00176         $user = User::newFromConfirmationCode( $code );
00177         if ( !is_object( $user ) ) {
00178             $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
00179 
00180             return;
00181         }
00182 
00183         $user->invalidateEmail();
00184         $user->saveSettings();
00185         $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
00186 
00187         if ( !$this->getUser()->isLoggedIn() ) {
00188             $this->getOutput()->returnToMain();
00189         }
00190     }
00191 }