(bug 34289) user.options CSS loaded twice. Fixed by splitting off the CSS part of...
[lhc/web/wiklou.git] / includes / UserMailer.php
1 <?php
2 /**
3 * Classes used to send e-mails
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 * @author <brion@pobox.com>
22 * @author <mail@tgries.de>
23 * @author Tim Starling
24 */
25
26
27 /**
28 * Stores a single person's name and email address.
29 * These are passed in via the constructor, and will be returned in SMTP
30 * header format when requested.
31 */
32 class MailAddress {
33 /**
34 * @param $address string|User string with an email address, or a User object
35 * @param $name String: human-readable name if a string address is given
36 * @param $realName String: human-readable real name if a string address is given
37 */
38 function __construct( $address, $name = null, $realName = null ) {
39 if ( is_object( $address ) && $address instanceof User ) {
40 $this->address = $address->getEmail();
41 $this->name = $address->getName();
42 $this->realName = $address->getRealName();
43 } else {
44 $this->address = strval( $address );
45 $this->name = strval( $name );
46 $this->realName = strval( $realName );
47 }
48 }
49
50 /**
51 * Return formatted and quoted address to insert into SMTP headers
52 * @return string
53 */
54 function toString() {
55 # PHP's mail() implementation under Windows is somewhat shite, and
56 # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
57 # so don't bother generating them
58 if ( $this->address ) {
59 if ( $this->name != '' && !wfIsWindows() ) {
60 global $wgEnotifUseRealName;
61 $name = ( $wgEnotifUseRealName && $this->realName ) ? $this->realName : $this->name;
62 $quoted = UserMailer::quotedPrintable( $name );
63 if ( strpos( $quoted, '.' ) !== false || strpos( $quoted, ',' ) !== false ) {
64 $quoted = '"' . $quoted . '"';
65 }
66 return "$quoted <{$this->address}>";
67 } else {
68 return $this->address;
69 }
70 } else {
71 return "";
72 }
73 }
74
75 function __toString() {
76 return $this->toString();
77 }
78 }
79
80
81 /**
82 * Collection of static functions for sending mail
83 */
84 class UserMailer {
85 static $mErrorString;
86
87 /**
88 * Send mail using a PEAR mailer
89 *
90 * @param $mailer
91 * @param $dest
92 * @param $headers
93 * @param $body
94 *
95 * @return Status
96 */
97 protected static function sendWithPear( $mailer, $dest, $headers, $body ) {
98 $mailResult = $mailer->send( $dest, $headers, $body );
99
100 # Based on the result return an error string,
101 if ( PEAR::isError( $mailResult ) ) {
102 wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" );
103 return Status::newFatal( 'pear-mail-error', $mailResult->getMessage() );
104 } else {
105 return Status::newGood();
106 }
107 }
108
109 /**
110 * Creates a single string from an associative array
111 *
112 * @param $headers Associative Array: keys are header field names,
113 * values are ... values.
114 * @param $endl String: The end of line character. Defaults to "\n"
115 * @return String
116 */
117 static function arrayToHeaderString( $headers, $endl = "\n" ) {
118 foreach( $headers as $name => $value ) {
119 $string[] = "$name: $value";
120 }
121 return implode( $endl, $string );
122 }
123
124 /**
125 * Create a value suitable for the MessageId Header
126 *
127 * @return String
128 */
129 static function makeMsgId() {
130 global $wgSMTP, $wgServer;
131
132 $msgid = uniqid( wfWikiID() . ".", true ); /* true required for cygwin */
133 if ( is_array($wgSMTP) && isset($wgSMTP['IDHost']) && $wgSMTP['IDHost'] ) {
134 $domain = $wgSMTP['IDHost'];
135 } else {
136 $url = wfParseUrl($wgServer);
137 $domain = $url['host'];
138 }
139 return "<$msgid@$domain>";
140 }
141
142 /**
143 * This function will perform a direct (authenticated) login to
144 * a SMTP Server to use for mail relaying if 'wgSMTP' specifies an
145 * array of parameters. It requires PEAR:Mail to do that.
146 * Otherwise it just uses the standard PHP 'mail' function.
147 *
148 * @param $to MailAddress: recipient's email (or an array of them)
149 * @param $from MailAddress: sender's email
150 * @param $subject String: email's subject.
151 * @param $body String: email's text.
152 * @param $replyto MailAddress: optional reply-to email (default: null).
153 * @param $contentType String: optional custom Content-Type (default: text/plain; charset=UTF-8)
154 * @return Status object
155 */
156 public static function send( $to, $from, $subject, $body, $replyto = null, $contentType = 'text/plain; charset=UTF-8' ) {
157 global $wgSMTP, $wgEnotifMaxRecips, $wgAdditionalMailParams;
158
159 if ( !is_array( $to ) ) {
160 $to = array( $to );
161 }
162
163 wfDebug( __METHOD__ . ': sending mail to ' . implode( ', ', $to ) . "\n" );
164
165 $dest = array();
166 foreach ( $to as $u ) {
167 if ( $u->address ) {
168 $dest[] = $u->address;
169 }
170 }
171 if ( count( $dest ) == 0 ) {
172 return Status::newFatal( 'user-mail-no-addy' );
173 }
174
175 $headers['From'] = $from->toString();
176 $headers['Return-Path'] = $from->address;
177 if ( count( $to ) == 1 ) {
178 $headers['To'] = $to[0]->toString();
179 } else {
180 $headers['To'] = 'undisclosed-recipients:;';
181 }
182
183 if ( $replyto ) {
184 $headers['Reply-To'] = $replyto->toString();
185 }
186
187 $headers['Subject'] = self::quotedPrintable( $subject );
188 $headers['Date'] = date( 'r' );
189 $headers['MIME-Version'] = '1.0';
190 $headers['Content-type'] = ( is_null( $contentType ) ?
191 'text/plain; charset=UTF-8' : $contentType );
192 $headers['Content-transfer-encoding'] = '8bit';
193
194 $headers['Message-ID'] = self::makeMsgId();
195 $headers['X-Mailer'] = 'MediaWiki mailer';
196
197 $ret = wfRunHooks( 'AlternateUserMailer', array( $headers, $to, $from, $subject, $body ) );
198 if ( $ret === false ) {
199 return Status::newGood();
200 } elseif ( $ret !== true ) {
201 return Status::newFatal( 'php-mail-error', $ret );
202 }
203
204 if ( is_array( $wgSMTP ) ) {
205 if ( function_exists( 'stream_resolve_include_path' ) ) {
206 $found = stream_resolve_include_path( 'Mail.php' );
207 } else {
208 $found = Fallback::stream_resolve_include_path( 'Mail.php' );
209 }
210 if ( !$found ) {
211 throw new MWException( 'PEAR mail package is not installed' );
212 }
213 require_once( 'Mail.php' );
214
215 wfSuppressWarnings();
216
217 // Create the mail object using the Mail::factory method
218 $mail_object =& Mail::factory( 'smtp', $wgSMTP );
219 if ( PEAR::isError( $mail_object ) ) {
220 wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" );
221 wfRestoreWarnings();
222 return Status::newFatal( 'pear-mail-error', $mail_object->getMessage() );
223 }
224
225 wfDebug( "Sending mail via PEAR::Mail\n" );
226 $chunks = array_chunk( $dest, $wgEnotifMaxRecips );
227 foreach ( $chunks as $chunk ) {
228 $status = self::sendWithPear( $mail_object, $chunk, $headers, $body );
229 if ( !$status->isOK() ) {
230 wfRestoreWarnings();
231 return $status;
232 }
233 }
234 wfRestoreWarnings();
235 return Status::newGood();
236 } else {
237 # Line endings need to be different on Unix and Windows due to
238 # the bug described at http://trac.wordpress.org/ticket/2603
239 if ( wfIsWindows() ) {
240 $body = str_replace( "\n", "\r\n", $body );
241 $endl = "\r\n";
242 } else {
243 $endl = "\n";
244 }
245
246 $headers = self::arrayToHeaderString( $headers, $endl );
247
248 wfDebug( "Sending mail via internal mail() function\n" );
249
250 self::$mErrorString = '';
251 $html_errors = ini_get( 'html_errors' );
252 ini_set( 'html_errors', '0' );
253 set_error_handler( 'UserMailer::errorHandler' );
254
255 $safeMode = wfIniGetBool( 'safe_mode' );
256 foreach ( $dest as $recip ) {
257 if ( $safeMode ) {
258 $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers );
259 } else {
260 $sent = mail( $recip, self::quotedPrintable( $subject ), $body, $headers, $wgAdditionalMailParams );
261 }
262 }
263
264 restore_error_handler();
265 ini_set( 'html_errors', $html_errors );
266
267 if ( self::$mErrorString ) {
268 wfDebug( "Error sending mail: " . self::$mErrorString . "\n" );
269 return Status::newFatal( 'php-mail-error', self::$mErrorString );
270 } elseif ( ! $sent ) {
271 // mail function only tells if there's an error
272 wfDebug( "Unknown error sending mail\n" );
273 return Status::newFatal( 'php-mail-error-unknown' );
274 } else {
275 return Status::newGood();
276 }
277 }
278 }
279
280 /**
281 * Set the mail error message in self::$mErrorString
282 *
283 * @param $code Integer: error number
284 * @param $string String: error message
285 */
286 static function errorHandler( $code, $string ) {
287 self::$mErrorString = preg_replace( '/^mail\(\)(\s*\[.*?\])?: /', '', $string );
288 }
289
290 /**
291 * Converts a string into a valid RFC 822 "phrase", such as is used for the sender name
292 * @param $phrase string
293 * @return string
294 */
295 public static function rfc822Phrase( $phrase ) {
296 $phrase = strtr( $phrase, array( "\r" => '', "\n" => '', '"' => '' ) );
297 return '"' . $phrase . '"';
298 }
299
300 /**
301 * Converts a string into quoted-printable format
302 * @since 1.17
303 */
304 public static function quotedPrintable( $string, $charset = '' ) {
305 # Probably incomplete; see RFC 2045
306 if( empty( $charset ) ) {
307 $charset = 'UTF-8';
308 }
309 $charset = strtoupper( $charset );
310 $charset = str_replace( 'ISO-8859', 'ISO8859', $charset ); // ?
311
312 $illegal = '\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\xff=';
313 $replace = $illegal . '\t ?_';
314 if( !preg_match( "/[$illegal]/", $string ) ) {
315 return $string;
316 }
317 $out = "=?$charset?Q?";
318 $out .= preg_replace_callback( "/([$replace])/",
319 array( __CLASS__, 'quotedPrintableCallback' ), $string );
320 $out .= '?=';
321 return $out;
322 }
323
324 protected static function quotedPrintableCallback( $matches ) {
325 return sprintf( "=%02X", ord( $matches[1] ) );
326 }
327 }
328
329 /**
330 * This module processes the email notifications when the current page is
331 * changed. It looks up the table watchlist to find out which users are watching
332 * that page.
333 *
334 * The current implementation sends independent emails to each watching user for
335 * the following reason:
336 *
337 * - Each watching user will be notified about the page edit time expressed in
338 * his/her local time (UTC is shown additionally). To achieve this, we need to
339 * find the individual timeoffset of each watching user from the preferences..
340 *
341 * Suggested improvement to slack down the number of sent emails: We could think
342 * of sending out bulk mails (bcc:user1,user2...) for all these users having the
343 * same timeoffset in their preferences.
344 *
345 * Visit the documentation pages under http://meta.wikipedia.com/Enotif
346 *
347 *
348 */
349 class EmailNotification {
350 protected $subject, $body, $replyto, $from;
351 protected $timestamp, $summary, $minorEdit, $oldid, $composed_common;
352 protected $mailTargets = array();
353
354 /**
355 * @var Title
356 */
357 protected $title;
358
359 /**
360 * @var User
361 */
362 protected $editor;
363
364 /**
365 * Send emails corresponding to the user $editor editing the page $title.
366 * Also updates wl_notificationtimestamp.
367 *
368 * May be deferred via the job queue.
369 *
370 * @param $editor User object
371 * @param $title Title object
372 * @param $timestamp
373 * @param $summary
374 * @param $minorEdit
375 * @param $oldid (default: false)
376 */
377 public function notifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid = false ) {
378 global $wgEnotifUseJobQ, $wgEnotifWatchlist, $wgShowUpdatedMarker, $wgEnotifMinorEdits,
379 $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk;
380
381 if ( $title->getNamespace() < 0 ) {
382 return;
383 }
384
385 // Build a list of users to notfiy
386 $watchers = array();
387 if ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) {
388 $dbw = wfGetDB( DB_MASTER );
389 $res = $dbw->select( array( 'watchlist' ),
390 array( 'wl_user' ),
391 array(
392 'wl_title' => $title->getDBkey(),
393 'wl_namespace' => $title->getNamespace(),
394 'wl_user != ' . intval( $editor->getID() ),
395 'wl_notificationtimestamp IS NULL',
396 ), __METHOD__
397 );
398 foreach ( $res as $row ) {
399 $watchers[] = intval( $row->wl_user );
400 }
401 if ( $watchers ) {
402 // Update wl_notificationtimestamp for all watching users except
403 // the editor
404 $dbw->begin();
405 $dbw->update( 'watchlist',
406 array( /* SET */
407 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
408 ), array( /* WHERE */
409 'wl_title' => $title->getDBkey(),
410 'wl_namespace' => $title->getNamespace(),
411 'wl_user' => $watchers
412 ), __METHOD__
413 );
414 $dbw->commit();
415 }
416 }
417
418 $sendEmail = true;
419 // If nobody is watching the page, and there are no users notified on all changes
420 // don't bother creating a job/trying to send emails
421 // $watchers deals with $wgEnotifWatchlist
422 if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) {
423 $sendEmail = false;
424 // Only send notification for non minor edits, unless $wgEnotifMinorEdits
425 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
426 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
427 if ( $wgEnotifUserTalk && $isUserTalkPage && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) ) {
428 $sendEmail = true;
429 }
430 }
431 }
432
433 if ( !$sendEmail ) {
434 return;
435 }
436 if ( $wgEnotifUseJobQ ) {
437 $params = array(
438 'editor' => $editor->getName(),
439 'editorID' => $editor->getID(),
440 'timestamp' => $timestamp,
441 'summary' => $summary,
442 'minorEdit' => $minorEdit,
443 'oldid' => $oldid,
444 'watchers' => $watchers
445 );
446 $job = new EnotifNotifyJob( $title, $params );
447 $job->insert();
448 } else {
449 $this->actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers );
450 }
451 }
452
453 /**
454 * Immediate version of notifyOnPageChange().
455 *
456 * Send emails corresponding to the user $editor editing the page $title.
457 * Also updates wl_notificationtimestamp.
458 *
459 * @param $editor User object
460 * @param $title Title object
461 * @param $timestamp string Edit timestamp
462 * @param $summary string Edit summary
463 * @param $minorEdit bool
464 * @param $oldid int Revision ID
465 * @param $watchers array of user IDs
466 */
467 public function actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers ) {
468 # we use $wgPasswordSender as sender's address
469 global $wgEnotifWatchlist;
470 global $wgEnotifMinorEdits, $wgEnotifUserTalk;
471
472 wfProfileIn( __METHOD__ );
473
474 # The following code is only run, if several conditions are met:
475 # 1. EmailNotification for pages (other than user_talk pages) must be enabled
476 # 2. minor edits (changes) are only regarded if the global flag indicates so
477
478 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
479
480 $this->title = $title;
481 $this->timestamp = $timestamp;
482 $this->summary = $summary;
483 $this->minorEdit = $minorEdit;
484 $this->oldid = $oldid;
485 $this->editor = $editor;
486 $this->composed_common = false;
487
488 $userTalkId = false;
489
490 if ( !$minorEdit || ( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
491
492 if ( $wgEnotifUserTalk && $isUserTalkPage && $this->canSendUserTalkEmail( $editor, $title, $minorEdit ) ) {
493 $targetUser = User::newFromName( $title->getText() );
494 $this->compose( $targetUser );
495 $userTalkId = $targetUser->getId();
496 }
497
498 if ( $wgEnotifWatchlist ) {
499 // Send updates to watchers other than the current editor
500 $userArray = UserArray::newFromIDs( $watchers );
501 foreach ( $userArray as $watchingUser ) {
502 if ( $watchingUser->getOption( 'enotifwatchlistpages' ) &&
503 ( !$minorEdit || $watchingUser->getOption( 'enotifminoredits' ) ) &&
504 $watchingUser->isEmailConfirmed() &&
505 $watchingUser->getID() != $userTalkId )
506 {
507 $this->compose( $watchingUser );
508 }
509 }
510 }
511 }
512
513 global $wgUsersNotifiedOnAllChanges;
514 foreach ( $wgUsersNotifiedOnAllChanges as $name ) {
515 if ( $editor->getName() == $name ) {
516 // No point notifying the user that actually made the change!
517 continue;
518 }
519 $user = User::newFromName( $name );
520 $this->compose( $user );
521 }
522
523 $this->sendMails();
524 wfProfileOut( __METHOD__ );
525 }
526
527 /**
528 * @param $editor User
529 * @param $title Title bool
530 * @param $minorEdit
531 * @return bool
532 */
533 private function canSendUserTalkEmail( $editor, $title, $minorEdit ) {
534 global $wgEnotifUserTalk;
535 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK );
536
537 if ( $wgEnotifUserTalk && $isUserTalkPage ) {
538 $targetUser = User::newFromName( $title->getText() );
539
540 if ( !$targetUser || $targetUser->isAnon() ) {
541 wfDebug( __METHOD__ . ": user talk page edited, but user does not exist\n" );
542 } elseif ( $targetUser->getId() == $editor->getId() ) {
543 wfDebug( __METHOD__ . ": user edited their own talk page, no notification sent\n" );
544 } elseif ( $targetUser->getOption( 'enotifusertalkpages' ) &&
545 ( !$minorEdit || $targetUser->getOption( 'enotifminoredits' ) ) )
546 {
547 if ( $targetUser->isEmailConfirmed() ) {
548 wfDebug( __METHOD__ . ": sending talk page update notification\n" );
549 return true;
550 } else {
551 wfDebug( __METHOD__ . ": talk page owner doesn't have validated email\n" );
552 }
553 } else {
554 wfDebug( __METHOD__ . ": talk page owner doesn't want notifications\n" );
555 }
556 }
557 return false;
558 }
559
560 /**
561 * Generate the generic "this page has been changed" e-mail text.
562 */
563 private function composeCommonMailtext() {
564 global $wgPasswordSender, $wgPasswordSenderName, $wgNoReplyAddress;
565 global $wgEnotifFromEditor, $wgEnotifRevealEditorAddress;
566 global $wgEnotifImpersonal, $wgEnotifUseRealName;
567
568 $this->composed_common = true;
569
570 # You as the WikiAdmin and Sysops can make use of plenty of
571 # named variables when composing your notification emails while
572 # simply editing the Meta pages
573
574 $keys = array();
575
576 if ( $this->oldid ) {
577 if ( $wgEnotifImpersonal ) {
578 // For impersonal mail, show a diff link to the last revision.
579 $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastdiff',
580 $this->title->getCanonicalUrl( 'diff=next&oldid=' . $this->oldid ) );
581 } else {
582 $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastvisited',
583 $this->title->getCanonicalUrl( 'diff=0&oldid=' . $this->oldid ) );
584 }
585 $keys['$OLDID'] = $this->oldid;
586 $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'changed' );
587 } else {
588 $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_newpagetext' );
589 # clear $OLDID placeholder in the message template
590 $keys['$OLDID'] = '';
591 $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'created' );
592 }
593
594 $keys['$PAGETITLE'] = $this->title->getPrefixedText();
595 $keys['$PAGETITLE_URL'] = $this->title->getCanonicalUrl();
596 $keys['$PAGEMINOREDIT'] = $this->minorEdit ? wfMsgForContent( 'minoredit' ) : '';
597 $keys['$PAGESUMMARY'] = $this->summary == '' ? ' - ' : $this->summary;
598 $keys['$UNWATCHURL'] = $this->title->getCanonicalUrl( 'action=unwatch' );
599
600 if ( $this->editor->isAnon() ) {
601 # real anon (user:xxx.xxx.xxx.xxx)
602 $keys['$PAGEEDITOR'] = wfMsgForContent( 'enotif_anon_editor', $this->editor->getName() );
603 $keys['$PAGEEDITOR_EMAIL'] = wfMsgForContent( 'noemailtitle' );
604 } else {
605 $keys['$PAGEEDITOR'] = $wgEnotifUseRealName ? $this->editor->getRealName() : $this->editor->getName();
606 $emailPage = SpecialPage::getSafeTitleFor( 'Emailuser', $this->editor->getName() );
607 $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalUrl();
608 }
609
610 $keys['$PAGEEDITOR_WIKI'] = $this->editor->getUserPage()->getCanonicalUrl();
611
612 # Now build message's subject and body
613
614 $subject = wfMsgExt( 'enotif_subject', 'content' );
615 $subject = strtr( $subject, $keys );
616 $this->subject = MessageCache::singleton()->transform( $subject, false, null, $this->title );
617
618 $body = wfMsgExt( 'enotif_body', 'content' );
619 $body = strtr( $body, $keys );
620 $body = MessageCache::singleton()->transform( $body, false, null, $this->title );
621 $this->body = wordwrap( $body, 72 );
622
623 # Reveal the page editor's address as REPLY-TO address only if
624 # the user has not opted-out and the option is enabled at the
625 # global configuration level.
626 $adminAddress = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
627 if ( $wgEnotifRevealEditorAddress
628 && ( $this->editor->getEmail() != '' )
629 && $this->editor->getOption( 'enotifrevealaddr' ) )
630 {
631 $editorAddress = new MailAddress( $this->editor );
632 if ( $wgEnotifFromEditor ) {
633 $this->from = $editorAddress;
634 } else {
635 $this->from = $adminAddress;
636 $this->replyto = $editorAddress;
637 }
638 } else {
639 $this->from = $adminAddress;
640 $this->replyto = new MailAddress( $wgNoReplyAddress );
641 }
642 }
643
644 /**
645 * Compose a mail to a given user and either queue it for sending, or send it now,
646 * depending on settings.
647 *
648 * Call sendMails() to send any mails that were queued.
649 * @param $user User
650 */
651 function compose( $user ) {
652 global $wgEnotifImpersonal;
653
654 if ( !$this->composed_common )
655 $this->composeCommonMailtext();
656
657 if ( $wgEnotifImpersonal ) {
658 $this->mailTargets[] = new MailAddress( $user );
659 } else {
660 $this->sendPersonalised( $user );
661 }
662 }
663
664 /**
665 * Send any queued mails
666 */
667 function sendMails() {
668 global $wgEnotifImpersonal;
669 if ( $wgEnotifImpersonal ) {
670 $this->sendImpersonal( $this->mailTargets );
671 }
672 }
673
674 /**
675 * Does the per-user customizations to a notification e-mail (name,
676 * timestamp in proper timezone, etc) and sends it out.
677 * Returns true if the mail was sent successfully.
678 *
679 * @param $watchingUser User object
680 * @return Boolean
681 * @private
682 */
683 function sendPersonalised( $watchingUser ) {
684 global $wgContLang, $wgEnotifUseRealName;
685 // From the PHP manual:
686 // Note: The to parameter cannot be an address in the form of "Something <someone@example.com>".
687 // The mail command will not parse this properly while talking with the MTA.
688 $to = new MailAddress( $watchingUser );
689
690 # $PAGEEDITDATE is the time and date of the page change
691 # expressed in terms of individual local time of the notification
692 # recipient, i.e. watching user
693 $body = str_replace(
694 array( '$WATCHINGUSERNAME',
695 '$PAGEEDITDATE',
696 '$PAGEEDITTIME' ),
697 array( $wgEnotifUseRealName ? $watchingUser->getRealName() : $watchingUser->getName(),
698 $wgContLang->userDate( $this->timestamp, $watchingUser ),
699 $wgContLang->userTime( $this->timestamp, $watchingUser ) ),
700 $this->body );
701
702 return UserMailer::send( $to, $this->from, $this->subject, $body, $this->replyto );
703 }
704
705 /**
706 * Same as sendPersonalised but does impersonal mail suitable for bulk
707 * mailing. Takes an array of MailAddress objects.
708 */
709 function sendImpersonal( $addresses ) {
710 global $wgContLang;
711
712 if ( empty( $addresses ) )
713 return;
714
715 $body = str_replace(
716 array( '$WATCHINGUSERNAME',
717 '$PAGEEDITDATE',
718 '$PAGEEDITTIME' ),
719 array( wfMsgForContent( 'enotif_impersonal_salutation' ),
720 $wgContLang->date( $this->timestamp, false, false ),
721 $wgContLang->time( $this->timestamp, false, false ) ),
722 $this->body );
723
724 return UserMailer::send( $addresses, $this->from, $this->subject, $body, $this->replyto );
725 }
726
727 } # end of class EmailNotification