* Merged WikiPage::updateRestrictions() and Title::updateTitleProtection() into WikiP...
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blob4a10b38dba1ffc83520a3d8e9109144c6fe7a4a7
1 <?php
2 /**
3 * Implements Special:Confirmemail and Special:Invalidateemail
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page allows users to request email confirmation message, and handles
26 * processing of the confirmation code when the link in the email is followed
28 * @ingroup SpecialPage
29 * @author Brion Vibber
30 * @author Rob Church <robchur@gmail.com>
32 class EmailConfirmation extends UnlistedSpecialPage {
34 /**
35 * Constructor
37 public function __construct() {
38 parent::__construct( 'Confirmemail' );
41 /**
42 * Main execution point
44 * @param $code Confirmation code passed to the page
46 function execute( $code ) {
47 $this->setHeaders();
49 $this->checkReadOnly();
51 if( empty( $code ) ) {
52 if( $this->getUser()->isLoggedIn() ) {
53 if( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
54 $this->showRequestForm();
55 } else {
56 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
58 } else {
59 $llink = Linker::linkKnown(
60 SpecialPage::getTitleFor( 'Userlogin' ),
61 wfMsgHtml( 'loginreqlink' ),
62 array(),
63 array( 'returnto' => $this->getTitle()->getPrefixedText() )
65 $this->getOutput()->addHTML( wfMessage( 'confirmemail_needlogin' )->rawParams( $llink )->parse() );
67 } else {
68 $this->attemptConfirm( $code );
72 /**
73 * Show a nice form for the user to request a confirmation mail
75 function showRequestForm() {
76 $user = $this->getUser();
77 $out = $this->getOutput();
78 if( $this->getRequest()->wasPosted() && $user->matchEditToken( $this->getRequest()->getText( 'token' ) ) ) {
79 $status = $user->sendConfirmationMail();
80 if ( $status->isGood() ) {
81 $out->addWikiMsg( 'confirmemail_sent' );
82 } else {
83 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
85 } else {
86 if( $user->isEmailConfirmed() ) {
87 // date and time are separate parameters to facilitate localisation.
88 // $time is kept for backward compat reasons.
89 // 'emailauthenticated' is also used in SpecialPreferences.php
90 $time = $this->getLanguage()->timeAndDate( $user->mEmailAuthenticated, true );
91 $d = $this->getLanguage()->date( $user->mEmailAuthenticated, true );
92 $t = $this->getLanguage()->time( $user->mEmailAuthenticated, true );
93 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
95 if( $user->isEmailConfirmationPending() ) {
96 $out->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>", 'confirmemail_pending' );
98 $out->addWikiMsg( 'confirmemail_text' );
99 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl() ) );
100 $form .= Html::hidden( 'token', $user->getEditToken() );
101 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
102 $form .= Xml::closeElement( 'form' );
103 $out->addHTML( $form );
108 * Attempt to confirm the user's email address and show success or failure
109 * as needed; if successful, take the user to log in
111 * @param $code Confirmation code
113 function attemptConfirm( $code ) {
114 $user = User::newFromConfirmationCode( $code );
115 if( is_object( $user ) ) {
116 $user->confirmEmail();
117 $user->saveSettings();
118 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
119 $this->getOutput()->addWikiMsg( $message );
120 if( !$this->getUser()->isLoggedIn() ) {
121 $title = SpecialPage::getTitleFor( 'Userlogin' );
122 $this->getOutput()->returnToMain( true, $title );
124 } else {
125 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
132 * Special page allows users to cancel an email confirmation using the e-mail
133 * confirmation code
135 * @ingroup SpecialPage
137 class EmailInvalidation extends UnlistedSpecialPage {
139 public function __construct() {
140 parent::__construct( 'Invalidateemail' );
143 function execute( $code ) {
144 $this->setHeaders();
146 if ( wfReadOnly() ) {
147 throw new ReadOnlyError;
150 $this->attemptInvalidate( $code );
154 * Attempt to invalidate the user's email address and show success or failure
155 * as needed; if successful, link to main page
157 * @param $code Confirmation code
159 function attemptInvalidate( $code ) {
160 $user = User::newFromConfirmationCode( $code );
161 if( is_object( $user ) ) {
162 $user->invalidateEmail();
163 $user->saveSettings();
164 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
165 if( !$this->getUser()->isLoggedIn() ) {
166 $this->getOutput()->returnToMain();
168 } else {
169 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );