w/s diff. mostly eol w/s and using only tabs of width 4 to indent.
[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
34 /**
35 * Constructor
36 */
37 public function __construct() {
38 parent::__construct( 'Confirmemail' );
39 }
40
41 /**
42 * Main execution point
43 *
44 * @param $code Confirmation code passed to the page
45 */
46 function execute( $code ) {
47 global $wgUser, $wgOut;
48 $this->setHeaders();
49
50 if ( wfReadOnly() ) {
51 $wgOut->readOnlyPage();
52 return;
53 }
54
55 if( empty( $code ) ) {
56 if( $wgUser->isLoggedIn() ) {
57 if( User::isValidEmailAddr( $wgUser->getEmail() ) ) {
58 $this->showRequestForm();
59 } else {
60 $wgOut->addWikiMsg( 'confirmemail_noemail' );
61 }
62 } else {
63 $title = SpecialPage::getTitleFor( 'Userlogin' );
64 $skin = $wgUser->getSkin();
65 $llink = $skin->linkKnown(
66 $title,
67 wfMsgHtml( 'loginreqlink' ),
68 array(),
69 array( 'returnto' => $this->getTitle()->getPrefixedText() )
70 );
71 $wgOut->addWikiMsgArray( 'confirmemail_needlogin', array( $llink ), array( 'replaceafter' ) );
72 }
73 } else {
74 $this->attemptConfirm( $code );
75 }
76 }
77
78 /**
79 * Show a nice form for the user to request a confirmation mail
80 */
81 function showRequestForm() {
82 global $wgOut, $wgUser, $wgLang, $wgRequest;
83 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
84 $status = $wgUser->sendConfirmationMail();
85 if ( $status->isGood() ) {
86 $wgOut->addWikiMsg( 'confirmemail_sent' );
87 } else {
88 $wgOut->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
89 }
90 } else {
91 if( $wgUser->isEmailConfirmed() ) {
92 // date and time are separate parameters to facilitate localisation.
93 // $time is kept for backward compat reasons.
94 // 'emailauthenticated' is also used in SpecialPreferences.php
95 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
96 $d = $wgLang->date( $wgUser->mEmailAuthenticated, true );
97 $t = $wgLang->time( $wgUser->mEmailAuthenticated, true );
98 $wgOut->addWikiMsg( 'emailauthenticated', $time, $d, $t );
99 }
100 if( $wgUser->isEmailConfirmationPending() ) {
101 $wgOut->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>", 'confirmemail_pending' );
102 }
103 $wgOut->addWikiMsg( 'confirmemail_text' );
104 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl() ) );
105 $form .= Html::hidden( 'token', $wgUser->editToken() );
106 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
107 $form .= Xml::closeElement( 'form' );
108 $wgOut->addHTML( $form );
109 }
110 }
111
112 /**
113 * Attempt to confirm the user's email address and show success or failure
114 * as needed; if successful, take the user to log in
115 *
116 * @param $code Confirmation code
117 */
118 function attemptConfirm( $code ) {
119 global $wgUser, $wgOut;
120 $user = User::newFromConfirmationCode( $code );
121 if( is_object( $user ) ) {
122 $user->confirmEmail();
123 $user->saveSettings();
124 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
125 $wgOut->addWikiMsg( $message );
126 if( !$wgUser->isLoggedIn() ) {
127 $title = SpecialPage::getTitleFor( 'Userlogin' );
128 $wgOut->returnToMain( true, $title );
129 }
130 } else {
131 $wgOut->addWikiMsg( 'confirmemail_invalid' );
132 }
133 }
134
135 }
136
137 /**
138 * Special page allows users to cancel an email confirmation using the e-mail
139 * confirmation code
140 *
141 * @ingroup SpecialPage
142 */
143 class EmailInvalidation extends UnlistedSpecialPage {
144
145 public function __construct() {
146 parent::__construct( 'Invalidateemail' );
147 }
148
149 function execute( $code ) {
150 $this->setHeaders();
151
152 if ( wfReadOnly() ) {
153 global $wgOut;
154 $wgOut->readOnlyPage();
155 return;
156 }
157
158 $this->attemptInvalidate( $code );
159 }
160
161 /**
162 * Attempt to invalidate the user's email address and show success or failure
163 * as needed; if successful, link to main page
164 *
165 * @param $code Confirmation code
166 */
167 function attemptInvalidate( $code ) {
168 global $wgUser, $wgOut;
169 $user = User::newFromConfirmationCode( $code );
170 if( is_object( $user ) ) {
171 $user->invalidateEmail();
172 $user->saveSettings();
173 $wgOut->addWikiMsg( 'confirmemail_invalidated' );
174 if( !$wgUser->isLoggedIn() ) {
175 $wgOut->returnToMain();
176 }
177 } else {
178 $wgOut->addWikiMsg( 'confirmemail_invalid' );
179 }
180 }
181 }