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