Escaping fixes
[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->makeKnownLinkObj( $title, wfMsgHtml( 'loginreqlink' ),
39 'returnto=' . $this->getTitle()->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->wrapWikiMsg( "<div class=\"error mw-confirmemail-pending\">$1</div>", 'confirmemail_pending' );
71 }
72 $wgOut->addWikiMsg( 'confirmemail_text' );
73 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl() ) );
74 $form .= Xml::hidden( 'token', $wgUser->editToken() );
75 $form .= Xml::submitButton( wfMsg( 'confirmemail_send' ) );
76 $form .= Xml::closeElement( 'form' );
77 $wgOut->addHTML( $form );
78 }
79 }
80
81 /**
82 * Attempt to confirm the user's email address and show success or failure
83 * as needed; if successful, take the user to log in
84 *
85 * @param $code Confirmation code
86 */
87 function attemptConfirm( $code ) {
88 global $wgUser, $wgOut;
89 $user = User::newFromConfirmationCode( $code );
90 if( is_object( $user ) ) {
91 $user->confirmEmail();
92 $user->saveSettings();
93 $message = $wgUser->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
94 $wgOut->addWikiMsg( $message );
95 if( !$wgUser->isLoggedIn() ) {
96 $title = SpecialPage::getTitleFor( 'Userlogin' );
97 $wgOut->returnToMain( true, $title );
98 }
99 } else {
100 $wgOut->addWikiMsg( 'confirmemail_invalid' );
101 }
102 }
103
104 }
105
106 /**
107 * Special page allows users to cancel an email confirmation using the e-mail
108 * confirmation code
109 *
110 * @ingroup SpecialPage
111 */
112 class EmailInvalidation extends UnlistedSpecialPage {
113
114 public function __construct() {
115 parent::__construct( 'Invalidateemail' );
116 }
117
118 function execute( $code ) {
119 $this->setHeaders();
120 $this->attemptInvalidate( $code );
121 }
122
123 /**
124 * Attempt to invalidate the user's email address and show success or failure
125 * as needed; if successful, link to main page
126 *
127 * @param $code Confirmation code
128 */
129 function attemptInvalidate( $code ) {
130 global $wgUser, $wgOut;
131 $user = User::newFromConfirmationCode( $code );
132 if( is_object( $user ) ) {
133 $user->invalidateEmail();
134 $user->saveSettings();
135 $wgOut->addWikiMsg( 'confirmemail_invalidated' );
136 if( !$wgUser->isLoggedIn() ) {
137 $wgOut->returnToMain();
138 }
139 } else {
140 $wgOut->addWikiMsg( 'confirmemail_invalid' );
141 }
142 }
143 }