diff options
Diffstat (limited to 'www/wiki/extensions/NewUserNotif/NewUserNotif.class.php')
-rw-r--r-- | www/wiki/extensions/NewUserNotif/NewUserNotif.class.php | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/www/wiki/extensions/NewUserNotif/NewUserNotif.class.php b/www/wiki/extensions/NewUserNotif/NewUserNotif.class.php new file mode 100644 index 00000000..4cfabe58 --- /dev/null +++ b/www/wiki/extensions/NewUserNotif/NewUserNotif.class.php @@ -0,0 +1,134 @@ +<?php + +/** + * Extension to provide customisable email notification of new user creation + * + * @file + * @ingroup Extensions + * @author Rob Church <robchur@gmail.com> + */ + +class NewUserNotifier { + + private $sender; + private $user; + + /** + * Constructor + */ + public function NewUserNotifier() { + global $wgNewUserNotifSender; + $this->sender = $wgNewUserNotifSender; + } + + /** + * Send all email notifications + * + * @param User $user User that was created + */ + public function execute( $user ) { + $this->user = $user; + $this->sendExternalMails(); + $this->sendInternalMails(); + } + + /** + * Send email to external addresses + */ + private function sendExternalMails() { + global $wgNewUserNotifEmailTargets, $wgSitename; + foreach( $wgNewUserNotifEmailTargets as $target ) { + UserMailer::send( + new MailAddress( $target ), + new MailAddress( $this->sender ), + $this->makeSubject( $target, $this->user ), + $this->makeMessage( $target, $this->user ) + ); + } + } + + /** + * Send email to users + */ + private function sendInternalMails() { + global $wgNewUserNotifTargets, $wgSitename; + foreach( $wgNewUserNotifTargets as $userSpec ) { + $user = $this->makeUser( $userSpec ); + if( $user instanceof User && $user->isEmailConfirmed() ) { + $user->sendMail( + $this->makeSubject( $user->getName(), $this->user ), + $this->makeMessage( $user->getName(), $this->user ), + $this->sender + ); + } + } + } + + /** + * Initialise a user from an identifier or a username + * + * @param mixed $spec User identifier or name + * @return User + */ + private function makeUser( $spec ) { + $name = is_integer( $spec ) ? User::whoIs( $spec ) : $spec; + $user = User::newFromName( $name ); + if( $user instanceof User && $user->getId() > 0 ) + return $user; + return null; + } + + /** + * Build a notification email subject line + * + * @param string $recipient Name of the recipient + * @param User $user User that was created + */ + private function makeSubject( $recipient, $user ) { + global $wgSitename; + $subjectLine = ""; + // Avoid PHP 7.1 warning of passing $this by reference + $userNotif = $this; + Hooks::run( 'NewUserNotifSubject', array( &$userNotif, &$subjectLine, $wgSitename, $recipient, $user ) ); + if (!strlen($subjectLine) ) + return wfMessage( 'newusernotifsubj', $wgSitename )->inContentLanguage()->text(); + return $subjectLine; + } + + /** + * Build a notification email message body + * + * @param string $recipient Name of the recipient + * @param User $user User that was created + */ + private function makeMessage( $recipient, $user ) { + global $wgSitename, $wgContLang; + $messageBody = ""; + // Avoid PHP 7.1 warning of passing $this by reference + $userNotif = $this; + Hooks::run( 'NewUserNotifBody', array( &$userNotif, &$messageBody, $wgSitename, $recipient, $user ) ); + if (!strlen($messageBody) ) + return wfMessage( + 'newusernotifbody', + $recipient, + $user->getName(), + $wgSitename, + $wgContLang->timeAndDate( wfTimestampNow() ), + $wgContLang->date( wfTimestampNow() ), + $wgContLang->time( wfTimestampNow() ) + )->inContentLanguage()->text(); + return $messageBody; + } + + /** + * Hook account creation + * + * @param User $user User that was created + * @return bool + */ + public static function hook( $user ) { + $notifier = new self(); + $notifier->execute( $user ); + return true; + } +} |