summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/NewUserNotif/NewUserNotif.class.php
blob: 4cfabe5826b8d2dedcd76095d2f647986c0e53b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
	}
}