SpecialConfirmemail.php 4.3 KB

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