a656c2ea112ef0dc09dca9453f724555988513f8
[lhc/web/wiklou.git] / includes / specials / SpecialConfirmemail.php
1 <?php
2 /**
3 * Implements Special:Confirmemail
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
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
27 *
28 * @ingroup SpecialPage
29 * @author Brion Vibber
30 * @author Rob Church <robchur@gmail.com>
31 */
32 class EmailConfirmation extends UnlistedSpecialPage {
33 public function __construct() {
34 parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
35 }
36
37 public function doesWrites() {
38 return true;
39 }
40
41 /**
42 * Main execution point
43 *
44 * @param null|string $code Confirmation code passed to the page
45 * @throws PermissionsError
46 * @throws ReadOnlyError
47 * @throws UserNotLoggedIn
48 */
49 function execute( $code ) {
50 // Ignore things like master queries/connections on GET requests.
51 // It's very convenient to just allow formless link usage.
52 Profiler::instance()->getTransactionProfiler()->resetExpectations();
53
54 $this->setHeaders();
55
56 $this->checkReadOnly();
57 $this->checkPermissions();
58
59 // This could also let someone check the current email address, so
60 // require both permissions.
61 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
62 throw new PermissionsError( 'viewmyprivateinfo' );
63 }
64
65 if ( $code === null || $code === '' ) {
66 $this->requireLogin( 'confirmemail_needlogin' );
67 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
68 $this->showRequestForm();
69 } else {
70 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
71 }
72 } else {
73 $this->attemptConfirm( $code );
74 }
75 }
76
77 /**
78 * Show a nice form for the user to request a confirmation mail
79 */
80 function showRequestForm() {
81 $user = $this->getUser();
82 $out = $this->getOutput();
83
84 if ( !$user->isEmailConfirmed() ) {
85 $descriptor = [];
86 if ( $user->isEmailConfirmationPending() ) {
87 $descriptor += [
88 'pending' => [
89 'type' => 'info',
90 'raw' => true,
91 'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
92 $this->msg( 'confirmemail_pending' )->escaped() .
93 "\n</div>",
94 ],
95 ];
96 }
97
98 $out->addWikiMsg( 'confirmemail_text' );
99 $form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() );
100 $form
101 ->setMethod( 'post' )
102 ->setAction( $this->getPageTitle()->getLocalURL() )
103 ->setSubmitTextMsg( 'confirmemail_send' )
104 ->setSubmitCallback( [ $this, 'submitSend' ] );
105
106 $retval = $form->show();
107
108 if ( $retval === true ) {
109 // should never happen, but if so, don't let the user without any message
110 $out->addWikiMsg( 'confirmemail_sent' );
111 } elseif ( $retval instanceof Status && $retval->isGood() ) {
112 $out->addWikiText( $retval->getValue() );
113 }
114 } else {
115 // date and time are separate parameters to facilitate localisation.
116 // $time is kept for backward compat reasons.
117 // 'emailauthenticated' is also used in SpecialPreferences.php
118 $lang = $this->getLanguage();
119 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
120 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
121 $d = $lang->userDate( $emailAuthenticated, $user );
122 $t = $lang->userTime( $emailAuthenticated, $user );
123 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
124 }
125 }
126
127 /**
128 * Callback for HTMLForm send confirmation mail.
129 *
130 * @return Status Status object with the result
131 */
132 public function submitSend() {
133 $status = $this->getUser()->sendConfirmationMail();
134 if ( $status->isGood() ) {
135 return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
136 } else {
137 return Status::newFatal( new RawMessage(
138 $status->getWikiText( 'confirmemail_sendfailed' )
139 ) );
140 }
141 }
142
143 /**
144 * Attempt to confirm the user's email address and show success or failure
145 * as needed; if successful, take the user to log in
146 *
147 * @param string $code Confirmation code
148 */
149 function attemptConfirm( $code ) {
150 $user = User::newFromConfirmationCode( $code, User::READ_LATEST );
151 if ( !is_object( $user ) ) {
152 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
153
154 return;
155 }
156
157 $user->confirmEmail();
158 $user->saveSettings();
159 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
160 $this->getOutput()->addWikiMsg( $message );
161
162 if ( !$this->getUser()->isLoggedIn() ) {
163 $title = SpecialPage::getTitleFor( 'Userlogin' );
164 $this->getOutput()->returnToMain( true, $title );
165 }
166 }
167 }