forked from wikimedia/mediawiki-extensions-BounceHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BounceHandlerHooks.php
93 lines (86 loc) · 2.64 KB
/
BounceHandlerHooks.php
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
<?php
/** Hooks used by BounceHandler
*/
class BounceHandlerHooks {
/**
* This function generates the VERP address on UserMailer::send()
* @param array $recip recipients array
* @param string returnPath return-path address
* @return bool true
*/
public static function onVERPAddressGenerate( $recip, &$returnPath ) {
$user = User::newFromName( $recip[0]->name );
if ( !$user ) {
return true;
}
$email = $recip[0]->address;
if ( $user->getEmail() === $email && $user->isEmailConfirmed() ) {
$uid = $user->getId();
} else {
return true;
}
$returnPath = self::generateVERP( $uid );
return true;
}
/**
* Generate VERP address of the form
*
* @param string recipient email
* @return string ReturnPath address
*/
protected static function generateVERP( $uid ) {
global $wgVERPalgorithm, $wgVERPsecret, $wgServer, $wgSMTP;
// Get the time in Unix timestamp to compare with seconds
$timeNow = wfTimestamp();
if( is_array( $wgSMTP ) && isset( $wgSMTP['IDHost'] ) && $wgSMTP['IDHost'] ) {
$email_domain = $wgSMTP['IDHost'];
} else {
$url = wfParseUrl( $wgServer );
$email_domain = $url['host'];
}
// Creating the VERP address prefix as wikiId-base36( $UserID )-base36( $Timestamp )
// and the generated VERP return path is of the form :
// wikiId-base36( $UserID )-base36( $Timestamp )-hash( $algorithm, $key, $prefix )@$email_domain
// We dont want repeating '-' in our WikiId
$wikiId = str_replace( '-', '.', wfWikiID() );
$email_prefix = $wikiId. '-'. base_convert( $uid, 10, 36). '-'. base_convert( $timeNow, 10, 36);
$verp_hash = hash_hmac( $wgVERPalgorithm, $email_prefix, $wgVERPsecret );
$returnPath = $email_prefix. '-' .$verp_hash. '@' .$email_domain;
return $returnPath;
}
/**
* Hook to add PHPUnit test cases.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
*
* @param array &$files
*
* @return boolean
*/
public static function registerUnitTests( array &$files ) {
// @codeCoverageIgnoreStart
$directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests/' );
/**
* @var SplFileInfo $fileInfo
*/
$ourFiles = array();
foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) {
$ourFiles[] = $fileInfo->getPathname();
}
}
$files = array_merge( $files, $ourFiles );
return true;
// @codeCoverageIgnoreEnd
}
/**
*
* Add tables to Database
*/
public static function addBounceRecordsTable( DatabaseUpdater $updater ) {
$updater->addExtensionTable(
'bounce_records',
__DIR__. '/sql/bounce_records.sql', true
);
return true;
}
}