Fixing some of the "@return true" or "@return false", need to be "@return bool" and...
[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( $code === null || $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 $this->msg( 'loginreqlink' )->escaped(),
62 array(),
63 array( 'returnto' => $this->getTitle()->getPrefixedText() )
64 );
65 $this->getOutput()->addHTML( $this->msg( '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 $lang = $this->getLanguage();
91 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
92 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
93 $d = $lang->userDate( $emailAuthenticated, $user );
94 $t = $lang->userTime( $emailAuthenticated, $user );
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->getEditToken() );
103 $form .= Xml::submitButton( $this->msg( 'confirmemail_send' )->text() );
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 string 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 string 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 }