wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialConfirmemail.php
blobea26ada08aa927abe724c63a2a17ccbeec1831b2
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 // Ignore things like master queries/connections on GET requests.
47 // It's very convenient to just allow formless link usage.
48 Profiler::instance()->getTransactionProfiler()->resetExpectations();
50 $this->setHeaders();
52 $this->checkReadOnly();
53 $this->checkPermissions();
55 $this->requireLogin( 'confirmemail_needlogin' );
57 // This could also let someone check the current email address, so
58 // require both permissions.
59 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
60 throw new PermissionsError( 'viewmyprivateinfo' );
63 if ( $code === null || $code === '' ) {
64 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
65 $this->showRequestForm();
66 } else {
67 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
69 } else {
70 $this->attemptConfirm( $code );
74 /**
75 * Show a nice form for the user to request a confirmation mail
77 function showRequestForm() {
78 $user = $this->getUser();
79 $out = $this->getOutput();
81 if ( !$user->isEmailConfirmed() ) {
82 $descriptor = array();
83 if ( $user->isEmailConfirmationPending() ) {
84 $descriptor += array(
85 'pending' => array(
86 'type' => 'info',
87 'raw' => true,
88 'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
89 $this->msg( 'confirmemail_pending' )->escaped() .
90 "\n</div>",
95 $out->addWikiMsg( 'confirmemail_text' );
96 $form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() );
97 $form
98 ->setMethod( 'post' )
99 ->setAction( $this->getPageTitle()->getLocalURL() )
100 ->setSubmitTextMsg( 'confirmemail_send' )
101 ->setSubmitCallback( array( $this, 'submitSend' ) );
103 $retval = $form->show();
105 if ( $retval === true ) {
106 // should never happen, but if so, don't let the user without any message
107 $out->addWikiMsg( 'confirmemail_sent' );
108 } elseif ( $retval instanceof Status && $retval->isGood() ) {
109 $out->addWikiText( $retval->getValue() );
111 } else {
112 // date and time are separate parameters to facilitate localisation.
113 // $time is kept for backward compat reasons.
114 // 'emailauthenticated' is also used in SpecialPreferences.php
115 $lang = $this->getLanguage();
116 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
117 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
118 $d = $lang->userDate( $emailAuthenticated, $user );
119 $t = $lang->userTime( $emailAuthenticated, $user );
120 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
125 * Callback for HTMLForm send confirmation mail.
127 * @return Status Status object with the result
129 public function submitSend() {
130 $status = $this->getUser()->sendConfirmationMail();
131 if ( $status->isGood() ) {
132 return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
133 } else {
134 return Status::newFatal( new RawMessage(
135 $status->getWikiText( 'confirmemail_sendfailed' )
136 ) );
141 * Attempt to confirm the user's email address and show success or failure
142 * as needed; if successful, take the user to log in
144 * @param string $code Confirmation code
146 function attemptConfirm( $code ) {
147 $user = User::newFromConfirmationCode( $code, User::READ_LATEST );
148 if ( !is_object( $user ) ) {
149 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
151 return;
154 $user->confirmEmail();
155 $user->saveSettings();
156 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
157 $this->getOutput()->addWikiMsg( $message );
159 if ( !$this->getUser()->isLoggedIn() ) {
160 $title = SpecialPage::getTitleFor( 'Userlogin' );
161 $this->getOutput()->returnToMain( true, $title );
167 * Special page allows users to cancel an email confirmation using the e-mail
168 * confirmation code
170 * @ingroup SpecialPage
172 class EmailInvalidation extends UnlistedSpecialPage {
173 public function __construct() {
174 parent::__construct( 'Invalidateemail', 'editmyprivateinfo' );
177 function execute( $code ) {
178 // Ignore things like master queries/connections on GET requests.
179 // It's very convenient to just allow formless link usage.
180 Profiler::instance()->getTransactionProfiler()->resetExpectations();
182 $this->setHeaders();
183 $this->checkReadOnly();
184 $this->checkPermissions();
185 $this->attemptInvalidate( $code );
189 * Attempt to invalidate the user's email address and show success or failure
190 * as needed; if successful, link to main page
192 * @param string $code Confirmation code
194 function attemptInvalidate( $code ) {
195 $user = User::newFromConfirmationCode( $code, User::READ_LATEST );
196 if ( !is_object( $user ) ) {
197 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
199 return;
202 $user->invalidateEmail();
203 $user->saveSettings();
204 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
206 if ( !$this->getUser()->isLoggedIn() ) {
207 $this->getOutput()->returnToMain();