Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 onUserMailerChangeReturnPath
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
 generateVerp
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 onBeforeCreateEchoEvent
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 onEchoGetDefaultNotifiedUsers
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace MediaWiki\Extension\BounceHandler;
3
4use EchoEvent;
5use InvalidArgumentException;
6use MailAddress;
7use MediaWiki\Hook\UserMailerChangeReturnPathHook;
8use MediaWiki\User\User;
9
10/**
11 * Hooks used by BounceHandler
12 *
13 * @file
14 * @ingroup Hooks
15 * @author Tony Thomas, Kunal Mehta, Jeff Green
16 * @license GPL-2.0-or-later
17 */
18class Hooks implements UserMailerChangeReturnPathHook {
19    /**
20     * This function generates the VERP address on UserMailer::send()
21     * Generating VERP address for a batch of send emails is complex. This feature is hence disabled
22     *
23     * @param MailAddress[] $recip Recipient's email array
24     * @param string &$returnPath return-path address
25     * @throws InvalidArgumentException
26     */
27    public function onUserMailerChangeReturnPath( $recip, &$returnPath ) {
28        global $wgGenerateVERP;
29        if ( $wgGenerateVERP && count( $recip ) === 1 ) {
30            self::generateVerp( $recip[0], $returnPath );
31        }
32    }
33
34    /**
35     * Process a given $to address and return its VERP return path
36     *
37     * @param MailAddress $to
38     * @param string &$returnPath return-path address
39     * @return bool true
40     */
41    protected static function generateVerp( MailAddress $to, &$returnPath ) {
42        global $wgVERPprefix, $wgVERPalgorithm, $wgVERPsecret, $wgVERPdomainPart, $wgServerName;
43        $user = User::newFromName( $to->name );
44        if ( !$user ) {
45            return true;
46        }
47        $email = $to->address;
48        if ( $user->getEmail() === $email && $user->isEmailConfirmed() ) {
49            $uid = $user->getId();
50        } else {
51            return true;
52        }
53        $domainPart = $wgVERPdomainPart ?? $wgServerName;
54        $verpAddress = new VerpAddressGenerator( $wgVERPprefix,
55            $wgVERPalgorithm, $wgVERPsecret, $domainPart );
56        $returnPath = $verpAddress->generateVERP( $uid );
57
58        return true;
59    }
60
61    /**
62     * Add BounceHandler events to Echo
63     *
64     * @param array &$notifications Echo notifications
65     * @return bool
66     */
67    public static function onBeforeCreateEchoEvent( array &$notifications ) {
68        $notifications['unsubscribe-bouncehandler'] = [
69            'presentation-model' => EchoBounceHandlerPresentationModel::class,
70            'primary-link' => [
71                'message' => 'notification-link-text-change-email',
72                'destination' => 'change-email'
73            ],
74            // We cannot have additional Echo emails being sent after a user is un-subscribed
75            'category' => 'system-noemail',
76            'section' => 'alert',
77
78            'title-message' => 'notification-bouncehandler',
79            'title-params' => [ 'user' ],
80            'flyout-message' => 'notification-bouncehandler-flyout',
81            'flyout-params' => [ 'failed-email', 'user' ],
82        ];
83
84        return true;
85    }
86
87    /**
88     * Add user to be notified on echo event
89     *
90     * @param EchoEvent $event
91     * @param User[] &$users
92     * @return bool
93     */
94    public static function onEchoGetDefaultNotifiedUsers( EchoEvent $event, array &$users ) {
95        if ( $event->getExtraParam( 'failed-user-id' ) === null ) {
96            return true;
97        }
98        $extra = $event->getExtra();
99        $eventType = $event->getType();
100        if ( $eventType === 'unsubscribe-bouncehandler' ) {
101            $recipientId = $extra['failed-user-id'];
102            $recipient = User::newFromId( $recipientId );
103            $users[$recipientId] = $recipient;
104        }
105
106        return true;
107    }
108
109}