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