3 * Implements Special:Confirmemail
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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
27 * Special page allows users to request email confirmation message, and handles
28 * processing of the confirmation code when the link in the email is followed
30 * @ingroup SpecialPage
31 * @author Brion Vibber
32 * @author Rob Church <robchur@gmail.com>
34 class SpecialConfirmEmail
extends UnlistedSpecialPage
{
35 public function __construct() {
36 parent
::__construct( 'Confirmemail', 'editmyprivateinfo' );
39 public function doesWrites() {
44 * Main execution point
46 * @param null|string $code Confirmation code passed to the page
47 * @throws PermissionsError
48 * @throws ReadOnlyError
49 * @throws UserNotLoggedIn
51 function execute( $code ) {
52 // Ignore things like master queries/connections on GET requests.
53 // It's very convenient to just allow formless link usage.
54 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
57 $this->checkReadOnly();
58 $this->checkPermissions();
60 // This could also let someone check the current email address, so
61 // require both permissions.
62 if ( !MediaWikiServices
::getInstance()
63 ->getPermissionManager()
64 ->userHasRight( $this->getUser(), 'viewmyprivateinfo' )
66 throw new PermissionsError( 'viewmyprivateinfo' );
69 if ( $code === null ||
$code === '' ) {
70 $this->requireLogin( 'confirmemail_needlogin' );
71 if ( Sanitizer
::validateEmail( $this->getUser()->getEmail() ) ) {
72 $this->showRequestForm();
74 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
77 $old = $trxProfiler->setSilenced( true );
78 $this->attemptConfirm( $code );
79 $trxProfiler->setSilenced( $old );
84 * Show a nice form for the user to request a confirmation mail
86 function showRequestForm() {
87 $user = $this->getUser();
88 $out = $this->getOutput();
90 if ( !$user->isEmailConfirmed() ) {
92 if ( $user->isEmailConfirmationPending() ) {
97 'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
98 $this->msg( 'confirmemail_pending' )->escaped() .
104 $out->addWikiMsg( 'confirmemail_text' );
105 $form = HTMLForm
::factory( 'ooui', $descriptor, $this->getContext() );
107 ->setMethod( 'post' )
108 ->setAction( $this->getPageTitle()->getLocalURL() )
109 ->setSubmitTextMsg( 'confirmemail_send' )
110 ->setSubmitCallback( [ $this, 'submitSend' ] );
112 $retval = $form->show();
114 if ( $retval === true ) {
115 // should never happen, but if so, don't let the user without any message
116 $out->addWikiMsg( 'confirmemail_sent' );
117 } elseif ( $retval instanceof Status
&& $retval->isGood() ) {
118 $out->addWikiTextAsInterface( $retval->getValue() );
121 // date and time are separate parameters to facilitate localisation.
122 // $time is kept for backward compat reasons.
123 // 'emailauthenticated' is also used in SpecialPreferences.php
124 $lang = $this->getLanguage();
125 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
126 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
127 $d = $lang->userDate( $emailAuthenticated, $user );
128 $t = $lang->userTime( $emailAuthenticated, $user );
129 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
134 * Callback for HTMLForm send confirmation mail.
136 * @return Status Status object with the result
138 public function submitSend() {
139 $status = $this->getUser()->sendConfirmationMail();
140 if ( $status->isGood() ) {
141 return Status
::newGood( $this->msg( 'confirmemail_sent' )->text() );
143 return Status
::newFatal( new RawMessage(
144 $status->getWikiText( 'confirmemail_sendfailed' )
150 * Attempt to confirm the user's email address and show success or failure
151 * as needed; if successful, take the user to log in
153 * @param string $code Confirmation code
155 private function attemptConfirm( $code ) {
156 $user = User
::newFromConfirmationCode( $code, User
::READ_EXCLUSIVE
);
157 if ( !is_object( $user ) ) {
158 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
163 // rate limit email confirmations
164 if ( $user->pingLimiter( 'confirmemail' ) ) {
165 $this->getOutput()->addWikiMsg( 'actionthrottledtext' );
170 $user->confirmEmail();
171 $user->saveSettings();
172 $message = $this->getUser()->isLoggedIn() ?
'confirmemail_loggedin' : 'confirmemail_success';
173 $this->getOutput()->addWikiMsg( $message );
175 if ( !$this->getUser()->isLoggedIn() ) {
176 $title = SpecialPage
::getTitleFor( 'Userlogin' );
177 $this->getOutput()->returnToMain( true, $title );