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', 10);
24 Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare', 10);
26 Logger::notice('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
38 function securemail_settings(array &$data)
40 if (!DI::userSession()->getLocalUserId()) {
44 $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'enable'));
45 $publickey = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'pkey');
47 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/securemail/');
48 $html = Renderer::replaceMacros($t, [
49 '$enabled' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enabled],
50 '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format')]
54 'addon' => 'securemail',
55 'title' => DI::l10n()->t('"Secure Mail" Settings'),
58 'securemail-submit' => DI::l10n()->t('Save Settings'),
59 'securemail-test' => DI::l10n()->t('Save and send test'),
65 * @brief Handle data from user settings form
67 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
69 * @param array $b hook data
73 function securemail_settings_post(array &$b)
75 if (!DI::userSession()->getLocalUserId()) {
79 if (!empty($_POST['securemail-submit']) || !empty($_POST['securemail-test'])) {
80 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
81 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
82 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable);
84 if (!empty($_POST['securemail-test'])) {
85 $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
87 // revert to saved value
88 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable);
91 DI::sysmsg()->addInfo(DI::l10n()->t('Test email sent'));
93 DI::sysmsg()->addNotice(DI::l10n()->t('There was an error sending the test email'));
100 * @brief Encrypt notification emails text
102 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
104 * @param IEmail $email Email
108 function securemail_emailer_send_prepare(IEmail &$email)
110 if (empty($email->getRecipientUid())) {
114 $uid = $email->getRecipientUid();
116 $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
117 if (!$enable_checked) {
118 DI::logger()->debug('No securemail enabled.');
122 $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
124 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
125 $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
126 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
128 $key = OpenPGP_Message::parse($public_key);
130 $data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
132 'filename' => 'encrypted.gpg'
136 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
137 $armored_encrypted = wordwrap(
138 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
144 $email = $email->withMessage($armored_encrypted, null);
146 } catch (Exception $e) {
147 DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);