e64232aa9aaf16dfd251d93a93524166e46860f7
[lhc/web/wiklou.git] / includes / SpecialConfirmemail.php
1 <?php
2
3 /**
4 * Special page allows users to request email confirmation message, and handles
5 * processing of the confirmation code when the link in the email is followed
6 *
7 * @package MediaWiki
8 * @subpackage Special pages
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12 /**
13 * Main execution point
14 *
15 * @param $par Parameters passed to the page
16 */
17 function wfSpecialConfirmemail( $par ) {
18 $form = new EmailConfirmation();
19 $form->execute( $par );
20 }
21
22 class EmailConfirmation extends SpecialPage {
23
24 /**
25 * Main execution point
26 *
27 * @param $code Confirmation code passed to the page
28 */
29 function execute( $code ) {
30 global $wgUser, $wgOut;
31 if( empty( $code ) ) {
32 if( $wgUser->isLoggedIn() ) {
33 if( User::isValidEmailAddr( $wgUser->getEmail() ) ) {
34 $this->showRequestForm();
35 } else {
36 $wgOut->addWikiText( wfMsg( 'confirmemail_noemail' ) );
37 }
38 } else {
39 $title = SpecialPage::getTitleFor( 'Userlogin' );
40 $self = SpecialPage::getTitleFor( 'Confirmemail' );
41 $skin = $wgUser->getSkin();
42 $llink = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'loginreqlink' ), 'returnto=' . $self->getPrefixedUrl() );
43 $wgOut->addHtml( wfMsgWikiHtml( 'confirmemail_needlogin', $llink ) );
44 }
45 } else {
46 $this->attemptConfirm( $code );
47 }
48 }
49
50 /**
51 * Show a nice form for the user to request a confirmation mail
52 */
53 function showRequestForm() {
54 global $wgOut, $wgUser, $wgLang, $wgRequest;
55 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
56 $ok = $wgUser->sendConfirmationMail();
57 if ( WikiError::isError( $ok ) ) {
58 $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $ok->toString() ) );
59 } else {
60 $wgOut->addWikiText( wfMsg( 'confirmemail_sent' ) );
61 }
62 } else {
63 if( $wgUser->isEmailConfirmed() ) {
64 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
65 $wgOut->addWikiText( wfMsg( 'emailauthenticated', $time ) );
66 }
67 if( $wgUser->isEmailConfirmationPending() ) {
68 $wgOut->addWikiText( wfMsg( 'confirmemail_pending' ) );
69 }
70 $wgOut->addWikiText( wfMsg( 'confirmemail_text' ) );
71 $self = SpecialPage::getTitleFor( 'Confirmemail' );
72 $form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
73 $form .= wfHidden( 'token', $wgUser->editToken() );
74 $form .= wfSubmitButton( wfMsgHtml( 'confirmemail_send' ) );
75 $form .= wfCloseElement( 'form' );
76 $wgOut->addHtml( $form );
77 }
78 }
79
80 /**
81 * Attempt to confirm the user's email address and show success or failure
82 * as needed; if successful, take the user to log in
83 *
84 * @param $code Confirmation code
85 */
86 function attemptConfirm( $code ) {
87 global $wgUser, $wgOut;
88 $user = User::newFromConfirmationCode( $code );
89 if( is_object( $user ) ) {
90 if( $user->confirmEmail() ) {
91 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
92 $wgOut->addWikiText( wfMsg( $message ) );
93 if( !$wgUser->isLoggedIn() ) {
94 $title = SpecialPage::getTitleFor( 'Userlogin' );
95 $wgOut->returnToMain( true, $title->getPrefixedText() );
96 }
97 } else {
98 $wgOut->addWikiText( wfMsg( 'confirmemail_error' ) );
99 }
100 } else {
101 $wgOut->addWikiText( wfMsg( 'confirmemail_invalid' ) );
102 }
103 }
104
105 }
106
107 ?>