X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=securemail%2Fsecuremail.php;h=5390ebd14523082876c08e5fdf38fb0f4abc866f;hb=90d897f4fab3f2c2fb7bb01daebdb58fffc710a1;hp=953766962092a5472ad0656de5b49624d8572fb7;hpb=42eefed6e68d241bd8bdf9eceea96abc445a9cac;p=friendica-addons.git diff --git a/securemail/securemail.php b/securemail/securemail.php index 95376696..5390ebd1 100644 --- a/securemail/securemail.php +++ b/securemail/securemail.php @@ -6,204 +6,144 @@ * Author: Fabio Comuni */ +use Friendica\Addon\securemail\SecureTestEmail; use Friendica\App; -use Friendica\Core\PConfig; +use Friendica\Core\Hook; +use Friendica\Core\Logger; +use Friendica\Core\Renderer; +use Friendica\DI; +use Friendica\Object\EMail\IEmail; -require_once 'include/Emailer.php'; +require_once __DIR__ . '/vendor/autoload.php'; -/* because the fraking openpgp-php is in composer, require libs in composer - * and then don't use autoloader to load classes... */ -$path = __DIR__ . '/vendor/phpseclib/phpseclib/phpseclib/'; -set_include_path(get_include_path() . PATH_SEPARATOR . $path); -/* so, we don't use the autoloader and include what we need */ -$path = __DIR__ . '/vendor/singpolyma/openpgp-php/lib'; -set_include_path(get_include_path() . PATH_SEPARATOR . $path); +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', 10); -require_once 'openpgp.php'; -require_once 'openpgp_crypt_symmetric.php'; + Hook::register('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare', 10); - -function securemail_install() { - register_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings'); - register_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post'); - - register_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare'); - - logger('installed securemail'); -} - -function securemail_uninstall() { - unregister_hook('plugin_settings', 'addon/securemail/securemail.php', 'securemail_settings'); - unregister_hook('plugin_settings_post', 'addon/securemail/securemail.php', 'securemail_settings_post'); - - unregister_hook('emailer_send_prepare', 'addon/securemail/securemail.php', 'securemail_emailer_send_prepare'); - - logger('removed securemail'); + Logger::notice('installed securemail'); } /** * @brief Build user settings form * - * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings 'plugin_settings' hook + * @link https://github.com/friendica/friendica/blob/develop/doc/Addons.md#addon_settings 'addon_settings' hook * - * @param App $a App instance - * @param string $s output html + * @param array $data * - * @see App + * @see App */ -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 = get_markup_template('admin.tpl', 'addon/securemail/'); - - $s .= replace_macros($t, array( - '$title' => t('"Secure Mail" Settings'), - '$submit' => t('Save Settings'), - '$test' => t('Save and send test'), //NOTE: update also in 'post' - '$enable' => array('securemail-enable', t('Enable Secure Mail'), $enable, ''), - '$publickey' => array('securemail-pkey', t('Public key'), $publickey, t('Your public PGP key, ascii armored format'), 'rows="10"') - )); +function securemail_settings(array &$data) +{ + if (!DI::userSession()->getLocalUserId()) { + return; + } + + $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'enable')); + $publickey = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'securemail', 'pkey'); + + $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/securemail/'); + $html = Renderer::replaceMacros($t, [ + '$enabled' => ['securemail-enable', DI::l10n()->t('Enable Secure Mail'), $enabled], + '$publickey' => ['securemail-pkey', DI::l10n()->t('Public key'), $publickey, DI::l10n()->t('Your public PGP key, ascii armored format')] + ]); + + $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'), + ], + ]; } /** * @brief Handle data from user settings form * - * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.md#plugin_settings_post 'plugin_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 array $b hook data * - * @see App + * @see App */ -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 = ((x($_POST, 'securemail-enable')) ? 1 : 0); - PConfig::set(local_user(), 'securemail', 'enable', $enable); - info(t('Secure Mail Settings saved.') . EOL); - - if ($_POST['securemail-submit'] == t('Save and send test')) { - $sitename = $a->config['sitename']; - - $hostname = $a->get_hostname(); - if (strpos($hostname, ':')) { - $hostname = substr($hostname, 0, strpos($hostname, ':')); - } - - $sender_email = $a->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 = array( - 'uid' => local_user(), - 'fromName' => $sitename, - 'fromEmail' => $sender_email, - 'toEmail' => $a->user['email'], - 'messageSubject' => $subject, - 'htmlVersion' => "

{$message}

", - '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(t('Test email sent') . EOL); - } else { - notice(t('There was an error sending the test email') . EOL); - } - } - } +function securemail_settings_post(array &$b) +{ + if (!DI::userSession()->getLocalUserId()) { + return; + } + + if (!empty($_POST['securemail-submit']) || !empty($_POST['securemail-test'])) { + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'pkey', trim($_POST['securemail-pkey'])); + $enable = (!empty($_POST['securemail-enable']) ? 1 : 0); + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable); + + if (!empty($_POST['securemail-test'])) { + $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl())); + + // revert to saved value + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'securemail', 'enable', $enable); + + if ($res) { + DI::sysmsg()->addInfo(DI::l10n()->t('Test email sent')); + } else { + DI::sysmsg()->addNotice(DI::l10n()->t('There was an error sending the test email')); + } + } + } } /** * @brief Encrypt notification emails text * - * @link https://github.com/friendica/friendica/blob/develop/doc/Plugins.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 array $b hook data + * @param IEmail $email Email * - * @see App + * @see App */ -function securemail_emailer_send_prepare(App &$a, array &$b) { - if (!x($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'], array( - 'format' => 'u', - 'filename' => 'encrypted.gpg' - )); - $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data))); - $armored_encrypted = wordwrap( - OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'), - 64, - "\n", - true - ); - - $b['textVersion'] = $armored_encrypted; - $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); - } +function securemail_emailer_send_prepare(IEmail &$email) +{ + if (empty($email->getRecipientUid())) { + return; + } + + $uid = $email->getRecipientUid(); + + $enable_checked = DI::pConfig()->get($uid, 'securemail', 'enable'); + if (!$enable_checked) { + DI::logger()->debug('No securemail enabled.'); + return; + } + + $public_key_ascii = DI::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($email->getMessage(true), [ + 'format' => 'u', + 'filename' => 'encrypted.gpg' + ]); + + try { + $encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message([$data])); + $armored_encrypted = wordwrap( + OpenPGP::enarmor($encrypted->to_bytes(), 'PGP MESSAGE'), + 64, + "\n", + true + ); + + $email = $email->withMessage($armored_encrypted, null); + + } catch (Exception $e) { + DI::logger()->warning('Encryption failed.', ['email' => $email, 'exception' => $e]); + } } -securemail_autoloader(); - -*/