4 * Description: Send notification mail encrypted with user-defined public GPG key
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
9 use Friendica\Addon\securemail\SecureTestEmail;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
15 use Friendica\Object\EMail\IEmail;
17 require_once __DIR__ . '/vendor/autoload.php';
19 function securemail_install()
21 Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
22 Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
24 Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
26 Logger::log('installed securemail');
30 * @brief Build user settings form
32 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
34 * @param App $a App instance
35 * @param string $s output html
39 function securemail_settings(App &$a, &$s)
45 $enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
46 $publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
48 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
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"']
60 * @brief Handle data from user settings form
62 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
64 * @param App $a App instance
65 * @param array $b hook data
69 function securemail_settings_post(App &$a, array &$b)
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);
80 if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
82 $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
84 // revert to saved value
85 DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
88 info(DI::l10n()->t('Test email sent') . EOL);
90 notice(DI::l10n()->t('There was an error sending the test email') . EOL);
97 * @brief Encrypt notification emails text
99 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
101 * @param App $a App instance
102 * @param IEmail $email Email
106 function securemail_emailer_send_prepare(App &$a, IEmail &$email)
108 if (empty($email->getRecipientUid())) {
112 $uid = $email->getRecipientUid();
114 $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
115 if (!$enable_checked) {
116 DI::logger()->debug('No securemail enabled.');
120 $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
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);
126 $key = OpenPGP_Message::parse($public_key);
128 $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
130 'filename' => 'encrypted.gpg'
134 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
135 $armored_encrypted = wordwrap(
136 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
142 $email = $email->withMessage($armored_encrypted, null);
144 } catch (Exception $e) {
145 DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);