[securemail] Fix formatting

This commit is contained in:
Hypolite Petovan 2019-02-10 08:45:14 -05:00
parent af672ecd1b
commit a465e7dcd9

View file

@ -25,189 +25,164 @@ set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'openpgp.php'; require_once 'openpgp.php';
require_once 'openpgp_crypt_symmetric.php'; require_once 'openpgp_crypt_symmetric.php';
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');
Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
function securemail_install() { Logger::log('installed securemail');
Hook::register('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
Hook::register('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
Logger::log('installed securemail');
} }
function securemail_uninstall() { 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'); Hook::unregister('addon_settings', 'addon/securemail/securemail.php', 'securemail_settings');
Hook::unregister('addon_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post');
Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare'); Hook::unregister('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare');
Logger::log('removed securemail'); Logger::log('removed securemail');
} }
/** /**
* @brief Build user settings form * @brief Build user settings form
* *
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook
* *
* @param App $a App instance * @param App $a App instance
* @param string $s output html * @param string $s output html
* *
* @see App * @see App
*/ */
function securemail_settings(App &$a, &$s){ function securemail_settings(App &$a, &$s)
if (!local_user()) { {
return; if (!local_user()) {
} return;
}
$enable = intval(PConfig::get(local_user(), 'securemail', 'enable')); $enable = intval(PConfig::get(local_user(), 'securemail', 'enable'));
$publickey = PConfig::get(local_user(), 'securemail', 'pkey'); $publickey = PConfig::get(local_user(), 'securemail', 'pkey');
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/'); $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/securemail/');
$s .= Renderer::replaceMacros($t, [ $s .= Renderer::replaceMacros($t, [
'$title' => L10n::t('"Secure Mail" Settings'), '$title' => L10n::t('"Secure Mail" Settings'),
'$submit' => L10n::t('Save Settings'), '$submit' => L10n::t('Save Settings'),
'$test' => L10n::t('Save and send test'), //NOTE: update also in 'post' '$test' => L10n::t('Save and send test'), //NOTE: update also in 'post'
'$enable' => ['securemail-enable', L10n::t('Enable Secure Mail'), $enable, ''], '$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"'] '$publickey' => ['securemail-pkey', L10n::t('Public key'), $publickey, L10n::t('Your public PGP key, ascii armored format'), 'rows="10"']
]); ]);
} }
/** /**
* @brief Handle data from user settings form * @brief Handle data from user settings form
* *
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings_post 'addon_settings_post' hook
* *
* @param App $a App instance * @param App $a App instance
* @param array $b hook data * @param array $b hook data
* *
* @see App * @see App
*/ */
function securemail_settings_post(App &$a, array &$b){ function securemail_settings_post(App &$a, array &$b)
{
if (!local_user()) {
return;
}
if (!local_user()) { if ($_POST['securemail-submit']) {
return; 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']) { if ($_POST['securemail-submit'] == L10n::t('Save and send test')) {
PConfig::set(local_user(), 'securemail', 'pkey', trim($_POST['securemail-pkey'])); $sitename = Config::get('config', 'sitename');
$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')) { $hostname = $a->getHostName();
$sitename = Config::get('config', 'sitename'); if (strpos($hostname, ':')) {
$hostname = substr($hostname, 0, strpos($hostname, ':'));
}
$hostname = $a->getHostName(); $sender_email = Config::get('config', 'sender_email');
if (strpos($hostname, ':')) { if (empty($sender_email)) {
$hostname = substr($hostname, 0, strpos($hostname, ':')); $sender_email = 'noreply@' . $hostname;
} }
$sender_email = Config::get('config', 'sender_email'); $subject = 'Friendica - Secure Mail - Test';
if (empty($sender_email)) { $message = 'This is a test message from your Friendica Secure Mail addon.';
$sender_email = 'noreply@' . $hostname;
}
$subject = 'Friendica - Secure Mail - Test'; $params = [
$message = 'This is a test message from your Friendica Secure Mail addon.'; 'uid' => local_user(),
'fromName' => $sitename,
'fromEmail' => $sender_email,
'toEmail' => $a->user['email'],
'messageSubject' => $subject,
'htmlVersion' => "<p>{$message}</p>",
'textVersion' => $message,
];
$params = [ // enable addon for test
'uid' => local_user(), PConfig::set(local_user(), 'securemail', 'enable', 1);
'fromName' => $sitename,
'fromEmail' => $sender_email,
'toEmail' => $a->user['email'],
'messageSubject' => $subject,
'htmlVersion' => "<p>{$message}</p>",
'textVersion' => $message,
];
// enable addon for test $res = Emailer::send($params);
PConfig::set(local_user(), 'securemail', 'enable', 1);
$res = Emailer::send($params); // revert to saved value
PConfig::set(local_user(), 'securemail', 'enable', $enable);
// revert to saved value if ($res) {
PConfig::set(local_user(), 'securemail', 'enable', $enable); info(L10n::t('Test email sent') . EOL);
} else {
if ($res) { notice(L10n::t('There was an error sending the test email') . EOL);
info(L10n::t('Test email sent') . EOL); }
} else { }
notice(L10n::t('There was an error sending the test email') . EOL); }
}
}
}
} }
/** /**
* @brief Encrypt notification emails text * @brief Encrypt notification emails text
* *
* @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#emailer_send_prepare 'emailer_send_prepare' hook
* *
* @param App $a App instance * @param App $a App instance
* @param array $b hook data * @param array $b hook data
* *
* @see App * @see App
*/ */
function securemail_emailer_send_prepare(App &$a, array &$b) { function securemail_emailer_send_prepare(App &$a, array &$b)
if (empty($b['uid'])) { {
return; if (empty($b['uid'])) {
} return;
}
$uid = $b['uid']; $uid = $b['uid'];
$enable_checked = PConfig::get($uid, 'securemail', 'enable'); $enable_checked = PConfig::get($uid, 'securemail', 'enable');
if (!$enable_checked) { if (!$enable_checked) {
return; return;
} }
$public_key_ascii = PConfig::get($uid, 'securemail', 'pkey'); $public_key_ascii = PConfig::get($uid, 'securemail', 'pkey');
preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches); preg_match('/-----BEGIN ([A-Za-z ]+)-----/', $public_key_ascii, $matches);
$marker = empty($matches[1]) ? 'MESSAGE' : $matches[1]; $marker = empty($matches[1]) ? 'MESSAGE' : $matches[1];
$public_key = OpenPGP::unarmor($public_key_ascii, $marker); $public_key = OpenPGP::unarmor($public_key_ascii, $marker);
$key = OpenPGP_Message::parse($public_key); $key = OpenPGP_Message::parse($public_key);
$data = new OpenPGP_LiteralDataPacket($b['textVersion'], [ $data = new OpenPGP_LiteralDataPacket($b['textVersion'], [
'format' => 'u', 'format' => 'u',
'filename' => 'encrypted.gpg' 'filename' => 'encrypted.gpg'
]); ]);
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data])); $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data]));
$armored_encrypted = wordwrap( $armored_encrypted = wordwrap(
OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'), OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'),
64, 64,
"\n", "\n",
true true
); );
$b['textVersion'] = $armored_encrypted; $b['textVersion'] = $armored_encrypted;
$b['htmlVersion'] = null; $b['htmlVersion'] = null;
} }
/**
* add addon composer autoloader maps to system autoloader
function securemail_autoloader() {
$loader = require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
$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();
*/