]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialConfirmemail.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.git] / includes / specials / 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  * @ingroup SpecialPage
8  * @author Brion Vibber
9  * @author Rob Church <robchur@gmail.com>
10  */
11 class EmailConfirmation extends UnlistedSpecialPage {
12
13         /**
14          * Constructor
15          */
16         public function __construct() {
17                 parent::__construct( 'Confirmemail' );
18         }
19
20         /**
21          * Main execution point
22          *
23          * @param $code Confirmation code passed to the page
24          */
25         function execute( $code ) {
26                 global $wgUser, $wgOut;
27                 $this->setHeaders();
28                 if( empty( $code ) ) {
29                         if( $wgUser->isLoggedIn() ) {
30                                 if( User::isValidEmailAddr( $wgUser->getEmail() ) ) {
31                                         $this->showRequestForm();
32                                 } else {
33                                         $wgOut->addWikiMsg( 'confirmemail_noemail' );
34                                 }
35                         } else {
36                                 $title = SpecialPage::getTitleFor( 'Userlogin' );
37                                 $self = SpecialPage::getTitleFor( 'Confirmemail' );
38                                 $skin = $wgUser->getSkin();
39                                 $llink = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'loginreqlink' ), 
40                                         'returnto=' . $self->getPrefixedUrl() );
41                                 $wgOut->addHTML( wfMsgWikiHtml( 'confirmemail_needlogin', $llink ) );
42                         }
43                 } else {
44                         $this->attemptConfirm( $code );
45                 }
46         }
47
48         /**
49          * Show a nice form for the user to request a confirmation mail
50          */
51         function showRequestForm() {
52                 global $wgOut, $wgUser, $wgLang, $wgRequest;
53                 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
54                         $ok = $wgUser->sendConfirmationMail();
55                         if ( WikiError::isError( $ok ) ) {
56                                 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $ok->toString() );
57                         } else {
58                                 $wgOut->addWikiMsg( 'confirmemail_sent' );
59                         }
60                 } else {
61                         if( $wgUser->isEmailConfirmed() ) {
62                                 // date and time are separate parameters to facilitate localisation.
63                                 // $time is kept for backward compat reasons.
64                                 // 'emailauthenticated' is also used in SpecialPreferences.php
65                                 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
66                                 $d = $wgLang->date( $wgUser->mEmailAuthenticated, true );
67                                 $t = $wgLang->time( $wgUser->mEmailAuthenticated, true );
68                                 $wgOut->addWikiMsg( 'emailauthenticated', $time, $d, $t );
69                         }
70                         if( $wgUser->isEmailConfirmationPending() ) {
71                                 $wgOut->addWikiMsg( 'confirmemail_pending' );
72                         }
73                         $wgOut->addWikiMsg( 'confirmemail_text' );
74                         $self = SpecialPage::getTitleFor( 'Confirmemail' );
75                         $form  = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
76                         $form .= Xml::hidden( 'token', $wgUser->editToken() );
77                         $form .= Xml::submitButton( wfMsgHtml( 'confirmemail_send' ) );
78                         $form .= Xml::closeElement( 'form' );
79                         $wgOut->addHTML( $form );
80                 }
81         }
82
83         /**
84          * Attempt to confirm the user's email address and show success or failure
85          * as needed; if successful, take the user to log in
86          *
87          * @param $code Confirmation code
88          */
89         function attemptConfirm( $code ) {
90                 global $wgUser, $wgOut;
91                 $user = User::newFromConfirmationCode( $code );
92                 if( is_object( $user ) ) {
93                         $user->confirmEmail();
94                         $user->saveSettings();
95                         $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
96                         $wgOut->addWikiMsg( $message );
97                         if( !$wgUser->isLoggedIn() ) {
98                                 $title = SpecialPage::getTitleFor( 'Userlogin' );
99                                 $wgOut->returnToMain( true, $title );
100                         }
101                 } else {
102                         $wgOut->addWikiMsg( 'confirmemail_invalid' );
103                 }
104         }
105
106 }
107
108 /**
109  * Special page allows users to cancel an email confirmation using the e-mail
110  * confirmation code
111  *
112  * @ingroup SpecialPage
113  */
114 class EmailInvalidation extends UnlistedSpecialPage {
115
116         public function __construct() {
117                 parent::__construct( 'Invalidateemail' );
118         }
119
120         function execute( $code ) {
121                 $this->setHeaders();
122                 $this->attemptInvalidate( $code );
123         }
124
125         /**
126          * Attempt to invalidate the user's email address and show success or failure
127          * as needed; if successful, link to main page
128          *
129          * @param $code Confirmation code
130          */
131         function attemptInvalidate( $code ) {
132                 global $wgUser, $wgOut;
133                 $user = User::newFromConfirmationCode( $code );
134                 if( is_object( $user ) ) {
135                         $user->invalidateEmail();
136                         $user->saveSettings();
137                         $wgOut->addWikiMsg( 'confirmemail_invalidated' );
138                         if( !$wgUser->isLoggedIn() ) {
139                                 $wgOut->returnToMain();
140                         }
141                 } else {
142                         $wgOut->addWikiMsg( 'confirmemail_invalid' );
143                 }
144         }
145 }