Debian Codegraph @

includes/specials/SpecialEmailUser.php

raw symbols SCIP
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 // Skip validation when visit directly without subpage (T347854)
216 'default' => '',
217 // Prefill for subpage syntax and old target param.
218 'filter-callback' => static function ( $value ) use ( $name ) {
219 return str_replace( '_', ' ',
220 ( $value !== '' && $value !== false && $value !== null ) ? $value : $name );
221 },
222 'validation-callback' => function ( $value ) {
223 // HTMLForm checked that this is a valid user name
224 $target = $this->userFactory->newFromName( $value );
225 $statusValue = $this->emailUserFactory
226 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
227 ->newEmailUser( $this->getUser() )->validateTarget( $target );
228 if ( !$statusValue->isGood() ) {
229 // TODO: Return Status instead of StatusValue from validateTarget() method?
230 return Status::wrap( $statusValue )->getMessage();
231 }
232 return true;
233 }
234 ]
235 ], $this->getContext() );
236
237 $htmlForm
238 ->setMethod( 'GET' )
239 ->setTitle( $this->getPageTitle() ) // Remove subpage
240 ->setSubmitCallback( [ $this, 'sendEmailForm' ] )
241 ->setId( 'askusername' )
242 ->setWrapperLegendMsg( 'emailtarget' )
243 ->setSubmitTextMsg( 'emailusernamesubmit' )
244 ->show();
245 }
246
247 /**
248 * @param array $data
249 * @return bool
250 */
251 public function sendEmailForm( array $data ) {
252 $out = $this->getOutput();
253
254 // HTMLForm checked that this is a valid user name, the return value can never be null.
255 $target = $this->userFactory->newFromName( $data['Target'] );
256 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable
257 $htmlForm = HTMLForm::factory( 'ooui', $this->getFormFields( $target ), $this->getContext() );
258 $htmlForm
259 ->setTitle( $this->getPageTitle() ) // Remove subpage
260 ->addPreHtml( $this->msg( 'emailpagetext', $target->getName() )->parse() )
261 ->setSubmitTextMsg( 'emailsend' )
262 ->setSubmitCallback( [ $this, 'onFormSubmit' ] )
263 ->setWrapperLegendMsg( 'email-legend' )
264 ->prepareForm();
265
266 if ( !$this->getHookRunner()->onEmailUserForm( $htmlForm ) ) {
267 return false;
268 }
269
270 $result = $htmlForm->show();
271
272 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
273 $out->setPageTitleMsg( $this->msg( 'emailsent' ) );
274 $out->addWikiMsg( 'emailsenttext', $target->getName() );
275 $out->returnToMain( false, $target->getUserPage() );
276 } else {
277 $out->setPageTitleMsg( $this->msg( 'emailuser-title-target', $target->getName() ) );
278 }
279 return true;
280 }
281
282 /**
283 * @param array $data
284 * @return StatusValue|false
285 * @internal Only public because it's used as an HTMLForm callback.
286 */
287 public function onFormSubmit( array $data ) {
288 // HTMLForm checked that this is a valid user name, the return value can never be null.
289 $target = $this->userFactory->newFromName( $data['Target'] );
290
291 $emailUser = $this->emailUserFactory->newEmailUser( $this->getAuthority() );
292 $emailUser->setEditToken( $this->getRequest()->getVal( 'wpEditToken' ) );
293
294 // Fully authorize on sending emails.
295 $status = $emailUser->authorizeSend();
296
297 if ( !$status->isOK() ) {
298 return $status;
299 }
300
301 // @phan-suppress-next-next-line PhanTypeMismatchArgumentNullable
302 $res = $emailUser->sendEmailUnsafe(
303 $target,
304 $data['Subject'],
305 $data['Text'],
306 $data['CCMe'],
307 $this->getLanguage()->getCode()
308 );
309 if ( $res->hasMessage( 'hookaborted' ) ) {
310 // BC: The method could previously return false if the EmailUser hook set the error to false. Preserve
311 // that behaviour until we replace the hook.
312 $res = false;
313 } else {
314 $res = Status::wrap( $res );
315 }
316 return $res;
317 }
318
319 /**
320 * Return an array of subpages beginning with $search that this special page will accept.
321 *
322 * @param string $search Prefix to search for
323 * @param int $limit Maximum number of results to return (usually 10)
324 * @param int $offset Number of results to skip (usually 0)
325 * @return string[] Matching subpages
326 */
327 public function prefixSearchSubpages( $search, $limit, $offset ) {
328 $search = $this->userNameUtils->getCanonical( $search );
329 if ( !$search ) {
330 // No prefix suggestion for invalid user
331 return [];
332 }
333 // Autocomplete subpage as user list - public to allow caching
334 return $this->userNamePrefixSearch
335 ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
336 }
337
338 /**
339 * @return bool
340 */
341 public function isListed() {
342 return $this->getConfig()->get( MainConfigNames::EnableUserEmail );
343 }
344
345 protected function getGroupName() {
346 return 'users';
347 }
348 }
349
350 /** @deprecated class alias since 1.41 */
351 class_alias( SpecialEmailUser::class, 'SpecialEmailUser' );