Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 207 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
SpecialEmailUser | |
0.00% |
0 / 206 |
|
0.00% |
0 / 15 |
2162 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFormFields | |
0.00% |
0 / 47 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
42 | |||
getTarget | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
validateTarget | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
getPermissionsError | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
userForm | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
30 | |||
sendEmailForm | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
onFormSubmit | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
submit | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
prefixSearchSubpages | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
isListed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Implements Special:Emailuser |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup SpecialPage |
22 | */ |
23 | |
24 | namespace MediaWiki\Specials; |
25 | |
26 | use ErrorPageError; |
27 | use MediaWiki\Config\Config; |
28 | use MediaWiki\Context\IContextSource; |
29 | use MediaWiki\HTMLForm\HTMLForm; |
30 | use MediaWiki\Mail\EmailUserFactory; |
31 | use MediaWiki\MainConfigNames; |
32 | use MediaWiki\MediaWikiServices; |
33 | use MediaWiki\Message\Message; |
34 | use MediaWiki\Permissions\PermissionStatus; |
35 | use MediaWiki\SpecialPage\SpecialPage; |
36 | use MediaWiki\Status\Status; |
37 | use MediaWiki\User\Options\UserOptionsLookup; |
38 | use MediaWiki\User\User; |
39 | use MediaWiki\User\UserFactory; |
40 | use MediaWiki\User\UserNamePrefixSearch; |
41 | use MediaWiki\User\UserNameUtils; |
42 | use StatusValue; |
43 | |
44 | /** |
45 | * A special page that allows users to send e-mails to other users |
46 | * |
47 | * @ingroup SpecialPage |
48 | */ |
49 | class SpecialEmailUser extends SpecialPage { |
50 | |
51 | private UserNameUtils $userNameUtils; |
52 | private UserNamePrefixSearch $userNamePrefixSearch; |
53 | private UserOptionsLookup $userOptionsLookup; |
54 | private EmailUserFactory $emailUserFactory; |
55 | private UserFactory $userFactory; |
56 | |
57 | /** |
58 | * @param UserNameUtils $userNameUtils |
59 | * @param UserNamePrefixSearch $userNamePrefixSearch |
60 | * @param UserOptionsLookup $userOptionsLookup |
61 | * @param EmailUserFactory $emailUserFactory |
62 | * @param UserFactory $userFactory |
63 | */ |
64 | public function __construct( |
65 | UserNameUtils $userNameUtils, |
66 | UserNamePrefixSearch $userNamePrefixSearch, |
67 | UserOptionsLookup $userOptionsLookup, |
68 | EmailUserFactory $emailUserFactory, |
69 | UserFactory $userFactory |
70 | ) { |
71 | parent::__construct( 'Emailuser' ); |
72 | $this->userNameUtils = $userNameUtils; |
73 | $this->userNamePrefixSearch = $userNamePrefixSearch; |
74 | $this->userOptionsLookup = $userOptionsLookup; |
75 | $this->emailUserFactory = $emailUserFactory; |
76 | $this->userFactory = $userFactory; |
77 | } |
78 | |
79 | public function doesWrites() { |
80 | return true; |
81 | } |
82 | |
83 | public function getDescription() { |
84 | return $this->msg( 'emailuser-title-notarget' ); |
85 | } |
86 | |
87 | protected function getFormFields( User $target ) { |
88 | $linkRenderer = $this->getLinkRenderer(); |
89 | $user = $this->getUser(); |
90 | return [ |
91 | 'From' => [ |
92 | 'type' => 'info', |
93 | 'raw' => 1, |
94 | 'default' => $linkRenderer->makeLink( |
95 | $user->getUserPage(), |
96 | $user->getName() |
97 | ), |
98 | 'label-message' => 'emailfrom', |
99 | 'id' => 'mw-emailuser-sender', |
100 | ], |
101 | 'To' => [ |
102 | 'type' => 'info', |
103 | 'raw' => 1, |
104 | 'default' => $linkRenderer->makeLink( |
105 | $target->getUserPage(), |
106 | $target->getName() |
107 | ), |
108 | 'label-message' => 'emailto', |
109 | 'id' => 'mw-emailuser-recipient', |
110 | ], |
111 | 'Target' => [ |
112 | 'type' => 'hidden', |
113 | 'default' => $target->getName(), |
114 | ], |
115 | 'Subject' => [ |
116 | 'type' => 'text', |
117 | 'default' => $this->msg( 'defemailsubject', $user->getName() )->inContentLanguage()->text(), |
118 | 'label-message' => 'emailsubject', |
119 | 'maxlength' => 200, |
120 | 'size' => 60, |
121 | 'required' => true, |
122 | ], |
123 | 'Text' => [ |
124 | 'type' => 'textarea', |
125 | 'rows' => 20, |
126 | 'label-message' => 'emailmessage', |
127 | 'required' => true, |
128 | ], |
129 | 'CCMe' => [ |
130 | 'type' => 'check', |
131 | 'label-message' => 'emailccme', |
132 | 'default' => $this->userOptionsLookup->getBoolOption( $user, 'ccmeonemails' ), |
133 | ], |
134 | ]; |
135 | } |
136 | |
137 | public function execute( $par ) { |
138 | $this->setHeaders(); |
139 | $this->outputHeader(); |
140 | |
141 | $out = $this->getOutput(); |
142 | $request = $this->getRequest(); |
143 | $out->addModuleStyles( 'mediawiki.special' ); |
144 | |
145 | // Error out if sending user cannot do this. Don't authorize yet. |
146 | $emailUser = $this->emailUserFactory->newEmailUserBC( |
147 | $this->getUser(), |
148 | $this->getConfig() |
149 | ); |
150 | $emailUser->setEditToken( (string)$request->getVal( 'wpEditToken' ) ); |
151 | $status = $emailUser->canSend(); |
152 | |
153 | if ( !$status->isGood() ) { |
154 | if ( $status instanceof PermissionStatus ) { |
155 | $status->throwErrorPageError(); |
156 | } elseif ( $status->hasMessage( 'mailnologin' ) ) { |
157 | throw new ErrorPageError( 'mailnologin', 'mailnologintext' ); |
158 | } elseif ( $status->hasMessage( 'usermaildisabled' ) ) { |
159 | throw new ErrorPageError( 'usermaildisabled', 'usermaildisabledtext' ); |
160 | } elseif ( $status->getValue() !== null ) { |
161 | // BC for deprecated hook errors |
162 | // (to be removed when UserCanSendEmail and EmailUserPermissionsErrors are removed) |
163 | $error = $status->getErrors()[0]; |
164 | throw new ErrorPageError( $status->getValue(), $error['message'], $error['params'] ); |
165 | } else { |
166 | // Fallback in case new error types are added in EmailUser |
167 | throw new ErrorPageError( $this->getDescription(), Status::wrap( $status )->getMessage() ); |
168 | } |
169 | } |
170 | |
171 | // Always go through the userform, it will do validations on the target |
172 | // and display the emailform for us. |
173 | $target = $par ?? $request->getVal( 'wpTarget', $request->getVal( 'target', '' ) ); |
174 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Defaults to empty string |
175 | $this->userForm( $target ); |
176 | } |
177 | |
178 | /** |
179 | * Validate target User |
180 | * |
181 | * @param string $target Target user name |
182 | * @param User $sender User sending the email |
183 | * @return User|string User object on success or a string on error |
184 | * @deprecated since 1.42 Use UserFactory::newFromName() and EmailUser::validateTarget() |
185 | */ |
186 | public static function getTarget( $target, User $sender ) { |
187 | $targetObject = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $target ); |
188 | if ( !$targetObject instanceof User ) { |
189 | return 'notarget'; |
190 | } |
191 | |
192 | $status = MediaWikiServices::getInstance()->getEmailUserFactory() |
193 | ->newEmailUser( $sender ) |
194 | ->validateTarget( $targetObject ); |
195 | if ( !$status->isGood() ) { |
196 | $msg = $status->getErrors()[0]['message']; |
197 | $ret = $msg === 'emailnotarget' ? 'notarget' : preg_replace( '/text$/', '', $msg ); |
198 | } else { |
199 | $ret = $targetObject; |
200 | } |
201 | return $ret; |
202 | } |
203 | |
204 | /** |
205 | * Validate target User |
206 | * |
207 | * @param User $target Target user |
208 | * @param User $sender User sending the email |
209 | * @return string Error message or empty string if valid. |
210 | * @since 1.30 |
211 | * @deprecated since 1.41 Use EmailUser::validateTarget() |
212 | */ |
213 | public static function validateTarget( $target, User $sender ) { |
214 | if ( !$target instanceof User ) { |
215 | return 'notarget'; |
216 | } |
217 | $status = MediaWikiServices::getInstance()->getEmailUserFactory() |
218 | ->newEmailUser( $sender ) |
219 | ->validateTarget( $target ); |
220 | if ( $status->isGood() ) { |
221 | $ret = ''; |
222 | } else { |
223 | $msg = $status->getErrors()[0]['message']; |
224 | $ret = $msg === 'emailnotarget' ? 'notarget' : preg_replace( '/text$/', '', $msg ); |
225 | } |
226 | return $ret; |
227 | } |
228 | |
229 | /** |
230 | * Check whether a user is allowed to send email |
231 | * |
232 | * @param User $user |
233 | * @param string $editToken |
234 | * @param Config|null $config optional for backwards compatibility |
235 | * @param bool $authorize whether to authorize the immediate sending of mails, |
236 | * rather than just checking beforehand. |
237 | * |
238 | * @return null|string|array Null on success, string on error, or array on |
239 | * hook error |
240 | * @deprecated since 1.41 Use EmailUser::canSend() or EmailUser::authorizeSend() |
241 | */ |
242 | public static function getPermissionsError( $user, $editToken, Config $config = null, $authorize = false ) { |
243 | $emailUser = MediaWikiServices::getInstance()->getEmailUserFactory()->newEmailUserBC( $user, $config ); |
244 | $emailUser->setEditToken( (string)$editToken ); |
245 | $status = $authorize ? $emailUser->authorizeSend() : $emailUser->canSend(); |
246 | |
247 | if ( $status->isGood() ) { |
248 | return null; |
249 | } |
250 | foreach ( $status->getErrors() as $err ) { |
251 | $errKey = $err['message'] instanceof Message ? $err['message']->getKey() : $err['message']; |
252 | if ( strpos( $errKey, 'blockedtext' ) !== false ) { |
253 | // BC for block messages |
254 | return "blockedemailuser"; |
255 | } |
256 | } |
257 | $error = $status->getErrors()[0]; |
258 | if ( $status->getValue() !== null ) { |
259 | // BC for hook errors intended to be used with ErrorPageError |
260 | return [ $status->getValue(), $error['message'], $error['params'] ]; |
261 | } |
262 | return $error['message']; |
263 | } |
264 | |
265 | /** |
266 | * Form to ask for target user name. |
267 | * |
268 | * @param string $name User name submitted. |
269 | */ |
270 | protected function userForm( $name ) { |
271 | $htmlForm = HTMLForm::factory( 'ooui', [ |
272 | 'Target' => [ |
273 | 'type' => 'user', |
274 | 'exists' => true, |
275 | 'required' => true, |
276 | 'label-message' => 'emailusername', |
277 | 'id' => 'emailusertarget', |
278 | 'autofocus' => true, |
279 | // Skip validation when visit directly without subpage (T347854) |
280 | 'default' => '', |
281 | // Prefill for subpage syntax and old target param. |
282 | 'filter-callback' => static function ( $value ) use ( $name ) { |
283 | return str_replace( '_', ' ', |
284 | ( $value !== '' && $value !== false && $value !== null ) ? $value : $name ); |
285 | }, |
286 | 'validation-callback' => function ( $value ) { |
287 | // HTMLForm checked that this is a valid user name |
288 | $target = $this->userFactory->newFromName( $value ); |
289 | $statusValue = $this->emailUserFactory |
290 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
291 | ->newEmailUser( $this->getUser() )->validateTarget( $target ); |
292 | if ( !$statusValue->isGood() ) { |
293 | // TODO: Return Status instead of StatusValue from validateTarget() method? |
294 | return Status::wrap( $statusValue )->getMessage(); |
295 | } |
296 | return true; |
297 | } |
298 | ] |
299 | ], $this->getContext() ); |
300 | |
301 | $htmlForm |
302 | ->setMethod( 'GET' ) |
303 | ->setTitle( $this->getPageTitle() ) // Remove subpage |
304 | ->setSubmitCallback( [ $this, 'sendEmailForm' ] ) |
305 | ->setId( 'askusername' ) |
306 | ->setWrapperLegendMsg( 'emailtarget' ) |
307 | ->setSubmitTextMsg( 'emailusernamesubmit' ) |
308 | ->show(); |
309 | } |
310 | |
311 | /** |
312 | * @param array $data |
313 | * @return bool |
314 | */ |
315 | public function sendEmailForm( array $data ) { |
316 | $out = $this->getOutput(); |
317 | |
318 | // HTMLForm checked that this is a valid user name, the return value can never be null. |
319 | $target = $this->userFactory->newFromName( $data['Target'] ); |
320 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
321 | $htmlForm = HTMLForm::factory( 'ooui', $this->getFormFields( $target ), $this->getContext() ); |
322 | $htmlForm |
323 | ->setTitle( $this->getPageTitle() ) // Remove subpage |
324 | ->addPreHtml( $this->msg( 'emailpagetext', $target->getName() )->parse() ) |
325 | ->setSubmitTextMsg( 'emailsend' ) |
326 | ->setSubmitCallback( [ $this, 'onFormSubmit' ] ) |
327 | ->setWrapperLegendMsg( 'email-legend' ) |
328 | ->prepareForm(); |
329 | |
330 | if ( !$this->getHookRunner()->onEmailUserForm( $htmlForm ) ) { |
331 | return false; |
332 | } |
333 | |
334 | $result = $htmlForm->show(); |
335 | |
336 | if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) { |
337 | $out->setPageTitleMsg( $this->msg( 'emailsent' ) ); |
338 | $out->addWikiMsg( 'emailsenttext', $target->getName() ); |
339 | $out->returnToMain( false, $target->getUserPage() ); |
340 | } else { |
341 | $out->setPageTitleMsg( $this->msg( 'emailuser-title-target', $target->getName() ) ); |
342 | } |
343 | return true; |
344 | } |
345 | |
346 | /** |
347 | * @param array $data |
348 | * @return StatusValue|false |
349 | * @internal Only public because it's used as an HTMLForm callback. |
350 | */ |
351 | public function onFormSubmit( array $data ) { |
352 | // HTMLForm checked that this is a valid user name, the return value can never be null. |
353 | $target = $this->userFactory->newFromName( $data['Target'] ); |
354 | |
355 | $emailUser = $this->emailUserFactory->newEmailUser( $this->getAuthority() ); |
356 | $emailUser->setEditToken( $this->getRequest()->getVal( 'wpEditToken' ) ); |
357 | |
358 | // Fully authorize on sending emails. |
359 | $status = $emailUser->authorizeSend(); |
360 | |
361 | if ( !$status->isOK() ) { |
362 | return $status; |
363 | } |
364 | |
365 | // @phan-suppress-next-next-line PhanTypeMismatchArgumentNullable |
366 | $res = $emailUser->sendEmailUnsafe( |
367 | $target, |
368 | $data['Subject'], |
369 | $data['Text'], |
370 | $data['CCMe'], |
371 | $this->getLanguage()->getCode() |
372 | ); |
373 | if ( $res->hasMessage( 'hookaborted' ) ) { |
374 | // BC: The method could previously return false if the EmailUser hook set the error to false. Preserve |
375 | // that behaviour until we replace the hook. |
376 | $res = false; |
377 | } else { |
378 | $res = Status::wrap( $res ); |
379 | } |
380 | return $res; |
381 | } |
382 | |
383 | /** |
384 | * Really send a mail. Permissions should have been checked using |
385 | * getPermissionsError(). It is probably also a good |
386 | * idea to check the edit token and ping limiter in advance. |
387 | * |
388 | * @param array $data |
389 | * @param IContextSource $context |
390 | * @return Status|false |
391 | * @deprecated since 1.41 Use EmailUser::sendEmailUnsafe() |
392 | */ |
393 | public static function submit( array $data, IContextSource $context ) { |
394 | $target = MediaWikiServices::getInstance()->getUserFactory()->newFromName( (string)$data['Target'] ); |
395 | if ( !$target instanceof User ) { |
396 | return Status::newFatal( 'emailnotarget' ); |
397 | } |
398 | |
399 | $emailUser = MediaWikiServices::getInstance()->getEmailUserFactory() |
400 | ->newEmailUserBC( $context->getAuthority(), $context->getConfig() ); |
401 | |
402 | $ret = $emailUser->sendEmailUnsafe( |
403 | $target, |
404 | (string)$data['Subject'], |
405 | (string)$data['Text'], |
406 | (bool)$data['CCMe'], |
407 | $context->getLanguage()->getCode() |
408 | ); |
409 | if ( $ret->hasMessage( 'hookaborted' ) ) { |
410 | // BC: The method could previously return false if the EmailUser hook set the error to false. |
411 | $ret = false; |
412 | } elseif ( $ret->hasMessage( 'noemailtarget' ) ) { |
413 | // BC: The previous implementation would use notargettext even if noemailtarget would be the right |
414 | // message to use here. |
415 | return Status::newFatal( 'notargettext' ); |
416 | } else { |
417 | $ret = Status::wrap( $ret ); |
418 | } |
419 | return $ret; |
420 | } |
421 | |
422 | /** |
423 | * Return an array of subpages beginning with $search that this special page will accept. |
424 | * |
425 | * @param string $search Prefix to search for |
426 | * @param int $limit Maximum number of results to return (usually 10) |
427 | * @param int $offset Number of results to skip (usually 0) |
428 | * @return string[] Matching subpages |
429 | */ |
430 | public function prefixSearchSubpages( $search, $limit, $offset ) { |
431 | $search = $this->userNameUtils->getCanonical( $search ); |
432 | if ( !$search ) { |
433 | // No prefix suggestion for invalid user |
434 | return []; |
435 | } |
436 | // Autocomplete subpage as user list - public to allow caching |
437 | return $this->userNamePrefixSearch |
438 | ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset ); |
439 | } |
440 | |
441 | /** |
442 | * @return bool |
443 | */ |
444 | public function isListed() { |
445 | return $this->getConfig()->get( MainConfigNames::EnableUserEmail ); |
446 | } |
447 | |
448 | protected function getGroupName() { |
449 | return 'users'; |
450 | } |
451 | } |
452 | |
453 | /** @deprecated class alias since 1.41 */ |
454 | class_alias( SpecialEmailUser::class, 'SpecialEmailUser' ); |