Use Agora oojs-ui theme on mobile
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blobd771589d82428cf779267be0b3d7aa2d51a1cfcb
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 {
33 public function __construct() {
34 parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
37 /**
38 * Main execution point
40 * @param null|string $code Confirmation code passed to the page
42 function execute( $code ) {
43 $this->setHeaders();
45 $this->checkReadOnly();
46 $this->checkPermissions();
48 $this->requireLogin( 'confirmemail_needlogin' );
50 // This could also let someone check the current email address, so
51 // require both permissions.
52 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
53 throw new PermissionsError( 'viewmyprivateinfo' );
56 if ( $code === null || $code === '' ) {
57 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
58 $this->showRequestForm();
59 } else {
60 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
62 } else {
63 $this->attemptConfirm( $code );
67 /**
68 * Show a nice form for the user to request a confirmation mail
70 function showRequestForm() {
71 $user = $this->getUser();
72 $out = $this->getOutput();
74 if ( $this->getRequest()->wasPosted() &&
75 $user->matchEditToken( $this->getRequest()->getText( 'token' ) )
76 ) {
77 $status = $user->sendConfirmationMail();
78 if ( $status->isGood() ) {
79 $out->addWikiMsg( 'confirmemail_sent' );
80 } else {
81 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
83 } elseif ( $user->isEmailConfirmed() ) {
84 // date and time are separate parameters to facilitate localisation.
85 // $time is kept for backward compat reasons.
86 // 'emailauthenticated' is also used in SpecialPreferences.php
87 $lang = $this->getLanguage();
88 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
89 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
90 $d = $lang->userDate( $emailAuthenticated, $user );
91 $t = $lang->userTime( $emailAuthenticated, $user );
92 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
93 } else {
94 if ( $user->isEmailConfirmationPending() ) {
95 $out->wrapWikiMsg(
96 "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
97 'confirmemail_pending'
101 $out->addWikiMsg( 'confirmemail_text' );
102 $form = Html::openElement(
103 'form',
104 array( 'method' => 'post', 'action' => $this->getPageTitle()->getLocalURL() )
105 ) . "\n";
106 $form .= Html::hidden( 'token', $user->getEditToken() ) . "\n";
107 $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
108 $form .= Html::closeElement( 'form' ) . "\n";
109 $out->addHTML( $form );
114 * Attempt to confirm the user's email address and show success or failure
115 * as needed; if successful, take the user to log in
117 * @param string $code Confirmation code
119 function attemptConfirm( $code ) {
120 $user = User::newFromConfirmationCode( $code );
121 if ( !is_object( $user ) ) {
122 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
124 return;
127 $user->confirmEmail();
128 $user->saveSettings();
129 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
130 $this->getOutput()->addWikiMsg( $message );
132 if ( !$this->getUser()->isLoggedIn() ) {
133 $title = SpecialPage::getTitleFor( 'Userlogin' );
134 $this->getOutput()->returnToMain( true, $title );
140 * Special page allows users to cancel an email confirmation using the e-mail
141 * confirmation code
143 * @ingroup SpecialPage
145 class EmailInvalidation extends UnlistedSpecialPage {
146 public function __construct() {
147 parent::__construct( 'Invalidateemail', 'editmyprivateinfo' );
150 function execute( $code ) {
151 $this->setHeaders();
152 $this->checkReadOnly();
153 $this->checkPermissions();
154 $this->attemptInvalidate( $code );
158 * Attempt to invalidate the user's email address and show success or failure
159 * as needed; if successful, link to main page
161 * @param string $code Confirmation code
163 function attemptInvalidate( $code ) {
164 $user = User::newFromConfirmationCode( $code );
165 if ( !is_object( $user ) ) {
166 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
168 return;
171 $user->invalidateEmail();
172 $user->saveSettings();
173 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
175 if ( !$this->getUser()->isLoggedIn() ) {
176 $this->getOutput()->returnToMain();