friendica-addons/securemail/securemail.php

163 lines
4.8 KiB
PHP
Raw Normal View History

<?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>
*/
use Friendica\Addon\securemail\SecureTestEmail;
2017-05-22 08:47:36 +02:00
use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Object\EMail\IEmail;
require_once __DIR__ . '/vendor/autoload.php';
2019-02-10 14:45:14 +01:00
function securemail_install()
{
Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
2019-02-10 14:45:14 +01:00
Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
2019-02-10 14:45:14 +01:00
Logger::log('installed securemail');
}
2019-02-10 14:45:14 +01:00
function securemail_uninstall()
{
Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
2019-02-10 14:45:14 +01:00
Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
2019-02-10 14:45:14 +01:00
Logger::log('removed securemail');
}
2017-04-14 09:13:19 +02:00
/**
* @brief Build user settings form
*
2019-02-10 14:45:14 +01:00
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
2017-04-14 09:13:19 +02:00
*
2019-02-10 14:45:14 +01:00
* @param App $a App instance
2017-04-14 09:13:19 +02:00
* @param string $s output html
*
2019-02-10 14:45:14 +01:00
* @see App
2017-04-14 09:13:19 +02:00
*/
2019-02-10 14:45:14 +01:00
function securemail_settings(App &$a, &$s)
{
if (!local_user()) {
return;
}
$enable = intval(DI::pConfig()->get(local_user(), 'securemail', 'enable'));
$publickey = DI::pConfig()->get(local_user(), 'securemail', 'pkey');
2019-02-10 14:45:14 +01:00
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
$s .= Renderer::replaceMacros($t, [
'$title' => DI::l10n()->t('"Secure Mail" Settings'),
'$submit' => DI::l10n()->t('Save Settings'),
'$test' => DI::l10n()->t('Save and send test'), //NOTE: update also in 'post'
'$enable' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enable, ''],
'$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format'), 'rows="10"']
2019-02-10 14:45:14 +01:00
]);
}
2017-04-14 09:13:19 +02:00
/**
* @brief Handle data from user settings form
*
2019-02-10 14:45:14 +01:00
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
2017-04-14 09:13:19 +02:00
*
2019-02-10 14:45:14 +01:00
* @param App $a App instance
2017-04-14 09:13:19 +02:00
* @param array $b hook data
*
2019-02-10 14:45:14 +01:00
* @see App
2017-04-14 09:13:19 +02:00
*/
2019-02-10 14:45:14 +01:00
function securemail_settings_post(App &$a, array &$b)
{
if (!local_user()) {
return;
}
if ($_POST['securemail-submit']) {
DI::pConfig()->set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
2019-02-10 14:45:14 +01:00
$enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
info(DI::l10n()->t('Secure Mail Settings saved.') . EOL);
2019-02-10 14:45:14 +01:00
if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
2019-02-10 14:45:14 +01:00
$res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
2019-02-10 14:45:14 +01:00
// revert to saved value
DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);
2019-02-10 14:45:14 +01:00
if ($res) {
info(DI::l10n()->t('Test email sent') . EOL);
2019-02-10 14:45:14 +01:00
} else {
notice(DI::l10n()->t('There was an error sending the test email') . EOL);
2019-02-10 14:45:14 +01:00
}
}
}
}
2017-04-14 09:13:19 +02:00
/**
* @brief Encrypt notification emails text
*
2019-02-10 14:45:14 +01:00
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
2017-04-14 09:13:19 +02:00
*
2019-02-10 14:45:14 +01:00
* @param App $a App instance
* @param IEmail $email Email
2017-04-14 09:13:19 +02:00
*
2019-02-10 14:45:14 +01:00
* @see App
2017-04-14 09:13:19 +02:00
*/
function securemail_emailer_send_prepare(App &$a, IEmail &$email)
2019-02-10 14:45:14 +01:00
{
2020-01-31 19:40:50 +01:00
DI::logger()->debug('start securemail', ['email' => $email]);
2020-01-31 19:32:16 +01:00
if (empty($email->getRecipientUid())) {
2019-02-10 14:45:14 +01:00
return;
}
$uid = $email->getRecipientUid();
2019-02-10 14:45:14 +01:00
$enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable');
2019-02-10 14:45:14 +01:00
if (!$enable_checked) {
2020-01-31 19:40:50 +01:00
DI::logger()->debug('No check', ['email' => $email]);
2019-02-10 14:45:14 +01:00
return;
}
$public_key_ascii = DI::pConfig()->get($uid, 'securemail', 'pkey');
2019-02-10 14:45:14 +01: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);
$data = new OpenPGP_LiteralDataPacket($email->getMessage(true), [
2019-02-10 14:45:14 +01:00
'format' => 'u',
'filename' => 'encrypted.gpg'
]);
2020-01-31 19:32:16 +01: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 19:32:16 +01:00
$email = $email->withMessage($armored_encrypted, null);
2020-01-31 19:40:50 +01:00
DI::logger()->debug('End securemail', ['email' => $email]);
2020-01-31 19:32:16 +01:00
} catch (Exception $e) {
DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]);
}
}