2017-04-11 15:19:54 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Secure Mail
|
|
|
|
* Description: Send notification mail encrypted with user-defined public GPG key
|
|
|
|
* Version: 2.0
|
|
|
|
* Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
|
|
|
|
*/
|
|
|
|
|
2020-01-26 17:47:15 -05:00
|
|
|
use Friendica\Addon\securemail\SecureTestEmail;
|
2017-05-22 02:47:36 -04:00
|
|
|
use Friendica\App;
|
2018-12-26 02:28:16 -05:00
|
|
|
use Friendica\Core\Hook;
|
2018-10-29 19:40:18 -04:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-31 10:55:15 -04:00
|
|
|
use Friendica\Core\Renderer;
|
2019-12-15 18:47:24 -05:00
|
|
|
use Friendica\DI;
|
2020-01-29 14:20:39 -05:00
|
|
|
use Friendica\Object\EMail\IEmail;
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2019-02-10 10:06:45 -05:00
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2019-02-10 08:45:14 -05:00
|
|
|
function securemail_install()
|
|
|
|
{
|
|
|
|
Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
|
2021-05-29 14:59:51 -04:00
|
|
|
Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post', 10);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2021-05-29 14:59:51 -04:00
|
|
|
Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare', 10);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2021-10-21 02:04:27 -04:00
|
|
|
Logger::notice('installed securemail');
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Build user settings form
|
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
|
2017-04-14 03:13:19 -04:00
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @param App $a App instance
|
2017-04-14 03:13:19 -04:00
|
|
|
* @param string $s output html
|
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @see App
|
2017-04-14 03:13:19 -04:00
|
|
|
*/
|
2021-11-20 04:56:55 -05:00
|
|
|
function securemail_settings(App &$a, array &$data)
|
2019-02-10 08:45:14 -05:00
|
|
|
{
|
|
|
|
if (!local_user()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
$enabled = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
|
2020-01-18 10:50:56 -05:00
|
|
|
$publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
|
2019-02-10 08:45:14 -05:00
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/securemail/');
|
|
|
|
$html = Renderer::replaceMacros($t, [
|
|
|
|
'$enabled' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enabled],
|
2021-06-27 15:33:12 -04:00
|
|
|
'$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format')]
|
2019-02-10 08:45:14 -05:00
|
|
|
]);
|
2021-11-20 04:56:55 -05:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'addon' => 'securemail',
|
|
|
|
'title' => DI::l10n()->t('"Secure Mail" Settings'),
|
|
|
|
'html' => $html,
|
|
|
|
'submit' => [
|
|
|
|
'securemail-submit' => DI::l10n()->t('Save Settings'),
|
|
|
|
'securemail-test' => DI::l10n()->t('Save and send test'),
|
|
|
|
],
|
|
|
|
];
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Handle data from user settings form
|
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
|
2017-04-14 03:13:19 -04:00
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @param App $a App instance
|
2017-04-14 03:13:19 -04:00
|
|
|
* @param array $b hook data
|
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @see App
|
2017-04-14 03:13:19 -04:00
|
|
|
*/
|
2019-02-10 08:45:14 -05:00
|
|
|
function securemail_settings_post(App &$a, array &$b)
|
|
|
|
{
|
|
|
|
if (!local_user()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
if (!empty($_POST['securemail-submit']) || !empty($_POST['securemail-test'])) {
|
2020-01-18 10:54:49 -05:00
|
|
|
DI::pConfig()->set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
|
2019-02-10 08:45:14 -05:00
|
|
|
$enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
|
2020-01-18 10:54:49 -05:00
|
|
|
DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
|
2019-02-10 08:45:14 -05:00
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
if (!empty($_POST['securemail-test'])) {
|
2020-01-26 17:47:15 -05:00
|
|
|
$res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
|
2019-02-10 08:45:14 -05:00
|
|
|
|
|
|
|
// revert to saved value
|
2020-01-18 10:54:49 -05:00
|
|
|
DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
|
2019-02-10 08:45:14 -05:00
|
|
|
|
|
|
|
if ($res) {
|
2020-01-18 14:52:33 -05:00
|
|
|
info(DI::l10n()->t('Test email sent') . EOL);
|
2019-02-10 08:45:14 -05:00
|
|
|
} else {
|
2020-01-18 14:52:33 -05:00
|
|
|
notice(DI::l10n()->t('There was an error sending the test email') . EOL);
|
2019-02-10 08:45:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Encrypt notification emails text
|
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
|
2017-04-14 03:13:19 -04:00
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @param App $a App instance
|
2020-01-29 14:20:39 -05:00
|
|
|
* @param IEmail $email Email
|
2017-04-14 03:13:19 -04:00
|
|
|
*
|
2019-02-10 08:45:14 -05:00
|
|
|
* @see App
|
2017-04-14 03:13:19 -04:00
|
|
|
*/
|
2020-01-29 14:20:39 -05:00
|
|
|
function securemail_emailer_send_prepare(App &$a, IEmail &$email)
|
2019-02-10 08:45:14 -05:00
|
|
|
{
|
2020-01-29 14:20:39 -05:00
|
|
|
if (empty($email->getRecipientUid())) {
|
2019-02-10 08:45:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:20:39 -05:00
|
|
|
$uid = $email->getRecipientUid();
|
2019-02-10 08:45:14 -05:00
|
|
|
|
2020-01-18 10:50:56 -05:00
|
|
|
$enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
|
2019-02-10 08:45:14 -05:00
|
|
|
if (!$enable_checked) {
|
2020-01-31 13:43:34 -05:00
|
|
|
DI::logger()->debug('No securemail enabled.');
|
2019-02-10 08:45:14 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-18 10:50:56 -05:00
|
|
|
$public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
|
2019-02-10 08:45:14 -05:00
|
|
|
|
|
|
|
preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
|
|
|
|
$marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
|
|
|
|
$public_key = OpenPGP::unarmor($public_key_ascii, $marker);
|
|
|
|
|
|
|
|
$key = OpenPGP_Message::parse($public_key);
|
|
|
|
|
2020-01-29 14:20:39 -05:00
|
|
|
$data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
|
2019-02-10 08:45:14 -05:00
|
|
|
'format' => 'u',
|
|
|
|
'filename' => 'encrypted.gpg'
|
|
|
|
]);
|
2020-01-31 13:32:16 -05:00
|
|
|
|
2020-01-29 14:20:39 -05:00
|
|
|
try {
|
|
|
|
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
|
|
|
|
$armored_encrypted = wordwrap(
|
|
|
|
OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
|
|
|
|
64,
|
|
|
|
"\n",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2020-01-31 13:32:16 -05:00
|
|
|
$email = $email->withMessage($armored_encrypted, null);
|
|
|
|
|
2020-01-29 14:20:39 -05:00
|
|
|
} catch (Exception $e) {
|
|
|
|
DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
|
|
|
|
}
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|