088ba7805a94e013a6e76b4cbf0580f59cb246a9
[lhc/web/wiklou.git] / includes / specials / SpecialConfirmemail.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Special page allows users to request email confirmation message, and handles
22 * processing of the confirmation code when the link in the email is followed
23 *
24 * @ingroup SpecialPage
25 * @author Brion Vibber
26 * @author Rob Church <robchur@gmail.com>
27 */
28 class EmailConfirmation extends UnlistedSpecialPage {
29
30 /**
31 * Constructor
32 */
33 public function __construct() {
34 parent::__construct( 'Confirmemail' );
35 }
36
37 /**
38 * Main execution point
39 *
40 * @param $code Confirmation code passed to the page
41 */
42 function execute( $code ) {
43 global $wgUser, $wgOut;
44 $this->setHeaders();
45
46 if ( wfReadOnly() ) {
47 $wgOut->readOnlyPage();
48 return;
49 }
50
51 if( empty( $code ) ) {
52 if( $wgUser->isLoggedIn() ) {
53 if( User::isValidEmailAddr( $wgUser->getEmail() ) ) {
54 $this->showRequestForm();
55 } else {
56 $wgOut->addWikiMsg( 'confirmemail_noemail' );
57 }
58 } else {
59 $title = SpecialPage::getTitleFor( 'Userlogin' );
60 $skin = $wgUser->getSkin();
61 $llink = $skin->linkKnown(
62 $title,
63 wfMsgHtml( 'loginreqlink' ),
64 array(),
65 array( 'returnto' => $this->getTitle()->getPrefixedText() )
66 );
67 $wgOut->addHTML( wfMsgWikiHtml( 'confirmemail_needlogin', $llink ) );
68 }
69 } else {
70 $this->attemptConfirm( $code );
71 }
72 }
73
74 /**
75 * Show a nice form for the user to request a confirmation mail
76 */
77 function showRequestForm() {
78 global $wgOut, $wgUser, $wgLang, $wgRequest;
79 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
80 $ok = $wgUser->sendConfirmationMail();
81 if ( WikiError::isError( $ok ) ) {
82 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $ok->toString() );
83 } else {
84 $wgOut->addWikiMsg( 'confirmemail_sent' );
85 }
86 } else {
87 if( $wgUser->isEmailConfirmed() ) {
88 // date and time are separate parameters to facilitate localisation.
89 // $time is kept for backward compat reasons.
90 // 'emailauthenticated' is also used in SpecialPreferences.php
91 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
92 $d = $wgLang->date( $wgUser->mEmailAuthenticated, true );
93 $t = $wgLang->time( $wgUser->mEmailAuthenticated, true );
94 $wgOut->addWikiMsg( 'emailauthenticated', $time, $d, $t );
95 }
96 if( $wgUser->isEmailConfirmationPending() ) {
97 $wgOut->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>", 'confirmemail_pending' );
98 }
99 $wgOut->addWikiMsg( 'confirmemail_text' );
100 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl() ) );
101 $form .= Xml::hidden( 'token', $wgUser->editToken() );
102 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
103 $form .= Xml::closeElement( 'form' );
104 $wgOut->addHTML( $form );
105 }
106 }
107
108 /**
109 * Attempt to confirm the user's email address and show success or failure
110 * as needed; if successful, take the user to log in
111 *
112 * @param $code Confirmation code
113 */
114 function attemptConfirm( $code ) {
115 global $wgUser, $wgOut;
116 $user = User::newFromConfirmationCode( $code );
117 if( is_object( $user ) ) {
118 $user->confirmEmail();
119 $user->saveSettings();
120 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
121 $wgOut->addWikiMsg( $message );
122 if( !$wgUser->isLoggedIn() ) {
123 $title = SpecialPage::getTitleFor( 'Userlogin' );
124 $wgOut->returnToMain( true, $title );
125 }
126 } else {
127 $wgOut->addWikiMsg( 'confirmemail_invalid' );
128 }
129 }
130
131 }
132
133 /**
134 * Special page allows users to cancel an email confirmation using the e-mail
135 * confirmation code
136 *
137 * @ingroup SpecialPage
138 */
139 class EmailInvalidation extends UnlistedSpecialPage {
140
141 public function __construct() {
142 parent::__construct( 'Invalidateemail' );
143 }
144
145 function execute( $code ) {
146 $this->setHeaders();
147
148 if ( wfReadOnly() ) {
149 global $wgOut;
150 $wgOut->readOnlyPage();
151 return;
152 }
153
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 $code Confirmation code
162 */
163 function attemptInvalidate( $code ) {
164 global $wgUser, $wgOut;
165 $user = User::newFromConfirmationCode( $code );
166 if( is_object( $user ) ) {
167 $user->invalidateEmail();
168 $user->saveSettings();
169 $wgOut->addWikiMsg( 'confirmemail_invalidated' );
170 if( !$wgUser->isLoggedIn() ) {
171 $wgOut->returnToMain();
172 }
173 } else {
174 $wgOut->addWikiMsg( 'confirmemail_invalid' );
175 }
176 }
177 }