4 * Description: Send notification mail encrypted with user-defined public GPG key
6 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
15 use Friendica\Util\Emailer;
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');
29 function securemail_uninstall()
31 Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
32 Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
34 Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
36 Logger::log('removed securemail');
40 * @brief Build user settings form
42 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
44 * @param App $a App instance
45 * @param string $s output html
49 function securemail_settings(App &$a, &$s)
55 $enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
56 $publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
58 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
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"']
70 * @brief Handle data from user settings form
72 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
74 * @param App $a App instance
75 * @param array $b hook data
79 function securemail_settings_post(App &$a, array &$b)
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);
91 if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
92 $sitename = DI::config()->get('config', 'sitename');
94 $hostname = DI::baseUrl()->getHostname();
95 if (strpos($hostname, ':')) {
96 $hostname = substr($hostname, 0, strpos($hostname, ':'));
99 $sender_email = DI::config()->get('config', 'sender_email');
100 if (empty($sender_email)) {
101 $sender_email = 'noreply@' . $hostname;
104 $subject = 'Friendica - Secure Mail - Test';
105 $message = 'This is a test message from your Friendica Secure Mail addon.';
108 'uid' => local_user(),
109 'fromName' => $sitename,
110 'fromEmail' => $sender_email,
111 'toEmail' => $a->user['email'],
112 'messageSubject' => $subject,
113 'htmlVersion' => "<p>{$message}</p>",
114 'textVersion' => $message,
117 // enable addon for test
118 DI::pConfig()->set(local_user(), 'securemail', 'enable', 1);
120 $res = Emailer::send($params);
122 // revert to saved value
123 DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
126 info(DI::l10n()->t('Test email sent') . EOL);
128 notice(DI::l10n()->t('There was an error sending the test email') . EOL);
135 * @brief Encrypt notification emails text
137 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
139 * @param App $a App instance
140 * @param array $b hook data
144 function securemail_emailer_send_prepare(App &$a, array &$b)
146 if (empty($b['uid'])) {
152 $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
153 if (!$enable_checked) {
157 $public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
159 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
160 $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
161 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
163 $key = OpenPGP_Message::parse($public_key);
165 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
167 'filename' => 'encrypted.gpg'
169 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
170 $armored_encrypted = wordwrap(
171 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
177 $b['textVersion'] = $armored_encrypted;
178 $b['htmlVersion'] = null;