Comment change
[friendica-addons.git/.git] / securemail / securemail.php
1 <?php
2 /**
3  * Name: Secure Mail
4  * Description: Send notification mail encrypted with user-defined public GPG key
5  * Version: 2.0
6  * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
7  */
8
9 use Friendica\Addon\securemail\SecureTestEmail;
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15 use Friendica\Object\EMail\IEmail;
16
17 require_once __DIR__ . '/vendor/autoload.php';
18
19 function securemail_install()
20 {
21         Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
22         Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
23
24         Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
25
26         Logger::log('installed securemail');
27 }
28
29 /**
30  * @brief Build user settings form
31  *
32  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
33  *
34  * @param App    $a App instance
35  * @param string $s output html
36  *
37  * @see   App
38  */
39 function securemail_settings(App &$a, &$s)
40 {
41         if (!local_user()) {
42                 return;
43         }
44
45         $enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
46         $publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
47
48         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
49
50         $s .= Renderer::replaceMacros($t, [
51                 '$title' => DI::l10n()->t('"Secure Mail" Settings'),
52                 '$submit' => DI::l10n()->t('Save Settings'),
53                 '$test' => DI::l10n()->t('Save and send test'), //NOTE: update also in 'post'
54                 '$enable' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enable, ''],
55                 '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format'), 'rows="10"']
56         ]);
57 }
58
59 /**
60  * @brief Handle data from user settings form
61  *
62  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
63  *
64  * @param App   $a App instance
65  * @param array $b hook data
66  *
67  * @see   App
68  */
69 function securemail_settings_post(App &$a, array &$b)
70 {
71         if (!local_user()) {
72                 return;
73         }
74
75         if ($_POST['securemail-submit']) {
76                 DI::pConfig()->set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
77                 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
78                 DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
79
80                 if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
81
82                         $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
83
84                         // revert to saved value
85                         DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
86
87                         if ($res) {
88                                 info(DI::l10n()->t('Test email sent') . EOL);
89                         } else {
90                                 notice(DI::l10n()->t('There was an error sending the test email') . EOL);
91                         }
92                 }
93         }
94 }
95
96 /**
97  * @brief Encrypt notification emails text
98  *
99  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
100  *
101  * @param App   $a App instance
102  * @param IEmail $email Email
103  *
104  * @see   App
105  */
106 function securemail_emailer_send_prepare(App &$a, IEmail &$email)
107 {
108         if (empty($email->getRecipientUid())) {
109                 return;
110         }
111
112         $uid = $email->getRecipientUid();
113
114         $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
115         if (!$enable_checked) {
116                 DI::logger()->debug('No securemail enabled.');
117                 return;
118         }
119
120         $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
121
122         preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
123         $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
124         $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
125
126         $key = OpenPGP_Message::parse($public_key);
127
128         $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
129                 'format' => 'u',
130                 'filename' => 'encrypted.gpg'
131         ]);
132
133         try {
134                 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
135                 $armored_encrypted = wordwrap(
136                         OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
137                         64,
138                         "\n",
139                         true
140                 );
141
142                 $email = $email->withMessage($armored_encrypted, null);
143
144         } catch (Exception $e) {
145                 DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
146         }
147 }