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