Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blob47808d1112f5837850a0ae5263e30540a65e50c5
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' );
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();
47 if ( $code === null || $code === '' ) {
48 if ( $this->getUser()->isLoggedIn() ) {
49 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
50 $this->showRequestForm();
51 } else {
52 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
54 } else {
55 $llink = Linker::linkKnown(
56 SpecialPage::getTitleFor( 'Userlogin' ),
57 $this->msg( 'loginreqlink' )->escaped(),
58 array(),
59 array( 'returnto' => $this->getTitle()->getPrefixedText() )
61 $this->getOutput()->addHTML(
62 $this->msg( 'confirmemail_needlogin' )->rawParams( $llink )->parse()
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 } else {
87 if ( $user->isEmailConfirmed() ) {
88 // date and time are separate parameters to facilitate localisation.
89 // $time is kept for backward compat reasons.
90 // 'emailauthenticated' is also used in SpecialPreferences.php
91 $lang = $this->getLanguage();
92 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
93 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
94 $d = $lang->userDate( $emailAuthenticated, $user );
95 $t = $lang->userTime( $emailAuthenticated, $user );
96 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
99 if ( $user->isEmailConfirmationPending() ) {
100 $out->wrapWikiMsg(
101 "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
102 'confirmemail_pending'
106 $out->addWikiMsg( 'confirmemail_text' );
107 $form = Html::openElement(
108 'form',
109 array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL() )
110 ) . "\n";
111 $form .= Html::hidden( 'token', $user->getEditToken() ) . "\n";
112 $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
113 $form .= Html::closeElement( 'form' ) . "\n";
114 $out->addHTML( $form );
119 * Attempt to confirm the user's email address and show success or failure
120 * as needed; if successful, take the user to log in
122 * @param string $code Confirmation code
124 function attemptConfirm( $code ) {
125 $user = User::newFromConfirmationCode( $code );
126 if ( !is_object( $user ) ) {
127 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
129 return;
132 $user->confirmEmail();
133 $user->saveSettings();
134 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
135 $this->getOutput()->addWikiMsg( $message );
137 if ( !$this->getUser()->isLoggedIn() ) {
138 $title = SpecialPage::getTitleFor( 'Userlogin' );
139 $this->getOutput()->returnToMain( true, $title );
145 * Special page allows users to cancel an email confirmation using the e-mail
146 * confirmation code
148 * @ingroup SpecialPage
150 class EmailInvalidation extends UnlistedSpecialPage {
151 public function __construct() {
152 parent::__construct( 'Invalidateemail' );
155 function execute( $code ) {
156 $this->setHeaders();
157 $this->checkReadOnly();
158 $this->attemptInvalidate( $code );
162 * Attempt to invalidate the user's email address and show success or failure
163 * as needed; if successful, link to main page
165 * @param string $code Confirmation code
167 function attemptInvalidate( $code ) {
168 $user = User::newFromConfirmationCode( $code );
169 if ( !is_object( $user ) ) {
170 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
172 return;
175 $user->invalidateEmail();
176 $user->saveSettings();
177 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
179 if ( !$this->getUser()->isLoggedIn() ) {
180 $this->getOutput()->returnToMain();