Failed Conditions
Branch newinternal (3df7fe)
by Simon
04:13
created

includes/Helpers/IrcNotificationHelper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Exception;
12
use Waca\DataObjects\Ban;
13
use Waca\DataObjects\Comment;
14
use Waca\DataObjects\EmailTemplate;
15
use Waca\DataObjects\Notification;
16
use Waca\DataObjects\Request;
17
use Waca\DataObjects\User;
18
use Waca\DataObjects\WelcomeTemplate;
19
use Waca\IrcColourCode;
20
use Waca\PdoDatabase;
21
use Waca\SiteConfiguration;
22
23
/**
24
 * Class IrcNotificationHelper
25
 * @package Waca\Helpers
26
 */
27
class IrcNotificationHelper
28
{
29
	/** @var PdoDatabase $notificationsDatabase */
30
	private $notificationsDatabase;
31
	/** @var PdoDatabase $primaryDatabase */
32
	private $primaryDatabase;
33
	/** @var bool $notificationsEnabled */
34
	private $notificationsEnabled;
35
	/** @var int $notificationType */
36
	private $notificationType;
37
	/** @var User $currentUser */
38
	private $currentUser;
39
	/** @var string $instanceName */
40
	private $instanceName;
41
	/** @var string */
42
	private $baseUrl;
43
	/** @var array */
44
	private $requestStates;
45
46
	/**
47
	 * IrcNotificationHelper constructor.
48
	 *
49
	 * @param SiteConfiguration $siteConfiguration
50
	 * @param PdoDatabase       $primaryDatabase
51
	 * @param PdoDatabase       $notificationsDatabase
52
	 */
53
	public function __construct(
54
		SiteConfiguration $siteConfiguration,
55
		PdoDatabase $primaryDatabase,
56
		PdoDatabase $notificationsDatabase = null
57
	) {
58
		$this->primaryDatabase = $primaryDatabase;
59
60
		if(true){
0 ignored issues
show
Avoid IF statements that are always true or false
Loading history...
61
			$this->notificationsDatabase = $notificationsDatabase;
62
			$this->notificationsEnabled = $siteConfiguration->getIrcNotificationsEnabled();
63
		}
64
		else{
65
			$this->notificationsEnabled = false;
66
		}
67
68
		$this->notificationType = $siteConfiguration->getIrcNotificationType();
69
		$this->instanceName = $siteConfiguration->getIrcNotificationsInstance();
70
		$this->baseUrl = $siteConfiguration->getBaseUrl();
71
		$this->requestStates = $siteConfiguration->getRequestStates();
72
73
		$this->currentUser = User::getCurrent($primaryDatabase);
74
	}
75
76
	/**
77
	 * Send a notification
78
	 *
79
	 * @param string $message The text to send
80
	 */
81
	protected function send($message)
82
	{
83
		$instanceName = $this->instanceName;
84
85
		if (!$this->notificationsEnabled) {
86
			return;
87
		}
88
89
		$blacklist = array("DCC", "CCTP", "PRIVMSG");
90
		$message = str_replace($blacklist, "(IRC Blacklist)", $message); // Lets stop DCC etc
91
92
		$msg = IrcColourCode::RESET . IrcColourCode::BOLD . "[$instanceName]" . IrcColourCode::RESET . ": $message";
93
94
		try {
95
			$notification = new Notification();
96
			$notification->setDatabase($this->notificationsDatabase);
97
			$notification->setType($this->notificationType);
98
			$notification->setText($msg);
99
100
			$notification->save();
101
		}
102
		catch (Exception $ex) {
103
			// OK, so we failed to send the notification - that db might be down?
104
			// This is non-critical, so silently fail.
105
106
			// Disable notifications for remainder of request.
107
			$this->notificationsEnabled = false;
108
		}
109
	}
110
111
	#region user management
112
113
	/**
114
	 * send a new user notification
115
	 *
116
	 * @param User $user
117
	 */
118
	public function userNew(User $user)
119
	{
120
		$this->send("New user: {$user->getUsername()}");
121
	}
122
123
	/**
124
	 * send an approved notification
125
	 *
126
	 * @param User $user
127
	 */
128
	public function userApproved(User $user)
129
	{
130
		$this->send("{$user->getUsername()} approved by " . $this->currentUser->getUsername());
131
	}
132
133
	/**
134
	 * send a promoted notification
135
	 *
136
	 * @param User $user
137
	 */
138
	public function userPromoted(User $user)
139
	{
140
		$this->send("{$user->getUsername()} promoted to tool admin by " . $this->currentUser->getUsername());
141
	}
142
143
	/**
144
	 * send a declined notification
145
	 *
146
	 * @param User   $user
147
	 * @param string $reason the reason the user was declined
148
	 */
149
	public function userDeclined(User $user, $reason)
150
	{
151
		$this->send("{$user->getUsername()} declined by " . $this->currentUser->getUsername() . " ($reason)");
152
	}
153
154
	/**
155
	 * send a demotion notification
156
	 *
157
	 * @param User   $user
158
	 * @param string $reason the reason the user was demoted
159
	 */
160
	public function userDemoted(User $user, $reason)
161
	{
162
		$this->send("{$user->getUsername()} demoted by " . $this->currentUser->getUsername() . " ($reason)");
163
	}
164
165
	/**
166
	 * send a suspended notification
167
	 *
168
	 * @param User   $user
169
	 * @param string $reason The reason the user has been suspended
170
	 */
171
	public function userSuspended(User $user, $reason)
172
	{
173
		$this->send("{$user->getUsername()} suspended by " . $this->currentUser->getUsername() . " ($reason)");
174
	}
175
176
	/**
177
	 * Send a preference change notification
178
	 *
179
	 * @param User $user
180
	 */
181
	public function userPrefChange(User $user)
182
	{
183
		$this->send("{$user->getUsername()}'s preferences were changed by " . $this->currentUser->getUsername());
184
	}
185
186
	/**
187
	 * Send a user renamed notification
188
	 *
189
	 * @param User   $user
190
	 * @param string $old
191
	 */
192
	public function userRenamed(User $user, $old)
193
	{
194
		$this->send($this->currentUser->getUsername() . " renamed $old to {$user->getUsername()}");
195
	}
196
197
	#endregion
198
199
	#region Site Notice
200
201
	/**
202
	 * Summary of siteNoticeEdited
203
	 */
204
	public function siteNoticeEdited()
205
	{
206
		$this->send("Site notice edited by " . $this->currentUser->getUsername());
207
	}
208
	#endregion
209
210
	#region Welcome Templates
211
	/**
212
	 * Summary of welcomeTemplateCreated
213
	 *
214
	 * @param WelcomeTemplate $template
215
	 */
216
	public function welcomeTemplateCreated(WelcomeTemplate $template)
217
	{
218
		$this->send("Welcome template {$template->getId()} created by " . $this->currentUser->getUsername());
219
	}
220
221
	/**
222
	 * Summary of welcomeTemplateDeleted
223
	 *
224
	 * @param int $templateid
225
	 */
226
	public function welcomeTemplateDeleted($templateid)
227
	{
228
		$this->send("Welcome template {$templateid} deleted by " . $this->currentUser->getUsername());
229
	}
230
231
	/**
232
	 * Summary of welcomeTemplateEdited
233
	 *
234
	 * @param WelcomeTemplate $template
235
	 */
236
	public function welcomeTemplateEdited(WelcomeTemplate $template)
237
	{
238
		$this->send("Welcome template {$template->getId()} edited by " . $this->currentUser->getUsername());
239
	}
240
241
	#endregion
242
243
	#region bans
244
	/**
245
	 * Summary of banned
246
	 *
247
	 * @param Ban $ban
248
	 */
249
	public function banned(Ban $ban)
250
	{
251
		if ($ban->getDuration() == -1) {
252
			$duration = "indefinitely";
253
		}
254
		else {
255
			$duration = "until " . date("F j, Y, g:i a", $ban->getDuration());
256
		}
257
258
		$username = $this->currentUser->getUsername();
259
260
		$this->send("{$ban->getTarget()} banned by {$username} for '{$ban->getReason()}' {$duration}");
261
	}
262
263
	/**
264
	 * Summary of unbanned
265
	 *
266
	 * @param Ban    $ban
267
	 * @param string $unbanreason
268
	 */
269
	public function unbanned(Ban $ban, $unbanreason)
270
	{
271
		$this->send($ban->getTarget() . " unbanned by " . $this->currentUser
272
				->getUsername() . " (" . $unbanreason . ")");
273
	}
274
275
	#endregion
276
277
	#region request management
278
279
	/**
280
	 * Summary of requestReceived
281
	 *
282
	 * @param Request $request
283
	 */
284
	public function requestReceived(Request $request)
285
	{
286
		$this->send(
287
			IrcColourCode::DARK_GREY . "[["
288
			. IrcColourCode::DARK_GREEN . "acc:"
289
			. IrcColourCode::ORANGE . $request->getId()
290
			. IrcColourCode::DARK_GREY . "]]"
291
			. IrcColourCode::RED . " N "
292
			. IrcColourCode::DARK_BLUE . $this->baseUrl . "/internal.php/viewRequest?id={$request->getId()} "
293
			. IrcColourCode::DARK_RED . "* "
294
			. IrcColourCode::DARK_GREEN . $request->getName()
295
			. IrcColourCode::DARK_RED . " * "
296
			. IrcColourCode::RESET
297
		);
298
	}
299
300
	/**
301
	 * Summary of requestDeferred
302
	 *
303
	 * @param Request $request
304
	 */
305
	public function requestDeferred(Request $request)
306
	{
307
		$availableRequestStates = $this->requestStates;
308
309
		$deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
310
		$username = $this->currentUser->getUsername();
311
312
		$this->send("Request {$request->getId()} ({$request->getName()}) deferred to {$deferTo} by {$username}");
313
	}
314
315
	/**
316
	 *
317
	 * Summary of requestDeferredWithMail
318
	 *
319
	 * @param Request $request
320
	 */
321
	public function requestDeferredWithMail(Request $request)
322
	{
323
		$availableRequestStates = $this->requestStates;
324
325
		$deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
326
		$username = $this->currentUser->getUsername();
327
		$id = $request->getId();
328
		$name = $request->getName();
329
330
		$this->send("Request {$id} ({$name}) deferred to {$deferTo} with an email by {$username}");
331
	}
332
333
	/**
334
	 * Summary of requestClosed
335
	 *
336
	 * @param Request $request
337
	 * @param string  $closetype
338
	 */
339
	public function requestClosed(Request $request, $closetype)
340
	{
341
		$username = $this->currentUser->getUsername();
342
343
		$this->send("Request {$request->getId()} ({$request->getName()}) closed ($closetype) by {$username}");
344
	}
345
346
	/**
347
	 * Summary of sentMail
348
	 *
349
	 * @param Request $request
350
	 */
351
	public function sentMail(Request $request)
352
	{
353
		$this->send($this->currentUser->getUsername()
354
			. " sent an email related to Request {$request->getId()} ({$request->getName()})");
355
	}
356
357
	#endregion
358
359
	#region reservations
360
361
	/**
362
	 * Summary of requestReserved
363
	 *
364
	 * @param Request $request
365
	 */
366
	public function requestReserved(Request $request)
367
	{
368
		$username = $this->currentUser->getUsername();
369
370
		$this->send("Request {$request->getId()} ({$request->getName()}) reserved by {$username}");
371
	}
372
373
	/**
374
	 * Summary of requestReserveBroken
375
	 *
376
	 * @param Request $request
377
	 */
378
	public function requestReserveBroken(Request $request)
379
	{
380
		$username = $this->currentUser->getUsername();
381
382
		$this->send("Reservation on request {$request->getId()} ({$request->getName()}) broken by {$username}");
383
	}
384
385
	/**
386
	 * Summary of requestUnreserved
387
	 *
388
	 * @param Request $request
389
	 */
390
	public function requestUnreserved(Request $request)
391
	{
392
		$this->send("Request {$request->getId()} ({$request->getName()}) is no longer being handled.");
393
	}
394
395
	/**
396
	 * Summary of requestReservationSent
397
	 *
398
	 * @param Request $request
399
	 * @param User    $target
400
	 */
401
	public function requestReservationSent(Request $request, User $target)
402
	{
403
		$username = $this->currentUser->getUsername();
404
405
		$this->send(
406
			"Reservation of request {$request->getId()} ({$request->getName()}) sent to {$target->getUsername()} by "
407
			. $username);
408
	}
409
410
	#endregion
411
412
	#region comments
413
414
	/**
415
	 * Summary of commentCreated
416
	 *
417
	 * @param Comment $comment
418
	 * @param Request $request
419
	 */
420
	public function commentCreated(Comment $comment, Request $request)
421
	{
422
		$username = $this->currentUser->getUsername();
423
		$visibility = ($comment->getVisibility() == "admin" ? "private " : "");
424
425
		$this->send("{$username} posted a {$visibility}comment on request {$request->getId()} ({$request->getName()})");
426
	}
427
428
	/**
429
	 * Summary of commentEdited
430
	 *
431
	 * @param Comment $comment
432
	 * @param Request $request
433
	 */
434
	public function commentEdited(Comment $comment, Request $request)
435
	{
436
		$username = $this->currentUser->getUsername();
437
438
		$this->send(<<<TAG
439
Comment {$comment->getId()} on request {$request->getId()} ({$request->getName()}) edited by {$username}
440
TAG
441
		);
442
	}
443
444
	#endregion
445
446
	#region email management (close reasons)
447
448
	/**
449
	 * Summary of emailCreated
450
	 *
451
	 * @param EmailTemplate $template
452
	 */
453
	public function emailCreated(EmailTemplate $template)
454
	{
455
		$username = $this->currentUser->getUsername();
456
		$this->send("Email {$template->getId()} ({$template->getName()}) created by " . $username);
457
	}
458
459
	/**
460
	 * Summary of emailEdited
461
	 *
462
	 * @param EmailTemplate $template
463
	 */
464
	public function emailEdited(EmailTemplate $template)
465
	{
466
		$username = $this->currentUser->getUsername();
467
		$this->send("Email {$template->getId()} ({$template->getName()}) edited by " . $username);
468
	}
469
	#endregion
470
}