Introduce mediawiki.RegExp module
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blobb6ab112b34b767e3e40b0f75264817cb6bba2880
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
41 * @throws PermissionsError
42 * @throws ReadOnlyError
43 * @throws UserNotLoggedIn
45 function execute( $code ) {
46 $this->setHeaders();
48 $this->checkReadOnly();
49 $this->checkPermissions();
51 $this->requireLogin( 'confirmemail_needlogin' );
53 // This could also let someone check the current email address, so
54 // require both permissions.
55 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
56 throw new PermissionsError( 'viewmyprivateinfo' );
59 if ( $code === null || $code === '' ) {
60 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
61 $this->showRequestForm();
62 } else {
63 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
65 } else {
66 $this->attemptConfirm( $code );
70 /**
71 * Show a nice form for the user to request a confirmation mail
73 function showRequestForm() {
74 $user = $this->getUser();
75 $out = $this->getOutput();
77 if ( $this->getRequest()->wasPosted() &&
78 $user->matchEditToken( $this->getRequest()->getText( 'token' ) )
79 ) {
80 $status = $user->sendConfirmationMail();
81 if ( $status->isGood() ) {
82 $out->addWikiMsg( 'confirmemail_sent' );
83 } else {
84 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
86 } elseif ( $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 $lang = $this->getLanguage();
91 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
92 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
93 $d = $lang->userDate( $emailAuthenticated, $user );
94 $t = $lang->userTime( $emailAuthenticated, $user );
95 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
96 } else {
97 if ( $user->isEmailConfirmationPending() ) {
98 $out->wrapWikiMsg(
99 "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
100 'confirmemail_pending'
104 $out->addWikiMsg( 'confirmemail_text' );
105 $form = Html::openElement(
106 'form',
107 array( 'method' => 'post', 'action' => $this->getPageTitle()->getLocalURL() )
108 ) . "\n";
109 $form .= Html::hidden( 'token', $user->getEditToken() ) . "\n";
110 $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
111 $form .= Html::closeElement( 'form' ) . "\n";
112 $out->addHTML( $form );
117 * Attempt to confirm the user's email address and show success or failure
118 * as needed; if successful, take the user to log in
120 * @param string $code Confirmation code
122 function attemptConfirm( $code ) {
123 $user = User::newFromConfirmationCode( $code );
124 if ( !is_object( $user ) ) {
125 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
127 return;
130 $user->confirmEmail();
131 $user->saveSettings();
132 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
133 $this->getOutput()->addWikiMsg( $message );
135 if ( !$this->getUser()->isLoggedIn() ) {
136 $title = SpecialPage::getTitleFor( 'Userlogin' );
137 $this->getOutput()->returnToMain( true, $title );
143 * Special page allows users to cancel an email confirmation using the e-mail
144 * confirmation code
146 * @ingroup SpecialPage
148 class EmailInvalidation extends UnlistedSpecialPage {
149 public function __construct() {
150 parent::__construct( 'Invalidateemail', 'editmyprivateinfo' );
153 function execute( $code ) {
154 $this->setHeaders();
155 $this->checkReadOnly();
156 $this->checkPermissions();
157 $this->attemptInvalidate( $code );
161 * Attempt to invalidate the user's email address and show success or failure
162 * as needed; if successful, link to main page
164 * @param string $code Confirmation code
166 function attemptInvalidate( $code ) {
167 $user = User::newFromConfirmationCode( $code );
168 if ( !is_object( $user ) ) {
169 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
171 return;
174 $user->invalidateEmail();
175 $user->saveSettings();
176 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
178 if ( !$this->getUser()->isLoggedIn() ) {
179 $this->getOutput()->returnToMain();