MediaWiki  master
SpecialConfirmEmail.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\ScopedCallback;
26 
36 
38  private $userFactory;
39 
43  public function __construct( UserFactory $userFactory ) {
44  parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
45 
46  $this->userFactory = $userFactory;
47  }
48 
49  public function doesWrites() {
50  return true;
51  }
52 
61  public function execute( $code ) {
62  // Ignore things like primary queries/connections on GET requests.
63  // It's very convenient to just allow formless link usage.
64  $trxProfiler = Profiler::instance()->getTransactionProfiler();
65 
66  $this->setHeaders();
67  $this->checkReadOnly();
68  $this->checkPermissions();
69 
70  // This could also let someone check the current email address, so
71  // require both permissions.
72  if ( !$this->getAuthority()->isAllowed( 'viewmyprivateinfo' ) ) {
73  throw new PermissionsError( 'viewmyprivateinfo' );
74  }
75 
76  if ( $code === null || $code === '' ) {
77  $this->requireLogin( 'confirmemail_needlogin' );
78  if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
79  $this->showRequestForm();
80  } else {
81  $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
82  }
83  } else {
84  $scope = $trxProfiler->silenceForScope();
85  $this->attemptConfirm( $code );
86  ScopedCallback::consume( $scope );
87  }
88  }
89 
93  private function showRequestForm() {
94  $user = $this->getUser();
95  $out = $this->getOutput();
96 
97  if ( !$user->isEmailConfirmed() ) {
98  $descriptor = [];
99  if ( $user->isEmailConfirmationPending() ) {
100  $descriptor += [
101  'pending' => [
102  'type' => 'info',
103  'raw' => true,
104  'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
105  $this->msg( 'confirmemail_pending' )->escaped() .
106  "\n</div>",
107  ],
108  ];
109  }
110 
111  $out->addWikiMsg( 'confirmemail_text' );
112  $form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() );
113  $form
114  ->setAction( $this->getPageTitle()->getLocalURL() )
115  ->setSubmitTextMsg( 'confirmemail_send' )
116  ->setSubmitCallback( [ $this, 'submitSend' ] );
117 
118  $retval = $form->show();
119 
120  if ( $retval === true ) {
121  // should never happen, but if so, don't let the user without any message
122  $out->addWikiMsg( 'confirmemail_sent' );
123  } elseif ( $retval instanceof Status && $retval->isGood() ) {
124  $out->addWikiTextAsInterface( $retval->getValue() );
125  }
126  } else {
127  // date and time are separate parameters to facilitate localisation.
128  // $time is kept for backward compat reasons.
129  // 'emailauthenticated' is also used in SpecialPreferences.php
130  $lang = $this->getLanguage();
131  $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
132  $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
133  $d = $lang->userDate( $emailAuthenticated, $user );
134  $t = $lang->userTime( $emailAuthenticated, $user );
135  $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
136  }
137  }
138 
144  public function submitSend() {
145  $status = $this->getUser()->sendConfirmationMail();
146  if ( $status->isGood() ) {
147  return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
148  } else {
149  return Status::newFatal( new RawMessage(
150  $status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() )
151  ) );
152  }
153  }
154 
161  private function attemptConfirm( $code ) {
162  $user = $this->userFactory->newFromConfirmationCode(
163  $code,
164  UserFactory::READ_LATEST
165  );
166 
167  if ( !is_object( $user ) ) {
168  $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
169 
170  return;
171  }
172 
173  // rate limit email confirmations
174  if ( $user->pingLimiter( 'confirmemail' ) ) {
175  $this->getOutput()->addWikiMsg( 'actionthrottledtext' );
176 
177  return;
178  }
179 
180  $userLatest = $user->getInstanceForUpdate();
181  $userLatest->confirmEmail();
182  $userLatest->saveSettings();
183  $message = $this->getUser()->isRegistered() ? 'confirmemail_loggedin' : 'confirmemail_success';
184  $this->getOutput()->addWikiMsg( $message );
185 
186  if ( !$this->getUser()->isRegistered() ) {
187  $title = SpecialPage::getTitleFor( 'Userlogin' );
188  $this->getOutput()->returnToMain( true, $title );
189  }
190  }
191 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:31
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:358
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1744
SpecialConfirmEmail\showRequestForm
showRequestForm()
Show a nice form for the user to request a confirmation mail.
Definition: SpecialConfirmEmail.php:93
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:107
SpecialPage\getAuthority
getAuthority()
Shortcut to get the Authority executing this instance.
Definition: SpecialPage.php:810
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:32
SpecialConfirmEmail\attemptConfirm
attemptConfirm( $code)
Attempt to confirm the user's email address and show success or failure as needed; if successful,...
Definition: SpecialConfirmEmail.php:161
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
SpecialConfirmEmail
Special page allows users to request email confirmation message, and handles processing of the confir...
Definition: SpecialConfirmEmail.php:35
SpecialConfirmEmail\submitSend
submitSend()
Callback for HTMLForm send confirmation mail.
Definition: SpecialConfirmEmail.php:144
SpecialConfirmEmail\$userFactory
UserFactory $userFactory
Definition: SpecialConfirmEmail.php:38
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:122
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:800
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:388
SpecialConfirmEmail\__construct
__construct(UserFactory $userFactory)
Definition: SpecialConfirmEmail.php:43
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
SpecialConfirmEmail\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialConfirmEmail.php:49
SpecialConfirmEmail\execute
execute( $code)
Main execution point.
Definition: SpecialConfirmEmail.php:61
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:371
$t
$t
Definition: testCompression.php:74
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326