-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPGMailHooks.php
235 lines (216 loc) · 6.66 KB
/
GPGMailHooks.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Hooks for GPGMail extension
*
* @file
* @ingroup Extensions
*/
use GpgLib\GpgLib;
use GpgLib\GpgLibException;
use GpgLib\PgpMime;
use GpgLib\ShellGpgLibFactory;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
/**
* Hooks for GPGMail extension
*/
class GPGMailHooks {
/**
* Integrate Composer autoloader with MediaWiki autoloading.
*/
public static function registerExtension() {
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
}
/**
* Add GPG checkbox + key textbox to user preferences.
* @param User $user
* @param array &$preferences
* @return bool
*/
public static function onGetPreferences( User $user, array &$preferences ) {
$preferences['gpgmail-enable'] = [
'type' => 'toggle',
'label-message' => 'gpgmail-pref-enable',
'section' => 'personal/email',
];
$preferences['gpgmail-key'] = [
'type' => 'textarea',
'label-message' => 'gpgmail-pref-key',
'help-message' => 'gpgmail-pref-key-help',
'section' => 'personal/email',
'validation-callback' => 'GPGMailHooks::validateKey',
'hide-if' => [ '===', 'gpgmail-enable', '' ],
];
return true;
}
/**
* Prevent bulk mailing users who requested encryption
* @param MailAddress[] &$to
* @return bool
*/
public static function onUserMailerSplitTo( &$to ) {
foreach ( $to as $i => $singleTo ) {
if ( !$singleTo instanceof MailAddress ) {
self::getLogger()->warning( 'invalid address: {to} ', [ 'to' => $singleTo ] );
continue;
}
$user = User::newFromName( $singleTo->name );
if ( MediaWikiServices::getInstance()->getUserOptionsLookup()
->getBoolOption( $user, 'gpgmail-enable' )
) {
unset( $to[$i] );
}
}
return true;
}
/**
* @param MailAddress[] $to
* @param MailAddress $from
* @param string|array &$body Email plaintext or an array with 'text' and 'html' keys
* @param Message|string &$error Error message when encryption fails
* @return bool
*/
public static function onUserMailerTransformContent( array $to, $from, &$body, &$error ) {
if ( count( $to ) > 1 ) {
// users who requested encryption have been split out already by UserMailerSplitTo
return true;
}
$user = User::newFromName( $to[0]->name );
if ( is_array( $body ) ) {
$status = Status::newGood();
foreach ( $body as &$content ) {
$status->merge( self::maybeEncrypt( $content, $user ) );
}
} else {
$status = self::maybeEncrypt( $body, $user );
}
if ( !$status->isOK() ) {
// if the user is emailing themselves, show a descriptive error message
// otherwise don't reveal to the sender that they are encrypting their email
if ( $to[0]->address === $from->address ) {
$error = $status->getWikiText();
}
return false;
}
return true;
}
/**
* @param MailAddress[] $to
* @param MailAddress $from
* @param string &$subject Email subject (not MIME encoded)
* @param array &$headers Email headers
* @param string &$body Email body (MIME-encoded)
* @param Message|string &$error Error message when encryption fails
* @return bool
*/
public static function onUserMailerTransformMessage( $to, $from,
&$subject, &$headers, &$body, &$error
) {
if ( count( $to ) > 1 ) {
// users who requested encryption have been split out already by UserMailerSplitTo
return true;
}
$user = User::newFromName( $to[0]->name );
$status = self::maybeEncryptMime( $headers, $body, $user );
if ( !$status->isOK() ) {
// if the user is emailing themselves, show a descriptive error message
// otherwise don't reveal to the sender that they are encrypting their email
if ( $to[0]->address === $from->address ) {
$error = $status->getWikiText();
}
return false;
}
return true;
}
/**
* HTMLForm validation callback that checks whether the value is a proper GPG public key.
* @param string $value The text of the key
* @param array $alldata Data from all form fields
* @param HTMLForm $form
* @return bool
*/
public static function validateKey( $value, $alldata, $form ) {
if ( !$alldata['gpgmail-enable'] ) {
return true;
}
$gpgLib = self::getGPGLib();
return $gpgLib->validateKey( $value, GpgLib::KEY_PUBLIC ) ? true : 'Invalid GPG public key';
}
/**
* Encrypts the message if the target user asked for that.
* @param string &$text Text of the message
* @param User $user User to whom the message will be sent
* @return Status Success or an error message
*/
protected static function maybeEncrypt( &$text, $user ) {
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
if ( $userOptionsLookup->getBoolOption( $user, 'gpgmail-enable' ) && !self::usePgpMime() ) {
try {
$text = self::getGPGLib()->encrypt(
$text,
$userOptionsLookup->getOption( $user, 'gpgmail-key' )
);
} catch ( GpgLibException $e ) {
return Status::newFatal( new RawMessage( $e->getMessage() ) );
}
if ( !$text ) {
return Status::newFatal( 'gpgmail-encrypt-error' );
}
}
return Status::newGood();
}
/**
* @param array &$headers Email headers
* @param string &$body Email body (MIME-encoded)
* @param User $user User to whom the message will be sent
* @return Status Success or an error message
*/
protected static function maybeEncryptMime( &$headers, &$body, $user ) {
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
if ( $userOptionsLookup->getBoolOption( $user, 'gpgmail-enable' ) && self::usePgpMime() ) {
try {
$pgpMime = new PgpMime( self::getGPGLib() );
[ $headers, $body ] = $pgpMime->encrypt(
$headers, $body, $userOptionsLookup->getOption( $user, 'gpgmail-key' ) );
} catch ( GpgLibException $e ) {
return Status::newFatal( new RawMessage( $e->getMessage() ) );
}
if ( !$body ) {
return Status::newFatal( 'gpgmail-encrypt-error' );
}
}
return Status::newGood();
}
/**
* When true, PGP/MIME (RFC 3156) should be used, otherwise inline encryption.
* @return bool
*/
protected static function usePgpMime() {
global $wgGpgMailUsePgpMime;
return $wgGpgMailUsePgpMime;
}
/**
* @return GpgLib
*/
protected static function getGPGLib() {
global $wgGPGMailBinary, $wgGPGMailTempDir;
$gpgLibFactory = new ShellGpgLibFactory();
if ( $wgGPGMailBinary ) {
$gpgLibFactory->setGpgBinary( $wgGPGMailBinary );
}
if ( $wgGPGMailTempDir ) {
$gpgLibFactory->setTempDirFactory( new \GpgLib\TempDirFactory( $wgGPGMailTempDir ) );
}
$gpgLibFactory->setLogger( self::getLogger() );
return $gpgLibFactory->create();
}
/**
* @return LoggerInterface
*/
protected static function getLogger() {
return LoggerFactory::getInstance( 'gpglib' );
}
}