Don't show header if including
[lhc/web/wiklou.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' ), 'returnto=' . $self->getPrefixedUrl() );
40 $wgOut->addHtml( wfMsgWikiHtml( 'confirmemail_needlogin', $llink ) );
41 }
42 } else {
43 $this->attemptConfirm( $code );
44 }
45 }
46
47 /**
48 * Show a nice form for the user to request a confirmation mail
49 */
50 function showRequestForm() {
51 global $wgOut, $wgUser, $wgLang, $wgRequest;
52 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getText( 'token' ) ) ) {
53 $ok = $wgUser->sendConfirmationMail();
54 if ( WikiError::isError( $ok ) ) {
55 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $ok->toString() );
56 } else {
57 $wgOut->addWikiMsg( 'confirmemail_sent' );
58 }
59 } else {
60 if( $wgUser->isEmailConfirmed() ) {
61 // date and time are separate parameters to facilitate localisation.
62 // $time is kept for backward compat reasons.
63 // 'emailauthenticated' is also used in SpecialPreferences.php
64 $time = $wgLang->timeAndDate( $wgUser->mEmailAuthenticated, true );
65 $d = $wgLang->date( $wgUser->mEmailAuthenticated, true );
66 $t = $wgLang->time( $wgUser->mEmailAuthenticated, true );
67 $wgOut->addWikiMsg( 'emailauthenticated', $time, $d, $t );
68 }
69 if( $wgUser->isEmailConfirmationPending() ) {
70 $wgOut->addWikiMsg( 'confirmemail_pending' );
71 }
72 $wgOut->addWikiMsg( 'confirmemail_text' );
73 $self = SpecialPage::getTitleFor( 'Confirmemail' );
74 $form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $self->getLocalUrl() ) );
75 $form .= wfHidden( 'token', $wgUser->editToken() );
76 $form .= wfSubmitButton( wfMsgHtml( 'confirmemail_send' ) );
77 $form .= wfCloseElement( 'form' );
78 $wgOut->addHtml( $form );
79 }
80 }
81
82 /**
83 * Attempt to confirm the user's email address and show success or failure
84 * as needed; if successful, take the user to log in
85 *
86 * @param $code Confirmation code
87 */
88 function attemptConfirm( $code ) {
89 global $wgUser, $wgOut;
90 $user = User::newFromConfirmationCode( $code );
91 if( is_object( $user ) ) {
92 $user->confirmEmail();
93 $user->saveSettings();
94 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
95 $wgOut->addWikiMsg( $message );
96 if( !$wgUser->isLoggedIn() ) {
97 $title = SpecialPage::getTitleFor( 'Userlogin' );
98 $wgOut->returnToMain( true, $title );
99 }
100 } else {
101 $wgOut->addWikiMsg( 'confirmemail_invalid' );
102 }
103 }
104
105 }
106
107 /**
108 * Special page allows users to cancel an email confirmation using the e-mail
109 * confirmation code
110 *
111 * @ingroup SpecialPage
112 */
113 class EmailInvalidation extends UnlistedSpecialPage {
114
115 public function __construct() {
116 parent::__construct( 'Invalidateemail' );
117 }
118
119 function execute( $code ) {
120 $this->setHeaders();
121 $this->attemptInvalidate( $code );
122 }
123
124 /**
125 * Attempt to invalidate the user's email address and show success or failure
126 * as needed; if successful, link to main page
127 *
128 * @param $code Confirmation code
129 */
130 function attemptInvalidate( $code ) {
131 global $wgUser, $wgOut;
132 $user = User::newFromConfirmationCode( $code );
133 if( is_object( $user ) ) {
134 $user->invalidateEmail();
135 $user->saveSettings();
136 $wgOut->addWikiMsg( 'confirmemail_invalidated' );
137 if( !$wgUser->isLoggedIn() ) {
138 $wgOut->returnToMain();
139 }
140 } else {
141 $wgOut->addWikiMsg( 'confirmemail_invalid' );
142 }
143 }
144 }