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\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Util\Emailer;
16 /* because the fraking openpgp-php is in composer, require libs in composer
17 * and then don't use autoloader to load classes... */
18 $path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
19 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
20 /* so, we don't use the autoloader and include what we need */
21 $path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
22 set_include_path(get_include_path() . PATH_SEPARATOR . $path);
24 require_once 'openpgp.php';
25 require_once 'openpgp_crypt_symmetric.php';
28 function securemail_install() {
29 Addon::registerHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
30 Addon::registerHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
32 Addon::registerHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
34 logger('installed securemail');
37 function securemail_uninstall() {
38 Addon::unregisterHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
39 Addon::unregisterHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
41 Addon::unregisterHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
43 logger('removed securemail');
47 * @brief Build user settings form
49 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
51 * @param App $a App instance
52 * @param string $s output html
56 function securemail_settings(App &$a, &$s){
61 $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
62 $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
64 $t = get_markup_template('admin.tpl', 'addon/securemail/');
66 $s .= replace_macros($t, [
67 '$title' => L10n::t('"Secure Mail" Settings'),
68 '$submit' => L10n::t('Save Settings'),
69 '$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
70 '$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''],
71 '$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
76 * @brief Handle data from user settings form
78 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
80 * @param App $a App instance
81 * @param array $b hook data
85 function securemail_settings_post(App &$a, array &$b){
91 if ($_POST['securemail-submit']) {
92 PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
93 $enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
94 PConfig::set(local_user(), 'securemail', 'enable', $enable);
95 info(L10n::t('Secure Mail Settings saved.') . EOL);
97 if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
98 $sitename = Config::get('config', 'sitename');
100 $hostname = $a->getHostName();
101 if (strpos($hostname, ':')) {
102 $hostname = substr($hostname, 0, strpos($hostname, ':'));
105 $sender_email = Config::get('config', 'sender_email');
106 if (empty($sender_email)) {
107 $sender_email = 'noreply@' . $hostname;
110 $subject = 'Friendica - Secure Mail - Test';
111 $message = 'This is a test message from your Friendica Secure Mail addon.';
114 'uid' => local_user(),
115 'fromName' => $sitename,
116 'fromEmail' => $sender_email,
117 'toEmail' => $a->user['email'],
118 'messageSubject' => $subject,
119 'htmlVersion' => "<p>{$message}</p>",
120 'textVersion' => $message,
123 // enable addon for test
124 PConfig::set(local_user(), 'securemail', 'enable', 1);
126 $res = Emailer::send($params);
128 // revert to saved value
129 PConfig::set(local_user(), 'securemail', 'enable', $enable);
132 info(L10n::t('Test email sent') . EOL);
134 notice(L10n::t('There was an error sending the test email') . EOL);
141 * @brief Encrypt notification emails text
143 * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
145 * @param App $a App instance
146 * @param array $b hook data
150 function securemail_emailer_send_prepare(App &$a, array &$b) {
157 $enable_checked = PConfig::get($uid, 'securemail', 'enable');
158 if (!$enable_checked) {
162 $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
164 preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
165 $marker = (empty($matches[1])) ? 'MESSAGE' : $matches[1];
166 $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
168 $key = OpenPGP_Message::parse($public_key);
170 $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
172 'filename' => 'encrypted.gpg'
174 $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
175 $armored_encrypted = wordwrap(
176 OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
182 $b['textVersion'] = $armored_encrypted;
183 $b['htmlVersion'] = null;
188 * add addon composer autoloader maps to system autoloader
190 function securemail_autoloader() {
192 $loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
194 $map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
195 foreach ($map as $namespace => $path) {
196 $loader->set($namespace, $path);
199 $map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
200 foreach ($map as $namespace => $path) {
201 $loader->setPsr4($namespace, $path);
204 $classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
206 $loader->addClassMap($classMap);
209 securemail_autoloader();