API: (bug 17703) Fix regression from r47039 causing error code and error text to...
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blob9c6f857da70b8174d1d1849bda84cf26479950a4
1 <?php
3 /**
4 * Special page allows users to request email confirmation message, and handles
5 * processing of the confirmation code when the link in the email is followed
7 * @ingroup SpecialPage
8 * @author Brion Vibber
9 * @author Rob Church <robchur@gmail.com>
11 class EmailConfirmation extends UnlistedSpecialPage {
13 /**
14 * Constructor
16 public function __construct() {
17 parent::__construct( 'Confirmemail' );
20 /**
21 * Main execution point
23 * @param $code Confirmation code passed to the page
25 function execute( $code ) {
26 global $wgUser, $wgOut;
27 $this->setHeaders();
28 if( empty( $code ) ) {
29 if( $wgUser->isLoggedIn() ) {
30 if( User::isValidEmailAddr( $wgUser->getEmail() ) ) {
31 $this->showRequestForm();
32 } else {
33 $wgOut->addWikiMsg( 'confirmemail_noemail' );
35 } else {
36 $title = SpecialPage::getTitleFor( 'Userlogin' );
37 $self = SpecialPage::getTitleFor( 'Confirmemail' );
38 $skin = $wgUser->getSkin();
39 $llink = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'loginreqlink' ),
40 'returnto=' . $self->getPrefixedUrl() );
41 $wgOut->addHTML( wfMsgWikiHtml( 'confirmemail_needlogin', $llink ) );
43 } else {
44 $this->attemptConfirm( $code );
48 /**
49 * Show a nice form for the user to request a confirmation mail
51 function showRequestForm() {
52 global $wgOut, $wgUser, $wgLang, $wgRequest;
53 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
54 $ok = $wgUser->sendConfirmationMail();
55 if ( WikiError::isError( $ok ) ) {
56 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $ok->toString() );
57 } else {
58 $wgOut->addWikiMsg( 'confirmemail_sent' );
60 } else {
61 if( $wgUser->isEmailConfirmed() ) {
62 // date and time are separate parameters to facilitate localisation.
63 // $time is kept for backward compat reasons.
64 // 'emailauthenticated' is also used in SpecialPreferences.php
65 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
66 $d = $wgLang->date( $wgUser->mEmailAuthenticated, true );
67 $t = $wgLang->time( $wgUser->mEmailAuthenticated, true );
68 $wgOut->addWikiMsg( 'emailauthenticated', $time, $d, $t );
70 if( $wgUser->isEmailConfirmationPending() ) {
71 $wgOut->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">$1</div>", 'confirmemail_pending' );
73 $wgOut->addWikiMsg( 'confirmemail_text' );
74 $self = SpecialPage::getTitleFor( 'Confirmemail' );
75 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
76 $form .= Xml::hidden( 'token', $wgUser->editToken() );
77 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
78 $form .= Xml::closeElement( 'form' );
79 $wgOut->addHTML( $form );
83 /**
84 * Attempt to confirm the user's email address and show success or failure
85 * as needed; if successful, take the user to log in
87 * @param $code Confirmation code
89 function attemptConfirm( $code ) {
90 global $wgUser, $wgOut;
91 $user = User::newFromConfirmationCode( $code );
92 if( is_object( $user ) ) {
93 $user->confirmEmail();
94 $user->saveSettings();
95 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
96 $wgOut->addWikiMsg( $message );
97 if( !$wgUser->isLoggedIn() ) {
98 $title = SpecialPage::getTitleFor( 'Userlogin' );
99 $wgOut->returnToMain( true, $title );
101 } else {
102 $wgOut->addWikiMsg( 'confirmemail_invalid' );
109 * Special page allows users to cancel an email confirmation using the e-mail
110 * confirmation code
112 * @ingroup SpecialPage
114 class EmailInvalidation extends UnlistedSpecialPage {
116 public function __construct() {
117 parent::__construct( 'Invalidateemail' );
120 function execute( $code ) {
121 $this->setHeaders();
122 $this->attemptInvalidate( $code );
126 * Attempt to invalidate the user's email address and show success or failure
127 * as needed; if successful, link to main page
129 * @param $code Confirmation code
131 function attemptInvalidate( $code ) {
132 global $wgUser, $wgOut;
133 $user = User::newFromConfirmationCode( $code );
134 if( is_object( $user ) ) {
135 $user->invalidateEmail();
136 $user->saveSettings();
137 $wgOut->addWikiMsg( 'confirmemail_invalidated' );
138 if( !$wgUser->isLoggedIn() ) {
139 $wgOut->returnToMain();
141 } else {
142 $wgOut->addWikiMsg( 'confirmemail_invalid' );