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