-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathmodules.php
More file actions
400 lines (365 loc) · 15.6 KB
/
modules.php
File metadata and controls
400 lines (365 loc) · 15.6 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* PGP modules
* @package modules
* @subpackage pgp
*/
if (!defined('DEBUG_MODE')) { die(); }
require_once APP_PATH.'modules/profiles/hm-profiles.php';
/**
* @subpackage pgp/handler
*/
class Hm_Handler_load_pgp_data extends Hm_Handler_Module {
public function process() {
$this->out('pgp_public_keys', $this->user_config->get('pgp_public_keys', array()));
$this->out('autocrypt_keys', $this->user_config->get('autocrypt_keys', array()));
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_ajax_encrypt_by_fingerprint extends Hm_Handler_Module
{
public function process()
{
list($success, $form) = $this->process_form(array('body', 'fingerprint', 'from'));
$tmp_dir = ini_get('keyring_dir') ? ini_get('keyring_dir') : '/keyring';
putenv(sprintf('GNUPGHOME=%s/.gnupg', $tmp_dir));
$gpg = gnupg_init();
gnupg_setarmor($gpg,1);
gnupg_addencryptkey($gpg ,$form['fingerprint']);
$from_exploded = explode('.', $form['from']);
$from = reset($from_exploded);
$info = gnupg_keyinfo($gpg, '');
foreach ($this->user_config->get('autocrypt_keys', array()) as $key) {
if ($key['email'] == $this->user_config->get('smtp_servers')[$from]['user']) {
gnupg_addsignkey($gpg, $key['key_fingerprint']);
gnupg_addencryptkey($gpg ,$key['key_fingerprint']);
$encrypted_msg = gnupg_sign($gpg, $form['body']);
$encrypted_msg = gnupg_encrypt($gpg, $encrypted_msg);
}
}
$this->out('encrypted_message', $encrypted_msg);
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_ajax_public_key_import_string extends Hm_Handler_Module {
public function process() {
list($success, $form) = $this->process_form(array('public_key', 'public_key_email', 'autocrypt'));
$gpg = new gnupg();
$data = base64_decode($form['public_key']);
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : '/keyring';
putenv(sprintf('GNUPGHOME=%s/.gnupg', $tmp_dir));
$info = $gpg->import($data);
if (is_array($info) && array_key_exists('fingerprint', $info)) {
$fingerprint = $info['fingerprint'];
}
if (!$info) {
Hm_Msgs::add('ERRUnable to import public key');
return;
}
$keys = $this->user_config->get('pgp_public_keys', array());
$key_exists = false;
$new_key = array('fingerprint' => $fingerprint, 'key' => $data, 'autocrypt' => $form['autocrypt'], 'email' => $form['public_key_email']);
foreach ($keys as $key => $values) {
if ($values['email'] == $form['public_key_email']) {
$key_exists = true;
$keys[$key] = $new_key;
}
}
if ($key_exists == false) {
$keys[] = $new_key;
}
$this->session->set('pgp_public_keys', $keys, true);
$this->session->record_unsaved('Public key imported');
Hm_Msgs::add('Public key imported');
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_pgp_delete_public_key extends Hm_Handler_Module {
public function process() {
list($success, $form) = $this->process_form(array('delete_public_key_id'));
if (!$success) {
return;
}
$keys = $this->user_config->get('pgp_public_keys', array());
if (!array_key_exists($form['delete_public_key_id'], $keys)) {
Hm_Msgs::add('ERRCould not find public key to remove');
return;
}
unset($keys[$form['delete_public_key_id']]);
$this->session->set('pgp_public_keys', $keys, true);
$this->session->record_unsaved('Public key deleted');
Hm_Msgs::add('Public key deleted');
}
}
class Hm_Handler_ajax_generate_autocrypt_keys extends Hm_Handler_Module {
public function process() {
$tmp_dir = ini_get('keyring_dir') ? ini_get('keyring_dir') : '/keyring';
putenv(sprintf('GNUPGHOME=%s/.gnupg', $tmp_dir));
$profiles = new Hm_Profiles($this);
$autocrypt_keys = [];
$gpg = gnupg_init();
$saved_keys = gnupg_keyinfo($gpg, '');
foreach ($profiles->list_all() as $profile) {
$key_exists = false;
foreach ($saved_keys as $saved_key) {
if($saved_key['uids'][0]['email'] == $profile['user']) {
$key_exists = $saved_key['subkeys'][0]['fingerprint'];
}
}
if ($key_exists) {
$autocrypt_keys[] = [
'email' => $profile['address'],
'key_fingerprint' => $key_exists
];
continue;
}
$contents = "%no-protection\nKey-Type: 1\nKey-Length: 3072\nSubkey-Type: 1\nSubkey-Length: 2048\nName-Real: Autocrypt\nName-Email: ". $profile['address'] ."\nExpire-Date: 0\n";
file_put_contents('.temp_key_info', $contents);
system("gpg --batch --gen-key .temp_key_info");
$saved_keys = gnupg_keyinfo($gpg, '');
foreach ($saved_keys as $saved_key) {
if($saved_key['uids'][0]['email'] == $profile['user']) {
$autocrypt_keys[] = [
'email' => $profile['address'],
'key_fingerprint' => $saved_key['subkeys'][0]['fingerprint']
];
}
}
}
$this->session->set('autocrypt_keys', $autocrypt_keys, true);
$this->session->record_unsaved('Autocrypt keys generated');
Hm_Msgs::add('Keys Generated');
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_pgp_import_public_key extends Hm_Handler_Module {
public function process() {
list($success, $form) = $this->process_form(array('public_key_email'));
if (!$success) {
return;
}
if (!is_array($this->request->files) || !array_key_exists('public_key', $this->request->files)) {
return;
}
if (!is_array($this->request->files['public_key']) || !array_key_exists('tmp_name', $this->request->files['public_key'])) {
return;
}
$fingerprint = validate_public_key($this->request->files['public_key']['tmp_name']);
if (!$fingerprint) {
Hm_Msgs::add('ERRUnable to import public key');
return;
}
$keys = $this->user_config->get('pgp_public_keys', array());
$keys[] = array('fingerprint' => $fingerprint, 'key' => file_get_contents($this->request->files['public_key']['tmp_name']), 'email' => $form['public_key_email']);
$this->session->set('pgp_public_keys', $keys, true);
$this->session->record_unsaved('Public key imported');
Hm_Msgs::add('Public key imported');
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_pgp_compose_data extends Hm_Handler_Module {
public function process() {
$this->out('html_mail', $this->user_config->get('smtp_compose_type_setting', 0));
$this->out('pgp_public_keys', $this->user_config->get('pgp_public_keys', array()));
}
}
/**
* @subpackage pgp/handler
*/
class Hm_Handler_pgp_message_check extends Hm_Handler_Module {
public function process() {
/* TODO: Check for pgp parts, look at current part for pgp lines */
$pgp = false;
$struct = $this->get('msg_struct', array());
$text = $this->get('msg_text');
if (strpos($text, '----BEGIN PGP MESSAGE-----') !== false) {
$pgp = true;
}
$part_struct = $this->get('msg_struct_current', array());
if (is_array($part_struct) && array_key_exists('type', $part_struct) &&
$part_struct['type'] == 'application' && $part_struct['subtype'] == 'pgp-encrypted') {
$pgp = true;
}
$this->out('pgp_msg_part', $pgp);
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_compose_controls extends Hm_Output_Module {
protected function output() {
if ($this->get('html_mail', 0)) {
return;
}
$pub_keys = $this->get('pgp_public_keys', array());
$res = '<script type="text/javascript" src="'.WEB_ROOT.'modules/pgp/assets/openpgp.min.js"></script>';
$res .= '<div class="pgp_section">';
$res .= '<span class="pgp_sign"><label for="pgp_sign">'.$this->trans('PGP Sign as').'</label>';
$res .= '<select id="pgp_sign" size="1"></select></span>';
if (count($pub_keys) > 0) {
$res .= '<label for="pgp_encrypt">'.$this->trans('PGP Encrypt for').
'</label><select id="pgp_encrypt" size="1"><option disabled selected value=""></option>';
foreach ($pub_keys as $vals) {
$res .= '<option value="'.$vals['fingerprint'].'">'.$vals['email'].'</option>';
}
$res .= '</select>';
}
$res .= '<input type="button" class="pgp_apply" value="'.$this->trans('Apply').'" /></div>'.prompt_for_passhrase($this);
return $res;
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_start extends Hm_Output_Module {
protected function output() {
$res = '<div class="pgp_settings"><div class="content_title">'.$this->trans('PGP Settings').'</div>';
$res .= '<script type="text/javascript" src="'.WEB_ROOT.'modules/pgp/assets/openpgp.min.js"></script>';
return $res;
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_public_keys extends Hm_Output_Module {
protected function output() {
$res = '<div class="public_title settings_subtitle">'.$this->trans('Public Keys');
$res .= '<span class="key_count">'.sprintf($this->trans('%s imported'), count($this->get('pgp_public_keys', array()))).'</span></div>';
$res .= '<div class="public_keys pgp_block">';
$res .= '<form enctype="multipart/form-data" method="post" action="?page=pgp#public_keys" class="pgp_subblock">'.$this->trans('Import a public key from a file').'<br /><br />';
$res .= '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />';
$res .= '<input required id="public_key" name="public_key" type="file"> for ';
$res .= '<input required id="public_email" name="public_key_email" placeholder="'.$this->trans('E-mail Address');
$res .= '" type="email"> <input type="submit" value="'.$this->trans('Import').'">';
$res .= '</form>';
$res .= '<br /><br /><table class="pgp_keys"><thead><tr><th>'.$this->trans('Fingerprint').'</th>';
$res .= '<th>'.$this->trans('E-mail').'</th><th></th></tr>';
$res .= '</thead><tbody>';
foreach ($this->get('pgp_public_keys', array()) as $index => $vals) {
$res .= '<tr><td>'.$this->html_safe($vals['fingerprint']).'</td><td>'.$this->html_safe($vals['email']).'</td>';
$res .= '</td><td><form method="post" action="?page=pgp#public_keys">';
$res .= '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />';
$res .= '<input type="hidden" value="'.$this->html_safe($index).'" name="delete_public_key_id" />'.
'<input type="image" class="delete_pgp_key" alt="'.$this->trans('Delete');
$res .= '" src="'.Hm_Image_Sources::$circle_x.'"/></button></form></td></tr>';
}
$res .= '</tbody></table>';
$res .= '</div>';
return $res;
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_private_key extends Hm_Output_Module {
protected function output() {
$res = '<div class="priv_title settings_subtitle">'.$this->trans('Private Keys');
$res .= '<span class="private_key_count">'.sprintf($this->trans('%s imported'), 0).'</span></div>';
$res .= '<div class="priv_keys pgp_block"><div class="pgp_subblock">';
$res .= $this->trans('Private keys never leave your browser, and are deleted when you logout');
$res .= '<br /><br /><input id="priv_key" type="file">';
$res .= '</div>'.$this->trans('Existing Keys').'<table class="pgp_keys private_key_list"><thead><tr><th>'.$this->trans('Identity').'</th><th></th></tr>';
$res .= '</thead><tbody></tbody></table>';
$res .= '</div>';
return $res;
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_autocrypt_private_keys extends Hm_Output_Module
{
protected function output()
{
$res = '<div class="priv_ac_title settings_subtitle">' . $this->trans('Autocrypt Private Keys');
$res .= '<span class="private_key_count">' . sprintf($this->trans('%s created'), 0) . '</span></div>';
$res .= '<div class="priv_ac_keys pgp_block"><div class="pgp_subblock">';
$res .= $this->trans('Private keys never leave your browser, and are deleted when you logout');
$res .= '<br /><br /><button class="generate_ac_keys_button"><span>Generate Keys</span></button>';
$res .= '</div>' . $this->trans('Existing Keys') . '<table class="pgp_keys autocrypt_key_list"><thead><tr><th>' . $this->trans('Identity') . '</th><th></th></tr>';
$res .= '</thead><tbody>';
foreach($this->get('autocrypt_keys') as $key) {
$res .= '<tr><td>'.$key['email'].'</td><td></td></tr>';
}
$res .= '</tbody></table>';
$res .= '</div>';
return $res;
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_end extends Hm_Output_Module {
protected function output() {
return '</div>';
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_msg_controls extends Hm_Output_Module {
protected function output() {
return '<script type="text/javascript" src="'.WEB_ROOT.'modules/pgp/assets/openpgp.min.js"></script>'.
'<div class="pgp_msg_controls"><select class="pgp_private_keys"></select> <input type="button" class="pgp_btn" value="Decrypt" /></div>'.prompt_for_passhrase($this);
}
}
/**
* @subpackage pgp/output
*/
class Hm_Output_pgp_settings_link extends Hm_Output_Module {
protected function output() {
$res = '<li class="menu_profiles"><a class="unread_link" href="?page=pgp">';
if (!$this->get('hide_folder_icons')) {
$res .= '<img class="account_icon" src="'.$this->html_safe(Hm_Image_Sources::$lock).'" alt="" width="16" height="16" /> ';
}
$res .= $this->trans('PGP').'</a></li>';
if ($this->format == 'HTML5') {
return $res;
}
$this->concat('formatted_folder_list', $res);
}
}
/**
* @subpackage pgp/functions
*/
if (!hm_exists('validate_public_key')) {
function validate_public_key($file_location) {
if (!class_exists('gnupg')) {
Hm_Debug::add('Gnupg PECL extension not found');
return false;
}
if (!is_readable($file_location)) {
Hm_Debug::add('Uploaded public key not readable');
return false;
}
$data = file_get_contents($file_location);
if (!$data) {
Hm_Debug::add('Uploaded public key not readable');
return false;
}
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
putenv(sprintf('GNUPGHOME=%s/.gnupg', $tmp_dir));
$gpg = new gnupg();
$info = $gpg->import($data);
if (is_array($info) && array_key_exists('fingerprint', $info)) {
return $info['fingerprint'];
}
return false;
}}
/**
* @subpackage pgp/functions
*/
if (!hm_exists('prompt_for_passhrase')) {
function prompt_for_passhrase($mod) {
return '<div class="passphrase_prompt"><div class="title">'.$mod->trans('Please enter your passphrase').'</div><input type="password" value="" id="pgp_pass" /> <input id="submit_pgp_pass" type="button" value="'.$mod->trans('Submit').'" /></div>';
}}