3 * Implements Special:Confirmemail and Special:Invalidateemail
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.
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.
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
21 * @ingroup SpecialPage
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
28 * @ingroup SpecialPage
29 * @author Brion Vibber
30 * @author Rob Church <robchur@gmail.com>
32 class EmailConfirmation
extends UnlistedSpecialPage
{
33 public function __construct() {
34 parent
::__construct( 'Confirmemail', 'editmyprivateinfo' );
38 * Main execution point
40 * @param null|string $code Confirmation code passed to the page
42 function execute( $code ) {
45 $this->checkReadOnly();
46 $this->checkPermissions();
48 // This could also let someone check the current email address, so
49 // require both permissions.
50 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
51 throw new PermissionsError( 'viewmyprivateinfo' );
54 if ( $code === null ||
$code === '' ) {
55 if ( $this->getUser()->isLoggedIn() ) {
56 if ( Sanitizer
::validateEmail( $this->getUser()->getEmail() ) ) {
57 $this->showRequestForm();
59 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
62 $llink = Linker
::linkKnown(
63 SpecialPage
::getTitleFor( 'Userlogin' ),
64 $this->msg( 'loginreqlink' )->escaped(),
66 array( 'returnto' => $this->getTitle()->getPrefixedText() )
68 $this->getOutput()->addHTML(
69 $this->msg( 'confirmemail_needlogin' )->rawParams( $llink )->parse()
73 $this->attemptConfirm( $code );
78 * Show a nice form for the user to request a confirmation mail
80 function showRequestForm() {
81 $user = $this->getUser();
82 $out = $this->getOutput();
84 if ( $this->getRequest()->wasPosted() &&
85 $user->matchEditToken( $this->getRequest()->getText( 'token' ) )
87 $status = $user->sendConfirmationMail();
88 if ( $status->isGood() ) {
89 $out->addWikiMsg( 'confirmemail_sent' );
91 $out->addWikiText( $status->getWikiText( 'confirmemail_sendfailed' ) );
94 if ( $user->isEmailConfirmed() ) {
95 // date and time are separate parameters to facilitate localisation.
96 // $time is kept for backward compat reasons.
97 // 'emailauthenticated' is also used in SpecialPreferences.php
98 $lang = $this->getLanguage();
99 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
100 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
101 $d = $lang->userDate( $emailAuthenticated, $user );
102 $t = $lang->userTime( $emailAuthenticated, $user );
103 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
106 if ( $user->isEmailConfirmationPending() ) {
108 "<div class=\"error mw-confirmemail-pending\">\n$1\n</div>",
109 'confirmemail_pending'
113 $out->addWikiMsg( 'confirmemail_text' );
114 $form = Html
::openElement(
116 array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL() )
118 $form .= Html
::hidden( 'token', $user->getEditToken() ) . "\n";
119 $form .= Xml
::submitButton( $this->msg( 'confirmemail_send' )->text() ) . "\n";
120 $form .= Html
::closeElement( 'form' ) . "\n";
121 $out->addHTML( $form );
126 * Attempt to confirm the user's email address and show success or failure
127 * as needed; if successful, take the user to log in
129 * @param string $code Confirmation code
131 function attemptConfirm( $code ) {
132 $user = User
::newFromConfirmationCode( $code );
133 if ( !is_object( $user ) ) {
134 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
139 $user->confirmEmail();
140 $user->saveSettings();
141 $message = $this->getUser()->isLoggedIn() ?
'confirmemail_loggedin' : 'confirmemail_success';
142 $this->getOutput()->addWikiMsg( $message );
144 if ( !$this->getUser()->isLoggedIn() ) {
145 $title = SpecialPage
::getTitleFor( 'Userlogin' );
146 $this->getOutput()->returnToMain( true, $title );
152 * Special page allows users to cancel an email confirmation using the e-mail
155 * @ingroup SpecialPage
157 class EmailInvalidation
extends UnlistedSpecialPage
{
158 public function __construct() {
159 parent
::__construct( 'Invalidateemail', 'editmyprivateinfo' );
162 function execute( $code ) {
164 $this->checkReadOnly();
165 $this->checkPermissions();
166 $this->attemptInvalidate( $code );
170 * Attempt to invalidate the user's email address and show success or failure
171 * as needed; if successful, link to main page
173 * @param string $code Confirmation code
175 function attemptInvalidate( $code ) {
176 $user = User
::newFromConfirmationCode( $code );
177 if ( !is_object( $user ) ) {
178 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
183 $user->invalidateEmail();
184 $user->saveSettings();
185 $this->getOutput()->addWikiMsg( 'confirmemail_invalidated' );
187 if ( !$this->getUser()->isLoggedIn() ) {
188 $this->getOutput()->returnToMain();