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\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
17 use Friendica\Util\Emailer;
19 require_once __DIR__ . '/vendor/autoload.php';
21 function securemail_install()
23 Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
24 Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
26 Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
28 Logger::log('installed securemail');
31 function securemail_uninstall()
33 Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
34 Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
36 Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
38 Logger::log('removed securemail');
42 * @brief Build user settings form
44 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
46 * @param App $a App instance
47 * @param string $s output html
51 function securemail_settings(App &$a, &$s)
57 $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
58 $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
60 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
62 $s .= Renderer::replaceMacros($t, [
63 '$title' => L10n::t('"Secure Mail" Settings'),
64 '$submit' => L10n::t('Save Settings'),
65 '$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
66 '$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''],
67 '$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
72 * @brief Handle data from user settings form
74 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
76 * @param App $a App instance
77 * @param array $b hook data
81 function securemail_settings_post(App &$a, array &$b)
87 if ($_POST['securemail-submit']) {
88 PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
89 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
90 PConfig::set(local_user(), 'securemail', 'enable', $enable);
91 info(L10n::t('Secure Mail Settings saved.') . EOL);
93 if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
94 $sitename = Config::get('config', 'sitename');
96 $hostname = DI::baseUrl()->getHostname();
97 if (strpos($hostname, ':')) {
98 $hostname = substr($hostname, 0, strpos($hostname, ':'));
101 $sender_email = Config::get('config', 'sender_email');
102 if (empty($sender_email)) {
103 $sender_email = 'noreply@' . $hostname;
106 $subject = 'Friendica - Secure Mail - Test';
107 $message = 'This is a test message from your Friendica Secure Mail addon.';
110 'uid' => local_user(),
111 'fromName' => $sitename,
112 'fromEmail' => $sender_email,
113 'toEmail' => $a->user['email'],
114 'messageSubject' => $subject,
115 'htmlVersion' => "<p>{$message}</p>",
116 'textVersion' => $message,
119 // enable addon for test
120 PConfig::set(local_user(), 'securemail', 'enable', 1);
122 $res = Emailer::send($params);
124 // revert to saved value
125 PConfig::set(local_user(), 'securemail', 'enable', $enable);
128 info(L10n::t('Test email sent') . EOL);
130 notice(L10n::t('There was an error sending the test email') . EOL);
137 * @brief Encrypt notification emails text
139 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
141 * @param App $a App instance
142 * @param array $b hook data
146 function securemail_emailer_send_prepare(App &$a, array &$b)
148 if (empty($b['uid'])) {
154 $enable_checked = PConfig::get($uid, 'securemail', 'enable');
155 if (!$enable_checked) {
159 $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
161 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
162 $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
163 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
165 $key = OpenPGP_Message::parse($public_key);
167 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
169 'filename' => 'encrypted.gpg'
171 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
172 $armored_encrypted = wordwrap(
173 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
179 $b['textVersion'] = $armored_encrypted;
180 $b['htmlVersion'] = null;