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;
16 use Friendica\Util\Emailer;
18 require_once __DIR__ . '/vendor/autoload.php';
20 function securemail_install()
22 Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
23 Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
25 Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
27 Logger::log('installed securemail');
30 function securemail_uninstall()
32 Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
33 Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
35 Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
37 Logger::log('removed securemail');
41 * @brief Build user settings form
43 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
45 * @param App $a App instance
46 * @param string $s output html
50 function securemail_settings(App &$a, &$s)
56 $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
57 $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
59 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
61 $s .= Renderer::replaceMacros($t, [
62 '$title' => L10n::t('"Secure Mail" Settings'),
63 '$submit' => L10n::t('Save Settings'),
64 '$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
65 '$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''],
66 '$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
71 * @brief Handle data from user settings form
73 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
75 * @param App $a App instance
76 * @param array $b hook data
80 function securemail_settings_post(App &$a, array &$b)
86 if ($_POST['securemail-submit']) {
87 PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
88 $enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
89 PConfig::set(local_user(), 'securemail', 'enable', $enable);
90 info(L10n::t('Secure Mail Settings saved.') . EOL);
92 if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
93 $sitename = Config::get('config', 'sitename');
95 $hostname = $a->getHostName();
96 if (strpos($hostname, ':')) {
97 $hostname = substr($hostname, 0, strpos($hostname, ':'));
100 $sender_email = Config::get('config', 'sender_email');
101 if (empty($sender_email)) {
102 $sender_email = 'noreply@' . $hostname;
105 $subject = 'Friendica - Secure Mail - Test';
106 $message = 'This is a test message from your Friendica Secure Mail addon.';
109 'uid' => local_user(),
110 'fromName' => $sitename,
111 'fromEmail' => $sender_email,
112 'toEmail' => $a->user['email'],
113 'messageSubject' => $subject,
114 'htmlVersion' => "<p>{$message}</p>",
115 'textVersion' => $message,
118 // enable addon for test
119 PConfig::set(local_user(), 'securemail', 'enable', 1);
121 $res = Emailer::send($params);
123 // revert to saved value
124 PConfig::set(local_user(), 'securemail', 'enable', $enable);
127 info(L10n::t('Test email sent') . EOL);
129 notice(L10n::t('There was an error sending the test email') . EOL);
136 * @brief Encrypt notification emails text
138 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
140 * @param App $a App instance
141 * @param array $b hook data
145 function securemail_emailer_send_prepare(App &$a, array &$b)
147 if (empty($b['uid'])) {
153 $enable_checked = PConfig::get($uid, 'securemail', 'enable');
154 if (!$enable_checked) {
158 $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
160 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
161 $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
162 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
164 $key = OpenPGP_Message::parse($public_key);
166 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
168 'filename' => 'encrypted.gpg'
170 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
171 $armored_encrypted = wordwrap(
172 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
178 $b['textVersion'] = $armored_encrypted;
179 $b['htmlVersion'] = null;