123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614 |
- <?php
- class MailAddress {
-
- function __construct( $address, $name = null, $realName = null ) {
- if( is_object( $address ) && $address instanceof User ) {
- $this->address = $address->getEmail();
- $this->name = $address->getName();
- $this->realName = $address->getRealName();
- } else {
- $this->address = strval( $address );
- $this->name = strval( $name );
- $this->realName = strval( $realName );
- }
- }
-
- function toString() {
-
-
-
- if( $this->name != '' && !wfIsWindows() ) {
- global $wgEnotifUseRealName;
- $name = ( $wgEnotifUseRealName && $this->realName ) ? $this->realName : $this->name;
- $quoted = wfQuotedPrintable( $name );
- if( strpos( $quoted, '.' ) !== false || strpos( $quoted, ',' ) !== false ) {
- $quoted = '"' . $quoted . '"';
- }
- return "$quoted <{$this->address}>";
- } else {
- return $this->address;
- }
- }
- function __toString() {
- return $this->toString();
- }
- }
- class UserMailer {
-
- protected static function sendWithPear($mailer, $dest, $headers, $body)
- {
- $mailResult = $mailer->send($dest, $headers, $body);
-
- if( PEAR::isError( $mailResult ) ) {
- wfDebug( "PEAR::Mail failed: " . $mailResult->getMessage() . "\n" );
- return new WikiError( $mailResult->getMessage() );
- } else {
- return true;
- }
- }
-
- static function send( $to, $from, $subject, $body, $replyto=null, $contentType=null ) {
- global $wgSMTP, $wgOutputEncoding, $wgErrorString, $wgEnotifImpersonal;
- global $wgEnotifMaxRecips;
- if ( is_array( $to ) ) {
- wfDebug( __METHOD__.': sending mail to ' . implode( ',', $to ) . "\n" );
- } else {
- wfDebug( __METHOD__.': sending mail to ' . implode( ',', array( $to->toString() ) ) . "\n" );
- }
- if (is_array( $wgSMTP )) {
- require_once( 'Mail.php' );
- $msgid = str_replace(" ", "_", microtime());
- if (function_exists('posix_getpid'))
- $msgid .= '.' . posix_getpid();
- if (is_array($to)) {
- $dest = array();
- foreach ($to as $u)
- $dest[] = $u->address;
- } else
- $dest = $to->address;
- $headers['From'] = $from->toString();
- if ($wgEnotifImpersonal) {
- $headers['To'] = 'undisclosed-recipients:;';
- }
- else {
- $headers['To'] = implode( ", ", (array )$dest );
- }
- if ( $replyto ) {
- $headers['Reply-To'] = $replyto->toString();
- }
- $headers['Subject'] = wfQuotedPrintable( $subject );
- $headers['Date'] = date( 'r' );
- $headers['MIME-Version'] = '1.0';
- $headers['Content-type'] = (is_null($contentType) ?
- 'text/plain; charset='.$wgOutputEncoding : $contentType);
- $headers['Content-transfer-encoding'] = '8bit';
- $headers['Message-ID'] = "<$msgid@" . $wgSMTP['IDHost'] . '>';
- $headers['X-Mailer'] = 'MediaWiki mailer';
-
- $mail_object =& Mail::factory('smtp', $wgSMTP);
- if( PEAR::isError( $mail_object ) ) {
- wfDebug( "PEAR::Mail factory failed: " . $mail_object->getMessage() . "\n" );
- return new WikiError( $mail_object->getMessage() );
- }
- wfDebug( "Sending mail via PEAR::Mail to $dest\n" );
- $chunks = array_chunk( (array)$dest, $wgEnotifMaxRecips );
- foreach ($chunks as $chunk) {
- $e = self::sendWithPear($mail_object, $chunk, $headers, $body);
- if( WikiError::isError( $e ) )
- return $e;
- }
- } else {
-
-
-
-
- if ( wfIsWindows() ) {
- $body = str_replace( "\n", "\r\n", $body );
- $endl = "\r\n";
- } else {
- $endl = "\n";
- }
- $ctype = (is_null($contentType) ?
- 'text/plain; charset='.$wgOutputEncoding : $contentType);
- $headers =
- "MIME-Version: 1.0$endl" .
- "Content-type: $ctype$endl" .
- "Content-Transfer-Encoding: 8bit$endl" .
- "X-Mailer: MediaWiki mailer$endl".
- 'From: ' . $from->toString();
- if ($replyto) {
- $headers .= "{$endl}Reply-To: " . $replyto->toString();
- }
- $wgErrorString = '';
- $html_errors = ini_get( 'html_errors' );
- ini_set( 'html_errors', '0' );
- set_error_handler( array( 'UserMailer', 'errorHandler' ) );
- wfDebug( "Sending mail via internal mail() function\n" );
- if (function_exists('mail')) {
- if (is_array($to)) {
- foreach ($to as $recip) {
- $sent = mail( $recip->toString(), wfQuotedPrintable( $subject ), $body, $headers );
- }
- } else {
- $sent = mail( $to->toString(), wfQuotedPrintable( $subject ), $body, $headers );
- }
- } else {
- $wgErrorString = 'PHP is not configured to send mail';
- }
- restore_error_handler();
- ini_set( 'html_errors', $html_errors );
- if ( $wgErrorString ) {
- wfDebug( "Error sending mail: $wgErrorString\n" );
- return new WikiError( $wgErrorString );
- } elseif (! $sent) {
-
- wfDebug( "Error sending mail\n" );
- return new WikiError( 'mailer error' );
- } else {
- return true;
- }
- }
- }
-
- static function errorHandler( $code, $string ) {
- global $wgErrorString;
- $wgErrorString = preg_replace( '/^mail\(\)(\s*\[.*?\])?: /', '', $string );
- }
-
- static function rfc822Phrase( $phrase ) {
- $phrase = strtr( $phrase, array( "\r" => '', "\n" => '', '"' => '' ) );
- return '"' . $phrase . '"';
- }
- }
- class EmailNotification {
- private $to, $subject, $body, $replyto, $from;
- private $user, $title, $timestamp, $summary, $minorEdit, $oldid, $composed_common, $editor;
- private $mailTargets = array();
-
- function notifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid = false) {
- global $wgEnotifUseJobQ, $wgEnotifWatchlist, $wgShowUpdatedMarker;
- if ($title->getNamespace() < 0)
- return;
-
- $watchers = array();
- if ($wgEnotifWatchlist || $wgShowUpdatedMarker) {
- $dbw = wfGetDB( DB_MASTER );
- $res = $dbw->select( array( 'watchlist' ),
- array( 'wl_user' ),
- array(
- 'wl_title' => $title->getDBkey(),
- 'wl_namespace' => $title->getNamespace(),
- 'wl_user != ' . intval( $editor->getID() ),
- 'wl_notificationtimestamp IS NULL',
- ), __METHOD__
- );
- while ($row = $dbw->fetchObject( $res ) ) {
- $watchers[] = intval( $row->wl_user );
- }
- if ($watchers) {
-
-
- $dbw->begin();
- $dbw->update( 'watchlist',
- array(
- 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
- ), array(
- 'wl_title' => $title->getDBkey(),
- 'wl_namespace' => $title->getNamespace(),
- 'wl_user' => $watchers
- ), __METHOD__
- );
- $dbw->commit();
- }
- }
- if ($wgEnotifUseJobQ) {
- $params = array(
- "editor" => $editor->getName(),
- "editorID" => $editor->getID(),
- "timestamp" => $timestamp,
- "summary" => $summary,
- "minorEdit" => $minorEdit,
- "oldid" => $oldid,
- "watchers" => $watchers);
- $job = new EnotifNotifyJob( $title, $params );
- $job->insert();
- } else {
- $this->actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers );
- }
- }
-
- function actuallyNotifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid, $watchers) {
-
- global $wgEnotifWatchlist;
- global $wgEnotifMinorEdits, $wgEnotifUserTalk;
- global $wgEnotifImpersonal;
- wfProfileIn( __METHOD__ );
-
-
-
- $isUserTalkPage = ($title->getNamespace() == NS_USER_TALK);
- $enotifusertalkpage = ($isUserTalkPage && $wgEnotifUserTalk);
- $enotifwatchlistpage = $wgEnotifWatchlist;
- $this->title = $title;
- $this->timestamp = $timestamp;
- $this->summary = $summary;
- $this->minorEdit = $minorEdit;
- $this->oldid = $oldid;
- $this->editor = $editor;
- $this->composed_common = false;
- $userTalkId = false;
- if ( !$minorEdit || ($wgEnotifMinorEdits && !$editor->isAllowed('nominornewtalk') ) ) {
- if ( $wgEnotifUserTalk && $isUserTalkPage ) {
- $targetUser = User::newFromName( $title->getText() );
- if ( !$targetUser || $targetUser->isAnon() ) {
- wfDebug( __METHOD__.": user talk page edited, but user does not exist\n" );
- } elseif ( $targetUser->getId() == $editor->getId() ) {
- wfDebug( __METHOD__.": user edited their own talk page, no notification sent\n" );
- } elseif( $targetUser->getOption( 'enotifusertalkpages' ) ) {
- if( $targetUser->isEmailConfirmed() ) {
- wfDebug( __METHOD__.": sending talk page update notification\n" );
- $this->compose( $targetUser );
- $userTalkId = $targetUser->getId();
- } else {
- wfDebug( __METHOD__.": talk page owner doesn't have validated email\n" );
- }
- } else {
- wfDebug( __METHOD__.": talk page owner doesn't want notifications\n" );
- }
- }
- if ( $wgEnotifWatchlist ) {
-
- $userArray = UserArray::newFromIDs( $watchers );
- foreach ( $userArray as $watchingUser ) {
- if ( $watchingUser->getOption( 'enotifwatchlistpages' ) &&
- ( !$minorEdit || $watchingUser->getOption('enotifminoredits') ) &&
- $watchingUser->isEmailConfirmed() &&
- $watchingUser->getID() != $userTalkId )
- {
- $this->compose( $watchingUser );
- }
- }
- }
- }
- global $wgUsersNotifiedOnAllChanges;
- foreach ( $wgUsersNotifiedOnAllChanges as $name ) {
- $user = User::newFromName( $name );
- $this->compose( $user );
- }
- $this->sendMails();
- wfProfileOut( __METHOD__ );
- }
-
- function composeCommonMailtext() {
- global $wgPasswordSender, $wgNoReplyAddress;
- global $wgEnotifFromEditor, $wgEnotifRevealEditorAddress;
- global $wgEnotifImpersonal, $wgEnotifUseRealName;
- $this->composed_common = true;
- $summary = ($this->summary == '') ? ' - ' : $this->summary;
- $medit = ($this->minorEdit) ? wfMsg( 'minoredit' ) : '';
-
-
-
- $subject = wfMsgForContent( 'enotif_subject' );
- $body = wfMsgForContent( 'enotif_body' );
- $from = '';
- $replyto = '';
- $keys = array();
- if( $this->oldid ) {
- $difflink = $this->title->getFullUrl( 'diff=0&oldid=' . $this->oldid );
- $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastvisited', $difflink );
- $keys['$OLDID'] = $this->oldid;
- $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'changed' );
- } else {
- $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_newpagetext' );
-
- $keys['$OLDID'] = '';
- $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'created' );
- }
- if ($wgEnotifImpersonal && $this->oldid)
-
- $keys['$NEWPAGE'] = wfMsgForContent('enotif_lastdiff',
- $this->title->getFullURL("oldid={$this->oldid}&diff=prev"));
- $body = strtr( $body, $keys );
- $pagetitle = $this->title->getPrefixedText();
- $keys['$PAGETITLE'] = $pagetitle;
- $keys['$PAGETITLE_URL'] = $this->title->getFullUrl();
- $keys['$PAGEMINOREDIT'] = $medit;
- $keys['$PAGESUMMARY'] = $summary;
- $subject = strtr( $subject, $keys );
-
-
-
- $editor = $this->editor;
- $name = $wgEnotifUseRealName ? $editor->getRealName() : $editor->getName();
- $adminAddress = new MailAddress( $wgPasswordSender, 'WikiAdmin' );
- $editorAddress = new MailAddress( $editor );
- if( $wgEnotifRevealEditorAddress
- && ( $editor->getEmail() != '' )
- && $editor->getOption( 'enotifrevealaddr' ) ) {
- if( $wgEnotifFromEditor ) {
- $from = $editorAddress;
- } else {
- $from = $adminAddress;
- $replyto = $editorAddress;
- }
- } else {
- $from = $adminAddress;
- $replyto = new MailAddress( $wgNoReplyAddress );
- }
- if( $editor->isIP( $name ) ) {
-
- $utext = wfMsgForContent('enotif_anon_editor', $name);
- $subject = str_replace('$PAGEEDITOR', $utext, $subject);
- $keys['$PAGEEDITOR'] = $utext;
- $keys['$PAGEEDITOR_EMAIL'] = wfMsgForContent( 'noemailtitle' );
- } else {
- $subject = str_replace('$PAGEEDITOR', $name, $subject);
- $keys['$PAGEEDITOR'] = $name;
- $emailPage = SpecialPage::getSafeTitleFor( 'Emailuser', $name );
- $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getFullUrl();
- }
- $userPage = $editor->getUserPage();
- $keys['$PAGEEDITOR_WIKI'] = $userPage->getFullUrl();
- $body = strtr( $body, $keys );
- $body = wordwrap( $body, 72 );
-
- $this->from = $from;
- $this->replyto = $replyto;
- $this->subject = $subject;
- $this->body = $body;
- }
-
- function compose( $user ) {
- global $wgEnotifImpersonal;
- if ( !$this->composed_common )
- $this->composeCommonMailtext();
- if ( $wgEnotifImpersonal ) {
- $this->mailTargets[] = new MailAddress( $user );
- } else {
- $this->sendPersonalised( $user );
- }
- }
-
- function sendMails() {
- global $wgEnotifImpersonal;
- if ( $wgEnotifImpersonal ) {
- $this->sendImpersonal( $this->mailTargets );
- }
- }
-
- function sendPersonalised( $watchingUser ) {
- global $wgContLang, $wgEnotifUseRealName;
-
-
-
- $to = new MailAddress( $watchingUser );
- $name = $wgEnotifUseRealName ? $watchingUser->getRealName() : $watchingUser->getName();
- $body = str_replace( '$WATCHINGUSERNAME', $name , $this->body );
- $timecorrection = $watchingUser->getOption( 'timecorrection' );
-
-
-
- $body = str_replace('$PAGEEDITDATE',
- $wgContLang->timeanddate( $this->timestamp, true, false, $timecorrection ),
- $body);
- return UserMailer::send($to, $this->from, $this->subject, $body, $this->replyto);
- }
-
- function sendImpersonal( $addresses ) {
- global $wgContLang;
- if (empty($addresses))
- return;
- $body = str_replace(
- array( '$WATCHINGUSERNAME',
- '$PAGEEDITDATE'),
- array( wfMsgForContent('enotif_impersonal_salutation'),
- $wgContLang->timeanddate($this->timestamp, true, false, false)),
- $this->body);
- return UserMailer::send($addresses, $this->from, $this->subject, $body, $this->replyto);
- }
- }
- function wfRFC822Phrase( $s ) {
- return UserMailer::rfc822Phrase( $s );
- }
- function userMailer( $to, $from, $subject, $body, $replyto=null ) {
- return UserMailer::send( $to, $from, $subject, $body, $replyto );
- }
|