Use local context instead of global variables
[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 if ( wfReadOnly() ) {
50 throw new ReadOnlyError;
51 }
52
53 if( empty( $code ) ) {
54 if( $this->getUser()->isLoggedIn() ) {
55 if( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
56 $this->showRequestForm();
57 } else {
58 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
59 }
60 } else {
61 $llink = Linker::linkKnown(
62 SpecialPage::getTitleFor( 'Userlogin' ),
63 wfMsgHtml( 'loginreqlink' ),
64 array(),
65 array( 'returnto' => $this->getTitle()->getPrefixedText() )
66 );
67 $this->getOutput()->addHTML( wfMessage( 'confirmemail_needlogin' )->rawParams( $llink )->parse() );
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 $user = $this->getUser();
79 $out = $this->getOutput();
80 if( $this->getRequest()->wasPosted() && $user->matchEditToken( $this->getRequest()->getText( 'token' ) ) ) {
81 $status = $user->sendConfirmationMail();
82 if ( $status->isGood() ) {
83 $out->addWikiMsg( 'confirmemail_sent' );
84 } else {
85 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
86 }
87 } else {
88 if( $user->isEmailConfirmed() ) {
89 // date and time are separate parameters to facilitate localisation.
90 // $time is kept for backward compat reasons.
91 // 'emailauthenticated' is also used in SpecialPreferences.php
92 $time = $this->getLang()->timeAndDate( $user->mEmailAuthenticated, true );
93 $d = $this->getLang()->date( $user->mEmailAuthenticated, true );
94 $t = $this->getLang()->time( $user->mEmailAuthenticated, true );
95 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
96 }
97 if( $user->isEmailConfirmationPending() ) {
98 $out->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>", 'confirmemail_pending' );
99 }
100 $out->addWikiMsg( 'confirmemail_text' );
101 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl() ) );
102 $form .= Html::hidden( 'token', $user->editToken() );
103 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
104 $form .= Xml::closeElement( 'form' );
105 $out->addHTML( $form );
106 }
107 }
108
109 /**
110 * Attempt to confirm the user's email address and show success or failure
111 * as needed; if successful, take the user to log in
112 *
113 * @param $code Confirmation code
114 */
115 function attemptConfirm( $code ) {
116 $user = User::newFromConfirmationCode( $code );
117 if( is_object( $user ) ) {
118 $user->confirmEmail();
119 $user->saveSettings();
120 $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
121 $this->getOutput()->addWikiMsg( $message );
122 if( !$this->getUser()->isLoggedIn() ) {
123 $title = SpecialPage::getTitleFor( 'Userlogin' );
124 $this->getOutput()->returnToMain( true, $title );
125 }
126 } else {
127 $this->getOutput()->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 throw new ReadOnlyError;
150 }
151
152 $this->attemptInvalidate( $code );
153 }
154
155 /**
156 * Attempt to invalidate the user's email address and show success or failure
157 * as needed; if successful, link to main page
158 *
159 * @param $code Confirmation code
160 */
161 function attemptInvalidate( $code ) {
162 $user = User::newFromConfirmationCode( $code );
163 if( is_object( $user ) ) {
164 $user->invalidateEmail();
165 $user->saveSettings();
166 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
167 if( !$this->getUser()->isLoggedIn() ) {
168 $this->getOutput()->returnToMain();
169 }
170 } else {
171 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
172 }
173 }
174 }