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