Index: trunk/phase3/includes/specials/SpecialEmailuser.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | */ |
7 | 7 | |
8 | 8 | /** |
9 | | - * Constructor for Special:Emailuser. |
| 9 | + * Constructor for Special:Emailuser. |
10 | 10 | */ |
11 | 11 | function wfSpecialEmailuser( $par ) { |
12 | 12 | global $wgRequest, $wgUser, $wgOut; |
— | — | @@ -18,12 +18,12 @@ |
19 | 19 | $action = $wgRequest->getVal( 'action' ); |
20 | 20 | $target = isset($par) ? $par : $wgRequest->getVal( 'target' ); |
21 | 21 | $targetUser = EmailUserForm::validateEmailTarget( $target ); |
22 | | - |
| 22 | + |
23 | 23 | if ( !( $targetUser instanceof User ) ) { |
24 | 24 | $wgOut->showErrorPage( $targetUser.'title', $targetUser.'text' ); |
25 | 25 | return; |
26 | 26 | } |
27 | | - |
| 27 | + |
28 | 28 | $form = new EmailUserForm( $targetUser, |
29 | 29 | $wgRequest->getText( 'wpText' ), |
30 | 30 | $wgRequest->getText( 'wpSubject' ), |
— | — | @@ -32,7 +32,7 @@ |
33 | 33 | $form->showSuccess(); |
34 | 34 | return; |
35 | 35 | } |
36 | | - |
| 36 | + |
37 | 37 | $error = EmailUserForm::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) ); |
38 | 38 | if ( $error ) { |
39 | 39 | switch ( $error ) { |
— | — | @@ -53,13 +53,13 @@ |
54 | 54 | list( $title, $msg, $params ) = $error; |
55 | 55 | $wgOut->showErrorPage( $title, $msg, $params ); |
56 | 56 | return; |
57 | | - |
| 57 | + |
58 | 58 | } |
59 | | - } |
60 | | - |
| 59 | + } |
| 60 | + |
61 | 61 | if ( "submit" == $action && $wgRequest->wasPosted() ) { |
62 | 62 | $result = $form->doSubmit(); |
63 | | - |
| 63 | + |
64 | 64 | if ( !is_null( $result ) ) { |
65 | 65 | $wgOut->addHTML( wfMsg( "usermailererror" ) . |
66 | 66 | ' ' . htmlspecialchars( $result->getMessage() ) ); |
— | — | @@ -74,26 +74,36 @@ |
75 | 75 | } |
76 | 76 | |
77 | 77 | /** |
78 | | - * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message. |
| 78 | + * Implements the Special:Emailuser web interface, and invokes |
| 79 | + * UserMailer::send() for sending the email message. |
| 80 | + * |
79 | 81 | * @ingroup SpecialPage |
80 | 82 | */ |
81 | 83 | class EmailUserForm { |
82 | 84 | |
83 | 85 | var $target; |
84 | 86 | var $text, $subject; |
85 | | - var $cc_me; // Whether user requested to be sent a separate copy of their email. |
| 87 | + var $cc_me; // Whether user requested to be sent a separate copy of their email. |
86 | 88 | |
87 | 89 | /** |
88 | | - * @param User $target |
| 90 | + * Constructor |
| 91 | + * |
| 92 | + * @param $target User object |
| 93 | + * @param $text String: message contents |
| 94 | + * @param $subject String: message subject |
| 95 | + * @param $cc_me Boolean: wheter to send a copy of the message to the sender user |
89 | 96 | */ |
90 | | - function EmailUserForm( $target, $text, $subject, $cc_me ) { |
| 97 | + public function EmailUserForm( $target, $text, $subject, $cc_me ) { |
91 | 98 | $this->target = $target; |
92 | 99 | $this->text = $text; |
93 | 100 | $this->subject = $subject; |
94 | 101 | $this->cc_me = $cc_me; |
95 | 102 | } |
96 | 103 | |
97 | | - function showForm() { |
| 104 | + /** |
| 105 | + * Display the form to send a email |
| 106 | + */ |
| 107 | + public function showForm() { |
98 | 108 | global $wgOut, $wgUser; |
99 | 109 | $skin = $wgUser->getSkin(); |
100 | 110 | |
— | — | @@ -108,7 +118,7 @@ |
109 | 119 | $action = $titleObj->getLocalURL( "target=" . |
110 | 120 | urlencode( $this->target->getName() ) . "&action=submit" ); |
111 | 121 | |
112 | | - $wgOut->addHTML( |
| 122 | + $wgOut->addHTML( |
113 | 123 | Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) . |
114 | 124 | Xml::openElement( 'fieldset' ) . |
115 | 125 | Xml::element( 'legend', null, wfMsgExt( 'email-legend', 'parsemag' ) ) . |
— | — | @@ -164,12 +174,14 @@ |
165 | 175 | ); |
166 | 176 | } |
167 | 177 | |
168 | | - /* |
169 | | - * Really send a mail. Permissions should have been checked using |
| 178 | + /** |
| 179 | + * Really send a mail. Permissions should have been checked using |
170 | 180 | * EmailUserForm::getPermissionsError. It is probably also a good idea to |
171 | 181 | * check the edit token and ping limiter in advance. |
| 182 | + * |
| 183 | + * @return Mixed: WikiError on error or null |
172 | 184 | */ |
173 | | - function doSubmit() { |
| 185 | + public function doSubmit() { |
174 | 186 | global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName; |
175 | 187 | |
176 | 188 | $to = new MailAddress( $this->target ); |
— | — | @@ -179,7 +191,7 @@ |
180 | 192 | // Add a standard footer and trim up trailing newlines |
181 | 193 | $this->text = rtrim($this->text) . "\n\n-- \n" . wfMsgExt( 'emailuserfooter', |
182 | 194 | array( 'content', 'parsemag' ), array( $from->name, $to->name ) ); |
183 | | - |
| 195 | + |
184 | 196 | if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) { |
185 | 197 | |
186 | 198 | if( $wgUserEmailUseReplyTo ) { |
— | — | @@ -209,14 +221,12 @@ |
210 | 222 | $mailFrom = $from; |
211 | 223 | $replyTo = null; |
212 | 224 | } |
213 | | - |
| 225 | + |
214 | 226 | $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo ); |
215 | 227 | |
216 | 228 | if( WikiError::isError( $mailResult ) ) { |
217 | 229 | return $mailResult; |
218 | | - |
219 | 230 | } else { |
220 | | - |
221 | 231 | // if the user requested a copy of this mail, do this now, |
222 | 232 | // unless they are emailing themselves, in which case one copy of the message is sufficient. |
223 | 233 | if ($this->cc_me && $to != $from) { |
— | — | @@ -240,9 +250,12 @@ |
241 | 251 | } |
242 | 252 | } |
243 | 253 | |
244 | | - function showSuccess( &$user = null ) { |
| 254 | + /** |
| 255 | + * Show "Your e-mail message has been sent." message |
| 256 | + */ |
| 257 | + public function showSuccess( &$user = null ) { |
245 | 258 | global $wgOut; |
246 | | - |
| 259 | + |
247 | 260 | if ( is_null($user) ) |
248 | 261 | $user = $this->target; |
249 | 262 | |
— | — | @@ -251,28 +264,44 @@ |
252 | 265 | |
253 | 266 | $wgOut->returnToMain( false, $user->getUserPage() ); |
254 | 267 | } |
255 | | - |
256 | | - function getTarget() { |
| 268 | + |
| 269 | + /** |
| 270 | + * Get target user |
| 271 | + * |
| 272 | + * @return User object |
| 273 | + */ |
| 274 | + public function getTarget() { |
257 | 275 | return $this->target; |
258 | 276 | } |
259 | | - |
260 | | - static function userEmailEnabled() { |
| 277 | + |
| 278 | + /** |
| 279 | + * Check whether user-to-user emails are enabled |
| 280 | + * |
| 281 | + * @return Boolean |
| 282 | + */ |
| 283 | + public static function userEmailEnabled() { |
261 | 284 | global $wgEnableEmail, $wgEnableUserEmail; |
262 | 285 | return $wgEnableEmail && $wgEnableUserEmail; |
263 | | - |
264 | 286 | } |
265 | | - static function validateEmailTarget ( $target ) { |
| 287 | + |
| 288 | + /** |
| 289 | + * Validate target User |
| 290 | + * |
| 291 | + * @param $target String: target user name |
| 292 | + * @return User object on success or a string on error |
| 293 | + */ |
| 294 | + public static function validateEmailTarget( $target ) { |
266 | 295 | if ( $target == "" ) { |
267 | 296 | wfDebug( "Target is empty.\n" ); |
268 | 297 | return "notarget"; |
269 | 298 | } |
270 | | - |
| 299 | + |
271 | 300 | $nt = Title::newFromURL( $target ); |
272 | 301 | if ( is_null( $nt ) ) { |
273 | 302 | wfDebug( "Target is invalid title.\n" ); |
274 | 303 | return "notarget"; |
275 | 304 | } |
276 | | - |
| 305 | + |
277 | 306 | $nu = User::newFromName( $nt->getText() ); |
278 | 307 | if( is_null( $nu ) || !$nu->getId() ) { |
279 | 308 | wfDebug( "Target is invalid user.\n" ); |
— | — | @@ -284,10 +313,18 @@ |
285 | 314 | wfDebug( "User does not allow user emails.\n" ); |
286 | 315 | return "nowikiemail"; |
287 | 316 | } |
288 | | - |
| 317 | + |
289 | 318 | return $nu; |
290 | 319 | } |
291 | | - static function getPermissionsError ( $user, $editToken ) { |
| 320 | + |
| 321 | + /** |
| 322 | + * Check whether user is allowed to send email |
| 323 | + * |
| 324 | + * @param $user User object |
| 325 | + * @param $editToken String: edit token |
| 326 | + * @return null on success or string on error |
| 327 | + */ |
| 328 | + public static function getPermissionsError( $user, $editToken ) { |
292 | 329 | if( !$user->canSendEmail() ) { |
293 | 330 | wfDebug( "User can't send.\n" ); |
294 | 331 | // FIXME: this is also the error if user is in a group |
— | — | @@ -296,32 +333,40 @@ |
297 | 334 | // be more fine grained. |
298 | 335 | return "mailnologin"; |
299 | 336 | } |
300 | | - |
| 337 | + |
301 | 338 | if( $user->isBlockedFromEmailuser() ) { |
302 | 339 | wfDebug( "User is blocked from sending e-mail.\n" ); |
303 | 340 | return "blockedemailuser"; |
304 | 341 | } |
305 | | - |
| 342 | + |
306 | 343 | if( $user->pingLimiter( 'emailuser' ) ) { |
307 | | - wfDebug( "Ping limiter triggered.\n" ); |
| 344 | + wfDebug( "Ping limiter triggered.\n" ); |
308 | 345 | return 'actionthrottledtext'; |
309 | 346 | } |
310 | | - |
| 347 | + |
311 | 348 | $hookErr = null; |
312 | 349 | wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) ); |
313 | | - |
| 350 | + |
314 | 351 | if ($hookErr) { |
315 | 352 | return $hookErr; |
316 | 353 | } |
317 | | - |
| 354 | + |
318 | 355 | if( !$user->matchEditToken( $editToken ) ) { |
319 | 356 | wfDebug( "Matching edit token failed.\n" ); |
320 | 357 | return 'sessionfailure'; |
321 | 358 | } |
322 | 359 | } |
323 | | - |
324 | | - static function newFromURL( $target, $text, $subject, $cc_me ) |
325 | | - { |
| 360 | + |
| 361 | + /** |
| 362 | + * Get a EmailUserForm object |
| 363 | + * |
| 364 | + * @param $target String: user name |
| 365 | + * @param $text String: message contents |
| 366 | + * @param $subject String: message subject |
| 367 | + * @param $cc_me Boolean: wheter to send a copy of the message to the sender user |
| 368 | + * @return EmailUserForm object |
| 369 | + */ |
| 370 | + public static function newFromURL( $target, $text, $subject, $cc_me ) { |
326 | 371 | $nt = Title::newFromURL( $target ); |
327 | 372 | $nu = User::newFromName( $nt->getText() ); |
328 | 373 | return new EmailUserForm( $nu, $text, $subject, $cc_me ); |