2017-04-11 19:19:54 +00: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>
|
|
|
|
*/
|
|
|
|
|
2017-05-22 06:47:36 +00:00
|
|
|
use Friendica\App;
|
2018-07-10 12:38:36 +00:00
|
|
|
use Friendica\Core\Config;
|
2018-12-26 07:28:16 +00:00
|
|
|
use Friendica\Core\Hook;
|
2018-01-22 19:03:11 +00:00
|
|
|
use Friendica\Core\L10n;
|
2018-10-29 23:40:18 +00:00
|
|
|
use Friendica\Core\Logger;
|
2017-11-06 23:55:24 +00:00
|
|
|
use Friendica\Core\PConfig;
|
2018-10-31 14:55:15 +00:00
|
|
|
use Friendica\Core\Renderer;
|
2017-11-22 23:10:01 +00:00
|
|
|
use Friendica\Util\Emailer;
|
2017-04-11 19:19:54 +00:00
|
|
|
|
|
|
|
/* because the fraking openpgp-php is in composer, require libs in composer
|
|
|
|
* and then don't use autoloader to load classes... */
|
2017-04-14 07:13:19 +00:00
|
|
|
$path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
|
2017-04-11 19:19:54 +00:00
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
|
|
|
|
/* so, we don't use the autoloader and include what we need */
|
2017-04-14 07:13:19 +00:00
|
|
|
$path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
|
2017-04-11 19:19:54 +00:00
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
|
|
|
|
|
2017-04-14 07:13:19 +00:00
|
|
|
require_once 'openpgp.php';
|
|
|
|
require_once 'openpgp_crypt_symmetric.php';
|
2019-02-10 13:45:14 +00: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');
|
2017-04-11 19:19:54 +00:00
|
|
|
|
2019-02-10 13:45:14 +00:00
|
|
|
Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
|
2017-04-11 19:19:54 +00:00
|
|
|
|
2019-02-10 13:45:14 +00:00
|
|
|
Logger::log('installed securemail');
|
2017-04-11 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 13:45:14 +00: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');
|
2017-04-11 19:19:54 +00:00
|
|
|
|
2019-02-10 13:45:14 +00:00
|
|
|
Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
|
2017-04-11 19:19:54 +00:00
|
|
|
|
2019-02-10 13:45:14 +00:00
|
|
|
Logger::log('removed securemail');
|
2017-04-11 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 07:13:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Build user settings form
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
|
2017-04-14 07:13:19 +00:00
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @param App $a App instance
|
2017-04-14 07:13:19 +00:00
|
|
|
* @param string $s output html
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @see App
|
2017-04-14 07:13:19 +00:00
|
|
|
*/
|
2019-02-10 13:45:14 +00:00
|
|
|
function securemail_settings(App &$a, &$s)
|
|
|
|
{
|
|
|
|
if (!local_user()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
|
|
|
|
$publickey = PConfig::get(local_user(), 'securemail', 'pkey');
|
|
|
|
|
|
|
|
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
|
|
|
|
|
|
|
|
$s .= Renderer::replaceMacros($t, [
|
|
|
|
'$title' => L10n::t('"Secure Mail" Settings'),
|
|
|
|
'$submit' => L10n::t('Save Settings'),
|
|
|
|
'$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
|
|
|
|
'$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''],
|
|
|
|
'$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
|
|
|
|
]);
|
2017-04-11 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 07:13:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Handle data from user settings form
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
|
2017-04-14 07:13:19 +00:00
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @param App $a App instance
|
2017-04-14 07:13:19 +00:00
|
|
|
* @param array $b hook data
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @see App
|
2017-04-14 07:13:19 +00:00
|
|
|
*/
|
2019-02-10 13:45:14 +00:00
|
|
|
function securemail_settings_post(App &$a, array &$b)
|
|
|
|
{
|
|
|
|
if (!local_user()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_POST['securemail-submit']) {
|
|
|
|
PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
|
|
|
|
$enable = (!empty($_POST['securemail-enable']) ? 1 : 0);
|
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', $enable);
|
|
|
|
info(L10n::t('Secure Mail Settings saved.') . EOL);
|
|
|
|
|
|
|
|
if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
|
|
|
|
$sitename = Config::get('config', 'sitename');
|
|
|
|
|
|
|
|
$hostname = $a->getHostName();
|
|
|
|
if (strpos($hostname, ':')) {
|
|
|
|
$hostname = substr($hostname, 0, strpos($hostname, ':'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$sender_email = Config::get('config', 'sender_email');
|
|
|
|
if (empty($sender_email)) {
|
|
|
|
$sender_email = 'noreply@' . $hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject = 'Friendica - Secure Mail - Test';
|
|
|
|
$message = 'This is a test message from your Friendica Secure Mail addon.';
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'uid' => local_user(),
|
|
|
|
'fromName' => $sitename,
|
|
|
|
'fromEmail' => $sender_email,
|
|
|
|
'toEmail' => $a->user['email'],
|
|
|
|
'messageSubject' => $subject,
|
|
|
|
'htmlVersion' => "<p>{$message}</p>",
|
|
|
|
'textVersion' => $message,
|
|
|
|
];
|
|
|
|
|
|
|
|
// enable addon for test
|
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', 1);
|
|
|
|
|
|
|
|
$res = Emailer::send($params);
|
|
|
|
|
|
|
|
// revert to saved value
|
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', $enable);
|
|
|
|
|
|
|
|
if ($res) {
|
|
|
|
info(L10n::t('Test email sent') . EOL);
|
|
|
|
} else {
|
|
|
|
notice(L10n::t('There was an error sending the test email') . EOL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 07:13:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Encrypt notification emails text
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
|
2017-04-14 07:13:19 +00:00
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @param App $a App instance
|
2017-04-14 07:13:19 +00:00
|
|
|
* @param array $b hook data
|
|
|
|
*
|
2019-02-10 13:45:14 +00:00
|
|
|
* @see App
|
2017-04-14 07:13:19 +00:00
|
|
|
*/
|
2019-02-10 13:45:14 +00:00
|
|
|
function securemail_emailer_send_prepare(App &$a, array &$b)
|
|
|
|
{
|
|
|
|
if (empty($b['uid'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = $b['uid'];
|
|
|
|
|
|
|
|
$enable_checked = PConfig::get($uid, 'securemail', 'enable');
|
|
|
|
if (!$enable_checked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
|
|
|
|
|
|
|
|
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($b['textVersion'], [
|
|
|
|
'format' => 'u',
|
|
|
|
'filename' => 'encrypted.gpg'
|
|
|
|
]);
|
|
|
|
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
|
|
|
|
$armored_encrypted = wordwrap(
|
|
|
|
OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
|
|
|
|
64,
|
|
|
|
"\n",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$b['textVersion'] = $armored_encrypted;
|
|
|
|
$b['htmlVersion'] = null;
|
2017-04-11 19:19:54 +00:00
|
|
|
}
|