Fix logging
[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 function securemail_uninstall()
30 {
31         Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
32         Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
33
34         Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
35
36         Logger::log('removed securemail');
37 }
38
39 /**
40  * @brief Build user settings form
41  *
42  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
43  *
44  * @param App    $a App instance
45  * @param string $s output html
46  *
47  * @see   App
48  */
49 function securemail_settings(App &$a, &$s)
50 {
51         if (!local_user()) {
52                 return;
53         }
54
55         $enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
56         $publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
57
58         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
59
60         $s .= Renderer::replaceMacros($t, [
61                 '$title' => DI::l10n()->t('"Secure Mail" Settings'),
62                 '$submit' => DI::l10n()->t('Save Settings'),
63                 '$test' => DI::l10n()->t('Save and send test'), //NOTE: update also in 'post'
64                 '$enable' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enable, ''],
65                 '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format'), 'rows="10"']
66         ]);
67 }
68
69 /**
70  * @brief Handle data from user settings form
71  *
72  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
73  *
74  * @param App   $a App instance
75  * @param array $b hook data
76  *
77  * @see   App
78  */
79 function securemail_settings_post(App &$a, array &$b)
80 {
81         if (!local_user()) {
82                 return;
83         }
84
85         if ($_POST['securemail-submit']) {
86                 DI::pConfig()->set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
87                 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
88                 DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
89                 info(DI::l10n()->t('Secure Mail Settings saved.') . EOL);
90
91                 if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
92
93                         $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
94
95                         // revert to saved value
96                         DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
97
98                         if ($res) {
99                                 info(DI::l10n()->t('Test email sent') . EOL);
100                         } else {
101                                 notice(DI::l10n()->t('There was an error sending the test email') . EOL);
102                         }
103                 }
104         }
105 }
106
107 /**
108  * @brief Encrypt notification emails text
109  *
110  * @link  https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
111  *
112  * @param App   $a App instance
113  * @param IEmail $email Email
114  *
115  * @see   App
116  */
117 function securemail_emailer_send_prepare(App &$a, IEmail &$email)
118 {
119         if (empty($email->getRecipientUid())) {
120                 return;
121         }
122
123         $uid = $email->getRecipientUid();
124
125         $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
126         if (!$enable_checked) {
127                 DI::logger()->debug('No securemail enabled.');
128                 return;
129         }
130
131         $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
132
133         preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
134         $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
135         $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
136
137         $key = OpenPGP_Message::parse($public_key);
138
139         $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
140                 'format' => 'u',
141                 'filename' => 'encrypted.gpg'
142         ]);
143
144         try {
145                 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
146                 $armored_encrypted = wordwrap(
147                         OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
148                         64,
149                         "\n",
150                         true
151                 );
152
153                 $email = $email->withMessage($armored_encrypted, null);
154
155         } catch (Exception $e) {
156                 DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
157         }
158 }