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>
|
|
|
|
*/
|
|
|
|
|
2017-05-22 02:47:36 -04:00
|
|
|
use Friendica\App;
|
2018-01-16 19:51:12 -05:00
|
|
|
use Friendica\Core\Addon;
|
2017-11-06 18:55:24 -05:00
|
|
|
use Friendica\Core\PConfig;
|
2017-11-22 18:10:01 -05:00
|
|
|
use Friendica\Util\Emailer;
|
2017-04-11 15:19:54 -04: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 03:13:19 -04:00
|
|
|
$path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/';
|
2017-04-11 15:19:54 -04: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 03:13:19 -04:00
|
|
|
$path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib';
|
2017-04-11 15:19:54 -04:00
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
require_once 'openpgp.php';
|
|
|
|
require_once 'openpgp_crypt_symmetric.php';
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
function securemail_install() {
|
2018-01-20 08:57:41 -05:00
|
|
|
Addon::registerHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
|
|
|
|
Addon::registerHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2018-01-16 19:51:12 -05:00
|
|
|
Addon::registerHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
logger('installed securemail');
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function securemail_uninstall() {
|
2018-01-20 08:57:41 -05:00
|
|
|
Addon::unregisterHook('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
|
|
|
|
Addon::unregisterHook('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2018-01-16 19:51:12 -05:00
|
|
|
Addon::unregisterHook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
logger('removed securemail');
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Build user settings form
|
|
|
|
*
|
2018-01-20 08:57:41 -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
|
|
|
*
|
|
|
|
* @param App $a App instance
|
|
|
|
* @param string $s output html
|
|
|
|
*
|
|
|
|
* @see App
|
|
|
|
*/
|
2017-04-14 10:13:49 -04:00
|
|
|
function securemail_settings(App &$a, &$s){
|
2017-04-14 03:13:19 -04:00
|
|
|
if (!local_user()) {
|
2017-04-11 15:19:54 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:55:24 -05:00
|
|
|
$enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
|
|
|
|
$publickey = PConfig::get(local_user(), 'securemail', 'pkey');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
$t = get_markup_template('admin.tpl', 'addon/securemail/');
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2018-01-15 08:15:33 -05:00
|
|
|
$s .= replace_macros($t, [
|
2017-04-11 15:19:54 -04:00
|
|
|
'$title' => t('"Secure Mail" Settings'),
|
|
|
|
'$submit' => t('Save Settings'),
|
|
|
|
'$test' => t('Save and send test'), //NOTE: update also in 'post'
|
2018-01-15 08:15:33 -05:00
|
|
|
'$enable' => ['securemail-enable', t('Enable Secure Mail'), $enable, ''],
|
|
|
|
'$publickey' => ['securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"']
|
|
|
|
]);
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Handle data from user settings form
|
|
|
|
*
|
2018-01-20 08:57:41 -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
|
|
|
*
|
|
|
|
* @param App $a App instance
|
|
|
|
* @param array $b hook data
|
|
|
|
*
|
|
|
|
* @see App
|
|
|
|
*/
|
|
|
|
function securemail_settings_post(App &$a, array &$b){
|
|
|
|
|
|
|
|
if (!local_user()) {
|
2017-04-11 15:19:54 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
if ($_POST['securemail-submit']) {
|
2017-11-06 18:55:24 -05:00
|
|
|
PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey']));
|
2017-04-14 03:13:19 -04:00
|
|
|
$enable = ((x($_POST, 'securemail-enable')) ? 1 : 0);
|
2017-11-06 18:55:24 -05:00
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', $enable);
|
2017-04-14 03:13:19 -04:00
|
|
|
info(t('Secure Mail Settings saved.') . EOL);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
if ($_POST['securemail-submit'] == t('Save and send test')) {
|
|
|
|
$sitename = $a->config['sitename'];
|
|
|
|
|
|
|
|
$hostname = $a->get_hostname();
|
2017-04-14 03:13:19 -04:00
|
|
|
if (strpos($hostname, ':')) {
|
2017-04-11 15:19:54 -04:00
|
|
|
$hostname = substr($hostname, 0, strpos($hostname, ':'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$sender_email = $a->config['sender_email'];
|
2017-04-14 03:13:19 -04:00
|
|
|
if (empty($sender_email)) {
|
|
|
|
$sender_email = 'noreply@' . $hostname;
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
$subject = 'Friendica - Secure Mail - Test';
|
2017-04-14 10:13:49 -04:00
|
|
|
$message = 'This is a test message from your Friendica Secure Mail addon.';
|
2017-04-11 15:19:54 -04:00
|
|
|
|
2018-01-15 08:15:33 -05:00
|
|
|
$params = [
|
2017-04-11 15:19:54 -04:00
|
|
|
'uid' => local_user(),
|
|
|
|
'fromName' => $sitename,
|
|
|
|
'fromEmail' => $sender_email,
|
|
|
|
'toEmail' => $a->user['email'],
|
|
|
|
'messageSubject' => $subject,
|
|
|
|
'htmlVersion' => "<p>{$message}</p>",
|
|
|
|
'textVersion' => $message,
|
2018-01-15 08:15:33 -05:00
|
|
|
];
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
// enable addon for test
|
2017-11-06 18:55:24 -05:00
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', 1);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
$res = Emailer::send($params);
|
|
|
|
|
|
|
|
// revert to saved value
|
2017-11-06 18:55:24 -05:00
|
|
|
PConfig::set(local_user(), 'securemail', 'enable', $enable);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
if ($res) {
|
2017-04-14 03:13:19 -04:00
|
|
|
info(t('Test email sent') . EOL);
|
2017-04-11 15:19:54 -04:00
|
|
|
} else {
|
2017-04-14 03:13:19 -04:00
|
|
|
notice(t('There was an error sending the test email') . EOL);
|
2017-04-11 15:19:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
/**
|
|
|
|
* @brief Encrypt notification emails text
|
|
|
|
*
|
2018-01-20 08:57:41 -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
|
|
|
*
|
|
|
|
* @param App $a App instance
|
|
|
|
* @param array $b hook data
|
|
|
|
*
|
|
|
|
* @see App
|
|
|
|
*/
|
|
|
|
function securemail_emailer_send_prepare(App &$a, array &$b) {
|
2017-04-14 10:13:49 -04:00
|
|
|
if (!x($b, 'uid')) {
|
2017-04-11 15:19:54 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = $b['uid'];
|
|
|
|
|
2017-11-06 18:55:24 -05:00
|
|
|
$enable_checked = PConfig::get($uid, 'securemail', 'enable');
|
2017-04-11 15:19:54 -04:00
|
|
|
if (!$enable_checked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:55:24 -05:00
|
|
|
$public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
|
2017-04-11 15:19:54 -04: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);
|
|
|
|
|
2018-01-15 08:15:33 -05:00
|
|
|
$data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
|
2017-04-11 15:19:54 -04:00
|
|
|
'format' => 'u',
|
|
|
|
'filename' => 'encrypted.gpg'
|
2018-01-15 08:15:33 -05:00
|
|
|
]);
|
|
|
|
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
|
2017-04-14 03:13:19 -04:00
|
|
|
$armored_encrypted = wordwrap(
|
|
|
|
OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
|
|
|
|
64,
|
2017-04-14 10:13:49 -04:00
|
|
|
"\n",
|
2017-04-14 03:13:19 -04:00
|
|
|
true
|
|
|
|
);
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
$b['textVersion'] = $armored_encrypted;
|
|
|
|
$b['htmlVersion'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add addon composer autoloader maps to system autoloader
|
|
|
|
|
|
|
|
function securemail_autoloader() {
|
|
|
|
|
2017-04-14 03:13:19 -04:00
|
|
|
$loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
|
2017-04-11 15:19:54 -04:00
|
|
|
|
|
|
|
$map = require __DIR__ . '/vendor/composer/autoload_namespaces.php';
|
|
|
|
foreach ($map as $namespace => $path) {
|
|
|
|
$loader->set($namespace, $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$map = require __DIR__ . '/vendor/composer/autoload_psr4.php';
|
|
|
|
foreach ($map as $namespace => $path) {
|
|
|
|
$loader->setPsr4($namespace, $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$classMap = require __DIR__ . '/vendor/composer/autoload_classmap.php';
|
|
|
|
if ($classMap) {
|
|
|
|
$loader->addClassMap($classMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
securemail_autoloader();
|
|
|
|
|
|
|
|
*/
|