Merge "Deprecate SpecialPage::getTitle"
[lhc/web/wiklou.git] / includes / specials / SpecialConfirmemail.php
1 <?php
2 /**
3 * Implements Special:Confirmemail and Special:Invalidateemail
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 /**
38 * Main execution point
39 *
40 * @param null|string $code Confirmation code passed to the page
41 */
42 function execute( $code ) {
43 $this->setHeaders();
44
45 $this->checkReadOnly();
46 $this->checkPermissions();
47
48 $this->requireLogin( 'confirmemail_needlogin' );
49
50 // This could also let someone check the current email address, so
51 // require both permissions.
52 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
53 throw new PermissionsError( 'viewmyprivateinfo' );
54 }
55
56 if ( $code === null || $code === '' ) {
57 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
58 $this->showRequestForm();
59 } else {
60 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
61 }
62 } else {
63 $this->attemptConfirm( $code );
64 }
65 }
66
67 /**
68 * Show a nice form for the user to request a confirmation mail
69 */
70 function showRequestForm() {
71 $user = $this->getUser();
72 $out = $this->getOutput();
73
74 if ( $this->getRequest()->wasPosted() &&
75 $user->matchEditToken( $this->getRequest()->getText( 'token' ) )
76 ) {
77 $status = $user->sendConfirmationMail();
78 if ( $status->isGood() ) {
79 $out->addWikiMsg( 'confirmemail_sent' );
80 } else {
81 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
82 }
83 } elseif ( $user->isEmailConfirmed() ) {
84 // date and time are separate parameters to facilitate localisation.
85 // $time is kept for backward compat reasons.
86 // 'emailauthenticated' is also used in SpecialPreferences.php
87 $lang = $this->getLanguage();
88 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
89 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
90 $d = $lang->userDate( $emailAuthenticated, $user );
91 $t = $lang->userTime( $emailAuthenticated, $user );
92 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
93 } else {
94 if ( $user->isEmailConfirmationPending() ) {
95 $out->wrapWikiMsg(
96 "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
97 'confirmemail_pending'
98 );
99 }
100
101 $out->addWikiMsg( 'confirmemail_text' );
102 $form = Html::openElement(
103 'form',
104 array( 'method' => 'post', 'action' => $this->getPageTitle()->getLocalURL() )
105 ) . "\n";
106 $form .= Html::hidden( 'token', $user->getEditToken() ) . "\n";
107 $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
108 $form .= Html::closeElement( 'form' ) . "\n";
109 $out->addHTML( $form );
110 }
111 }
112
113 /**
114 * Attempt to confirm the user's email address and show success or failure
115 * as needed; if successful, take the user to log in
116 *
117 * @param string $code Confirmation code
118 */
119 function attemptConfirm( $code ) {
120 $user = User::newFromConfirmationCode( $code );
121 if ( !is_object( $user ) ) {
122 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
123
124 return;
125 }
126
127 $user->confirmEmail();
128 $user->saveSettings();
129 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
130 $this->getOutput()->addWikiMsg( $message );
131
132 if ( !$this->getUser()->isLoggedIn() ) {
133 $title = SpecialPage::getTitleFor( 'Userlogin' );
134 $this->getOutput()->returnToMain( true, $title );
135 }
136 }
137 }
138
139 /**
140 * Special page allows users to cancel an email confirmation using the e-mail
141 * confirmation code
142 *
143 * @ingroup SpecialPage
144 */
145 class EmailInvalidation extends UnlistedSpecialPage {
146 public function __construct() {
147 parent::__construct( 'Invalidateemail', 'editmyprivateinfo' );
148 }
149
150 function execute( $code ) {
151 $this->setHeaders();
152 $this->checkReadOnly();
153 $this->checkPermissions();
154 $this->attemptInvalidate( $code );
155 }
156
157 /**
158 * Attempt to invalidate the user's email address and show success or failure
159 * as needed; if successful, link to main page
160 *
161 * @param string $code Confirmation code
162 */
163 function attemptInvalidate( $code ) {
164 $user = User::newFromConfirmationCode( $code );
165 if ( !is_object( $user ) ) {
166 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
167
168 return;
169 }
170
171 $user->invalidateEmail();
172 $user->saveSettings();
173 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
174
175 if ( !$this->getUser()->isLoggedIn() ) {
176 $this->getOutput()->returnToMain();
177 }
178 }
179 }